-
Notifications
You must be signed in to change notification settings - Fork 215
/
PersistenceActorTestBase.java
executable file
·244 lines (200 loc) · 9.7 KB
/
PersistenceActorTestBase.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
/*
* 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.things.service.persistence.actors;
import static java.util.Objects.requireNonNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.auth.AuthorizationModelFactory;
import org.eclipse.ditto.base.model.auth.AuthorizationSubject;
import org.eclipse.ditto.base.model.auth.DittoAuthorizationContextType;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.internal.utils.pubsub.DistributedPub;
import org.eclipse.ditto.internal.utils.pubsub.LiveSignalPub;
import org.eclipse.ditto.internal.utils.pubsub.extractors.AckExtractor;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.things.model.Attributes;
import org.eclipse.ditto.things.model.Feature;
import org.eclipse.ditto.things.model.FeatureDefinition;
import org.eclipse.ditto.things.model.FeatureProperties;
import org.eclipse.ditto.things.model.Features;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.ThingLifecycle;
import org.eclipse.ditto.things.model.ThingsModelFactory;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
import org.eclipse.ditto.things.service.common.config.DefaultThingConfig;
import org.eclipse.ditto.things.service.common.config.ThingConfig;
import org.eclipse.ditto.things.service.enforcement.TestSetup;
import org.eclipse.ditto.utils.jsr305.annotations.AllParametersAndReturnValuesAreNonnullByDefault;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.rules.TestWatcher;
import org.slf4j.Logger;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.testkit.TestProbe;
import akka.testkit.javadsl.TestKit;
/**
* Base test class for testing persistence actors of the things persistence.
*/
public abstract class PersistenceActorTestBase {
protected static final ThingId THING_ID = ThingId.of("org.eclipse.ditto", "thingId");
protected static final PolicyId POLICY_ID = PolicyId.of("org.eclipse.ditto:policyId");
protected static final String AUTH_SUBJECT = "allowedId";
protected static final AuthorizationSubject AUTHORIZED_SUBJECT =
AuthorizationModelFactory.newAuthSubject("test:" + AUTH_SUBJECT);
protected static final Attributes THING_ATTRIBUTES = ThingsModelFactory.newAttributesBuilder()
.set("attrKey", "attrVal")
.build();
protected static final Predicate<JsonField> IS_MODIFIED = field -> field.getDefinition()
.map(Thing.JsonFields.MODIFIED::equals)
.orElse(false);
protected static final JsonFieldSelector ALL_FIELDS_SELECTOR = JsonFactory.newFieldSelector(
Thing.JsonFields.ATTRIBUTES, Thing.JsonFields.FEATURES, Thing.JsonFields.ID, Thing.JsonFields.MODIFIED,
Thing.JsonFields.CREATED, Thing.JsonFields.REVISION, Thing.JsonFields.POLICY_ID,
Thing.JsonFields.LIFECYCLE);
private static final FeatureDefinition FEATURE_DEFINITION = FeatureDefinition.fromIdentifier("ns:name:version");
private static final FeatureProperties FEATURE_PROPERTIES =
FeatureProperties.newBuilder().set("featureKey", "featureValue").build();
private static final Feature THING_FEATURE =
ThingsModelFactory.newFeature("featureId", FEATURE_DEFINITION, FEATURE_PROPERTIES);
private static final Features THING_FEATURES = ThingsModelFactory.newFeaturesBuilder()
.set(THING_FEATURE)
.build();
private static final ThingLifecycle THING_LIFECYCLE = ThingLifecycle.ACTIVE;
private static final long THING_REVISION = 1;
protected static Config testConfig;
protected static ThingConfig thingConfig;
protected ActorSystem actorSystem = null;
protected TestProbe pubSubTestProbe = null;
protected TestProbe policiesShardRegionTestProbe = null;
protected ActorRef pubSubMediator = null;
protected ActorRef policiesShardRegion = null;
protected DittoHeaders dittoHeadersV2;
@BeforeClass
public static void initTestFixture() {
testConfig = ConfigFactory.load("test");
thingConfig = getThingConfig(testConfig);
}
protected static ThingConfig getThingConfig(final Config testConfig) {
return DefaultThingConfig.of(testConfig.getConfig("ditto.things"));
}
protected static DittoHeaders createDittoHeadersMock(final JsonSchemaVersion schemaVersion,
final String... authSubjectIds) {
final List<AuthorizationSubject> authSubjects = Arrays.stream(authSubjectIds)
.map(AuthorizationSubject::newInstance)
.collect(Collectors.toList());
return DittoHeaders.newBuilder()
.authorizationContext(
AuthorizationContext.newInstance(DittoAuthorizationContextType.UNSPECIFIED, authSubjects))
.schemaVersion(schemaVersion)
.build();
}
protected static Thing createThingV2WithRandomId() {
return createThingV2WithId(ThingId.of(THING_ID.getNamespace(), THING_ID.getName() + UUID.randomUUID()));
}
protected static Thing createThingV2WithId(final ThingId thingId) {
return ThingsModelFactory.newThingBuilder()
.setLifecycle(THING_LIFECYCLE)
.setAttributes(THING_ATTRIBUTES)
.setFeatures(THING_FEATURES)
.setRevision(THING_REVISION)
.setId(thingId)
.setPolicyId(POLICY_ID)
.build();
}
protected void setup(final Config customConfig) {
requireNonNull(customConfig, "Consider to use ConfigFactory.empty()");
final Config config = customConfig.withFallback(ConfigFactory.parseMap(
Map.of("ditto.pre-enforcer-provider","org.eclipse.ditto.policies.enforcement." +
"DefaultPreEnforcerProvider"))).withFallback(ConfigFactory.load("test"));
actorSystem = ActorSystem.create("AkkaTestSystem", config);
pubSubTestProbe = TestProbe.apply("mock-pubSub-mediator", actorSystem);
pubSubMediator = pubSubTestProbe.ref();
policiesShardRegionTestProbe = TestProbe.apply("mock-policiesShardRegion", actorSystem);
policiesShardRegion = policiesShardRegionTestProbe.ref();
dittoHeadersV2 = createDittoHeadersMock(JsonSchemaVersion.V_2, "test:" + AUTH_SUBJECT);
}
@After
public void tearDownBase() {
TestKit.shutdownActorSystem(actorSystem);
actorSystem = null;
}
protected ActorRef createPersistenceActorFor(final ThingId thingId) {
return createPersistenceActorWithPubSubFor(thingId);
}
protected ActorRef createPersistenceActorWithPubSubFor(final ThingId thingId) {
return actorSystem.actorOf(getPropsOfThingPersistenceActor(thingId, getDistributedPub()));
}
private Props getPropsOfThingPersistenceActor(final ThingId thingId, final DistributedPub<ThingEvent<?>> pub) {
return ThingPersistenceActor.props(thingId, pub, pubSubMediator);
}
protected ActorRef createSupervisorActorFor(final ThingId thingId) {
final LiveSignalPub liveSignalPub = new TestSetup.DummyLiveSignalPub(pubSubMediator);
final Props props =
ThingSupervisorActor.props(pubSubMediator, policiesShardRegion, liveSignalPub,
this::getPropsOfThingPersistenceActor, null);
return actorSystem.actorOf(props, thingId.toString());
}
/**
* This TestWatcher logs the name of each performed test method.
*/
protected static final class TestedMethodLoggingWatcher extends TestWatcher {
private final Logger logger;
public TestedMethodLoggingWatcher(final Logger logger) {
this.logger = logger;
}
@Override
protected void starting(final org.junit.runner.Description description) {
logger.info("Testing: {}#{}()", description.getTestClass().getSimpleName(), description.getMethodName());
}
}
/**
* Disable logging for 1 test to hide stacktrace or other logs on level ERROR. Comment out to debug the test.
*/
protected void disableLogging() {
actorSystem.eventStream().setLogLevel(akka.stream.Attributes.logLevelOff());
}
protected DistributedPub<ThingEvent<?>> getDistributedPub() {
return new TestPub();
}
@AllParametersAndReturnValuesAreNonnullByDefault
private final class TestPub implements DistributedPub<ThingEvent<?>> {
private TestPub() {}
@Override
public ActorRef getPublisher() {
return pubSubMediator;
}
@Override
public Object wrapForPublication(final ThingEvent<?> message) {
return message;
}
@Override
public <S extends ThingEvent<?>> Object wrapForPublicationWithAcks(final S message,
final AckExtractor<S> ackExtractor) {
return wrapForPublication(message);
}
}
}