Skip to content

Commit

Permalink
Issue jakartaee#829 unit tests for negated restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
njr-11 committed Dec 5, 2024
1 parent 83c127f commit 5240da6
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
20 changes: 20 additions & 0 deletions api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@


class BasicRestrictionRecordTest {
// A mock entity class for tests
static class Book {
}

@Test
void shouldCreateBasicRestrictionWithDefaultNegation() {
Expand Down Expand Up @@ -62,6 +65,23 @@ void shouldCreateBasicRestrictionWithNullValue() {
});
}

@Test
void shouldNegateLTERestriction() {
Restriction<Book> numChaptersLTE10 = Restrict.lessThanEqual(10, "numChapters");
BasicRestriction<Book> numChaptersLTE10Basic = (BasicRestriction<Book>) numChaptersLTE10;
BasicRestriction<Book> numChaptersGT10Basic = (BasicRestriction<Book>) numChaptersLTE10Basic.negate();

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(numChaptersLTE10Basic.comparison()).isEqualTo(Operator.LESS_THAN_EQUAL);
soft.assertThat(numChaptersLTE10Basic.value()).isEqualTo(10);
soft.assertThat(numChaptersLTE10Basic.isNegated()).isEqualTo(false);

soft.assertThat(numChaptersGT10Basic.comparison()).isEqualTo(Operator.GREATER_THAN);
soft.assertThat(numChaptersGT10Basic.value()).isEqualTo(10);
soft.assertThat(numChaptersGT10Basic.isNegated()).isEqualTo(false);
});
}

@Test
void shouldSupportNegatedRestrictionUsingDefaultConstructor() {
BasicRestrictionRecord<String> restriction = new BasicRestrictionRecord<>("author", Operator.EQUAL, "Unknown");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@


class CompositeRestrictionRecordTest {

// A mock entity class for tests
static class Person {
}

@Test
void shouldCreateCompositeRestrictionWithDefaultNegation() {
Expand Down Expand Up @@ -71,6 +73,35 @@ void shouldFailIfEmptyRestrictions() {
.hasMessage("Cannot create a composite restriction without any restrictions to combine.");
}

@Test
void shouldNegateCompositeRestriction() {
Restriction<Person> ageLessThan50 = Restrict.lessThan(50, "age");
Restriction<Person> nameStartsWithDuke = Restrict.startsWith("Duke ", "name");
Restriction<Person> all = Restrict.all(ageLessThan50, nameStartsWithDuke);
Restriction<Person> allNegated = all.negate();
Restriction<Person> notAll = Restrict.not(all);

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(all.isNegated()).isEqualTo(false);
soft.assertThat(((CompositeRestriction<Person>) all).restrictions()
.get(0).isNegated()).isEqualTo(false);
soft.assertThat(((CompositeRestriction<Person>) all).restrictions()
.get(1).isNegated()).isEqualTo(false);

soft.assertThat(allNegated.isNegated()).isEqualTo(true);
soft.assertThat(((CompositeRestriction<Person>) allNegated).restrictions()
.get(0).isNegated()).isEqualTo(false);
soft.assertThat(((CompositeRestriction<Person>) allNegated).restrictions()
.get(1).isNegated()).isEqualTo(false);

soft.assertThat(notAll.isNegated()).isEqualTo(true);
soft.assertThat(((CompositeRestriction<Person>) notAll).restrictions()
.get(0).isNegated()).isEqualTo(false);
soft.assertThat(((CompositeRestriction<Person>) notAll).restrictions()
.get(1).isNegated()).isEqualTo(false);
});
}

@Test
void shouldPreserveRestrictionsOrder() {
Restriction<String> restriction1 = new BasicRestrictionRecord<>("title", Operator.EQUAL, "Java Guide");
Expand Down
49 changes: 49 additions & 0 deletions api/src/test/java/jakarta/data/TextRestrictionRecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;

import jakarta.data.BasicRestrictionRecordTest.Book;

import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;


Expand Down Expand Up @@ -103,6 +105,53 @@ void shouldCreateTextRestrictionWithEscapedValue() {
});
}

@Test
void shouldNegateLikeRestriction() {
TextRestriction<Book> likeJakartaEE = Restrict.like("%Jakarta EE%", "title");
TextRestriction<Book> notLikeJakartaEE = likeJakartaEE.negate();
TextRestriction<Book> anyCaseNotLikeJakartaEE = likeJakartaEE.ignoreCase().negate();
TextRestriction<Book> notLikeJakartaEEAnyCase = likeJakartaEE.negate().ignoreCase();

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(likeJakartaEE.isNegated()).isFalse();
soft.assertThat(likeJakartaEE.comparison()).isEqualTo(Operator.LIKE);
soft.assertThat(likeJakartaEE.isCaseSensitive()).isTrue();

soft.assertThat(notLikeJakartaEE.isNegated()).isTrue();
soft.assertThat(notLikeJakartaEE.comparison()).isEqualTo(Operator.LIKE);
soft.assertThat(notLikeJakartaEE.isCaseSensitive()).isTrue();

soft.assertThat(anyCaseNotLikeJakartaEE.isNegated()).isTrue();
soft.assertThat(anyCaseNotLikeJakartaEE.comparison()).isEqualTo(Operator.LIKE);
soft.assertThat(anyCaseNotLikeJakartaEE.isCaseSensitive()).isFalse();

soft.assertThat(notLikeJakartaEEAnyCase.isNegated()).isTrue();
soft.assertThat(notLikeJakartaEEAnyCase.comparison()).isEqualTo(Operator.LIKE);
soft.assertThat(notLikeJakartaEEAnyCase.isCaseSensitive()).isFalse();
});
}

@Test
void shouldNegateNegatedRestriction() {
TextRestriction<Book> endsWithJakartaEE = Restrict.endsWith("Jakarta EE", "title");
TextRestriction<Book> notEndsWithJakartaEE = endsWithJakartaEE.negate();
TextRestriction<Book> notNotEndsWithJakartaEE = notEndsWithJakartaEE.negate();

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(endsWithJakartaEE.isNegated()).isFalse();
soft.assertThat(endsWithJakartaEE.comparison()).isEqualTo(Operator.LIKE);
soft.assertThat(endsWithJakartaEE.value()).isEqualTo("%Jakarta EE");

soft.assertThat(notEndsWithJakartaEE.isNegated()).isTrue();
soft.assertThat(notEndsWithJakartaEE.comparison()).isEqualTo(Operator.LIKE);
soft.assertThat(notEndsWithJakartaEE.value()).isEqualTo("%Jakarta EE");

soft.assertThat(notNotEndsWithJakartaEE.isNegated()).isFalse();
soft.assertThat(notNotEndsWithJakartaEE.comparison()).isEqualTo(Operator.LIKE);
soft.assertThat(notNotEndsWithJakartaEE.value()).isEqualTo("%Jakarta EE");
});
}

@Test
void shouldSupportNegationForTextRestriction() {
TextRestrictionRecord<String> restriction = new TextRestrictionRecord<>(
Expand Down

0 comments on commit 5240da6

Please sign in to comment.