From c19dd2d582f78b91b458d1e7076addcd82342c35 Mon Sep 17 00:00:00 2001 From: quinnandrews Date: Wed, 24 Jan 2024 11:56:04 -0800 Subject: [PATCH] Add with() method and rename fetch() methods to fetchOf(). --- .../builder/SpecificationBuilder.java | 16 ++- .../builder/SpecificationFactory.java | 4 +- .../SpecificationBuilderIntegrationTest.java | 32 +++--- .../builder/SpecificationBuilderTest.java | 107 ++++++++++-------- .../builder/SpecificationFactoryTest.java | 8 +- .../GuitarPedalSpecifications.java | 42 +++---- 6 files changed, 110 insertions(+), 99 deletions(-) diff --git a/src/main/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationBuilder.java b/src/main/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationBuilder.java index b24fc66..ff88fe0 100644 --- a/src/main/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationBuilder.java +++ b/src/main/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationBuilder.java @@ -62,6 +62,10 @@ public SpecificationBuilder or(final Specification specification) { return this; } + public SpecificationBuilder with() { + return this; + } + @Deprecated public SpecificationBuilder andWhere(final Specification specification) { return where(specification); @@ -167,12 +171,12 @@ public SpecificationBuilder whereIn(final SingularAttribute attribute, @Deprecated public SpecificationBuilder withFetch(final SingularAttribute attribute) { - return and(SpecificationFactory.fetch(attribute)); + return and(SpecificationFactory.fetchOf(attribute)); } @Deprecated public SpecificationBuilder withFetch(final PluralAttribute attribute) { - return and(SpecificationFactory.fetch(attribute)); + return and(SpecificationFactory.fetchOf(attribute)); } public SpecificationBuilder isEqualTo(final SingularAttribute attribute, @@ -252,11 +256,11 @@ public SpecificationBuilder isIn(final SingularAttribute attribute, return where(SpecificationFactory.isIn(attribute, values)); } - public SpecificationBuilder fetch(final SingularAttribute attribute) { - return and(SpecificationFactory.fetch(attribute)); + public SpecificationBuilder fetchOf(final SingularAttribute attribute) { + return and(SpecificationFactory.fetchOf(attribute)); } - public SpecificationBuilder fetch(final PluralAttribute attribute) { - return and(SpecificationFactory.fetch(attribute)); + public SpecificationBuilder fetchOf(final PluralAttribute attribute) { + return and(SpecificationFactory.fetchOf(attribute)); } } diff --git a/src/main/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationFactory.java b/src/main/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationFactory.java index 1fca07c..1f4a805 100644 --- a/src/main/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationFactory.java +++ b/src/main/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationFactory.java @@ -158,7 +158,7 @@ public static Specification isIn(final SingularAttribute attribute, .collect(Collectors.toSet())); } - public static Specification fetch(final SingularAttribute attribute) { + public static Specification fetchOf(final SingularAttribute attribute) { Objects.requireNonNull(attribute, ATTRIBUTE_CANNOT_BE_NULL); if (noneAreNull(attribute)) { return (root, query, builder) -> { @@ -169,7 +169,7 @@ public static Specification fetch(final SingularAttribute attribute return ghost(); } - public static Specification fetch(final PluralAttribute attribute) { + public static Specification fetchOf(final PluralAttribute attribute) { Objects.requireNonNull(attribute, ATTRIBUTE_CANNOT_BE_NULL); if (noneAreNull(attribute)) { return (root, query, builder) -> { diff --git a/src/test/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationBuilderIntegrationTest.java b/src/test/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationBuilderIntegrationTest.java index 53b85d1..0a49bf1 100644 --- a/src/test/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationBuilderIntegrationTest.java +++ b/src/test/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationBuilderIntegrationTest.java @@ -317,7 +317,7 @@ void or_withSpecification() { void isEqualTo() { var optionalPedal = guitarPedalRepository.findOne( SpecificationBuilder.from(GuitarPedal.class) - .isEqualTo(GuitarPedal_.id, 3L) + .where().isEqualTo(GuitarPedal_.id, 3L) .toSpecification()); assertTrue(optionalPedal.isPresent()); assertEquals(3L, optionalPedal.get().getId()); @@ -328,7 +328,7 @@ void isEqualTo() { void isNotEqualTo() { var pedals = guitarPedalRepository.findAll( SpecificationBuilder.from(GuitarPedal.class) - .isNotEqualTo(GuitarPedal_.id, 3L) + .where().isNotEqualTo(GuitarPedal_.id, 3L) .toSpecification()); assertEquals(3, pedals.size()); assertTrue(pedals.stream() @@ -341,7 +341,7 @@ void isNotEqualTo() { void isLike() { var pedals = guitarPedalRepository.findAll( SpecificationBuilder.from(GuitarPedal.class) - .isLike(GuitarPedal_.name, "%and%") + .where().isLike(GuitarPedal_.name, "%and%") .toSpecification(), Sort.by("name")); assertEquals(2, pedals.size()); assertEquals("Deco: Tape Saturation and Double Tracker", pedals.get(0).getName()); @@ -352,7 +352,7 @@ void isLike() { void isNotLike() { var pedals = guitarPedalRepository.findAll( SpecificationBuilder.from(GuitarPedal.class) - .isNotLike(GuitarPedal_.name, "%and%") + .where().isNotLike(GuitarPedal_.name, "%and%") .toSpecification(), Sort.by("name")); assertEquals(2, pedals.size()); assertEquals("Big Muff Fuzz", pedals.get(0).getName()); @@ -362,7 +362,7 @@ void isNotLike() { @Test void isEqualToOrLike() { var pedals = guitarPedalRepository.findAll(SpecificationBuilder.from(GuitarPedal.class) - .isEqualToOrLike(GuitarPedal_.name, "Deco%") + .where().isEqualToOrLike(GuitarPedal_.name, "Deco%") .toSpecification(), Sort.by("name")); assertEquals(1, pedals.size()); assertEquals("Deco: Tape Saturation and Double Tracker", pedals.get(0).getName()); @@ -372,7 +372,7 @@ void isEqualToOrLike() { void isNull() { var pedals = guitarPedalRepository.findAll( SpecificationBuilder.from(GuitarPedal.class) - .isNull(GuitarPedal_.dateSold) + .where().isNull(GuitarPedal_.dateSold) .toSpecification(), Sort.by("name")); assertEquals(3, pedals.size()); assertEquals("Big Muff Fuzz", pedals.get(0).getName()); @@ -387,7 +387,7 @@ void isNull() { void isNotNull() { var pedals = guitarPedalRepository.findAll( SpecificationBuilder.from(GuitarPedal.class) - .isNotNull(GuitarPedal_.dateSold) + .where().isNotNull(GuitarPedal_.dateSold) .toSpecification(), Sort.by("name")); assertEquals(1, pedals.size()); assertEquals("Sneak Attack: Attack/Decay and Tremolo", pedals.get(0).getName()); @@ -398,7 +398,7 @@ void isNotNull() { void isTrue() { var pedals = guitarPedalRepository.findAll( SpecificationBuilder.from(GuitarPedal.class) - .isTrue(GuitarPedal_.hasStereoOutput) + .where().isTrue(GuitarPedal_.hasStereoOutput) .toSpecification(), Sort.by("name")); assertEquals(1, pedals.size()); assertEquals("Deco: Tape Saturation and Double Tracker", pedals.get(0).getName()); @@ -410,7 +410,7 @@ void isTrue() { void isFalse() { var pedals = guitarPedalRepository.findAll( SpecificationBuilder.from(GuitarPedal.class) - .isFalse(GuitarPedal_.hasStereoOutput) + .where().isFalse(GuitarPedal_.hasStereoOutput) .toSpecification(), Sort.by("name")); assertEquals(3, pedals.size()); assertEquals("Big Muff Fuzz", pedals.get(0).getName()); @@ -425,7 +425,7 @@ void isFalse() { void isGreaterThan() { var pedals = guitarPedalRepository.findAll( SpecificationBuilder.from(GuitarPedal.class) - .isGreaterThan(GuitarPedal_.usedValue, 200) + .where().isGreaterThan(GuitarPedal_.usedValue, 200) .toSpecification(), Sort.by("name")); assertEquals(1, pedals.size()); assertEquals("Deco: Tape Saturation and Double Tracker", pedals.get(0).getName()); @@ -436,7 +436,7 @@ void isGreaterThan() { void isGreaterThanOrEqualTo() { var pedals = guitarPedalRepository.findAll( SpecificationBuilder.from(GuitarPedal.class) - .isGreaterThanOrEqualTo(GuitarPedal_.usedValue, 200) + .where().isGreaterThanOrEqualTo(GuitarPedal_.usedValue, 200) .toSpecification(), Sort.by("name")); assertEquals(2, pedals.size()); assertEquals("Deco: Tape Saturation and Double Tracker", pedals.get(0).getName()); @@ -449,7 +449,7 @@ void isGreaterThanOrEqualTo() { void isLessThan() { var pedals = guitarPedalRepository.findAll( SpecificationBuilder.from(GuitarPedal.class) - .isLessThan(GuitarPedal_.usedValue, 150) + .where().isLessThan(GuitarPedal_.usedValue, 150) .toSpecification(), Sort.by("name")); assertEquals(1, pedals.size()); assertEquals("Big Muff Fuzz", pedals.get(0).getName()); @@ -460,7 +460,7 @@ void isLessThan() { void isLessThanOrEqualTo() { var pedals = guitarPedalRepository.findAll( SpecificationBuilder.from(GuitarPedal.class) - .isLessThanOrEqualTo(GuitarPedal_.usedValue, 150) + .where().isLessThanOrEqualTo(GuitarPedal_.usedValue, 150) .toSpecification(), Sort.by("name")); assertEquals(2, pedals.size()); assertEquals("Big Muff Fuzz", pedals.get(0).getName()); @@ -473,7 +473,7 @@ void isLessThanOrEqualTo() { void isBetween() { var pedals = guitarPedalRepository.findAll( SpecificationBuilder.from(GuitarPedal.class) - .isBetween( + .where().isBetween( GuitarPedal_.datePurchased, LocalDate.of(2021, 7, 19), LocalDate.of(2022, 9, 11)) @@ -489,7 +489,7 @@ void isBetween() { void isIn_collection() { var pedals = guitarPedalRepository.findAll( SpecificationBuilder.from(GuitarPedal.class) - .isIn(GuitarPedal_.id, List.of(2L, 3L)) + .where().isIn(GuitarPedal_.id, List.of(2L, 3L)) .toSpecification(), Sort.by("name")); assertEquals(2, pedals.size()); assertEquals(2L, pedals.get(0).getId()); @@ -502,7 +502,7 @@ void isIn_collection() { void isIn_array() { var pedals = guitarPedalRepository.findAll( SpecificationBuilder.from(GuitarPedal.class) - .isIn(GuitarPedal_.id, 2L, 3L) + .where().isIn(GuitarPedal_.id, 2L, 3L) .toSpecification(), Sort.by("name")); assertEquals(2, pedals.size()); assertEquals(2L, pedals.get(0).getId()); diff --git a/src/test/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationBuilderTest.java b/src/test/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationBuilderTest.java index 7a228ff..6490c6d 100644 --- a/src/test/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationBuilderTest.java +++ b/src/test/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationBuilderTest.java @@ -127,6 +127,13 @@ void or_throwsException_whenArgumentIsNull() { ); } + @Test + void with_returnsBuilder() { + var builder = SpecificationBuilder.from(GuitarPedal.class) + .with(); + assertNotNull(builder); + } + @Test @Deprecated void andWhere_returnsBuilderWithSpecification() { @@ -632,7 +639,7 @@ void withFetch_list_throwsException_whenAttributeIsNull() { @Test void isEqualTo_returnsBuilderWithSpecification() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isEqualTo(GuitarPedal_.id, 0L); + .where().isEqualTo(GuitarPedal_.id, 0L); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -640,7 +647,7 @@ void isEqualTo_returnsBuilderWithSpecification() { @Test void isEqualTo_returnsBuilderWithSpecification_evenWhenValueIsNull() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isEqualTo(GuitarPedal_.id, null); + .where().isEqualTo(GuitarPedal_.id, null); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -650,14 +657,14 @@ void isEqualTo_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, () -> SpecificationBuilder.from(GuitarPedal.class) - .isEqualTo(null, 0L) + .where().isEqualTo(null, 0L) ); } @Test void isNotEqualTo_returnsBuilderWithSpecification() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isNotEqualTo(GuitarPedal_.id, 0L); + .where().isNotEqualTo(GuitarPedal_.id, 0L); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -665,7 +672,7 @@ void isNotEqualTo_returnsBuilderWithSpecification() { @Test void isNotEqualTo_returnsBuilderWithSpecification_evenWhenValueIsNull() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isNotEqualTo(GuitarPedal_.id, null); + .where().isNotEqualTo(GuitarPedal_.id, null); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -675,14 +682,14 @@ void isNotEqualTo_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, () -> SpecificationBuilder.from(GuitarPedal.class) - .isNotEqualTo(null, 0L) + .where().isNotEqualTo(null, 0L) ); } @Test void isLike_returnsBuilderWithSpecification() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isLike(GuitarPedal_.name, "%test%"); + .where().isLike(GuitarPedal_.name, "%test%"); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -690,7 +697,7 @@ void isLike_returnsBuilderWithSpecification() { @Test void isLike_returnsBuilderWithSpecification_evenWhenValueIsNull() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isLike(GuitarPedal_.name, null); + .where().isLike(GuitarPedal_.name, null); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -700,14 +707,14 @@ void isLike_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, () -> SpecificationBuilder.from(GuitarPedal.class) - .isLike(null, "%test%") + .where().isLike(null, "%test%") ); } @Test void isNotLike_returnsBuilderWithSpecification() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isNotLike(GuitarPedal_.name, "%test%"); + .where().isNotLike(GuitarPedal_.name, "%test%"); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -715,7 +722,7 @@ void isNotLike_returnsBuilderWithSpecification() { @Test void isNotLike_returnsBuilderWithSpecification_evenWhenValueIsNull() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isNotLike(GuitarPedal_.name, null); + .where().isNotLike(GuitarPedal_.name, null); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -725,14 +732,14 @@ void isNotLike_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, () -> SpecificationBuilder.from(GuitarPedal.class) - .isNotLike(null, "%test%") + .where().isNotLike(null, "%test%") ); } @Test void isEqualToOrLike_returnsBuilderWithSpecification() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isEqualToOrLike(GuitarPedal_.name, "%test%"); + .where().isEqualToOrLike(GuitarPedal_.name, "%test%"); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -740,7 +747,7 @@ void isEqualToOrLike_returnsBuilderWithSpecification() { @Test void isEqualsOrLike_returnsBuilderWithSpecification_evenWhenValueIsNull() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isEqualToOrLike(GuitarPedal_.name, null); + .where().isEqualToOrLike(GuitarPedal_.name, null); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -750,14 +757,14 @@ void isEqualToOrLike_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, () -> SpecificationBuilder.from(GuitarPedal.class) - .isEqualToOrLike(null, "%test%") + .where().isEqualToOrLike(null, "%test%") ); } @Test void isIsTrue_returnsBuilderWithSpecification() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isTrue(GuitarPedal_.hasStereoOutput); + .where().isTrue(GuitarPedal_.hasStereoOutput); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -767,14 +774,14 @@ void isIsTrue_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, () -> SpecificationBuilder.from(GuitarPedal.class) - .isTrue(null) + .where().isTrue(null) ); } @Test void isIsFalse_returnsBuilderWithSpecification() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isFalse(GuitarPedal_.hasStereoOutput); + .where().isFalse(GuitarPedal_.hasStereoOutput); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -784,14 +791,14 @@ void isIsFalse_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, () -> SpecificationBuilder.from(GuitarPedal.class) - .isFalse(null) + .where().isFalse(null) ); } @Test void isIsNull_returnsBuilderWithSpecification() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isNull(GuitarPedal_.dateSold); + .where().isNull(GuitarPedal_.dateSold); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -801,14 +808,14 @@ void isIsNull_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, () -> SpecificationBuilder.from(GuitarPedal.class) - .isNull(null) + .where().isNull(null) ); } @Test void isIsNotNull_returnsBuilderWithSpecification() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isNotNull(GuitarPedal_.dateSold); + .where().isNotNull(GuitarPedal_.dateSold); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -818,14 +825,14 @@ void isIsNotNull_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, () -> SpecificationBuilder.from(GuitarPedal.class) - .isNotNull(null) + .where().isNotNull(null) ); } @Test void isGreaterThan_returnsBuilderWithSpecification() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isGreaterThan(GuitarPedal_.usedValue, 0); + .where().isGreaterThan(GuitarPedal_.usedValue, 0); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -833,7 +840,7 @@ void isGreaterThan_returnsBuilderWithSpecification() { @Test void isGreaterThan_returnsBuilderWithSpecification_evenWhenValueIsNull() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isGreaterThan(GuitarPedal_.usedValue, null); + .where().isGreaterThan(GuitarPedal_.usedValue, null); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -843,14 +850,14 @@ void isGreaterThan_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, () -> SpecificationBuilder.from(GuitarPedal.class) - .isGreaterThan(null, 0) + .where().isGreaterThan(null, 0) ); } @Test void isGreaterThanOrEqualTo_returnsBuilderWithSpecification() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isGreaterThanOrEqualTo(GuitarPedal_.usedValue, 0); + .where().isGreaterThanOrEqualTo(GuitarPedal_.usedValue, 0); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -858,7 +865,7 @@ void isGreaterThanOrEqualTo_returnsBuilderWithSpecification() { @Test void isGreaterThanOrEqualTo_returnsBuilderWithSpecification_evenWhenValueIsNull() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isGreaterThanOrEqualTo(GuitarPedal_.usedValue, null); + .where().isGreaterThanOrEqualTo(GuitarPedal_.usedValue, null); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -868,14 +875,14 @@ void isGreaterThanOrEqualTo_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, () -> SpecificationBuilder.from(GuitarPedal.class) - .isGreaterThanOrEqualTo(null, 0) + .where().isGreaterThanOrEqualTo(null, 0) ); } @Test void isLessThan_returnsBuilderWithSpecification() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isLessThan(GuitarPedal_.usedValue, 0); + .where().isLessThan(GuitarPedal_.usedValue, 0); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -883,7 +890,7 @@ void isLessThan_returnsBuilderWithSpecification() { @Test void isLessThan_returnsBuilderWithSpecification_evenWhenValueIsNull() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isLessThan(GuitarPedal_.usedValue, null); + .where().isLessThan(GuitarPedal_.usedValue, null); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -893,14 +900,14 @@ void isLessThan_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, () -> SpecificationBuilder.from(GuitarPedal.class) - .isLessThan(null, 0) + .where().isLessThan(null, 0) ); } @Test void isLessThanOrEqualTo_returnsBuilderWithSpecification() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isLessThanOrEqualTo(GuitarPedal_.usedValue, 0); + .where().isLessThanOrEqualTo(GuitarPedal_.usedValue, 0); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -908,7 +915,7 @@ void isLessThanOrEqualTo_returnsBuilderWithSpecification() { @Test void isLessThanOrEqualTo_returnsBuilderWithSpecification_evenWhenValueIsNull() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isLessThanOrEqualTo(GuitarPedal_.usedValue, null); + .where().isLessThanOrEqualTo(GuitarPedal_.usedValue, null); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -918,14 +925,14 @@ void isLessThanOrEqualTo_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, () -> SpecificationBuilder.from(GuitarPedal.class) - .isLessThanOrEqualTo(null, 0) + .where().isLessThanOrEqualTo(null, 0) ); } @Test void isBetween_returnsBuilderWithSpecification() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isBetween( + .where().isBetween( GuitarPedal_.datePurchased, LocalDate.now(), LocalDate.now().plusDays(7) @@ -937,17 +944,17 @@ void isBetween_returnsBuilderWithSpecification() { @Test void isBetween_returnsBuilderWithSpecification_evenWhenEitherValueIsNull() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isBetween(GuitarPedal_.usedValue, null, null); + .where().isBetween(GuitarPedal_.usedValue, null, null); assertNotNull(builder); assertNotNull(builder.toSpecification()); builder = SpecificationBuilder.from(GuitarPedal.class) - .isBetween(GuitarPedal_.usedValue, 0, null); + .where().isBetween(GuitarPedal_.usedValue, 0, null); assertNotNull(builder); assertNotNull(builder.toSpecification()); builder = SpecificationBuilder.from(GuitarPedal.class) - .isBetween(GuitarPedal_.usedValue, null, 9); + .where().isBetween(GuitarPedal_.usedValue, null, 9); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -957,14 +964,14 @@ void isBetween_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, () -> SpecificationBuilder.from(GuitarPedal.class) - .isBetween(null, 0L, 9L) + .where().isBetween(null, 0L, 9L) ); } @Test void isIn_collection_returnsBuilderWithSpecification() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isIn(GuitarPedal_.id, List.of(1L, 2L, 3L)); + .where().isIn(GuitarPedal_.id, List.of(1L, 2L, 3L)); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -972,7 +979,7 @@ void isIn_collection_returnsBuilderWithSpecification() { @Test void isIn_collection_returnsBuilderWithSpecification_evenWhenValueIsNull() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isIn(GuitarPedal_.id, (Collection) null); + .where().isIn(GuitarPedal_.id, (Collection) null); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -982,14 +989,14 @@ void isIn_collection_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, () -> SpecificationBuilder.from(GuitarPedal.class) - .isIn(null, List.of(1L, 2L, 3L)) + .where().isIn(null, List.of(1L, 2L, 3L)) ); } @Test void isIn_array_returnsBuilderWithSpecification() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isIn(GuitarPedal_.id, 1L, 2L, 3L); + .where().isIn(GuitarPedal_.id, 1L, 2L, 3L); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -997,7 +1004,7 @@ void isIn_array_returnsBuilderWithSpecification() { @Test void isIn_array_returnsBuilderWithSpecification_evenWhenValueIsNull() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .isIn(GuitarPedal_.id, (Object) null); + .where().isIn(GuitarPedal_.id, (Object) null); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -1007,14 +1014,14 @@ void isIn_array_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, () -> SpecificationBuilder.from(GuitarPedal.class) - .isIn(null, 1L, 2L, 3L) + .where().isIn(null, 1L, 2L, 3L) ); } @Test void fetch_singular_returnsBuilderWithSpecification() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .fetch(GuitarPedal_.manufacturer); + .with().fetchOf(GuitarPedal_.manufacturer); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -1024,14 +1031,14 @@ void fetch_singular_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, () -> SpecificationBuilder.from(GuitarPedal.class) - .fetch((SingularAttribute) null) + .with().fetchOf((SingularAttribute) null) ); } @Test void fetch_list_returnsBuilderWithSpecification() { var builder = SpecificationBuilder.from(GuitarPedal.class) - .fetch(GuitarPedal_.tags); + .with().fetchOf(GuitarPedal_.tags); assertNotNull(builder); assertNotNull(builder.toSpecification()); } @@ -1041,7 +1048,7 @@ void fetch_list_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, () -> SpecificationBuilder.from(GuitarPedal.class) - .fetch((ListAttribute) null) + .with().fetchOf((ListAttribute) null) ); } } \ No newline at end of file diff --git a/src/test/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationFactoryTest.java b/src/test/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationFactoryTest.java index 24196d0..f788235 100644 --- a/src/test/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationFactoryTest.java +++ b/src/test/java/io/github/quinnandrews/spring/data/specification/builder/SpecificationFactoryTest.java @@ -385,7 +385,7 @@ void isIn_array_throwsException_whenAttributeIsNull() { @Test void fetch_singular_returnsNonNullPredicate() { - var specification = fetch(GuitarPedal_.manufacturer); + var specification = fetchOf(GuitarPedal_.manufacturer); assertNotNull(specification); assertNotNull(specification.toPredicate(root, query, builder)); } @@ -394,12 +394,12 @@ void fetch_singular_returnsNonNullPredicate() { void fetch_singular_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, - () -> fetch((SingularAttribute) null)); + () -> fetchOf((SingularAttribute) null)); } @Test void fetch_list_returnsNonNullPredicate() { - var specification = fetch(GuitarPedal_.tags); + var specification = fetchOf(GuitarPedal_.tags); assertNotNull(specification); assertNotNull(specification.toPredicate(root, query, builder)); } @@ -408,7 +408,7 @@ void fetch_list_returnsNonNullPredicate() { void fetch_list_throwsException_whenAttributeIsNull() { assertThrows( NullPointerException.class, - () -> fetch((ListAttribute) null)); + () -> fetchOf((ListAttribute) null)); } @Test diff --git a/src/test/java/io/github/quinnandrews/spring/data/specification/builder/application/data/guitarpedals/specifications/GuitarPedalSpecifications.java b/src/test/java/io/github/quinnandrews/spring/data/specification/builder/application/data/guitarpedals/specifications/GuitarPedalSpecifications.java index 4b194cc..2f94130 100644 --- a/src/test/java/io/github/quinnandrews/spring/data/specification/builder/application/data/guitarpedals/specifications/GuitarPedalSpecifications.java +++ b/src/test/java/io/github/quinnandrews/spring/data/specification/builder/application/data/guitarpedals/specifications/GuitarPedalSpecifications.java @@ -270,7 +270,7 @@ public Specification search_example_07(final Integer usedValueGreat } /* - This example, Fetch Example #1, introduces the fetch() methods provided by the + This example, Fetch Example #1, introduces the fetchOf() methods provided by the SpecificationBuilder to eagerly fetch associated Entities in an optimized way. If search_example_07() is executed with a value of 75 passed in as its argument, 2 GuitarPedals @@ -314,7 +314,7 @@ If search_example_07() is executed with a value of 75 passed in as its argument, and we can make that happen by utilizing what the underlying JPA Criteria API provides and encapsulating it in the SpecificationBuilder and SpecificationFactory. - All we need to do is call fetch() on the SpecificationBuilder (or fetch() on the + All we need to do is call fetchOf() on the SpecificationBuilder (or fetchOf() on the SpecificationFactory) with the attribute representing the association we want to eagerly fetch. In this example, Fetch Example #1, we define a fetch of Manufacturer and the SQL is then rendered @@ -347,12 +347,12 @@ All we need to do is call fetch() on the SpecificationBuilder (or fetch() on the */ public Specification fetch_example_01() { return from(GuitarPedal.class) - .fetch(GuitarPedal_.manufacturer) + .with().fetchOf(GuitarPedal_.manufacturer) .toSpecification(); } /* - This example, Fetch Example #2, uses the fetch() method provided by the + This example, Fetch Example #2, uses the fetchOf() method provided by the SpecificationBuilder to eagerly fetch an associated Entity, but it also uses the where() method to filter by one of the associated Entity's properties. @@ -370,8 +370,8 @@ Here we use a method from the SpecificationUtil Class (which is used by many and it will have no effect on the rendered SQL query. Otherwise, a Criteria Predicate is built and returned. - NOTE: Calling fetch() is not required to do the filtering. They are independent - operations. We call fetch() simply to limit the number of queries. + NOTE: Calling fetchOf() is not required to do the filtering. They are independent + operations. We call fetchOf() simply to limit the number of queries. The SQL renders as: @@ -408,7 +408,7 @@ may get unexpected results (see Fetch Example #7). */ public Specification fetch_example_02(final String name) { return from(GuitarPedal.class) - .fetch(GuitarPedal_.manufacturer) + .with().fetchOf(GuitarPedal_.manufacturer) .where( (root, query, builder) -> { if (SpecificationUtil.noneAreNull(name)) { @@ -422,7 +422,7 @@ public Specification fetch_example_02(final String name) { } /* - This example, Fetch Example #3, uses the fetch() method provided by the + This example, Fetch Example #3, uses the fetchOf() method provided by the SpecificationBuilder twice, one to eagerly fetch an associated Entity and another to eagerly fetch an associated Collection of Entities. It also uses the where() method to filter by a property belonging to Entity of the @@ -443,8 +443,8 @@ the where() method to filter by a property belonging to Entity of the */ public Specification fetch_example_03(final Collection tags) { return from(GuitarPedal.class) - .fetch(GuitarPedal_.manufacturer) - .fetch(GuitarPedal_.tags) + .with().fetchOf(GuitarPedal_.manufacturer) + .and().fetchOf(GuitarPedal_.tags) .where( (root, query, builder) -> { if (SpecificationUtil.noneAreNull(tags)) { @@ -475,8 +475,8 @@ called, which simply returns a Specification whose toPredicate() method (the lam */ public Specification fetch_example_04(final Collection tags) { return from(GuitarPedal.class) - .fetch(GuitarPedal_.manufacturer) - .fetch(GuitarPedal_.tags) + .with().fetchOf(GuitarPedal_.manufacturer) + .and().fetchOf(GuitarPedal_.tags) .where(tagsContain(tags)) .toSpecification(); } @@ -515,8 +515,8 @@ public Specification tagsContain(final Collection tags) { public Specification fetch_example_05(final Long id, final Collection tags) { return from(GuitarPedal.class) - .fetch(GuitarPedal_.manufacturer) - .fetch(GuitarPedal_.tags) + .with().fetchOf(GuitarPedal_.manufacturer) + .and().fetchOf(GuitarPedal_.tags) .where( (root, query, builder) -> { if (SpecificationUtil.noneAreNull(id)) { @@ -562,8 +562,8 @@ the same Join, only two joins will be rendered in the SQL query (again, one for public Specification fetch_example_06(final Long id, final Collection tags) { return from(GuitarPedal.class) - .fetch(GuitarPedal_.manufacturer) - .fetch(GuitarPedal_.tags) + .with().fetchOf(GuitarPedal_.manufacturer) + .and().fetchOf(GuitarPedal_.tags) .where( (root, query, builder) -> { if (SpecificationUtil.noneAreNull(id) @@ -594,11 +594,11 @@ public Specification fetch_example_06(final Long id, same Join instance for both the eager fetch and the Specifications that filter by the associated Collection. - Unlike Example #6, in this example we create a join by calling root.fetch() rather - than calling root.join(). The fetch() method doesn't return a Join, but the Object + Unlike Example #6, in this example we create a join by calling root.fetchOf() rather + than calling root.join(). The fetchOf() method doesn't return a Join, but the Object is castable to a Join, which is unfortunately recognized as an unchecked cast. - Since we're using fetch() to render a join for both the eager fetching and the + Since we're using fetchOf() to render a join for both the eager fetching and the filtering, there is no need to for an additional null check like there was in Example #6. @@ -634,7 +634,7 @@ and t1_0.tag in (?) an Entity rather than a Collection of Entities. But for an associated Collection, it doesn't give us what we would typically want. - When we use fetch() to eagerly fetch an associated Collections, most of the time we + When we use fetchOf() to eagerly fetch an associated Collections, most of the time we will want the whole Collection returned, since we recognize that it is best practice when working with an Aggregate, but this query fails to provide the whole Collection. @@ -655,7 +655,7 @@ When we use fetch() to eagerly fetch an associated Collections, most of the time public Specification fetch_example_07(final Long id, final Collection tags) { return from(GuitarPedal.class) - .fetch(GuitarPedal_.manufacturer) + .with().fetchOf(GuitarPedal_.manufacturer) .where( (root, query, builder) -> { final var join = (Join) root.fetch(GuitarPedal_.tags);