-
Notifications
You must be signed in to change notification settings - Fork 214
/
PolicySupervisorActorTest.java
147 lines (126 loc) · 6.24 KB
/
PolicySupervisorActorTest.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
/*
* 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.service.persistence.actors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.internal.utils.cluster.StopShardedActor;
import org.eclipse.ditto.internal.utils.namespaces.BlockedNamespaces;
import org.eclipse.ditto.internal.utils.persistence.mongo.streaming.MongoReadJournal;
import org.eclipse.ditto.internal.utils.pubsub.DistributedPub;
import org.eclipse.ditto.internal.utils.tracing.DittoTracingInitResource;
import org.eclipse.ditto.policies.enforcement.PolicyEnforcer;
import org.eclipse.ditto.policies.enforcement.PolicyEnforcerProvider;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.enforcers.Enforcer;
import org.eclipse.ditto.policies.model.signals.announcements.PolicyAnnouncement;
import org.eclipse.ditto.policies.model.signals.commands.exceptions.PolicyNotAccessibleException;
import org.eclipse.ditto.policies.model.signals.commands.modify.CreatePolicy;
import org.eclipse.ditto.policies.model.signals.commands.modify.CreatePolicyResponse;
import org.eclipse.ditto.policies.model.signals.commands.query.RetrievePolicy;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.apache.pekko.stream.Attributes;
import org.apache.pekko.testkit.TestProbe;
import org.apache.pekko.testkit.javadsl.TestKit;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
/**
* Tests {@link PolicySupervisorActor}.
*/
public final class PolicySupervisorActorTest extends PersistenceActorTestBase {
@ClassRule
public static final DittoTracingInitResource DITTO_TRACING_INIT_RESOURCE =
DittoTracingInitResource.disableDittoTracing();
@SuppressWarnings("unchecked")
@Mock
public DistributedPub<PolicyAnnouncement<?>> pub = mock(DistributedPub.class);
@Mock
public BlockedNamespaces blockedNamespaces = mock(BlockedNamespaces.class);
@Mock
public PolicyEnforcerProvider policyEnforcerProvider = mock(PolicyEnforcerProvider.class);
@Mock
public PolicyEnforcer policyEnforcer = mock(PolicyEnforcer.class);
@Mock
public Enforcer enforcer = mock(Enforcer.class);
@Before
public void setup() {
setUpBase();
}
@Test
public void stopNonexistentPolicy() {
new TestKit(actorSystem) {{
final PolicyId policyId = PolicyId.of("test.ns", "stopNonexistentPolicy");
final var props = PolicySupervisorActor.props(pubSubMediator, pub, blockedNamespaces,
policyEnforcerProvider, Mockito.mock(MongoReadJournal.class));
final var underTest = watch(childActorOf(props, policyId.toString()));
underTest.tell(new StopShardedActor(), getRef());
expectTerminated(underTest);
}};
}
@Test
public void stopAfterRetrievingNonexistentPolicy() {
new TestKit(actorSystem) {{
final PolicyId policyId = PolicyId.of("test.ns", "retrieveNonexistentPolicy");
final var props = PolicySupervisorActor.props(pubSubMediator, pub, blockedNamespaces,
policyEnforcerProvider, Mockito.mock(MongoReadJournal.class));
final var underTest = watch(childActorOf(props, policyId.toString()));
final var probe = TestProbe.apply(actorSystem);
final var retrievePolicy = RetrievePolicy.of(policyId, DittoHeaders.empty());
mockPolicyEnforcer(policyId);
underTest.tell(retrievePolicy, probe.ref());
underTest.tell(new StopShardedActor(), getRef());
expectTerminated(underTest);
probe.expectMsgClass(PolicyNotAccessibleException.class);
}};
}
@Test
public void stopAfterRetrievingExistingPolicy() {
actorSystem.eventStream().setLogLevel(Attributes.LogLevels$.MODULE$.Debug());
new TestKit(actorSystem) {
{
final var policy = createPolicyWithRandomId();
final var policyId = policy.getEntityId().orElseThrow();
final var props = PolicySupervisorActor.props(pubSubMediator, pub, blockedNamespaces,
policyEnforcerProvider, Mockito.mock(MongoReadJournal.class));
final var underTest = watch(childActorOf(props, policyId.toString()));
final var probe = TestProbe.apply(actorSystem);
final var createPolicy = CreatePolicy.of(policy, dittoHeadersV2);
underTest.tell(createPolicy, probe.ref());
final var response = probe.expectMsgClass(CreatePolicyResponse.class);
assertThat(response.getPolicyCreated().orElseThrow().getEntriesSet()).isEqualTo(policy.getEntriesSet());
// Tolerate some delay between policy commands
expectNoMsg();
final var retrievePolicy = RetrievePolicy.of(policyId, DittoHeaders.empty());
mockPolicyEnforcer(policyId);
underTest.tell(retrievePolicy, probe.ref());
underTest.tell(new StopShardedActor(), getRef());
expectTerminated(Duration.of(1200, ChronoUnit.SECONDS), underTest);
probe.expectMsgClass(PolicyNotAccessibleException.class);
}
};
}
private void mockPolicyEnforcer(PolicyId policyId) {
when(enforcer.hasPartialPermissions(any(), any(), any())).thenReturn(Boolean.TRUE);
when(policyEnforcerProvider.getPolicyEnforcer(policyId))
.thenReturn(CompletableFuture.completedFuture(Optional.of(policyEnforcer)));
when(policyEnforcer.getEnforcer()).thenReturn(enforcer);
}
}