/
DefaultDocumentConditionTest.java
245 lines (204 loc) · 9.63 KB
/
DefaultDocumentConditionTest.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
242
243
244
245
/*
*
* Copyright (c) 2017 Otávio Santana and others
* 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.
*
* Contributors:
*
* Otavio Santana
*
*/
package org.eclipse.jnosql.diana.document;
import jakarta.nosql.Condition;
import jakarta.nosql.document.Document;
import jakarta.nosql.document.DocumentCondition;
import org.hamcrest.Matchers;
import jakarta.nosql.TypeReference;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class DefaultDocumentConditionTest {
@Test
public void shouldReturnErrorWhenDocumentIsNull() {
assertThrows(NullPointerException.class, () -> DefaultDocumentCondition.of(null, Condition.AND));
}
@Test
public void shouldCreateAnInstance() {
Document name = Document.of("name", "Otavio");
DocumentCondition condition = DefaultDocumentCondition.of(name, Condition.EQUALS);
assertNotNull(condition);
assertEquals(name, condition.getDocument());
assertEquals(Condition.EQUALS, condition.getCondition());
}
@Test
public void shouldCreateNegationConditon() {
Document age = Document.of("age", 26);
DocumentCondition condition = DefaultDocumentCondition.of(age, Condition.GREATER_THAN);
DocumentCondition negate = condition.negate();
Document negateDocument = negate.getDocument();
assertEquals(Condition.NOT, negate.getCondition());
assertEquals(Condition.NOT.getNameField(), negateDocument.getName());
assertEquals(DefaultDocumentCondition.of(age, Condition.GREATER_THAN), negateDocument.getValue().get());
}
@Test
public void shouldCreateAndCondition() {
Document age = Document.of("age", 26);
Document name = Document.of("name", "Otavio");
DocumentCondition condition1 = DefaultDocumentCondition.of(name, Condition.EQUALS);
DocumentCondition condition2 = DefaultDocumentCondition.of(age, Condition.GREATER_THAN);
DocumentCondition and = condition1.and(condition2);
Document andDocument = and.getDocument();
assertEquals(Condition.AND, and.getCondition());
assertEquals(Condition.AND.getNameField(), andDocument.getName());
assertThat(andDocument.getValue().get(new TypeReference<List<DocumentCondition>>() {
}),
Matchers.containsInAnyOrder(condition1, condition2));
}
@Test
public void shouldCreateOrCondition() {
Document age = Document.of("age", 26);
Document name = Document.of("name", "Otavio");
DocumentCondition condition1 = DefaultDocumentCondition.of(name, Condition.EQUALS);
DocumentCondition condition2 = DefaultDocumentCondition.of(age, Condition.GREATER_THAN);
DocumentCondition and = condition1.or(condition2);
Document andDocument = and.getDocument();
assertEquals(Condition.OR, and.getCondition());
assertEquals(Condition.OR.getNameField(), andDocument.getName());
assertThat(andDocument.getValue().get(new TypeReference<List<DocumentCondition>>() {
}),
Matchers.containsInAnyOrder(condition1, condition2));
}
@Test
public void shouldReturnErrorWhenCreateAndWithNullValues() {
assertThrows(NullPointerException.class, () -> DefaultDocumentCondition.and((DocumentCondition[]) null));
}
@Test
public void shouldReturnErrorWhenCreateOrWithNullValues() {
assertThrows(NullPointerException.class, () -> DefaultDocumentCondition.or((DocumentCondition[]) null));
}
@Test
public void shouldAppendAnd() {
DocumentCondition eq = DocumentCondition.eq(Document.of("name", "otavio"));
DocumentCondition gt = DocumentCondition.gt(Document.of("age", 10));
DocumentCondition and = DocumentCondition.and(eq, gt);
assertEquals(Condition.AND, and.getCondition());
List<DocumentCondition> conditions = and.getDocument().get(new TypeReference<List<DocumentCondition>>() {
});
assertThat(conditions, Matchers.containsInAnyOrder(eq, gt));
}
@Test
public void shouldAppendOr() {
DocumentCondition eq = DocumentCondition.eq(Document.of("name", "otavio"));
DocumentCondition gt = DocumentCondition.gt(Document.of("age", 10));
DocumentCondition and = DocumentCondition.or(eq, gt);
assertEquals(Condition.OR, and.getCondition());
List<DocumentCondition> conditions = and.getDocument().get(new TypeReference<List<DocumentCondition>>() {
});
assertThat(conditions, Matchers.containsInAnyOrder(eq, gt));
}
@Test
public void shouldAnd() {
DocumentCondition eq = DocumentCondition.eq(Document.of("name", "otavio"));
DocumentCondition gt = DocumentCondition.gt(Document.of("age", 10));
DocumentCondition lte = DocumentCondition.lte(Document.of("salary", 10_000.00));
DocumentCondition and = eq.and(gt);
List<DocumentCondition> conditions = and.getDocument().get(new TypeReference<List<DocumentCondition>>() {
});
assertEquals(Condition.AND, and.getCondition());
assertThat(conditions, Matchers.containsInAnyOrder(eq, gt));
DocumentCondition result = and.and(lte);
assertEquals(Condition.AND, result.getCondition());
assertThat(result.getDocument().get(new TypeReference<List<DocumentCondition>>() {
}), Matchers.containsInAnyOrder(eq, gt, lte));
}
@Test
public void shouldOr() {
DocumentCondition eq = DocumentCondition.eq(Document.of("name", "otavio"));
DocumentCondition gt = DocumentCondition.gt(Document.of("age", 10));
DocumentCondition lte = DocumentCondition.lte(Document.of("salary", 10_000.00));
DocumentCondition or = eq.or(gt);
List<DocumentCondition> conditions = or.getDocument().get(new TypeReference<List<DocumentCondition>>() {
});
assertEquals(Condition.OR, or.getCondition());
assertThat(conditions, Matchers.containsInAnyOrder(eq, gt));
DocumentCondition result = or.or(lte);
assertEquals(Condition.OR, result.getCondition());
assertThat(result.getDocument().get(new TypeReference<List<DocumentCondition>>() {
}), Matchers.containsInAnyOrder(eq, gt, lte));
}
@Test
public void shouldNegate() {
DocumentCondition eq = DocumentCondition.eq(Document.of("name", "otavio"));
DocumentCondition negate = eq.negate();
assertEquals(Condition.NOT, negate.getCondition());
DocumentCondition condition = negate.getDocument().get(DocumentCondition.class);
assertEquals(eq, condition);
}
@Test
public void shouldAfirmeDoubleNegate() {
DocumentCondition eq = DocumentCondition.eq(Document.of("name", "otavio"));
DocumentCondition afirmative = eq.negate().negate();
assertEquals(eq.getCondition(), afirmative.getCondition());
}
@Test
public void shouldReturnErrorWhenBetweenIsNull() {
assertThrows(NullPointerException.class, () -> DocumentCondition.between(null));
}
@Test
public void shouldReturnErrorWhenBetweenIsNotIterable() {
assertThrows(IllegalArgumentException.class, () -> {
Document document = Document.of("age", 12);
DocumentCondition.between(document);
});
}
@Test
public void shouldReturnErrorWhenIterableHasOneElement() {
assertThrows(IllegalArgumentException.class, () -> {
Document document = Document.of("age", Collections.singleton(12));
DocumentCondition.between(document);
});
}
@Test
public void shouldReturnErrorWhenIterableHasMoreThanTwoElement2() {
assertThrows(IllegalArgumentException.class, () -> {
Document document = Document.of("age", Arrays.asList(12, 12, 12));
DocumentCondition.between(document);
});
}
@Test
public void shouldReturnBetween() {
Document document = Document.of("age", Arrays.asList(12, 13));
DocumentCondition between = DocumentCondition.between(document);
assertEquals(Condition.BETWEEN, between.getCondition());
Iterable<Integer> integers = between.getDocument().get(new TypeReference<Iterable<Integer>>() {
});
assertThat(integers, contains(12, 13));
}
@Test
public void shouldReturnErrorWhenInConditionIsInvalid() {
assertThrows(NullPointerException.class, () -> DocumentCondition.in(null));
assertThrows(IllegalArgumentException.class, () -> DocumentCondition.in(Document.of("value", 10)));
}
@Test
public void shouldReturnInClause() {
Document column = Document.of("age", Arrays.asList(12, 13));
DocumentCondition in = DocumentCondition.in(column);
assertEquals(Condition.IN, in.getCondition());
Iterable<Integer> integers = in.getDocument().get(new TypeReference<Iterable<Integer>>() {
});
assertThat(integers, contains(12, 13));
}
}