-
Notifications
You must be signed in to change notification settings - Fork 215
/
AbstractThingEnforcementTest.java
151 lines (130 loc) · 6.32 KB
/
AbstractThingEnforcementTest.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
/*
* 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.things.service.enforcement;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.ditto.things.service.enforcement.TestSetup.THING_ID;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.eclipse.ditto.base.model.auth.AuthorizationSubject;
import org.eclipse.ditto.internal.utils.pubsub.DistributedPub;
import org.eclipse.ditto.internal.utils.pubsub.extractors.AckExtractor;
import org.eclipse.ditto.policies.api.commands.sudo.SudoRetrievePolicy;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.signals.commands.query.RetrievePolicy;
import org.eclipse.ditto.things.api.commands.sudo.SudoRetrieveThing;
import org.eclipse.ditto.things.model.signals.commands.ThingCommand;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
import org.eclipse.ditto.things.service.persistence.actors.ThingSupervisorActor;
import org.junit.After;
import org.junit.Before;
import com.typesafe.config.ConfigFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.testkit.TestActorRef;
import akka.testkit.TestProbe;
import akka.testkit.javadsl.TestKit;
/**
* Abstract base class for all {@link org.eclipse.ditto.things.service.enforcement.ThingEnforcement} related unit tests.
*/
abstract class AbstractThingEnforcementTest {
protected ActorSystem system;
protected TestProbe pubSubMediatorProbe;
protected TestProbe thingPersistenceActorProbe;
protected TestProbe policiesShardRegionProbe;
protected ActorRef supervisor;
protected ThingSupervisorActor mockThingPersistenceSupervisor;
@Before
public void init() {
system = ActorSystem.create("test", ConfigFactory.parseMap(Map.of("akka.actor.provider",
"akka.cluster.ClusterActorRefProvider")).withFallback(ConfigFactory.load(
"test")));
pubSubMediatorProbe = createPubSubMediatorProbe();
thingPersistenceActorProbe = createThingPersistenceActorProbe();
policiesShardRegionProbe = getTestProbe(createUniqueName("policiesShardRegionProbe-"));
final TestActorRef<ThingSupervisorActor> thingPersistenceSupervisorTestActorRef =
createThingPersistenceSupervisor();
supervisor = thingPersistenceSupervisorTestActorRef;
mockThingPersistenceSupervisor = thingPersistenceSupervisorTestActorRef.underlyingActor();
}
@After
public void shutdown() {
if (system != null) {
TestKit.shutdownActorSystem(system);
}
}
private TestProbe createPubSubMediatorProbe() {
return getTestProbe(createUniqueName("pubSubMediatorProbe-"));
}
private TestProbe createThingPersistenceActorProbe() {
return getTestProbe(createUniqueName("thingPersistenceActorProbe-"));
}
private TestProbe getTestProbe(final String uniqueName) {
return new TestProbe(system, uniqueName);
}
private static String createUniqueName(final String prefix) {
return prefix + UUID.randomUUID();
}
private TestActorRef<ThingSupervisorActor> createThingPersistenceSupervisor() {
return new TestActorRef<>(system, ThingSupervisorActor.props(
pubSubMediatorProbe.ref(),
policiesShardRegionProbe.ref(),
new DistributedPub<>() {
@Override
public ActorRef getPublisher() {
return pubSubMediatorProbe.ref();
}
@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);
}
},
new TestSetup.DummyLiveSignalPub(pubSubMediatorProbe.ref()),
thingPersistenceActorProbe.ref(),
null
).withDispatcher("akka.actor.default-dispatcher"), system.guardian(), URLEncoder.encode(THING_ID.toString(), Charset.defaultCharset()));
// Actors using "stash()" require the above dispatcher to be configured, otherwise stash() and unstashAll() won't
// work like in the "normal" actor!
}
protected void expectAndAnswerSudoRetrieveThing(final Object sudoRetrieveThingResponse) {
final SudoRetrieveThing sudoRetrieveThing =
thingPersistenceActorProbe.expectMsgClass(SudoRetrieveThing.class);
assertThat((CharSequence) sudoRetrieveThing.getEntityId()).isEqualTo(THING_ID);
thingPersistenceActorProbe.reply(sudoRetrieveThingResponse);
}
protected void expectAndAnswerSudoRetrievePolicy(final PolicyId policyId, final Object sudoRetrievePolicyResponse) {
final SudoRetrievePolicy sudoRetrievePolicy =
policiesShardRegionProbe.expectMsgClass(SudoRetrievePolicy.class);
assertThat((CharSequence) sudoRetrievePolicy.getEntityId()).isEqualTo(policyId);
policiesShardRegionProbe.reply(sudoRetrievePolicyResponse);
}
protected void expectAndAnswerRetrievePolicy(final PolicyId policyId, final Object retrievePolicyResponse) {
final var retrievePolicy = policiesShardRegionProbe.expectMsgClass(RetrievePolicy.class);
assertThat((CharSequence) retrievePolicy.getEntityId()).isEqualTo(policyId);
policiesShardRegionProbe.reply(retrievePolicyResponse);
}
@SuppressWarnings("unchecked")
protected <C extends ThingCommand<?>> C addReadSubjectHeader(final C command, final AuthorizationSubject subject) {
return (C) command.setDittoHeaders(command.getDittoHeaders().toBuilder()
.readGrantedSubjects(List.of(subject))
.build()
);
}
}