-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathDistributedStorageIntegrationTestBase.java
More file actions
1914 lines (1667 loc) · 70.1 KB
/
DistributedStorageIntegrationTestBase.java
File metadata and controls
1914 lines (1667 loc) · 70.1 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
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.scalar.db.api;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import com.google.common.collect.ImmutableList;
import com.scalar.db.api.Scan.Ordering;
import com.scalar.db.api.Scan.Ordering.Order;
import com.scalar.db.config.DatabaseConfig;
import com.scalar.db.exception.storage.ExecutionException;
import com.scalar.db.exception.storage.NoMutationException;
import com.scalar.db.io.BlobColumn;
import com.scalar.db.io.BooleanColumn;
import com.scalar.db.io.BooleanValue;
import com.scalar.db.io.DataType;
import com.scalar.db.io.IntColumn;
import com.scalar.db.io.IntValue;
import com.scalar.db.io.Key;
import com.scalar.db.io.TextColumn;
import com.scalar.db.io.TextValue;
import com.scalar.db.service.StorageFactory;
import com.scalar.db.util.TestUtils;
import com.scalar.db.util.TestUtils.ExpectedResult;
import com.scalar.db.util.TestUtils.ExpectedResult.ExpectedResultBuilder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.stream.IntStream;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public abstract class DistributedStorageIntegrationTestBase {
private static final String TEST_NAME = "storage";
private static final String NAMESPACE = "int_test_" + TEST_NAME;
private static final String TABLE = "test_table";
private static final String COL_NAME1 = "c1";
private static final String COL_NAME2 = "c2";
private static final String COL_NAME3 = "c3";
private static final String COL_NAME4 = "c4";
private static final String COL_NAME5 = "c5";
private static final String COL_NAME6 = "c6";
private DistributedStorage storage;
private DistributedStorageAdmin admin;
private String namespace;
@BeforeAll
public void beforeAll() throws Exception {
initialize(TEST_NAME);
StorageFactory factory = StorageFactory.create(getProperties(TEST_NAME));
admin = factory.getAdmin();
namespace = getNamespace();
createTable();
storage = factory.getStorage();
}
protected void initialize(String testName) throws Exception {}
protected abstract Properties getProperties(String testName);
protected String getNamespace() {
return NAMESPACE;
}
private void createTable() throws ExecutionException {
Map<String, String> options = getCreationOptions();
admin.createNamespace(namespace, true, options);
admin.createTable(
namespace,
TABLE,
TableMetadata.newBuilder()
.addColumn(COL_NAME1, DataType.INT)
.addColumn(COL_NAME2, DataType.TEXT)
.addColumn(COL_NAME3, DataType.INT)
.addColumn(COL_NAME4, DataType.INT)
.addColumn(COL_NAME5, DataType.BOOLEAN)
.addColumn(COL_NAME6, DataType.BLOB)
.addPartitionKey(COL_NAME1)
.addClusteringKey(COL_NAME4)
.addSecondaryIndex(COL_NAME3)
.build(),
true,
options);
}
protected int getLargeDataSizeInBytes() {
return 5000;
}
protected Map<String, String> getCreationOptions() {
return Collections.emptyMap();
}
@BeforeEach
public void setUp() throws Exception {
truncateTable();
storage.with(namespace, TABLE);
}
private void truncateTable() throws ExecutionException {
admin.truncateTable(namespace, TABLE);
}
@AfterAll
public void afterAll() throws Exception {
dropTable();
admin.close();
storage.close();
}
private void dropTable() throws ExecutionException {
admin.dropTable(namespace, TABLE);
admin.dropNamespace(namespace);
}
@Test
public void operation_NoTargetGiven_ShouldThrowIllegalArgumentException() {
// Arrange
storage.with(null, TABLE);
Key partitionKey = new Key(COL_NAME1, 0);
Key clusteringKey = new Key(COL_NAME4, 0);
Get get = new Get(partitionKey, clusteringKey);
// Act Assert
assertThatThrownBy(() -> storage.get(get)).isInstanceOf(IllegalArgumentException.class);
}
@Test
public void operation_WrongNamespaceGiven_ShouldThrowIllegalArgumentException() {
// Arrange
storage.with("wrong_" + namespace, TABLE); // a wrong namespace
Key partitionKey = new Key(COL_NAME1, 0);
Key clusteringKey = new Key(COL_NAME4, 0);
Get get = new Get(partitionKey, clusteringKey);
// Act Assert
assertThatThrownBy(() -> storage.get(get)).isInstanceOf(IllegalArgumentException.class);
}
@Test
public void operation_WrongTableGiven_ShouldThrowIllegalArgumentException() {
// Arrange
storage.with(namespace, "wrong_" + TABLE); // a wrong table
Key partitionKey = new Key(COL_NAME1, 0);
Key clusteringKey = new Key(COL_NAME4, 0);
Get get = new Get(partitionKey, clusteringKey);
// Act Assert
assertThatThrownBy(() -> storage.get(get)).isInstanceOf(IllegalArgumentException.class);
}
@Test
public void operation_DefaultNamespaceGiven_ShouldWorkProperly() {
Properties properties = getProperties(TEST_NAME);
properties.put(DatabaseConfig.DEFAULT_NAMESPACE_NAME, getNamespace());
final DistributedStorage storageWithDefaultNamespace =
StorageFactory.create(properties).getStorage();
try {
// Arrange
populateRecords();
Get get =
Get.newBuilder()
.table(TABLE)
.partitionKey(Key.ofInt(COL_NAME1, 0))
.clusteringKey(Key.ofInt(COL_NAME4, 0))
.build();
Scan scan = Scan.newBuilder().table(TABLE).partitionKey(Key.ofInt(COL_NAME1, 0)).build();
Put put =
Put.newBuilder()
.table(TABLE)
.partitionKey(Key.ofInt(COL_NAME1, 1))
.clusteringKey(Key.ofInt(COL_NAME4, 0))
.textValue(COL_NAME2, "foo")
.build();
Delete delete =
Delete.newBuilder()
.table(TABLE)
.partitionKey(Key.ofInt(COL_NAME1, 2))
.clusteringKey(Key.ofInt(COL_NAME4, 0))
.build();
Mutation putAsMutation1 =
Put.newBuilder()
.table(TABLE)
.partitionKey(Key.ofInt(COL_NAME1, 3))
.clusteringKey(Key.ofInt(COL_NAME4, 0))
.textValue(COL_NAME2, "foo")
.build();
Mutation deleteAsMutation2 =
Delete.newBuilder()
.table(TABLE)
.partitionKey(Key.ofInt(COL_NAME1, 3))
.clusteringKey(Key.ofInt(COL_NAME4, 1))
.build();
// Act Assert
Assertions.assertThatCode(
() -> {
storageWithDefaultNamespace.get(get);
storageWithDefaultNamespace.scan(scan).close();
storageWithDefaultNamespace.put(put);
storageWithDefaultNamespace.delete(delete);
storageWithDefaultNamespace.mutate(
ImmutableList.of(putAsMutation1, deleteAsMutation2));
})
.doesNotThrowAnyException();
} finally {
if (storageWithDefaultNamespace != null) {
storageWithDefaultNamespace.close();
}
}
}
@Test
public void get_GetWithPartitionKeyAndClusteringKeyGiven_ShouldRetrieveSingleResult()
throws ExecutionException {
// Arrange
populateRecords();
int pKey = 0;
// Act
Get get = prepareGet(pKey, 0);
Optional<Result> actual = storage.get(get);
// Assert
assertThat(actual.isPresent()).isTrue();
assertThat(actual.get().getValue(COL_NAME1))
.isEqualTo(Optional.of(new IntValue(COL_NAME1, pKey)));
assertThat(actual.get().getValue(COL_NAME4)).isEqualTo(Optional.of(new IntValue(COL_NAME4, 0)));
}
@Test
public void get_GetWithoutPartitionKeyGiven_ShouldThrowInvalidUsageException() {
// Arrange
populateRecords();
int pKey = 0;
// Act Assert
Key partitionKey = new Key(COL_NAME1, pKey);
Get get = new Get(partitionKey);
assertThatThrownBy(() -> storage.get(get)).isInstanceOf(IllegalArgumentException.class);
}
@Test
public void get_GetWithProjectionsGiven_ShouldRetrieveSpecifiedValues()
throws ExecutionException {
// Arrange
populateRecords();
int pKey = 0;
int cKey = 0;
// Act
Get get = prepareGet(pKey, cKey);
get.withProjections(Arrays.asList(COL_NAME1, COL_NAME2, COL_NAME3, COL_NAME6));
Optional<Result> actual = storage.get(get);
// Assert
assertThat(actual).isNotEmpty();
assertThat(actual.get().getContainedColumnNames())
.containsOnly(COL_NAME1, COL_NAME2, COL_NAME3, COL_NAME6);
assertThat(actual.get().getInt(COL_NAME1)).isEqualTo(0);
assertThat(actual.get().getText(COL_NAME2)).isEqualTo("0");
assertThat(actual.get().getInt(COL_NAME3)).isEqualTo(0);
assertThat(actual.get().isNull(COL_NAME6)).isTrue();
}
@Test
public void scan_ScanWithProjectionsGiven_ShouldRetrieveSpecifiedValues()
throws IOException, ExecutionException {
// Arrange
populateRecords();
int pKey = 0;
// Act
Scan scan =
new Scan(Key.ofInt(COL_NAME1, pKey))
.withProjection(COL_NAME1)
.withProjection(COL_NAME2)
.withProjection(COL_NAME3)
.withProjection(COL_NAME6);
List<Result> actual = scanAll(scan);
// Assert
actual.forEach(
a -> {
assertThat(a.getContainedColumnNames())
.containsOnly(COL_NAME1, COL_NAME2, COL_NAME3, COL_NAME6);
assertThat(a.getInt(COL_NAME1)).isEqualTo(0);
assertThat(a.isNull(COL_NAME6)).isTrue();
});
assertThat(actual.size()).isEqualTo(3);
assertThat(actual.get(0).getText(COL_NAME2)).isEqualTo("0");
assertThat(actual.get(1).getText(COL_NAME2)).isEqualTo("1");
assertThat(actual.get(2).getText(COL_NAME2)).isEqualTo("2");
assertThat(actual.get(0).getInt(COL_NAME3)).isEqualTo(0);
assertThat(actual.get(1).getInt(COL_NAME3)).isEqualTo(1);
assertThat(actual.get(2).getInt(COL_NAME3)).isEqualTo(2);
}
@Test
public void scan_ScanWithPartitionKeyGivenAndResultsIteratedWithOne_ShouldReturnWhatsPut()
throws ExecutionException, IOException {
// Arrange
populateRecords();
int pKey = 0;
// Act
Scan scan = new Scan(new Key(COL_NAME1, pKey));
Scanner scanner = storage.scan(scan);
// Assert
List<Result> results = new ArrayList<>();
Optional<Result> result = scanner.one();
assertThat(result.isPresent()).isTrue();
results.add(result.get());
result = scanner.one();
assertThat(result.isPresent()).isTrue();
results.add(result.get());
result = scanner.one();
assertThat(result.isPresent()).isTrue();
results.add(result.get());
result = scanner.one();
assertThat(result.isPresent()).isFalse();
assertThat(results.size()).isEqualTo(3);
assertThat(results.get(0).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(0).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(0);
assertThat(results.get(1).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(1).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(1).getValue(COL_NAME4).get().getAsInt()).isEqualTo(1);
assertThat(results.get(2).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(2).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(2).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(2).getValue(COL_NAME4).get().getAsInt()).isEqualTo(2);
scanner.close();
}
@Test
public void scan_ScanWithPartitionGivenThreeTimes_ShouldRetrieveResultsProperlyEveryTime()
throws IOException, ExecutionException {
// Arrange
populateRecords();
int pKey = 0;
// Act
Scan scan = new Scan(new Key(COL_NAME1, pKey));
double t1 = System.currentTimeMillis();
List<Result> actual = scanAll(scan);
double t2 = System.currentTimeMillis();
Scanner scanner = storage.scan(scan);
scanner.close();
double t3 = System.currentTimeMillis();
scanner = storage.scan(scan);
scanner.close();
double t4 = System.currentTimeMillis();
// Assert
assertThat(actual.get(0).getValue(COL_NAME1))
.isEqualTo(Optional.of(new IntValue(COL_NAME1, pKey)));
assertThat(actual.get(0).getValue(COL_NAME4))
.isEqualTo(Optional.of(new IntValue(COL_NAME4, 0)));
System.err.println("first: " + (t2 - t1) + " (ms)");
System.err.println("second: " + (t3 - t2) + " (ms)");
System.err.println("third: " + (t4 - t3) + " (ms)");
}
@Test
public void scan_ScanWithStartInclusiveRangeGiven_ShouldRetrieveResultsOfGivenRange()
throws IOException, ExecutionException {
// Arrange
populateRecords();
int pKey = 0;
// Act
Scan scan =
new Scan(new Key(COL_NAME1, pKey))
.withStart(new Key(COL_NAME4, 0), true)
.withEnd(new Key(COL_NAME4, 2), false);
List<Result> actual = scanAll(scan);
// verify
assertThat(actual.size()).isEqualTo(2);
assertThat(actual.get(0).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(actual.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(0).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(actual.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(1).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(actual.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(1).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(actual.get(1).getValue(COL_NAME4).get().getAsInt()).isEqualTo(1);
}
@Test
public void scan_ScanWithEndInclusiveRangeGiven_ShouldRetrieveResultsOfGivenRange()
throws IOException, ExecutionException {
// Arrange
populateRecords();
int pKey = 0;
// Act
Scan scan =
new Scan(new Key(COL_NAME1, pKey))
.withStart(new Key(COL_NAME4, 0), false)
.withEnd(new Key(COL_NAME4, 2), true);
List<Result> actual = scanAll(scan);
// verify
assertThat(actual.size()).isEqualTo(2);
assertThat(actual.get(0).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(actual.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(0).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(actual.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(1);
assertThat(actual.get(1).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(actual.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(1).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(actual.get(1).getValue(COL_NAME4).get().getAsInt()).isEqualTo(2);
}
@Test
public void scan_ScanWithOrderAscGiven_ShouldReturnAscendingOrderedResults()
throws IOException, ExecutionException {
// Arrange
List<Put> puts = preparePuts();
storage.mutate(Arrays.asList(puts.get(0), puts.get(1), puts.get(2)));
Scan scan =
new Scan(new Key(COL_NAME1, 0))
.withOrdering(new Scan.Ordering(COL_NAME4, Scan.Ordering.Order.ASC));
// Act
List<Result> actual = scanAll(scan);
// Assert
assertThat(actual.size()).isEqualTo(3);
assertThat(actual.get(0).getValue(COL_NAME4))
.isEqualTo(Optional.of(new IntValue(COL_NAME4, 0)));
assertThat(actual.get(1).getValue(COL_NAME4))
.isEqualTo(Optional.of(new IntValue(COL_NAME4, 1)));
assertThat(actual.get(2).getValue(COL_NAME4))
.isEqualTo(Optional.of(new IntValue(COL_NAME4, 2)));
}
@Test
public void scan_ScanWithOrderDescGiven_ShouldReturnDescendingOrderedResults()
throws IOException, ExecutionException {
// Arrange
List<Put> puts = preparePuts();
storage.mutate(Arrays.asList(puts.get(0), puts.get(1), puts.get(2)));
Scan scan =
new Scan(new Key(COL_NAME1, 0))
.withOrdering(new Scan.Ordering(COL_NAME4, Scan.Ordering.Order.DESC));
// Act
List<Result> actual = scanAll(scan);
// Assert
assertThat(actual.size()).isEqualTo(3);
assertThat(actual.get(0).getValue(COL_NAME4))
.isEqualTo(Optional.of(new IntValue(COL_NAME4, 2)));
assertThat(actual.get(1).getValue(COL_NAME4))
.isEqualTo(Optional.of(new IntValue(COL_NAME4, 1)));
assertThat(actual.get(2).getValue(COL_NAME4))
.isEqualTo(Optional.of(new IntValue(COL_NAME4, 0)));
}
@Test
public void scan_ScanWithLimitGiven_ShouldReturnGivenNumberOfResults()
throws IOException, ExecutionException {
// setup
List<Put> puts = preparePuts();
storage.mutate(Arrays.asList(puts.get(0), puts.get(1), puts.get(2)));
Scan scan =
new Scan(new Key(COL_NAME1, 0))
.withOrdering(new Scan.Ordering(COL_NAME4, Scan.Ordering.Order.DESC))
.withLimit(1);
// exercise
List<Result> actual = scanAll(scan);
// verify
assertThat(actual.size()).isEqualTo(1);
assertThat(actual.get(0).getValue(COL_NAME4))
.isEqualTo(Optional.of(new IntValue(COL_NAME4, 2)));
}
@Test
public void scannerIterator_ScanWithPartitionKeyGiven_ShouldRetrieveCorrectResults()
throws ExecutionException, IOException {
// Arrange
populateRecords();
int pKey = 0;
// Act
Scan scan = new Scan(new Key(COL_NAME1, pKey));
List<Result> actual = new ArrayList<>();
Scanner scanner = storage.scan(scan);
scanner.forEach(actual::add);
scanner.close();
// Assert
assertThat(actual.size()).isEqualTo(3);
assertThat(actual.get(0).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(actual.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(0).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(actual.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(1).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(actual.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(1).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(actual.get(1).getValue(COL_NAME4).get().getAsInt()).isEqualTo(1);
assertThat(actual.get(2).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(actual.get(2).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(2).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(actual.get(2).getValue(COL_NAME4).get().getAsInt()).isEqualTo(2);
}
@Test
public void scannerIterator_OneAndIteratorCalled_ShouldRetrieveCorrectResults()
throws ExecutionException, IOException {
// Arrange
populateRecords();
int pKey = 0;
// Act
Scan scan = new Scan(new Key(COL_NAME1, pKey));
List<Result> actual = new ArrayList<>();
Scanner scanner = storage.scan(scan);
Optional<Result> result = scanner.one();
scanner.forEach(actual::add);
scanner.close();
// Assert
assertThat(result.isPresent()).isTrue();
assertThat(result.get().getValue(COL_NAME1).isPresent()).isTrue();
assertThat(result.get().getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(result.get().getValue(COL_NAME4).isPresent()).isTrue();
assertThat(result.get().getValue(COL_NAME4).get().getAsInt()).isEqualTo(0);
assertThat(actual.size()).isEqualTo(2);
assertThat(actual.get(0).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(actual.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(0).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(actual.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(1);
assertThat(actual.get(1).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(actual.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(1).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(actual.get(1).getValue(COL_NAME4).get().getAsInt()).isEqualTo(2);
}
@Test
public void scannerIterator_AllAndIteratorCalled_ShouldRetrieveCorrectResults()
throws ExecutionException, IOException {
// Arrange
populateRecords();
int pKey = 0;
// Act
Scan scan = new Scan(new Key(COL_NAME1, pKey));
List<Result> actual = new ArrayList<>();
Scanner scanner = storage.scan(scan);
List<Result> all = scanner.all();
scanner.forEach(actual::add);
scanner.close();
// Assert
assertThat(all.size()).isEqualTo(3);
assertThat(all.get(0).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(all.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(all.get(0).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(all.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(0);
assertThat(all.get(1).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(all.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(all.get(1).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(all.get(1).getValue(COL_NAME4).get().getAsInt()).isEqualTo(1);
assertThat(all.get(2).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(all.get(2).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(all.get(2).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(all.get(2).getValue(COL_NAME4).get().getAsInt()).isEqualTo(2);
assertThat(actual).isEmpty();
}
@Test
public void scannerIterator_IteratorCalledMultipleTimes_ShouldRetrieveCorrectResults()
throws ExecutionException, IOException {
// Arrange
populateRecords();
int pKey = 0;
// Act
Scan scan = new Scan(new Key(COL_NAME1, pKey));
List<Result> actual = new ArrayList<>();
Scanner scanner = storage.scan(scan);
actual.add(scanner.iterator().next());
actual.add(scanner.iterator().next());
actual.add(scanner.iterator().next());
scanner.close();
// Assert
assertThat(actual.size()).isEqualTo(3);
assertThat(actual.get(0).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(actual.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(0).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(actual.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(1).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(actual.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(1).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(actual.get(1).getValue(COL_NAME4).get().getAsInt()).isEqualTo(1);
assertThat(actual.get(2).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(actual.get(2).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(actual.get(2).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(actual.get(2).getValue(COL_NAME4).get().getAsInt()).isEqualTo(2);
}
@Test
public void put_SinglePutGiven_ShouldStoreProperly() throws ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
Key partitionKey = new Key(COL_NAME1, pKey);
Key clusteringKey = new Key(COL_NAME4, cKey);
Get get = new Get(partitionKey, clusteringKey);
// Act
storage.put(puts.get(pKey * 2 + cKey));
// Assert
Optional<Result> actual = storage.get(get);
assertThat(actual.isPresent()).isTrue();
assertThat(actual.get().getValue(COL_NAME1))
.isEqualTo(Optional.of(new IntValue(COL_NAME1, pKey)));
assertThat(actual.get().getValue(COL_NAME2))
.isEqualTo(Optional.of(new TextValue(COL_NAME2, Integer.toString(pKey + cKey))));
assertThat(actual.get().getValue(COL_NAME3))
.isEqualTo(Optional.of(new IntValue(COL_NAME3, pKey + cKey)));
assertThat(actual.get().getValue(COL_NAME4))
.isEqualTo(Optional.of(new IntValue(COL_NAME4, cKey)));
assertThat(actual.get().getValue(COL_NAME5))
.isEqualTo(Optional.of(new BooleanValue(COL_NAME5, cKey % 2 == 0)));
}
@Test
public void put_SinglePutWithIfNotExistsGiven_ShouldStoreProperly() throws ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
puts.get(0).withCondition(new PutIfNotExists());
Key partitionKey = new Key(COL_NAME1, pKey);
Key clusteringKey = new Key(COL_NAME4, cKey);
Get get = new Get(partitionKey, clusteringKey);
// Act
storage.put(puts.get(0));
puts.get(0).withValue(COL_NAME3, Integer.MAX_VALUE);
assertThatThrownBy(() -> storage.put(puts.get(0))).isInstanceOf(NoMutationException.class);
// Assert
Optional<Result> actual = storage.get(get);
assertThat(actual.isPresent()).isTrue();
assertThat(actual.get().getValue(COL_NAME1))
.isEqualTo(Optional.of(new IntValue(COL_NAME1, pKey)));
assertThat(actual.get().getValue(COL_NAME2))
.isEqualTo(Optional.of(new TextValue(COL_NAME2, Integer.toString(pKey + cKey))));
assertThat(actual.get().getValue(COL_NAME3))
.isEqualTo(Optional.of(new IntValue(COL_NAME3, pKey + cKey)));
assertThat(actual.get().getValue(COL_NAME4))
.isEqualTo(Optional.of(new IntValue(COL_NAME4, cKey)));
assertThat(actual.get().getValue(COL_NAME5))
.isEqualTo(Optional.of(new BooleanValue(COL_NAME5, cKey % 2 == 0)));
}
@Test
public void put_MultiplePutGiven_ShouldStoreProperly() throws IOException, ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
Scan scan = new Scan(new Key(COL_NAME1, pKey));
// Act
assertThatCode(() -> storage.put(Arrays.asList(puts.get(0), puts.get(1), puts.get(2))))
.doesNotThrowAnyException();
// Assert
List<Result> results = scanAll(scan);
assertThat(results.size()).isEqualTo(3);
assertThat(results.get(0).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(0).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(pKey + cKey);
assertThat(results.get(1).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(1).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(1).getValue(COL_NAME4).get().getAsInt()).isEqualTo(pKey + cKey + 1);
assertThat(results.get(2).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(2).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(2).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(2).getValue(COL_NAME4).get().getAsInt()).isEqualTo(pKey + cKey + 2);
}
@Test
public void put_MultiplePutWithIfNotExistsGiven_ShouldStoreProperly()
throws IOException, ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
puts.get(0).withCondition(new PutIfNotExists());
puts.get(1).withCondition(new PutIfNotExists());
puts.get(2).withCondition(new PutIfNotExists());
Scan scan = new Scan(new Key(COL_NAME1, pKey));
// Act
assertThatCode(() -> storage.put(Arrays.asList(puts.get(0), puts.get(1), puts.get(2))))
.doesNotThrowAnyException();
// Assert
List<Result> results = scanAll(scan);
assertThat(results.size()).isEqualTo(3);
assertThat(results.get(0).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(0).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(pKey + cKey);
assertThat(results.get(1).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(1).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(1).getValue(COL_NAME4).get().getAsInt()).isEqualTo(pKey + cKey + 1);
assertThat(results.get(2).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(2).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(2).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(2).getValue(COL_NAME4).get().getAsInt()).isEqualTo(pKey + cKey + 2);
}
@Test
public void put_PutWithoutValuesGiven_ShouldStoreProperly() throws ExecutionException {
// Arrange
Key partitionKey = new Key(COL_NAME1, 0);
Key clusteringKey = new Key(COL_NAME4, 0);
// Act
assertThatCode(() -> storage.put(new Put(partitionKey, clusteringKey)))
.doesNotThrowAnyException();
// Assert
Optional<Result> result = storage.get(new Get(partitionKey, clusteringKey));
assertThat(result).isPresent();
}
@Test
public void put_PutWithoutValuesGivenTwice_ShouldStoreProperly() throws ExecutionException {
// Arrange
Key partitionKey = new Key(COL_NAME1, 0);
Key clusteringKey = new Key(COL_NAME4, 0);
// Act
assertThatCode(() -> storage.put(new Put(partitionKey, clusteringKey)))
.doesNotThrowAnyException();
assertThatCode(() -> storage.put(new Put(partitionKey, clusteringKey)))
.doesNotThrowAnyException();
// Assert
Optional<Result> result = storage.get(new Get(partitionKey, clusteringKey));
assertThat(result).isPresent();
}
@Test
public void put_MultiplePutWithIfNotExistsGivenWhenOneExists_ShouldThrowNoMutationException()
throws IOException, ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
assertThatCode(() -> storage.put(puts.get(0))).doesNotThrowAnyException();
puts.get(0).withCondition(new PutIfNotExists());
puts.get(1).withCondition(new PutIfNotExists());
puts.get(2).withCondition(new PutIfNotExists());
Scan scan = new Scan(new Key(COL_NAME1, pKey));
// Act
assertThatThrownBy(() -> storage.put(Arrays.asList(puts.get(0), puts.get(1), puts.get(2))))
.isInstanceOf(NoMutationException.class);
// Assert
List<Result> results = scanAll(scan);
assertThat(results.size()).isEqualTo(1);
assertThat(results.get(0).getValue(COL_NAME4))
.isEqualTo(Optional.of(new IntValue(COL_NAME4, pKey + cKey)));
}
@Test
public void
put_MultiplePutWithDifferentPartitionsWithIfNotExistsGiven_ShouldThrowIllegalArgumentException()
throws IOException, ExecutionException {
// Arrange
List<Put> puts = preparePuts();
puts.get(0).withCondition(new PutIfNotExists());
puts.get(3).withCondition(new PutIfNotExists());
puts.get(6).withCondition(new PutIfNotExists());
// Act
assertThatThrownBy(() -> storage.put(Arrays.asList(puts.get(0), puts.get(3), puts.get(6))))
.isInstanceOf(IllegalArgumentException.class);
// Assert
List<Result> results;
results = scanAll(new Scan(new Key(COL_NAME1, 0)));
assertThat(results.size()).isEqualTo(0);
results = scanAll(new Scan(new Key(COL_NAME1, 3)));
assertThat(results.size()).isEqualTo(0);
results = scanAll(new Scan(new Key(COL_NAME1, 6)));
assertThat(results.size()).isEqualTo(0);
}
@Test
public void put_MultiplePutWithDifferentPartitionsGiven_ShouldThrowIllegalArgumentException()
throws IOException, ExecutionException {
// Arrange
List<Put> puts = preparePuts();
// Act
assertThatThrownBy(() -> storage.put(Arrays.asList(puts.get(0), puts.get(3), puts.get(6))))
.isInstanceOf(IllegalArgumentException.class);
// Assert
List<Result> results;
results = scanAll(new Scan(new Key(COL_NAME1, 0)));
assertThat(results.size()).isEqualTo(0);
results = scanAll(new Scan(new Key(COL_NAME1, 3)));
assertThat(results.size()).isEqualTo(0);
results = scanAll(new Scan(new Key(COL_NAME1, 6)));
assertThat(results.size()).isEqualTo(0);
}
@Test
public void put_MultiplePutWithDifferentConditionsGiven_ShouldStoreProperly()
throws IOException, ExecutionException {
// Arrange
List<Put> puts = preparePuts();
storage.put(puts.get(1));
puts.get(0).withCondition(new PutIfNotExists());
puts.get(1)
.withCondition(
new PutIf(
new ConditionalExpression(
COL_NAME2, new TextValue("1"), ConditionalExpression.Operator.EQ)));
// Act
assertThatCode(() -> storage.put(Arrays.asList(puts.get(0), puts.get(1))))
.doesNotThrowAnyException();
// Assert
List<Result> results = scanAll(new Scan(new Key(COL_NAME1, 0)));
assertThat(results.size()).isEqualTo(2);
assertThat(results.get(0).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(0).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(0);
assertThat(results.get(1).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(1).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(1).getValue(COL_NAME4).get().getAsInt()).isEqualTo(1);
}
@Test
public void put_PutWithIfExistsGivenWhenNoSuchRecord_ShouldThrowNoMutationException()
throws ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
puts.get(0).withCondition(new PutIfExists());
Get get = prepareGet(pKey, cKey);
// Act Assert
assertThatThrownBy(() -> storage.put(puts.get(0))).isInstanceOf(NoMutationException.class);
// Assert
Optional<Result> actual = storage.get(get);
assertThat(actual.isPresent()).isFalse();
}
@Test
public void put_PutWithIfExistsGivenWhenSuchRecordExists_ShouldUpdateRecord()
throws ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
Get get = prepareGet(pKey, cKey);
// Act Assert
storage.put(puts.get(0));
puts.get(0).withCondition(new PutIfExists());
puts.get(0).withValue(COL_NAME3, Integer.MAX_VALUE);
assertThatCode(() -> storage.put(puts.get(0))).doesNotThrowAnyException();
// Assert
Optional<Result> actual = storage.get(get);
assertThat(actual.isPresent()).isTrue();
Result result = actual.get();
assertThat(result.getValue(COL_NAME1)).isEqualTo(Optional.of(new IntValue(COL_NAME1, pKey)));
assertThat(result.getValue(COL_NAME4)).isEqualTo(Optional.of(new IntValue(COL_NAME4, cKey)));
assertThat(result.getValue(COL_NAME3))
.isEqualTo(Optional.of(new IntValue(COL_NAME3, Integer.MAX_VALUE)));
}
@Test
public void put_PutWithIfGivenWhenSuchRecordExists_ShouldUpdateRecord()
throws ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
Get get = prepareGet(pKey, cKey);
// Act Assert
storage.put(puts.get(0));
puts.get(0)
.withCondition(
new PutIf(
new ConditionalExpression(
COL_NAME3, new IntValue(pKey + cKey), ConditionalExpression.Operator.EQ)));
puts.get(0).withValue(COL_NAME3, Integer.MAX_VALUE);
assertThatCode(() -> storage.put(puts.get(0))).doesNotThrowAnyException();
// Assert
Optional<Result> actual = storage.get(get);
assertThat(actual.isPresent()).isTrue();
Result result = actual.get();
assertThat(result.getValue(COL_NAME1)).isEqualTo(Optional.of(new IntValue(COL_NAME1, pKey)));
assertThat(result.getValue(COL_NAME4)).isEqualTo(Optional.of(new IntValue(COL_NAME4, cKey)));
assertThat(result.getValue(COL_NAME3))
.isEqualTo(Optional.of(new IntValue(COL_NAME3, Integer.MAX_VALUE)));
}
@Test
public void put_PutWithIfGivenWhenNoSuchRecord_ShouldThrowNoMutationException()
throws ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
Get get = prepareGet(pKey, cKey);
// Act Assert
storage.put(puts.get(0));
puts.get(0)
.withCondition(
new PutIf(
new ConditionalExpression(
COL_NAME3, new IntValue(pKey + cKey + 1), ConditionalExpression.Operator.EQ)));
puts.get(0).withValue(COL_NAME3, Integer.MAX_VALUE);
assertThatThrownBy(() -> storage.put(puts.get(0))).isInstanceOf(NoMutationException.class);
// Assert
Optional<Result> actual = storage.get(get);
assertThat(actual.isPresent()).isTrue();
Result result = actual.get();
assertThat(result.getValue(COL_NAME1)).isEqualTo(Optional.of(new IntValue(COL_NAME1, pKey)));
assertThat(result.getValue(COL_NAME4)).isEqualTo(Optional.of(new IntValue(COL_NAME4, cKey)));
assertThat(result.getValue(COL_NAME3))
.isEqualTo(Optional.of(new IntValue(COL_NAME3, pKey + cKey)));
}
@Test
public void put_PutWithNullValue_ShouldPutProperly() throws ExecutionException {
// Arrange
Put put = preparePuts().get(0);
storage.put(put);
put.withTextValue(COL_NAME2, null);
put.withBooleanValue(COL_NAME5, null);
// Act
storage.put(put);
// Assert