-
Notifications
You must be signed in to change notification settings - Fork 214
/
MappingSinksTest.java
287 lines (260 loc) · 14.6 KB
/
MappingSinksTest.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
/*
* 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.connectivity.service.messaging;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.service.config.ThrottlingConfig;
import org.eclipse.ditto.connectivity.api.ExternalMessage;
import org.eclipse.ditto.connectivity.api.ExternalMessageFactory;
import org.eclipse.ditto.connectivity.api.MappedInboundExternalMessage;
import org.eclipse.ditto.connectivity.api.OutboundSignalFactory;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.model.ConnectivityModelFactory;
import org.eclipse.ditto.connectivity.model.MappingContext;
import org.eclipse.ditto.connectivity.service.config.mapping.DefaultMappingConfig;
import org.eclipse.ditto.connectivity.service.config.mapping.MappingConfig;
import org.eclipse.ditto.connectivity.service.messaging.mappingoutcome.MappingOutcome;
import org.eclipse.ditto.internal.utils.pekko.ActorSystemResource;
import org.eclipse.ditto.internal.utils.protocol.DittoProtocolAdapterProvider;
import org.eclipse.ditto.internal.utils.protocol.config.DefaultProtocolConfig;
import org.eclipse.ditto.internal.utils.tracing.DittoTracingInitResource;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.protocol.TopicPath;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.modify.ModifyAttribute;
import org.eclipse.ditto.things.model.signals.events.AttributeModified;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import com.typesafe.config.ConfigFactory;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.Props;
import org.apache.pekko.dispatch.MessageDispatcher;
import org.apache.pekko.stream.javadsl.Sink;
import org.apache.pekko.stream.javadsl.Source;
import org.apache.pekko.testkit.TestKit;
import scala.concurrent.duration.FiniteDuration;
/**
* Tests {@link InboundMappingSink} and its outbound equivalent.
*/
public final class MappingSinksTest {
@ClassRule
public static final DittoTracingInitResource DITTO_TRACING_INIT_RESOURCE =
DittoTracingInitResource.disableDittoTracing();
private static final String NOOP_OUTBOUND_SCRIPT = "function mapFromDittoProtocolMsg(){return null;}";
private static final String NOOP_INBOUND_SCRIPT = "function mapToDittoProtocolMsg(){return null;}";
@Rule
public final ActorSystemResource resource = ActorSystemResource.newInstance(ConfigFactory.load("test"));
@Test
public void inboundConcurrentJsMapping() {
new TestKit(resource.getActorSystem()) {{
// GIVEN:
// Incoming script sleeps for the seconds specified in the text payload,
// then reads and sets the thing name as the value of the global variable.
final int processorPoolSize = 5;
final var connection = getConnection(getRacyInboundScript(), NOOP_OUTBOUND_SCRIPT, processorPoolSize);
final var processors = IntStream.range(0, processorPoolSize)
.mapToObj(i -> getInboundMappingProcessor(connection))
.toList();
final var sink = Sink.foreach(o -> testActor().tell(o, ActorRef.noSender()));
final var underTest = InboundMappingSink.createSink(processors, connection.getId(),
processorPoolSize, sink, getMappingConfig(),
ThrottlingConfig.of(ConfigFactory.empty()),
(MessageDispatcher) resource.getActorSystem().getDispatcher());
// WHEN:
// Mapper is asked to map the text messages ["5", "4", "3", "2", "1"], which make the mapper sleep for 5 to 1s.
final var messages = IntStream.range(-processorPoolSize, 0)
.map(i -> -i)
.<Object>mapToObj(i -> {
final var string = String.valueOf(i);
final var headers = Map.of("i", string, "content-type", "text/plain");
final var message = ExternalMessageFactory.newExternalMessageBuilder(headers)
.withText(string)
.withPayloadMapping(ConnectivityModelFactory.newPayloadMapping("javascript"))
.build();
return new ExternalMessageWithSender(message, testActor());
})
.toList();
Source.from(messages).to(underTest).run(resource.getActorSystem());
// THEN:
// The results of the mapper should preserve the incoming message order "5", "4", "3", "2", "1".
// The results should not be "4", "3", "2", "1", "1" due to the global variable persisting between messages.
for (int i = processorPoolSize; i > 0; --i) {
final var outcomes = expectMsgClass(FiniteDuration.apply(30, "s"), InboundMappingOutcomes.class);
assertThat(outcomes.getOutcomes()).hasSize(1);
final int j = i;
outcomes.getOutcomes().get(0).accept(new MappingOutcome.Visitor<MappedInboundExternalMessage, Void>() {
@Override
public Void onMapped(final String mapperId, final MappedInboundExternalMessage mapped) {
final var signal = mapped.getSignal();
assertThat(signal).isInstanceOf(ModifyAttribute.class);
final var modifyAttribute = (ModifyAttribute) signal;
assertThat(modifyAttribute.getAttributeValue()).isEqualTo(JsonValue.of(String.valueOf(j)));
assertThat(modifyAttribute.getEntityId().toString()).hasToString("ns:" + j);
return null;
}
@Override
public Void onDropped(final String mapperId, @Nullable final ExternalMessage droppedMessage) {
throw new AssertionError("Not expecting dropped: " + droppedMessage);
}
@Override
public Void onError(final String mapperId, final Exception error,
@Nullable final TopicPath topicPath,
@Nullable final ExternalMessage externalMessage) {
throw new AssertionError("Not expecting error: " + externalMessage, error);
}
});
}
}};
}
@Test
public void outboundConcurrentJsMapping() {
new TestKit(resource.getActorSystem()) {{
// GIVEN:
// Outgoing script sleeps for the seconds specified in the signal entity name,
// then reads and sets the text payload as the value of the global variable.
final int processorPoolSize = 5;
final var connection = getConnection(NOOP_INBOUND_SCRIPT, getRacyOutboundScript(), processorPoolSize);
final var processors = IntStream.range(0, processorPoolSize)
.mapToObj(i -> getOutboundMappingProcessor(connection))
.toList();
final Props props = OutboundMappingProcessorActor.props(testActor(), processors, connection,
TestConstants.CONNECTIVITY_CONFIG, 3);
final ActorRef underTest = childActorOf(props);
// WHEN:
// Mapper is asked to map the events with thing names ["5", "4", "3", "2", "1"],
// which make the mapper sleep for 5 to 1s.
for (int i = processorPoolSize; i > 0; --i) {
final var string = String.valueOf(i);
final var headers = DittoHeaders.of(Map.of("i", string));
final var signal = AttributeModified.of(ThingId.of("ns:" + i),
JsonPointer.of("/attributes/i"), JsonValue.of(i), i, null, headers, null);
final var message = OutboundSignalFactory.newOutboundSignal(signal, connection.getTargets());
underTest.tell(message, testActor());
}
// THEN:
// The results of the mapper should preserve the incoming message order with text payload
// "5", "4", "3", "2", "1". The results should not have text payload "4", "3", "2", "1", "1"
// due to the global variable persisting between messages.
for (int i = processorPoolSize; i > 0; --i) {
final var publish =
expectMsgClass(FiniteDuration.apply(30, "s"), BaseClientActor.PublishMappedMessage.class);
assertThat(publish.getOutboundSignal().getMappedOutboundSignals()).hasSize(1);
final var outboundSignal = publish.getOutboundSignal().getMappedOutboundSignals().get(0);
assertThat(outboundSignal.getExternalMessage().getHeaders()).containsEntry("i", String.valueOf(i));
assertThat(outboundSignal.getExternalMessage().getTextPayload()).contains(String.valueOf(i));
}
}};
}
private static Map<String, MappingContext> getJavascriptMappings(final String inboundScript,
final String outboundScript) {
return Map.of("javascript", ConnectivityModelFactory.newMappingContext("JavaScript",
JsonObject.newBuilder()
.set("incomingScript", inboundScript)
.set("outgoingScript", outboundScript)
.build(),
Map.of(), Collections.emptyMap()));
}
private static Connection getConnection(final String inboundScript, final String outboundScript, final int poolSize) {
final var mappings = getJavascriptMappings(inboundScript, outboundScript);
final var definition = ConnectivityModelFactory.newPayloadMappingDefinition(mappings);
final var payloadMapping = ConnectivityModelFactory.newPayloadMapping("javascript");
final var source = ConnectivityModelFactory.newSourceBuilder(
TestConstants.Sources.SOURCES_WITH_AUTH_CONTEXT.get(0))
.payloadMapping(payloadMapping)
.build();
final var target = ConnectivityModelFactory.newTargetBuilder(TestConstants.Targets.TWIN_TARGET)
.payloadMapping(payloadMapping)
.build();
return TestConstants.createConnection()
.toBuilder()
.processorPoolSize(poolSize)
.payloadMappingDefinition(definition)
.setSources(List.of(source))
.setTargets(List.of(target))
.build();
}
private OutboundMappingProcessor getOutboundMappingProcessor(final Connection connection) {
final var provider = new DittoProtocolAdapterProvider(DefaultProtocolConfig.of(ConfigFactory.empty()));
final var adapter = provider.getProtocolAdapter(null);
final var logger = TestConstants.mockThreadSafeDittoLoggingAdapter();
return OutboundMappingProcessor.of(connection, TestConstants.CONNECTIVITY_CONFIG, resource.getActorSystem(),
adapter, logger);
}
private InboundMappingProcessor getInboundMappingProcessor(final Connection connection) {
final var provider = new DittoProtocolAdapterProvider(DefaultProtocolConfig.of(ConfigFactory.empty()));
final var adapter = provider.getProtocolAdapter(null);
final var logger = TestConstants.mockThreadSafeDittoLoggingAdapter();
return InboundMappingProcessor.of(connection, TestConstants.CONNECTIVITY_CONFIG, resource.getActorSystem(),
adapter, logger);
}
private static MappingConfig getMappingConfig() {
final var config = ConfigFactory.parseString(
"mapping {\n" +
" javascript {\n" +
" maxScriptSizeBytes = 50000 # 50kB\n" +
" maxScriptExecutionTime = 60s\n" +
" maxScriptStackDepth = 25\n" +
" commonJsModulePath = \"./target/test-classes/unpacked-test-webjars\"\n" +
" }\n" +
"}");
return DefaultMappingConfig.of(config);
}
private static String getRacyInboundScript() {
return "var $global = $global;\n" +
"function sleep(sec) {\n" +
" var start = new Date();\n" +
" var now = new Date();\n" +
" while(now - start < sec*1000) now = new Date();\n" +
"}\n" +
"function mapToDittoProtocolMsg(headers,textPayload,bytePayload,contentType) {\n" +
" $global = textPayload;\n" +
" sleep(parseInt(textPayload, 10));\n" +
" let namespace = \"ns\";\n" +
" let group = \"things\";\n" +
" let channel = \"twin\";\n" +
" let criterion = \"commands\";\n" +
" let action = \"modify\";\n" +
" let path = \"/attributes/i\";\n" +
" let dittoHeaders = {};\n" +
" dittoHeaders[\"correlation-id\"] = textPayload;\n" +
" let value = textPayload;\n" +
" let name = $global;\n" +
" $global = textPayload;\n" +
" return Ditto.buildDittoProtocolMsg(namespace,name,group,channel,criterion,action,path," +
" dittoHeaders,value);\n" +
"}";
}
private static String getRacyOutboundScript() {
return "var $global = $global;\n" +
"function sleep(sec) {\n" +
" var start = new Date();\n" +
" var now = new Date();\n" +
" while(now - start < sec*1000) now = new Date();\n" +
"}\n" +
"function mapFromDittoProtocolMsg(ns,i,g,ch,cr,a,p,h,value,s,e) {\n" +
" $global = i;\n" +
" sleep(parseInt(i, 10));\n" +
" let payload = $global;\n" +
" $global = i;\n" +
" return Ditto.buildExternalMsg(h, payload, null, 'text/plain');\n" +
"}";
}
}