forked from jakartaee/data
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from soujava/829-dynamic-query-restrictions
Create test scenarions
- Loading branch information
Showing
5 changed files
with
483 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package jakarta.data; | ||
|
||
import org.assertj.core.api.SoftAssertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
|
||
class BasicRestrictionRecordTest { | ||
|
||
@Test | ||
void shouldCreateBasicRestrictionWithDefaultNegation() { | ||
BasicRestrictionRecord<String> restriction = new BasicRestrictionRecord<>("title", Operator.EQUAL, "Java Guide"); | ||
|
||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(restriction.field()).isEqualTo("title"); | ||
soft.assertThat(restriction.isNegated()).isFalse(); | ||
soft.assertThat(restriction.comparison()).isEqualTo(Operator.EQUAL); | ||
soft.assertThat(restriction.value()).isEqualTo("Java Guide"); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldCreateBasicRestrictionWithExplicitNegation() { | ||
BasicRestrictionRecord<String> restriction = new BasicRestrictionRecord<>("title", true, Operator.EQUAL, "Java Guide"); | ||
|
||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(restriction.field()).isEqualTo("title"); | ||
soft.assertThat(restriction.isNegated()).isTrue(); | ||
soft.assertThat(restriction.comparison()).isEqualTo(Operator.EQUAL); | ||
soft.assertThat(restriction.value()).isEqualTo("Java Guide"); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldCreateBasicRestrictionWithNullValue() { | ||
// Create a restriction with a null value | ||
BasicRestrictionRecord<String> restriction = new BasicRestrictionRecord<>("title", Operator.EQUAL, null); | ||
|
||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(restriction.field()).isEqualTo("title"); | ||
soft.assertThat(restriction.isNegated()).isFalse(); | ||
soft.assertThat(restriction.comparison()).isEqualTo(Operator.EQUAL); | ||
soft.assertThat(restriction.value()).isNull(); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldSupportNegatedRestrictionUsingDefaultConstructor() { | ||
BasicRestrictionRecord<String> restriction = new BasicRestrictionRecord<>("author", Operator.EQUAL, "Unknown"); | ||
BasicRestrictionRecord<String> negatedRestriction = new BasicRestrictionRecord<>(restriction.field(), true, restriction.comparison(), restriction.value()); | ||
|
||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(negatedRestriction.field()).isEqualTo("author"); | ||
soft.assertThat(negatedRestriction.isNegated()).isTrue(); | ||
soft.assertThat(negatedRestriction.comparison()).isEqualTo(Operator.EQUAL); | ||
soft.assertThat(negatedRestriction.value()).isEqualTo("Unknown"); | ||
}); | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
api/src/test/java/jakarta/data/CompositeRestrictionRecordTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package jakarta.data; | ||
|
||
|
||
import org.assertj.core.api.SoftAssertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
|
||
class CompositeRestrictionRecordTest { | ||
|
||
|
||
@Test | ||
void shouldCreateCompositeRestrictionWithDefaultNegation() { | ||
Restriction<String> restriction1 = new BasicRestrictionRecord<>("title", Operator.EQUAL, "Java Guide"); | ||
Restriction<String> restriction2 = new BasicRestrictionRecord<>("author", Operator.EQUAL, "John Doe"); | ||
|
||
CompositeRestrictionRecord<String> composite = new CompositeRestrictionRecord<>( | ||
CompositeRestriction.Type.ALL, | ||
List.of(restriction1, restriction2) | ||
); | ||
|
||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(composite.type()).isEqualTo(CompositeRestriction.Type.ALL); | ||
soft.assertThat(composite.restrictions()).containsExactly(restriction1, restriction2); | ||
soft.assertThat(composite.isNegated()).isFalse(); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldCreateCompositeRestrictionWithExplicitNegation() { | ||
Restriction<String> restriction1 = new BasicRestrictionRecord<>("title", Operator.EQUAL, "Java Guide"); | ||
Restriction<String> restriction2 = new BasicRestrictionRecord<>("author", Operator.EQUAL, "John Doe"); | ||
|
||
CompositeRestrictionRecord<String> composite = new CompositeRestrictionRecord<>( | ||
CompositeRestriction.Type.ANY, | ||
List.of(restriction1, restriction2), | ||
true | ||
); | ||
|
||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(composite.type()).isEqualTo(CompositeRestriction.Type.ANY); | ||
soft.assertThat(composite.restrictions()).containsExactly(restriction1, restriction2); | ||
soft.assertThat(composite.isNegated()).isTrue(); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldHandleEmptyRestrictions() { | ||
CompositeRestrictionRecord<String> composite = new CompositeRestrictionRecord<>( | ||
CompositeRestriction.Type.ALL, | ||
List.of() | ||
); | ||
|
||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(composite.type()).isEqualTo(CompositeRestriction.Type.ALL); | ||
soft.assertThat(composite.restrictions()).isEmpty(); | ||
soft.assertThat(composite.isNegated()).isFalse(); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldPreserveRestrictionsOrder() { | ||
Restriction<String> restriction1 = new BasicRestrictionRecord<>("title", Operator.EQUAL, "Java Guide"); | ||
Restriction<String> restriction2 = new BasicRestrictionRecord<>("author", Operator.EQUAL, "John Doe"); | ||
|
||
CompositeRestrictionRecord<String> composite = new CompositeRestrictionRecord<>( | ||
CompositeRestriction.Type.ALL, | ||
List.of(restriction1, restriction2) | ||
); | ||
|
||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(composite.restrictions().get(0)).isEqualTo(restriction1); | ||
soft.assertThat(composite.restrictions().get(1)).isEqualTo(restriction2); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldSupportNegationUsingDefaultConstructor() { | ||
// Given multiple restrictions | ||
Restriction<String> restriction1 = new BasicRestrictionRecord<>("title", Operator.EQUAL, "Java Guide"); | ||
Restriction<String> restriction2 = new BasicRestrictionRecord<>("author", Operator.EQUAL, "John Doe"); | ||
|
||
// When creating a composite restriction and manually setting negation | ||
CompositeRestrictionRecord<String> composite = new CompositeRestrictionRecord<>( | ||
CompositeRestriction.Type.ALL, | ||
List.of(restriction1, restriction2) | ||
); | ||
CompositeRestrictionRecord<String> negatedComposite = new CompositeRestrictionRecord<>( | ||
composite.type(), | ||
composite.restrictions(), | ||
true | ||
); | ||
|
||
// Then validate the negated composite restriction | ||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(negatedComposite.type()).isEqualTo(CompositeRestriction.Type.ALL); | ||
soft.assertThat(negatedComposite.restrictions()).containsExactly(restriction1, restriction2); | ||
soft.assertThat(negatedComposite.isNegated()).isTrue(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package jakarta.data; | ||
|
||
import org.assertj.core.api.SoftAssertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; | ||
|
||
class RestrictTest { | ||
|
||
|
||
@Test | ||
void shouldCreateEqualToRestriction() { | ||
Restriction<String> restriction = Restrict.equalTo("value", "field"); | ||
|
||
assertThat(restriction).isInstanceOf(TextRestrictionRecord.class); | ||
|
||
TextRestrictionRecord<String> basic = (TextRestrictionRecord<String>) restriction; | ||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(basic.field()).isEqualTo("field"); | ||
soft.assertThat(basic.comparison()).isEqualTo(Operator.EQUAL); | ||
soft.assertThat(basic.value()).isEqualTo("value"); | ||
soft.assertThat(basic.isNegated()).isFalse(); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldCreateNotEqualToRestriction() { | ||
Restriction<String> restriction = Restrict.notEqualTo("value", "field"); | ||
|
||
assertThat(restriction).isInstanceOf(TextRestrictionRecord.class); | ||
|
||
TextRestrictionRecord<String> basic = (TextRestrictionRecord<String>) restriction; | ||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(basic.field()).isEqualTo("field"); | ||
soft.assertThat(basic.comparison()).isEqualTo(Operator.EQUAL); | ||
soft.assertThat(basic.value()).isEqualTo("value"); | ||
soft.assertThat(basic.isNegated()).isTrue(); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldCombineAllRestrictionsWithNegation() { | ||
Restriction<String> r1 = Restrict.notEqualTo("value1", "field1"); | ||
Restriction<String> r2 = Restrict.greaterThan(100, "field2"); | ||
|
||
Restriction<String> combined = Restrict.all(r1, r2); | ||
|
||
assertThat(combined).isInstanceOf(CompositeRestrictionRecord.class); | ||
|
||
CompositeRestrictionRecord<String> composite = (CompositeRestrictionRecord<String>) combined; | ||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(composite.type()).isEqualTo(CompositeRestriction.Type.ALL); | ||
soft.assertThat(composite.restrictions()).containsExactly(r1, r2); | ||
soft.assertThat(composite.isNegated()).isFalse(); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldCreateContainsRestriction() { | ||
TextRestriction<String> restriction = Restrict.contains("substring", "field"); | ||
|
||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(restriction.field()).isEqualTo("field"); | ||
soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); | ||
soft.assertThat(restriction.value()).isEqualTo("%substring%"); | ||
soft.assertThat(restriction.isNegated()).isFalse(); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldCreateNegatedContainsRestriction() { | ||
TextRestriction<String> restriction = Restrict.notContains("substring", "field"); | ||
|
||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(restriction.field()).isEqualTo("field"); | ||
soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); | ||
soft.assertThat(restriction.value()).isEqualTo("%substring%"); | ||
soft.assertThat(restriction.isNegated()).isTrue(); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldCreateStartsWithRestriction() { | ||
TextRestriction<String> restriction = Restrict.startsWith("prefix", "field"); | ||
|
||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(restriction.field()).isEqualTo("field"); | ||
soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); | ||
soft.assertThat(restriction.value()).isEqualTo("prefix%"); | ||
soft.assertThat(restriction.isNegated()).isFalse(); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldCreateNegatedStartsWithRestriction() { | ||
TextRestriction<String> restriction = Restrict.notStartsWith("prefix", "field"); | ||
|
||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(restriction.field()).isEqualTo("field"); | ||
soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); | ||
soft.assertThat(restriction.value()).isEqualTo("prefix%"); | ||
soft.assertThat(restriction.isNegated()).isTrue(); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldCreateEndsWithRestriction() { | ||
TextRestriction<String> restriction = Restrict.endsWith("suffix", "field"); | ||
|
||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(restriction.field()).isEqualTo("field"); | ||
soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); | ||
soft.assertThat(restriction.value()).isEqualTo("%suffix"); | ||
soft.assertThat(restriction.isNegated()).isFalse(); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldCreateNegatedEndsWithRestriction() { | ||
TextRestriction<String> restriction = Restrict.notEndsWith("suffix", "field"); | ||
|
||
SoftAssertions.assertSoftly(soft -> { | ||
soft.assertThat(restriction.field()).isEqualTo("field"); | ||
soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); | ||
soft.assertThat(restriction.value()).isEqualTo("%suffix"); | ||
soft.assertThat(restriction.isNegated()).isTrue(); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldEscapeToLikePatternCorrectly() { | ||
String result = invokeToLikeEscaped('_', '%', true, "test_value", false); | ||
|
||
assertThat(result).isEqualTo("%test\\_value"); | ||
} | ||
|
||
@Test | ||
void shouldThrowExceptionForInvalidWildcard() { | ||
assertThatThrownBy(() -> invokeToLikeEscaped('_', '_', true, "value", false)) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("Cannot use the same character (_) for both types of wildcards."); | ||
} | ||
|
||
private String invokeToLikeEscaped(char charWildcard, char stringWildcard, boolean allowPrevious, String literal, boolean allowSubsequent) { | ||
return Restrict.toLikeEscaped(charWildcard, stringWildcard, allowPrevious, literal, allowSubsequent); | ||
} | ||
} |
Oops, something went wrong.