-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathTestPOJOReadWrite1.java
More file actions
537 lines (471 loc) · 17.4 KB
/
TestPOJOReadWrite1.java
File metadata and controls
537 lines (471 loc) · 17.4 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
* Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
package com.marklogic.client.fastfunctest;
import com.marklogic.client.ResourceNotFoundException;
import com.marklogic.client.functionaltest.Artifact;
import com.marklogic.client.functionaltest.ArtifactIndexedOnCalendar;
import com.marklogic.client.functionaltest.Company;
import com.marklogic.client.io.JacksonHandle;
import com.marklogic.client.pojo.PojoPage;
import com.marklogic.client.pojo.PojoQueryBuilder;
import com.marklogic.client.pojo.PojoQueryBuilder.Operator;
import com.marklogic.client.pojo.PojoQueryDefinition;
import com.marklogic.client.pojo.PojoRepository;
import com.marklogic.client.pojo.annotation.Id;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Calendar;
import java.util.TimeZone;
public class TestPOJOReadWrite1 extends AbstractFunctionalTest {
Calendar calOne = null;
Calendar calTwo = null;
/*
* @Id annotation on String type
*/
public static class StringPOJO {
@Id
public String name;
public String getName() {
return name;
}
public StringPOJO setName(String name) {
this.name = name;
return this;
}
}
/*
* @Id annotation on Integer type.
*/
public static class IntegerPOJO {
@Id
public Integer id;
public Integer getId() {
return id;
}
public IntegerPOJO setId(Integer id) {
this.id = id;
return this;
}
}
/*
* @Id annotation on Calender type.
*/
public static class CalendarPOJO {
@Id
public Calendar dateId;
public Calendar getDateId() {
return dateId;
}
public CalendarPOJO setDateId(Calendar dateId) {
this.dateId = dateId;
return this;
}
}
/*
* @Id annotation on double type.
*/
public static class DoublePOJO {
@Id
public double doubleId;
public double getDoubleId() {
return doubleId;
}
public DoublePOJO setDoubleId(double doubleId) {
this.doubleId = doubleId;
return this;
}
}
public static class NumberPOJO {
@Id
public Number nId;
public Number getNumberId() {
return nId;
}
public NumberPOJO setNumberId(Number nId) {
this.nId = nId;
return this;
}
}
@BeforeEach
public void setUp() throws Exception {
client = getDatabaseClient("rest-admin", "x", getConnType());
}
public Artifact getArtifact(int counter) {
Company acme = new Company();
acme.setName("Acme " + counter + ", Inc.");
acme.setWebsite("http://www.acme" + counter + ".com");
acme.setLatitude(41.998 + counter);
acme.setLongitude(-87.966 + counter);
Artifact cogs = new Artifact();
cogs.setId(counter);
cogs.setName("Cogs " + counter);
cogs.setManufacturer(acme);
cogs.setInventory(1000 + counter);
return cogs;
}
public void validateArtifact(Artifact art) {
assertNotNull( art);
assertNotNull( art.id);
assertTrue( art.getInventory() > 1000);
}
// This test is to persist a simple design model objects in ML, read from ML,
// delete all
@Test
public void testPOJOWrite() {
PojoRepository<Artifact, Long> products = client.newPojoRepository(Artifact.class, Long.class);
products.deleteAll();
// Load more than 100 objects
for (int i = 1; i < 112; i++) {
products.write(this.getArtifact(i));
}
assertEquals( 111, products.count());
for (long i = 1; i < 112; i++) {
assertTrue(products.exists(i));
this.validateArtifact(products.read(i));
}
products.deleteAll();
for (long i = 1; i < 112; i++) {
assertFalse(products.exists(i));
}
}
// Issue 192 describes the use case
@Test
public void testPOJOReadInvalidId() {
PojoRepository<Artifact, Long> products = client.newPojoRepository(Artifact.class, Long.class);
products.deleteAll();
// Load more than 100 objects
for (int i = 1; i < 112; i++) {
products.write(this.getArtifact(i));
}
assertEquals( 111, products.count());
assertThrows(ResourceNotFoundException.class, () -> this.validateArtifact(products.read(1143l)));
products.deleteAll();
}
// This test is to persist objects into different collections, read documents
// based on Id and delete single object based on Id
@Test
public void testPOJOWriteWithCollection() {
PojoRepository<Artifact, Long> products = client.newPojoRepository(Artifact.class, Long.class);
// Load more than 110 objects into different collections
products.deleteAll();
for (int i = 112; i < 222; i++) {
if (i % 2 == 0) {
products.write(this.getArtifact(i), "even", "numbers");
}
else {
products.write(this.getArtifact(i), "odd", "numbers");
}
}
assertEquals( 110, products.count("numbers"));
assertEquals( 55, products.count("even"));
assertEquals( 55, products.count("odd"));
for (long i = 112; i < 222; i++) {
// validate all the records inserted are readable
assertTrue(products.exists(i));
this.validateArtifact(products.read(i));
}
products.delete((long) 112);
assertFalse( products.exists((long) 112));
products.deleteAll();
// see any document exists
for (long i = 112; i < 222; i++) {
assertFalse(products.exists(i));
}
// see if it complains when there are no records
products.delete((long) 112);
products.deleteAll();
}
// This test is to read objects into pojo page based on Ids ,it has a scenario
// for Issue 192
// until #103 is resolved
@Test
public void testPOJOWriteWithPojoPage() {
PojoRepository<Artifact, Long> products = client.newPojoRepository(Artifact.class, Long.class);
// Load more than 110 objects into different collections
products.deleteAll();
Long[] ids = new Long[112];
int j = 0;
for (int i = 222; i < 333; i++) {
ids[j] = (long) i;
j++;
if (i % 2 == 0) {
products.write(this.getArtifact(i), "even", "numbers");
}
else {
products.write(this.getArtifact(i), "odd", "numbers");
}
}
ids[j] = (long) 1234234;
j++;
assertEquals( 111, products.count("numbers"));
assertEquals( 56, products.count("even"));
assertEquals( 55, products.count("odd"));
System.out.println("Default Page length setting on docMgr :" + products.getPageLength());
assertEquals( 50, products.getPageLength());
PojoPage<Artifact> p = products.read(ids);
// test for page methods
System.out.println("Total number of estimated results:" + p.getTotalSize() + ids.length);
System.out.println("Total number of estimated pages :" + p.getTotalPages());
long count = 0;
while (p.hasNext()) {
this.validateArtifact(p.next());
count++;
}
assertEquals( 111, count);
products.deleteAll();
// see any document exists
for (long i = 112; i < 222; i++) {
assertFalse(products.exists(i));
}
// see if it complains when there are no records
}
@Test
public void testPOJOWriteWithPojoPageReadAll() {
PojoRepository<Artifact, Long> products = client.newPojoRepository(Artifact.class, Long.class);
// Load more than 110 objects into different collections
products.deleteAll();
for (int i = 222; i < 333; i++) {
if (i % 2 == 0) {
products.write(this.getArtifact(i), "even", "numbers");
}
else {
products.write(this.getArtifact(i), "odd", "numbers");
}
}
assertEquals( 111, products.count("numbers"));
assertEquals( 56, products.count("even"));
assertEquals( 55, products.count("odd"));
System.out.println("Default Page length setting on docMgr :" + products.getPageLength());
assertEquals( 50, products.getPageLength());
products.setPageLength(1);
assertEquals( 1, products.getPageLength());
PojoPage<Artifact> p = products.readAll(1);
// test for page methods
assertEquals( 1, p.size());
System.out.println("Page size" + p.size());
assertEquals( 1, p.getStart());
System.out.println("Starting record in first page " + p.getStart());
assertEquals(111, p.getTotalSize());
System.out.println("Total number of estimated results:" + p.getTotalSize());
assertEquals(111, p.getTotalPages());
System.out.println("Total number of estimated pages :" + p.getTotalPages());
assertTrue(p.isFirstPage());
assertFalse(p.isLastPage());
assertTrue(p.hasContent());
// Need the Issue #75 to be fixed
assertFalse( p.hasPreviousPage());
long pageNo = 1, count = 0;
do {
count = 0;
p = products.readAll(pageNo);
if (pageNo > 1) {
assertFalse( p.isFirstPage());
assertTrue( p.hasPreviousPage());
}
while (p.iterator().hasNext()) {
this.validateArtifact(p.iterator().next());
count++;
}
assertEquals( p.size(), count);
pageNo = pageNo + p.getPageSize();
} while (!(p.isLastPage()) && pageNo < p.getTotalSize());
assertTrue( pageNo == p.getTotalPages());
assertTrue( p.hasPreviousPage());
assertEquals( 1, p.getPageSize());
assertEquals( 111, p.getTotalSize());
products.deleteAll();
p = products.readAll(1);
assertFalse( p.hasContent());
}
@Test
public void testPOJOgetDocumentUri() {
PojoRepository<StringPOJO, String> strPojoRepo = client.newPojoRepository(StringPOJO.class, String.class);
StringPOJO sobj = new StringPOJO();
sobj.setName("StringUri");
strPojoRepo.write(sobj);
System.out.println(strPojoRepo.getDocumentUri(sobj));
assertEquals("com.marklogic.client.fastfunctest.TestPOJOReadWrite1$StringPOJO/StringUri.json", strPojoRepo.getDocumentUri(sobj));
PojoRepository<IntegerPOJO, Integer> intPojoRepo = client.newPojoRepository(IntegerPOJO.class, Integer.class);
IntegerPOJO iobj = new IntegerPOJO();
iobj.setId(-12);
intPojoRepo.write(iobj);
System.out.println(intPojoRepo.getDocumentUri(iobj));
assertEquals("com.marklogic.client.fastfunctest.TestPOJOReadWrite1$IntegerPOJO/-12.json", intPojoRepo.getDocumentUri(iobj));
PojoRepository<CalendarPOJO, Calendar> calPojoRepo = client.newPojoRepository(CalendarPOJO.class, Calendar.class);
CalendarPOJO cobj = new CalendarPOJO();
Calendar cal = Calendar.getInstance();
cobj.setDateId(cal);
calPojoRepo.write(cobj);
System.out.println(calPojoRepo.getDocumentUri(cobj));
assertTrue(calPojoRepo.getDocumentUri(cobj).contains("com.marklogic.client.fastfunctest.TestPOJOReadWrite1$CalendarPOJO"));
PojoRepository<DoublePOJO, Double> dPojoRepo = client.newPojoRepository(DoublePOJO.class, Double.class);
DoublePOJO dobj = new DoublePOJO();
double dvar = 2.015;
dobj.setDoubleId(dvar);
dPojoRepo.write(dobj);
System.out.println(dPojoRepo.getDocumentUri(dobj));
assertEquals("com.marklogic.client.fastfunctest.TestPOJOReadWrite1$DoublePOJO/2.015.json", dPojoRepo.getDocumentUri(dobj));
PojoRepository<NumberPOJO, Number> nPojoRepo = client.newPojoRepository(NumberPOJO.class, Number.class);
NumberPOJO nobj = new NumberPOJO();
Number nvar = 99;
nvar.intValue();
nobj.setNumberId(nvar);
nPojoRepo.write(nobj);
System.out.println(nPojoRepo.getDocumentUri(nobj));
assertEquals("com.marklogic.client.fastfunctest.TestPOJOReadWrite1$NumberPOJO/99.json", nPojoRepo.getDocumentUri(nobj));
}
// Below scenario is verifying range query from PojoQueryBuilder on Calendar
// type.
@Test
public void testPOJORangeQueryOnCalendar() throws Exception {
System.out.println("Running testPOJORangeQueryOnCalendar");
PojoRepository<ArtifactIndexedOnCalendar, String> rangeQryRepos = client.newPojoRepository(ArtifactIndexedOnCalendar.class, String.class);
PojoPage<ArtifactIndexedOnCalendar> p;
loadSimplePojos(rangeQryRepos);
PojoQueryBuilder qb = rangeQryRepos.getQueryBuilder();
// Range query on equality.
PojoQueryDefinition qdEQ = qb.range("expiryDate", Operator.EQ, calOne);
JacksonHandle jh = new JacksonHandle();
rangeQryRepos.setPageLength(56);
p = rangeQryRepos.search(qdEQ, 1, jh);
assertEquals( 1, p.getTotalPages());
long pageNo = 1, count = 0;
do {
count = 0;
p = rangeQryRepos.search(qdEQ, pageNo);
while (p.hasNext()) {
ArtifactIndexedOnCalendar a = p.next();
assertTrue( a.getExpiryDate().getTime().equals(calOne.getTime()));
assertTrue( a.getId() == 57);
assertTrue( a.getInventory() == 1057);
assertTrue( a.getName().equalsIgnoreCase("Cogs special 57"));
assertTrue( a.getManufacturer().getLatitude() == 98.998);
assertTrue( a.getManufacturer().getLongitude() == -30.966);
assertTrue( a.getManufacturer().getName().equalsIgnoreCase("Acme special, Inc."));
assertTrue( a.getManufacturer().getWebsite().equalsIgnoreCase("http://www.acme special.com"));
count++;
}
assertEquals( count, p.size());
pageNo = pageNo + p.getPageSize();
} while (!p.isLastPage() && pageNo <= p.getTotalSize());
assertEquals( 1, p.getPageNumber());
assertEquals( 1, p.getTotalPages());
// Range query on Greater than equal.
PojoQueryDefinition qdGE = qb.range("expiryDate", Operator.GE, calTwo);
JacksonHandle jhGE = new JacksonHandle();
p = rangeQryRepos.search(qdGE, 1, jhGE);
assertEquals( 1, p.getTotalPages());
pageNo = 1;
count = 0;
boolean bFound = false;
do {
count = 0;
p = rangeQryRepos.search(qdGE, pageNo);
while (p.hasNext()) {
ArtifactIndexedOnCalendar a = p.next();
if (a.getId() == 97) {
assertTrue( a.getExpiryDate().getTime().equals(calTwo.getTime()));
assertTrue( a.getInventory() == 1097);
assertTrue( a.getName().equalsIgnoreCase("Cogs special 97"));
assertTrue( a.getManufacturer().getLatitude() == 138.998);
assertTrue( a.getManufacturer().getLongitude() == 9.03400000000001);
assertTrue( a.getManufacturer().getName().equalsIgnoreCase("Acme special, Inc."));
assertTrue( a.getManufacturer().getWebsite().equalsIgnoreCase("http://www.acme special.com"));
bFound = true;
}
count++;
}
// Assert if id = 97 is not found.
if (!bFound) {
fail("Range Query on Calendar type failed when greater than or equal used.");
}
assertEquals( 14, count);
assertEquals( count, p.size());
pageNo = pageNo + p.getPageSize();
} while (!p.isLastPage() && pageNo <= p.getTotalSize());
assertEquals( 1, p.getPageNumber());
assertEquals( 1, p.getTotalPages());
// Range query on Greater than. count should be 1 less than previous count.
PojoQueryDefinition qdGT = qb.range("expiryDate", Operator.GT, calTwo);
JacksonHandle jhGT = new JacksonHandle();
p = rangeQryRepos.search(qdGT, 1, jhGT);
assertEquals( 1, p.getTotalPages());
pageNo = 1;
count = 0;
do {
count = 0;
p = rangeQryRepos.search(qdGT, pageNo);
while (p.hasNext()) {
ArtifactIndexedOnCalendar a = p.next();
// Assert if id = 97 is found.
if (a.getId() == 97) {
fail("Range Query on Calendar type failed when greater than 97 used.");
}
count++;
}
assertEquals( 13, count);
assertEquals( count, p.size());
pageNo = pageNo + p.getPageSize();
} while (!p.isLastPage() && pageNo <= p.getTotalSize());
assertEquals( 1, p.getPageNumber());
assertEquals( 1, p.getTotalPages());
}
@SuppressWarnings("unchecked")
public void loadSimplePojos(PojoRepository rangeQueryRepos) {
for (int i = 1; i < 111; i++) {
if (i % 2 == 0) {
rangeQueryRepos.write(this.getArtifactIndexedOnCalendar(i), "even", "numbers");
}
else {
rangeQueryRepos.write(this.getArtifactIndexedOnCalendar(i), "odd", "numbers");
}
}
}
public ArtifactIndexedOnCalendar getArtifactIndexedOnCalendar(int counter) {
StringBuilder str = new StringBuilder();
str.append("Cogs special");
str.append(counter);
ArtifactIndexedOnCalendar cogs = new ArtifactIndexedOnCalendar();
cogs.setId(counter);
cogs.setInventory(1000 + counter);
// Time zone is UTC. Server time when POJO written is UTC. Without timezone
// on client side
// there will be difference on times between client and ML server values.
Calendar expiryDate = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
if (counter % 57 == 0) {
calOne = expiryDate;
Company acme = new Company();
acme.setName("Acme special, Inc.");
acme.setWebsite("http://www.acme special.com");
acme.setLatitude(41.998 + counter);
acme.setLongitude(-87.966 + counter);
cogs.setManufacturer(acme);
cogs.setExpiryDate(expiryDate);
cogs.setName("Cogs special 57");
}
else if (counter % 97 == 0) {
calTwo = expiryDate;
Company acme = new Company();
acme.setName("Acme special, Inc.");
acme.setWebsite("http://www.acme special.com");
acme.setLatitude(41.998 + counter);
acme.setLongitude(-87.966 + counter);
cogs.setManufacturer(acme);
cogs.setExpiryDate(expiryDate);
cogs.setName("Cogs special 97");
}
else {
Company widgets = new Company();
widgets.setName(str.toString());
widgets.setWebsite("http://www.widgets counter.com");
widgets.setLatitude(41.998 + counter);
widgets.setLongitude(-87.966 + counter);
cogs.setManufacturer(widgets);
cogs.setExpiryDate(expiryDate);
cogs.setName(str.toString());
}
return cogs;
}
}