Skip to content

Commit d0a85c4

Browse files
committed
Unit test for empty restriction
1 parent fbfe515 commit d0a85c4

File tree

1 file changed

+77
-14
lines changed

1 file changed

+77
-14
lines changed

api/src/test/java/jakarta/data/metamodel/restrict/RestrictTest.java

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,69 @@
2020
import org.assertj.core.api.SoftAssertions;
2121
import org.junit.jupiter.api.Test;
2222

23+
import jakarta.data.metamodel.SortableAttribute;
24+
import jakarta.data.metamodel.TextAttribute;
25+
import jakarta.data.metamodel.impl.SortableAttributeRecord;
26+
import jakarta.data.metamodel.impl.TextAttributeRecord;
27+
2328
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
2429
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
2530

31+
import java.util.List;
32+
2633
class RestrictTest {
34+
// Mock static metamodel class for tests
35+
interface _Employee {
36+
String BADGENUM = "badgeNum";
37+
String NAME = "name";
38+
String POSITION = "position";
39+
String YEARHIRED = "yearHired";
40+
41+
SortableAttribute<Employee> badgeNum = new SortableAttributeRecord<>(BADGENUM);
42+
TextAttribute<Employee> name = new TextAttributeRecord<>(NAME);
43+
TextAttribute<Employee> position = new TextAttributeRecord<>(POSITION);
44+
SortableAttribute<Employee> yearHired = new SortableAttributeRecord<>(YEARHIRED);
45+
}
46+
47+
// Mock entity class for tests
48+
class Employee {
49+
int badgeNum;
50+
String name;
51+
String position;
52+
int yearHired;
53+
}
54+
55+
/**
56+
* Mock repository method for tests.
57+
*/
58+
List<Employee> findByPosition(String position,
59+
Restriction<Employee> restriction) {
60+
return List.of();
61+
}
62+
63+
@Test
64+
void shouldCreateEmptyRestriction() {
65+
Restriction<Employee> unrestricted = Restrict.none();
66+
67+
assertThat(unrestricted).isInstanceOf(EmptyRestriction.class);
2768

69+
EmptyRestriction<Employee> emptyRestriction =
70+
(EmptyRestriction<Employee>) unrestricted;
71+
72+
SoftAssertions.assertSoftly(soft -> {
73+
soft.assertThat(emptyRestriction.isNegated()).isEqualTo(false);
74+
soft.assertThat(emptyRestriction.restrictions()).isEmpty();
75+
soft.assertThat(emptyRestriction.type()).isEqualTo(CompositeRestriction.Type.ALL);
76+
});
77+
}
2878

2979
@Test
3080
void shouldCreateEqualToRestriction() {
31-
Restriction<String> restriction = Restrict.equalTo("value", "attributeName");
81+
Restriction<Employee> restriction = Restrict.equalTo("value", "attributeName");
3282

3383
assertThat(restriction).isInstanceOf(TextRestrictionRecord.class);
3484

35-
TextRestrictionRecord<String> basic = (TextRestrictionRecord<String>) restriction;
85+
TextRestrictionRecord<Employee> basic = (TextRestrictionRecord<Employee>) restriction;
3686
SoftAssertions.assertSoftly(soft -> {
3787
soft.assertThat(basic.attribute()).isEqualTo("attributeName");
3888
soft.assertThat(basic.comparison()).isEqualTo(Operator.EQUAL);
@@ -42,11 +92,11 @@ void shouldCreateEqualToRestriction() {
4292

4393
@Test
4494
void shouldCreateNotEqualToRestriction() {
45-
Restriction<String> restriction = Restrict.notEqualTo("value", "attributeName");
95+
Restriction<Employee> restriction = Restrict.notEqualTo("value", "attributeName");
4696

4797
assertThat(restriction).isInstanceOf(TextRestrictionRecord.class);
4898

49-
TextRestrictionRecord<String> basic = (TextRestrictionRecord<String>) restriction;
99+
TextRestrictionRecord<Employee> basic = (TextRestrictionRecord<Employee>) restriction;
50100
SoftAssertions.assertSoftly(soft -> {
51101
soft.assertThat(basic.attribute()).isEqualTo("attributeName");
52102
soft.assertThat(basic.comparison()).isEqualTo(Operator.NOT_EQUAL);
@@ -56,14 +106,14 @@ void shouldCreateNotEqualToRestriction() {
56106

57107
@Test
58108
void shouldCombineAllRestrictionsWithNegation() {
59-
Restriction<String> r1 = Restrict.notEqualTo("value1", "attributeName1");
60-
Restriction<String> r2 = Restrict.greaterThan(100, "attributeName2");
109+
Restriction<Employee> r1 = Restrict.notEqualTo("value1", "attributeName1");
110+
Restriction<Employee> r2 = Restrict.greaterThan(100, "attributeName2");
61111

62-
Restriction<String> combined = Restrict.all(r1, r2);
112+
Restriction<Employee> combined = Restrict.all(r1, r2);
63113

64114
assertThat(combined).isInstanceOf(CompositeRestrictionRecord.class);
65115

66-
CompositeRestrictionRecord<String> composite = (CompositeRestrictionRecord<String>) combined;
116+
CompositeRestrictionRecord<Employee> composite = (CompositeRestrictionRecord<Employee>) combined;
67117
SoftAssertions.assertSoftly(soft -> {
68118
soft.assertThat(composite.type()).isEqualTo(CompositeRestriction.Type.ALL);
69119
soft.assertThat(composite.restrictions()).containsExactly(r1, r2);
@@ -73,7 +123,7 @@ void shouldCombineAllRestrictionsWithNegation() {
73123

74124
@Test
75125
void shouldCreateContainsRestriction() {
76-
TextRestriction<String> restriction = Restrict.contains("substring", "attributeName");
126+
TextRestriction<Employee> restriction = Restrict.contains("substring", "attributeName");
77127

78128
SoftAssertions.assertSoftly(soft -> {
79129
soft.assertThat(restriction.attribute()).isEqualTo("attributeName");
@@ -84,7 +134,7 @@ void shouldCreateContainsRestriction() {
84134

85135
@Test
86136
void shouldCreateNegatedContainsRestriction() {
87-
TextRestriction<String> restriction = Restrict.notContains("substring", "attributeName");
137+
TextRestriction<Employee> restriction = Restrict.notContains("substring", "attributeName");
88138

89139
SoftAssertions.assertSoftly(soft -> {
90140
soft.assertThat(restriction.attribute()).isEqualTo("attributeName");
@@ -95,7 +145,7 @@ void shouldCreateNegatedContainsRestriction() {
95145

96146
@Test
97147
void shouldCreateStartsWithRestriction() {
98-
TextRestriction<String> restriction = Restrict.startsWith("prefix", "attributeName");
148+
TextRestriction<Employee> restriction = Restrict.startsWith("prefix", "attributeName");
99149

100150
SoftAssertions.assertSoftly(soft -> {
101151
soft.assertThat(restriction.attribute()).isEqualTo("attributeName");
@@ -106,7 +156,7 @@ void shouldCreateStartsWithRestriction() {
106156

107157
@Test
108158
void shouldCreateNegatedStartsWithRestriction() {
109-
TextRestriction<String> restriction = Restrict.notStartsWith("prefix", "attributeName");
159+
TextRestriction<Employee> restriction = Restrict.notStartsWith("prefix", "attributeName");
110160

111161
SoftAssertions.assertSoftly(soft -> {
112162
soft.assertThat(restriction.attribute()).isEqualTo("attributeName");
@@ -117,7 +167,7 @@ void shouldCreateNegatedStartsWithRestriction() {
117167

118168
@Test
119169
void shouldCreateEndsWithRestriction() {
120-
TextRestriction<String> restriction = Restrict.endsWith("suffix", "attributeName");
170+
TextRestriction<Employee> restriction = Restrict.endsWith("suffix", "attributeName");
121171

122172
SoftAssertions.assertSoftly(soft -> {
123173
soft.assertThat(restriction.attribute()).isEqualTo("attributeName");
@@ -128,7 +178,7 @@ void shouldCreateEndsWithRestriction() {
128178

129179
@Test
130180
void shouldCreateNegatedEndsWithRestriction() {
131-
TextRestriction<String> restriction = Restrict.notEndsWith("suffix", "attributeName");
181+
TextRestriction<Employee> restriction = Restrict.notEndsWith("suffix", "attributeName");
132182

133183
SoftAssertions.assertSoftly(soft -> {
134184
soft.assertThat(restriction.attribute()).isEqualTo("attributeName");
@@ -144,10 +194,23 @@ void shouldEscapeToLikePatternCorrectly() {
144194
assertThat(result).isEqualTo("%test\\_value");
145195
}
146196

197+
@Test
198+
void shouldSupplyEmptyRestrictionToRepositoryMethod() {
199+
this.findByPosition("Software Engineer", Restrict.none());
200+
}
201+
147202
@Test
148203
void shouldThrowExceptionForInvalidWildcard() {
149204
assertThatThrownBy(() -> Restrict.like("pattern_value", '_', '_', "attributeName"))
150205
.isInstanceOf(IllegalArgumentException.class)
151206
.hasMessage("Cannot use the same character (_) for both types of wildcards.");
152207
}
208+
209+
@Test
210+
void shouldThrowExceptionForNegatingEmptyRestriction() {
211+
Restriction<Employee> unrestricted = Restrict.none();
212+
assertThatThrownBy(() -> unrestricted.negate())
213+
.isInstanceOf(UnsupportedOperationException.class)
214+
.hasMessage("Empty restriction cannot be negated.");
215+
}
153216
}

0 commit comments

Comments
 (0)