Skip to content

Commit 846d430

Browse files
committed
fix: eclipse-uprotocol#7 fixed unit tests
1 parent ffe292c commit 846d430

File tree

2 files changed

+65
-82
lines changed

2 files changed

+65
-82
lines changed

src/main/java/org/eclipse/uprotocol/mqtt/HiveMqMQTT5Client.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@
4545

4646
class HiveMqMQTT5Client implements UTransport {
4747

48-
private static final Logger LOG = LoggerFactory.getLogger(HiveMqMQTT5Client.class);
49-
private static final String USER_PROPERTIES_KEY_FOR_ID = "1";
50-
private static final String USER_PROPERTIES_KEY_FOR_MESSAGE_TYPE = "2";
51-
private static final String USER_PROPERTIES_KEY_FOR_SOURCE_NAME = "3";
52-
private static final String USER_PROPERTIES_KEY_FOR_SINK_NAME = "4";
53-
private static final String USER_PROPERTIES_KEY_FOR_PRIORITY = "5";
54-
private static final String USER_PROPERTIES_KEY_FOR_TTL = "6";
55-
private static final String USER_PROPERTIES_KEY_FOR_PERMISSION_LEVEL = "7";
56-
private static final String USER_PROPERTIES_KEY_FOR_COMMSTATUS = "8";
57-
private static final String USER_PROPERTIES_KEY_FOR_REQID = "9";
58-
private static final String USER_PROPERTIES_KEY_FOR_TOKEN = "10";
59-
private static final String USER_PROPERTIES_KEY_FOR_TRACEPARENT = "11";
60-
private static final String USER_PROPERTIES_KEY_FOR_PAYLOAD_FORMAT = "12";
48+
public static final Logger LOG = LoggerFactory.getLogger(HiveMqMQTT5Client.class);
49+
public static final String USER_PROPERTIES_KEY_FOR_ID = "1";
50+
public static final String USER_PROPERTIES_KEY_FOR_MESSAGE_TYPE = "2";
51+
public static final String USER_PROPERTIES_KEY_FOR_SOURCE_NAME = "3";
52+
public static final String USER_PROPERTIES_KEY_FOR_SINK_NAME = "4";
53+
public static final String USER_PROPERTIES_KEY_FOR_PRIORITY = "5";
54+
public static final String USER_PROPERTIES_KEY_FOR_TTL = "6";
55+
public static final String USER_PROPERTIES_KEY_FOR_PERMISSION_LEVEL = "7";
56+
public static final String USER_PROPERTIES_KEY_FOR_COMMSTATUS = "8";
57+
public static final String USER_PROPERTIES_KEY_FOR_REQID = "9";
58+
public static final String USER_PROPERTIES_KEY_FOR_TOKEN = "10";
59+
public static final String USER_PROPERTIES_KEY_FOR_TRACEPARENT = "11";
60+
public static final String USER_PROPERTIES_KEY_FOR_PAYLOAD_FORMAT = "12";
6161
private final Mqtt5AsyncClient client;
6262
private final UUri source;
6363

@@ -69,18 +69,21 @@ public HiveMqMQTT5Client(UUri source, Mqtt5Client client) {
6969
@Override
7070
public CompletionStage<UStatus> send(UMessage uMessage) {
7171
LOG.trace("should send a message:\n{}", uMessage);
72-
CompletableFuture<UStatus> result = new CompletableFuture<>();
7372

7473
UAttributesValidator validator = UAttributesValidator.getValidator(uMessage.getAttributes());
7574
ValidationResult validationResult = validator.validate(uMessage.getAttributes());
7675
if (validationResult.isFailure()) {
7776
throw new IllegalArgumentException("Invalid message attributes: " + validationResult);
7877
}
78+
if(uMessage.getAttributes().hasTtl() && uMessage.getAttributes().getTtl() < 500){
79+
throw new IllegalArgumentException("TimeToLive needs to be at least 500ms. All smaller ttls will be dropped immediately by hiveMq");
80+
}
7981

8082
Mqtt5UserProperties userProperties = buildUserProperties(uMessage.getAttributes());
8183

8284
Mqtt5PublishBuilder.Send.Complete<CompletableFuture<Mqtt5PublishResult>> sendHandle = buildMqttSendHandle(uMessage, userProperties);
8385

86+
CompletableFuture<UStatus> result = new CompletableFuture<>();
8487
sendHandle
8588
.send()
8689
.whenCompleteAsync((mqtt5PublishResult, throwable) -> {

src/test/java/org/eclipse/uprotocol/mqtt/HiveMqIntegratedTest.java

Lines changed: 48 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616
import com.hivemq.client.mqtt.MqttGlobalPublishFilter;
1717
import com.hivemq.client.mqtt.mqtt5.Mqtt5BlockingClient;
1818
import com.hivemq.client.mqtt.mqtt5.Mqtt5Client;
19+
import com.hivemq.client.mqtt.mqtt5.datatypes.Mqtt5UserProperties;
20+
import com.hivemq.client.mqtt.mqtt5.datatypes.Mqtt5UserProperty;
1921
import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5Publish;
22+
import org.eclipse.uprotocol.communication.UPayload;
2023
import org.eclipse.uprotocol.transport.UListener;
2124
import org.eclipse.uprotocol.transport.UTransport;
25+
import org.eclipse.uprotocol.transport.builder.UMessageBuilder;
26+
import org.eclipse.uprotocol.uri.serializer.UriSerializer;
2227
import org.eclipse.uprotocol.v1.*;
2328
import org.junit.jupiter.api.BeforeEach;
2429
import org.junit.jupiter.api.Disabled;
@@ -37,6 +42,8 @@
3742
import java.util.concurrent.TimeUnit;
3843

3944
import static org.assertj.core.api.Assertions.assertThat;
45+
import static org.eclipse.uprotocol.mqtt.HiveMqMQTT5Client.USER_PROPERTIES_KEY_FOR_SINK_NAME;
46+
import static org.eclipse.uprotocol.mqtt.HiveMqMQTT5Client.USER_PROPERTIES_KEY_FOR_SOURCE_NAME;
4047
import static org.mockito.ArgumentMatchers.any;
4148
import static org.mockito.Mockito.mock;
4249
import static org.mockito.Mockito.verify;
@@ -78,43 +85,27 @@ void setUp() {
7885

7986
@Test
8087
void givenValidClientAndMessage_whenInvokeSend_shouldSendCorrectMessageToMqtt() throws InterruptedException {
81-
UMessage message = UMessage.newBuilder()
82-
.setPayload(ByteString.copyFrom("Hello World", Charset.defaultCharset()))
83-
.setAttributes(UAttributes.newBuilder()
84-
.setId(UUID.newBuilder().build())
85-
.setTtl(1000)
86-
.setReqid(UUID.newBuilder().build())
87-
.setToken("SomeToken")
88-
.setTraceparent("someTraceParent")
89-
.setSource(UUri.newBuilder()
90-
.setAuthorityName("testSource.someUri.network")
91-
.build())
92-
.setSink(UUri.newBuilder()
93-
.setAuthorityName("testDestination.someUri.network")
94-
.build())
95-
.build())
96-
.build();
88+
UMessage message = UMessageBuilder.request(
89+
UUri.newBuilder().setAuthorityName("testSource.someUri.network").setUeId(2).setUeVersionMajor(1).setResourceId(0).build(),
90+
UUri.newBuilder().setAuthorityName("testDestination.someUri.network").setUeId(2).setUeVersionMajor(1).setResourceId(1).build(), 500)
91+
.withToken("SomeToken")
92+
.withTraceparent("someTraceParent")
93+
.build(new UPayload(ByteString.copyFrom("Hello World", Charset.defaultCharset()), UPayloadFormat.UPAYLOAD_FORMAT_TEXT));
9794

9895
UStatus response = serviceUnderTest.send(message).toCompletableFuture().join();
9996
assertThat(response.getCode()).isEqualTo(UCode.OK);
100-
Mqtt5Publish receive = handleToReceiveMqttMessages.receive(1, TimeUnit.SECONDS).get();
97+
98+
Mqtt5Publish receive = handleToReceiveMqttMessages.receive(1, TimeUnit.SECONDS).orElseThrow();
10199
assertThat(new String(receive.getPayloadAsBytes())).isEqualTo("Hello World");
102100
}
103101

104102
@Test
105103
void givenValidClientAndSmallestMessage_whenInvokeSend_shouldSendCorrectMessageToMqtt() throws InterruptedException {
106-
UMessage message = UMessage.newBuilder()
107-
.setPayload(ByteString.copyFrom("Hello World", Charset.defaultCharset()))
108-
.setAttributes(UAttributes.newBuilder()
109-
.setId(UUID.newBuilder().build())
110-
.setSource(UUri.newBuilder()
111-
.setAuthorityName("testSource.someUri.network")
112-
.build())
113-
.setSink(UUri.newBuilder()
114-
.setAuthorityName("testDestination.someUri.network")
115-
.build())
116-
.build())
117-
.build();
104+
UMessage message = UMessageBuilder.request(
105+
UUri.newBuilder().setAuthorityName("testSource.someUri.network").setUeId(2).setUeVersionMajor(1).setResourceId(0).build(),
106+
UUri.newBuilder().setAuthorityName("testDestination.someUri.network").setUeId(2).setUeVersionMajor(1).setResourceId(1).build(), 500)
107+
.build(new UPayload(ByteString.copyFrom("Hello World", Charset.defaultCharset()), UPayloadFormat.UPAYLOAD_FORMAT_TEXT));
108+
118109
UStatus response = serviceUnderTest.send(message).toCompletableFuture().join();
119110
assertThat(response.getCode()).isEqualTo(UCode.OK);
120111
Mqtt5Publish receive = handleToReceiveMqttMessages.receive(1, TimeUnit.SECONDS).get();
@@ -124,15 +115,11 @@ void givenValidClientAndSmallestMessage_whenInvokeSend_shouldSendCorrectMessageT
124115
@Test
125116
@Disabled("Broadcast topic is not defined")
126117
void givenValidClientAndBroadcastMessage_whenInvokeSend_shouldSendCorrectMessageToMqtt() throws InterruptedException {
127-
UMessage message = UMessage.newBuilder()
128-
.setPayload(ByteString.copyFrom("Hello World", Charset.defaultCharset()))
129-
.setAttributes(UAttributes.newBuilder()
130-
.setId(UUID.newBuilder().build())
131-
.setSource(UUri.newBuilder()
132-
.setAuthorityName("testSource.someUri.network")
133-
.build())
134-
.build())
135-
.build();
118+
UMessage message = UMessageBuilder.publish(
119+
UUri.newBuilder().setAuthorityName("testSource.someUri.network").setUeId(2).setUeVersionMajor(1).setResourceId(0).build())
120+
.build(new UPayload(ByteString.copyFrom("Hello World", Charset.defaultCharset()), UPayloadFormat.UPAYLOAD_FORMAT_TEXT));
121+
122+
136123
UStatus response = serviceUnderTest.send(message).toCompletableFuture().join();
137124
assertThat(response.getCode()).isEqualTo(UCode.OK);
138125
Mqtt5Publish receive = handleToReceiveMqttMessages.receive(1, TimeUnit.SECONDS).get();
@@ -145,15 +132,21 @@ void givenBlancoListener_whenAddingListenerAndReceivingMessages_shouldCallListen
145132

146133
UStatus status = serviceUnderTest.registerListener(null, listener).toCompletableFuture().join();
147134

148-
mqttClientForTests.publishWith().topic("a/some-source/c/d/e/some-sink/a/b/c").payload("Hello World".getBytes(Charset.defaultCharset())).send();
135+
mqttClientForTests.publishWith().topic("a/some-source/c/d/e/some-sink/a/b/c")
136+
.userProperties(Mqtt5UserProperties.of(
137+
Mqtt5UserProperty.of(USER_PROPERTIES_KEY_FOR_SOURCE_NAME, UriSerializer.serialize(UUri.newBuilder().setAuthorityName("testSource.someUri.network").setUeId(2).setUeVersionMajor(1).setResourceId(0).build())),
138+
Mqtt5UserProperty.of(USER_PROPERTIES_KEY_FOR_SINK_NAME, UriSerializer.serialize(UUri.newBuilder().setAuthorityName("testDestination.someUri.network").setUeId(2).setUeVersionMajor(1).setResourceId(1).build()))
139+
))
140+
.payload("Hello World".getBytes(Charset.defaultCharset()))
141+
.send();
149142

150143
assertThat(status.getCode()).isEqualTo(UCode.OK);
151144

152145
ArgumentCaptor<UMessage> captor = ArgumentCaptor.captor();
153146
verify(listener, Mockito.timeout(1000).times(1)).onReceive(captor.capture());
154147
assertThat(captor.getValue()).isNotNull();
155-
assertThat(captor.getValue().getAttributes().getSink().getAuthorityName()).isEqualTo("some-sink");
156-
assertThat(captor.getValue().getAttributes().getSource().getAuthorityName()).isEqualTo("some-source");
148+
assertThat(captor.getValue().getAttributes().getSink().getAuthorityName()).isEqualTo("testDestination.someUri.network");
149+
assertThat(captor.getValue().getAttributes().getSource().getAuthorityName()).isEqualTo("testSource.someUri.network");
157150
assertThat(captor.getValue().getPayload()).isNotNull();
158151
assertThat(captor.getValue().getPayload().toString(Charset.defaultCharset())).isEqualTo("Hello World");
159152
}
@@ -332,14 +325,14 @@ void given2Listeners_whenUnregisterOneListener_shouldInvokeOtherListenersOnMessa
332325
}
333326

334327
@Test
335-
void givenListener_whenReceivingUMessageWithAllFields_shouldRouteAllFieldsToListener() {
328+
void givenListener_whenCloudSendsMessageToRadioAndRadioListens_shouldRouteMessageToRadio() {
336329
//given a radio and a cloudService
337330
UListener radioListener = mock(UListener.class);
338331
UUri radioUuid = UUri.newBuilder()
339332
.setAuthorityName("radio")
340333
.setUeId(0xffff)
341334
.setUeVersionMajor(0xff)
342-
.setResourceId(0xffff)
335+
.setResourceId(0)
343336
.build();
344337
UTransport mqttClientOfRadio = TransportFactory.createInstance(radioUuid, mqttClientForTests);
345338

@@ -351,30 +344,19 @@ void givenListener_whenReceivingUMessageWithAllFields_shouldRouteAllFieldsToList
351344
.build();
352345
UTransport mqttClientOfCloud = TransportFactory.createInstance(cloudService, mqttClientForTests);
353346

354-
mqttClientOfRadio.registerListener(
355-
cloudService,
356-
radioUuid,
357-
radioListener);
347+
mqttClientOfRadio.registerListener(cloudService, radioUuid, radioListener);
358348

359349
//when cloud service sends a message
360-
mqttClientOfCloud.send(
361-
UMessage.newBuilder()
362-
.setPayload(ByteString.copyFrom("Hello World", Charset.defaultCharset()))
363-
.setAttributes(UAttributes.newBuilder()
364-
.setId(UUID.newBuilder().setMsb(123L).build())
365-
.setType(UMessageType.UMESSAGE_TYPE_NOTIFICATION)
366-
.setSource(cloudService)
367-
.setSink(radioUuid)
368-
.setPriority(UPriority.UPRIORITY_CS0)
369-
.setTtl(1000)
370-
.setPermissionLevel(4211)
371-
.setCommstatus(UCode.OK)
372-
.setReqid(UUID.newBuilder().setMsb(456L).build())
373-
.setToken("SomeToken")
374-
.setTraceparent("someTraceParent")
375-
.setPayloadFormat(UPayloadFormat.UPAYLOAD_FORMAT_TEXT)
376-
.build())
377-
.build());
350+
351+
UMessage message = UMessageBuilder.notification(cloudService, radioUuid)
352+
.withPriority(UPriority.UPRIORITY_CS2)
353+
.withTtl(1000)
354+
.withPermissionLevel(4211)
355+
.withToken("SomeToken")
356+
.withTraceparent("someTraceParent")
357+
.build(new UPayload(ByteString.copyFrom("Hello World", Charset.defaultCharset()), UPayloadFormat.UPAYLOAD_FORMAT_TEXT));
358+
359+
mqttClientOfCloud.send(message);
378360

379361
//should be received by radio
380362
ArgumentCaptor<UMessage> captor = ArgumentCaptor.captor();
@@ -383,15 +365,13 @@ void givenListener_whenReceivingUMessageWithAllFields_shouldRouteAllFieldsToList
383365
UMessage receivedMessage = captor.getValue();
384366

385367
assertThat(receivedMessage.getPayload().toString(Charset.defaultCharset())).isEqualTo("Hello World");
386-
assertThat(receivedMessage.getAttributes().getId().getMsb()).isEqualTo(123L);
387368
assertThat(receivedMessage.getAttributes().getType()).isEqualTo(UMessageType.UMESSAGE_TYPE_NOTIFICATION);
388369
assertThat(receivedMessage.getAttributes().getSource().getAuthorityName()).isEqualTo("cloud");
389370
assertThat(receivedMessage.getAttributes().getSink().getAuthorityName()).isEqualTo("radio");
390-
assertThat(receivedMessage.getAttributes().getPriority()).isEqualTo(UPriority.UPRIORITY_CS0);
371+
assertThat(receivedMessage.getAttributes().getPriority()).isEqualTo(UPriority.UPRIORITY_CS2);
391372
assertThat(receivedMessage.getAttributes().getTtl()).isEqualTo(1000);
392373
assertThat(receivedMessage.getAttributes().getPermissionLevel()).isEqualTo(4211);
393374
assertThat(receivedMessage.getAttributes().getCommstatus()).isEqualTo(UCode.OK);
394-
assertThat(receivedMessage.getAttributes().getReqid()).isEqualTo(UUID.newBuilder().setMsb(456L).build());
395375
assertThat(receivedMessage.getAttributes().getToken()).isEqualTo("SomeToken");
396376
assertThat(receivedMessage.getAttributes().getTraceparent()).isEqualTo("someTraceParent");
397377
assertThat(receivedMessage.getAttributes().getPayloadFormat()).isEqualTo(UPayloadFormat.UPAYLOAD_FORMAT_TEXT);

0 commit comments

Comments
 (0)