-
Notifications
You must be signed in to change notification settings - Fork 215
/
TestSetup.java
311 lines (262 loc) · 13.4 KB
/
TestSetup.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
/*
* Copyright (c) 2022 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.policies.enforcement;
import static org.eclipse.ditto.base.model.json.JsonSchemaVersion.V_2;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.auth.AuthorizationSubject;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.base.model.signals.SignalWithEntityId;
import org.eclipse.ditto.internal.utils.cache.Cache;
import org.eclipse.ditto.internal.utils.cache.CaffeineCache;
import org.eclipse.ditto.internal.utils.cache.entry.Entry;
import org.eclipse.ditto.internal.utils.cacheloaders.EnforcementCacheKey;
import org.eclipse.ditto.internal.utils.cacheloaders.PolicyEnforcer;
import org.eclipse.ditto.internal.utils.cacheloaders.PolicyEnforcerCacheLoader;
import org.eclipse.ditto.internal.utils.cacheloaders.ThingEnforcementIdCacheLoader;
import org.eclipse.ditto.internal.utils.cacheloaders.config.AskWithRetryConfig;
import org.eclipse.ditto.internal.utils.cluster.DistPubSubAccess;
import org.eclipse.ditto.internal.utils.config.DefaultScopedConfig;
import org.eclipse.ditto.internal.utils.pubsub.DistributedPub;
import org.eclipse.ditto.internal.utils.pubsub.LiveSignalPub;
import org.eclipse.ditto.internal.utils.pubsub.StreamingType;
import org.eclipse.ditto.internal.utils.pubsub.extractors.AckExtractor;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.policies.enforcement.config.CachesConfig;
import org.eclipse.ditto.policies.enforcement.config.DefaultCachesConfig;
import org.eclipse.ditto.policies.enforcement.config.DefaultEnforcementConfig;
import org.eclipse.ditto.policies.enforcement.config.EnforcementConfig;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.enforcers.Enforcer;
import org.eclipse.ditto.things.model.Feature;
import org.eclipse.ditto.things.model.FeatureProperties;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingBuilder;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.ThingsModelFactory;
import org.eclipse.ditto.things.model.signals.commands.ThingCommand;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
import com.github.benmanes.caffeine.cache.Caffeine;
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;
import scala.concurrent.duration.FiniteDuration;
public final class TestSetup {
public static final String THING_SUDO = "thing-sudo";
public static final String POLICY_SUDO = "policy-sudo";
public static final String FEATURE_ID = "x";
public static final String FEATURE_PROPERTY_1 = "key1";
public static final String FEATURE_PROPERTY_2 = "key2";
public static final ThingId THING_ID = ThingId.of("thing", "id");
public static final PolicyId POLICY_ID = PolicyId.of("policy", "id");
public static final Feature FEATURE = Feature.newBuilder()
.properties(FeatureProperties.newBuilder()
.set(FEATURE_PROPERTY_1, 1)
.set(FEATURE_PROPERTY_2, "some string")
.build())
.withId(FEATURE_ID)
.build();
public static final Thing THING = Thing.newBuilder()
.setId(THING_ID)
.setPolicyId(POLICY_ID)
.setFeature(FEATURE)
.build();
public static final String SUBJECT_ID = "subject";
public static final AuthorizationSubject SUBJECT = AuthorizationSubject.newInstance("dummy:" + SUBJECT_ID);
private static final Config RAW_CONFIG = ConfigFactory.load("test");
private static final CachesConfig CACHES_CONFIG;
private static final EnforcementConfig ENFORCEMENT_CONFIG;
static {
final var dittoScopedConfig = DefaultScopedConfig.dittoScoped(RAW_CONFIG);
final var conciergeScopedConfig = DefaultScopedConfig.newInstance(dittoScopedConfig, "concierge");
CACHES_CONFIG = DefaultCachesConfig.of(conciergeScopedConfig);
ENFORCEMENT_CONFIG = DefaultEnforcementConfig.of(conciergeScopedConfig);
}
public static ActorRef newEnforcerActor(final ActorSystem system, final ActorRef testActorRef,
final ActorRef mockEntitiesActor) {
return new EnforcerActorBuilder(system, testActorRef, mockEntitiesActor, mockEntitiesActor).build();
}
public static ActorRef newEnforcerActor(final ActorSystem system,
final ActorRef testActorRef,
final ActorRef thingsShardRegion,
final ActorRef policiesShardRegion,
final ActorRef pubSubMediatorRef,
@Nullable final PreEnforcer preEnforcer,
@Nullable final CreationRestrictionEnforcer creationRestrictionEnforcer) {
return new EnforcerActorBuilder(system, testActorRef, thingsShardRegion, policiesShardRegion, pubSubMediatorRef)
.setPreEnforcer(preEnforcer).setCreationRestrictionEnforcer(creationRestrictionEnforcer).build();
}
static class EnforcerActorBuilder {
private final ActorSystem system;
private final ActorRef testActorRef;
private final ActorRef thingsShardRegion;
private final ActorRef policiesShardRegion;
private final ActorRef puSubMediatorRef;
@Nullable private ActorRef conciergeForwarder;
@Nullable private PreEnforcer preEnforcer;
@Nullable private CreationRestrictionEnforcer creationRestrictionEnforcer;
EnforcerActorBuilder(final ActorSystem system, final ActorRef testActorRef, final ActorRef thingsShardRegion,
final ActorRef policiesShardRegion) {
this.system = system;
this.testActorRef = testActorRef;
this.thingsShardRegion = thingsShardRegion;
this.policiesShardRegion = policiesShardRegion;
this.puSubMediatorRef = testActorRef;
}
EnforcerActorBuilder(final ActorSystem system, final ActorRef testActorRef, final ActorRef thingsShardRegion,
final ActorRef policiesShardRegion, final ActorRef puSubMediatorRef) {
this.system = system;
this.testActorRef = testActorRef;
this.thingsShardRegion = thingsShardRegion;
this.policiesShardRegion = policiesShardRegion;
this.puSubMediatorRef = puSubMediatorRef;
}
public EnforcerActorBuilder setPreEnforcer(@Nullable final PreEnforcer preEnforcer) {
this.preEnforcer = preEnforcer;
return this;
}
public EnforcerActorBuilder setConciergeForwarder(final ActorRef conciergeForwarder) {
this.conciergeForwarder = conciergeForwarder;
return this;
}
public EnforcerActorBuilder setCreationRestrictionEnforcer(@Nullable final CreationRestrictionEnforcer creationRestrictionEnforcer) {
this.creationRestrictionEnforcer = creationRestrictionEnforcer;
return this;
}
public ActorRef build() {
var conciergeForwarder = Optional.ofNullable(this.conciergeForwarder)
.orElseGet(() -> new TestProbe(system, createUniqueName()).ref());
final AskWithRetryConfig askWithRetryConfig = CACHES_CONFIG.getAskWithRetryConfig();
final PolicyEnforcerCacheLoader policyEnforcerCacheLoader =
new PolicyEnforcerCacheLoader(askWithRetryConfig, system.getScheduler(), policiesShardRegion);
final Cache<EnforcementCacheKey, Entry<PolicyEnforcer>> policyEnforcerCache =
CaffeineCache.of(Caffeine.newBuilder(), policyEnforcerCacheLoader);
final Cache<EnforcementCacheKey, Entry<Enforcer>> projectedEnforcerCache =
policyEnforcerCache.projectValues(PolicyEnforcer::project, PolicyEnforcer::embed);
final ThingEnforcementIdCacheLoader thingEnforcementIdCacheLoader =
new ThingEnforcementIdCacheLoader(askWithRetryConfig, system.getScheduler(), thingsShardRegion);
final Cache<EnforcementCacheKey, Entry<EnforcementCacheKey>> thingIdCache =
CaffeineCache.of(Caffeine.newBuilder(), thingEnforcementIdCacheLoader);
final Set<EnforcementProvider<?>> enforcementProviders = new HashSet<>();
final LiveSignalPub liveSignalPub = new DummyLiveSignalPub(puSubMediatorRef);
enforcementProviders.add(new ThingCommandEnforcement.Provider(system, thingsShardRegion,
policiesShardRegion, thingIdCache, projectedEnforcerCache, preEnforcer,
creationRestrictionEnforcer, liveSignalPub, ENFORCEMENT_CONFIG));
// enforcementProviders.add(new PolicyCommandEnforcement.Provider(policiesShardRegion, policyEnforcerCache,
// creationRestrictionEnforcer));
enforcementProviders.add(new LiveSignalEnforcement.Provider(thingIdCache,
projectedEnforcerCache,
system,
liveSignalPub,
ENFORCEMENT_CONFIG));
final Props props = EnforcerActor.props(testActorRef, enforcementProviders, conciergeForwarder, null, preEnforcer,
null, null);
return system.actorOf(props, EnforcerActor.ACTOR_NAME);
}
}
static String createUniqueName() {
return "conciergeForwarder-" + UUID.randomUUID();
}
public static ThingBuilder.FromScratch newThing() {
return ThingsModelFactory.newThingBuilder()
.setId(THING_ID)
.setRevision(1L);
}
public static JsonObject newThingWithPolicyId(final PolicyId policyId) {
return newThing()
.setPolicyId(policyId)
.build()
.toJson(V_2, FieldType.all());
}
/**
* Similar to {@link TestKit#expectMsgClass(Class)} but ignores other messages occurring while waiting for a
* message of the passed {@code clazz}.
*
* @param testKit the TestKit to fish for messages in
* @param clazz the type of the message to wait for
* @param <T> the type of the waited for message
* @return the message
*/
public static <T> T fishForMsgClass(final TestKit testKit, final Class<T> clazz) {
return clazz.cast(
testKit.fishForMessage(FiniteDuration.apply(5, TimeUnit.SECONDS), clazz.getName(), clazz::isInstance));
}
private static final class DummyLiveSignalPub implements LiveSignalPub {
private final ActorRef pubSubMediator;
private DummyLiveSignalPub(final ActorRef pubSubMediator) {
this.pubSubMediator = pubSubMediator;
}
@Override
public DistributedPub<ThingCommand<?>> command() {
return new DistributedPub<>() {
@Override
public ActorRef getPublisher() {
return pubSubMediator;
}
@Override
public Object wrapForPublication(final ThingCommand<?> message) {
return DistPubSubAccess.publish(StreamingType.LIVE_COMMANDS.getDistributedPubSubTopic(), message);
}
@Override
public <S extends ThingCommand<?>> Object wrapForPublicationWithAcks(final S message,
final AckExtractor<S> ackExtractor) {
return wrapForPublication(message);
}
};
}
@Override
public DistributedPub<ThingEvent<?>> event() {
return new DistributedPub<>() {
@Override
public ActorRef getPublisher() {
return pubSubMediator;
}
@Override
public Object wrapForPublication(final ThingEvent<?> message) {
return DistPubSubAccess.publish(StreamingType.LIVE_EVENTS.getDistributedPubSubTopic(), message);
}
@Override
public <S extends ThingEvent<?>> Object wrapForPublicationWithAcks(final S message,
final AckExtractor<S> ackExtractor) {
return wrapForPublication(message);
}
};
}
@Override
public DistributedPub<SignalWithEntityId<?>> message() {
return new DistributedPub<>() {
@Override
public ActorRef getPublisher() {
return pubSubMediator;
}
@Override
public Object wrapForPublication(final SignalWithEntityId<?> message) {
return DistPubSubAccess.publish(StreamingType.MESSAGES.getDistributedPubSubTopic(), message);
}
@Override
public <S extends SignalWithEntityId<?>> Object wrapForPublicationWithAcks(final S message,
final AckExtractor<S> ackExtractor) {
return wrapForPublication(message);
}
};
}
}
}