changed response data structure

This commit is contained in:
dashevchenko 2023-07-21 12:52:15 +03:00
parent fc499c74e3
commit d99c08fbbb
2 changed files with 45 additions and 68 deletions

View File

@ -213,7 +213,7 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
credentials.getCredentialsId()));
JsonNode linuxMqttCommands = commands.get(MQTT).get(LINUX);
JsonNode linuxMqttCommands = commands.get(MQTT);
assertThat(linuxMqttCommands.get(MQTT).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 -h localhost -p 1883 -t v1/devices/me/telemetry " +
"-u %s -m \"{temperature:25}\"",
credentials.getCredentialsId()));
@ -221,11 +221,6 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
"-t v1/devices/me/telemetry -u %s -m \"{temperature:25}\"",
credentials.getCredentialsId()));
JsonNode windowsMqttCommands = commands.get(MQTT).get(WINDOWS);
assertThat(windowsMqttCommands.get(MQTT).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 -h localhost -p 1883 -t v1/devices/me/telemetry " +
"-u %s -m \"{temperature:25}\"",
credentials.getCredentialsId()));
JsonNode dockerMqttCommands = commands.get(MQTT).get(DOCKER);
assertThat(dockerMqttCommands.get(MQTT).asText()).isEqualTo(String.format("docker run -it --rm thingsboard/mosquitto-clients pub -h localhost" +
@ -235,13 +230,11 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
"-it --rm thingsboard/mosquitto-clients pub --cafile tmp/tb-server-chain.pem -h localhost -p 8883 -t v1/devices/me/telemetry -u %s -m \"{temperature:25}\"",
credentials.getCredentialsId()));
JsonNode linuxCoapCommands = commands.get(COAP).get(LINUX);
JsonNode linuxCoapCommands = commands.get(COAP);
assertThat(linuxCoapCommands.get(COAP).asText()).isEqualTo(String.format("coap-client -m POST coap://localhost:5683/api/v1/%s/telemetry " +
"-t json -e \"{temperature:25}\"",
credentials.getCredentialsId()));
"-t json -e \"{temperature:25}\"", credentials.getCredentialsId()));
assertThat(linuxCoapCommands.get(COAPS).asText()).isEqualTo(String.format("coap-client-openssl -m POST coaps://localhost:5684/api/v1/%s/telemetry" +
" -t json -e \"{temperature:25}\"",
credentials.getCredentialsId()));
" -t json -e \"{temperature:25}\"", credentials.getCredentialsId()));
}
@Test
@ -258,7 +251,7 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
doGetTyped("/api/device-connectivity/" + savedDevice.getId().getId() , new TypeReference<>() {});
assertThat(commands).hasSize(1);
JsonNode linuxMqttCommands = commands.get(MQTT).get(LINUX);
JsonNode linuxMqttCommands = commands.get(MQTT);
assertThat(linuxMqttCommands.get(MQTT).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 -h localhost -p 1883 -t %s " +
"-u %s -m \"{temperature:25}\"",
DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId()));
@ -266,11 +259,6 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
"-t %s -u %s -m \"{temperature:25}\"",
DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId()));
JsonNode windowsMqttCommands = commands.get(MQTT).get(WINDOWS);
assertThat(windowsMqttCommands.get(MQTT).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 -h localhost -p 1883 -t %s " +
"-u %s -m \"{temperature:25}\"",
DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId()));
JsonNode dockerMqttCommands = commands.get(MQTT).get(DOCKER);
assertThat(dockerMqttCommands.get(MQTT).asText()).isEqualTo(String.format("docker run -it --rm thingsboard/mosquitto-clients pub -h localhost" +
@ -303,12 +291,11 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
doPost("/api/device/credentials", credentials)
.andExpect(status().isOk());
JsonNode commands =
doGetTyped("/api/device-connectivity/" + savedDevice.getId().getId() , new TypeReference<>() {});
assertThat(commands).hasSize(1);
JsonNode linuxMqttCommands = commands.get(MQTT).get(LINUX);
JsonNode linuxMqttCommands = commands.get(MQTT);
assertThat(linuxMqttCommands.get(MQTT).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 -h localhost -p 1883 -t %s " +
"-i %s -u %s -P %s -m \"{temperature:25}\"",
DEVICE_TELEMETRY_TOPIC, clientId, userName, password));
@ -316,12 +303,6 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
"-t %s -i %s -u %s -P %s -m \"{temperature:25}\"",
DEVICE_TELEMETRY_TOPIC, clientId, userName, password));
JsonNode windowsMqttCommands = commands.get(MQTT).get(WINDOWS);
assertThat(windowsMqttCommands.get(MQTT).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 -h localhost -p 1883 -t %s " +
"-i %s -u %s -P %s -m \"{temperature:25}\"",
DEVICE_TELEMETRY_TOPIC, clientId, userName, password));
JsonNode dockerMqttCommands = commands.get(MQTT).get(DOCKER);
assertThat(dockerMqttCommands.get(MQTT).asText()).isEqualTo(String.format("docker run -it --rm thingsboard/mosquitto-clients pub -h localhost" +
" -p 1883 -t %s -i %s -u %s -P %s -m \"{temperature:25}\"",
@ -349,8 +330,7 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
JsonNode commands =
doGetTyped("/api/device-connectivity/" + savedDevice.getId().getId(), new TypeReference<>() {});
assertThat(commands).hasSize(1);
assertThat(commands.get(MQTT).get(LINUX).get(MQTTS).asText()).isEqualTo(CHECK_DOCUMENTATION);
assertThat(commands.get(MQTT).get(WINDOWS).get(MQTTS).asText()).isEqualTo(CHECK_DOCUMENTATION);
assertThat(commands.get(MQTT).get(MQTTS).asText()).isEqualTo(CHECK_DOCUMENTATION);
assertThat(commands.get(MQTT).get(DOCKER).get(MQTTS).asText()).isEqualTo(CHECK_DOCUMENTATION);
}
@ -368,7 +348,7 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
doGetTyped("/api/device-connectivity/" + savedDevice.getId().getId(), new TypeReference<>() {});
assertThat(commands).hasSize(1);
JsonNode linuxCommands = commands.get(COAP).get(LINUX);
JsonNode linuxCommands = commands.get(COAP);
assertThat(linuxCommands.get(COAP).asText()).isEqualTo(String.format("coap-client -m POST coap://localhost:5683/api/v1/%s/telemetry -t json -e \"{temperature:25}\"",
credentials.getCredentialsId()));
assertThat(linuxCommands.get(COAPS).asText()).isEqualTo(String.format("coap-client-openssl -m POST coaps://localhost:5684/api/v1/%s/telemetry -t json -e \"{temperature:25}\"",
@ -393,6 +373,6 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
JsonNode commands =
doGetTyped("/api/device-connectivity/" + savedDevice.getId().getId(), new TypeReference<>() {});
assertThat(commands).hasSize(1);
assertThat(commands.get(COAP).get(LINUX).get(COAPS).asText()).isEqualTo(CHECK_DOCUMENTATION);
assertThat(commands.get(COAP).get(COAPS).asText()).isEqualTo(CHECK_DOCUMENTATION);
}
}

View File

@ -85,22 +85,27 @@ public class DeviceСonnectivityServiceImpl implements DeviceConnectivityService
ObjectNode commands = JacksonUtil.newObjectNode();
switch (transportType) {
case DEFAULT:
commands.set(HTTP, getHttpTransportPublishCommands(defaultHostname, creds));
commands.set(MQTT, getMqttTransportPublishCommands(defaultHostname, creds));
commands.set(COAP, getCoapTransportPublishCommands(defaultHostname, creds));
Optional.ofNullable(getHttpTransportPublishCommands(defaultHostname, creds))
.ifPresent(v -> commands.set(HTTP, v));
Optional.ofNullable(getMqttTransportPublishCommands(defaultHostname, creds))
.ifPresent(v -> commands.set(MQTT, v));
Optional.ofNullable(getCoapTransportPublishCommands(defaultHostname, creds))
.ifPresent(v -> commands.set(COAP, v));
break;
case MQTT:
MqttDeviceProfileTransportConfiguration transportConfiguration =
(MqttDeviceProfileTransportConfiguration) deviceProfile.getProfileData().getTransportConfiguration();
String topicName = transportConfiguration.getDeviceTelemetryTopic();
commands.set(MQTT, getMqttTransportPublishCommands(defaultHostname, topicName, creds));
Optional.ofNullable(getMqttTransportPublishCommands(defaultHostname, topicName, creds))
.ifPresent(v -> commands.set(MQTT, v));
break;
case COAP:
commands.set(COAP, getCoapTransportPublishCommands(defaultHostname, creds));
Optional.ofNullable(getCoapTransportPublishCommands(defaultHostname, creds))
.ifPresent(v -> commands.set(COAP, v));
break;
default:
commands.set(transportType.name(), JacksonUtil.toJsonNode(CHECK_DOCUMENTATION));
commands.put(transportType.name(), CHECK_DOCUMENTATION);
}
return commands;
}
@ -123,7 +128,7 @@ public class DeviceСonnectivityServiceImpl implements DeviceConnectivityService
.ifPresent(v -> httpCommands.put(HTTP, v));
Optional.ofNullable(getHttpPublishCommand(HTTPS, defaultHostname, deviceCredentials))
.ifPresent(v -> httpCommands.put(HTTPS, v));
return httpCommands;
return httpCommands.isEmpty() ? null : httpCommands;
}
private String getHttpPublishCommand(String protocol, String defaultHostname, DeviceCredentials deviceCredentials) {
@ -145,32 +150,22 @@ public class DeviceСonnectivityServiceImpl implements DeviceConnectivityService
private JsonNode getMqttTransportPublishCommands(String defaultHostname, String topic, DeviceCredentials deviceCredentials) {
ObjectNode mqttCommands = JacksonUtil.newObjectNode();
ObjectNode linuxMqttCommands = JacksonUtil.newObjectNode();
Optional.ofNullable(getMqttPublishCommand(LINUX, MQTT, defaultHostname, topic, deviceCredentials))
.ifPresent(v -> linuxMqttCommands.put(MQTT, v));
Optional.ofNullable(getMqttPublishCommand(LINUX, MQTTS, defaultHostname, topic, deviceCredentials))
.ifPresent(v -> linuxMqttCommands.put(MQTTS, v));
ObjectNode windowsMqttCommands = JacksonUtil.newObjectNode();
Optional.ofNullable(getMqttPublishCommand(WINDOWS, MQTT, defaultHostname, topic, deviceCredentials))
.ifPresent(v -> windowsMqttCommands.put(MQTT, v));
Optional.ofNullable(getMqttPublishCommand(WINDOWS, MQTTS, defaultHostname, topic, deviceCredentials))
.ifPresent(v -> windowsMqttCommands.put(MQTTS, v));
Optional.ofNullable(getMqttPublishCommand(MQTT, defaultHostname, topic, deviceCredentials))
.ifPresent(v -> mqttCommands.put(MQTT, v));
Optional.ofNullable(getMqttPublishCommand(MQTTS, defaultHostname, topic, deviceCredentials))
.ifPresent(v -> mqttCommands.put(MQTTS, v));
ObjectNode dockerMqttCommands = JacksonUtil.newObjectNode();
Optional.ofNullable(getMqttPublishCommand(DOCKER, MQTT, defaultHostname, topic, deviceCredentials))
Optional.ofNullable(getDockerMqttPublishCommand(MQTT, defaultHostname, topic, deviceCredentials))
.ifPresent(v -> dockerMqttCommands.put(MQTT, v));
Optional.ofNullable(getMqttPublishCommand(DOCKER, MQTTS, defaultHostname, topic, deviceCredentials))
Optional.ofNullable(getDockerMqttPublishCommand(MQTTS, defaultHostname, topic, deviceCredentials))
.ifPresent(v -> dockerMqttCommands.put(MQTTS, v));
mqttCommands.set(LINUX, linuxMqttCommands);
mqttCommands.set(WINDOWS, windowsMqttCommands);
mqttCommands.set(DOCKER, dockerMqttCommands);
return mqttCommands;
return mqttCommands.isEmpty() ? null : mqttCommands;
}
private String getMqttPublishCommand(String os, String protocol, String defaultHostname, String deviceTelemetryTopic, DeviceCredentials deviceCredentials) {
private String getMqttPublishCommand(String protocol, String defaultHostname, String deviceTelemetryTopic, DeviceCredentials deviceCredentials) {
if (MQTTS.equals(protocol) && deviceCredentials.getCredentialsType() == DeviceCredentialsType.X509_CERTIFICATE) {
return CHECK_DOCUMENTATION;
}
@ -180,29 +175,31 @@ public class DeviceСonnectivityServiceImpl implements DeviceConnectivityService
}
String mqttHost = properties.getHost().isEmpty() ? defaultHostname : properties.getHost();
String mqttPort = properties.getPort().isEmpty() ? null : properties.getPort();
switch (os) {
case LINUX:
return getMosquittoPubPublishCommand(protocol, mqttHost, mqttPort, deviceTelemetryTopic, deviceCredentials);
case WINDOWS:
return getMosquittoPubPublishCommand(protocol, mqttHost, mqttPort, deviceTelemetryTopic, deviceCredentials);
case DOCKER:
return getDockerMosquittoClientsPublishCommand(protocol, mqttHost, mqttPort, deviceTelemetryTopic, deviceCredentials);
default:
throw new IllegalArgumentException("Unsupported operating system: " + os);
}
private String getDockerMqttPublishCommand(String protocol, String defaultHostname, String deviceTelemetryTopic, DeviceCredentials deviceCredentials) {
if (MQTTS.equals(protocol) && deviceCredentials.getCredentialsType() == DeviceCredentialsType.X509_CERTIFICATE) {
return CHECK_DOCUMENTATION;
}
DeviceConnectivityInfo properties = deviceConnectivityConfiguration.getConnectivity().get(protocol);
if (properties == null || !properties.getEnabled()) {
return null;
}
String mqttHost = properties.getHost().isEmpty() ? defaultHostname : properties.getHost();
String mqttPort = properties.getPort().isEmpty() ? null : properties.getPort();
return getDockerMosquittoClientsPublishCommand(protocol, mqttHost, mqttPort, deviceTelemetryTopic, deviceCredentials);
}
private JsonNode getCoapTransportPublishCommands(String defaultHostname, DeviceCredentials deviceCredentials) {
ObjectNode coapCommands = JacksonUtil.newObjectNode();
ObjectNode linuxCoapCommands = JacksonUtil.newObjectNode();
Optional.ofNullable(getCoapPublishCommand(LINUX, COAP, defaultHostname, deviceCredentials))
.ifPresent(v -> linuxCoapCommands.put(COAP, v));
.ifPresent(v -> coapCommands.put(COAP, v));
Optional.ofNullable(getCoapPublishCommand(LINUX, COAPS, defaultHostname, deviceCredentials))
.ifPresent(v -> linuxCoapCommands.put(COAPS, v));
.ifPresent(v -> coapCommands.put(COAPS, v));
coapCommands.set(LINUX, linuxCoapCommands);
return coapCommands;
return coapCommands.isEmpty() ? null : coapCommands;
}
private String getCoapPublishCommand(String os, String protocol, String defaultHostname, DeviceCredentials deviceCredentials) {