-
Notifications
You must be signed in to change notification settings - Fork 214
/
AbstractThingSearchPersistenceITBase.java
224 lines (189 loc) · 8.02 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
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.thingsearch.service.persistence;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletionStage;
import javax.annotation.Nullable;
import org.bson.Document;
import org.eclipse.ditto.rql.query.Query;
import org.eclipse.ditto.rql.query.QueryBuilderFactory;
import org.eclipse.ditto.rql.query.criteria.CriteriaFactory;
import org.eclipse.ditto.rql.query.expression.FieldExpressionUtil;
import org.eclipse.ditto.rql.query.expression.ThingsFieldExpressionFactory;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.base.service.config.limits.DefaultLimitsConfig;
import org.eclipse.ditto.thingsearch.service.common.model.ResultList;
import org.eclipse.ditto.thingsearch.service.persistence.read.MongoThingsSearchPersistence;
import org.eclipse.ditto.thingsearch.service.persistence.read.query.MongoQueryBuilderFactory;
import org.eclipse.ditto.thingsearch.service.persistence.write.streaming.SearchUpdateMapper;
import org.eclipse.ditto.thingsearch.service.persistence.write.streaming.TestSearchUpdaterStream;
import org.eclipse.ditto.internal.utils.persistence.mongo.DittoMongoClient;
import org.eclipse.ditto.internal.utils.persistence.mongo.MongoClientWrapper;
import org.eclipse.ditto.internal.utils.test.mongo.MongoDbResource;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
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.Materializer;
import akka.stream.SystemMaterializer;
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 {
@ClassRule
public static final MongoDbResource MONGO_RESOURCE = new MongoDbResource();
protected static final List<String> KNOWN_SUBJECTS = Collections.singletonList("abc:mySid");
private static final Map<String, String> mongoSimpleFieldMappings = new HashMap<>();
static {
mongoSimpleFieldMappings.put(FieldExpressionUtil.FIELD_NAME_THING_ID, FieldExpressionUtil.FIELD_ID);
mongoSimpleFieldMappings.put(FieldExpressionUtil.FIELD_NAME_NAMESPACE, FieldExpressionUtil.FIELD_NAMESPACE);
}
protected static final CriteriaFactory cf = CriteriaFactory.getInstance();
protected static final ThingsFieldExpressionFactory fef = ThingsFieldExpressionFactory.of(mongoSimpleFieldMappings);
protected static QueryBuilderFactory qbf;
protected static DittoMongoClient mongoClient;
protected MongoCollection<Document> thingsCollection;
protected MongoThingsSearchPersistence readPersistence;
protected TestSearchUpdaterStream writePersistence;
protected ActorSystem actorSystem;
protected LoggingAdapter log;
@BeforeClass
public static void startMongoResource() {
final Config rawTestConfig = ConfigFactory.load("test");
final DefaultLimitsConfig limitsConfig = DefaultLimitsConfig.of(rawTestConfig.getConfig("ditto"));
qbf = new MongoQueryBuilderFactory(limitsConfig);
mongoClient = provideClientWrapper();
}
@Before
public void before() {
final Config config = ConfigFactory.load("test");
actorSystem = ActorSystem.create("AkkaTestSystem", config);
log = actorSystem.log();
readPersistence = provideReadPersistence();
writePersistence = provideWritePersistence();
thingsCollection = mongoClient.getDefaultDatabase().getCollection(PersistenceConstants.THINGS_COLLECTION_NAME);
}
private MongoThingsSearchPersistence provideReadPersistence() {
final MongoThingsSearchPersistence result = new MongoThingsSearchPersistence(mongoClient, actorSystem);
// explicitly trigger CompletableFuture to make sure that indices are created before test runs
result.initializeIndices().toCompletableFuture().join();
return result;
}
private TestSearchUpdaterStream provideWritePersistence() {
return TestSearchUpdaterStream.of(mongoClient.getDefaultDatabase(), SearchUpdateMapper.get(actorSystem));
}
private static DittoMongoClient provideClientWrapper() {
return MongoClientWrapper.getBuilder()
.connectionString(
"mongodb://" + MONGO_RESOURCE.getBindIp() + ":" + MONGO_RESOURCE.getPort() + "/testSearchDB")
.connectionPoolMaxSize(100)
.connectionPoolMaxWaitTime(Duration.ofSeconds(30))
.build();
}
@After
public void after() {
if (mongoClient != null) {
dropCollections(List.of(thingsCollection));
}
if (actorSystem != null) {
TestKit.shutdownActorSystem(actorSystem);
actorSystem = null;
log = null;
actorSystem = 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.close();
}
} catch (final IllegalStateException e) {
System.err.println("IllegalStateException during shutdown of MongoDB: " + e.getMessage());
}
}
protected Long count(final Query query) {
return count(query, KNOWN_SUBJECTS);
}
protected Long count(final Query query, @Nullable final List<String> subjectIds) {
return readPersistence.count(query, subjectIds)
.runWith(Sink.head(), actorSystem)
.toCompletableFuture()
.join();
}
protected ResultList<ThingId> findAll(final Query query) {
return findAll(query, KNOWN_SUBJECTS);
}
protected ResultList<ThingId> findAll(final Query query, final List<String> subjectIds) {
return readPersistence.findAll(query, subjectIds)
.runWith(Sink.head(), actorSystem)
.toCompletableFuture()
.join();
}
protected <T> T runBlockingWithReturn(final Source<T, NotUsed> publisher) {
final CompletionStage<T> done = publisher.runWith(Sink.last(), actorSystem);
return done.toCompletableFuture().join();
}
protected final DittoMongoClient getClient() {
return mongoClient;
}
final Materializer getMaterializer() {
return SystemMaterializer.get(actorSystem).materializer();
}
protected <T> List<T> waitFor(final Source<T, ?> source) {
return source.runWith(Sink.seq(), actorSystem)
.toCompletableFuture()
.join();
}
@SuppressWarnings("squid:S2925")
private void backoff() {
try {
Thread.sleep(100);
} catch (final Exception e) {
// do nothing
}
}
}