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