-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlicingDiceTester.java
621 lines (533 loc) · 21.6 KB
/
SlicingDiceTester.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
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
package com.slicingdice.jslicer;
import com.slicingdice.jslicer.core.Requester;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Objects;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.json.JSONArray;
import org.json.JSONObject;
public class SlicingDiceTester {
// The SlicingDice client
private final SlicingDice client;
private boolean verbose = false;
// Translation table for columns with timestamp
private JSONObject columnTranslation;
// Sleep time in seconds
private long sleepTime;
// Directory containing examples to test
private String path;
// Examples file format
private String fileExtension;
public int numberOfSuccesses;
public int numberOfFails;
public ArrayList<Object> failedTests;
private boolean perTestInsertion;
private boolean insertSqlData;
public SlicingDiceTester(final String apiKey) {
this.client = new SlicingDice(apiKey);
this.loadConfigTest();
}
public SlicingDiceTester(final String apiKey, final boolean verbose) {
this.client = new SlicingDice(apiKey);
this.verbose = verbose;
this.loadConfigTest();
}
private void loadConfigTest() {
this.sleepTime = 10;
this.path = "src/test/java/com/slicingdice/jslicer/examples/";
this.fileExtension = ".json";
this.numberOfSuccesses = 0;
this.numberOfFails = 0;
this.failedTests = new ArrayList<>();
}
/**
* Run tests
*
* @param queryType the query type
*/
public void runTests(final String queryType) throws ExecutionException, InterruptedException {
final JSONArray testData = this.loadTestData(queryType);
final int numberOfTests = testData.length();
this.perTestInsertion = testData.getJSONObject(0).has("insert");
if (!this.perTestInsertion && this.insertSqlData) {
final JSONArray insertionData = this.loadInsertionData(queryType);
for (final Object insertCommand : insertionData) {
this.client.insert((JSONObject) insertCommand).get();
}
Thread.sleep(this.sleepTime);
}
for (int i = 0; i < numberOfTests; i++) {
String queryType_ = queryType;
final JSONObject testObject = (JSONObject) testData.get(i);
this.emptyColumnTranslation();
System.out.println(String.format("(%1$d/%2$d) Executing test \"%3$s\"", i + 1,
numberOfTests, testObject.getString("name")));
if (testObject.has("description")) {
System.out.println(String.format("\tDescription: %s",
testObject.get("description")));
}
System.out.println(String.format("\tQuery type: %s", queryType));
JSONObject result = null;
try {
if (this.perTestInsertion) {
this.createColumns(testObject);
this.insertData(testObject);
}
if (queryType.equals("delete") || queryType.equals("update")) {
final boolean additionalResult = this.runAdditionalOperations(queryType, testObject);
if (!additionalResult) {
continue;
}
queryType_ = "count_entity";
}
result = this.executeQuery(queryType_, testObject);
} catch (final Exception e) {
result = new JSONObject()
.put("result", new JSONObject()
.put("error", e.toString()));
if (queryType.equals("delete") || queryType.equals("update")) {
this.numberOfFails += 1;
this.failedTests.add(testObject.getString("name"));
System.out.println(String.format("\tResult: %1$s", result.toString()));
System.out.println("\tStatus: Failed\n");
continue;
}
}
try {
this.compareResult(testObject, queryType_, result);
} catch (final IOException e) {
e.printStackTrace();
}
}
}
/**
* Method used to run delete and update operations, these operations are executed before the
* query and the result comparison.
*/
private boolean runAdditionalOperations(final String queryType, final JSONObject testObject)
throws ExecutionException, InterruptedException {
final JSONObject queryData = this.translateColumnNames(testObject.getJSONObject(
"additional_operation"));
if (queryType.equals("update")) {
System.out.println("\tUpdating");
} else {
System.out.println("\tDeleting");
}
if (this.verbose) {
System.out.println(String.format("\t\t- %s", queryData));
}
JSONObject response = null;
if (queryType.equals("update")) {
response = Requester.responseToJson(this.client.update(queryData).get());
} else if (queryType.equals("delete")) {
response = Requester.responseToJson(this.client.delete(queryData).get());
}
final JSONObject expected = this.translateColumnNames(testObject.getJSONObject(
"result_additional"));
for (final Object key : expected.keySet()) {
final String keyStr = (String) key;
final Object value = expected.get(keyStr);
if (value.toString().equals("ignore")) {
continue;
}
boolean testFailed = false;
if (!response.has(keyStr)) {
testFailed = true;
} else {
if (!this.compareJsonValue(expected.get(keyStr), response.get(keyStr))) {
testFailed = true;
}
}
if (testFailed) {
this.numberOfFails += 1;
this.failedTests.add(testObject.getString("name"));
try {
System.out.println(String.format("\tExpected: \"%1$s\": %2$s", keyStr,
expected.getJSONObject(keyStr).toString()));
} catch (final Exception e) {
System.out.println(String.format("\tExpected: \"%1$s\": %2$s", keyStr,
e.getMessage()));
}
try {
System.out.println(String.format("\tResult: \"%1$s\": %2$s", keyStr,
response.getJSONObject(keyStr).toString()));
} catch (final Exception e) {
System.out.println(String.format("\tResult: \"%1$s\": %2$s", keyStr,
e.getMessage()));
}
System.out.println("\tStatus: Failed\n");
return false;
} else {
this.numberOfSuccesses += 1;
System.out.println("\tStatus: Passed\n");
return true;
}
}
return true;
}
private void emptyColumnTranslation() {
this.columnTranslation = new JSONObject();
}
/**
* Load test data from examples files
*
* @param queryType the query type
* @return JSONArray with test data
*/
private JSONArray loadTestData(final String queryType) {
return loadData(queryType, "");
}
/**
* Load insertion data from examples files
*
* @param queryType the query type
* @return JSONArray with insertion data
*/
private JSONArray loadInsertionData(final String queryType) {
return loadData(queryType, "_insert");
}
private JSONArray loadData(final String queryType, final String file_suffix) {
final String file = new File(this.path + queryType + file_suffix
+ this.fileExtension).getAbsolutePath();
String content = null;
try {
content = new Scanner(new File(file)).useDelimiter("\\Z").next();
return new JSONArray(content);
} catch (final FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* Create columns on SlicingDice API
*
* @param columnObject the column object to create
*/
private void createColumns(final JSONObject columnObject) {
final JSONArray columns = columnObject.getJSONArray("columns");
final boolean isSingular = columns.length() == 1;
String columnOrColumns = null;
if (isSingular) {
columnOrColumns = "column";
} else {
columnOrColumns = "columns";
}
System.out.println(String.format("\tCreating %1$d %2$s", columns.length(), columnOrColumns));
for (final Object column : columns) {
final JSONObject columnDict = (JSONObject) column;
this.addTimestampToColumnName(columnDict);
// call client command to create columns
try {
this.client.createColumn(columnDict).get();
} catch (final InterruptedException | ExecutionException e) {
e.printStackTrace();
}
if (this.verbose) {
System.out.println(String.format("\t\t- %s", columnDict.getString("api-name")));
}
}
}
/**
* Add timestamp to column name
*
* @param column the column to put timestamp
*/
private void addTimestampToColumnName(final JSONObject column) {
final String oldName = "\"" + column.getString("api-name") + "\"";
final String timestamp = this.getTimestamp();
column.put("name", column.get("name") + timestamp);
column.put("api-name", column.get("api-name") + timestamp);
final String newName = "\"" + column.getString("api-name") + "\"";
this.columnTranslation.put(oldName, newName);
}
/**
* Get actual timestamp
*
* @return timestamp converted to string
*/
private String getTimestamp() {
final Long currentTime = System.currentTimeMillis() * 10;
return currentTime.toString();
}
/**
* Insert data to SlicingDice API
*
* @param insertionObject the data to insert on SlicingDice
*/
private void insertData(final JSONObject insertionObject) {
final JSONObject insert = insertionObject.getJSONObject("insert");
final boolean isSingular = insert.length() == 1;
final String entityOrEntities;
if (isSingular) {
entityOrEntities = "entity";
} else {
entityOrEntities = "entities";
}
System.out.println(String.format("\tInserting %1$d %2$s", insert.length(), entityOrEntities));
final JSONObject insertData = this.translateColumnNames(insert);
// call client command to insert data
try {
this.client.insert(insertData).get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
try {
// Wait a few seconds so the data can be inserted by SlicingDice
TimeUnit.SECONDS.sleep(this.sleepTime);
} catch (final InterruptedException e) {
e.printStackTrace();
System.out.println("An error occurred while processing your query on SlicingDice");
}
}
/**
* Translate column name to use timestamp
*
* @param column the column to translate
* @return the new column translated
*/
private JSONObject translateColumnNames(final JSONObject column) {
String dataString = column.toString();
for (final Object oldName : this.columnTranslation.keySet()) {
final String oldNameStr = (String) oldName;
final String newName = this.columnTranslation.getString(oldNameStr);
dataString = dataString.replaceAll(oldNameStr, newName);
}
return new JSONObject(dataString);
}
/**
* Execute query
*
* @param queryType the type of the query
* @param query the query to send to SlicingDice API
* @return the result of the query
*/
private JSONObject executeQuery(final String queryType, final JSONObject query)
throws ExecutionException, InterruptedException {
final JSONObject queryData;
if (this.perTestInsertion) {
queryData = this.translateColumnNames(query.getJSONObject("query"));
} else {
queryData = query;
}
System.out.println("\tQuerying");
if (this.verbose) {
System.out.println(String.format("\t\t- %s", queryData));
}
JSONObject result = null;
// call client command to make a query
switch (queryType) {
case "count_entity":
result = Requester.responseToJson(this.client.countEntity(queryData).get());
break;
case "count_event":
result = Requester.responseToJson(this.client.countEvent(queryData).get());
break;
case "top_values":
result = Requester.responseToJson(this.client.topValues(queryData).get());
break;
case "aggregation":
result = Requester.responseToJson(this.client.aggregation(queryData).get());
break;
case "result":
result = Requester.responseToJson(this.client.result(queryData).get());
break;
case "score":
result = Requester.responseToJson(this.client.score(queryData).get());
break;
case "sql":
result = Requester.responseToJson(this.client.sql(queryData.getString("query")).get());
break;
}
return result;
}
/**
* Compare result received from SlicingDice API
*
* @param expectedObject the object with expected result
* @param queryType - the type of the query
* @param result the result received from SlicingDice API
*/
private void compareResult(final JSONObject expectedObject, final String queryType,
final JSONObject result) throws IOException {
final JSONObject testExpected = expectedObject.getJSONObject("expected");
final JSONObject expected;
if (this.perTestInsertion) {
expected = this.translateColumnNames(expectedObject.getJSONObject("expected"));
} else {
expected = expectedObject.getJSONObject("expected");
}
for (final Object key : testExpected.keySet()) {
final String keyStr = (String) key;
final Object value = testExpected.get(keyStr);
if (value.toString().equals("ignore")) {
continue;
}
boolean testFailed = false;
if (!result.has(keyStr)) {
// try second time
if (testSecondTime(expectedObject, queryType, expected, keyStr)) {
continue;
}
testFailed = true;
} else {
if (!this.compareJsonValue(expected.get(keyStr),
result.get(keyStr))) {
// try second time
if (testSecondTime(expectedObject, queryType, expected, keyStr)) {
continue;
}
testFailed = true;
}
}
if (testFailed) {
this.numberOfFails += 1;
this.failedTests.add(expectedObject.getString("name"));
try {
System.out.println(String.format("\tExpected: \"%1$s\": %2$s", keyStr,
expected.getJSONObject(keyStr).toString()));
} catch (final Exception e) {
System.out.println(String.format("\tExpected: \"%1$s\": %2$s", keyStr,
e.getMessage()));
}
try {
System.out.println(String.format("\tResult: \"%1$s\": %2$s", keyStr,
result.getJSONObject(keyStr).toString()));
} catch (final Exception e) {
System.out.println(String.format("\tResult: \"%1$s\": %2$s", keyStr,
e.getMessage()));
}
System.out.println("\tStatus: Failed\n");
return;
} else {
this.numberOfSuccesses += 1;
System.out.println("\tStatus: Passed\n");
}
}
}
/**
* If first query doesn't return as expected we will try another time
*
* @param expectedObject -
* @param queryType - type of the query to send to SlicingDice
* @param expected = the expected json
* @param key - the json key to test
* @return true if second test succeed and false otherwise
* @throws IOException
*/
private boolean testSecondTime(final JSONObject expectedObject, final String queryType,
final JSONObject expected, final String key) {
try {
TimeUnit.SECONDS.sleep(this.sleepTime * 3);
} catch (final InterruptedException e) {
e.printStackTrace();
}
final JSONObject secondResult;
try {
secondResult = this.executeQuery(queryType, expectedObject);
if (this.compareJsonValue(expected.get(key), secondResult.get(key))) {
System.out.println("\tPassed at second try!");
this.numberOfSuccesses += 1;
System.out.println("\tStatus: Passed\n");
return true;
}
} catch (final Exception e) {
e.printStackTrace();
}
return false;
}
/**
* Compare two JSONObjects
*
* @param expected - The json with the expected result
* @param got = The json returned by the SlicingDice API
* @return - true if the two json's are equal and false otherwise
*/
private boolean compareJson(final JSONObject expected, final JSONObject got) {
if (expected.length() != got.length()) {
return false;
}
final Set keySet = expected.keySet();
final Iterator iterator = keySet.iterator();
while (iterator.hasNext()) {
final String name = (String) iterator.next();
final Object valueExpected = expected.get(name);
final Object valueGot = got.get(name);
if (!this.compareJsonValue(valueExpected, valueGot)) {
return false;
}
}
return true;
}
/**
* Compare two JSONArrays
*
* @param expected - The json with the expected result
* @param got = The json returned by the SlicingDice API
* @return - true if the two json's are equal and false otherwise
*/
private boolean compareJsonArray(final JSONArray expected, final JSONArray got) {
if (expected.length() != got.length()) {
return false;
}
for (int i = 0; i < expected.length(); ++i) {
final Object valueExpected = expected.get(i);
boolean hasTrue = false;
for (int j = 0; j < got.length(); j++) {
final Object valueGot = got.get(j);
if (this.compareJsonValue(valueExpected, valueGot)) {
hasTrue = true;
}
}
if (!hasTrue) {
return false;
}
}
return true;
}
/**
* Compare two json values
*
* @param valueExpected - the expected value
* @param valueGot - the received value
* @return true if the two values are equal and false otherwise
*/
private boolean compareJsonValue(final Object valueExpected, final Object valueGot) {
try {
if (valueExpected instanceof JSONObject) {
if (!this.compareJson((JSONObject) valueExpected, (JSONObject) valueGot)) {
return false;
}
} else if (valueExpected instanceof JSONArray) {
if (!this.compareJsonArray((JSONArray) valueExpected, (JSONArray) valueGot)) {
return false;
}
} else if (!valueExpected.equals(valueGot)) {
if (valueExpected instanceof Integer && valueGot instanceof Double ||
valueExpected instanceof Double && valueGot instanceof Integer) {
final Number expectedInteger = (Number) valueExpected;
final Number gotInteger = (Number) valueGot;
return expectedInteger.intValue() == gotInteger.intValue();
} else if (valueExpected instanceof Double || valueExpected instanceof Float) {
return this.compareNumbersClose((Number) valueExpected, (Number) valueGot);
} else {
return Objects.equals(valueExpected, valueGot);
}
}
} catch (final Exception e) {
return false;
}
return true;
}
private boolean compareNumbersClose(final Number expected, final Number result) {
final double expectedDouble = expected.doubleValue();
final double resultDouble = result.doubleValue();
return Math.abs(expectedDouble - resultDouble) <= Math.max(
(1e-09) * Math.max(Math.abs(expectedDouble), Math.abs(resultDouble)), 0.0);
}
}