-
Notifications
You must be signed in to change notification settings - Fork 215
/
SmartChannelSelectionWithResponseReceiverTest.java
217 lines (189 loc) · 10.7 KB
/
SmartChannelSelectionWithResponseReceiverTest.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
/*
* 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.policies.model.SubjectIssuer.GOOGLE;
import java.util.List;
import java.util.function.Consumer;
import org.assertj.core.api.Assertions;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.auth.AuthorizationSubject;
import org.eclipse.ditto.base.model.auth.DittoAuthorizationContextType;
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.DittoHeadersBuilder;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.internal.utils.pubsub.StreamingType;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.policies.api.commands.sudo.SudoRetrievePolicyResponse;
import org.eclipse.ditto.policies.model.Permissions;
import org.eclipse.ditto.policies.model.PoliciesModelFactory;
import org.eclipse.ditto.policies.model.PoliciesResourceType;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.things.api.Permission;
import org.eclipse.ditto.things.api.commands.sudo.SudoRetrieveThingResponse;
import org.eclipse.ditto.things.model.ThingIdInvalidException;
import org.eclipse.ditto.things.model.signals.commands.ThingCommand;
import org.eclipse.ditto.things.model.signals.commands.ThingErrorResponse;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThing;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThingResponse;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import akka.actor.ActorRef;
import akka.cluster.pubsub.DistributedPubSubMediator;
import akka.testkit.javadsl.TestKit;
import scala.PartialFunction;
import scala.concurrent.duration.FiniteDuration;
/**
* Tests {@link org.eclipse.ditto.things.service.persistence.actors.ThingSupervisorActor} and its
* {@link org.eclipse.ditto.things.service.enforcement.ThingEnforcement} for commands requiring smart channel
* selection where live responses come from a different channel than where the command is published.
*/
public final class SmartChannelSelectionWithResponseReceiverTest extends AbstractThingEnforcementTest {
public static final PolicyId POLICY_ID = PolicyId.of("policy:id");
@Rule
public final TestName testName = new TestName();
private JsonObject thing;
private SudoRetrieveThingResponse sudoRetrieveThingResponse;
private SudoRetrievePolicyResponse sudoRetrievePolicyResponse;
@Before
public void init() {
super.init();
final PolicyId policyId = POLICY_ID;
thing = TestSetup.newThingWithPolicyId(policyId);
final JsonObject policy = PoliciesModelFactory.newPolicyBuilder(policyId)
.setRevision(1L)
.forLabel("authorize-self")
.setSubject(GOOGLE, TestSetup.SUBJECT_ID)
.setGrantedPermissions(PoliciesResourceType.thingResource(JsonPointer.empty()),
Permissions.newInstance(Permission.READ, Permission.WRITE))
.build()
.toJson(FieldType.all());
sudoRetrieveThingResponse = SudoRetrieveThingResponse.of(thing, DittoHeaders.empty());
sudoRetrievePolicyResponse = SudoRetrievePolicyResponse.of(policyId, policy, DittoHeaders.empty());
}
@Test
public void matchLiveChannelCondition() {
new TestKit(system) {{
final var retrieveThing = getRetrieveThing(headers -> headers.liveChannelCondition("exists(thingId)"));
supervisor.tell(retrieveThing, getRef());
expectAndAnswerSudoRetrieveThing(sudoRetrieveThingResponse);
expectAndAnswerSudoRetrievePolicy(POLICY_ID, sudoRetrievePolicyResponse);
thingPersistenceActorProbe.expectMsg(addReadSubjectHeader(retrieveThing, TestSetup.GOOGLE_SUBJECT));
thingPersistenceActorProbe.reply(getRetrieveThingResponse(retrieveThing, b -> {}));
expectLiveQueryCommandOnPubSub(retrieveThing);
supervisor.tell(getRetrieveThingResponse(retrieveThing, b -> b.channel("live")), ActorRef.noSender());
assertLiveChannel(expectMsgClass(RetrieveThingResponse.class));
}};
}
@Test
public void liveChannelErrorWithTwinFallback() {
new TestKit(system) {{
final var retrieveThing = getRetrieveThing(headers -> headers.liveChannelCondition("exists(thingId)")
.putHeader(DittoHeaderDefinition.LIVE_CHANNEL_TIMEOUT_STRATEGY.getKey(), "use-twin"));
supervisor.tell(retrieveThing, getRef());
expectAndAnswerSudoRetrieveThing(sudoRetrieveThingResponse);
expectAndAnswerSudoRetrievePolicy(POLICY_ID, sudoRetrievePolicyResponse);
thingPersistenceActorProbe.expectMsg(addReadSubjectHeader(retrieveThing, TestSetup.GOOGLE_SUBJECT));
final var twinResponse = getRetrieveThingResponse(retrieveThing, b -> {});
thingPersistenceActorProbe.reply(twinResponse);
expectLiveQueryCommandOnPubSub(retrieveThing);
supervisor.tell(ThingErrorResponse.of(retrieveThing.getEntityId(),
ThingIdInvalidException.newBuilder(retrieveThing.getEntityId())
.dittoHeaders(retrieveThing.getDittoHeaders().toBuilder().channel("live").build())
.build()), ActorRef.noSender());
final var receivedError = expectMsgClass(ThingIdInvalidException.class);
assertLiveChannel(receivedError);
}};
}
@Test
public void liveCommandErrorWithTwinFallback() {
new TestKit(system) {{
final var retrieveThing = getRetrieveThing(headers -> headers.channel("live")
.putHeader(DittoHeaderDefinition.LIVE_CHANNEL_TIMEOUT_STRATEGY.getKey(), "use-twin"));
supervisor.tell(retrieveThing, getRef());
expectAndAnswerSudoRetrieveThing(sudoRetrieveThingResponse);
expectAndAnswerSudoRetrievePolicy(POLICY_ID, sudoRetrievePolicyResponse);
RetrieveThing expectedRetrieveThing = addReadSubjectHeader(retrieveThing, TestSetup.GOOGLE_SUBJECT);
expectedRetrieveThing = expectedRetrieveThing.setDittoHeaders(expectedRetrieveThing.getDittoHeaders()
.toBuilder()
.channel(Signal.CHANNEL_TWIN)
.readRevokedSubjects(List.of())
.build());
thingPersistenceActorProbe.expectMsg(expectedRetrieveThing);
final var twinResponse = getRetrieveThingResponse(retrieveThing, b -> {});
thingPersistenceActorProbe.reply(twinResponse);
expectLiveQueryCommandOnPubSub(retrieveThing);
supervisor.tell(ThingErrorResponse.of(retrieveThing.getEntityId(),
ThingIdInvalidException.newBuilder(retrieveThing.getEntityId())
.dittoHeaders(retrieveThing.getDittoHeaders().toBuilder().channel("live").build())
.build()), ActorRef.noSender());
final var receivedError = expectMsgClass(ThingIdInvalidException.class);
assertLiveChannel(receivedError);
}};
}
private DittoHeaders headers() {
return DittoHeaders.newBuilder()
.authorizationContext(
AuthorizationContext.newInstance(DittoAuthorizationContextType.UNSPECIFIED, TestSetup.SUBJECT,
AuthorizationSubject.newInstance(String.format("%s:%s", GOOGLE, TestSetup.SUBJECT_ID))))
.correlationId(testName.getMethodName())
.schemaVersion(JsonSchemaVersion.V_2)
.build();
}
private RetrieveThing getRetrieveThing(final Consumer<DittoHeadersBuilder<?, ?>> headerModifier) {
final DittoHeadersBuilder<?, ?> builder = headers().toBuilder();
headerModifier.accept(builder);
return RetrieveThing.of(TestSetup.THING_ID, builder.build());
}
private RetrieveThingResponse getRetrieveThingResponse(final RetrieveThing retrieveThing,
final Consumer<DittoHeadersBuilder<?, ?>> headerModifier) {
final var builder = retrieveThing.getDittoHeaders()
.toBuilder()
.putHeader(DittoHeaderDefinition.LIVE_CHANNEL_CONDITION_MATCHED.getKey(), "true");
headerModifier.accept(builder);
return RetrieveThingResponse.of(retrieveThing.getEntityId(), thing, builder.build());
}
private void expectLiveQueryCommandOnPubSub(final RetrieveThing retrieveThing) {
final DistributedPubSubMediator.Publish publishLiveQueryCommand =
(DistributedPubSubMediator.Publish) pubSubMediatorProbe.fishForMessage(
FiniteDuration.apply(5, "s"),
"publish live query command",
PartialFunction.fromFunction(msg ->
msg instanceof DistributedPubSubMediator.Publish publish &&
publish.topic().equals(
StreamingType.LIVE_COMMANDS.getDistributedPubSubTopic())
)
);
assertThat(publishLiveQueryCommand.topic()).isEqualTo(StreamingType.LIVE_COMMANDS.getDistributedPubSubTopic());
assertThat(publishLiveQueryCommand.msg()).isInstanceOf(ThingCommand.class);
assertThat((CharSequence) ((ThingCommand<?>) publishLiveQueryCommand.msg()).getEntityId())
.isEqualTo(retrieveThing.getEntityId());
assertLiveChannel((ThingCommand<?>) publishLiveQueryCommand.msg());
}
private void assertLiveChannel(final Class<? extends WithDittoHeaders> clazz, final Object message) {
assertThat(message).isInstanceOf(clazz);
Assertions.assertThat(Signal.isChannelLive(clazz.cast(message)))
.describedAs("Expect live channel: " + message)
.isTrue();
}
private void assertLiveChannel(final WithDittoHeaders signal) {
assertLiveChannel(WithDittoHeaders.class, signal);
}
}