From f5d3bb91143dd534ab0cda7089cf80b906a9dbc7 Mon Sep 17 00:00:00 2001 From: Nathan Rauh Date: Fri, 6 Dec 2024 08:47:44 -0600 Subject: [PATCH] Tests for double negation --- .../data/BasicRestrictionRecordTest.java | 35 +++++++++++++++++++ .../data/CompositeRestrictionRecordTest.java | 29 +++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java b/api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java index 6c540e46..ee41345e 100644 --- a/api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java +++ b/api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java @@ -82,6 +82,41 @@ void shouldNegateLTERestriction() { }); } + @Test + void shouldNegateNegatedRestriction() { + Restriction titleRestriction = + Restrict.equalTo("A Developer's Guide to Jakarta Data", "title"); + BasicRestriction titleRestrictionBasic = + (BasicRestriction) titleRestriction; + BasicRestriction negatedTitleRestrictionBasic = + (BasicRestriction) titleRestriction.negate(); + BasicRestriction negatedNegatedTitleRestrictionBasic = + (BasicRestriction) negatedTitleRestrictionBasic.negate(); + + SoftAssertions.assertSoftly(soft -> { + soft.assertThat(titleRestrictionBasic.comparison()) + .isEqualTo(Operator.EQUAL); + soft.assertThat(titleRestrictionBasic.value()) + .isEqualTo("A Developer's Guide to Jakarta Data"); + soft.assertThat(titleRestrictionBasic.isNegated()) + .isEqualTo(false); + + soft.assertThat(negatedTitleRestrictionBasic.comparison()) + .isEqualTo(Operator.EQUAL); + soft.assertThat(negatedTitleRestrictionBasic.value()) + .isEqualTo("A Developer's Guide to Jakarta Data"); + soft.assertThat(negatedTitleRestrictionBasic.isNegated()) + .isEqualTo(true); + + soft.assertThat(negatedNegatedTitleRestrictionBasic.comparison()) + .isEqualTo(Operator.EQUAL); + soft.assertThat(negatedNegatedTitleRestrictionBasic.value()) + .isEqualTo("A Developer's Guide to Jakarta Data"); + soft.assertThat(negatedNegatedTitleRestrictionBasic.isNegated()) + .isEqualTo(false); + }); + } + @Test void shouldSupportNegatedRestrictionUsingDefaultConstructor() { BasicRestrictionRecord restriction = new BasicRestrictionRecord<>("author", Operator.EQUAL, "Unknown"); diff --git a/api/src/test/java/jakarta/data/CompositeRestrictionRecordTest.java b/api/src/test/java/jakarta/data/CompositeRestrictionRecordTest.java index b80c182c..c4acf781 100644 --- a/api/src/test/java/jakarta/data/CompositeRestrictionRecordTest.java +++ b/api/src/test/java/jakarta/data/CompositeRestrictionRecordTest.java @@ -102,6 +102,35 @@ void shouldNegateCompositeRestriction() { }); } + @Test + void shouldNegateNegatedCompositeRestriction() { + Restriction ageBetween20and30 = Restrict.between(20, 30, "age"); + Restriction nameContainsDuke = Restrict.contains("Duke", "name"); + Restriction any = Restrict.any(ageBetween20and30, nameContainsDuke); + Restriction anyNegated = any.negate(); + Restriction anyNotNegated = Restrict.not(anyNegated); + + SoftAssertions.assertSoftly(soft -> { + soft.assertThat(any.isNegated()).isEqualTo(false); + soft.assertThat(((CompositeRestriction) any).restrictions() + .get(0).isNegated()).isEqualTo(false); + soft.assertThat(((CompositeRestriction) any).restrictions() + .get(1).isNegated()).isEqualTo(false); + + soft.assertThat(anyNegated.isNegated()).isEqualTo(true); + soft.assertThat(((CompositeRestriction) anyNegated).restrictions() + .get(0).isNegated()).isEqualTo(false); + soft.assertThat(((CompositeRestriction) anyNegated).restrictions() + .get(1).isNegated()).isEqualTo(false); + + soft.assertThat(anyNotNegated.isNegated()).isEqualTo(false); + soft.assertThat(((CompositeRestriction) anyNotNegated).restrictions() + .get(0).isNegated()).isEqualTo(false); + soft.assertThat(((CompositeRestriction) anyNotNegated).restrictions() + .get(1).isNegated()).isEqualTo(false); + }); + } + @Test void shouldPreserveRestrictionsOrder() { Restriction restriction1 = new BasicRestrictionRecord<>("title", Operator.EQUAL, "Java Guide");