-
Notifications
You must be signed in to change notification settings - Fork 25
/
BucketManagerTest.java
144 lines (123 loc) · 5.19 KB
/
BucketManagerTest.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
/*
* Copyright (c) 2020 Otavio Santana and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License v2.0
* w/Classpath exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package jakarta.nosql.tck.communication.driver.keyvalue;
import jakarta.nosql.ServiceLoaderProvider;
import jakarta.nosql.Value;
import jakarta.nosql.keyvalue.BucketManager;
import jakarta.nosql.keyvalue.KeyValueEntity;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class BucketManagerTest {
private User userOtavio = new User("otavio");
private KeyValueEntity keyValueOtavio = KeyValueEntity.of("otavio", Value.of(userOtavio));
private User userSoro = new User("soro");
private KeyValueEntity keyValueSoro = KeyValueEntity.of("soro", Value.of(userSoro));
@BeforeEach
public void setUp() {
Module module = BucketManagerTest.class.getModule();
module.addUses(BucketManagerSupplier.class);
}
@Test
public void shouldPutValue() {
final BucketManager manager = getBucketManager();
manager.put("otavio", userOtavio);
Optional<Value> otavio = manager.get("otavio");
assertTrue(otavio.isPresent());
assertEquals(userOtavio, otavio.get().get(User.class));
}
@Test
public void shouldPutKeyValue() {
final BucketManager manager = getBucketManager();
manager.put(keyValueOtavio);
Optional<Value> otavio = manager.get("otavio");
assertTrue(otavio.isPresent());
assertEquals(userOtavio, otavio.get().get(User.class));
}
@Test
public void shouldPutIterableKeyValue() {
final BucketManager manager = getBucketManager();
manager.put(asList(keyValueSoro, keyValueOtavio));
Optional<Value> otavio = manager.get("otavio");
assertTrue(otavio.isPresent());
assertEquals(userOtavio, otavio.get().get(User.class));
Optional<Value> soro = manager.get("soro");
assertTrue(soro.isPresent());
assertEquals(userSoro, soro.get().get(User.class));
}
@Test
public void shouldMultiGet() {
final BucketManager manager = getBucketManager();
User user = new User("otavio");
KeyValueEntity keyValue = KeyValueEntity.of("otavio", Value.of(user));
manager.put(keyValue);
assertNotNull(manager.get("otavio"));
}
@Test
public void shouldRemoveKey() {
final BucketManager manager = getBucketManager();
manager.put(keyValueOtavio);
assertTrue(manager.get("otavio").isPresent());
manager.delete("otavio");
assertFalse(manager.get("otavio").isPresent());
}
@Test
public void shouldRemoveMultiKey() {
final BucketManager manager = getBucketManager();
manager.put(asList(keyValueSoro, keyValueOtavio));
List<String> keys = asList("otavio", "soro");
Iterable<Value> values = manager.get(keys);
assertThat(StreamSupport.stream(values.spliterator(), false).map(value -> value.get(User.class))
.collect(Collectors.toList()),
Matchers.containsInAnyOrder(userOtavio, userSoro));
manager.delete(keys);
assertEquals(0L, StreamSupport.stream(manager.get(keys).spliterator(), false).count());
}
@AfterEach
public void remove() {
final Optional<BucketManagerSupplier> supplier = getSupplier();
if (!supplier.isPresent()) {
final BucketManager manager = getBucketManager();
manager.delete(Arrays.asList("otavio", "soro"));
}
}
private BucketManager getBucketManager() {
final BucketManagerSupplier bucketManagerSupplier = ServiceLoaderProvider
.get(BucketManagerSupplier.class,
()-> ServiceLoader.load(BucketManagerSupplier.class));
return bucketManagerSupplier.get();
}
private Optional<BucketManagerSupplier> getSupplier() {
return ServiceLoaderProvider
.getSupplierStream(BucketManagerSupplier.class,
()-> ServiceLoader.load(BucketManagerSupplier.class))
.map(BucketManagerSupplier.class::cast)
.findFirst();
}
}