-
Notifications
You must be signed in to change notification settings - Fork 214
/
MqttPublisherActorTest.java
428 lines (381 loc) · 21 KB
/
MqttPublisherActorTest.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* 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.mqtt.hivemq.publishing;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.assertj.core.api.JUnitSoftAssertions;
import org.awaitility.Awaitility;
import org.eclipse.ditto.base.model.acks.AcknowledgementLabel;
import org.eclipse.ditto.base.model.common.ByteBufferUtils;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.acks.Acknowledgements;
import org.eclipse.ditto.connectivity.api.OutboundSignalFactory;
import org.eclipse.ditto.connectivity.model.ConnectivityModelFactory;
import org.eclipse.ditto.connectivity.model.HeaderMapping;
import org.eclipse.ditto.connectivity.model.MessageSendingFailedException;
import org.eclipse.ditto.connectivity.model.Target;
import org.eclipse.ditto.connectivity.service.config.ConnectivityConfig;
import org.eclipse.ditto.connectivity.service.messaging.AbstractPublisherActorTest;
import org.eclipse.ditto.connectivity.service.messaging.ConnectivityStatusResolver;
import org.eclipse.ditto.connectivity.service.messaging.TestConstants;
import org.eclipse.ditto.connectivity.service.messaging.internal.ConnectionFailure;
import org.eclipse.ditto.connectivity.service.messaging.mqtt.MqttHeader;
import org.eclipse.ditto.connectivity.service.messaging.mqtt.hivemq.client.GenericMqttPublishResult;
import org.eclipse.ditto.connectivity.service.messaging.mqtt.hivemq.client.GenericMqttPublishingClient;
import org.eclipse.ditto.connectivity.service.messaging.mqtt.hivemq.message.publish.GenericMqttPublish;
import org.eclipse.ditto.connectivity.service.messaging.mqtt.hivemq.message.publish.UserProperty;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.things.model.ThingId;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mockito;
import com.hivemq.client.mqtt.datatypes.MqttQos;
import com.hivemq.client.mqtt.datatypes.MqttTopic;
import org.apache.pekko.actor.Props;
import org.apache.pekko.actor.Status;
import org.apache.pekko.testkit.TestProbe;
import org.apache.pekko.testkit.javadsl.TestKit;
/**
* Unit test for {@link MqttPublisherActor}.
*/
public final class MqttPublisherActorTest extends AbstractPublisherActorTest {
private static final MqttQos DEFAULT_QOS = MqttQos.AT_MOST_ONCE;
private static final boolean DEFAULT_RETAIN = false;
private static final String CUSTOM_RETAIN_HEADER = "custom.retain";
private static final String CUSTOM_TOPIC_HEADER = "custom.topic";
private static final String CUSTOM_QOS_HEADER = "custom.qos";
private static final String OUTBOUND_ADDRESS = "mqtt/eclipse/ditto";
private static final AcknowledgementLabel AUTO_ACK_LABEL = AcknowledgementLabel.of("please-verify");
@Rule
public final JUnitSoftAssertions softly = new JUnitSoftAssertions();
private final List<GenericMqttPublish> received = new ArrayList<>();
private GenericMqttPublishingClient genericMqttPublishingClient;
@Before
public void before() {
setupMocks(null);
}
@Override
protected void setupMocks(final TestProbe _probe) {
genericMqttPublishingClient = Mockito.mock(GenericMqttPublishingClient.class);
Mockito.when(genericMqttPublishingClient.publish(Mockito.any(GenericMqttPublish.class)))
.thenAnswer(invocationOnMock -> {
final GenericMqttPublish genericMqttPublish = invocationOnMock.getArgument(0);
received.add(genericMqttPublish);
return CompletableFuture.completedFuture(GenericMqttPublishResult.success(genericMqttPublish));
});
}
@Override
protected HeaderMapping getHeaderMapping() {
final var headerMapping = super.getHeaderMapping();
final Map<String, String> mappingMap = new HashMap<>(headerMapping.getMapping());
mappingMap.put(MqttHeader.MQTT_RETAIN.getName(), "{{ header:" + CUSTOM_RETAIN_HEADER + " }}");
mappingMap.put(MqttHeader.MQTT_TOPIC.getName(), "{{ header:" + CUSTOM_TOPIC_HEADER + " }}");
mappingMap.put(MqttHeader.MQTT_QOS.getName(), "{{ header:" + CUSTOM_QOS_HEADER + " }}");
return ConnectivityModelFactory.newHeaderMapping(mappingMap);
}
@Override
protected String getOutboundAddress() {
return OUTBOUND_ADDRESS;
}
@Override
protected Props getPublisherActorProps() {
return MqttPublisherActor.propsProcessing(TestConstants.createConnection(),
Mockito.mock(ConnectivityStatusResolver.class),
ConnectivityConfig.of(actorSystem.settings().config()),
genericMqttPublishingClient);
}
@Override
protected Target decorateTarget(final Target target) {
return ConnectivityModelFactory.newTarget(target,
getOutboundAddress(),
MqttQos.AT_MOST_ONCE.getCode(),
AUTO_ACK_LABEL);
}
@Override
protected void verifyPublishedMessage() {
verifyPublishedMessage(getOutboundAddress(), DEFAULT_RETAIN, DEFAULT_QOS);
}
private void verifyPublishedMessage(final String topic,
final boolean expectedRetain,
final MqttQos expectedQos,
final UserProperty... additionalExpectedUserProperties) {
Awaitility.await().until(() -> !received.isEmpty());
softly.assertThat(received)
.hasSize(1)
.first()
.satisfies(receivedGenericMqttPublish -> {
softly.assertThat(receivedGenericMqttPublish.getTopic()).as("topic").isEqualTo(MqttTopic.of(topic));
softly.assertThat(receivedGenericMqttPublish.getQos()).as("QoS").isEqualTo(expectedQos);
softly.assertThat(receivedGenericMqttPublish.isRetain()).as("retain").isEqualTo(expectedRetain);
softly.assertThat(receivedGenericMqttPublish.getPayload())
.as("payload")
.hasValue(ByteBufferUtils.fromUtf8String("payload"));
softly.assertThat(receivedGenericMqttPublish.getContentType()).as("content-type").isEmpty();
softly.assertThat(receivedGenericMqttPublish.getCorrelationData())
.as("correlation data")
.hasValue(ByteBufferUtils.fromUtf8String(TestConstants.CORRELATION_ID));
softly.assertThat(receivedGenericMqttPublish.getResponseTopic()).as("response topic").isEmpty();
softly.assertThat(receivedGenericMqttPublish.userProperties())
.as("user properties")
.containsExactlyInAnyOrder(
Stream.concat(
Stream.of(new UserProperty("device_id", DEVICE_ID),
new UserProperty("thing_id", DEVICE_ID),
new UserProperty("suffixed_thing_id", DEVICE_ID + ".some.suffix"),
new UserProperty("prefixed_thing_id", "some.prefix." + DEVICE_ID),
new UserProperty("ditto-connection-id", "hallo"),
new UserProperty("eclipse", "ditto")),
Stream.of(additionalExpectedUserProperties)
).toArray(UserProperty[]::new)
);
});
}
@Override
protected void verifyPublishedMessageToReplyTarget() {
Awaitility.await().until(() -> !received.isEmpty());
softly.assertThat(received)
.hasSize(1)
.first()
.satisfies(receivedGenericMqttPublish -> softly.assertThat(receivedGenericMqttPublish.getTopic())
.as("topic")
.isEqualTo(MqttTopic.of("replyTarget/thing:id"))
);
}
@Override
protected void verifyAcknowledgements(final Supplier<Acknowledgements> ackSupplier) {
final var acknowledgements = ackSupplier.get();
softly.assertThat(acknowledgements).as("acknowledgements size").hasSize(1);
for (final var acknowledgement : acknowledgements.getSuccessfulAcknowledgements()) {
softly.assertThat((CharSequence) acknowledgement.getLabel()).as("label").isEqualTo(AUTO_ACK_LABEL);
softly.assertThat(acknowledgement.getHttpStatus()).as("HTTP status").isEqualTo(HttpStatus.OK);
}
}
@Test
public void publishMessageWithCustomRetainFlag() {
final var customRetainFlat = true;
final var testKit = new TestKit(actorSystem);
final var underTest = testKit.childActorOf(getPublisherActorProps());
publisherCreated(testKit, underTest);
underTest.tell(
OutboundSignalFactory.newMultiMappedOutboundSignal(
List.of(getMockOutboundSignal(CUSTOM_RETAIN_HEADER, String.valueOf(customRetainFlat))),
testKit.getRef()
),
testKit.getRef()
);
verifyPublishedMessage(getOutboundAddress(),
customRetainFlat,
DEFAULT_QOS,
new UserProperty(MqttHeader.MQTT_RETAIN.getName(), String.valueOf(true)));
}
@Test
public void publishMessageWithCustomQos() {
final var customMqttQos = MqttQos.EXACTLY_ONCE;
final var testKit = new TestKit(actorSystem);
final var underTest = testKit.childActorOf(getPublisherActorProps());
publisherCreated(testKit, underTest);
underTest.tell(
OutboundSignalFactory.newMultiMappedOutboundSignal(
List.of(getMockOutboundSignal(CUSTOM_QOS_HEADER, String.valueOf(customMqttQos.getCode()))),
testKit.getRef()
),
testKit.getRef()
);
verifyPublishedMessage(getOutboundAddress(),
DEFAULT_RETAIN,
customMqttQos,
new UserProperty(MqttHeader.MQTT_QOS.getName(), String.valueOf(customMqttQos.getCode())));
}
@Test
public void publishMessageWithCustomQosInTarget() {
final var customMqttQos = MqttQos.EXACTLY_ONCE;
final var testKit = new TestKit(actorSystem);
final var underTest = testKit.childActorOf(getPublisherActorProps());
publisherCreated(testKit, underTest);
underTest.tell(
OutboundSignalFactory.newMultiMappedOutboundSignal(
List.of(getMockOutboundSignal(
ConnectivityModelFactory.newTargetBuilder(decorateTarget(createTestTarget()))
.qos(customMqttQos.getCode())
.build()
)),
testKit.getRef()
),
testKit.getRef()
);
verifyPublishedMessage(getOutboundAddress(), DEFAULT_RETAIN, customMqttQos);
}
@Test
public void publishMessageWithCustomTopic() {
final var customTopic = "my/custom/topic";
final var testKit = new TestKit(actorSystem);
final var underTest = testKit.childActorOf(getPublisherActorProps());
publisherCreated(testKit, underTest);
underTest.tell(
OutboundSignalFactory.newMultiMappedOutboundSignal(
List.of(getMockOutboundSignal(CUSTOM_TOPIC_HEADER, customTopic)),
testKit.getRef()
),
testKit.getRef()
);
verifyPublishedMessage(customTopic,
DEFAULT_RETAIN,
DEFAULT_QOS,
new UserProperty(MqttHeader.MQTT_TOPIC.getName(), customTopic));
}
@Test
public void sendPublishToBrokerFailureTriggersNegativeAcknowledgementIfAutoAckTargetPresent() {
final var illegalStateException = new IllegalStateException("This is totally expected.");
final var messageSendingFailedException = MessageSendingFailedException.newBuilder()
.cause(illegalStateException)
.build();
Mockito.when(genericMqttPublishingClient.publish(Mockito.any(GenericMqttPublish.class)))
.thenAnswer(invocationOnMock -> {
final GenericMqttPublish genericMqttPublish = invocationOnMock.getArgument(0);
received.add(genericMqttPublish);
return CompletableFuture.completedFuture(GenericMqttPublishResult.failure(genericMqttPublish,
illegalStateException));
});
final var testKit = new TestKit(actorSystem);
final var underTest = testKit.childActorOf(getPublisherActorProps());
final var outboundSignal = OutboundSignalFactory.newMultiMappedOutboundSignal(
List.of(getMockOutboundSignalWithAutoAck(AUTO_ACK_LABEL,
DittoHeaderDefinition.DITTO_ACKREGATOR_ADDRESS.getKey(), testKit.getRef().path().toSerializationFormat()
)),
testKit.getRef()
);
publisherCreated(testKit, underTest);
underTest.tell(outboundSignal, testKit.getRef());
softly.assertThat(testKit.expectMsgClass(ConnectionFailure.class))
.as("connection failure")
.satisfies(connectionFailure -> {
softly.assertThat(connectionFailure.getFailure())
.as("failure")
.isEqualTo(new Status.Failure(messageSendingFailedException));
softly.assertThat(connectionFailure.getStatus()).as("status").isEmpty();
});
softly.assertThat(testKit.expectMsgClass(Acknowledgements.class))
.as("acknowledgements")
.satisfies(acknowledgements -> assertThat(acknowledgements)
.hasSize(1)
.first()
.satisfies(acknowledgement -> {
softly.assertThat((CharSequence) acknowledgement.getLabel())
.as("label")
.isEqualTo(AUTO_ACK_LABEL);
softly.assertThat((CharSequence) acknowledgement.getEntityId())
.as("entity ID")
.isEqualTo(ThingId.of(DEVICE_ID));
softly.assertThat(acknowledgement.getHttpStatus())
.as("HTTP status")
.isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
softly.assertThat(acknowledgement.getDittoHeaders())
.as("DittoHeaders")
.containsEntry(DittoHeaderDefinition.CORRELATION_ID.getKey(), "cid")
.containsEntry("device_id", DEVICE_ID)
.containsEntry(DittoHeaderDefinition.RESPONSE_REQUIRED.getKey(), "false");
softly.assertThat(acknowledgement.getEntity(JsonSchemaVersion.LATEST)
.map(JsonValue::toString))
.as("entity")
.hasValueSatisfying(entityJsonString -> softly.assertThat(entityJsonString)
.contains(illegalStateException.getMessage()));
}));
}
@Test
public void publishOutboundSignalWithInvalidMqttTopicInHeadersYieldsNegativeAcknowledgement() {
final var invalidMqttTopic = "target/#";
final var testKit = new TestKit(actorSystem);
final var underTest = testKit.childActorOf(getPublisherActorProps());
publisherCreated(testKit, underTest);
final var outboundSignal = OutboundSignalFactory.newMultiMappedOutboundSignal(
List.of(getMockOutboundSignalWithAutoAck(AUTO_ACK_LABEL,
"custom.topic", invalidMqttTopic,
DittoHeaderDefinition.DITTO_ACKREGATOR_ADDRESS.getKey(), testKit.getRef().path().toSerializationFormat()
)),
testKit.getRef()
);
underTest.tell(outboundSignal, testKit.getRef());
softly.assertThat(testKit.expectMsgClass(Acknowledgements.class))
.satisfies(acknowledgements -> softly.assertThat(acknowledgements)
.as("acknowledgements")
.hasSize(1)
.first()
.satisfies(acknowledgement -> {
softly.assertThat((CharSequence) acknowledgement.getLabel())
.as("label")
.isEqualTo(AUTO_ACK_LABEL);
softly.assertThat((CharSequence) acknowledgement.getEntityId())
.as("entity ID")
.isEqualTo(ThingId.of(DEVICE_ID));
softly.assertThat(acknowledgement.getHttpStatus())
.as("HTTP status")
.isEqualTo(HttpStatus.BAD_REQUEST);
softly.assertThat(acknowledgement.getDittoHeaders())
.as("DittoHeaders")
.containsEntry(DittoHeaderDefinition.CORRELATION_ID.getKey(), "cid")
.containsEntry("device_id", DEVICE_ID)
.containsEntry(DittoHeaderDefinition.RESPONSE_REQUIRED.getKey(), "false");
softly.assertThat(acknowledgement.getEntity(JsonSchemaVersion.LATEST))
.as("entity")
.hasValue(JsonObject.newBuilder()
.set("error", "connectivity:message.sending.failed")
.set("message", """
Failed to send message: Failed to transform ExternalMessage to \
GenericMqttPublish: Topic [target/#] must not contain multi level \
wildcard (#), found at index 7.\
""")
.set("description", """
Sending the message to an external system failed, please check if \
your connection is configured properly and the target system is \
available and consuming messages.\
""")
.build());
}));
}
@Test
public void allOutgoingSignalsAreDroppedIfOperatingInDryRunMode() {
final var testKit = new TestKit(actorSystem);
final var outboundSignals = List.of(
OutboundSignalFactory.newMultiMappedOutboundSignal(
List.of(getMockOutboundSignalWithAutoAck(AUTO_ACK_LABEL)),
testKit.getRef()
),
OutboundSignalFactory.newMultiMappedOutboundSignal(
List.of(getMockOutboundSignal(CUSTOM_TOPIC_HEADER, "my/custom/topic")),
testKit.getRef()
),
OutboundSignalFactory.newMultiMappedOutboundSignal(
List.of(getMockOutboundSignal(CUSTOM_QOS_HEADER,
String.valueOf(MqttQos.EXACTLY_ONCE.getCode()))),
testKit.getRef()
)
);
final var underTest = testKit.childActorOf(MqttPublisherActor.propsDryRun(TestConstants.createConnection(),
Mockito.mock(ConnectivityStatusResolver.class),
ConnectivityConfig.of(actorSystem.settings().config()),
genericMqttPublishingClient));
outboundSignals.forEach(outboundSignal -> underTest.tell(outboundSignal, testKit.getRef()));
testKit.expectNoMessage();
Mockito.verifyNoInteractions(genericMqttPublishingClient);
}
}