10 changed files with 300 additions and 27 deletions
@ -0,0 +1,144 @@ |
|||||
|
/** |
||||
|
* 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.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.annotation.DirtiesContext; |
||||
|
import org.springframework.test.context.ContextConfiguration; |
||||
|
import org.springframework.test.context.TestPropertySource; |
||||
|
import org.thingsboard.common.util.JacksonUtil; |
||||
|
import org.thingsboard.common.util.ThingsBoardExecutors; |
||||
|
import org.thingsboard.server.common.data.Device; |
||||
|
import org.thingsboard.server.common.data.DeviceProfile; |
||||
|
import org.thingsboard.server.common.data.DeviceProfileType; |
||||
|
import org.thingsboard.server.common.data.DeviceTransportType; |
||||
|
import org.thingsboard.server.common.data.Tenant; |
||||
|
import org.thingsboard.server.common.data.User; |
||||
|
import org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials; |
||||
|
import org.thingsboard.server.common.data.device.profile.CoapDeviceProfileTransportConfiguration; |
||||
|
import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration; |
||||
|
import org.thingsboard.server.common.data.device.profile.DeviceProfileData; |
||||
|
import org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration; |
||||
|
import org.thingsboard.server.common.data.id.DeviceProfileId; |
||||
|
import org.thingsboard.server.common.data.security.Authority; |
||||
|
import org.thingsboard.server.common.data.security.DeviceCredentials; |
||||
|
import org.thingsboard.server.common.data.security.DeviceCredentialsType; |
||||
|
import org.thingsboard.server.dao.device.DeviceDao; |
||||
|
import org.thingsboard.server.dao.service.DaoSqlTest; |
||||
|
|
||||
|
import java.nio.file.Files; |
||||
|
import java.nio.file.Path; |
||||
|
|
||||
|
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.COAP; |
||||
|
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.COAPS; |
||||
|
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.DOCKER; |
||||
|
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.HTTP; |
||||
|
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.HTTPS; |
||||
|
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.MQTT; |
||||
|
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.MQTTS; |
||||
|
import static org.thingsboard.server.dao.util.DeviceConnectivityUtil.PEM_CERT_FILE_NAME; |
||||
|
|
||||
|
@TestPropertySource(properties = { |
||||
|
"device.connectivity.https.enabled=true", |
||||
|
"device.connectivity.http.port=80", |
||||
|
"device.connectivity.mqtt.enabled=false", |
||||
|
"device.connectivity.mqtts.enabled=false", |
||||
|
"device.connectivity.coap.enabled=false", |
||||
|
"device.connectivity.coaps.enabled=false", |
||||
|
}) |
||||
|
@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(); |
||||
|
|
||||
|
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())); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,119 @@ |
|||||
|
/** |
||||
|
* 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.service.install; |
||||
|
|
||||
|
import com.fasterxml.jackson.databind.JsonNode; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.junit.jupiter.api.BeforeEach; |
||||
|
import org.junit.jupiter.api.Test; |
||||
|
import org.springframework.boot.test.context.SpringBootTest; |
||||
|
import org.springframework.boot.test.mock.mockito.MockBean; |
||||
|
import org.springframework.boot.test.mock.mockito.SpyBean; |
||||
|
import org.thingsboard.common.util.JacksonUtil; |
||||
|
import org.thingsboard.server.common.data.id.RuleChainId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.rule.RuleChain; |
||||
|
import org.thingsboard.server.common.data.rule.RuleChainMetaData; |
||||
|
import org.thingsboard.server.dao.dashboard.DashboardService; |
||||
|
import org.thingsboard.server.dao.oauth2.OAuth2ConfigTemplateService; |
||||
|
import org.thingsboard.server.dao.resource.ResourceService; |
||||
|
import org.thingsboard.server.dao.rule.RuleChainService; |
||||
|
import org.thingsboard.server.dao.service.validator.RuleChainDataValidator; |
||||
|
import org.thingsboard.server.dao.tenant.TenantService; |
||||
|
import org.thingsboard.server.dao.usagerecord.ApiLimitService; |
||||
|
import org.thingsboard.server.dao.widget.WidgetTypeService; |
||||
|
import org.thingsboard.server.dao.widget.WidgetsBundleService; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.nio.file.Path; |
||||
|
import java.util.Collections; |
||||
|
import java.util.List; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
import static org.mockito.ArgumentMatchers.any; |
||||
|
import static org.mockito.BDDMockito.willReturn; |
||||
|
|
||||
|
@Slf4j |
||||
|
@SpringBootTest(classes = {InstallScripts.class, RuleChainDataValidator.class}) |
||||
|
class InstallScriptsTest { |
||||
|
|
||||
|
@MockBean |
||||
|
RuleChainService ruleChainService; |
||||
|
@MockBean |
||||
|
DashboardService dashboardService; |
||||
|
@MockBean |
||||
|
WidgetTypeService widgetTypeService; |
||||
|
@MockBean |
||||
|
WidgetsBundleService widgetsBundleService; |
||||
|
@MockBean |
||||
|
OAuth2ConfigTemplateService oAuth2TemplateService; |
||||
|
@MockBean |
||||
|
ResourceService resourceService; |
||||
|
@SpyBean |
||||
|
InstallScripts installScripts; |
||||
|
|
||||
|
@MockBean |
||||
|
TenantService tenantService; |
||||
|
@MockBean |
||||
|
ApiLimitService apiLimitService; |
||||
|
@SpyBean |
||||
|
RuleChainDataValidator ruleChainValidator; |
||||
|
TenantId tenantId = TenantId.fromUUID(UUID.fromString("9ef79cdf-37a8-4119-b682-2e7ed4e018da")); |
||||
|
|
||||
|
@BeforeEach |
||||
|
void setUp() { |
||||
|
willReturn(true).given(tenantService).tenantExists(tenantId); |
||||
|
willReturn(true).given(apiLimitService).checkEntitiesLimit(any(), any()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void testDefaultRuleChainsTemplates() throws IOException { |
||||
|
Path dir = installScripts.getTenantRuleChainsDir(); |
||||
|
installScripts.findRuleChainsFromPath(dir) |
||||
|
.forEach(this::validateRuleChainTemplate); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void testDefaultEdgeRuleChainsTemplates() throws IOException { |
||||
|
Path dir = installScripts.getEdgeRuleChainsDir(); |
||||
|
installScripts.findRuleChainsFromPath(dir) |
||||
|
.forEach(this::validateRuleChainTemplate); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void testDeviceProfileDefaultRuleChainTemplate() { |
||||
|
validateRuleChainTemplate(installScripts.getDeviceProfileDefaultRuleChainTemplateFilePath()); |
||||
|
} |
||||
|
|
||||
|
private void validateRuleChainTemplate(Path templateFilePath) { |
||||
|
log.warn("validateRuleChainTemplate {}", templateFilePath); |
||||
|
JsonNode ruleChainJson = JacksonUtil.toJsonNode(templateFilePath.toFile()); |
||||
|
|
||||
|
RuleChain ruleChain = JacksonUtil.treeToValue(ruleChainJson.get("ruleChain"), RuleChain.class); |
||||
|
ruleChain.setTenantId(tenantId); |
||||
|
ruleChainValidator.validate(ruleChain, RuleChain::getTenantId); |
||||
|
ruleChain.setId(new RuleChainId(UUID.randomUUID())); |
||||
|
|
||||
|
RuleChainMetaData ruleChainMetaData = JacksonUtil.treeToValue(ruleChainJson.get("metadata"), RuleChainMetaData.class); |
||||
|
ruleChainMetaData.setRuleChainId(ruleChain.getId()); |
||||
|
List<Throwable> throwables = RuleChainDataValidator.validateMetaData(ruleChainMetaData); |
||||
|
|
||||
|
assertThat(throwables).as("templateFilePath " + templateFilePath) |
||||
|
.containsExactlyInAnyOrderElementsOf(Collections.emptyList()); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue