From b72c08d988e89e8ba29211e37abf96075e25e60a Mon Sep 17 00:00:00 2001 From: YevhenBondarenko Date: Thu, 23 May 2024 22:27:14 +0200 Subject: [PATCH] fixed mqtt basic connectivity command --- .../DeviceConnectivityControllerTest.java | 30 +++++++++++++++++-- .../dao/util/DeviceConnectivityUtil.java | 12 ++++---- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/DeviceConnectivityControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/DeviceConnectivityControllerTest.java index 38e537340f..71b6a52571 100644 --- a/application/src/test/java/org/thingsboard/server/controller/DeviceConnectivityControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/DeviceConnectivityControllerTest.java @@ -462,8 +462,7 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest { basicMqttCredentials.setUserName(userName); basicMqttCredentials.setPassword(password); credentials.setCredentialsValue(JacksonUtil.toString(basicMqttCredentials)); - doPost("/api/device/credentials", credentials) - .andExpect(status().isOk()); + credentials = doPost("/api/device/credentials", credentials, DeviceCredentials.class); JsonNode commands = doGetTyped("/api/device-connectivity/" + savedDevice.getId().getId(), new TypeReference<>() { @@ -485,6 +484,33 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest { "/bin/sh -c \"curl -f -S -o " + CA_ROOT_CERT_PEM + " http://localhost:80/api/device-connectivity/mqtts/certificate/download && " + "mosquitto_pub -d -q 1 --cafile " + CA_ROOT_CERT_PEM + " -h host.docker.internal -p 8883 -t %s -i \"%s\" -u \"%s\" -P \"%s\" -m \"{temperature:25}\"\"", DEVICE_TELEMETRY_TOPIC, clientId, userName, password)); + + basicMqttCredentials.setClientId(""); + credentials.setCredentialsValue(JacksonUtil.toString(basicMqttCredentials)); + + doPost("/api/device/credentials", credentials) + .andExpect(status().isOk()); + + commands = + doGetTyped("/api/device-connectivity/" + savedDevice.getId().getId(), new TypeReference<>() { + }); + assertThat(commands).hasSize(1); + + mqttCommands = commands.get(MQTT); + assertThat(mqttCommands.get(MQTT).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 -h localhost -p 1883 -t %s " + + "-u \"%s\" -P \"%s\" -m \"{temperature:25}\"", DEVICE_TELEMETRY_TOPIC, userName, password)); + assertThat(mqttCommands.get(MQTTS).get(0).asText()).isEqualTo("curl -f -S -o " + CA_ROOT_CERT_PEM + " http://localhost:80/api/device-connectivity/mqtts/certificate/download"); + assertThat(mqttCommands.get(MQTTS).get(1).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 --cafile " + CA_ROOT_CERT_PEM + " -h localhost -p 8883 " + + "-t %s -u \"%s\" -P \"%s\" -m \"{temperature:25}\"", DEVICE_TELEMETRY_TOPIC, userName, password)); + + dockerMqttCommands = commands.get(MQTT).get(DOCKER); + assertThat(dockerMqttCommands.get(MQTT).asText()).isEqualTo(String.format("docker run --rm -it --add-host=host.docker.internal:host-gateway thingsboard/mosquitto-clients mosquitto_pub -d -q 1 -h host.docker.internal" + + " -p 1883 -t %s -u \"%s\" -P \"%s\" -m \"{temperature:25}\"", + DEVICE_TELEMETRY_TOPIC, userName, password)); + assertThat(dockerMqttCommands.get(MQTTS).asText()).isEqualTo(String.format("docker run --rm -it --add-host=host.docker.internal:host-gateway thingsboard/mosquitto-clients " + + "/bin/sh -c \"curl -f -S -o " + CA_ROOT_CERT_PEM + " http://localhost:80/api/device-connectivity/mqtts/certificate/download && " + + "mosquitto_pub -d -q 1 --cafile " + CA_ROOT_CERT_PEM + " -h host.docker.internal -p 8883 -t %s -u \"%s\" -P \"%s\" -m \"{temperature:25}\"\"", + DEVICE_TELEMETRY_TOPIC, userName, password)); } @Test diff --git a/dao/src/main/java/org/thingsboard/server/dao/util/DeviceConnectivityUtil.java b/dao/src/main/java/org/thingsboard/server/dao/util/DeviceConnectivityUtil.java index f4a77dc725..da0b25b5f6 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/util/DeviceConnectivityUtil.java +++ b/dao/src/main/java/org/thingsboard/server/dao/util/DeviceConnectivityUtil.java @@ -72,13 +72,13 @@ public class DeviceConnectivityUtil { BasicMqttCredentials credentials = JacksonUtil.fromString(deviceCredentials.getCredentialsValue(), BasicMqttCredentials.class); if (credentials != null) { - if (credentials.getClientId() != null) { + if (StringUtils.isNotEmpty(credentials.getClientId())) { command.append(" -i \"").append(credentials.getClientId()).append("\""); } - if (credentials.getUserName() != null) { + if (StringUtils.isNotEmpty(credentials.getUserName())) { command.append(" -u \"").append(credentials.getUserName()).append("\""); } - if (credentials.getPassword() != null) { + if (StringUtils.isNotEmpty(credentials.getPassword())) { command.append(" -P \"").append(credentials.getPassword()).append("\""); } } else { @@ -129,13 +129,13 @@ public class DeviceConnectivityUtil { BasicMqttCredentials credentials = JacksonUtil.fromString(deviceCredentials.getCredentialsValue(), BasicMqttCredentials.class); if (credentials != null) { - if (credentials.getClientId() != null) { + if (StringUtils.isNotEmpty(credentials.getClientId())) { dockerComposeBuilder.append(" - clientId=").append(credentials.getClientId()).append("\n"); } - if (credentials.getUserName() != null) { + if (StringUtils.isNotEmpty(credentials.getUserName())) { dockerComposeBuilder.append(" - username=").append(credentials.getUserName()).append("\n"); } - if (credentials.getPassword() != null) { + if (StringUtils.isNotEmpty(credentials.getPassword())) { dockerComposeBuilder.append(" - password=").append(credentials.getPassword()).append("\n"); } }