Browse Source
/api/iot-hub/connectivity now returns, per enabled transport:
- hostSource: DEVICE_CONNECTIVITY when the host is set in the Device
connectivity settings, otherwise BASE_URL or REQUEST, depending on whether
the base URL came from General settings or from the request. The install
dialog uses it to decide whether the user may change the host.
- port: a blank port is replaced with the standard port of the protocol
(MQTT 1883, MQTTS 8883, HTTP 80, HTTPS 443, CoAP 5683, CoAPS 5684), as the
settings page promises. Before, packages got an empty ${mqtt.port} and the
generated sketch didn't compile.
hostSource is added to the response only; DeviceConnectivityInfo stays the
model of the stored settings.
feature/lts-4.2/iot-hub-devices
5 changed files with 139 additions and 11 deletions
@ -0,0 +1,15 @@ |
|||||
|
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
|
||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||
|
package org.thingsboard.server.dao.device; |
||||
|
|
||||
|
/** |
||||
|
* Where the host of a transport in the device connectivity info comes from. |
||||
|
*/ |
||||
|
public enum DeviceConnectivityHostSource { |
||||
|
/** Set explicitly in the Device connectivity settings. */ |
||||
|
DEVICE_CONNECTIVITY, |
||||
|
/** Taken from the Base URL in General settings. */ |
||||
|
BASE_URL, |
||||
|
/** Taken from the address of the request. */ |
||||
|
REQUEST |
||||
|
} |
||||
@ -0,0 +1,96 @@ |
|||||
|
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
|
||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||
|
package org.thingsboard.server.dao.device; |
||||
|
|
||||
|
import com.fasterxml.jackson.databind.JsonNode; |
||||
|
import org.junit.jupiter.api.Test; |
||||
|
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
import org.mockito.InjectMocks; |
||||
|
import org.mockito.Mock; |
||||
|
import org.mockito.junit.jupiter.MockitoExtension; |
||||
|
import org.thingsboard.common.util.JacksonUtil; |
||||
|
import org.thingsboard.server.common.data.AdminSettings; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.dao.settings.AdminSettingsService; |
||||
|
|
||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
import static org.mockito.Mockito.when; |
||||
|
|
||||
|
@ExtendWith(MockitoExtension.class) |
||||
|
class DeviceConnectivityServiceImplTest { |
||||
|
|
||||
|
private static final String BASE_URL = "http://192.168.1.10:8080"; |
||||
|
|
||||
|
@Mock |
||||
|
private AdminSettingsService adminSettingsService; |
||||
|
@Mock |
||||
|
private DeviceCredentialsService deviceCredentialsService; |
||||
|
@Mock |
||||
|
private DeviceProfileService deviceProfileService; |
||||
|
@InjectMocks |
||||
|
private DeviceConnectivityServiceImpl service; |
||||
|
|
||||
|
@Test |
||||
|
void configuredHostIsReportedAsDeviceConnectivity() throws Exception { |
||||
|
givenConnectivity(""" |
||||
|
{"mqtt": {"enabled": true, "host": "mqtt.example.com", "port": "1884"}}"""); |
||||
|
|
||||
|
JsonNode mqtt = service.getConnectivityInfo(BASE_URL, DeviceConnectivityHostSource.REQUEST).get("mqtt"); |
||||
|
|
||||
|
assertThat(mqtt.get("host").asText()).isEqualTo("mqtt.example.com"); |
||||
|
assertThat(mqtt.get("port").asText()).isEqualTo("1884"); |
||||
|
assertThat(mqtt.get("hostSource").asText()).isEqualTo("DEVICE_CONNECTIVITY"); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void blankHostTakesBaseUrlAndItsSource() throws Exception { |
||||
|
givenConnectivity(""" |
||||
|
{"mqtt": {"enabled": true, "host": "", "port": "1883"}}"""); |
||||
|
|
||||
|
JsonNode fromRequest = service.getConnectivityInfo(BASE_URL, DeviceConnectivityHostSource.REQUEST).get("mqtt"); |
||||
|
JsonNode fromBaseUrl = service.getConnectivityInfo(BASE_URL, DeviceConnectivityHostSource.BASE_URL).get("mqtt"); |
||||
|
|
||||
|
assertThat(fromRequest.get("host").asText()).isEqualTo("192.168.1.10"); |
||||
|
assertThat(fromRequest.get("hostSource").asText()).isEqualTo("REQUEST"); |
||||
|
assertThat(fromBaseUrl.get("hostSource").asText()).isEqualTo("BASE_URL"); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void blankPortIsReplacedWithStandardPort() throws Exception { |
||||
|
givenConnectivity(""" |
||||
|
{"http": {"enabled": true, "host": "", "port": ""}, |
||||
|
"https": {"enabled": true, "host": "", "port": ""}, |
||||
|
"mqtt": {"enabled": true, "host": "", "port": ""}, |
||||
|
"mqtts": {"enabled": true, "host": "", "port": ""}, |
||||
|
"coap": {"enabled": true, "host": "", "port": ""}, |
||||
|
"coaps": {"enabled": true, "host": "", "port": ""}}"""); |
||||
|
|
||||
|
JsonNode info = service.getConnectivityInfo(BASE_URL, DeviceConnectivityHostSource.REQUEST); |
||||
|
|
||||
|
assertThat(info.get("http").get("port").asText()).isEqualTo("80"); |
||||
|
assertThat(info.get("https").get("port").asText()).isEqualTo("443"); |
||||
|
assertThat(info.get("mqtt").get("port").asText()).isEqualTo("1883"); |
||||
|
assertThat(info.get("mqtts").get("port").asText()).isEqualTo("8883"); |
||||
|
assertThat(info.get("coap").get("port").asText()).isEqualTo("5683"); |
||||
|
assertThat(info.get("coaps").get("port").asText()).isEqualTo("5684"); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void disabledProtocolIsLeftOut() throws Exception { |
||||
|
givenConnectivity(""" |
||||
|
{"mqtt": {"enabled": true, "host": "", "port": "1883"}, |
||||
|
"coap": {"enabled": false, "host": "", "port": "5683"}}"""); |
||||
|
|
||||
|
JsonNode info = service.getConnectivityInfo(BASE_URL, DeviceConnectivityHostSource.REQUEST); |
||||
|
|
||||
|
assertThat(info.has("mqtt")).isTrue(); |
||||
|
assertThat(info.has("coap")).isFalse(); |
||||
|
} |
||||
|
|
||||
|
private void givenConnectivity(String json) { |
||||
|
AdminSettings settings = new AdminSettings(); |
||||
|
settings.setJsonValue(JacksonUtil.toJsonNode(json)); |
||||
|
when(adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, "connectivity")).thenReturn(settings); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue