-
Notifications
You must be signed in to change notification settings - Fork 214
/
AbstractConsumerActorTest.java
289 lines (250 loc) · 13.4 KB
/
AbstractConsumerActorTest.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
/*
* 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.connectivity.service.messaging;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.connectivity.api.OutboundSignal;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.model.ConnectionId;
import org.eclipse.ditto.connectivity.model.ConnectionSignalIdEnforcementFailedException;
import org.eclipse.ditto.connectivity.model.ConnectivityModelFactory;
import org.eclipse.ditto.connectivity.model.Enforcement;
import org.eclipse.ditto.connectivity.model.MappingContext;
import org.eclipse.ditto.connectivity.model.PayloadMapping;
import org.eclipse.ditto.connectivity.model.PayloadMappingDefinition;
import org.eclipse.ditto.connectivity.service.config.ConnectivityConfig;
import org.eclipse.ditto.connectivity.service.mapping.DittoMessageMapper;
import org.eclipse.ditto.edge.service.headers.DittoHeadersValidator;
import org.eclipse.ditto.internal.utils.pekko.logging.ThreadSafeDittoLoggingAdapter;
import org.eclipse.ditto.internal.utils.config.ScopedConfig;
import org.eclipse.ditto.internal.utils.protocol.ProtocolAdapterProvider;
import org.eclipse.ditto.internal.utils.tracing.DittoTracingInitResource;
import org.eclipse.ditto.placeholders.UnresolvedPlaceholderException;
import org.eclipse.ditto.protocol.adapter.ProtocolAdapter;
import org.eclipse.ditto.things.model.signals.commands.modify.ModifyThing;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.mockito.Mockito;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import org.apache.pekko.NotUsed;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSelection;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.actor.Props;
import org.apache.pekko.stream.javadsl.Sink;
import org.apache.pekko.testkit.TestProbe;
import org.apache.pekko.testkit.javadsl.TestKit;
import scala.concurrent.duration.FiniteDuration;
public abstract class AbstractConsumerActorTest<M> {
@ClassRule
public static final DittoTracingInitResource DITTO_TRACING_INIT_RESOURCE =
DittoTracingInitResource.disableDittoTracing();
protected static final Config CONFIG = ConfigFactory.load("test");
private static final Connection CONNECTION = TestConstants.createConnection();
private static final ConnectionId CONNECTION_ID = CONNECTION.getId();
private static final FiniteDuration ONE_SECOND = FiniteDuration.apply(1, TimeUnit.SECONDS);
protected static final Map.Entry<String, String> REPLY_TO_HEADER =
TestConstants.header("reply-to", "reply-to-address");
protected static final Enforcement ENFORCEMENT =
ConnectivityModelFactory.newEnforcement("{{ header:device_id }}", "{{ thing:id }}");
protected static ActorSystem actorSystem;
protected static ProtocolAdapterProvider protocolAdapterProvider;
protected TestProbe connectionActorProbe;
@Rule
public TestName name = new TestName();
@Before
public void init() {
actorSystem = ActorSystem.create("AkkaTestSystem", CONFIG);
protocolAdapterProvider = ProtocolAdapterProvider.load(TestConstants.PROTOCOL_CONFIG, actorSystem);
connectionActorProbe = TestProbe.apply("connectionActor", actorSystem);
}
@After
public void tearDown() {
if (actorSystem != null) {
TestConstants.disableLogging(actorSystem);
TestKit.shutdownActorSystem(actorSystem);
}
}
@Test
public void testInboundMessageSucceeds() {
testInboundMessage(TestConstants.header("device_id", TestConstants.Things.THING_ID), true, s -> {}, o -> {});
}
@Test
public void testInboundMessageWitMultipleMappingsSucceeds() {
testInboundMessage(TestConstants.header("device_id", TestConstants.Things.THING_ID), 2, 0,
s -> {}, o -> {}, ConnectivityModelFactory.newPayloadMapping("ditto", "ditto"));
}
@Test
public void testInboundMessageWithMultipleMappingsOneFailingSucceeds() {
testInboundMessage(TestConstants.header("device_id", TestConstants.Things.THING_ID), 1, 1,
s -> {}, o -> {}, ConnectivityModelFactory.newPayloadMapping("faulty", "ditto"));
}
@Test
public void testInboundMessageWithDuplicatingMapperSucceeds() {
testInboundMessage(TestConstants.header("device_id", TestConstants.Things.THING_ID), 3, 0,
s -> {}, o -> {}, ConnectivityModelFactory.newPayloadMapping("duplicator", "ditto"));
}
@Test
public void testInboundMessageFails() {
TestConstants.disableLogging(actorSystem);
testInboundMessage(TestConstants.header("device_id", "_invalid"), false, s -> {}, outboundSignal -> {
final ConnectionSignalIdEnforcementFailedException exception =
ConnectionSignalIdEnforcementFailedException.fromMessage(
outboundSignal.getExternalMessage().getTextPayload().orElse(""),
outboundSignal.getSource().getDittoHeaders());
assertThat(exception.getErrorCode()).isEqualTo(ConnectionSignalIdEnforcementFailedException.ERROR_CODE);
assertThat(exception.getDittoHeaders()).contains(REPLY_TO_HEADER);
});
}
@Test
public void testInboundMessageFailsIfHeaderIsMissing() {
TestConstants.disableLogging(actorSystem);
testInboundMessage(TestConstants.header("some", "header"), false, s -> {}, outboundSignal -> {
final UnresolvedPlaceholderException exception =
UnresolvedPlaceholderException.fromMessage(
outboundSignal.getExternalMessage().getTextPayload().orElse(""),
outboundSignal.getSource().getDittoHeaders());
assertThat(exception.getErrorCode()).isEqualTo(UnresolvedPlaceholderException.ERROR_CODE);
assertThat(exception.getDittoHeaders()).contains(REPLY_TO_HEADER);
assertThat(exception.getMessage()).contains("header:device_id");
});
}
@Test
public void testInboundMessageWithHeaderMapping() {
testHeaderMapping();
}
protected abstract void testHeaderMapping();
@Test
public void testInboundMessageWithHeaderMappingThrowsUnresolvedPlaceholderException() {
TestConstants.disableLogging(actorSystem);
testInboundMessage(TestConstants.header("useless", "header"), false,
msg -> {},
response -> {
assertThat(response.getSource().getDittoHeaders()).contains(REPLY_TO_HEADER);
assertThat(response.getExternalMessage().getTextPayload().orElse(""))
.contains("{{ header:device_id }}");
}
);
}
protected abstract Props getConsumerActorProps(final Sink<Object, NotUsed> inboundMappingSink,
final PayloadMapping payloadMapping);
protected abstract M getInboundMessage(final String payload, final Map.Entry<String, Object> header);
protected void consumeMessage(final ActorRef consumerActor, final M inboundMessage, final ActorRef sender) {
consumerActor.tell(inboundMessage, sender);
}
protected void testInboundMessage(final Map.Entry<String, Object> header,
final boolean isForwardedToConcierge,
final Consumer<Signal<?>> verifySignal,
final Consumer<OutboundSignal.Mapped> verifyResponse) {
testInboundMessage(header, isForwardedToConcierge ? 1 : 0, isForwardedToConcierge ? 0 : 1, verifySignal,
verifyResponse, ConnectivityModelFactory.emptyPayloadMapping());
}
private void testInboundMessage(final Map.Entry<String, Object> header,
final int forwardedToConcierge,
final int respondedToCaller,
final Consumer<Signal<?>> verifySignal,
final Consumer<OutboundSignal.Mapped> verifyResponse,
final PayloadMapping payloadMapping
) {
new TestKit(actorSystem) {{
final TestProbe sender = TestProbe.apply(actorSystem);
final TestProbe proxyActor = TestProbe.apply(actorSystem);
final TestProbe clientActor = TestProbe.apply(actorSystem);
final Sink<Object, NotUsed> inboundMappingSink =
setupInboundMappingSink(clientActor.ref(), proxyActor.ref());
final ActorRef underTest = actorSystem.actorOf(getConsumerActorProps(inboundMappingSink, payloadMapping));
consumeMessage(underTest, getInboundMessage(TestConstants.modifyThing(), header), sender.ref());
if (forwardedToConcierge >= 0) {
for (int i = 0; i < forwardedToConcierge; i++) {
final ModifyThing modifyThing = proxyActor.expectMsgClass(ModifyThing.class);
assertThat((CharSequence) modifyThing.getEntityId()).isEqualTo(TestConstants.Things.THING_ID);
verifySignal.accept(modifyThing);
}
} else {
proxyActor.expectNoMessage(ONE_SECOND);
}
if (respondedToCaller >= 0) {
for (int i = 0; i < respondedToCaller; i++) {
final BaseClientActor.PublishMappedMessage publishMappedMessage =
clientActor.expectMsgClass(BaseClientActor.PublishMappedMessage.class);
verifyResponse.accept(publishMappedMessage.getOutboundSignal().first());
}
} else {
clientActor.expectNoMessage(ONE_SECOND);
}
stopConsumerActor(underTest);
}};
}
protected void stopConsumerActor(final ActorRef underTest) {
// implement in subclasses if required
}
protected Sink<Object, NotUsed> setupInboundMappingSink(final ActorRef clientActor, final ActorRef proxyActor) {
final Map<String, MappingContext> mappings = new HashMap<>();
mappings.put("ditto", DittoMessageMapper.CONTEXT);
mappings.put("faulty", FaultyMessageMapper.CONTEXT);
mappings.put("duplicator", DuplicatingMessageMapper.CONTEXT);
final PayloadMappingDefinition payloadMappingDefinition =
ConnectivityModelFactory.newPayloadMappingDefinition(mappings);
final ConnectivityConfig connectivityConfig = TestConstants.CONNECTIVITY_CONFIG;
final ThreadSafeDittoLoggingAdapter logger = Mockito.mock(ThreadSafeDittoLoggingAdapter.class);
when(logger.withMdcEntry(Mockito.any(CharSequence.class), Mockito.nullable(CharSequence.class)))
.thenReturn(logger);
when(logger.withCorrelationId(Mockito.any(DittoHeaders.class)))
.thenReturn(logger);
when(logger.withCorrelationId(Mockito.nullable(CharSequence.class)))
.thenReturn(logger);
when(logger.withCorrelationId(Mockito.any(WithDittoHeaders.class)))
.thenReturn(logger);
final ProtocolAdapter protocolAdapter = protocolAdapterProvider.getProtocolAdapter(null);
final var connection = CONNECTION.toBuilder().payloadMappingDefinition(payloadMappingDefinition).build();
final InboundMappingProcessor inboundMappingProcessor =
InboundMappingProcessor.of(connection, connectivityConfig, actorSystem, protocolAdapter, logger);
final OutboundMappingProcessor outboundMappingProcessor =
OutboundMappingProcessor.of(connection, connectivityConfig, actorSystem, protocolAdapter, logger);
final Props props = OutboundMappingProcessorActor.props(clientActor, List.of(outboundMappingProcessor),
CONNECTION, connectivityConfig, 43);
final ActorRef outboundProcessorActor = actorSystem.actorOf(props,
OutboundMappingProcessorActor.ACTOR_NAME + "-" + name.getMethodName());
final var config = actorSystem.settings().config();
final Sink<Object, NotUsed> inboundDispatchingSink = InboundDispatchingSink.createSink(CONNECTION,
protocolAdapter.headerTranslator(),
ActorSelection.apply(proxyActor, ""),
connectionActorProbe.ref(),
outboundProcessorActor,
TestProbe.apply(actorSystem).ref(),
actorSystem,
ConnectivityConfig.of(config),
DittoHeadersValidator.get(actorSystem, ScopedConfig.dittoExtension(config)),
null);
return InboundMappingSink.createSink(List.of(inboundMappingProcessor),
CONNECTION_ID,
99,
inboundDispatchingSink,
connectivityConfig.getMappingConfig(),
null,
actorSystem.dispatchers().defaultGlobalDispatcher());
}
}