|
17 | 17 |
|
18 | 18 | import org.junit.Before;
|
19 | 19 | import org.junit.Rule;
|
20 |
| - |
21 | 20 | import org.junit.Test;
|
22 | 21 | import org.junit.rules.ExpectedException;
|
23 | 22 | import org.junit.runner.RunWith;
|
|
36 | 35 | import java.time.Instant;
|
37 | 36 | import java.util.ArrayList;
|
38 | 37 | import java.util.Arrays;
|
| 38 | +import java.util.HashSet; |
39 | 39 | import java.util.List;
|
40 | 40 | import java.util.Optional;
|
| 41 | +import java.util.Set; |
41 | 42 | import java.util.concurrent.ExecutionException;
|
42 | 43 | import java.util.concurrent.Future;
|
43 | 44 | import java.util.concurrent.ThreadLocalRandom;
|
@@ -263,14 +264,163 @@ public void testFilterAndPagination() {
|
263 | 264 |
|
264 | 265 | @Test
|
265 | 266 | public void testDeleteNonExistentIdWithCondition() {
|
266 |
| - // Delete conditional |
267 |
| - userRepository.deleteByIdAndName("non-existent", "non-existent"); |
268 |
| - } |
| 267 | + // Delete conditional |
| 268 | + userRepository.deleteByIdAndName("non-existent", "non-existent"); |
| 269 | + } |
| 270 | + |
| 271 | + @Test |
| 272 | + public void testDeleteNonExistingGsiWithCondition() { |
| 273 | + // Delete via GSI |
| 274 | + userRepository.deleteByPostCodeAndNumberOfPlaylists("non-existing", 23); |
| 275 | + |
| 276 | + } |
| 277 | + |
| 278 | + |
| 279 | + @Test |
| 280 | + public void testFilterWithCollections() { |
| 281 | + // Prepare |
| 282 | + User u1 = new User(); |
| 283 | + String name1 = "name1" + ThreadLocalRandom.current().nextLong(); |
| 284 | + u1.setId("u1"); |
| 285 | + u1.setName(name1); |
| 286 | + u1.setPostCode("1234"); |
| 287 | + Set<String> u1Tags = new HashSet<>(); |
| 288 | + u1Tags.add("tag-a"); |
| 289 | + u1Tags.add("tag-b"); |
| 290 | + u1Tags.add("tag-c"); |
| 291 | + u1.setTags(u1Tags); |
| 292 | + |
| 293 | + User u2 = new User(); |
| 294 | + String name2 = "name1" + ThreadLocalRandom.current().nextLong(); |
| 295 | + u2.setId("u2"); |
| 296 | + u2.setName(name2); |
| 297 | + u2.setPostCode("1234"); |
| 298 | + Set<String> u2Tags = new HashSet<>(); |
| 299 | + u2Tags.add("tag-a"); |
| 300 | + u2Tags.add("tag-b"); |
| 301 | + u2.setTags(u2Tags); |
| 302 | + |
| 303 | + User u3 = new User(); |
| 304 | + String name3 = "name1" + ThreadLocalRandom.current().nextLong(); |
| 305 | + u3.setId("u3"); |
| 306 | + u3.setName(name3); |
| 307 | + u3.setPostCode("1234"); |
| 308 | + Set<String> u3Tags = new HashSet<>(); |
| 309 | + u3Tags.add("tag-a"); |
| 310 | + u3Tags.add("tag-c"); |
| 311 | + u3.setTags(u3Tags); |
| 312 | + |
| 313 | + |
| 314 | + u1 = userRepository.save(u1); |
| 315 | + u2 = userRepository.save(u2); |
| 316 | + u3 = userRepository.save(u3); |
| 317 | + |
| 318 | + Set<User> tagA = setOf(u1, u2, u3); |
| 319 | + Set<User> tagB = setOf(u1,u2); |
| 320 | + Set<User> tagC = setOf(u1, u3); |
| 321 | + |
| 322 | + Set<User> notTagA = setOf(); |
| 323 | + Set<User> notTagB = setOf(u3); |
| 324 | + Set<User> notTagC = setOf(u2); |
| 325 | + |
| 326 | + |
| 327 | + // Single value |
| 328 | + assertEquals(tagA, new HashSet<>(userRepository.findAllByTagsContaining("tag-a"))); |
| 329 | + assertEquals(tagB, new HashSet<>(userRepository.findAllByTagsContaining("tag-b"))); |
| 330 | + assertEquals(tagC, new HashSet<>(userRepository.findAllByTagsContaining("tag-c"))); |
| 331 | + |
| 332 | + assertEquals(tagA, new HashSet<>(userRepository.findAllByTagsContains("tag-a"))); |
| 333 | + assertEquals(tagB, new HashSet<>(userRepository.findAllByTagsContains("tag-b"))); |
| 334 | + assertEquals(tagC, new HashSet<>(userRepository.findAllByTagsContains("tag-c"))); |
| 335 | + |
| 336 | + assertEquals(tagA, new HashSet<>(userRepository.findAllByTagsIsContaining("tag-a"))); |
| 337 | + assertEquals(tagB, new HashSet<>(userRepository.findAllByTagsIsContaining("tag-b"))); |
| 338 | + assertEquals(tagC, new HashSet<>(userRepository.findAllByTagsIsContaining("tag-c"))); |
| 339 | + |
| 340 | + assertEquals(notTagA, new HashSet<>(userRepository.findAllByTagsNotContaining("tag-a"))); |
| 341 | + assertEquals(notTagB, new HashSet<>(userRepository.findAllByTagsNotContaining("tag-b"))); |
| 342 | + assertEquals(notTagC, new HashSet<>(userRepository.findAllByTagsNotContaining("tag-c"))); |
| 343 | + |
| 344 | + assertEquals(notTagA, new HashSet<>(userRepository.findAllByTagsNotContains("tag-a"))); |
| 345 | + assertEquals(notTagB, new HashSet<>(userRepository.findAllByTagsNotContains("tag-b"))); |
| 346 | + assertEquals(notTagC, new HashSet<>(userRepository.findAllByTagsNotContains("tag-c"))); |
| 347 | + |
| 348 | + assertEquals(notTagA, new HashSet<>(userRepository.findAllByTagsIsNotContaining("tag-a"))); |
| 349 | + assertEquals(notTagB, new HashSet<>(userRepository.findAllByTagsIsNotContaining("tag-b"))); |
| 350 | + assertEquals(notTagC, new HashSet<>(userRepository.findAllByTagsIsNotContaining("tag-c"))); |
| 351 | + |
| 352 | + |
| 353 | + //Sets |
| 354 | + assertEquals(tagA, new HashSet<>(userRepository.findAllByTagsContaining(setOf("tag-a")))); |
| 355 | + assertEquals(tagB, new HashSet<>(userRepository.findAllByTagsContaining(setOf("tag-b")))); |
| 356 | + assertEquals(tagC, new HashSet<>(userRepository.findAllByTagsContaining(setOf("tag-c")))); |
| 357 | + |
| 358 | + assertEquals(tagA, new HashSet<>(userRepository.findAllByTagsContains(setOf("tag-a")))); |
| 359 | + assertEquals(tagB, new HashSet<>(userRepository.findAllByTagsContains(setOf("tag-b")))); |
| 360 | + assertEquals(tagC, new HashSet<>(userRepository.findAllByTagsContains(setOf("tag-c")))); |
| 361 | + |
| 362 | + assertEquals(tagA, new HashSet<>(userRepository.findAllByTagsIsContaining(setOf("tag-a")))); |
| 363 | + assertEquals(tagB, new HashSet<>(userRepository.findAllByTagsIsContaining(setOf("tag-b")))); |
| 364 | + assertEquals(tagC, new HashSet<>(userRepository.findAllByTagsIsContaining(setOf("tag-c")))); |
| 365 | + |
| 366 | + assertEquals(notTagA, new HashSet<>(userRepository.findAllByTagsNotContaining(setOf("tag-a")))); |
| 367 | + assertEquals(notTagB, new HashSet<>(userRepository.findAllByTagsNotContaining(setOf("tag-b")))); |
| 368 | + assertEquals(notTagC, new HashSet<>(userRepository.findAllByTagsNotContaining(setOf("tag-c")))); |
| 369 | + |
| 370 | + assertEquals(notTagA, new HashSet<>(userRepository.findAllByTagsNotContains(setOf("tag-a")))); |
| 371 | + assertEquals(notTagB, new HashSet<>(userRepository.findAllByTagsNotContains(setOf("tag-b")))); |
| 372 | + assertEquals(notTagC, new HashSet<>(userRepository.findAllByTagsNotContains(setOf("tag-c")))); |
| 373 | + |
| 374 | + assertEquals(notTagA, new HashSet<>(userRepository.findAllByTagsIsNotContaining(setOf("tag-a")))); |
| 375 | + assertEquals(notTagB, new HashSet<>(userRepository.findAllByTagsIsNotContaining(setOf("tag-b")))); |
| 376 | + assertEquals(notTagC, new HashSet<>(userRepository.findAllByTagsIsNotContaining(setOf("tag-c")))); |
| 377 | + |
| 378 | + //Lists |
| 379 | + assertEquals(tagA, new HashSet<>(userRepository.findAllByTagsContaining(listOf("tag-a")))); |
| 380 | + assertEquals(tagB, new HashSet<>(userRepository.findAllByTagsContaining(listOf("tag-b")))); |
| 381 | + assertEquals(tagC, new HashSet<>(userRepository.findAllByTagsContaining(listOf("tag-c")))); |
| 382 | + |
| 383 | + assertEquals(tagA, new HashSet<>(userRepository.findAllByTagsContains(listOf("tag-a")))); |
| 384 | + assertEquals(tagB, new HashSet<>(userRepository.findAllByTagsContains(listOf("tag-b")))); |
| 385 | + assertEquals(tagC, new HashSet<>(userRepository.findAllByTagsContains(listOf("tag-c")))); |
| 386 | + |
| 387 | + assertEquals(tagA, new HashSet<>(userRepository.findAllByTagsIsContaining(listOf("tag-a")))); |
| 388 | + assertEquals(tagB, new HashSet<>(userRepository.findAllByTagsIsContaining(listOf("tag-b")))); |
| 389 | + assertEquals(tagC, new HashSet<>(userRepository.findAllByTagsIsContaining(listOf("tag-c")))); |
| 390 | + |
| 391 | + assertEquals(notTagA, new HashSet<>(userRepository.findAllByTagsNotContaining(listOf("tag-a")))); |
| 392 | + assertEquals(notTagB, new HashSet<>(userRepository.findAllByTagsNotContaining(listOf("tag-b")))); |
| 393 | + assertEquals(notTagC, new HashSet<>(userRepository.findAllByTagsNotContaining(listOf("tag-c")))); |
| 394 | + |
| 395 | + assertEquals(notTagA, new HashSet<>(userRepository.findAllByTagsNotContains(listOf("tag-a")))); |
| 396 | + assertEquals(notTagB, new HashSet<>(userRepository.findAllByTagsNotContains(listOf("tag-b")))); |
| 397 | + assertEquals(notTagC, new HashSet<>(userRepository.findAllByTagsNotContains(listOf("tag-c")))); |
269 | 398 |
|
270 |
| - @Test |
271 |
| - public void testDeleteNonExistingGsiWithConditoin() { |
272 |
| - // Delete via GSI |
273 |
| - userRepository.deleteByPostCodeAndNumberOfPlaylists("non-existing", 23); |
| 399 | + assertEquals(notTagA, new HashSet<>(userRepository.findAllByTagsIsNotContaining(listOf("tag-a")))); |
| 400 | + assertEquals(notTagB, new HashSet<>(userRepository.findAllByTagsIsNotContaining(listOf("tag-b")))); |
| 401 | + assertEquals(notTagC, new HashSet<>(userRepository.findAllByTagsIsNotContaining(listOf("tag-c")))); |
274 | 402 |
|
275 |
| - } |
| 403 | + |
| 404 | + } |
| 405 | + |
| 406 | + |
| 407 | + @SafeVarargs |
| 408 | + private final <E> Set<E> setOf(E... values) { |
| 409 | + Set<E> result = new HashSet<>(); |
| 410 | + |
| 411 | + if (values != null) { |
| 412 | + result.addAll(Arrays.asList(values)); |
| 413 | + } |
| 414 | + return result; |
| 415 | + } |
| 416 | + |
| 417 | + @SafeVarargs |
| 418 | + private final <E> List<E> listOf(E... values) { |
| 419 | + List<E> result = new ArrayList<>(); |
| 420 | + |
| 421 | + if (values != null) { |
| 422 | + result.addAll(Arrays.asList(values)); |
| 423 | + } |
| 424 | + return result; |
| 425 | + } |
276 | 426 | }
|
0 commit comments