Merge pull request #9587 from dashevchenko/dockerCommandFix
Fixed device connectivity docker command for localhost
This commit is contained in:
commit
bca383eb4c
@ -24,7 +24,6 @@ import org.junit.After;
|
|||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
|
||||||
import org.mockito.AdditionalAnswers;
|
import org.mockito.AdditionalAnswers;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
@ -100,8 +99,6 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
|
|||||||
"C3swkUdrR3pezg==\n" +
|
"C3swkUdrR3pezg==\n" +
|
||||||
"-----END PRIVATE KEY-----\n";
|
"-----END PRIVATE KEY-----\n";
|
||||||
|
|
||||||
ListeningExecutorService executor;
|
|
||||||
|
|
||||||
private Tenant savedTenant;
|
private Tenant savedTenant;
|
||||||
private User tenantAdmin;
|
private User tenantAdmin;
|
||||||
private DeviceProfileId mqttDeviceProfileId;
|
private DeviceProfileId mqttDeviceProfileId;
|
||||||
@ -117,8 +114,6 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void beforeTest() throws Exception {
|
public void beforeTest() throws Exception {
|
||||||
executor = MoreExecutors.listeningDecorator(ThingsBoardExecutors.newWorkStealingPool(8, getClass()));
|
|
||||||
|
|
||||||
loginSysAdmin();
|
loginSysAdmin();
|
||||||
|
|
||||||
ObjectNode config = JacksonUtil.newObjectNode();
|
ObjectNode config = JacksonUtil.newObjectNode();
|
||||||
@ -208,8 +203,6 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
|
|||||||
|
|
||||||
@After
|
@After
|
||||||
public void afterTest() throws Exception {
|
public void afterTest() throws Exception {
|
||||||
executor.shutdownNow();
|
|
||||||
|
|
||||||
loginSysAdmin();
|
loginSysAdmin();
|
||||||
|
|
||||||
doDelete("/api/tenant/" + savedTenant.getId().getId())
|
doDelete("/api/tenant/" + savedTenant.getId().getId())
|
||||||
@ -248,10 +241,10 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
|
|||||||
"-t v1/devices/me/telemetry -u \"%s\" -m \"{temperature:25}\"", credentials.getCredentialsId()));
|
"-t v1/devices/me/telemetry -u \"%s\" -m \"{temperature:25}\"", credentials.getCredentialsId()));
|
||||||
|
|
||||||
JsonNode dockerMqttCommands = commands.get(MQTT).get(DOCKER);
|
JsonNode dockerMqttCommands = commands.get(MQTT).get(DOCKER);
|
||||||
assertThat(dockerMqttCommands.get(MQTT).asText()).isEqualTo(String.format("docker run --rm -it thingsboard/mosquitto-clients mosquitto_pub -d -q 1 -h localhost" +
|
assertThat(dockerMqttCommands.get(MQTT).asText()).isEqualTo(String.format("docker run --rm -it --network=host thingsboard/mosquitto-clients mosquitto_pub -d -q 1 -h localhost" +
|
||||||
" -p 1883 -t v1/devices/me/telemetry -u \"%s\" -m \"{temperature:25}\"",
|
" -p 1883 -t v1/devices/me/telemetry -u \"%s\" -m \"{temperature:25}\"",
|
||||||
credentials.getCredentialsId()));
|
credentials.getCredentialsId()));
|
||||||
assertThat(dockerMqttCommands.get(MQTTS).asText()).isEqualTo(String.format("docker run --rm -it thingsboard/mosquitto-clients " +
|
assertThat(dockerMqttCommands.get(MQTTS).asText()).isEqualTo(String.format("docker run --rm -it --network=host thingsboard/mosquitto-clients " +
|
||||||
"/bin/sh -c \"curl -f -S -o ca-root.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download && " +
|
"/bin/sh -c \"curl -f -S -o ca-root.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download && " +
|
||||||
"mosquitto_pub -d -q 1 --cafile ca-root.pem -h localhost -p 8883 -t v1/devices/me/telemetry -u \"%s\" -m \"{temperature:25}\"\"",
|
"mosquitto_pub -d -q 1 --cafile ca-root.pem -h localhost -p 8883 -t v1/devices/me/telemetry -u \"%s\" -m \"{temperature:25}\"\"",
|
||||||
credentials.getCredentialsId()));
|
credentials.getCredentialsId()));
|
||||||
@ -261,6 +254,12 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
|
|||||||
"-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" +
|
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()));
|
||||||
|
|
||||||
|
JsonNode dockerCoapCommands = commands.get(COAP).get(DOCKER);
|
||||||
|
assertThat(dockerCoapCommands.get(COAP).asText()).isEqualTo(String.format("docker run --rm -it --network=host" +
|
||||||
|
" thingsboard/coap-clients coap-client -m POST coap://localhost:5683/api/v1/%s/telemetry -t json -e \"{temperature:25}\"", credentials.getCredentialsId()));
|
||||||
|
assertThat(dockerCoapCommands.get(COAPS).asText()).isEqualTo(String.format("docker run --rm -it --network=host" +
|
||||||
|
" thingsboard/coap-clients coap-client-openssl -m POST coaps://localhost:5684/api/v1/%s/telemetry -t json -e \"{temperature:25}\"", credentials.getCredentialsId()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -286,10 +285,10 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
|
|||||||
"-t %s -u \"%s\" -m \"{temperature:25}\"", DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId()));
|
"-t %s -u \"%s\" -m \"{temperature:25}\"", DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId()));
|
||||||
|
|
||||||
JsonNode dockerMqttCommands = commands.get(MQTT).get(DOCKER);
|
JsonNode dockerMqttCommands = commands.get(MQTT).get(DOCKER);
|
||||||
assertThat(dockerMqttCommands.get(MQTT).asText()).isEqualTo(String.format("docker run --rm -it thingsboard/mosquitto-clients mosquitto_pub -d -q 1 -h localhost" +
|
assertThat(dockerMqttCommands.get(MQTT).asText()).isEqualTo(String.format("docker run --rm -it --network=host thingsboard/mosquitto-clients mosquitto_pub -d -q 1 -h localhost" +
|
||||||
" -p 1883 -t %s -u \"%s\" -m \"{temperature:25}\"",
|
" -p 1883 -t %s -u \"%s\" -m \"{temperature:25}\"",
|
||||||
DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId()));
|
DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId()));
|
||||||
assertThat(dockerMqttCommands.get(MQTTS).asText()).isEqualTo(String.format("docker run --rm -it thingsboard/mosquitto-clients " +
|
assertThat(dockerMqttCommands.get(MQTTS).asText()).isEqualTo(String.format("docker run --rm -it --network=host thingsboard/mosquitto-clients " +
|
||||||
"/bin/sh -c \"curl -f -S -o ca-root.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download && " +
|
"/bin/sh -c \"curl -f -S -o ca-root.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download && " +
|
||||||
"mosquitto_pub -d -q 1 --cafile ca-root.pem -h localhost -p 8883 -t %s -u \"%s\" -m \"{temperature:25}\"\"",
|
"mosquitto_pub -d -q 1 --cafile ca-root.pem -h localhost -p 8883 -t %s -u \"%s\" -m \"{temperature:25}\"\"",
|
||||||
DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId()));
|
DEVICE_TELEMETRY_TOPIC, credentials.getCredentialsId()));
|
||||||
@ -330,10 +329,10 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
|
|||||||
"-t %s -i \"%s\" -u \"%s\" -P \"%s\" -m \"{temperature:25}\"", DEVICE_TELEMETRY_TOPIC, clientId, userName, password));
|
"-t %s -i \"%s\" -u \"%s\" -P \"%s\" -m \"{temperature:25}\"", DEVICE_TELEMETRY_TOPIC, clientId, userName, password));
|
||||||
|
|
||||||
JsonNode dockerMqttCommands = commands.get(MQTT).get(DOCKER);
|
JsonNode dockerMqttCommands = commands.get(MQTT).get(DOCKER);
|
||||||
assertThat(dockerMqttCommands.get(MQTT).asText()).isEqualTo(String.format("docker run --rm -it thingsboard/mosquitto-clients mosquitto_pub -d -q 1 -h localhost" +
|
assertThat(dockerMqttCommands.get(MQTT).asText()).isEqualTo(String.format("docker run --rm -it --network=host thingsboard/mosquitto-clients mosquitto_pub -d -q 1 -h localhost" +
|
||||||
" -p 1883 -t %s -i \"%s\" -u \"%s\" -P \"%s\" -m \"{temperature:25}\"",
|
" -p 1883 -t %s -i \"%s\" -u \"%s\" -P \"%s\" -m \"{temperature:25}\"",
|
||||||
DEVICE_TELEMETRY_TOPIC, clientId, userName, password));
|
DEVICE_TELEMETRY_TOPIC, clientId, userName, password));
|
||||||
assertThat(dockerMqttCommands.get(MQTTS).asText()).isEqualTo(String.format("docker run --rm -it thingsboard/mosquitto-clients " +
|
assertThat(dockerMqttCommands.get(MQTTS).asText()).isEqualTo(String.format("docker run --rm -it --network=host thingsboard/mosquitto-clients " +
|
||||||
"/bin/sh -c \"curl -f -S -o ca-root.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download && " +
|
"/bin/sh -c \"curl -f -S -o ca-root.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download && " +
|
||||||
"mosquitto_pub -d -q 1 --cafile ca-root.pem -h localhost -p 8883 -t %s -i \"%s\" -u \"%s\" -P \"%s\" -m \"{temperature:25}\"\"",
|
"mosquitto_pub -d -q 1 --cafile ca-root.pem -h localhost -p 8883 -t %s -i \"%s\" -u \"%s\" -P \"%s\" -m \"{temperature:25}\"\"",
|
||||||
DEVICE_TELEMETRY_TOPIC, clientId, userName, password));
|
DEVICE_TELEMETRY_TOPIC, clientId, userName, password));
|
||||||
@ -445,4 +444,172 @@ public class DeviceConnectivityControllerTest extends AbstractControllerTest {
|
|||||||
public void testDownloadCertWithUnknownProtocol() throws Exception {
|
public void testDownloadCertWithUnknownProtocol() throws Exception {
|
||||||
doGet("/api/device-connectivity/unknownProtocol/certificate/download").andExpect(status().isNotFound());
|
doGet("/api/device-connectivity/unknownProtocol/certificate/download").andExpect(status().isNotFound());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFetchPublishTelemetryCommandsForDefaultDeviceIfPortsSetToDefault() throws Exception {
|
||||||
|
loginSysAdmin();
|
||||||
|
|
||||||
|
ObjectNode config = JacksonUtil.newObjectNode();
|
||||||
|
|
||||||
|
ObjectNode http = JacksonUtil.newObjectNode();
|
||||||
|
http.put("enabled", true);
|
||||||
|
http.put("host", "");
|
||||||
|
http.put("port", 80);
|
||||||
|
config.set("http", http);
|
||||||
|
|
||||||
|
ObjectNode https = JacksonUtil.newObjectNode();
|
||||||
|
https.put("enabled", true);
|
||||||
|
https.put("host", "");
|
||||||
|
https.put("port", 443);
|
||||||
|
config.set("https", https);
|
||||||
|
|
||||||
|
ObjectNode mqtt = JacksonUtil.newObjectNode();
|
||||||
|
mqtt.put("enabled", false);
|
||||||
|
mqtt.put("host", "");
|
||||||
|
mqtt.put("port", 1883);
|
||||||
|
config.set("mqtt", mqtt);
|
||||||
|
|
||||||
|
ObjectNode mqtts = JacksonUtil.newObjectNode();
|
||||||
|
mqtts.put("enabled", false);
|
||||||
|
mqtts.put("host", "");
|
||||||
|
mqtts.put("port", 8883);
|
||||||
|
config.set("mqtts", mqtts);
|
||||||
|
|
||||||
|
ObjectNode coap = JacksonUtil.newObjectNode();
|
||||||
|
coap.put("enabled", false);
|
||||||
|
coap.put("host", "");
|
||||||
|
coap.put("port", 5683);
|
||||||
|
config.set("coap", coap);
|
||||||
|
|
||||||
|
ObjectNode coaps = JacksonUtil.newObjectNode();
|
||||||
|
coaps.put("enabled", false);
|
||||||
|
coaps.put("host", "");
|
||||||
|
coaps.put("port", 5684);
|
||||||
|
config.set("coaps", coaps);
|
||||||
|
|
||||||
|
AdminSettings adminSettings = doGet("/api/admin/settings/connectivity", AdminSettings.class);
|
||||||
|
adminSettings.setJsonValue(config);
|
||||||
|
doPost("/api/admin/settings", adminSettings).andExpect(status().isOk());
|
||||||
|
|
||||||
|
login("tenant2@thingsboard.org", "testPassword1");
|
||||||
|
|
||||||
|
Device device = new Device();
|
||||||
|
device.setName("My device");
|
||||||
|
device.setType("default");
|
||||||
|
Device savedDevice = doPost("/api/device", device, Device.class);
|
||||||
|
JsonNode commands =
|
||||||
|
doGetTyped("/api/device-connectivity/" + savedDevice.getId().getId(), new TypeReference<>() {
|
||||||
|
});
|
||||||
|
|
||||||
|
DeviceCredentials credentials =
|
||||||
|
doGet("/api/device/" + savedDevice.getId().getId() + "/credentials", DeviceCredentials.class);
|
||||||
|
|
||||||
|
assertThat(commands).hasSize(1);
|
||||||
|
JsonNode httpCommands = commands.get(HTTP);
|
||||||
|
assertThat(httpCommands.get(HTTP).asText()).isEqualTo(String.format("curl -v -X POST http://localhost/api/v1/%s/telemetry " +
|
||||||
|
"--header Content-Type:application/json --data \"{temperature:25}\"",
|
||||||
|
credentials.getCredentialsId()));
|
||||||
|
assertThat(httpCommands.get(HTTPS).asText()).isEqualTo(String.format("curl -v -X POST https://localhost/api/v1/%s/telemetry " +
|
||||||
|
"--header Content-Type:application/json --data \"{temperature:25}\"",
|
||||||
|
credentials.getCredentialsId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFetchPublishTelemetryCommandsForDefaultDeviceIfHostIsNotLocalhost() throws Exception {
|
||||||
|
loginSysAdmin();
|
||||||
|
|
||||||
|
ObjectNode config = JacksonUtil.newObjectNode();
|
||||||
|
|
||||||
|
ObjectNode http = JacksonUtil.newObjectNode();
|
||||||
|
http.put("enabled", true);
|
||||||
|
http.put("host", "test.domain");
|
||||||
|
http.put("port", 8080);
|
||||||
|
config.set("http", http);
|
||||||
|
|
||||||
|
ObjectNode https = JacksonUtil.newObjectNode();
|
||||||
|
https.put("enabled", true);
|
||||||
|
https.put("host", "test.domain");
|
||||||
|
https.put("port", 443);
|
||||||
|
config.set("https", https);
|
||||||
|
|
||||||
|
ObjectNode mqtt = JacksonUtil.newObjectNode();
|
||||||
|
mqtt.put("enabled", true);
|
||||||
|
mqtt.put("host", "test.domain");
|
||||||
|
mqtt.put("port", 1883);
|
||||||
|
config.set("mqtt", mqtt);
|
||||||
|
|
||||||
|
ObjectNode mqtts = JacksonUtil.newObjectNode();
|
||||||
|
mqtts.put("enabled", true);
|
||||||
|
mqtts.put("host", "test.domain");
|
||||||
|
mqtts.put("port", 8883);
|
||||||
|
config.set("mqtts", mqtts);
|
||||||
|
|
||||||
|
ObjectNode coap = JacksonUtil.newObjectNode();
|
||||||
|
coap.put("enabled", true);
|
||||||
|
coap.put("host", "test.domain");
|
||||||
|
coap.put("port", 5683);
|
||||||
|
config.set("coap", coap);
|
||||||
|
|
||||||
|
ObjectNode coaps = JacksonUtil.newObjectNode();
|
||||||
|
coaps.put("enabled", true);
|
||||||
|
coaps.put("host", "test.domain");
|
||||||
|
coaps.put("port", 5684);
|
||||||
|
config.set("coaps", coaps);
|
||||||
|
|
||||||
|
AdminSettings adminSettings = doGet("/api/admin/settings/connectivity", AdminSettings.class);
|
||||||
|
adminSettings.setJsonValue(config);
|
||||||
|
doPost("/api/admin/settings", adminSettings).andExpect(status().isOk());
|
||||||
|
|
||||||
|
login("tenant2@thingsboard.org", "testPassword1");
|
||||||
|
|
||||||
|
Device device = new Device();
|
||||||
|
device.setName("My device");
|
||||||
|
device.setType("default");
|
||||||
|
Device savedDevice = doPost("/api/device", device, Device.class);
|
||||||
|
JsonNode commands =
|
||||||
|
doGetTyped("/api/device-connectivity/" + savedDevice.getId().getId(), new TypeReference<>() {
|
||||||
|
});
|
||||||
|
|
||||||
|
DeviceCredentials credentials =
|
||||||
|
doGet("/api/device/" + savedDevice.getId().getId() + "/credentials", DeviceCredentials.class);
|
||||||
|
|
||||||
|
assertThat(commands).hasSize(3);
|
||||||
|
JsonNode httpCommands = commands.get(HTTP);
|
||||||
|
assertThat(httpCommands.get(HTTP).asText()).isEqualTo(String.format("curl -v -X POST http://test.domain:8080/api/v1/%s/telemetry " +
|
||||||
|
"--header Content-Type:application/json --data \"{temperature:25}\"",
|
||||||
|
credentials.getCredentialsId()));
|
||||||
|
assertThat(httpCommands.get(HTTPS).asText()).isEqualTo(String.format("curl -v -X POST https://test.domain/api/v1/%s/telemetry " +
|
||||||
|
"--header Content-Type:application/json --data \"{temperature:25}\"",
|
||||||
|
credentials.getCredentialsId()));
|
||||||
|
|
||||||
|
|
||||||
|
JsonNode mqttCommands = commands.get(MQTT);
|
||||||
|
assertThat(mqttCommands.get(MQTT).asText()).isEqualTo(String.format("mosquitto_pub -d -q 1 -h test.domain -p 1883 -t v1/devices/me/telemetry " +
|
||||||
|
"-u \"%s\" -m \"{temperature:25}\"",
|
||||||
|
credentials.getCredentialsId()));
|
||||||
|
assertThat(mqttCommands.get(MQTTS).get(0).asText()).isEqualTo("curl -f -S -o ca-root.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.pem -h test.domain -p 8883 " +
|
||||||
|
"-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 --rm -it thingsboard/mosquitto-clients mosquitto_pub -d -q 1 -h test.domain" +
|
||||||
|
" -p 1883 -t v1/devices/me/telemetry -u \"%s\" -m \"{temperature:25}\"",
|
||||||
|
credentials.getCredentialsId()));
|
||||||
|
assertThat(dockerMqttCommands.get(MQTTS).asText()).isEqualTo(String.format("docker run --rm -it thingsboard/mosquitto-clients " +
|
||||||
|
"/bin/sh -c \"curl -f -S -o ca-root.pem http://localhost:80/api/device-connectivity/mqtts/certificate/download && " +
|
||||||
|
"mosquitto_pub -d -q 1 --cafile ca-root.pem -h test.domain -p 8883 -t v1/devices/me/telemetry -u \"%s\" -m \"{temperature:25}\"\"",
|
||||||
|
credentials.getCredentialsId()));
|
||||||
|
|
||||||
|
JsonNode linuxCoapCommands = commands.get(COAP);
|
||||||
|
assertThat(linuxCoapCommands.get(COAP).asText()).isEqualTo(String.format("coap-client -m POST coap://test.domain:5683/api/v1/%s/telemetry " +
|
||||||
|
"-t json -e \"{temperature:25}\"", credentials.getCredentialsId()));
|
||||||
|
assertThat(linuxCoapCommands.get(COAPS).asText()).isEqualTo(String.format("coap-client-openssl -m POST coaps://test.domain:5684/api/v1/%s/telemetry" +
|
||||||
|
" -t json -e \"{temperature:25}\"", credentials.getCredentialsId()));
|
||||||
|
|
||||||
|
JsonNode dockerCoapCommands = commands.get(COAP).get(DOCKER);
|
||||||
|
assertThat(dockerCoapCommands.get(COAP).asText()).isEqualTo(String.format("docker run --rm -it " +
|
||||||
|
"thingsboard/coap-clients coap-client -m POST coap://test.domain:5683/api/v1/%s/telemetry -t json -e \"{temperature:25}\"", credentials.getCredentialsId()));
|
||||||
|
assertThat(dockerCoapCommands.get(COAPS).asText()).isEqualTo(String.format("docker run --rm -it " +
|
||||||
|
"thingsboard/coap-clients coap-client-openssl -m POST coaps://test.domain:5684/api/v1/%s/telemetry -t json -e \"{temperature:25}\"", credentials.getCredentialsId()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,159 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright © 2016-2023 The Thingsboard Authors
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package org.thingsboard.server.controller;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
|
||||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
||||||
import com.google.common.util.concurrent.ListeningExecutorService;
|
|
||||||
import com.google.common.util.concurrent.MoreExecutors;
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.mockito.AdditionalAnswers;
|
|
||||||
import org.mockito.Mockito;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Primary;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
|
||||||
import org.thingsboard.common.util.JacksonUtil;
|
|
||||||
import org.thingsboard.common.util.ThingsBoardExecutors;
|
|
||||||
import org.thingsboard.server.common.data.AdminSettings;
|
|
||||||
import org.thingsboard.server.common.data.Device;
|
|
||||||
import org.thingsboard.server.common.data.Tenant;
|
|
||||||
import org.thingsboard.server.common.data.User;
|
|
||||||
import org.thingsboard.server.common.data.security.Authority;
|
|
||||||
import org.thingsboard.server.common.data.security.DeviceCredentials;
|
|
||||||
import org.thingsboard.server.dao.device.DeviceDao;
|
|
||||||
import org.thingsboard.server.dao.service.DaoSqlTest;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
||||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.HTTP;
|
|
||||||
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.HTTPS;
|
|
||||||
|
|
||||||
@ContextConfiguration(classes = {DeviceConnectivityControllerWithDefaultPortTest.Config.class})
|
|
||||||
@DaoSqlTest
|
|
||||||
public class DeviceConnectivityControllerWithDefaultPortTest extends AbstractControllerTest {
|
|
||||||
|
|
||||||
ListeningExecutorService executor;
|
|
||||||
|
|
||||||
private Tenant savedTenant;
|
|
||||||
|
|
||||||
static class Config {
|
|
||||||
@Bean
|
|
||||||
@Primary
|
|
||||||
public DeviceDao deviceDao(DeviceDao deviceDao) {
|
|
||||||
return Mockito.mock(DeviceDao.class, AdditionalAnswers.delegatesTo(deviceDao));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void beforeTest() throws Exception {
|
|
||||||
executor = MoreExecutors.listeningDecorator(ThingsBoardExecutors.newWorkStealingPool(8, getClass()));
|
|
||||||
|
|
||||||
loginSysAdmin();
|
|
||||||
|
|
||||||
ObjectNode config = JacksonUtil.newObjectNode();
|
|
||||||
|
|
||||||
ObjectNode http = JacksonUtil.newObjectNode();
|
|
||||||
http.put("enabled", true);
|
|
||||||
http.put("host", "");
|
|
||||||
http.put("port", 80);
|
|
||||||
config.set("http", http);
|
|
||||||
|
|
||||||
ObjectNode https = JacksonUtil.newObjectNode();
|
|
||||||
https.put("enabled", true);
|
|
||||||
https.put("host", "");
|
|
||||||
https.put("port", 443);
|
|
||||||
config.set("https", https);
|
|
||||||
|
|
||||||
ObjectNode mqtt = JacksonUtil.newObjectNode();
|
|
||||||
mqtt.put("enabled", false);
|
|
||||||
mqtt.put("host", "");
|
|
||||||
mqtt.put("port", 1883);
|
|
||||||
config.set("mqtt", mqtt);
|
|
||||||
|
|
||||||
ObjectNode mqtts = JacksonUtil.newObjectNode();
|
|
||||||
mqtts.put("enabled", false);
|
|
||||||
mqtts.put("host", "");
|
|
||||||
mqtts.put("port", 8883);
|
|
||||||
config.set("mqtts", mqtts);
|
|
||||||
|
|
||||||
ObjectNode coap = JacksonUtil.newObjectNode();
|
|
||||||
coap.put("enabled", false);
|
|
||||||
coap.put("host", "");
|
|
||||||
coap.put("port", 5683);
|
|
||||||
config.set("coap", coap);
|
|
||||||
|
|
||||||
ObjectNode coaps = JacksonUtil.newObjectNode();
|
|
||||||
coaps.put("enabled", false);
|
|
||||||
coaps.put("host", "");
|
|
||||||
coaps.put("port", 5684);
|
|
||||||
config.set("coaps", coaps);
|
|
||||||
|
|
||||||
AdminSettings adminSettings = doGet("/api/admin/settings/connectivity", AdminSettings.class);
|
|
||||||
adminSettings.setJsonValue(config);
|
|
||||||
doPost("/api/admin/settings", adminSettings).andExpect(status().isOk());
|
|
||||||
|
|
||||||
Tenant tenant = new Tenant();
|
|
||||||
tenant.setTitle("My tenant");
|
|
||||||
savedTenant = doPost("/api/tenant", tenant, Tenant.class);
|
|
||||||
Assert.assertNotNull(savedTenant);
|
|
||||||
|
|
||||||
User tenantAdmin = new User();
|
|
||||||
tenantAdmin.setAuthority(Authority.TENANT_ADMIN);
|
|
||||||
tenantAdmin.setTenantId(savedTenant.getId());
|
|
||||||
tenantAdmin.setEmail("tenant2@thingsboard.org");
|
|
||||||
tenantAdmin.setFirstName("Joe");
|
|
||||||
tenantAdmin.setLastName("Downs");
|
|
||||||
|
|
||||||
createUserAndLogin(tenantAdmin, "testPassword1");
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void afterTest() throws Exception {
|
|
||||||
executor.shutdownNow();
|
|
||||||
|
|
||||||
loginSysAdmin();
|
|
||||||
|
|
||||||
doDelete("/api/tenant/" + savedTenant.getId().getId())
|
|
||||||
.andExpect(status().isOk());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testFetchPublishTelemetryCommandsForDefaultDevice() throws Exception {
|
|
||||||
Device device = new Device();
|
|
||||||
device.setName("My device");
|
|
||||||
device.setType("default");
|
|
||||||
Device savedDevice = doPost("/api/device", device, Device.class);
|
|
||||||
JsonNode commands =
|
|
||||||
doGetTyped("/api/device-connectivity/" + savedDevice.getId().getId(), new TypeReference<>() {
|
|
||||||
});
|
|
||||||
|
|
||||||
DeviceCredentials credentials =
|
|
||||||
doGet("/api/device/" + savedDevice.getId().getId() + "/credentials", DeviceCredentials.class);
|
|
||||||
|
|
||||||
assertThat(commands).hasSize(1);
|
|
||||||
JsonNode httpCommands = commands.get(HTTP);
|
|
||||||
assertThat(httpCommands.get(HTTP).asText()).isEqualTo(String.format("curl -v -X POST http://localhost/api/v1/%s/telemetry " +
|
|
||||||
"--header Content-Type:application/json --data \"{temperature:25}\"",
|
|
||||||
credentials.getCredentialsId()));
|
|
||||||
assertThat(httpCommands.get(HTTPS).asText()).isEqualTo(String.format("curl -v -X POST https://localhost/api/v1/%s/telemetry " +
|
|
||||||
"--header Content-Type:application/json --data \"{temperature:25}\"",
|
|
||||||
credentials.getCredentialsId()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -20,6 +20,9 @@ import org.thingsboard.common.util.JacksonUtil;
|
|||||||
import org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials;
|
import org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials;
|
||||||
import org.thingsboard.server.common.data.security.DeviceCredentials;
|
import org.thingsboard.server.common.data.security.DeviceCredentials;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class DeviceConnectivityUtil {
|
public class DeviceConnectivityUtil {
|
||||||
|
|
||||||
public static final String HTTP = "http";
|
public static final String HTTP = "http";
|
||||||
@ -38,6 +41,7 @@ public class DeviceConnectivityUtil {
|
|||||||
public static final String GATEWAY_DOCKER_RUN = "docker run -it ";
|
public static final String GATEWAY_DOCKER_RUN = "docker run -it ";
|
||||||
public static final String MQTT_IMAGE = "thingsboard/mosquitto-clients ";
|
public static final String MQTT_IMAGE = "thingsboard/mosquitto-clients ";
|
||||||
public static final String COAP_IMAGE = "thingsboard/coap-clients ";
|
public static final String COAP_IMAGE = "thingsboard/coap-clients ";
|
||||||
|
public static final List<String> LOCAL_HOSTS = Arrays.asList("localhost", "127.0.0.1");
|
||||||
|
|
||||||
public static String getHttpPublishCommand(String protocol, String host, String port, DeviceCredentials deviceCredentials) {
|
public static String getHttpPublishCommand(String protocol, String host, String port, DeviceCredentials deviceCredentials) {
|
||||||
return String.format("curl -v -X POST %s://%s%s/api/v1/%s/telemetry --header Content-Type:application/json --data " + JSON_EXAMPLE_PAYLOAD,
|
return String.format("curl -v -X POST %s://%s%s/api/v1/%s/telemetry --header Content-Type:application/json --data " + JSON_EXAMPLE_PAYLOAD,
|
||||||
@ -135,7 +139,7 @@ public class DeviceConnectivityUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
StringBuilder mqttDockerCommand = new StringBuilder();
|
StringBuilder mqttDockerCommand = new StringBuilder();
|
||||||
mqttDockerCommand.append(DOCKER_RUN).append(MQTT_IMAGE);
|
mqttDockerCommand.append(DOCKER_RUN).append(LOCAL_HOSTS.contains(host) ? "--network=host ":"").append(MQTT_IMAGE);
|
||||||
|
|
||||||
if (MQTTS.equals(protocol)) {
|
if (MQTTS.equals(protocol)) {
|
||||||
mqttDockerCommand.append("/bin/sh -c \"")
|
mqttDockerCommand.append("/bin/sh -c \"")
|
||||||
@ -167,6 +171,6 @@ public class DeviceConnectivityUtil {
|
|||||||
|
|
||||||
public static String getDockerCoapPublishCommand(String protocol, String host, String port, DeviceCredentials deviceCredentials) {
|
public static String getDockerCoapPublishCommand(String protocol, String host, String port, DeviceCredentials deviceCredentials) {
|
||||||
String coapCommand = getCoapPublishCommand(protocol, host, port, deviceCredentials);
|
String coapCommand = getCoapPublishCommand(protocol, host, port, deviceCredentials);
|
||||||
return coapCommand != null ? String.format("%s%s%s", DOCKER_RUN, COAP_IMAGE, coapCommand) : null;
|
return coapCommand != null ? String.format("%s%s%s", DOCKER_RUN + (LOCAL_HOSTS.contains(host) ? "--network=host ":""), COAP_IMAGE, coapCommand) : null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user