-
Notifications
You must be signed in to change notification settings - Fork 215
/
GenericMqttPublishTest.java
637 lines (516 loc) · 26.3 KB
/
GenericMqttPublishTest.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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
* 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.publish;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Set;
import java.util.stream.Collectors;
import org.assertj.core.api.JUnitSoftAssertions;
import org.eclipse.ditto.base.model.common.ByteBufferUtils;
import org.eclipse.ditto.base.model.correlationid.TestNameCorrelationId;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import com.hivemq.client.mqtt.MqttVersion;
import com.hivemq.client.mqtt.datatypes.MqttQos;
import com.hivemq.client.mqtt.datatypes.MqttTopic;
import com.hivemq.client.mqtt.datatypes.MqttUtf8String;
import com.hivemq.client.mqtt.mqtt3.message.publish.Mqtt3Publish;
import com.hivemq.client.mqtt.mqtt5.datatypes.Mqtt5UserProperties;
import com.hivemq.client.mqtt.mqtt5.datatypes.Mqtt5UserProperty;
import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5PayloadFormatIndicator;
import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5Publish;
import nl.jqno.equalsverifier.EqualsVerifier;
/**
* Unit test for {@link GenericMqttPublish}.
*/
@RunWith(Enclosed.class)
public final class GenericMqttPublishTest {
private static final MqttTopic MQTT_TOPIC = MqttTopic.of("/source/my-connection");
private static final MqttQos MQTT_QOS = MqttQos.AT_LEAST_ONCE;
private static final boolean RETAIN = true;
private static final ByteBuffer 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
}
}
}
""");
private static final String CONTENT_TYPE = "application/json";
private static final MqttTopic RESPONSE_TOPIC = MqttTopic.of("source/status");
private static final Set<UserProperty> USER_PROPERTIES = Set.of(new UserProperty("foo", "bar"),
new UserProperty("bar", "baz"),
new UserProperty("baz", "foo"));
@RunWith(MockitoJUnitRunner.class)
public static final class GenericMqtt3PublishTest {
private Mqtt3Publish mqtt3Publish;
@Before
public void before() {
mqtt3Publish = Mqtt3Publish.builder()
.topic(MQTT_TOPIC)
.qos(MQTT_QOS)
.retain(RETAIN)
.payload(PAYLOAD)
.build();
}
@Test
public void testHashCodeAndEquals() throws ClassNotFoundException {
EqualsVerifier.forClass(Class.forName("""
org.eclipse.ditto.connectivity.service.messaging.mqtt.hivemq.publish.\
GenericMqttPublish$GenericMqtt3Publish\
"""))
.usingGetClass()
.verify();
}
@Test
public void ofMqtt3PublishWithNullThrowsException() {
assertThatNullPointerException()
.isThrownBy(() -> GenericMqttPublish.ofMqtt3Publish(null))
.withMessage("The mqtt3Publish must not be null!")
.withNoCause();
}
@Test
public void ofMqtt3PublishReturnsNotNull() {
assertThat(GenericMqttPublish.ofMqtt3Publish(mqtt3Publish)).isNotNull();
}
@Test
public void getTopicReturnsExpected() {
final var underTest = GenericMqttPublish.ofMqtt3Publish(mqtt3Publish);
assertThat(underTest.getTopic()).isEqualTo(MQTT_TOPIC);
}
@Test
public void getQosReturnsExpected() {
final var underTest = GenericMqttPublish.ofMqtt3Publish(mqtt3Publish);
assertThat(underTest.getQos()).isEqualTo(MQTT_QOS);
}
@Test
public void isRetainReturnsTrue() {
final var underTest = GenericMqttPublish.ofMqtt3Publish(mqtt3Publish);
assertThat(underTest.isRetain()).isTrue();
}
@Test
public void getCorrelationDataReturnsEmptyOptional() {
final var underTest = GenericMqttPublish.ofMqtt3Publish(mqtt3Publish);
assertThat(underTest.getCorrelationData()).isEmpty();
}
@Test
public void getResponseTopicReturnsEmptyOptional() {
final var underTest = GenericMqttPublish.ofMqtt3Publish(mqtt3Publish);
assertThat(underTest.getResponseTopic()).isEmpty();
}
@Test
public void getContentTypeReturnsEmptyOptional() {
final var underTest = GenericMqttPublish.ofMqtt3Publish(mqtt3Publish);
assertThat(underTest.getContentType()).isEmpty();
}
@Test
public void userPropertiesReturnsEmptyStream() {
final var underTest = GenericMqttPublish.ofMqtt3Publish(mqtt3Publish);
assertThat(underTest.userProperties()).isEmpty();
}
@Test
public void getPayloadReturnsOptionalContainingExpectedValue() {
final var underTest = GenericMqttPublish.ofMqtt3Publish(mqtt3Publish);
assertThat(underTest.getPayload()).contains(PAYLOAD);
}
@Test
public void getPayloadAsHumanReadableReturnsExpected() {
final var underTest = GenericMqttPublish.ofMqtt3Publish(Mqtt3Publish.builder()
.topic(MQTT_TOPIC)
.payload(PAYLOAD)
.build());
assertThat(underTest.getPayloadAsHumanReadable()).hasValue(ByteBufferUtils.toUtf8String(PAYLOAD));
}
@Test
public void acknowledgeIsDelegatedToMqtt3Publish() throws MqttPublishAcknowledgementException {
final var mqtt3Publish = Mockito.mock(Mqtt3Publish.class);
final var underTest = GenericMqttPublish.ofMqtt3Publish(mqtt3Publish);
underTest.acknowledge();
Mockito.verify(mqtt3Publish).acknowledge();
}
@Test
public void acknowledgeWithDisabledManualAcknowledgementThrowsExpectedException() {
final var mqtt3Publish = Mockito.mock(Mqtt3Publish.class);
final var unsupportedOperationException =
new UnsupportedOperationException("Manual acknowledgement disabled.");
Mockito.doThrow(unsupportedOperationException).when(mqtt3Publish).acknowledge();
final var underTest = GenericMqttPublish.ofMqtt3Publish(mqtt3Publish);
assertThatExceptionOfType(ManualAcknowledgementDisabledException.class)
.isThrownBy(underTest::acknowledge)
.withMessageEndingWith(unsupportedOperationException.getMessage())
.withCause(unsupportedOperationException);
}
@Test
public void acknowledgeWhenAlreadyAcknowledgedThrowsExpectedException() {
final var mqtt3Publish = Mockito.mock(Mqtt3Publish.class);
final var illegalStateException = new IllegalStateException("Already acknowledged.");
Mockito.doThrow(illegalStateException).when(mqtt3Publish).acknowledge();
final var underTest = GenericMqttPublish.ofMqtt3Publish(mqtt3Publish);
assertThatExceptionOfType(MessageAlreadyAcknowledgedException.class)
.isThrownBy(underTest::acknowledge)
.withMessageEndingWith(illegalStateException.getMessage())
.withCause(illegalStateException);
}
@Test
public void isMqttVersionReturnsExpected() {
final var underTest = GenericMqttPublish.ofMqtt3Publish(Mqtt3Publish.builder().topic(MQTT_TOPIC).build());
assertThat(underTest.isMqttVersion(MqttVersion.MQTT_3_1_1)).isTrue();
assertThat(underTest.isMqttVersion(MqttVersion.MQTT_5_0)).isFalse();
}
@Test
public void getAsMqtt3PublishReturnsPublishProvidedAtObjectConstruction() {
final var underTest = GenericMqttPublish.ofMqtt3Publish(mqtt3Publish);
assertThat(underTest.getAsMqtt3Publish()).isEqualTo(mqtt3Publish);
}
@Test
public void getAsMqtt5PublishReturnsExpectedMqtt5Publish() {
final var underTest = GenericMqttPublish.ofMqtt3Publish(mqtt3Publish);
assertThat(underTest.getAsMqtt5Publish())
.isEqualTo(Mqtt5Publish.builder()
.topic(mqtt3Publish.getTopic())
.qos(mqtt3Publish.getQos())
.retain(mqtt3Publish.isRetain())
.payload(mqtt3Publish.getPayloadAsBytes())
.build());
}
@Test
public void toStringReturnsExpected() {
final var stringRepresentation = "Do not read this!";
final var mqtt3Publish = Mockito.mock(Mqtt3Publish.class);
Mockito.when(mqtt3Publish.toString()).thenReturn(stringRepresentation);
final var underTest = GenericMqttPublish.ofMqtt3Publish(mqtt3Publish);
assertThat(underTest.toString()).isEqualTo(stringRepresentation);
}
}
public static final class GenericMqtt5PublishTest {
private static final Mqtt5Publish MQTT_5_PUBLISH = Mqtt5Publish.builder()
.topic(MQTT_TOPIC)
.qos(MQTT_QOS)
.retain(true)
.payload(PAYLOAD)
.contentType(CONTENT_TYPE)
.userProperties(Mqtt5UserProperties.of(USER_PROPERTIES.stream()
.map(userProperty -> Mqtt5UserProperty.of(userProperty.name(), userProperty.value()))
.collect(Collectors.toList())))
.build();
@Rule
public final TestNameCorrelationId testNameCorrelationId = TestNameCorrelationId.newInstance();
@Test
public void testHashCodeAndEquals() throws ClassNotFoundException {
EqualsVerifier.forClass(Class.forName("""
org.eclipse.ditto.connectivity.service.messaging.mqtt.hivemq.publish.\
GenericMqttPublish$GenericMqtt5Publish\
"""))
.usingGetClass()
.verify();
}
@Test
public void ofMqtt5PublishWithNullThrowsException() {
assertThatNullPointerException()
.isThrownBy(() -> GenericMqttPublish.ofMqtt5Publish(null))
.withMessage("The mqtt5Publish must not be null!")
.withNoCause();
}
@Test
public void getTopicReturnsExpected() {
final var underTest = GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder().topic(MQTT_TOPIC).build());
assertThat(underTest.getTopic()).isEqualTo(MQTT_TOPIC);
}
@Test
public void getQosReturnsExpected() {
final var underTest =
GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder().topic(MQTT_TOPIC).qos(MQTT_QOS).build());
assertThat(underTest.getQos()).isEqualTo(MQTT_QOS);
}
@Test
public void isRetainReturnsTrueIfSet() {
final var underTest =
GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder().topic(MQTT_TOPIC).retain(true).build());
assertThat(underTest.isRetain()).isTrue();
}
@Test
public void isRetainReturnsFalseIfSet() {
final var underTest =
GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder().topic(MQTT_TOPIC).retain(false).build());
assertThat(underTest.isRetain()).isFalse();
}
@Test
public void isRetainReturnsFalseIfAbsent() {
final var underTest = GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder().topic(MQTT_TOPIC).build());
assertThat(underTest.isRetain()).isFalse();
}
@Test
public void getCorrelationDataReturnsEmptyOptionalIfAbsent() {
final var underTest = GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder().topic(MQTT_TOPIC).build());
assertThat(underTest.getCorrelationData()).isEmpty();
}
@Test
public void getCorrelationDataReturnsOptionalContainingExpectedValueIfPresent() {
final var correlationData =
ByteBufferUtils.fromUtf8String(String.valueOf(testNameCorrelationId.getCorrelationId()));
final var underTest = GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder()
.topic(MQTT_TOPIC)
.correlationData(correlationData)
.build());
assertThat(underTest.getCorrelationData()).contains(correlationData);
}
@Test
public void getResponseTopicReturnsOptionalContainingExpectedValueIfPresent() {
final var underTest = GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder()
.topic(MQTT_TOPIC)
.responseTopic(RESPONSE_TOPIC)
.build());
assertThat(underTest.getResponseTopic()).contains(RESPONSE_TOPIC);
}
@Test
public void getResponseTopicReturnsEmptyOptionalIfAbsent() {
final var underTest = GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder().topic(MQTT_TOPIC).build());
assertThat(underTest.getResponseTopic()).isEmpty();
}
@Test
public void getContentTypeReturnsOptionalContainingExpectedValueIfPresent() {
final var contentType = "application/json";
final var underTest = GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder()
.topic(MQTT_TOPIC)
.contentType(contentType)
.build());
assertThat(underTest.getContentType()).contains(contentType);
}
@Test
public void getContentTypeReturnsOptionalContainingTextPlainIfIndicatedByPayload() {
final var underTest = GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder()
.topic(MQTT_TOPIC)
.payloadFormatIndicator(Mqtt5PayloadFormatIndicator.UTF_8)
.build());
assertThat(underTest.getContentType()).contains("text/plain; charset=UTF-8");
}
@Test
public void getContentTypeReturnsOptionalContainingApplicationOctetStreamIfIndicatedByPayload() {
final var underTest = GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder()
.topic(MQTT_TOPIC)
.payloadFormatIndicator(Mqtt5PayloadFormatIndicator.UNSPECIFIED)
.build());
assertThat(underTest.getContentType()).contains("application/octet-stream");
}
@Test
public void getContentTypeReturnsEmptyOptionalIfAbsent() {
final var underTest = GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder().topic(MQTT_TOPIC).build());
assertThat(underTest.getContentType()).isEmpty();
}
@Test
public void userPropertiesReturnsExpectedStreamIfPresent() {
final var underTest = GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder()
.topic(MQTT_TOPIC)
.userProperties(Mqtt5UserProperties.of(USER_PROPERTIES.stream()
.map(userProperty -> Mqtt5UserProperty.of(userProperty.name(), userProperty.value()))
.collect(Collectors.toList())))
.build());
assertThat(underTest.userProperties()).containsExactlyElementsOf(USER_PROPERTIES);
}
@Test
public void userPropertiesReturnsEmptyStreamIfAbsent() {
final var underTest = GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder()
.topic(MQTT_TOPIC)
.build());
assertThat(underTest.userProperties()).isEmpty();
}
@Test
public void getPayloadReturnsOptionalContainingExpectedValueIfPresent() {
final var underTest = GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder()
.topic(MQTT_TOPIC)
.payload(PAYLOAD)
.build());
assertThat(underTest.getPayload()).contains(PAYLOAD);
}
@Test
public void getPayloadReturnsEmptyOptionalIfAbsent() {
final var underTest = GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder().topic(MQTT_TOPIC).build());
assertThat(underTest.getPayload()).isEmpty();
}
@Test
public void getPayloadAsHumanReadableReturnsExpected() {
final var underTest = GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder()
.topic(MQTT_TOPIC)
.payload(PAYLOAD)
.contentType(CONTENT_TYPE)
.build());
assertThat(underTest.getPayloadAsHumanReadable()).hasValue(ByteBufferUtils.toUtf8String(PAYLOAD));
}
@Test
public void acknowledgeIsDelegatedToMqtt5Publish() throws MqttPublishAcknowledgementException {
final var mqtt5Publish = Mockito.mock(Mqtt5Publish.class);
final var underTest = GenericMqttPublish.ofMqtt5Publish(mqtt5Publish);
underTest.acknowledge();
Mockito.verify(mqtt5Publish).acknowledge();
}
@Test
public void acknowledgeWithDisabledManualAcknowledgementThrowsExpectedException() {
final var mqtt5Publish = Mockito.mock(Mqtt5Publish.class);
final var unsupportedOperationException =
new UnsupportedOperationException("Manual acknowledgement disabled.");
Mockito.doThrow(unsupportedOperationException).when(mqtt5Publish).acknowledge();
final var underTest = GenericMqttPublish.ofMqtt5Publish(mqtt5Publish);
assertThatExceptionOfType(ManualAcknowledgementDisabledException.class)
.isThrownBy(underTest::acknowledge)
.withMessageEndingWith(unsupportedOperationException.getMessage())
.withCause(unsupportedOperationException);
}
@Test
public void acknowledgeWhenAlreadyAcknowledgedThrowsExpectedException() {
final var mqtt5Publish = Mockito.mock(Mqtt5Publish.class);
final var illegalStateException = new IllegalStateException("Already acknowledged.");
Mockito.doThrow(illegalStateException).when(mqtt5Publish).acknowledge();
final var underTest = GenericMqttPublish.ofMqtt5Publish(mqtt5Publish);
assertThatExceptionOfType(MessageAlreadyAcknowledgedException.class)
.isThrownBy(underTest::acknowledge)
.withMessageEndingWith(illegalStateException.getMessage())
.withCause(illegalStateException);
}
@Test
public void isMqttVersionReturnsExpected() {
final var underTest = GenericMqttPublish.ofMqtt5Publish(Mqtt5Publish.builder().topic(MQTT_TOPIC).build());
assertThat(underTest.isMqttVersion(MqttVersion.MQTT_3_1_1)).isFalse();
assertThat(underTest.isMqttVersion(MqttVersion.MQTT_5_0)).isTrue();
}
@Test
public void getAsMqtt3PublishReturnsPublishProvidedAtObjectConstruction() {
final var underTest = GenericMqttPublish.ofMqtt5Publish(MQTT_5_PUBLISH);
assertThat(underTest.getAsMqtt3Publish())
.isEqualTo(Mqtt3Publish.builder()
.topic(MQTT_5_PUBLISH.getTopic())
.qos(MQTT_5_PUBLISH.getQos())
.retain(MQTT_5_PUBLISH.isRetain())
.payload(MQTT_5_PUBLISH.getPayloadAsBytes())
.build());
}
@Test
public void getAsMqtt5PublishReturnsExpectedMqtt5Publish() {
final var underTest = GenericMqttPublish.ofMqtt5Publish(MQTT_5_PUBLISH);
assertThat(underTest.getAsMqtt5Publish()).isEqualTo(MQTT_5_PUBLISH);
}
@Test
public void toStringReturnsExpected() {
final var stringRepresentation = "Do not read this!";
final var mqtt5Publish = Mockito.mock(Mqtt5Publish.class);
Mockito.when(mqtt5Publish.toString()).thenReturn(stringRepresentation);
final var underTest = GenericMqttPublish.ofMqtt5Publish(mqtt5Publish);
assertThat(underTest.toString()).isEqualTo(stringRepresentation);
}
}
public static final class FromScratchGenericMqttPublishTest {
@Rule
public final TestNameCorrelationId testNameCorrelationId = TestNameCorrelationId.newInstance();
@Rule
public final JUnitSoftAssertions softly = new JUnitSoftAssertions();
private ByteBuffer correlationData;
private GenericMqttPublish underTest;
@Before
public void before() {
correlationData = ByteBuffer.wrap(
String.valueOf(testNameCorrelationId.getCorrelationId()).getBytes(StandardCharsets.UTF_8)
);
underTest = GenericMqttPublish.builder(MQTT_TOPIC, MQTT_QOS)
.retain(true)
.payload(PAYLOAD)
.contentType(CONTENT_TYPE)
.correlationData(correlationData)
.responseTopic(RESPONSE_TOPIC)
.userProperties(USER_PROPERTIES)
.build();
}
@Test
public void testHashCodeAndEquals() throws ClassNotFoundException {
EqualsVerifier.forClass(Class.forName("""
org.eclipse.ditto.connectivity.service.messaging.mqtt.hivemq.publish.\
GenericMqttPublish$FromScratchGenericMqttPublish\
"""))
.usingGetClass()
.verify();
}
@Test
public void instanceByBuilderContainsExpectedProperties() {
softly.assertThat(underTest.getTopic()).as("topic").isEqualTo(MQTT_TOPIC);
softly.assertThat(underTest.getQos()).as("QoS").isEqualTo(MQTT_QOS);
softly.assertThat(underTest.isRetain()).as("retain").isTrue();
softly.assertThat(underTest.getPayload()).as("payload").hasValue(PAYLOAD);
softly.assertThat(underTest.getContentType()).as("content type").hasValue(CONTENT_TYPE);
softly.assertThat(underTest.getCorrelationData()).as("correlation data").hasValue(correlationData);
softly.assertThat(underTest.getResponseTopic()).as("response topic").hasValue(RESPONSE_TOPIC);
softly.assertThat(underTest.userProperties()).as("user properties").hasSameElementsAs(USER_PROPERTIES);
}
@Test
public void acknowledgeThrowsExpectedException() {
assertThatExceptionOfType(AcknowledgementUnsupportedException.class)
.isThrownBy(underTest::acknowledge)
.withMessage("A GenericMqttPublish that was built from scratch cannot be acknowledged.")
.withNoCause();
}
@Test
public void isMqttVersionReturnsAlwaysFalse() {
for (final var mqttVersion : MqttVersion.values()) {
softly.assertThat(underTest.isMqttVersion(mqttVersion)).as("is not %s", mqttVersion).isFalse();
}
}
@Test
public void getPayloadAsHumanReadableReturnsExpected() {
final var underTest =
GenericMqttPublish.builder(MQTT_TOPIC, MQTT_QOS).payload(PAYLOAD).contentType(CONTENT_TYPE).build();
assertThat(underTest.getPayloadAsHumanReadable()).hasValue(ByteBufferUtils.toUtf8String(PAYLOAD));
}
@Test
public void getAsMqtt3PublishReturnsExpected() {
final var mqtt3Publish = underTest.getAsMqtt3Publish();
softly.assertThat(mqtt3Publish.getTopic()).as("topic").isEqualTo(underTest.getTopic());
softly.assertThat(mqtt3Publish.getQos()).as("QoS").isEqualTo(underTest.getQos());
softly.assertThat(mqtt3Publish.isRetain()).as("retain").isEqualTo(underTest.isRetain());
softly.assertThat(mqtt3Publish.getPayload()).as("payload").isEqualTo(underTest.getPayload());
}
@Test
public void getAsMqtt5PublishReturnsExpected() {
final var mqtt5Publish = underTest.getAsMqtt5Publish();
softly.assertThat(mqtt5Publish.getTopic()).as("topic").isEqualTo(underTest.getTopic());
softly.assertThat(mqtt5Publish.getQos()).as("QoS").isEqualTo(underTest.getQos());
softly.assertThat(mqtt5Publish.isRetain()).as("retain").isEqualTo(underTest.isRetain());
softly.assertThat(mqtt5Publish.getPayload()).as("payload").isEqualTo(underTest.getPayload());
softly.assertThat(mqtt5Publish.getContentType())
.as("content type")
.hasValue(MqttUtf8String.of(CONTENT_TYPE));
softly.assertThat(mqtt5Publish.getCorrelationData())
.as("correlation data")
.isEqualTo(underTest.getCorrelationData());
softly.assertThat(mqtt5Publish.getResponseTopic())
.as("response topic")
.isEqualTo(underTest.getResponseTopic());
softly.assertThat(mqtt5Publish.getUserProperties())
.as("user properties")
.isEqualTo(Mqtt5UserProperties.of(USER_PROPERTIES.stream()
.map(userProperty -> Mqtt5UserProperty.of(userProperty.name(), userProperty.value()))
.collect(Collectors.toList())));
}
}
}