/
EventSourceNamespaceOpsActorTestCases.java
260 lines (216 loc) · 10.3 KB
/
EventSourceNamespaceOpsActorTestCases.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
/*
* 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.utils.persistence.mongo.namespace;
import java.math.BigInteger;
import java.time.Duration;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import javax.annotation.Nullable;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.services.utils.config.ConfigUtil;
import org.eclipse.ditto.services.utils.persistence.mongo.suffixes.NamespaceSuffixCollectionNames;
import org.eclipse.ditto.services.utils.persistence.mongo.suffixes.SuffixBuilderConfig;
import org.eclipse.ditto.services.utils.test.mongo.MongoDbResource;
import org.eclipse.ditto.signals.commands.namespaces.PurgeNamespace;
import org.eclipse.ditto.signals.commands.namespaces.PurgeNamespaceResponse;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.PoisonPill;
import akka.cluster.pubsub.DistributedPubSub;
import akka.testkit.javadsl.TestKit;
/**
* Tests subclasses of {@link AbstractEventSourceNamespaceOpsActor}.
*/
public abstract class EventSourceNamespaceOpsActorTestCases {
private static final Random RANDOM = new Random();
/**
* Embedded MongoDB resource.
*/
private static MongoDbResource mongoDbResource;
private ActorSystem actorSystem;
@Rule public TestName name = new TestName();
@BeforeClass
public static void startMongoDb() {
mongoDbResource = new MongoDbResource("localhost");
mongoDbResource.start();
}
@AfterClass
public static void tearDown() {
if (mongoDbResource != null) {
mongoDbResource.stop();
mongoDbResource = null;
}
}
@After
public void after() {
stopActorSystem(actorSystem);
}
@Test
public void purgeNamespaceWithSuffixBuilder() {
// cannot start actor system in @Before because config is specific to the test executed
final Config eventSourcingConfiguration = getEventSourcingConfiguration(ConfigFactory.empty());
final ActorSystem actorSystem = startActorSystem(eventSourcingConfiguration);
// suffix builder is active by default
purgeNamespace(actorSystem, eventSourcingConfiguration);
}
@Test
public void purgeNamespaceWithoutSuffixBuilder() {
final Config configOverride =
ConfigFactory.parseString("akka.contrib.persistence.mongodb.mongo.suffix-builder.class=\"\"");
final Config eventSourcingConfiguration = getEventSourcingConfiguration(configOverride);
actorSystem = startActorSystem(eventSourcingConfiguration);
// suffix builder is active by default
purgeNamespace(actorSystem, eventSourcingConfiguration);
}
/**
* Set up configuration required for event-sourcing to work.
*
* @param configOverride overriding config options.
* @return config to feed the actor system and its actors.
*/
private Config getEventSourcingConfiguration(final Config configOverride) {
final String databaseName =
name.getMethodName() + "-" + BigInteger.valueOf(System.currentTimeMillis()).toString(16);
final String mongoUriValue = String.format("\"mongodb://%s:%s/%s\"\n",
mongoDbResource.getBindIp(), mongoDbResource.getPort(), databaseName);
// - do not log dead letters (i. e., events for which there is no subscriber)
// - bind to random available port
// - do not attempt to join an Akka cluster
// - do not shutdown jvm on exit (breaks unit tests)
// - make Mongo URI known to the persistence plugin and to the NamespaceOps actor
final String testConfig = "akka.log-dead-letters=0\n" +
"akka.remote.artery.bind.port=0\n" +
"akka.cluster.seed-nodes=[]\n" +
"akka.coordinated-shutdown.exit-jvm=off\n" +
"ditto.things.log-incoming-messages=true\n" +
"akka.contrib.persistence.mongodb.mongo.mongouri=" + mongoUriValue +
"ditto.services-utils-config.mongodb.uri=" + mongoUriValue;
// load the service config for info about event journal, snapshot store and metadata
final Config configWithSuffixBuilder = ConfigFactory.parseString(testConfig)
.withFallback(ConfigUtil.determineConfig(getServiceName()));
// set namespace suffix config before persisting any event - NullPointerException otherwise
NamespaceSuffixCollectionNames.setConfig(new SuffixBuilderConfig(getSupportedPrefixes()));
return configOverride.withFallback(configWithSuffixBuilder);
}
private ActorSystem startActorSystem(final Config config) {
return ActorSystem.create("AkkaTestSystem-" + name.getMethodName(), config);
}
private static void stopActorSystem(@Nullable final ActorSystem actorSystem) {
if (actorSystem != null) {
TestKit.shutdownActorSystem(actorSystem);
}
}
/**
* @return name of the configured service.
*/
protected abstract String getServiceName();
/**
* @return list of supported persistence ID prefixes - usually a singleton of the actor's resource type.
*/
protected abstract List<String> getSupportedPrefixes();
private void purgeNamespace(final ActorSystem actorSystem, final Config config) {
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.correlationId(String.valueOf(UUID.randomUUID()))
.build();
new TestKit(actorSystem) {{
final String purgedNamespace = "purgedNamespace.x" + RANDOM.nextInt(1000000);
final String survivingNamespace = "survivingNamespace.x" + RANDOM.nextInt(1000000);
final String purgedId = purgedNamespace + ":name";
final String survivingId = survivingNamespace + ":name";
final ActorRef pubSubMediator = DistributedPubSub.get(actorSystem).mediator();
final ActorRef actorToPurge = watch(startEntityActor(actorSystem, pubSubMediator, purgedId));
final ActorRef survivingActor = watch(startEntityActor(actorSystem, pubSubMediator, survivingId));
final ActorRef underTest = startActorUnderTest(actorSystem, pubSubMediator, config);
// create 2 entities in 2 namespaces, 1 of which will be purged
actorToPurge.tell(getCreateEntityCommand(purgedId), getRef());
expectMsgClass(Duration.ofSeconds(10L), getCreateEntityResponseClass());
survivingActor.tell(getCreateEntityCommand(survivingId), getRef());
expectMsgClass(Duration.ofSeconds(10L), getCreateEntityResponseClass());
// kill the actor in the namespace to be purged to avoid write conflict
actorToPurge.tell(PoisonPill.getInstance(), getRef());
expectTerminated(actorToPurge);
// purge the namespace
underTest.tell(PurgeNamespace.of(purgedNamespace, dittoHeaders), getRef());
expectMsg(PurgeNamespaceResponse.successful(purgedNamespace, getResourceType(), dittoHeaders));
// restart the actor in the purged namespace - it should work as if its entity never existed
final ActorRef purgedActor = watch(startEntityActor(actorSystem, pubSubMediator, purgedId));
purgedActor.tell(getRetrieveEntityCommand(purgedId), getRef());
expectMsgClass(getEntityNotAccessibleClass());
// the actor outside the purged namespace should not be affected
survivingActor.tell(getRetrieveEntityCommand(survivingId), getRef());
expectMsgClass(getRetrieveEntityResponseClass());
// stop both actors - neither should pollute the database on death
purgedActor.tell(PoisonPill.getInstance(), getRef());
expectTerminated(purgedActor);
survivingActor.tell(PoisonPill.getInstance(), getRef());
expectTerminated(survivingActor);
}};
}
/**
* Start an entity's persistence actor.
*
* @param system the actor system.
* @param pubSubMediator Akka pub-sub mediator.
* @param id ID of the entity.
* @return reference to the entity actor.
*/
protected abstract ActorRef startEntityActor(final ActorSystem system, final ActorRef pubSubMediator,
final String id);
/**
* Starts the NamespaceOps actor.
*
* @param actorSystem the actor system.
* @param pubSubMediator Akka pub-sub mediator.
* @param config configuration with info about event journal, snapshot store, metadata and database.
* @return reference of the NamespaceOps actor.
*/
protected abstract ActorRef startActorUnderTest(final ActorSystem actorSystem, final ActorRef pubSubMediator,
final Config config);
/**
* Get the command to create an entity belonging to the given resource type.
*
* @param id ID of the entity.
* @return Command to create it.
*/
protected abstract Object getCreateEntityCommand(final String id);
/**
* @return type of responses for successful entity creation.
*/
protected abstract Class<?> getCreateEntityResponseClass();
/**
* @return type of responses for retrieval of nonexistent entities.
*/
protected abstract Class<?> getEntityNotAccessibleClass();
/**
* @return type of responses for successful entity retrieval.
*/
protected abstract Class<?> getRetrieveEntityResponseClass();
/**
* Get the command to retrieve an entity belonging to the given resource type.
*
* @param id ID of the entity.
* @return Command to retrieve it.
*/
protected abstract Object getRetrieveEntityCommand(final String id);
/**
* @return resource type of the NamespaceOps actor being tested.
*/
protected abstract String getResourceType();
}