/
DefaultElementConditionTest.java
241 lines (200 loc) · 9.17 KB
/
DefaultElementConditionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
* You may elect to redistribute this code under either of these licenses.
*
*/
package org.eclipse.jnosql.communication.semistructured;
import org.eclipse.jnosql.communication.Condition;
import org.eclipse.jnosql.communication.TypeReference;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
class DefaultElementConditionTest {
private final CriteriaCondition lte = CriteriaCondition.lte(Element.of("salary", 10.32));
@Test
void shouldReturnErrorWhenColumnIsNull() {
assertThrows(NullPointerException.class, () -> CriteriaCondition.of(null, Condition.AND));
}
@Test
void shouldCreateAnInstance() {
Element name = Element.of("name", "Otavio");
CriteriaCondition condition = CriteriaCondition.of(name, Condition.EQUALS);
assertNotNull(condition);
assertEquals(name, condition.element());
assertEquals(Condition.EQUALS, condition.condition());
}
@Test
void shouldCreateNegationCondition() {
Element age = Element.of("age", 26);
CriteriaCondition condition = CriteriaCondition.of(age, Condition.GREATER_THAN);
CriteriaCondition negate = condition.negate();
Element negateElement = negate.element();
assertEquals(Condition.NOT, negate.condition());
assertEquals(Condition.NOT.getNameField(), negateElement.name());
assertEquals(CriteriaCondition.of(age, Condition.GREATER_THAN), negateElement.value().get());
}
@Test
void shouldReturnValidDoubleNegation() {
Element age = Element.of("age", 26);
CriteriaCondition condition = CriteriaCondition.of(age, Condition.GREATER_THAN);
CriteriaCondition affirmative = condition.negate().negate();
Assertions.assertEquals(condition, affirmative);
}
@Test
void shouldCreateAndCondition() {
Element age = Element.of("age", 26);
Element name = Element.of("name", "Otavio");
CriteriaCondition condition1 = CriteriaCondition.of(name, Condition.EQUALS);
CriteriaCondition condition2 = CriteriaCondition.of(age, Condition.GREATER_THAN);
CriteriaCondition and = condition1.and(condition2);
Element andElement = and.element();
assertEquals(Condition.AND, and.condition());
assertEquals(Condition.AND.getNameField(), andElement.name());
assertThat(andElement.value().get(new TypeReference<List<CriteriaCondition>>() {
})).contains(condition1, condition2);
}
@Test
void shouldCreateOrCondition() {
Element age = Element.of("age", 26);
Element name = Element.of("name", "Otavio");
CriteriaCondition condition1 = CriteriaCondition.of(name, Condition.EQUALS);
CriteriaCondition condition2 = CriteriaCondition.of(age, Condition.GREATER_THAN);
CriteriaCondition and = condition1.or(condition2);
Element andElement = and.element();
assertEquals(Condition.OR, and.condition());
assertEquals(Condition.OR.getNameField(), andElement.name());
assertThat(andElement.value().get(new TypeReference<List<CriteriaCondition>>() {
})).contains(condition1, condition2);
}
@Test
void shouldReturnErrorWhenCreateAndWithNullValues() {
assertThrows(NullPointerException.class, () -> CriteriaCondition.and((CriteriaCondition[]) null));
}
@Test
void shouldReturnErrorWhenCreateOrWithNullValues() {
assertThrows(NullPointerException.class, () -> CriteriaCondition.or((CriteriaCondition[]) null));
}
@Test
void shouldAppendAnd() {
CriteriaCondition eq = CriteriaCondition.eq(Element.of("name", "otavio"));
CriteriaCondition gt = CriteriaCondition.gt(Element.of("age", 10));
CriteriaCondition and = CriteriaCondition.and(eq, gt);
assertEquals(Condition.AND, and.condition());
List<CriteriaCondition> conditions = and.element().get(new TypeReference<>() {
});
assertThat(conditions).contains(eq, gt);
}
@Test
void shouldAppendOr() {
CriteriaCondition eq = CriteriaCondition.eq(Element.of("name", "otavio"));
CriteriaCondition gt = CriteriaCondition.gt(Element.of("age", 10));
CriteriaCondition and = CriteriaCondition.or(eq, gt);
assertEquals(Condition.OR, and.condition());
List<CriteriaCondition> conditions = and.element().get(new TypeReference<>() {
});
assertThat(conditions).contains(eq, gt);
}
@Test
void shouldAnd() {
CriteriaCondition eq = CriteriaCondition.eq(Element.of("name", "otavio"));
CriteriaCondition gt = CriteriaCondition.gt(Element.of("age", 10));
CriteriaCondition lte = CriteriaCondition.lte(Element.of("salary", 10_000.00));
CriteriaCondition and = eq.and(gt);
List<CriteriaCondition> conditions = and.element().get(new TypeReference<>() {
});
assertEquals(Condition.AND, and.condition());
assertThat(conditions).contains(eq, gt);
CriteriaCondition result = and.and(lte);
assertEquals(Condition.AND, result.condition());
assertThat(result.element().get(new TypeReference<List<CriteriaCondition>>() {
})).contains(eq, gt, lte);
}
@Test
void shouldOr() {
CriteriaCondition eq = CriteriaCondition.eq(Element.of("name", "otavio"));
CriteriaCondition gt = CriteriaCondition.gt(Element.of("age", 10));
CriteriaCondition lte = CriteriaCondition.lte(Element.of("salary", 10_000.00));
CriteriaCondition or = eq.or(gt);
List<CriteriaCondition> conditions = or.element().get(new TypeReference<>() {
});
assertEquals(Condition.OR, or.condition());
assertThat(conditions).contains(eq, gt);
CriteriaCondition result = or.or(lte);
assertEquals(Condition.OR, result.condition());
assertThat(result.element().get(new TypeReference<List<CriteriaCondition>>() {
})).contains(eq, gt, lte);
}
@Test
void shouldNegate() {
CriteriaCondition eq = CriteriaCondition.eq(Element.of("name", "otavio"));
CriteriaCondition negate = eq.negate();
assertEquals(Condition.NOT, negate.condition());
CriteriaCondition condition = negate.element().get(CriteriaCondition.class);
assertEquals(eq, condition);
}
@Test
void shouldAffirmDoubleNegate() {
CriteriaCondition eq = CriteriaCondition.eq(Element.of("name", "otavio"));
CriteriaCondition affirm = eq.negate().negate();
assertEquals(eq.condition(), affirm.condition());
}
@Test
void shouldReturnErrorWhenBetweenIsNull() {
assertThrows(NullPointerException.class, () -> CriteriaCondition.between(null));
}
@Test
void shouldReturnErrorWhenBetweenIsNotIterable() {
assertThrows(IllegalArgumentException.class, () -> {
Element element = Element.of("age", 12);
CriteriaCondition.between(element);
});
}
@Test
void shouldReturnErrorWhenIterableHasOneElement() {
assertThrows(IllegalArgumentException.class, () -> {
Element element = Element.of("age", Collections.singleton(12));
CriteriaCondition.between(element);
});
}
@Test
void shouldReturnErrorWhenIterableHasMoreThanTwoElement2() {
assertThrows(IllegalArgumentException.class, () -> {
Element element = Element.of("age", Arrays.asList(12, 12, 12));
CriteriaCondition.between(element);
});
}
@Test
void shouldReturnBetween() {
Element element = Element.of("age", Arrays.asList(12, 13));
CriteriaCondition between = CriteriaCondition.between(element);
assertEquals(Condition.BETWEEN, between.condition());
Iterable<Integer> integers = between.element().get(new TypeReference<>() {
});
assertThat(integers).contains(12, 13);
}
@Test
void shouldReturnErrorWhenInConditionIsInvalid() {
assertThrows(NullPointerException.class, () -> CriteriaCondition.in(null));
assertThrows(IllegalArgumentException.class, () -> CriteriaCondition.in(Element.of("value", 10)));
}
@Test
void shouldReturnInClause() {
Element element = Element.of("age", Arrays.asList(12, 13));
CriteriaCondition in = CriteriaCondition.in(element);
assertEquals(Condition.IN, in.condition());
Iterable<Integer> integers = in.element().get(new TypeReference<>() {
});
assertThat(integers).contains(12, 13);
}
}