|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Contributors to the Eclipse Foundation |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * and Apache License v2.0 which accompanies this distribution. |
| 6 | + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. |
| 8 | + * |
| 9 | + * You may elect to redistribute this code under either of these licenses. |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * |
| 13 | + * Otavio Santana |
| 14 | + */ |
| 15 | +package org.eclipse.jnosql.databases.tinkerpop.mapping; |
| 16 | + |
| 17 | +import jakarta.data.exceptions.EmptyResultException; |
| 18 | +import jakarta.inject.Inject; |
| 19 | +import org.eclipse.jnosql.communication.Value; |
| 20 | +import org.eclipse.jnosql.communication.semistructured.Element; |
| 21 | +import org.eclipse.jnosql.databases.tinkerpop.mapping.entities.Human; |
| 22 | +import org.eclipse.jnosql.databases.tinkerpop.mapping.entities.Magazine; |
| 23 | +import org.eclipse.jnosql.databases.tinkerpop.mapping.spi.GraphExtension; |
| 24 | +import org.eclipse.jnosql.mapping.core.Converters; |
| 25 | +import org.eclipse.jnosql.mapping.core.spi.EntityMetadataExtension; |
| 26 | +import org.eclipse.jnosql.mapping.reflection.Reflections; |
| 27 | +import org.eclipse.jnosql.mapping.semistructured.EntityConverter; |
| 28 | +import org.jboss.weld.junit5.auto.AddExtensions; |
| 29 | +import org.jboss.weld.junit5.auto.AddPackages; |
| 30 | +import org.jboss.weld.junit5.auto.EnableAutoWeld; |
| 31 | +import org.junit.jupiter.api.Assertions; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | + |
| 34 | +import java.util.Optional; |
| 35 | + |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 38 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 39 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 40 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 41 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 42 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 43 | + |
| 44 | +@EnableAutoWeld |
| 45 | +@AddPackages(value = {Converters.class, EntityConverter.class, TinkerpopTemplate.class}) |
| 46 | +@AddPackages(GraphProducer.class) |
| 47 | +@AddPackages(Reflections.class) |
| 48 | +@AddExtensions({EntityMetadataExtension.class, GraphExtension.class}) |
| 49 | +class EdgeEntityTest { |
| 50 | + |
| 51 | + |
| 52 | + @Inject |
| 53 | + private TinkerpopTemplate tinkerpopTemplate; |
| 54 | + |
| 55 | + |
| 56 | + @Test |
| 57 | + void shouldReturnErrorWhenInboundIsNull() { |
| 58 | + Assertions.assertThrows(NullPointerException.class, () -> { |
| 59 | + Human human = Human.builder().withName("Poliana").withAge().build(); |
| 60 | + Magazine magazine = null; |
| 61 | + tinkerpopTemplate.edge(human, "reads", magazine); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void shouldReturnErrorWhenOutboundIsNull() { |
| 67 | + Assertions.assertThrows(IllegalStateException.class, () -> { |
| 68 | + Human human = Human.builder().withName("Poliana").withAge().build(); |
| 69 | + Magazine magazine = Magazine.builder().withAge(2007).withName("The Shack").build(); |
| 70 | + tinkerpopTemplate.edge(human, "reads", magazine); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void shouldReturnErrorWhenLabelIsNull() { |
| 76 | + Assertions.assertThrows(NullPointerException.class, () -> { |
| 77 | + Human human = Human.builder().withName("Poliana").withAge().build(); |
| 78 | + Magazine magazine = Magazine.builder().withAge(2007).withName("The Shack").build(); |
| 79 | + tinkerpopTemplate.edge(human, (String) null, magazine); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void shouldReturnNullWhenInboundIdIsNull() { |
| 85 | + Assertions.assertThrows(EmptyResultException.class, () -> { |
| 86 | + Human human = Human.builder().withId(-5).withName("Poliana").withAge().build(); |
| 87 | + Magazine magazine = tinkerpopTemplate.insert(Magazine.builder().withAge(2007).withName("The Shack").build()); |
| 88 | + tinkerpopTemplate.edge(human, "reads", magazine); |
| 89 | + }); |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void shouldReturnNullWhenOutboundIdIsNull() { |
| 95 | + Assertions.assertThrows(IllegalStateException.class, () -> { |
| 96 | + Human human = tinkerpopTemplate.insert(Human.builder().withName("Poliana").withAge().build()); |
| 97 | + Magazine magazine = Magazine.builder().withAge(2007).withName("The Shack").build(); |
| 98 | + tinkerpopTemplate.edge(human, "reads", magazine); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void shouldReturnEntityNotFoundWhenOutBoundDidNotFound() { |
| 104 | + Assertions.assertThrows( EmptyResultException.class, () -> { |
| 105 | + Human human = Human.builder().withId(-10L).withName("Poliana").withAge().build(); |
| 106 | + Magazine magazine = tinkerpopTemplate.insert(Magazine.builder().withAge(2007).withName("The Shack").build()); |
| 107 | + tinkerpopTemplate.edge(human, "reads", magazine); |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void shouldReturnEntityNotFoundWhenInBoundDidNotFound() { |
| 113 | + Assertions.assertThrows( EmptyResultException.class, () -> { |
| 114 | + Human human = tinkerpopTemplate.insert(Human.builder().withName("Poliana").withAge().build()); |
| 115 | + Magazine magazine = Magazine.builder().withId(10L).withAge(2007).withName("The Shack").build(); |
| 116 | + tinkerpopTemplate.edge(human, "reads", magazine); |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void shouldCreateAnEdge() { |
| 122 | + Human human = tinkerpopTemplate.insert(Human.builder().withName("Poliana").withAge().build()); |
| 123 | + Magazine magazine = tinkerpopTemplate.insert(Magazine.builder().withAge(2007).withName("The Shack").build()); |
| 124 | + EdgeEntity edge = tinkerpopTemplate.edge(human, "reads", magazine); |
| 125 | + |
| 126 | + assertEquals("reads", edge.label()); |
| 127 | + assertEquals(human, edge.outgoing()); |
| 128 | + assertEquals(magazine, edge.incoming()); |
| 129 | + assertTrue(edge.isEmpty()); |
| 130 | + assertNotNull(edge.id()); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void shouldGetId() { |
| 135 | + Human human = tinkerpopTemplate.insert(Human.builder().withName("Poliana").withAge().build()); |
| 136 | + Magazine magazine = tinkerpopTemplate.insert(Magazine.builder().withAge(2007).withName("The Shack").build()); |
| 137 | + EdgeEntity edge = tinkerpopTemplate.edge(human, "reads", magazine); |
| 138 | + |
| 139 | + assertEquals("reads", edge.label()); |
| 140 | + assertEquals(human, edge.outgoing()); |
| 141 | + assertEquals(magazine, edge.incoming()); |
| 142 | + assertTrue(edge.isEmpty()); |
| 143 | + assertNotNull(edge.id()); |
| 144 | + final Long id = edge.id(Long.class); |
| 145 | + assertNotNull(id); |
| 146 | + |
| 147 | + assertEquals(id, edge.id(Integer.class).longValue()); |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + void shouldCreateAnEdgeWithSupplier() { |
| 153 | + Human human = tinkerpopTemplate.insert(Human.builder().withName("Poliana").withAge().build()); |
| 154 | + Magazine magazine = tinkerpopTemplate.insert(Magazine.builder().withAge(2007).withName("The Shack").build()); |
| 155 | + EdgeEntity edge = tinkerpopTemplate.edge(human, () -> "reads", magazine); |
| 156 | + |
| 157 | + assertEquals("reads", edge.label()); |
| 158 | + assertEquals(human, edge.outgoing()); |
| 159 | + assertEquals(magazine, edge.incoming()); |
| 160 | + assertTrue(edge.isEmpty()); |
| 161 | + assertNotNull(edge.id()); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + void shouldUseAnEdge() { |
| 166 | + Human human = tinkerpopTemplate.insert(Human.builder().withName("Poliana").withAge().build()); |
| 167 | + Magazine magazine = tinkerpopTemplate.insert(Magazine.builder().withAge(2007).withName("The Shack").build()); |
| 168 | + EdgeEntity edge = tinkerpopTemplate.edge(human, "reads", magazine); |
| 169 | + |
| 170 | + EdgeEntity sameEdge = tinkerpopTemplate.edge(human, "reads", magazine); |
| 171 | + |
| 172 | + assertEquals(edge.id(), sameEdge.id()); |
| 173 | + assertEquals(edge, sameEdge); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + void shouldUseAnEdge2() { |
| 178 | + Human poliana = tinkerpopTemplate.insert(Human.builder().withName("Poliana").withAge().build()); |
| 179 | + Human nilzete = tinkerpopTemplate.insert(Human.builder().withName("Nilzete").withAge().build()); |
| 180 | + |
| 181 | + Magazine magazine = tinkerpopTemplate.insert(Magazine.builder().withAge(2007).withName("The Shack").build()); |
| 182 | + EdgeEntity edge = tinkerpopTemplate.edge(poliana, "reads", magazine); |
| 183 | + EdgeEntity edge1 = tinkerpopTemplate.edge(nilzete, "reads", magazine); |
| 184 | + |
| 185 | + EdgeEntity sameEdge = tinkerpopTemplate.edge(poliana, "reads", magazine); |
| 186 | + EdgeEntity sameEdge1 = tinkerpopTemplate.edge(nilzete, "reads", magazine); |
| 187 | + |
| 188 | + assertEquals(edge.id(), sameEdge.id()); |
| 189 | + assertEquals(edge, sameEdge); |
| 190 | + |
| 191 | + assertEquals(edge1.id(), sameEdge1.id()); |
| 192 | + assertEquals(edge1, sameEdge1); |
| 193 | + |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + void shouldUseADifferentEdge() { |
| 198 | + Human poliana = tinkerpopTemplate.insert(Human.builder().withName("Poliana").withAge().build()); |
| 199 | + Human nilzete = tinkerpopTemplate.insert(Human.builder().withName("Nilzete").withAge().build()); |
| 200 | + |
| 201 | + Magazine magazine = tinkerpopTemplate.insert(Magazine.builder().withAge(2007).withName("The Shack").build()); |
| 202 | + EdgeEntity edge = tinkerpopTemplate.edge(poliana, "reads", magazine); |
| 203 | + EdgeEntity edge1 = tinkerpopTemplate.edge(nilzete, "reads", magazine); |
| 204 | + |
| 205 | + EdgeEntity sameEdge = tinkerpopTemplate.edge(poliana, "reads", magazine); |
| 206 | + EdgeEntity sameEdge1 = tinkerpopTemplate.edge(nilzete, "reads", magazine); |
| 207 | + |
| 208 | + assertNotEquals(edge.id(), edge1.id()); |
| 209 | + assertNotEquals(edge.id(), sameEdge1.id()); |
| 210 | + |
| 211 | + assertNotEquals(sameEdge1.id(), sameEdge.id()); |
| 212 | + } |
| 213 | + |
| 214 | + @Test |
| 215 | + void shouldReturnErrorWhenAddKeyIsNull() { |
| 216 | + assertThrows(NullPointerException.class, () -> { |
| 217 | + Human human = tinkerpopTemplate.insert(Human.builder().withName("Poliana").withAge().build()); |
| 218 | + Magazine magazine = tinkerpopTemplate.insert(Magazine.builder().withAge(2007).withName("The Shack").build()); |
| 219 | + EdgeEntity edge = tinkerpopTemplate.edge(human, "reads", magazine); |
| 220 | + edge.add(null, "Brazil"); |
| 221 | + }); |
| 222 | + } |
| 223 | + |
| 224 | + @Test |
| 225 | + void shouldReturnErrorWhenAddValueIsNull() { |
| 226 | + |
| 227 | + assertThrows(NullPointerException.class, () -> { |
| 228 | + Human human = tinkerpopTemplate.insert(Human.builder().withName("Poliana").withAge().build()); |
| 229 | + Magazine magazine = tinkerpopTemplate.insert(Magazine.builder().withAge(2007).withName("The Shack").build()); |
| 230 | + EdgeEntity edge = tinkerpopTemplate.edge(human, "reads", magazine); |
| 231 | + edge.add("where", null); |
| 232 | + }); |
| 233 | + } |
| 234 | + |
| 235 | + @Test |
| 236 | + void shouldAddProperty() { |
| 237 | + Human human = tinkerpopTemplate.insert(Human.builder().withName("Poliana").withAge().build()); |
| 238 | + Magazine magazine = tinkerpopTemplate.insert(Magazine.builder().withAge(2007).withName("The Shack").build()); |
| 239 | + EdgeEntity edge = tinkerpopTemplate.edge(human, "reads", magazine); |
| 240 | + edge.add("where", "Brazil"); |
| 241 | + |
| 242 | + assertFalse(edge.isEmpty()); |
| 243 | + assertEquals(1, edge.size()); |
| 244 | + assertThat(edge.properties()).contains(Element.of("where", "Brazil")); |
| 245 | + } |
| 246 | + |
| 247 | + @Test |
| 248 | + void shouldAddPropertyWithValue() { |
| 249 | + Human human = tinkerpopTemplate.insert(Human.builder().withName("Poliana").withAge().build()); |
| 250 | + Magazine magazine = tinkerpopTemplate.insert(Magazine.builder().withAge(2007).withName("The Shack").build()); |
| 251 | + EdgeEntity edge = tinkerpopTemplate.edge(human, "reads", magazine); |
| 252 | + edge.add("where", Value.of("Brazil")); |
| 253 | + |
| 254 | + assertFalse(edge.isEmpty()); |
| 255 | + assertEquals(1, edge.size()); |
| 256 | + assertThat(edge.properties()).contains(Element.of("where", "Brazil")); |
| 257 | + } |
| 258 | + |
| 259 | + |
| 260 | + @Test |
| 261 | + void shouldReturnErrorWhenRemoveNullKeyProperty() { |
| 262 | + assertThrows(NullPointerException.class, () -> { |
| 263 | + Human human = tinkerpopTemplate.insert(Human.builder().withName("Poliana").withAge().build()); |
| 264 | + Magazine magazine = tinkerpopTemplate.insert(Magazine.builder().withAge(2007).withName("The Shack").build()); |
| 265 | + EdgeEntity edge = tinkerpopTemplate.edge(human, "reads", magazine); |
| 266 | + edge.add("where", "Brazil"); |
| 267 | + |
| 268 | + |
| 269 | + assertFalse(edge.isEmpty()); |
| 270 | + edge.remove(null); |
| 271 | + }); |
| 272 | + } |
| 273 | + |
| 274 | + @Test |
| 275 | + void shouldRemoveProperty() { |
| 276 | + Human human = tinkerpopTemplate.insert(Human.builder().withName("Poliana").withAge().build()); |
| 277 | + Magazine magazine = tinkerpopTemplate.insert(Magazine.builder().withAge(2007).withName("The Shack").build()); |
| 278 | + EdgeEntity edge = tinkerpopTemplate.edge(human, "reads", magazine); |
| 279 | + edge.add("where", "Brazil"); |
| 280 | + assertEquals(1, edge.size()); |
| 281 | + assertFalse(edge.isEmpty()); |
| 282 | + edge.remove("where"); |
| 283 | + assertTrue(edge.isEmpty()); |
| 284 | + assertEquals(0, edge.size()); |
| 285 | + } |
| 286 | + |
| 287 | + @Test |
| 288 | + void shouldFindProperty() { |
| 289 | + Human human = tinkerpopTemplate.insert(Human.builder().withName("Poliana").withAge().build()); |
| 290 | + Magazine magazine = tinkerpopTemplate.insert(Magazine.builder().withAge(2007).withName("The Shack").build()); |
| 291 | + EdgeEntity edge = tinkerpopTemplate.edge(human, "reads", magazine); |
| 292 | + edge.add("where", "Brazil"); |
| 293 | + |
| 294 | + Optional<Value> where = edge.get("where"); |
| 295 | + assertTrue(where.isPresent()); |
| 296 | + assertEquals("Brazil", where.get().get()); |
| 297 | + assertFalse(edge.get("not").isPresent()); |
| 298 | + |
| 299 | + } |
| 300 | + |
| 301 | + @Test |
| 302 | + void shouldDeleteAnEdge() { |
| 303 | + Human human = tinkerpopTemplate.insert(Human.builder().withName("Poliana").withAge().build()); |
| 304 | + Magazine magazine = tinkerpopTemplate.insert(Magazine.builder().withAge(2007).withName("The Shack").build()); |
| 305 | + EdgeEntity edge = tinkerpopTemplate.edge(human, "reads", magazine); |
| 306 | + edge.delete(); |
| 307 | + |
| 308 | + EdgeEntity newEdge = tinkerpopTemplate.edge(human, "reads", magazine); |
| 309 | + assertNotEquals(edge.id(), newEdge.id()); |
| 310 | + |
| 311 | + tinkerpopTemplate.deleteEdge(newEdge.id()); |
| 312 | + } |
| 313 | + |
| 314 | + @Test |
| 315 | + void shouldReturnErrorWhenDeleteAnEdgeWithNull() { |
| 316 | + assertThrows(NullPointerException.class, () -> tinkerpopTemplate.delete((Iterable<Object>) null)); |
| 317 | + } |
| 318 | + |
| 319 | + @Test |
| 320 | + void shouldDeleteAnEdge2() { |
| 321 | + Human human = tinkerpopTemplate.insert(Human.builder().withName("Poliana").withAge().build()); |
| 322 | + Magazine magazine = tinkerpopTemplate.insert(Magazine.builder().withAge(2007).withName("The Shack").build()); |
| 323 | + |
| 324 | + EdgeEntity edge = tinkerpopTemplate.edge(human, "reads", magazine); |
| 325 | + |
| 326 | + tinkerpopTemplate.deleteEdge(edge.id()); |
| 327 | + |
| 328 | + EdgeEntity newEdge = tinkerpopTemplate.edge(human, "reads", magazine); |
| 329 | + assertNotEquals(edge.id(), newEdge.id()); |
| 330 | + } |
| 331 | + |
| 332 | + |
| 333 | + @Test |
| 334 | + void shouldReturnErrorWhenFindEdgeWithNull() { |
| 335 | + assertThrows(NullPointerException.class, () -> tinkerpopTemplate.edge(null)); |
| 336 | + } |
| 337 | + |
| 338 | + |
| 339 | + @Test |
| 340 | + void shouldFindAnEdge() { |
| 341 | + Human human = tinkerpopTemplate.insert(Human.builder().withName("Poliana").withAge().build()); |
| 342 | + Magazine magazine = tinkerpopTemplate.insert(Magazine.builder().withAge(2007).withName("The Shack").build()); |
| 343 | + EdgeEntity edge = tinkerpopTemplate.edge(human, "reads", magazine); |
| 344 | + |
| 345 | + Optional<EdgeEntity> newEdge = tinkerpopTemplate.edge(edge.id()); |
| 346 | + |
| 347 | + assertTrue(newEdge.isPresent()); |
| 348 | + assertEquals(edge.id(), newEdge.get().id()); |
| 349 | + |
| 350 | + tinkerpopTemplate.deleteEdge(edge.id()); |
| 351 | + } |
| 352 | + |
| 353 | + @Test |
| 354 | + void shouldNotFindAnEdge() { |
| 355 | + Optional<EdgeEntity> edgeEntity = tinkerpopTemplate.edge(-12L); |
| 356 | + |
| 357 | + assertFalse(edgeEntity.isPresent()); |
| 358 | + } |
| 359 | + |
| 360 | +} |
0 commit comments