/
AbstractThingSearchPersistenceITBase.java
317 lines (277 loc) · 12.6 KB
/
AbstractThingSearchPersistenceITBase.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
/*
* Copyright (c) 2017-2018 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.thingsearch.persistence;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.POLICIES_BASED_SEARCH_INDEX_COLLECTION_NAME;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.THINGS_COLLECTION_NAME;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.THINGS_SYNC_STATE_COLLECTION_NAME;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import org.bson.Document;
import org.eclipse.ditto.model.query.Query;
import org.eclipse.ditto.model.query.QueryBuilderFactory;
import org.eclipse.ditto.model.query.criteria.CriteriaFactory;
import org.eclipse.ditto.model.query.criteria.CriteriaFactoryImpl;
import org.eclipse.ditto.model.query.expression.ThingsFieldExpressionFactory;
import org.eclipse.ditto.model.query.expression.ThingsFieldExpressionFactoryImpl;
import org.eclipse.ditto.model.things.Thing;
import org.eclipse.ditto.services.base.config.DittoLimitsConfigReader;
import org.eclipse.ditto.services.thingsearch.common.model.ResultList;
import org.eclipse.ditto.services.thingsearch.persistence.read.AggregationBuilderFactory;
import org.eclipse.ditto.services.thingsearch.persistence.read.MongoThingsSearchPersistence;
import org.eclipse.ditto.services.thingsearch.persistence.read.PolicyRestrictedSearchAggregation;
import org.eclipse.ditto.services.thingsearch.persistence.read.query.MongoAggregationBuilderFactory;
import org.eclipse.ditto.services.thingsearch.persistence.read.query.MongoQueryBuilderFactory;
import org.eclipse.ditto.services.thingsearch.persistence.write.impl.MongoEventToPersistenceStrategyFactory;
import org.eclipse.ditto.services.thingsearch.persistence.write.impl.MongoThingsSearchUpdaterPersistence;
import org.eclipse.ditto.services.utils.persistence.mongo.MongoClientWrapper;
import org.eclipse.ditto.services.utils.test.mongo.MongoDbResource;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import com.mongodb.reactivestreams.client.MongoCollection;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import akka.NotUsed;
import akka.actor.ActorSystem;
import akka.event.LoggingAdapter;
import akka.stream.ActorMaterializer;
import akka.stream.Materializer;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;
import akka.testkit.javadsl.TestKit;
/**
* Abstract base class for search persistence tests.
*/
public abstract class AbstractThingSearchPersistenceITBase {
protected static final List<String> KNOWN_SUBJECTS = Collections.singletonList("abc:mySid");
protected static final List<String> KNOWN_SUBJECTS_2 = Arrays.asList("some:mySid", "some:unknown");
protected static final String KNOWN_ATTRIBUTE_1 = "attribute1";
protected static final String KNOWN_NEW_VALUE = "newValue";
protected static final CriteriaFactory cf = new CriteriaFactoryImpl();
protected static final ThingsFieldExpressionFactory fef = new ThingsFieldExpressionFactoryImpl();
protected static final QueryBuilderFactory qbf = new MongoQueryBuilderFactory
(DittoLimitsConfigReader.fromRawConfig(ConfigFactory.load("test")));
protected static final AggregationBuilderFactory abf = new MongoAggregationBuilderFactory
(DittoLimitsConfigReader.fromRawConfig(ConfigFactory.load("test")));
private static MongoDbResource mongoResource;
private static MongoClientWrapper mongoClient;
/** */
private MongoThingsSearchPersistence readPersistence;
private MongoCollection<Document> thingsCollection;
private MongoCollection<Document> policiesCollection;
private MongoCollection<Document> syncCollection;
protected MongoThingsSearchUpdaterPersistence writePersistence;
private ActorSystem actorSystem;
private ActorMaterializer actorMaterializer;
protected LoggingAdapter log;
@BeforeClass
public static void startMongoResource() {
mongoResource = new MongoDbResource("localhost");
mongoResource.start();
mongoClient = provideClientWrapper();
}
/** */
@Before
public void before() {
final Config config = ConfigFactory.load("test");
actorSystem = ActorSystem.create("AkkaTestSystem", config);
log = actorSystem.log();
actorMaterializer = ActorMaterializer.create(actorSystem);
readPersistence = provideReadPersistence();
writePersistence = provideWritePersistence();
thingsCollection = mongoClient.getDatabase().getCollection(THINGS_COLLECTION_NAME);
policiesCollection = mongoClient.getDatabase().getCollection(POLICIES_BASED_SEARCH_INDEX_COLLECTION_NAME);
syncCollection = mongoClient.getDatabase().getCollection(THINGS_SYNC_STATE_COLLECTION_NAME);
}
private MongoThingsSearchPersistence provideReadPersistence() {
final MongoThingsSearchPersistence mongoThingsSearchPersistence =
new MongoThingsSearchPersistence(provideClientWrapper(), actorSystem);
try {
// explicitly trigger CompletableFuture to make sure that indices are created before test runs
mongoThingsSearchPersistence.initializeIndices().toCompletableFuture().get();
} catch (final InterruptedException | ExecutionException e) {
throw new IllegalStateException(e);
}
return mongoThingsSearchPersistence;
}
private MongoThingsSearchUpdaterPersistence provideWritePersistence() {
final MongoThingsSearchUpdaterPersistence mongoThingsSearchUpdaterPersistence =
new MongoThingsSearchUpdaterPersistence(mongoClient, log,
MongoEventToPersistenceStrategyFactory.getInstance(), getMaterializer());
try {
// explicitly trigger CompletableFuture to make sure that indices are created before test runs
mongoThingsSearchUpdaterPersistence.initializeIndices().toCompletableFuture().get();
} catch (final InterruptedException | ExecutionException e) {
throw new IllegalStateException(e);
}
return mongoThingsSearchUpdaterPersistence;
}
private static MongoClientWrapper provideClientWrapper() {
return MongoClientWrapper.newInstance(mongoResource.getBindIp(), mongoResource.getPort(), "testSearchDB",
100, 500000, 30);
}
/** */
@After
public void after() {
if (mongoClient != null) {
dropCollections(Arrays.asList(thingsCollection, policiesCollection, syncCollection));
}
if (actorSystem != null) {
TestKit.shutdownActorSystem(actorSystem);
actorSystem = null;
log = null;
actorMaterializer = null;
}
}
private void dropCollections(final List<MongoCollection<Document>> collections) {
collections.stream()
.filter(Objects::nonNull)
.forEach(this::dropCollectionWithBackoff);
}
private void dropCollectionWithBackoff(final MongoCollection<Document> collection) {
RuntimeException lastException = null;
for (int i = 0; i < 20; ++i) {
try {
waitFor(Source.fromPublisher(collection.drop()));
return;
} catch (final RuntimeException e) {
lastException = e;
backoff();
}
}
throw lastException;
}
@AfterClass
public static void stopMongoResource() {
try {
if (mongoClient != null) {
mongoClient.getMongoClient().close();
}
if (mongoResource != null) {
mongoResource.stop();
}
} catch (final IllegalStateException e) {
System.err.println("IllegalStateException during shutdown of MongoDB: " + e.getMessage());
}
}
protected Long count(final Query query) {
try {
return readPersistence.count(query) //
.limit(1) //
.runWith(Sink.seq(), actorMaterializer) //
.toCompletableFuture() //
.get() //
.get(0);
} catch (final InterruptedException | ExecutionException e) {
throw mapAsRuntimeException(e);
}
}
protected long aggregateCount(final PolicyRestrictedSearchAggregation policyRestrictedSearchAggregation) {
try {
return readPersistence.count(policyRestrictedSearchAggregation) //
.runWith(Sink.seq(), actorMaterializer) //
.toCompletableFuture() //
.get() //
.get(0);
} catch (final InterruptedException | ExecutionException e) {
throw mapAsRuntimeException(e);
}
}
protected ResultList<String> findAll(final PolicyRestrictedSearchAggregation policyRestrictedSearchAggregation) {
return waitFor(readPersistence.findAll(policyRestrictedSearchAggregation)).get(0);
}
protected ResultList<String> findAll(final Query query) {
try {
return readPersistence.findAll(query) //
.limit(1) //
.runWith(Sink.seq(), actorMaterializer) //
.toCompletableFuture() //
.get() //
.get(0);
} catch (final InterruptedException | ExecutionException e) {
throw mapAsRuntimeException(e);
}
}
protected void runBlocking(final Source<?, NotUsed> publisher) {
final List<Source<?, NotUsed>> publishers = Collections.singletonList(publisher);
runBlocking(publishers);
}
protected void runBlocking(final List<Source<?, NotUsed>> publishers) {
publishers.stream()
.map(p -> p.runWith(Sink.ignore(), actorMaterializer))
.map(CompletionStage::toCompletableFuture)
.forEach(this::finishCompletableFuture);
}
private void finishCompletableFuture(final CompletableFuture future) {
try {
future.get();
} catch (final InterruptedException | ExecutionException e) {
throw mapAsRuntimeException(e);
}
}
protected <T> T runBlockingWithReturn(final Source<T, NotUsed> publisher) {
final CompletionStage<T> done = publisher.runWith(Sink.last(), actorMaterializer);
try {
return done.toCompletableFuture().get();
} catch (final InterruptedException | ExecutionException e) {
throw mapAsRuntimeException(e);
}
}
private static RuntimeException mapAsRuntimeException(final Throwable t) {
// shortcut: RTEs can be returned as-is
if (t instanceof RuntimeException) {
return (RuntimeException) t;
}
// for ExecutionExceptions, extract the cause
if (t instanceof ExecutionException && t.getCause() != null) {
return mapAsRuntimeException(t.getCause());
}
// wrap non-RTEs as IllegalStateException
return new IllegalStateException(t);
}
protected void insertOrUpdateThing(final Thing thing, final long revision, final long policyRevision) {
runBlocking(writePersistence.insertOrUpdate(thing, revision, policyRevision));
}
protected void delete(final String thingId, final long revision) {
runBlocking(writePersistence.delete(thingId, revision));
}
protected final MongoClientWrapper getClient() {
return mongoClient;
}
protected final Materializer getMaterializer() {
return actorMaterializer;
}
private <T> List<T> waitFor(final Source<T, ?> source) {
try {
return source
.limit(1) //
.runWith(Sink.seq(), actorMaterializer) //
.toCompletableFuture() //
.get();
} catch (final InterruptedException | ExecutionException e) {
throw mapAsRuntimeException(e);
}
}
@SuppressWarnings("squid:S2925")
private void backoff() {
try {
Thread.sleep(100);
} catch (final InterruptedException e) {
// do nothing
}
}
}