1 package org.synchronoss.cpo.cassandra;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import static org.testng.Assert.*;
26
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.stream.Stream;
30 import org.synchronoss.cpo.core.CpoAdapter;
31 import org.synchronoss.cpo.core.CpoAdapterFactoryManager;
32 import org.synchronoss.cpo.core.CpoQuery;
33 import org.synchronoss.cpo.core.CpoWhere;
34 import org.synchronoss.cpo.core.enums.Comparison;
35 import org.synchronoss.cpo.core.enums.Logical;
36 import org.testng.annotations.AfterClass;
37 import org.testng.annotations.BeforeClass;
38 import org.testng.annotations.Test;
39
40
41
42
43
44
45 public class WhereTest {
46
47
48 private static final int IDB = 1200000;
49
50 private CpoAdapter cpoAdapter = null;
51 private ArrayList<ValueObject> al = new ArrayList<>();
52
53 public WhereTest() {}
54
55
56
57
58
59 @BeforeClass
60 public void setUp() {
61 String method = "setUp:";
62
63 try {
64 cpoAdapter = CpoAdapterFactoryManager.getCpoAdapter(CassandraStatics.ADAPTER_CONTEXT_DEFAULT);
65 assertNotNull(cpoAdapter, method + "CpoAdapter is null");
66 } catch (Exception e) {
67 fail(method + e.getMessage());
68 }
69 ValueObject vo = ValueObjectFactory.createValueObject(IDB + 1);
70 vo.setAttrVarChar("Test");
71 vo.setAttrInt(1);
72 vo.setAttrBigInt(1);
73 al.add(vo);
74 al.add(ValueObjectFactory.createValueObject(IDB + 2));
75 al.add(ValueObjectFactory.createValueObject(IDB + 3));
76 al.add(ValueObjectFactory.createValueObject(IDB + 4));
77 al.add(ValueObjectFactory.createValueObject(IDB + 5));
78 al.add(ValueObjectFactory.createValueObject(-(IDB + 6)));
79 try {
80 cpoAdapter.insertBeans("TestOrderByInsert", al);
81 } catch (Exception e) {
82 fail(method + e.getMessage());
83 }
84 }
85
86
87 @AfterClass
88 public void tearDown() {
89 String method = "tearDown:";
90 try {
91 cpoAdapter.deleteBeans("TestOrderByDelete", al);
92 } catch (Exception e) {
93 fail(method + e.getMessage());
94 }
95 cpoAdapter = null;
96 }
97
98 @Test
99 public void testStaticWhere() {
100 String method = "testStaticWhere:";
101 Collection<ValueObject> col;
102 CpoWhere cw;
103
104 try {
105 ValueObject valObj = ValueObjectFactory.createValueObject();
106 cw = cpoAdapter.newWhere(Logical.NONE, "id", Comparison.EQ, null);
107 cw.setStaticValue(String.valueOf(IDB + 3));
108 ArrayList<CpoWhere> wheres = new ArrayList<>();
109 wheres.add(cw);
110 try (Stream<ValueObject> beans =
111 cpoAdapter.retrieveBeans(
112 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
113 long count =
114 beans
115 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
116 .count();
117 assertEquals(count, 1, "Number of beans is " + count);
118 }
119 } catch (Exception e) {
120 fail(method + e.getMessage());
121 }
122 }
123
124 @Test
125 public void testValueWhere() {
126 String method = "testValueWhere:";
127 Collection<ValueObject> col;
128 CpoWhere cw;
129
130 try {
131 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 3);
132 cw = cpoAdapter.newWhere(Logical.NONE, "id", Comparison.EQ, valObj);
133
134 ArrayList<CpoWhere> wheres = new ArrayList<>();
135 wheres.add(cw);
136 try (Stream<ValueObject> beans =
137 cpoAdapter.retrieveBeans(
138 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
139 long count =
140 beans
141 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
142 .count();
143 assertEquals(count, 1, "Number of beans is " + count);
144 }
145 } catch (Exception e) {
146 fail(method + e.getMessage());
147 }
148 }
149
150 @Test
151 public void testNoMarkerWhere() {
152 String method = "testValueWhere:";
153 Collection<ValueObject> col;
154 CpoWhere cw;
155
156 try {
157 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 3);
158 cw = cpoAdapter.newWhere(Logical.NONE, "id", Comparison.EQ, valObj);
159
160 ArrayList<CpoWhere> wheres = new ArrayList<>();
161 wheres.add(cw);
162 try (Stream<ValueObject> beans =
163 cpoAdapter.retrieveBeans(
164 CpoQuery.group(ValueObject.FG_RETRIEVE_NULL).wheres(wheres), valObj)) {
165 long count =
166 beans
167 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
168 .count();
169 assertEquals(count, 1, "Number of beans is " + count);
170 }
171 } catch (Exception e) {
172 fail(method + e.getMessage());
173 }
174 }
175
176
177
178
179
180 @Test
181 public void testNoWhere() {
182 String method = "testNoWhere:";
183 Collection<ValueObject> col = null;
184
185 try {
186 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 3);
187 try (Stream<ValueObject> beans =
188 cpoAdapter.retrieveBeans(ValueObject.FG_LIST_TESTWHERERETRIEVE, valObj)) {
189 long count =
190 beans
191 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
192 .count();
193 assertEquals(count, 6, "Number of beans is " + count);
194 }
195 } catch (Exception e) {
196 fail(method + e.getMessage());
197 }
198 }
199
200 @Test
201 public void testAndWhere() {
202 String method = "testAndWhere:";
203
204 try {
205 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 1);
206 CpoWhere cw = cpoAdapter.newWhere();
207 CpoWhere cw1 = cpoAdapter.newWhere(Logical.NONE, "id", Comparison.EQ, IDB + 1);
208 CpoWhere cw2 = cpoAdapter.newWhere(Logical.AND, "attrInt", Comparison.EQ, 1);
209
210 cw.addWhere(cw1);
211 cw.addWhere(cw2);
212
213 ArrayList<CpoWhere> wheres = new ArrayList<>();
214 wheres.add(cw);
215 try (Stream<ValueObject> beans =
216 cpoAdapter.retrieveBeans(
217 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
218 long count =
219 beans
220 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
221 .count();
222 assertEquals(count, 1, "Number of beans is " + count);
223 }
224 } catch (Exception e) {
225 fail(method + e.getMessage());
226 }
227 }
228
229 @Test
230 public void testMultipleBindWhere() {
231 String method = "testMultipleBindWhere:";
232
233 try {
234 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 1);
235 valObj.setAttrInt(1);
236
237 CpoWhere cw = cpoAdapter.newWhere();
238 CpoWhere cw1 = cpoAdapter.newWhere(Logical.NONE, "attrInt", Comparison.EQ, valObj);
239 CpoWhere cw2 = cpoAdapter.newWhere(Logical.AND, "id", Comparison.EQ, valObj);
240
241 cw.addWhere(cw1);
242 cw.addWhere(cw2);
243
244 ArrayList<CpoWhere> wheres = new ArrayList<>();
245 wheres.add(cw);
246 try (Stream<ValueObject> beans =
247 cpoAdapter.retrieveBeans(
248 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
249 long count =
250 beans
251 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
252 .count();
253 assertEquals(count, 1, "Number of beans is " + count);
254 }
255 } catch (Exception e) {
256 fail(method + e.getMessage());
257 }
258 }
259
260 @Test
261 public void testNonAttributeWhere() {
262 String method = "testNonAttributeWhere:";
263
264 try {
265 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 1);
266
267 CpoWhere cw = cpoAdapter.newWhere();
268 CpoWhere cw1 = cpoAdapter.newWhere(Logical.NONE, "id", Comparison.EQ, IDB + 1);
269
270 cw.addWhere(cw1);
271
272 ArrayList<CpoWhere> wheres = new ArrayList<>();
273 wheres.add(cw);
274 try (Stream<ValueObject> beans =
275 cpoAdapter.retrieveBeans(
276 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
277 long count =
278 beans
279 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
280 .count();
281 assertEquals(count, 1, "Number of beans is " + count);
282 }
283 } catch (Exception e) {
284 fail(method + e.getMessage());
285 }
286 }
287
288 @Test
289 public void testInWhereStaticValue() {
290 String method = "testNonAttributeWhere:";
291
292 try {
293 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 1);
294
295 CpoWhere cw = cpoAdapter.newWhere();
296 CpoWhere cw1 = cpoAdapter.newWhere(Logical.NONE, "id", Comparison.IN, null);
297 cw1.setStaticValue("(" + (IDB + 1) + "," + (IDB + 3) + "," + (IDB + 5) + ")");
298
299 cw.addWhere(cw1);
300
301 ArrayList<CpoWhere> wheres = new ArrayList<>();
302 wheres.add(cw);
303 try (Stream<ValueObject> beans =
304 cpoAdapter.retrieveBeans(
305 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
306 long count =
307 beans
308 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
309 .count();
310 assertEquals(count, 3, "Number of beans is " + count);
311 }
312 } catch (Exception e) {
313 fail(method + e.getMessage());
314 }
315 }
316
317 @Test
318 public void testInWhereCollection() {
319 String method = "testNonAttributeWhere:";
320
321 try {
322 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 1);
323 Collection<Integer> inColl = new ArrayList<>();
324 inColl.add(IDB + 1);
325 inColl.add(IDB + 3);
326 inColl.add(IDB + 5);
327
328 CpoWhere cw = cpoAdapter.newWhere();
329 CpoWhere cw1 = cpoAdapter.newWhere(Logical.NONE, "id", Comparison.IN, inColl);
330
331 cw.addWhere(cw1);
332
333 ArrayList<CpoWhere> wheres = new ArrayList<>();
334 wheres.add(cw);
335 try (Stream<ValueObject> beans =
336 cpoAdapter.retrieveBeans(
337 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
338 long count =
339 beans
340 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
341 .count();
342 assertEquals(count, 3, "Number of beans is " + count);
343 }
344 } catch (Exception e) {
345 fail(method + e.getMessage());
346 }
347 }
348
349 @Test
350 public void testNonAttributeInWhereCollection() {
351 String method = "testNonAttributeWhere:";
352
353 try {
354 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 1);
355 Collection<Integer> inColl = new ArrayList<>();
356 inColl.add(IDB + 1);
357 inColl.add(IDB + 3);
358 inColl.add(IDB + 5);
359
360 CpoWhere cw = cpoAdapter.newWhere();
361 CpoWhere cw1 = cpoAdapter.newWhere(Logical.NONE, "id", Comparison.IN, inColl);
362
363 cw.addWhere(cw1);
364
365 ArrayList<CpoWhere> wheres = new ArrayList<>();
366 wheres.add(cw);
367 try (Stream<ValueObject> beans =
368 cpoAdapter.retrieveBeans(
369 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
370 long count =
371 beans
372 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
373 .count();
374 assertEquals(count, 3, "Number of beans is " + count);
375 }
376 } catch (Exception e) {
377 fail(method + e.getMessage());
378 }
379 }
380 }