-
Notifications
You must be signed in to change notification settings - Fork 214
/
MqttConsumerActorTest.java
578 lines (499 loc) · 26.8 KB
/
MqttConsumerActorTest.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
* 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.consuming;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;
import org.assertj.core.api.AutoCloseableSoftAssertions;
import org.assertj.core.data.Offset;
import org.eclipse.ditto.base.model.acks.DittoAcknowledgementLabel;
import org.eclipse.ditto.base.model.common.ByteBufferUtils;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.signals.acks.Acknowledgement;
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.model.Connection;
import org.eclipse.ditto.connectivity.model.ConnectionId;
import org.eclipse.ditto.connectivity.model.ConnectionType;
import org.eclipse.ditto.connectivity.model.ConnectivityStatus;
import org.eclipse.ditto.connectivity.model.ResourceStatus;
import org.eclipse.ditto.connectivity.service.config.ConnectionConfig;
import org.eclipse.ditto.connectivity.service.config.ConnectivityConfig;
import org.eclipse.ditto.connectivity.service.config.MqttConfig;
import org.eclipse.ditto.connectivity.service.messaging.ConnectivityStatusResolver;
import org.eclipse.ditto.connectivity.service.messaging.ExternalMessageWithSender;
import org.eclipse.ditto.connectivity.service.messaging.ResponseCollectorActor;
import org.eclipse.ditto.connectivity.service.messaging.TestConstants;
import org.eclipse.ditto.connectivity.service.messaging.internal.RetrieveAddressStatus;
import org.eclipse.ditto.connectivity.service.messaging.mqtt.hivemq.message.publish.GenericMqttPublish;
import org.eclipse.ditto.internal.utils.pekko.ActorSystemResource;
import org.eclipse.ditto.internal.utils.tracing.DittoTracingInitResource;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotAccessibleException;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import com.hivemq.client.mqtt.datatypes.MqttQos;
import com.hivemq.client.mqtt.datatypes.MqttTopic;
import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5Publish;
import org.apache.pekko.NotUsed;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.stream.javadsl.Sink;
import org.apache.pekko.stream.javadsl.Source;
/**
* Unit test for {@link MqttConsumerActor}.
*/
@RunWith(MockitoJUnitRunner.class)
public final class MqttConsumerActorTest {
@ClassRule
public static final DittoTracingInitResource DITTO_TRACING_INIT_RESOURCE =
DittoTracingInitResource.disableDittoTracing();
@ClassRule
public static final ActorSystemResource ACTOR_SYSTEM_RESOURCE = ActorSystemResource.newInstance();
private static final ConnectionId CONNECTION_ID = ConnectionId.generateRandom();
private static final Set<String> SOURCE_ADDRESSES = Set.of("source/telemetry", "source/status");
private static final String SOURCE_ADDRESSES_JOINED = String.join(";", SOURCE_ADDRESSES);
private static final Mqtt5Publish MQTT_5_PUBLISH = Mqtt5Publish.builder()
.topic(MqttTopic.of("source/status"))
.qos(MqttQos.AT_LEAST_ONCE)
.retain(true)
.payload(ByteBufferUtils.fromUtf8String("""
{
"topic": "org.eclipse.ditto/fancy-thing/things/twin/commands/modify",
"headers": {
"correlation-id": "<command-correlation-id>"
},
"path": "/attributes",
"value": {
"location": {
"latitude": 44.673856,
"longitude": 8.261719
}
}
}\
"""))
.build();
@Rule
public final TestName testName = new TestName();
@Mock private Connection connection;
@Mock private Sink<Object, NotUsed> inboundMappingSink;
@Mock private org.eclipse.ditto.connectivity.model.Source connectionSource;
@Mock private ConnectivityStatusResolver connectivityStatusResolver;
@Before
public void before() {
Mockito.when(connection.getId()).thenReturn(CONNECTION_ID);
Mockito.when(connection.getConnectionType()).thenReturn(ConnectionType.MQTT_5);
Mockito.when(connectionSource.getAddresses()).thenReturn(SOURCE_ADDRESSES);
}
@Test
public void propsProcessingWithNullConnectionThrowsException() {
assertThatNullPointerException()
.isThrownBy(() -> MqttConsumerActor.propsProcessing(null,
inboundMappingSink,
connectionSource,
connectivityStatusResolver,
TestConstants.CONNECTIVITY_CONFIG,
Mockito.mock(org.apache.pekko.stream.javadsl.Source.class)))
.withMessage("The connection must not be null!")
.withNoCause();
}
@Test
public void propsProcessingWithNullInboundMappingSinkThrowsException() {
assertThatNullPointerException()
.isThrownBy(() -> MqttConsumerActor.propsProcessing(connection,
null,
connectionSource,
connectivityStatusResolver,
TestConstants.CONNECTIVITY_CONFIG,
Mockito.mock(org.apache.pekko.stream.javadsl.Source.class)))
.withMessage("The inboundMappingSink must not be null!")
.withNoCause();
}
@Test
public void propsProcessingWithNullConnectionSourceThrowsException() {
assertThatNullPointerException()
.isThrownBy(() -> MqttConsumerActor.propsProcessing(connection,
inboundMappingSink,
null,
connectivityStatusResolver,
TestConstants.CONNECTIVITY_CONFIG,
Mockito.mock(org.apache.pekko.stream.javadsl.Source.class)))
.withMessage("The connectionSource must not be null!")
.withNoCause();
}
@Test
public void propsProcessingWithNullConnectivityStatusResolverThrowsException() {
assertThatNullPointerException()
.isThrownBy(() -> MqttConsumerActor.propsProcessing(connection,
inboundMappingSink,
connectionSource,
null,
TestConstants.CONNECTIVITY_CONFIG,
Mockito.mock(org.apache.pekko.stream.javadsl.Source.class)))
.withMessage("The connectivityStatusResolver must not be null!")
.withNoCause();
}
@Test
public void propsProcessingWithNullConnectivityConfigThrowsException() {
assertThatNullPointerException()
.isThrownBy(() -> MqttConsumerActor.propsProcessing(connection,
inboundMappingSink,
connectionSource,
connectivityStatusResolver,
null,
Mockito.mock(org.apache.pekko.stream.javadsl.Source.class)))
.withMessage("The connectivityConfig must not be null!")
.withNoCause();
}
@Test
public void propsProcessingWithNullMqttPublishSourceThrowsException() {
assertThatNullPointerException()
.isThrownBy(() -> MqttConsumerActor.propsProcessing(connection,
inboundMappingSink,
connectionSource,
connectivityStatusResolver,
TestConstants.CONNECTIVITY_CONFIG,
null))
.withMessage("The mqttPublishSource must not be null!")
.withNoCause();
}
@Test
public void sendRetrieveAddressStatusToMqttConsumerActorReturnsCurrentSourceStatus() {
final var underTest = ACTOR_SYSTEM_RESOURCE.newActor(
MqttConsumerActor.propsProcessing(connection,
Sink.onComplete(doneTry -> {}),
connectionSource,
connectivityStatusResolver,
TestConstants.CONNECTIVITY_CONFIG,
Source.repeat(GenericMqttPublish.ofMqtt5Publish(MQTT_5_PUBLISH))),
testName.getMethodName()
);
final var testKit = ACTOR_SYSTEM_RESOURCE.newTestKit();
underTest.tell(RetrieveAddressStatus.getInstance(), testKit.getRef());
final var resourceStatus = testKit.expectMsgClass(ResourceStatus.class);
try (final var softly = new AutoCloseableSoftAssertions()) {
softly.assertThat((CharSequence) resourceStatus.getResourceType())
.as("resource type")
.isEqualTo(ResourceStatus.ResourceType.SOURCE);
softly.assertThat((CharSequence) resourceStatus.getStatus())
.as("connectivity status")
.isEqualTo(ConnectivityStatus.OPEN);
softly.assertThat(resourceStatus.getAddress()).as("address").hasValue(SOURCE_ADDRESSES_JOINED);
softly.assertThat(resourceStatus.getRecoveryStatus()).as("recovery status").isEmpty();
softly.assertThat(resourceStatus.getStatusDetails()).as("status details").hasValue("Consumer started.");
}
ACTOR_SYSTEM_RESOURCE.stopActor(underTest);
}
@Test
public void sendGracefulStopShutsDownProcessingMqttConsumerActor() {
final var underTestWatcher = ACTOR_SYSTEM_RESOURCE.newTestKit();
final var underTest = underTestWatcher.watch(ACTOR_SYSTEM_RESOURCE.newActor(
MqttConsumerActor.propsProcessing(connection,
Sink.onComplete(doneTry -> {}),
connectionSource,
connectivityStatusResolver,
TestConstants.CONNECTIVITY_CONFIG,
Source.repeat(GenericMqttPublish.ofMqtt5Publish(MQTT_5_PUBLISH))),
testName.getMethodName()
));
underTest.tell(GracefulStop.INSTANCE, ActorRef.noSender());
underTestWatcher.expectTerminated(underTest);
}
@SuppressWarnings("unchecked")
@Test
public void mqttPublishSourceGetsThrottledIfThrottlingIsEnabled() {
final var throttlingConfig = Mockito.mock(ThrottlingConfig.class);
Mockito.when(throttlingConfig.isEnabled()).thenReturn(true);
Mockito.when(throttlingConfig.getLimit()).thenReturn(5);
Mockito.when(throttlingConfig.getInterval()).thenReturn(Duration.ofMillis(500));
final var connectivityConfig = getConnectivityConfigWithCustomThrottlingConfig(throttlingConfig);
final var mqttPublishSource = Mockito.mock(Source.class);
Mockito.when(mqttPublishSource.throttle(Mockito.anyInt(), Mockito.any(Duration.class)))
.thenReturn(Source.empty());
ACTOR_SYSTEM_RESOURCE.newActor(
MqttConsumerActor.propsProcessing(connection,
Sink.onComplete(doneTry -> {}),
connectionSource,
connectivityStatusResolver,
connectivityConfig,
mqttPublishSource),
testName.getMethodName()
);
Mockito.verify(mqttPublishSource, Mockito.after(500L))
.throttle(throttlingConfig.getLimit(), throttlingConfig.getInterval());
}
private static ConnectivityConfig getConnectivityConfigWithCustomThrottlingConfig(
final ThrottlingConfig throttlingConfig
) {
final var mqttConfig = Mockito.mock(MqttConfig.class);
Mockito.when(mqttConfig.getConsumerThrottlingConfig()).thenReturn(throttlingConfig);
return getConnectivityConfigWithCustomMqttConfig(mqttConfig);
}
private static ConnectivityConfig getConnectivityConfigWithCustomMqttConfig(final MqttConfig mqttConfig) {
final var connectionConfig = Mockito.mock(ConnectionConfig.class);
Mockito.when(connectionConfig.getMqttConfig()).thenReturn(mqttConfig);
final var connectivityConfig = Mockito.mock(ConnectivityConfig.class);
Mockito.when(connectivityConfig.getConnectionConfig()).thenReturn(connectionConfig);
Mockito.when(connectivityConfig.getMonitoringConfig()).thenReturn(TestConstants.MONITORING_CONFIG);
Mockito.when(connectivityConfig.getAcknowledgementConfig())
.thenReturn(TestConstants.CONNECTIVITY_CONFIG.getAcknowledgementConfig());
return connectivityConfig;
}
@Test
public void mqttPublishSourceThrottlingWorksAsExpected() throws InterruptedException {
final var processedMessagesCount = new LongAdder();
final var inboundMappingSink = Sink.foreach(msg -> processedMessagesCount.increment());
final var throttlingLimitPerInterval = 5;
final var throttlingInterval = Duration.ofSeconds(1L);
final var throttlingConfig = Mockito.mock(ThrottlingConfig.class);
Mockito.when(throttlingConfig.isEnabled()).thenReturn(true);
Mockito.when(throttlingConfig.getLimit()).thenReturn(throttlingLimitPerInterval);
Mockito.when(throttlingConfig.getInterval()).thenReturn(throttlingInterval);
final var connectivityConfig = getConnectivityConfigWithCustomThrottlingConfig(throttlingConfig);
final var runtimeIntervalAmount = 2;
ACTOR_SYSTEM_RESOURCE.newActor(
MqttConsumerActor.propsProcessing(connection,
inboundMappingSink,
connectionSource,
connectivityStatusResolver,
connectivityConfig,
Source.repeat(GenericMqttPublish.ofMqtt5Publish(MQTT_5_PUBLISH))),
testName.getMethodName()
);
// Wait some time
Thread.sleep(throttlingInterval.toMillis() * runtimeIntervalAmount);
// Assert that the expected amount of messages was processed during the waited time
assertThat(processedMessagesCount.sum())
.isCloseTo(throttlingLimitPerInterval * runtimeIntervalAmount, Offset.offset(1L));
}
@Test
public void mqttPublishesAreDroppedIfConsumerActorOperatesInDryRunMode() throws InterruptedException {
final var receivedMqttPublishMessages = new ArrayList<>();
final var underTest = ACTOR_SYSTEM_RESOURCE.newActor(
MqttConsumerActor.propsDryRun(connection,
Sink.foreach(receivedMqttPublishMessages::add),
connectionSource,
connectivityStatusResolver,
TestConstants.CONNECTIVITY_CONFIG,
Source.repeat(Mockito.mock(GenericMqttPublish.class)).take(100)),
testName.getMethodName()
);
// Wait some tome to make sure there was enough time to potentially process messages (which should not happen!)
Thread.sleep(1_000L);
// 'inbound mapping sink' should receive nothing because should have been dropped.
assertThat(receivedMqttPublishMessages).isEmpty();
}
@Test
public void mqttPublishesAreMappedToExternalMessagesAndSentToInboundMappingSink() throws InterruptedException {
final var genericMqttPublish = GenericMqttPublish.ofMqtt5Publish(MQTT_5_PUBLISH);
final var mqttPublishTransformer =
MqttPublishToExternalMessageTransformer.newInstance(SOURCE_ADDRESSES_JOINED, connectionSource);
final var transformationResult = mqttPublishTransformer.transform(genericMqttPublish);
final var amountMqttPublishes = 100;
final var countDownLatch = new CountDownLatch(amountMqttPublishes);
final var externalMessages = new HashSet<ExternalMessage>(amountMqttPublishes);
final var inboundMappingSink = Sink.foreach(o -> {
if (o instanceof ExternalMessageWithSender externalMessageWithSender) {
externalMessages.add(externalMessageWithSender.externalMessage());
}
countDownLatch.countDown();
});
ACTOR_SYSTEM_RESOURCE.newActor(
MqttConsumerActor.propsProcessing(connection,
inboundMappingSink,
connectionSource,
connectivityStatusResolver,
TestConstants.CONNECTIVITY_CONFIG,
Source.repeat(genericMqttPublish).take(amountMqttPublishes)),
testName.getMethodName()
);
assertThat(countDownLatch.await(3_000L, TimeUnit.MILLISECONDS)).isTrue();
assertThat(externalMessages).containsExactly(
ExternalMessageFactory.newExternalMessageBuilder(transformationResult.getSuccessValueOrThrow())
.withSource(connectionSource) // Imitates BaseConsumerActor.addSourceAndReplyTarget().
.build()
);
}
@Test
public void successfullyAcknowledgeMqttPublish() {
final var mqtt5Publish = Mockito.spy(MQTT_5_PUBLISH);
Mockito.when(mqtt5Publish.isRetain()).thenReturn(false);
final var inboundMappingSinkElementReceiver = ACTOR_SYSTEM_RESOURCE.newTestKit();
final var onCompleteMessage = new Object();
//When
ACTOR_SYSTEM_RESOURCE.newActor(
MqttConsumerActor.propsProcessing(connection,
Sink.actorRef(inboundMappingSinkElementReceiver.getRef(), onCompleteMessage),
connectionSource,
connectivityStatusResolver,
TestConstants.CONNECTIVITY_CONFIG,
Source.single(GenericMqttPublish.ofMqtt5Publish(mqtt5Publish))),
testName.getMethodName()
);
//Then
final var externalMessageWithSender =
inboundMappingSinkElementReceiver.expectMsgClass(ExternalMessageWithSender.class);
final var responseCollector = externalMessageWithSender.sender();
responseCollector.tell(ResponseCollectorActor.setCount(1), ActorRef.noSender());
responseCollector.tell(
Acknowledgement.of(DittoAcknowledgementLabel.TWIN_PERSISTED,
ThingId.of("org.eclipse.ditto", "fancy-thing"),
HttpStatus.NO_CONTENT,
DittoHeaders.newBuilder().correlationId("<command-correlation-id>").build()),
ActorRef.noSender()
);
inboundMappingSinkElementReceiver.expectMsg(onCompleteMessage);
Mockito.verify(mqtt5Publish, Mockito.timeout(1_000L)).acknowledge();
}
@Test
public void reconnectConsumerClientForRedeliveryIfInboundMessageIsRejected() {
final var mqtt5Publish = Mockito.spy(MQTT_5_PUBLISH);
Mockito.when(mqtt5Publish.isRetain()).thenReturn(false);
final var inboundMappingSinkElementReceiver = ACTOR_SYSTEM_RESOURCE.newTestKit();
final var onCompleteMessage = new Object();
final var fakeMqttClientActor = ACTOR_SYSTEM_RESOURCE.newTestKit();
fakeMqttClientActor.childActorOf(
MqttConsumerActor.propsProcessing(connection,
Sink.actorRef(inboundMappingSinkElementReceiver.getRef(), onCompleteMessage),
connectionSource,
connectivityStatusResolver,
TestConstants.CONNECTIVITY_CONFIG,
Source.single(GenericMqttPublish.ofMqtt5Publish(mqtt5Publish))),
testName.getMethodName()
);
final var externalMessageWithSender =
inboundMappingSinkElementReceiver.expectMsgClass(ExternalMessageWithSender.class);
final var responseCollector = externalMessageWithSender.sender();
responseCollector.tell(ResponseCollectorActor.setCount(1), ActorRef.noSender());
responseCollector.tell(
Acknowledgement.of(DittoAcknowledgementLabel.TWIN_PERSISTED,
ThingId.of("org.eclipse.ditto", "fancy-thing"),
HttpStatus.INTERNAL_SERVER_ERROR,
DittoHeaders.newBuilder().correlationId("<command-correlation-id>").build()),
ActorRef.noSender()
);
fakeMqttClientActor.expectMsgClass(ReconnectConsumerClient.class);
inboundMappingSinkElementReceiver.expectMsg(onCompleteMessage);
Mockito.verify(mqtt5Publish, Mockito.never()).acknowledge();
}
@Test
public void doNotRedeliverRejectedMessageIfShouldDeliverButReconnectForRedeliveryIsDisabled() {
final var mqttConfig = Mockito.mock(MqttConfig.class);
Mockito.when(mqttConfig.shouldReconnectForRedelivery()).thenReturn(false);
Mockito.when(mqttConfig.getConsumerThrottlingConfig()).thenReturn(Mockito.mock(ThrottlingConfig.class));
final var connectivityConfig = getConnectivityConfigWithCustomMqttConfig(mqttConfig);
final var mqtt5Publish = Mockito.spy(MQTT_5_PUBLISH);
Mockito.when(mqtt5Publish.isRetain()).thenReturn(false);
final var inboundMappingSinkElementReceiver = ACTOR_SYSTEM_RESOURCE.newTestKit();
final var onCompleteMessage = new Object();
final var fakeMqttClientActor = ACTOR_SYSTEM_RESOURCE.newTestKit();
fakeMqttClientActor.childActorOf(
MqttConsumerActor.propsProcessing(connection,
Sink.actorRef(inboundMappingSinkElementReceiver.getRef(), onCompleteMessage),
connectionSource,
connectivityStatusResolver,
connectivityConfig,
Source.single(GenericMqttPublish.ofMqtt5Publish(mqtt5Publish))),
testName.getMethodName()
);
final var externalMessageWithSender =
inboundMappingSinkElementReceiver.expectMsgClass(ExternalMessageWithSender.class);
final var responseCollector = externalMessageWithSender.sender();
responseCollector.tell(ResponseCollectorActor.setCount(1), ActorRef.noSender());
responseCollector.tell(
Acknowledgement.of(DittoAcknowledgementLabel.TWIN_PERSISTED,
ThingId.of("org.eclipse.ditto", "fancy-thing"),
HttpStatus.INTERNAL_SERVER_ERROR,
DittoHeaders.newBuilder().correlationId("<command-correlation-id>").build()),
ActorRef.noSender()
);
fakeMqttClientActor.expectNoMessage();
inboundMappingSinkElementReceiver.expectMsg(onCompleteMessage);
Mockito.verify(mqtt5Publish).acknowledge();
}
@Test
public void acknowledgeRejectedIncomingMessageIfShouldNotRedeliver() {
final var mqtt5Publish = Mockito.spy(MQTT_5_PUBLISH);
Mockito.when(mqtt5Publish.isRetain()).thenReturn(false);
final var inboundMappingSinkElementReceiver = ACTOR_SYSTEM_RESOURCE.newTestKit();
final var onCompleteMessage = new Object();
final var fakeMqttClientActor = ACTOR_SYSTEM_RESOURCE.newTestKit();
fakeMqttClientActor.childActorOf(
MqttConsumerActor.propsProcessing(connection,
Sink.actorRef(inboundMappingSinkElementReceiver.getRef(), onCompleteMessage),
connectionSource,
connectivityStatusResolver,
TestConstants.CONNECTIVITY_CONFIG,
Source.single(GenericMqttPublish.ofMqtt5Publish(mqtt5Publish))),
testName.getMethodName()
);
final var externalMessageWithSender =
inboundMappingSinkElementReceiver.expectMsgClass(ExternalMessageWithSender.class);
final var responseCollector = externalMessageWithSender.sender();
responseCollector.tell(ResponseCollectorActor.setCount(1), ActorRef.noSender());
responseCollector.tell(
Acknowledgement.of(DittoAcknowledgementLabel.TWIN_PERSISTED,
ThingId.of("org.eclipse.ditto", "fancy-thing"),
HttpStatus.BAD_REQUEST,
DittoHeaders.newBuilder().correlationId("<command-correlation-id>").build()),
ActorRef.noSender()
);
fakeMqttClientActor.expectNoMessage();
inboundMappingSinkElementReceiver.expectMsg(onCompleteMessage);
Mockito.verify(mqtt5Publish).acknowledge();
}
@Test
public void rejectIncomingMessageDueToDittoRuntimeExceptionInsteadOfAcknowledgement() {
final var mqtt5Publish = Mockito.spy(MQTT_5_PUBLISH);
Mockito.when(mqtt5Publish.isRetain()).thenReturn(false);
final var inboundMappingSinkElementReceiver = ACTOR_SYSTEM_RESOURCE.newTestKit();
final var onCompleteMessage = new Object();
final var fakeMqttClientActor = ACTOR_SYSTEM_RESOURCE.newTestKit();
fakeMqttClientActor.childActorOf(
MqttConsumerActor.propsProcessing(connection,
Sink.actorRef(inboundMappingSinkElementReceiver.getRef(), onCompleteMessage),
connectionSource,
connectivityStatusResolver,
TestConstants.CONNECTIVITY_CONFIG,
Source.single(GenericMqttPublish.ofMqtt5Publish(mqtt5Publish))),
testName.getMethodName()
);
final var externalMessageWithSender =
inboundMappingSinkElementReceiver.expectMsgClass(ExternalMessageWithSender.class);
final var responseCollector = externalMessageWithSender.sender();
responseCollector.tell(ResponseCollectorActor.setCount(1), ActorRef.noSender());
responseCollector.tell(
ThingNotAccessibleException.newBuilder(ThingId.of("org.eclipse.ditto", "fancy-thing"))
.dittoHeaders(DittoHeaders.newBuilder().correlationId("<command-correlation-id>").build())
.build(),
ActorRef.noSender()
);
fakeMqttClientActor.expectNoMessage();
inboundMappingSinkElementReceiver.expectMsg(onCompleteMessage);
Mockito.verify(mqtt5Publish).acknowledge();
}
}