diff --git a/application/src/test/java/org/thingsboard/server/edge/BaseDeviceEdgeTest.java b/application/src/test/java/org/thingsboard/server/edge/BaseDeviceEdgeTest.java index 98508e72ce..303e91ee80 100644 --- a/application/src/test/java/org/thingsboard/server/edge/BaseDeviceEdgeTest.java +++ b/application/src/test/java/org/thingsboard/server/edge/BaseDeviceEdgeTest.java @@ -690,7 +690,7 @@ abstract public class BaseDeviceEdgeTest extends AbstractEdgeTest { edgeImitator.sendUplinkMsg(uplinkMsgBuilder.build()); Assert.assertTrue(edgeImitator.waitForResponses()); - Assert.assertTrue(onUpdateCallback.getSubscribeLatch().await(5, TimeUnit.SECONDS)); + Assert.assertTrue(onUpdateCallback.getSubscribeLatch().await(30, TimeUnit.SECONDS)); Assert.assertEquals(JacksonUtil.OBJECT_MAPPER.createObjectNode().put(attrKey, attrValue), JacksonUtil.fromBytes(onUpdateCallback.getPayloadBytes())); diff --git a/application/src/test/java/org/thingsboard/server/service/resource/sql/BaseTbResourceServiceTest.java b/application/src/test/java/org/thingsboard/server/service/resource/sql/BaseTbResourceServiceTest.java index b0282832bc..e2c5985553 100644 --- a/application/src/test/java/org/thingsboard/server/service/resource/sql/BaseTbResourceServiceTest.java +++ b/application/src/test/java/org/thingsboard/server/service/resource/sql/BaseTbResourceServiceTest.java @@ -19,10 +19,8 @@ import com.datastax.oss.driver.api.core.uuid.Uuids; import org.junit.After; import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; import org.junit.jupiter.api.Assertions; -import org.junit.rules.ExpectedException; import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.server.common.data.EntityInfo; import org.thingsboard.server.common.data.ResourceType; @@ -46,6 +44,7 @@ import java.util.Base64; import java.util.Collections; import java.util.List; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @DaoSqlTest @@ -116,10 +115,6 @@ public class BaseTbResourceServiceTest extends AbstractControllerTest { .andExpect(status().isOk()); } - @SuppressWarnings("deprecation") - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test public void testSaveResourceWithMaxSumDataSizeOutOfLimit() throws Exception { loginSysAdmin(); @@ -138,9 +133,9 @@ public class BaseTbResourceServiceTest extends AbstractControllerTest { Assert.assertEquals(1, resourceService.sumDataSizeByTenantId(tenantId)); try { - thrown.expect(DataValidationException.class); - thrown.expectMessage(String.format("Failed to create the tb resource, files size limit is exhausted %d bytes!", limit)); - createResource("test1", 1 + DEFAULT_FILE_NAME); + assertThatThrownBy(() -> createResource("test1", 1 + DEFAULT_FILE_NAME)) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("Failed to create the tb resource, files size limit is exhausted %d bytes!", limit); } finally { defaultTenantProfile.getProfileData().setConfiguration(DefaultTenantProfileConfiguration.builder().maxResourcesInBytes(0).build()); loginSysAdmin(); diff --git a/application/src/test/java/org/thingsboard/server/transport/lwm2m/AbstractLwM2MIntegrationTest.java b/application/src/test/java/org/thingsboard/server/transport/lwm2m/AbstractLwM2MIntegrationTest.java index ef00a3050e..c92369e3c2 100644 --- a/application/src/test/java/org/thingsboard/server/transport/lwm2m/AbstractLwM2MIntegrationTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/lwm2m/AbstractLwM2MIntegrationTest.java @@ -70,7 +70,7 @@ import org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd; import org.thingsboard.server.transport.AbstractTransportIntegrationTest; import org.thingsboard.server.transport.lwm2m.client.LwM2MTestClient; import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientContext; -import org.thingsboard.server.transport.lwm2m.server.uplink.DefaultLwM2mUplinkMsgHandler; +import org.thingsboard.server.transport.lwm2m.server.uplink.LwM2mUplinkMsgHandler; import java.io.IOException; import java.net.ServerSocket; @@ -108,7 +108,7 @@ import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.LwM2MProfil public abstract class AbstractLwM2MIntegrationTest extends AbstractTransportIntegrationTest { @SpyBean - DefaultLwM2mUplinkMsgHandler defaultLwM2mUplinkMsgHandlerTest; + LwM2mUplinkMsgHandler defaultLwM2mUplinkMsgHandlerTest; @Autowired private LwM2mClientContext clientContextTest; diff --git a/application/src/test/java/org/thingsboard/server/transport/lwm2m/client/LwM2MTestClient.java b/application/src/test/java/org/thingsboard/server/transport/lwm2m/client/LwM2MTestClient.java index 7cb5f88733..449ac4c2ae 100644 --- a/application/src/test/java/org/thingsboard/server/transport/lwm2m/client/LwM2MTestClient.java +++ b/application/src/test/java/org/thingsboard/server/transport/lwm2m/client/LwM2MTestClient.java @@ -45,7 +45,7 @@ import org.junit.Assert; import org.mockito.Mockito; import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient; import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientContext; -import org.thingsboard.server.transport.lwm2m.server.uplink.DefaultLwM2mUplinkMsgHandler; +import org.thingsboard.server.transport.lwm2m.server.uplink.LwM2mUplinkMsgHandler; import org.thingsboard.server.transport.lwm2m.utils.LwM2mValueConverterImpl; import java.io.IOException; @@ -109,12 +109,12 @@ public class LwM2MTestClient { private LwM2MLocationParams locationParams; private LwM2mTemperatureSensor lwM2MTemperatureSensor; private Set clientStates; - private DefaultLwM2mUplinkMsgHandler defaultLwM2mUplinkMsgHandlerTest; + private LwM2mUplinkMsgHandler defaultLwM2mUplinkMsgHandlerTest; private LwM2mClientContext clientContext; public void init(Security security, Configuration coapConfig, int port, boolean isRpc, boolean isBootstrap, int shortServerId, int shortServerIdBs, Security securityBs, - DefaultLwM2mUplinkMsgHandler defaultLwM2mUplinkMsgHandler, + LwM2mUplinkMsgHandler defaultLwM2mUplinkMsgHandler, LwM2mClientContext clientContext) throws InvalidDDFFileException, IOException { Assert.assertNull("client already initialized", leshanClient); this.defaultLwM2mUplinkMsgHandlerTest = defaultLwM2mUplinkMsgHandler; diff --git a/application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/attributes/AbstractMqttV5ClientSparkplugAttributesTest.java b/application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/attributes/AbstractMqttV5ClientSparkplugAttributesTest.java index 04349d0672..4e05e44668 100644 --- a/application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/attributes/AbstractMqttV5ClientSparkplugAttributesTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/attributes/AbstractMqttV5ClientSparkplugAttributesTest.java @@ -20,6 +20,7 @@ import io.netty.handler.codec.mqtt.MqttQoS; import lombok.extern.slf4j.Slf4j; import org.junit.Assert; import org.thingsboard.server.common.data.Device; +import org.thingsboard.server.common.msg.session.FeatureType; import org.thingsboard.server.transport.mqtt.sparkplug.AbstractMqttV5ClientSparkplugTest; import org.thingsboard.server.transport.mqtt.util.sparkplug.MetricDataType; import org.thingsboard.server.transport.mqtt.util.sparkplug.SparkplugMessageType; @@ -50,6 +51,7 @@ public abstract class AbstractMqttV5ClientSparkplugAttributesTest extends Abstra String SHARED_ATTRIBUTES_PAYLOAD = "{\"" + keyNodeRebirth + "\":" + value + "}"; Assert.assertTrue("Connection node is failed", client.isConnected()); client.subscribeAndWait(NAMESPACE + "/" + groupId + "/" + NCMD.name() + "/" + edgeNode + "/#", MqttQoS.AT_MOST_ONCE); + awaitForDeviceActorToReceiveSubscription(savedGateway.getId(), FeatureType.ATTRIBUTES, 1); doPostAsync("/api/plugins/telemetry/DEVICE/" + savedGateway.getId().getId() + "/attributes/SHARED_SCOPE", SHARED_ATTRIBUTES_PAYLOAD, String.class, status().isOk()); await(alias + SparkplugMessageType.NBIRTH.name()) .atMost(40, TimeUnit.SECONDS) @@ -75,6 +77,7 @@ public abstract class AbstractMqttV5ClientSparkplugAttributesTest extends Abstra connectionWithNBirth(metricDataType, metricKey, metricValue); Assert.assertTrue("Connection node is failed", client.isConnected()); client.subscribeAndWait(NAMESPACE + "/" + groupId + "/" + NCMD.name() + "/" + edgeNode + "/#", MqttQoS.AT_MOST_ONCE); + awaitForDeviceActorToReceiveSubscription(savedGateway.getId(), FeatureType.ATTRIBUTES, 1); // Boolean <-> String boolean expectedValue = true; @@ -138,6 +141,7 @@ public abstract class AbstractMqttV5ClientSparkplugAttributesTest extends Abstra connectionWithNBirth(metricDataType, metricKey, metricValue); Assert.assertTrue("Connection node is failed", client.isConnected()); client.subscribeAndWait(NAMESPACE + "/" + groupId + "/" + NCMD.name() + "/" + edgeNode + "/#", MqttQoS.AT_MOST_ONCE); + awaitForDeviceActorToReceiveSubscription(savedGateway.getId(), FeatureType.ATTRIBUTES, 1); // Long <-> String String valueStr = "123"; @@ -189,6 +193,7 @@ public abstract class AbstractMqttV5ClientSparkplugAttributesTest extends Abstra connectionWithNBirth(metricDataType, metricKey, metricValue); Assert.assertTrue("Connection node is failed", client.isConnected()); client.subscribeAndWait(NAMESPACE + "/" + groupId + "/" + NCMD.name() + "/" + edgeNode + "/#", MqttQoS.AT_MOST_ONCE); + awaitForDeviceActorToReceiveSubscription(savedGateway.getId(), FeatureType.ATTRIBUTES, 1); // Float <-> String String valueStr = "123.345"; @@ -240,6 +245,7 @@ public abstract class AbstractMqttV5ClientSparkplugAttributesTest extends Abstra connectionWithNBirth(metricDataType, metricKey, metricValue); Assert.assertTrue("Connection node is failed", client.isConnected()); client.subscribeAndWait(NAMESPACE + "/" + groupId + "/" + NCMD.name() + "/" + edgeNode + "/#", MqttQoS.AT_MOST_ONCE); + awaitForDeviceActorToReceiveSubscription(savedGateway.getId(), FeatureType.ATTRIBUTES, 1); // Double <-> String String valueStr = "123345456"; @@ -291,6 +297,7 @@ public abstract class AbstractMqttV5ClientSparkplugAttributesTest extends Abstra connectionWithNBirth(metricDataType, metricKey, metricValue); Assert.assertTrue("Connection node is failed", client.isConnected()); client.subscribeAndWait(NAMESPACE + "/" + groupId + "/" + NCMD.name() + "/" + edgeNode + "/#", MqttQoS.AT_MOST_ONCE); + awaitForDeviceActorToReceiveSubscription(savedGateway.getId(), FeatureType.ATTRIBUTES, 1); // String <-> Long long valueLong = 123345456L; diff --git a/application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/rpc/AbstractMqttV5RpcSparkplugTest.java b/application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/rpc/AbstractMqttV5RpcSparkplugTest.java index b08baac875..036b779de1 100644 --- a/application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/rpc/AbstractMqttV5RpcSparkplugTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/rpc/AbstractMqttV5RpcSparkplugTest.java @@ -20,6 +20,7 @@ import lombok.extern.slf4j.Slf4j; import org.junit.Assert; import org.junit.Test; import org.thingsboard.server.common.data.Device; +import org.thingsboard.server.common.msg.session.FeatureType; import org.thingsboard.server.transport.mqtt.sparkplug.AbstractMqttV5ClientSparkplugTest; import org.thingsboard.server.transport.mqtt.util.sparkplug.SparkplugMessageType; @@ -45,6 +46,7 @@ public abstract class AbstractMqttV5RpcSparkplugTest extends AbstractMqttV5Clie connectionWithNBirth(metricBirthDataType_Int32, metricBirthName_Int32, nextInt32()); Assert.assertTrue("Connection node is failed", client.isConnected()); client.subscribeAndWait(NAMESPACE + "/" + groupId + "/" + NCMD.name() + "/" + edgeNode + "/#", MqttQoS.AT_MOST_ONCE); + awaitForDeviceActorToReceiveSubscription(savedGateway.getId(), FeatureType.RPC, 1); String expected = "{\"result\":\"Success: " + SparkplugMessageType.NCMD.name() + "\"}"; String actual = sendRPCSparkplug(NCMD.name(), sparkplugRpcRequest, savedGateway); await(alias + SparkplugMessageType.NCMD.name()) @@ -79,6 +81,7 @@ public abstract class AbstractMqttV5RpcSparkplugTest extends AbstractMqttV5Clie connectionWithNBirth(metricBirthDataType_Int32, metricBirthName_Int32, nextInt32()); Assert.assertTrue("Connection node is failed", client.isConnected()); client.subscribeAndWait(NAMESPACE + "/" + groupId + "/" + NCMD.name() + "/" + edgeNode + "/#", MqttQoS.AT_MOST_ONCE); + awaitForDeviceActorToReceiveSubscription(savedGateway.getId(), FeatureType.RPC, 1); String invalidateTypeMessageName = "RCMD"; String expected = "{\"result\":\"" + INVALID_ARGUMENTS + "\",\"error\":\"Failed to convert device RPC command to MQTT msg: " + invalidateTypeMessageName + "{\\\"metricName\\\":\\\"" + metricBirthName_Int32 + "\\\",\\\"value\\\":" + metricBirthValue_Int32 + "}\"}"; @@ -92,6 +95,7 @@ public abstract class AbstractMqttV5RpcSparkplugTest extends AbstractMqttV5Clie connectionWithNBirth(metricBirthDataType_Int32, metricBirthName_Int32, nextInt32()); Assert.assertTrue("Connection node is failed", client.isConnected()); client.subscribeAndWait(NAMESPACE + "/" + groupId + "/" + NCMD.name() + "/" + edgeNode + "/#", MqttQoS.AT_MOST_ONCE); + awaitForDeviceActorToReceiveSubscription(savedGateway.getId(), FeatureType.RPC, 1); String metricNameBad = metricBirthName_Int32 + "_Bad"; String sparkplugRpcRequestBad = "{\"metricName\":\"" + metricNameBad + "\",\"value\":" + metricBirthValue_Int32 + "}"; String expected = "{\"result\":\"BAD_REQUEST_PARAMS\",\"error\":\"Failed send To Node Rpc Request: " + diff --git a/application/src/test/resources/logback-test.xml b/application/src/test/resources/logback-test.xml index 23e6d8c2f6..953b7094a4 100644 --- a/application/src/test/resources/logback-test.xml +++ b/application/src/test/resources/logback-test.xml @@ -21,6 +21,13 @@ + + + + + + + diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/DefaultLwM2mTransportService.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/DefaultLwM2mTransportService.java index f4b52a0f5f..3eb096cd38 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/DefaultLwM2mTransportService.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/DefaultLwM2mTransportService.java @@ -25,6 +25,7 @@ import org.eclipse.leshan.core.node.codec.DefaultLwM2mEncoder; import org.eclipse.leshan.server.californium.LeshanServer; import org.eclipse.leshan.server.californium.LeshanServerBuilder; import org.eclipse.leshan.server.californium.registration.CaliforniumRegistrationStore; +import org.springframework.context.annotation.DependsOn; import org.springframework.stereotype.Component; import org.thingsboard.server.cache.ota.OtaPackageDataCache; import org.thingsboard.server.common.data.DataConstants; @@ -35,7 +36,7 @@ import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig; import org.thingsboard.server.transport.lwm2m.secure.TbLwM2MAuthorizer; import org.thingsboard.server.transport.lwm2m.secure.TbLwM2MDtlsCertificateVerifier; import org.thingsboard.server.transport.lwm2m.server.store.TbSecurityStore; -import org.thingsboard.server.transport.lwm2m.server.uplink.DefaultLwM2mUplinkMsgHandler; +import org.thingsboard.server.transport.lwm2m.server.uplink.LwM2mUplinkMsgHandler; import org.thingsboard.server.transport.lwm2m.utils.LwM2mValueConverterImpl; import javax.annotation.PreDestroy; @@ -56,6 +57,7 @@ import static org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaU @Slf4j @Component +@DependsOn({"lwM2mDownlinkMsgHandler", "lwM2mUplinkMsgHandler"}) @TbLwM2mTransportComponent @RequiredArgsConstructor public class DefaultLwM2mTransportService implements LwM2MTransportService { @@ -66,7 +68,7 @@ public class DefaultLwM2mTransportService implements LwM2MTransportService { private final LwM2mTransportContext context; private final LwM2MTransportServerConfig config; private final OtaPackageDataCache otaPackageDataCache; - private final DefaultLwM2mUplinkMsgHandler handler; + private final LwM2mUplinkMsgHandler handler; private final CaliforniumRegistrationStore registrationStore; private final TbSecurityStore securityStore; private final TbLwM2MDtlsCertificateVerifier certificateVerifier; diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/client/LwM2mClientContextImpl.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/client/LwM2mClientContextImpl.java index 49a77d1eea..d624badc1a 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/client/LwM2mClientContextImpl.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/client/LwM2mClientContextImpl.java @@ -42,7 +42,7 @@ import org.thingsboard.server.transport.lwm2m.server.ota.LwM2MOtaUpdateService; import org.thingsboard.server.transport.lwm2m.server.session.LwM2MSessionManager; import org.thingsboard.server.transport.lwm2m.server.store.TbLwM2MClientStore; import org.thingsboard.server.transport.lwm2m.server.store.TbMainSecurityStore; -import org.thingsboard.server.transport.lwm2m.server.uplink.DefaultLwM2mUplinkMsgHandler; +import org.thingsboard.server.transport.lwm2m.server.uplink.LwM2mUplinkMsgHandler; import org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil; import java.util.Arrays; @@ -75,7 +75,7 @@ public class LwM2mClientContextImpl implements LwM2mClientContext { @Autowired @Lazy - private DefaultLwM2mUplinkMsgHandler defaultLwM2MUplinkMsgHandler; + private LwM2mUplinkMsgHandler defaultLwM2MUplinkMsgHandler; @Autowired @Lazy private LwM2MOtaUpdateService otaUpdateService; diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/downlink/DefaultLwM2mDownlinkMsgHandler.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/downlink/DefaultLwM2mDownlinkMsgHandler.java index 735fa36e1e..ecc57a7458 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/downlink/DefaultLwM2mDownlinkMsgHandler.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/downlink/DefaultLwM2mDownlinkMsgHandler.java @@ -85,6 +85,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.RejectedExecutionException; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -103,7 +104,7 @@ import static org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.ge import static org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.validateVersionedId; @Slf4j -@Service +@Service("lwM2mDownlinkMsgHandler") @TbLwM2mTransportComponent @RequiredArgsConstructor public class DefaultLwM2mDownlinkMsgHandler extends LwM2MExecutorAwareService implements LwM2mDownlinkMsgHandler { @@ -124,6 +125,7 @@ public class DefaultLwM2mDownlinkMsgHandler extends LwM2MExecutorAwareService im @PreDestroy public void destroy() { + log.trace("Destroying {}", getClass().getSimpleName()); super.destroy(); } @@ -521,16 +523,22 @@ public class DefaultLwM2mDownlinkMsgHandler extends LwM2MExecutorAwareService im private , T extends LwM2mResponse> void handleDownlinkError(LwM2mClient client, R request, DownlinkRequestCallback callback, Exception e) { log.trace("[{}] Received downlink error: {}.", client.getEndpoint(), e); - client.updateLastUplinkTime(); - executor.submit(() -> { - if (e instanceof TimeoutException || e instanceof ClientSleepingException) { - log.trace("[{}] Received {}, client is probably sleeping", client.getEndpoint(), e.getClass().getSimpleName()); - clientContext.asleep(client); - } else { - log.trace("[{}] Received {}", client.getEndpoint(), e.getClass().getSimpleName()); - } - callback.onError(toString(request), e); - }); + try { + client.updateLastUplinkTime(); + executor.submit(() -> { + if (e instanceof TimeoutException || e instanceof ClientSleepingException) { + log.trace("[{}] Received {}, client is probably sleeping", client.getEndpoint(), e.getClass().getSimpleName()); + clientContext.asleep(client); + } else { + log.trace("[{}] Received {}", client.getEndpoint(), e.getClass().getSimpleName()); + } + callback.onError(toString(request), e); + }); + } catch (RejectedExecutionException ree) { + log.warn("[{}] Can not handle downlink error. Executor already down", client.getEndpoint(), ree); + } catch (Exception exception) { + log.warn("[{}] Can not handle downlink error", client.getEndpoint(), exception); + } } private WriteRequest getWriteRequestSingleResource(ResourceModel.Type type, ContentFormat contentFormat, int objectId, int instanceId, int resourceId, Object value) { diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/ota/DefaultLwM2MOtaUpdateService.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/ota/DefaultLwM2MOtaUpdateService.java index 153e3894fb..f59643cbeb 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/ota/DefaultLwM2MOtaUpdateService.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/ota/DefaultLwM2MOtaUpdateService.java @@ -146,6 +146,7 @@ public class DefaultLwM2MOtaUpdateService extends LwM2MExecutorAwareService impl @PreDestroy public void destroy() { + log.trace("Destroying {}", getClass().getSimpleName()); super.destroy(); } diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/uplink/DefaultLwM2mUplinkMsgHandler.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/uplink/DefaultLwM2mUplinkMsgHandler.java index c790d2495c..25e3a6b591 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/uplink/DefaultLwM2mUplinkMsgHandler.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/uplink/DefaultLwM2mUplinkMsgHandler.java @@ -20,6 +20,8 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; +import lombok.Getter; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.eclipse.leshan.core.ResponseCode; import org.eclipse.leshan.core.model.ObjectModel; @@ -32,6 +34,7 @@ import org.eclipse.leshan.core.node.LwM2mPath; import org.eclipse.leshan.core.node.LwM2mResource; import org.eclipse.leshan.core.node.LwM2mResourceInstance; import org.eclipse.leshan.core.node.LwM2mSingleResource; +import org.eclipse.leshan.core.node.codec.LwM2mValueConverter; import org.eclipse.leshan.core.observation.Observation; import org.eclipse.leshan.core.request.*; import org.eclipse.leshan.core.request.WriteRequest.Mode; @@ -122,69 +125,41 @@ import static org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.fr @Slf4j -@Service +@Service("lwM2mUplinkMsgHandler") @TbLwM2mTransportComponent +@RequiredArgsConstructor public class DefaultLwM2mUplinkMsgHandler extends LwM2MExecutorAwareService implements LwM2mUplinkMsgHandler { - public LwM2mValueConverterImpl converter; + @Getter + private final LwM2mValueConverter converter = LwM2mValueConverterImpl.getInstance();; private final TransportService transportService; private final LwM2mTransportContext context; + @Lazy private final LwM2MAttributesService attributesService; private final LwM2MSessionManager sessionManager; + @Lazy private final LwM2MOtaUpdateService otaService; private final LwM2MTransportServerConfig config; private final LwM2MTelemetryLogService logService; private final LwM2mTransportServerHelper helper; private final TbLwM2MDtlsSessionStore sessionStore; private final LwM2mClientContext clientContext; - private final LwM2mDownlinkMsgHandler defaultLwM2MDownlinkMsgHandler; + private final LwM2mDownlinkMsgHandler defaultLwM2MDownlinkMsgHandler; //Do not use Lazy because we need live executor to handle msgs private final LwM2mVersionedModelProvider modelProvider; private final RegistrationStore registrationStore; private final TbLwM2mSecurityStore securityStore; private final LwM2MModelConfigService modelConfigService; - public DefaultLwM2mUplinkMsgHandler(TransportService transportService, - LwM2MTransportServerConfig config, - LwM2mTransportServerHelper helper, - LwM2mClientContext clientContext, - LwM2MTelemetryLogService logService, - LwM2MSessionManager sessionManager, - @Lazy LwM2MOtaUpdateService otaService, - @Lazy LwM2MAttributesService attributesService, - @Lazy LwM2mDownlinkMsgHandler defaultLwM2MDownlinkMsgHandler, - LwM2mTransportContext context, - TbLwM2MDtlsSessionStore sessionStore, - LwM2mVersionedModelProvider modelProvider, - RegistrationStore registrationStore, - TbLwM2mSecurityStore securityStore, - LwM2MModelConfigService modelConfigService) { - this.transportService = transportService; - this.sessionManager = sessionManager; - this.attributesService = attributesService; - this.otaService = otaService; - this.config = config; - this.helper = helper; - this.clientContext = clientContext; - this.logService = logService; - this.defaultLwM2MDownlinkMsgHandler = defaultLwM2MDownlinkMsgHandler; - this.context = context; - this.sessionStore = sessionStore; - this.modelProvider = modelProvider; - this.registrationStore = registrationStore; - this.securityStore = securityStore; - this.modelConfigService = modelConfigService; - } - @PostConstruct public void init() { super.init(); this.context.getScheduler().scheduleAtFixedRate(this::reportActivity, new Random().nextInt((int) config.getSessionReportTimeout()), config.getSessionReportTimeout(), TimeUnit.MILLISECONDS); - this.converter = LwM2mValueConverterImpl.getInstance(); } @PreDestroy public void destroy() { + log.trace("Destroying {}", getClass().getSimpleName()); super.destroy(); } @@ -955,6 +930,7 @@ public class DefaultLwM2mUplinkMsgHandler extends LwM2MExecutorAwareService impl * * @param lwM2MClient - LwM2M Client */ + @Override public void initAttributes(LwM2mClient lwM2MClient, boolean logFailedUpdateOfNonChangedValue) { Map keyNamesMap = this.getNamesFromProfileForSharedAttributes(lwM2MClient); if (!keyNamesMap.isEmpty()) { diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/uplink/LwM2mUplinkMsgHandler.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/uplink/LwM2mUplinkMsgHandler.java index 0b2d819fa8..9beebe5279 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/uplink/LwM2mUplinkMsgHandler.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/uplink/LwM2mUplinkMsgHandler.java @@ -15,6 +15,7 @@ */ package org.thingsboard.server.transport.lwm2m.server.uplink; +import org.eclipse.leshan.core.node.codec.LwM2mValueConverter; import org.eclipse.leshan.core.observation.Observation; import org.eclipse.leshan.core.request.CreateRequest; import org.eclipse.leshan.core.request.SendRequest; @@ -69,5 +70,10 @@ public interface LwM2mUplinkMsgHandler { void onToTransportUpdateCredentials(TransportProtos.SessionInfoProto sessionInfo, TransportProtos.ToTransportUpdateCredentialsProto updateCredentials); + void initAttributes(LwM2mClient lwM2MClient, boolean logFailedUpdateOfNonChangedValue); + LwM2MTransportServerConfig getConfig(); + + LwM2mValueConverter getConverter(); + } diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/utils/LwM2MTransportUtil.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/utils/LwM2MTransportUtil.java index f207e7d05a..861261d951 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/utils/LwM2MTransportUtil.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/utils/LwM2MTransportUtil.java @@ -52,7 +52,7 @@ import org.thingsboard.server.transport.lwm2m.server.ota.firmware.FirmwareUpdate import org.thingsboard.server.transport.lwm2m.server.ota.firmware.FirmwareUpdateState; import org.thingsboard.server.transport.lwm2m.server.ota.software.SoftwareUpdateResult; import org.thingsboard.server.transport.lwm2m.server.ota.software.SoftwareUpdateState; -import org.thingsboard.server.transport.lwm2m.server.uplink.DefaultLwM2mUplinkMsgHandler; +import org.thingsboard.server.transport.lwm2m.server.uplink.LwM2mUplinkMsgHandler; import java.util.ArrayList; import java.util.Arrays; @@ -289,12 +289,12 @@ public class LwM2MTransportUtil { * Attribute pmax = new Attribute(MAXIMUM_PERIOD, "60"); * Attribute [] attrs = {gt, st}; */ - public static SimpleDownlinkRequest createWriteAttributeRequest(String target, Object params, DefaultLwM2mUplinkMsgHandler serviceImpl) { + public static SimpleDownlinkRequest createWriteAttributeRequest(String target, Object params, LwM2mUplinkMsgHandler serviceImpl) { AttributeSet attrSet = new AttributeSet(createWriteAttributes(params, serviceImpl, target)); return attrSet.getAttributes().size() > 0 ? new WriteAttributesRequest(target, attrSet) : null; } - private static Attribute[] createWriteAttributes(Object params, DefaultLwM2mUplinkMsgHandler serviceImpl, String target) { + private static Attribute[] createWriteAttributes(Object params, LwM2mUplinkMsgHandler serviceImpl, String target) { List attributeLists = new ArrayList<>(); Map map = JacksonUtil.convertValue(params, new TypeReference<>() { }); @@ -366,19 +366,19 @@ public class LwM2MTransportUtil { return newValues; } - public static Object convertWriteAttributes(String type, Object value, DefaultLwM2mUplinkMsgHandler serviceImpl, String target) { + public static Object convertWriteAttributes(String type, Object value, LwM2mUplinkMsgHandler serviceImpl, String target) { switch (type) { /** Integer [0:255]; */ case DIMENSION: - Long dim = (Long) serviceImpl.converter.convertValue(value, equalsResourceTypeGetSimpleName(value), INTEGER, new LwM2mPath(target)); + Long dim = (Long) serviceImpl.getConverter().convertValue(value, equalsResourceTypeGetSimpleName(value), INTEGER, new LwM2mPath(target)); return dim >= 0 && dim <= 255 ? dim : null; /**String;*/ case OBJECT_VERSION: - return serviceImpl.converter.convertValue(value, equalsResourceTypeGetSimpleName(value), STRING, new LwM2mPath(target)); + return serviceImpl.getConverter().convertValue(value, equalsResourceTypeGetSimpleName(value), STRING, new LwM2mPath(target)); /**INTEGER */ case MINIMUM_PERIOD: case MAXIMUM_PERIOD: - return serviceImpl.converter.convertValue(value, equalsResourceTypeGetSimpleName(value), INTEGER, new LwM2mPath(target)); + return serviceImpl.getConverter().convertValue(value, equalsResourceTypeGetSimpleName(value), INTEGER, new LwM2mPath(target)); /**Float; */ case GREATER_THAN: case LESSER_THAN: @@ -386,7 +386,7 @@ public class LwM2MTransportUtil { if (value.getClass().getSimpleName().equals("String")) { value = Double.valueOf((String) value); } - return serviceImpl.converter.convertValue(value, equalsResourceTypeGetSimpleName(value), FLOAT, new LwM2mPath(target)); + return serviceImpl.getConverter().convertValue(value, equalsResourceTypeGetSimpleName(value), FLOAT, new LwM2mPath(target)); default: return null; } diff --git a/dao/src/main/java/org/thingsboard/server/dao/entity/DefaultEntityServiceRegistry.java b/dao/src/main/java/org/thingsboard/server/dao/entity/DefaultEntityServiceRegistry.java index a8b1d97bac..232905e9fc 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/entity/DefaultEntityServiceRegistry.java +++ b/dao/src/main/java/org/thingsboard/server/dao/entity/DefaultEntityServiceRegistry.java @@ -15,27 +15,31 @@ */ package org.thingsboard.server.dao.entity; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationContext; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.context.event.EventListener; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; import org.springframework.stereotype.Service; import org.thingsboard.server.common.data.EntityType; -import javax.annotation.PostConstruct; import java.util.HashMap; import java.util.Map; @Service +@RequiredArgsConstructor +@Slf4j public class DefaultEntityServiceRegistry implements EntityServiceRegistry { private final ApplicationContext applicationContext; - private final Map entityDaoServicesMap; + private final Map entityDaoServicesMap = new HashMap<>(); - public DefaultEntityServiceRegistry(ApplicationContext applicationContext) { - this.applicationContext = applicationContext; - this.entityDaoServicesMap = new HashMap<>(); - } - - @PostConstruct + @EventListener(ContextRefreshedEvent.class) + @Order(Ordered.HIGHEST_PRECEDENCE) public void init() { + log.debug("Initializing EntityServiceRegistry on ContextRefreshedEvent"); applicationContext.getBeansOfType(EntityDaoService.class).values().forEach(entityDaoService -> { EntityType entityType = entityDaoService.getEntityType(); entityDaoServicesMap.put(entityType, entityDaoService); @@ -43,6 +47,7 @@ public class DefaultEntityServiceRegistry implements EntityServiceRegistry { entityDaoServicesMap.put(EntityType.RULE_NODE, entityDaoService); } }); + log.debug("Initialized EntityServiceRegistry total [{}] entries", entityDaoServicesMap.size()); } @Override diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/AbstractServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/AbstractServiceTest.java index 7f1f5e9ca0..55f3bb58c8 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/AbstractServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/AbstractServiceTest.java @@ -18,6 +18,8 @@ package org.thingsboard.server.dao.service; import com.datastax.oss.driver.api.core.uuid.Uuids; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.After; +import org.junit.Before; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; @@ -47,44 +49,17 @@ import org.thingsboard.server.common.data.id.HasId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.ota.ChecksumAlgorithm; import org.thingsboard.server.common.data.ota.OtaPackageType; -import org.thingsboard.server.dao.alarm.AlarmCommentService; -import org.thingsboard.server.dao.alarm.AlarmService; -import org.thingsboard.server.dao.asset.AssetProfileService; -import org.thingsboard.server.dao.asset.AssetService; import org.thingsboard.server.dao.audit.AuditLogLevelFilter; import org.thingsboard.server.dao.audit.AuditLogLevelMask; import org.thingsboard.server.dao.audit.AuditLogLevelProperties; -import org.thingsboard.server.dao.component.ComponentDescriptorService; -import org.thingsboard.server.dao.customer.CustomerService; -import org.thingsboard.server.dao.dashboard.DashboardService; -import org.thingsboard.server.dao.device.DeviceCredentialsService; -import org.thingsboard.server.dao.device.DeviceProfileService; -import org.thingsboard.server.dao.device.DeviceService; -import org.thingsboard.server.dao.edge.EdgeEventService; -import org.thingsboard.server.dao.edge.EdgeService; -import org.thingsboard.server.dao.entity.EntityService; -import org.thingsboard.server.dao.entityview.EntityViewService; -import org.thingsboard.server.dao.event.EventService; -import org.thingsboard.server.dao.ota.OtaPackageService; -import org.thingsboard.server.dao.queue.QueueService; -import org.thingsboard.server.dao.relation.RelationService; -import org.thingsboard.server.dao.resource.ResourceService; -import org.thingsboard.server.dao.rpc.RpcService; -import org.thingsboard.server.dao.rule.RuleChainService; -import org.thingsboard.server.dao.settings.AdminSettingsService; -import org.thingsboard.server.dao.tenant.TenantProfileService; import org.thingsboard.server.dao.tenant.TenantService; -import org.thingsboard.server.dao.timeseries.TimeseriesService; -import org.thingsboard.server.dao.usagerecord.ApiUsageStateService; -import org.thingsboard.server.dao.user.UserService; -import org.thingsboard.server.dao.widget.WidgetTypeService; -import org.thingsboard.server.dao.widget.WidgetsBundleService; import java.io.IOException; import java.nio.ByteBuffer; import java.util.Comparator; import java.util.HashMap; import java.util.Map; +import java.util.UUID; import static org.junit.Assert.assertNotNull; @@ -100,91 +75,20 @@ public abstract class AbstractServiceTest { public static final TenantId SYSTEM_TENANT_ID = TenantId.SYS_TENANT_ID; - @Autowired - protected UserService userService; - - @Autowired - protected ApiUsageStateService apiUsageStateService; - - @Autowired - protected AdminSettingsService adminSettingsService; - @Autowired protected TenantService tenantService; - @Autowired - protected CustomerService customerService; - - @Autowired - protected DeviceService deviceService; - - @Autowired - protected AssetService assetService; - - @Autowired - protected EntityViewService entityViewService; - - @Autowired - protected EntityService entityService; - - @Autowired - protected DeviceCredentialsService deviceCredentialsService; - - @Autowired - protected WidgetsBundleService widgetsBundleService; - - @Autowired - protected WidgetTypeService widgetTypeService; - - @Autowired - protected DashboardService dashboardService; - - @Autowired - protected TimeseriesService tsService; - - @Autowired - protected EventService eventService; - - @Autowired - protected RelationService relationService; - - @Autowired - protected AlarmService alarmService; - @Autowired - protected AlarmCommentService alarmCommentService; - - @Autowired - protected RuleChainService ruleChainService; - - @Autowired - protected EdgeService edgeService; - - @Autowired - protected EdgeEventService edgeEventService; + protected TenantId tenantId; - @Autowired - private ComponentDescriptorService componentDescriptorService; - - @Autowired - protected TenantProfileService tenantProfileService; - - @Autowired - protected DeviceProfileService deviceProfileService; - - @Autowired - protected AssetProfileService assetProfileService; - - @Autowired - protected ResourceService resourceService; - - @Autowired - protected OtaPackageService otaPackageService; - - @Autowired - protected RpcService rpcService; + @Before + public void beforeAbstractService() { + tenantId = createTenant(); + } - @Autowired - protected QueueService queueService; + @After + public void afterAbstractService() { + tenantService.deleteTenants(); + } public class IdComparator implements Comparator { @Override @@ -270,7 +174,7 @@ public abstract class AbstractServiceTest { public TenantId createTenant() { Tenant tenant = new Tenant(); - tenant.setTitle("My tenant " + Uuids.timeBased()); + tenant.setTitle("My tenant " + UUID.randomUUID()); Tenant savedTenant = tenantService.saveTenant(tenant); assertNotNull(savedTenant); return savedTenant.getId(); diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseAdminSettingsServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseAdminSettingsServiceTest.java index c839b8ed69..3b8659f93c 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseAdminSettingsServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseAdminSettingsServiceTest.java @@ -20,11 +20,16 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import org.junit.Assert; import org.junit.Test; import org.junit.jupiter.api.Assertions; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.server.common.data.AdminSettings; import org.thingsboard.server.dao.exception.DataValidationException; +import org.thingsboard.server.dao.settings.AdminSettingsService; public abstract class BaseAdminSettingsServiceTest extends AbstractServiceTest { + @Autowired + AdminSettingsService adminSettingsService; + @Test public void testFindAdminSettingsByKey() { AdminSettings adminSettings = adminSettingsService.findAdminSettingsByKey(SYSTEM_TENANT_ID, "general"); diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseAlarmCommentServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseAlarmCommentServiceTest.java index 688793d6c6..1144ec6513 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseAlarmCommentServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseAlarmCommentServiceTest.java @@ -20,21 +20,22 @@ import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.testcontainers.shaded.org.apache.commons.lang3.RandomStringUtils; import org.thingsboard.common.util.JacksonUtil; -import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.User; import org.thingsboard.server.common.data.alarm.Alarm; import org.thingsboard.server.common.data.alarm.AlarmComment; import org.thingsboard.server.common.data.alarm.AlarmCommentInfo; import org.thingsboard.server.common.data.alarm.AlarmSeverity; -import org.thingsboard.server.common.data.alarm.AlarmStatus; import org.thingsboard.server.common.data.id.AssetId; -import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.UserId; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.security.Authority; +import org.thingsboard.server.dao.alarm.AlarmCommentService; +import org.thingsboard.server.dao.alarm.AlarmService; +import org.thingsboard.server.dao.user.UserService; import java.util.UUID; import java.util.concurrent.ExecutionException; @@ -43,19 +44,19 @@ import static org.thingsboard.server.common.data.alarm.AlarmCommentType.OTHER; public abstract class BaseAlarmCommentServiceTest extends AbstractServiceTest { + @Autowired + AlarmService alarmService; + @Autowired + AlarmCommentService alarmCommentService; + @Autowired + UserService userService; + public static final String TEST_ALARM = "TEST_ALARM"; - private TenantId tenantId; private Alarm alarm; private User user; @Before public void before() { - Tenant tenant = new Tenant(); - tenant.setTitle("My tenant"); - Tenant savedTenant = tenantService.saveTenant(tenant); - Assert.assertNotNull(savedTenant); - tenantId = savedTenant.getId(); - alarm = Alarm.builder().tenantId(tenantId).originator(new AssetId(Uuids.timeBased())) .type(TEST_ALARM) .severity(AlarmSeverity.CRITICAL) @@ -74,7 +75,6 @@ public abstract class BaseAlarmCommentServiceTest extends AbstractServiceTest { @After public void after() { alarmService.deleteAlarm(tenantId, alarm.getId()); - tenantService.deleteTenant(tenantId); } diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseAlarmServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseAlarmServiceTest.java index b7f5135533..73a489ab68 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseAlarmServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseAlarmServiceTest.java @@ -16,14 +16,12 @@ package org.thingsboard.server.dao.service; import com.datastax.oss.driver.api.core.uuid.Uuids; -import org.junit.After; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.common.data.Customer; 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.alarm.Alarm; import org.thingsboard.server.common.data.alarm.AlarmCreateOrUpdateActiveRequest; @@ -36,9 +34,7 @@ import org.thingsboard.server.common.data.alarm.AlarmStatus; import org.thingsboard.server.common.data.alarm.AlarmUpdateRequest; import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.common.data.id.AssetId; -import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.PageData; -import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.page.SortOrder; import org.thingsboard.server.common.data.page.TimePageLink; import org.thingsboard.server.common.data.query.AlarmData; @@ -52,7 +48,12 @@ import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.relation.RelationTypeGroup; import org.thingsboard.server.common.data.security.Authority; import org.thingsboard.server.dao.alarm.AlarmApiCallResult; -import org.thingsboard.server.dao.alarm.AlarmOperationResult; +import org.thingsboard.server.dao.alarm.AlarmService; +import org.thingsboard.server.dao.asset.AssetService; +import org.thingsboard.server.dao.customer.CustomerService; +import org.thingsboard.server.dao.device.DeviceService; +import org.thingsboard.server.dao.relation.RelationService; +import org.thingsboard.server.dao.user.UserService; import java.util.Arrays; import java.util.Collections; @@ -61,29 +62,24 @@ import java.util.concurrent.ExecutionException; public abstract class BaseAlarmServiceTest extends AbstractServiceTest { - public static final String TEST_ALARM = "TEST_ALARM"; + @Autowired + AlarmService alarmService; + @Autowired + AssetService assetService; + @Autowired + CustomerService customerService; + @Autowired + DeviceService deviceService; + @Autowired + RelationService relationService; + @Autowired + UserService userService; + public static final String TEST_ALARM = "TEST_ALARM"; private static final String TEST_TENANT_EMAIL = "testtenant@thingsboard.org"; private static final String TEST_TENANT_FIRST_NAME = "testtenantfirstname"; private static final String TEST_TENANT_LAST_NAME = "testtenantlastname"; - private TenantId tenantId; - - @Before - public void before() { - Tenant tenant = new Tenant(); - tenant.setTitle("My tenant"); - Tenant savedTenant = tenantService.saveTenant(tenant); - Assert.assertNotNull(savedTenant); - tenantId = savedTenant.getId(); - } - - @After - public void after() { - tenantService.deleteTenant(tenantId); - } - - @Test public void testSaveAndFetchAlarm() throws ExecutionException, InterruptedException { AssetId parentId = new AssetId(Uuids.timeBased()); diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseApiUsageStateServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseApiUsageStateServiceTest.java index 2ea7147a65..2ee2f74b3e 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseApiUsageStateServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseApiUsageStateServiceTest.java @@ -15,33 +15,18 @@ */ package org.thingsboard.server.dao.service; -import org.junit.After; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.server.common.data.ApiUsageStateValue; -import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.ApiUsageState; -import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.dao.usagerecord.ApiUsageStateService; public abstract class BaseApiUsageStateServiceTest extends AbstractServiceTest { - private TenantId tenantId; - - @Before - public void before() { - Tenant tenant = new Tenant(); - tenant.setTitle("My tenant"); - Tenant savedTenant = tenantService.saveTenant(tenant); - Assert.assertNotNull(savedTenant); - tenantId = savedTenant.getId(); - } - - @After - public void after() { - tenantService.deleteTenant(tenantId); - } + @Autowired + ApiUsageStateService apiUsageStateService; @Test public void testFindApiUsageStateByTenantId() { diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseAssetProfileServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseAssetProfileServiceTest.java index 091b309269..df0cb464fc 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseAssetProfileServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseAssetProfileServiceTest.java @@ -19,19 +19,18 @@ import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; 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.junit.jupiter.api.Assertions; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.common.util.ThingsBoardThreadFactory; -import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.common.data.asset.AssetProfile; import org.thingsboard.server.common.data.asset.AssetProfileInfo; -import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; +import org.thingsboard.server.dao.asset.AssetProfileService; +import org.thingsboard.server.dao.asset.AssetService; import org.thingsboard.server.dao.exception.DataValidationException; import java.util.ArrayList; @@ -46,21 +45,10 @@ public abstract class BaseAssetProfileServiceTest extends AbstractServiceTest { private IdComparator idComparator = new IdComparator<>(); private IdComparator assetProfileInfoIdComparator = new IdComparator<>(); - private TenantId tenantId; - - @Before - public void before() { - Tenant tenant = new Tenant(); - tenant.setTitle("My tenant"); - Tenant savedTenant = tenantService.saveTenant(tenant); - Assert.assertNotNull(savedTenant); - tenantId = savedTenant.getId(); - } - - @After - public void after() { - tenantService.deleteTenant(tenantId); - } + @Autowired + AssetProfileService assetProfileService; + @Autowired + AssetService assetService; @Test public void testSaveAssetProfile() { diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseAssetServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseAssetServiceTest.java index 108fcb70d9..9f4c8fe363 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseAssetServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseAssetServiceTest.java @@ -16,11 +16,10 @@ package org.thingsboard.server.dao.service; import com.datastax.oss.driver.api.core.uuid.Uuids; -import org.junit.After; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import org.junit.jupiter.api.Assertions; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.EntitySubtype; import org.thingsboard.server.common.data.StringUtils; @@ -31,6 +30,8 @@ import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; +import org.thingsboard.server.dao.asset.AssetService; +import org.thingsboard.server.dao.customer.CustomerService; import org.thingsboard.server.dao.exception.DataValidationException; import java.util.ArrayList; @@ -41,23 +42,12 @@ import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; public abstract class BaseAssetServiceTest extends AbstractServiceTest { - private IdComparator idComparator = new IdComparator<>(); - - private TenantId tenantId; - - @Before - public void before() { - Tenant tenant = new Tenant(); - tenant.setTitle("My tenant"); - Tenant savedTenant = tenantService.saveTenant(tenant); - Assert.assertNotNull(savedTenant); - tenantId = savedTenant.getId(); - } + @Autowired + AssetService assetService; + @Autowired + CustomerService customerService; - @After - public void after() { - tenantService.deleteTenant(tenantId); - } + private IdComparator idComparator = new IdComparator<>(); @Test public void testSaveAsset() { @@ -220,12 +210,6 @@ public abstract class BaseAssetServiceTest extends AbstractServiceTest { @Test public void testFindAssetsByTenantId() { - Tenant tenant = new Tenant(); - tenant.setTitle("Test tenant"); - tenant = tenantService.saveTenant(tenant); - - TenantId tenantId = tenant.getId(); - List assets = new ArrayList<>(); for (int i=0;i<178;i++) { Asset asset = new Asset(); @@ -257,8 +241,6 @@ public abstract class BaseAssetServiceTest extends AbstractServiceTest { pageData = assetService.findAssetsByTenantId(tenantId, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertTrue(pageData.getData().isEmpty()); - - tenantService.deleteTenant(tenantId); } @Test @@ -419,12 +401,6 @@ public abstract class BaseAssetServiceTest extends AbstractServiceTest { @Test public void testFindAssetsByTenantIdAndCustomerId() { - Tenant tenant = new Tenant(); - tenant.setTitle("Test tenant"); - tenant = tenantService.saveTenant(tenant); - - TenantId tenantId = tenant.getId(); - Customer customer = new Customer(); customer.setTitle("Test customer"); customer.setTenantId(tenantId); @@ -463,8 +439,6 @@ public abstract class BaseAssetServiceTest extends AbstractServiceTest { pageData = assetService.findAssetInfosByTenantIdAndCustomerId(tenantId, customerId, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertTrue(pageData.getData().isEmpty()); - - tenantService.deleteTenant(tenantId); } @Test diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseCustomerServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseCustomerServiceTest.java index 18bf036495..371d8abb99 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseCustomerServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseCustomerServiceTest.java @@ -25,6 +25,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.jupiter.api.Assertions; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.common.util.ThingsBoardExecutors; import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.StringUtils; @@ -32,6 +33,7 @@ import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; +import org.thingsboard.server.dao.customer.CustomerService; import org.thingsboard.server.dao.exception.DataValidationException; import java.util.ArrayList; @@ -41,28 +43,22 @@ import java.util.concurrent.TimeUnit; import static org.assertj.core.api.Assertions.assertThat; public abstract class BaseCustomerServiceTest extends AbstractServiceTest { + + @Autowired + CustomerService customerService; + static final int TIMEOUT = 30; ListeningExecutorService executor; - private TenantId tenantId; - @Before public void before() { executor = MoreExecutors.listeningDecorator(ThingsBoardExecutors.newWorkStealingPool(8, getClass())); - - Tenant tenant = new Tenant(); - tenant.setTitle("My tenant"); - Tenant savedTenant = tenantService.saveTenant(tenant); - Assert.assertNotNull(savedTenant); - tenantId = savedTenant.getId(); - } + } @After public void after() { executor.shutdownNow(); - - tenantService.deleteTenant(tenantId); } @Test @@ -152,11 +148,6 @@ public abstract class BaseCustomerServiceTest extends AbstractServiceTest { @Test public void testFindCustomersByTenantId() throws Exception { - Tenant tenant = new Tenant(); - tenant.setTitle("Test tenant"); - tenant = tenantService.saveTenant(tenant); - - TenantId tenantId = tenant.getId(); List> futures = new ArrayList<>(135); for (int i = 0; i < 135; i++) { @@ -187,8 +178,6 @@ public abstract class BaseCustomerServiceTest extends AbstractServiceTest { pageData = customerService.findCustomersByTenantId(tenantId, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertTrue(pageData.getData().isEmpty()); - - tenantService.deleteTenant(tenantId); } @Test diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseDashboardServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseDashboardServiceTest.java index 4dc28a210a..7ccd5e6211 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseDashboardServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseDashboardServiceTest.java @@ -16,11 +16,10 @@ package org.thingsboard.server.dao.service; import com.datastax.oss.driver.api.core.uuid.Uuids; -import org.junit.After; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import org.junit.jupiter.api.Assertions; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.Dashboard; import org.thingsboard.server.common.data.DashboardInfo; @@ -33,6 +32,9 @@ import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.page.SortOrder; +import org.thingsboard.server.dao.customer.CustomerService; +import org.thingsboard.server.dao.dashboard.DashboardService; +import org.thingsboard.server.dao.edge.EdgeService; import org.thingsboard.server.dao.exception.DataValidationException; import java.io.IOException; @@ -42,24 +44,15 @@ import java.util.List; import java.util.concurrent.ExecutionException; public abstract class BaseDashboardServiceTest extends AbstractServiceTest { - - private IdComparator idComparator = new IdComparator<>(); - - private TenantId tenantId; - @Before - public void before() { - Tenant tenant = new Tenant(); - tenant.setTitle("My tenant"); - Tenant savedTenant = tenantService.saveTenant(tenant); - Assert.assertNotNull(savedTenant); - tenantId = savedTenant.getId(); - } + @Autowired + CustomerService customerService; + @Autowired + DashboardService dashboardService; + @Autowired + EdgeService edgeService; - @After - public void after() { - tenantService.deleteTenant(tenantId); - } + private IdComparator idComparator = new IdComparator<>(); @Test public void testSaveDashboard() throws IOException { @@ -176,12 +169,6 @@ public abstract class BaseDashboardServiceTest extends AbstractServiceTest { @Test public void testFindDashboardsByTenantId() { - Tenant tenant = new Tenant(); - tenant.setTitle("Test tenant"); - tenant = tenantService.saveTenant(tenant); - - TenantId tenantId = tenant.getId(); - List dashboards = new ArrayList<>(); for (int i=0;i<165;i++) { Dashboard dashboard = new Dashboard(); @@ -212,18 +199,10 @@ public abstract class BaseDashboardServiceTest extends AbstractServiceTest { pageData = dashboardService.findDashboardsByTenantId(tenantId, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertTrue(pageData.getData().isEmpty()); - - tenantService.deleteTenant(tenantId); } @Test public void testFindMobileDashboardsByTenantId() { - Tenant tenant = new Tenant(); - tenant.setTitle("Test tenant"); - tenant = tenantService.saveTenant(tenant); - - TenantId tenantId = tenant.getId(); - List mobileDashboards = new ArrayList<>(); for (int i=0;i<165;i++) { Dashboard dashboard = new Dashboard(); @@ -272,8 +251,6 @@ public abstract class BaseDashboardServiceTest extends AbstractServiceTest { pageData = dashboardService.findMobileDashboardsByTenantId(tenantId, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertTrue(pageData.getData().isEmpty()); - - tenantService.deleteTenant(tenantId); } @Test @@ -353,12 +330,6 @@ public abstract class BaseDashboardServiceTest extends AbstractServiceTest { @Test public void testFindDashboardsByTenantIdAndCustomerId() throws ExecutionException, InterruptedException { - Tenant tenant = new Tenant(); - tenant.setTitle("Test tenant"); - tenant = tenantService.saveTenant(tenant); - - TenantId tenantId = tenant.getId(); - Customer customer = new Customer(); customer.setTitle("Test customer"); customer.setTenantId(tenantId); @@ -396,8 +367,6 @@ public abstract class BaseDashboardServiceTest extends AbstractServiceTest { pageData = dashboardService.findDashboardsByTenantIdAndCustomerId(tenantId, customerId, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertTrue(pageData.getData().isEmpty()); - - tenantService.deleteTenant(tenantId); } @Test diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceCredentialsServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceCredentialsServiceTest.java index f3e052b3e9..4512a5fa4f 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceCredentialsServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceCredentialsServiceTest.java @@ -16,37 +16,25 @@ package org.thingsboard.server.dao.service; import com.datastax.oss.driver.api.core.uuid.Uuids; -import org.junit.After; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import org.junit.jupiter.api.Assertions; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.server.common.data.Device; -import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.id.DeviceCredentialsId; import org.thingsboard.server.common.data.id.DeviceId; -import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.security.DeviceCredentials; import org.thingsboard.server.common.data.security.DeviceCredentialsType; +import org.thingsboard.server.dao.device.DeviceCredentialsService; +import org.thingsboard.server.dao.device.DeviceService; import org.thingsboard.server.dao.exception.DataValidationException; public abstract class BaseDeviceCredentialsServiceTest extends AbstractServiceTest { - private TenantId tenantId; - - @Before - public void before() { - Tenant tenant = new Tenant(); - tenant.setTitle("My tenant"); - Tenant savedTenant = tenantService.saveTenant(tenant); - Assert.assertNotNull(savedTenant); - tenantId = savedTenant.getId(); - } - - @After - public void after() { - tenantService.deleteTenant(tenantId); - } + @Autowired + DeviceCredentialsService deviceCredentialsService; + @Autowired + DeviceService deviceService; @Test public void testCreateDeviceCredentials() { diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceProfileServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceProfileServiceTest.java index 77a8a479f2..cdfa73fac9 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceProfileServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceProfileServiceTest.java @@ -19,24 +19,24 @@ import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; 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.Ignore; import org.junit.Test; import org.junit.jupiter.api.Assertions; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.common.util.ThingsBoardThreadFactory; import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.DeviceProfileInfo; import org.thingsboard.server.common.data.DeviceTransportType; import org.thingsboard.server.common.data.OtaPackage; -import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.ota.ChecksumAlgorithm; -import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; +import org.thingsboard.server.dao.device.DeviceProfileService; +import org.thingsboard.server.dao.device.DeviceService; import org.thingsboard.server.dao.exception.DataValidationException; +import org.thingsboard.server.dao.ota.OtaPackageService; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -44,7 +44,6 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import static org.assertj.core.api.Assertions.assertThat; @@ -52,25 +51,16 @@ import static org.thingsboard.server.common.data.ota.OtaPackageType.FIRMWARE; public abstract class BaseDeviceProfileServiceTest extends AbstractServiceTest { + @Autowired + DeviceProfileService deviceProfileService; + @Autowired + DeviceService deviceService; + @Autowired + OtaPackageService otaPackageService; + private IdComparator idComparator = new IdComparator<>(); private IdComparator deviceProfileInfoIdComparator = new IdComparator<>(); - private TenantId tenantId; - - @Before - public void before() { - Tenant tenant = new Tenant(); - tenant.setTitle("My tenant"); - Tenant savedTenant = tenantService.saveTenant(tenant); - Assert.assertNotNull(savedTenant); - tenantId = savedTenant.getId(); - } - - @After - public void after() { - tenantService.deleteTenant(tenantId); - } - @Test public void testSaveDeviceProfile() { DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Device Profile"); diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceServiceTest.java index 010f56235f..73b71ceae3 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceServiceTest.java @@ -19,10 +19,9 @@ import com.datastax.oss.driver.api.core.uuid.Uuids; import org.junit.After; import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; import org.junit.jupiter.api.Assertions; -import org.junit.rules.ExpectedException; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.DeviceInfo; @@ -40,26 +39,43 @@ import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.security.DeviceCredentials; import org.thingsboard.server.common.data.security.DeviceCredentialsType; import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration; +import org.thingsboard.server.dao.customer.CustomerService; +import org.thingsboard.server.dao.device.DeviceCredentialsService; +import org.thingsboard.server.dao.device.DeviceProfileService; +import org.thingsboard.server.dao.device.DeviceService; import org.thingsboard.server.dao.exception.DataValidationException; +import org.thingsboard.server.dao.ota.OtaPackageService; +import org.thingsboard.server.dao.tenant.TenantProfileService; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.thingsboard.server.common.data.ota.OtaPackageType.FIRMWARE; import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; public abstract class BaseDeviceServiceTest extends AbstractServiceTest { - private IdComparator idComparator = new IdComparator<>(); + @Autowired + CustomerService customerService; + @Autowired + DeviceCredentialsService deviceCredentialsService; + @Autowired + DeviceProfileService deviceProfileService; + @Autowired + DeviceService deviceService; + @Autowired + OtaPackageService otaPackageService; + @Autowired + TenantProfileService tenantProfileService; - private TenantId tenantId; + private IdComparator idComparator = new IdComparator<>(); private TenantId anotherTenantId; @Before public void before() { - tenantId = createTenant(); anotherTenantId = createTenant(); } @@ -72,10 +88,6 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { tenantProfileService.deleteTenantProfiles(anotherTenantId); } - @SuppressWarnings("deprecation") - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test public void testSaveDevicesWithoutMaxDeviceLimit() { Device device = this.saveDevice(tenantId, "My device"); @@ -244,9 +256,9 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { savedDevice.setFirmwareId(savedFirmware.getId()); - thrown.expect(DataValidationException.class); - thrown.expectMessage("Can't assign firmware with different deviceProfile!"); - deviceService.saveDevice(savedDevice); + assertThatThrownBy(() -> deviceService.saveDevice(savedDevice)) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("Can't assign firmware with different deviceProfile!"); } @Test @@ -389,12 +401,6 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { @Test public void testFindDevicesByTenantId() { - Tenant tenant = new Tenant(); - tenant.setTitle("Test tenant"); - tenant = tenantService.saveTenant(tenant); - - TenantId tenantId = tenant.getId(); - List devices = new ArrayList<>(); for (int i = 0; i < 178; i++) { Device device = new Device(); @@ -426,8 +432,6 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { pageData = deviceService.findDevicesByTenantId(tenantId, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertTrue(pageData.getData().isEmpty()); - - tenantService.deleteTenant(tenantId); } @Test @@ -588,12 +592,6 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { @Test public void testFindDevicesByTenantIdAndCustomerId() { - Tenant tenant = new Tenant(); - tenant.setTitle("Test tenant"); - tenant = tenantService.saveTenant(tenant); - - TenantId tenantId = tenant.getId(); - Customer customer = new Customer(); customer.setTitle("Test customer"); customer.setTenantId(tenantId); @@ -632,8 +630,6 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { pageData = deviceService.findDeviceInfosByTenantIdAndCustomerId(tenantId, customerId, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertTrue(pageData.getData().isEmpty()); - - tenantService.deleteTenant(tenantId); } @Test diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseEdgeEventServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseEdgeEventServiceTest.java index 3bd47cc991..3b942dab5f 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseEdgeEventServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseEdgeEventServiceTest.java @@ -21,6 +21,7 @@ import com.google.common.util.concurrent.ListenableFuture; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.server.common.data.edge.EdgeEvent; import org.thingsboard.server.common.data.edge.EdgeEventActionType; import org.thingsboard.server.common.data.edge.EdgeEventType; @@ -32,20 +33,20 @@ import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.SortOrder; import org.thingsboard.server.common.data.page.TimePageLink; +import org.thingsboard.server.dao.edge.EdgeEventService; import java.io.IOException; import java.text.ParseException; -import java.time.LocalDateTime; -import java.time.Month; -import java.time.ZoneOffset; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.TimeUnit; import static org.apache.commons.lang3.time.DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT; public abstract class BaseEdgeEventServiceTest extends AbstractServiceTest { + @Autowired + EdgeEventService edgeEventService; + long timeBeforeStartTime; long startTime; long eventTime; diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseEdgeServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseEdgeServiceTest.java index 70d41f4681..9b307b2f8c 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseEdgeServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseEdgeServiceTest.java @@ -17,11 +17,10 @@ package org.thingsboard.server.dao.service; import com.datastax.oss.driver.api.core.uuid.Uuids; import com.fasterxml.jackson.databind.node.ObjectNode; -import org.junit.After; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import org.junit.jupiter.api.Assertions; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.EntitySubtype; @@ -36,7 +35,10 @@ import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainMetaData; import org.thingsboard.server.common.data.rule.RuleChainType; import org.thingsboard.server.common.data.rule.RuleNode; +import org.thingsboard.server.dao.customer.CustomerService; +import org.thingsboard.server.dao.edge.EdgeService; import org.thingsboard.server.dao.exception.DataValidationException; +import org.thingsboard.server.dao.rule.RuleChainService; import java.util.ArrayList; import java.util.Arrays; @@ -47,23 +49,14 @@ import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; public abstract class BaseEdgeServiceTest extends AbstractServiceTest { - private IdComparator idComparator = new IdComparator<>(); - - private TenantId tenantId; - - @Before - public void before() { - Tenant tenant = new Tenant(); - tenant.setTitle("My tenant"); - Tenant savedTenant = tenantService.saveTenant(tenant); - Assert.assertNotNull(savedTenant); - tenantId = savedTenant.getId(); - } + @Autowired + CustomerService customerService; + @Autowired + EdgeService edgeService; + @Autowired + RuleChainService ruleChainService; - @After - public void after() { - tenantService.deleteTenant(tenantId); - } + private IdComparator idComparator = new IdComparator<>(); @Test public void testSaveEdge() { @@ -204,12 +197,6 @@ public abstract class BaseEdgeServiceTest extends AbstractServiceTest { @Test public void testFindEdgesByTenantId() { - Tenant tenant = new Tenant(); - tenant.setTitle("Test tenant"); - tenant = tenantService.saveTenant(tenant); - - TenantId tenantId = tenant.getId(); - List edges = new ArrayList<>(); for (int i = 0; i < 178; i++) { Edge edge = constructEdge(tenantId, "Edge " + i, "default"); @@ -238,8 +225,6 @@ public abstract class BaseEdgeServiceTest extends AbstractServiceTest { pageData = edgeService.findEdgesByTenantId(tenantId, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertTrue(pageData.getData().isEmpty()); - - tenantService.deleteTenant(tenantId); } @Test @@ -388,12 +373,6 @@ public abstract class BaseEdgeServiceTest extends AbstractServiceTest { @Test public void testFindEdgesByTenantIdAndCustomerId() { - Tenant tenant = new Tenant(); - tenant.setTitle("Test tenant"); - tenant = tenantService.saveTenant(tenant); - - TenantId tenantId = tenant.getId(); - Customer customer = new Customer(); customer.setTitle("Test customer"); customer.setTenantId(tenantId); @@ -429,8 +408,6 @@ public abstract class BaseEdgeServiceTest extends AbstractServiceTest { pageData = edgeService.findEdgesByTenantIdAndCustomerId(tenantId, customerId, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertTrue(pageData.getData().isEmpty()); - - tenantService.deleteTenant(tenantId); } @Test diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseEntityServiceRegistryTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseEntityServiceRegistryTest.java index 81a512dc95..87b6bbdd80 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseEntityServiceRegistryTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseEntityServiceRegistryTest.java @@ -28,7 +28,7 @@ import org.thingsboard.server.dao.rule.RuleChainService; public abstract class BaseEntityServiceRegistryTest extends AbstractServiceTest { @Autowired - private EntityServiceRegistry entityServiceRegistry; + EntityServiceRegistry entityServiceRegistry; @Test public void givenAllEntityTypes_whenGetServiceByEntityTypeCalled_thenAllBeansExists() { diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseEntityServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseEntityServiceTest.java index 7c80bef095..1af63c0ed7 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseEntityServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseEntityServiceTest.java @@ -21,9 +21,7 @@ import com.google.common.util.concurrent.ListenableFuture; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.RandomUtils; import org.hamcrest.Matchers; -import org.junit.After; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.ResultSetExtractor; @@ -31,7 +29,6 @@ import org.thingsboard.server.common.data.DataConstants; import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.StringUtils; -import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.id.CustomerId; @@ -73,8 +70,13 @@ import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.relation.EntitySearchDirection; import org.thingsboard.server.common.data.relation.RelationEntityTypeFilter; import org.thingsboard.server.common.data.relation.RelationTypeGroup; +import org.thingsboard.server.dao.asset.AssetService; import org.thingsboard.server.dao.attributes.AttributesService; +import org.thingsboard.server.dao.device.DeviceService; +import org.thingsboard.server.dao.edge.EdgeService; +import org.thingsboard.server.dao.entity.EntityService; import org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity; +import org.thingsboard.server.dao.relation.RelationService; import org.thingsboard.server.dao.sql.relation.RelationRepository; import org.thingsboard.server.dao.timeseries.TimeseriesService; @@ -100,30 +102,21 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest { static final int ENTITY_COUNT = 5; @Autowired - private AttributesService attributesService; - + AssetService assetService; @Autowired - private TimeseriesService timeseriesService; - - private TenantId tenantId; - + AttributesService attributesService; @Autowired - private RelationRepository relationRepository; - - @Before - public void before() { - Tenant tenant = new Tenant(); - tenant.setTitle("My tenant"); - Tenant savedTenant = tenantService.saveTenant(tenant); - Assert.assertNotNull(savedTenant); - tenantId = savedTenant.getId(); - } - - @After - public void after() { - tenantService.deleteTenant(tenantId); - } - + DeviceService deviceService; + @Autowired + EdgeService edgeService; + @Autowired + EntityService entityService; + @Autowired + RelationRepository relationRepository; + @Autowired + RelationService relationService; + @Autowired + TimeseriesService timeseriesService; @Test public void testCountEntitiesByQuery() throws InterruptedException { diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseOtaPackageServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseOtaPackageServiceTest.java index c89e7c4b69..d430e1945a 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseOtaPackageServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseOtaPackageServiceTest.java @@ -19,16 +19,14 @@ import com.datastax.oss.driver.api.core.uuid.Uuids; import org.junit.After; import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.OtaPackage; import org.thingsboard.server.common.data.OtaPackageInfo; import org.thingsboard.server.common.data.StringUtils; -import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.TenantProfile; import org.thingsboard.server.common.data.id.DeviceProfileId; import org.thingsboard.server.common.data.id.TenantId; @@ -36,7 +34,11 @@ import org.thingsboard.server.common.data.ota.ChecksumAlgorithm; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration; +import org.thingsboard.server.dao.device.DeviceProfileService; +import org.thingsboard.server.dao.device.DeviceService; import org.thingsboard.server.dao.exception.DataValidationException; +import org.thingsboard.server.dao.ota.OtaPackageService; +import org.thingsboard.server.dao.tenant.TenantProfileService; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -44,6 +46,7 @@ import java.util.Collections; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.thingsboard.server.common.data.ota.OtaPackageType.FIRMWARE; public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { @@ -60,28 +63,25 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { private final IdComparator idComparator = new IdComparator<>(); - private TenantId tenantId; - private DeviceProfileId deviceProfileId; + @Autowired + DeviceProfileService deviceProfileService; + @Autowired + DeviceService deviceService; + @Autowired + OtaPackageService otaPackageService; + @Autowired + TenantProfileService tenantProfileService; + @Before public void before() { - Tenant tenant = new Tenant(); - tenant.setTitle("My tenant"); - Tenant savedTenant = tenantService.saveTenant(tenant); - Assert.assertNotNull(savedTenant); - tenantId = savedTenant.getId(); - DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Device Profile"); DeviceProfile savedDeviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile); Assert.assertNotNull(savedDeviceProfile); deviceProfileId = savedDeviceProfile.getId(); } - @SuppressWarnings("deprecation") - @Rule - public ExpectedException thrown = ExpectedException.none(); - @After public void after() { tenantService.deleteTenant(tenantId); @@ -99,9 +99,9 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { createAndSaveFirmware(tenantId, "1"); Assert.assertEquals(1, otaPackageService.sumDataSizeByTenantId(tenantId)); - thrown.expect(DataValidationException.class); - thrown.expectMessage(String.format("Failed to create the ota package, files size limit is exhausted %d bytes!", DATA_SIZE)); - createAndSaveFirmware(tenantId, "2"); + assertThatThrownBy(() -> createAndSaveFirmware(tenantId, "2")) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("Failed to create the ota package, files size limit is exhausted %d bytes!", DATA_SIZE); } @Test @@ -243,9 +243,9 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmware.setChecksum(CHECKSUM); firmware.setData(DATA); - thrown.expect(DataValidationException.class); - thrown.expectMessage("OtaPackage should be assigned to tenant!"); - otaPackageService.saveOtaPackage(firmware); + assertThatThrownBy(() -> otaPackageService.saveOtaPackage(firmware)) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("OtaPackage should be assigned to tenant!"); } @Test @@ -261,9 +261,9 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmware.setChecksum(CHECKSUM); firmware.setData(DATA); - thrown.expect(DataValidationException.class); - thrown.expectMessage("Type should be specified!"); - otaPackageService.saveOtaPackage(firmware); + assertThatThrownBy(() -> otaPackageService.saveOtaPackage(firmware)) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("Type should be specified!"); } @Test @@ -279,9 +279,9 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmware.setChecksum(CHECKSUM); firmware.setData(DATA); - thrown.expect(DataValidationException.class); - thrown.expectMessage("OtaPackage title should be specified!"); - otaPackageService.saveOtaPackage(firmware); + assertThatThrownBy(() -> otaPackageService.saveOtaPackage(firmware)) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("OtaPackage title should be specified!"); } @Test @@ -297,9 +297,9 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmware.setChecksum(CHECKSUM); firmware.setData(DATA); - thrown.expect(DataValidationException.class); - thrown.expectMessage("OtaPackage file name should be specified!"); - otaPackageService.saveOtaPackage(firmware); + assertThatThrownBy(() -> otaPackageService.saveOtaPackage(firmware)) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("OtaPackage file name should be specified!"); } @Test @@ -315,9 +315,9 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmware.setChecksum(CHECKSUM); firmware.setData(DATA); - thrown.expect(DataValidationException.class); - thrown.expectMessage("OtaPackage content type should be specified!"); - otaPackageService.saveOtaPackage(firmware); + assertThatThrownBy(() -> otaPackageService.saveOtaPackage(firmware)) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("OtaPackage content type should be specified!"); } @Test @@ -333,9 +333,9 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmware.setChecksumAlgorithm(CHECKSUM_ALGORITHM); firmware.setChecksum(CHECKSUM); - thrown.expect(DataValidationException.class); - thrown.expectMessage("OtaPackage data should be specified!"); - otaPackageService.saveOtaPackage(firmware); + assertThatThrownBy(() -> otaPackageService.saveOtaPackage(firmware)) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("OtaPackage data should be specified!"); } @Test @@ -352,9 +352,9 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmware.setChecksum(CHECKSUM); firmware.setData(DATA); - thrown.expect(DataValidationException.class); - thrown.expectMessage("OtaPackage is referencing to non-existent tenant!"); - otaPackageService.saveOtaPackage(firmware); + assertThatThrownBy(() -> otaPackageService.saveOtaPackage(firmware)) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("OtaPackage is referencing to non-existent tenant!"); } @Test @@ -371,9 +371,9 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmware.setChecksum(CHECKSUM); firmware.setData(DATA); - thrown.expect(DataValidationException.class); - thrown.expectMessage("OtaPackage is referencing to non-existent device profile!"); - otaPackageService.saveOtaPackage(firmware); + assertThatThrownBy(() -> otaPackageService.saveOtaPackage(firmware)) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("OtaPackage is referencing to non-existent device profile!"); } @Test @@ -389,9 +389,9 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmware.setChecksumAlgorithm(CHECKSUM_ALGORITHM); firmware.setData(DATA); - thrown.expect(DataValidationException.class); - thrown.expectMessage("OtaPackage checksum should be specified!"); - otaPackageService.saveOtaPackage(firmware); + assertThatThrownBy(() -> otaPackageService.saveOtaPackage(firmware)) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("OtaPackage checksum should be specified!"); } @Test @@ -411,17 +411,17 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { newFirmwareInfo.setTitle(TITLE); newFirmwareInfo.setVersion(VERSION); - thrown.expect(DataValidationException.class); - thrown.expectMessage("OtaPackage with such title and version already exists!"); - otaPackageService.saveOtaPackageInfo(newFirmwareInfo, false); + assertThatThrownBy(() -> otaPackageService.saveOtaPackageInfo(newFirmwareInfo, false)) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("OtaPackage with such title and version already exists!"); } @Test public void testSaveFirmwareWithExistingTitleAndVersion() { createAndSaveFirmware(tenantId, VERSION); - thrown.expect(DataValidationException.class); - thrown.expectMessage("OtaPackage with such title and version already exists!"); - createAndSaveFirmware(tenantId, VERSION); + assertThatThrownBy(() -> createAndSaveFirmware(tenantId, VERSION)) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("OtaPackage with such title and version already exists!"); } @Test @@ -436,9 +436,9 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { Device savedDevice = deviceService.saveDevice(device); try { - thrown.expect(DataValidationException.class); - thrown.expectMessage("The otaPackage referenced by the devices cannot be deleted!"); - otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId()); + assertThatThrownBy(() -> otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId())) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("The otaPackage referenced by the devices cannot be deleted!"); } finally { deviceService.deleteDevice(tenantId, savedDevice.getId()); otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId()); @@ -448,12 +448,12 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { @Test public void testUpdateDeviceProfileId() { OtaPackage savedFirmware = createAndSaveFirmware(tenantId, VERSION); + savedFirmware.setDeviceProfileId(null); try { - thrown.expect(DataValidationException.class); - thrown.expectMessage("Updating otaPackage deviceProfile is prohibited!"); - savedFirmware.setDeviceProfileId(null); - otaPackageService.saveOtaPackage(savedFirmware); + assertThatThrownBy(() -> otaPackageService.saveOtaPackage(savedFirmware)) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("Updating otaPackage deviceProfile is prohibited!"); } finally { otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId()); } @@ -482,9 +482,9 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { deviceProfileService.saveDeviceProfile(savedDeviceProfile); try { - thrown.expect(DataValidationException.class); - thrown.expectMessage("The otaPackage referenced by the device profile cannot be deleted!"); - otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId()); + assertThatThrownBy(() -> otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId())) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("The otaPackage referenced by the device profile cannot be deleted!"); } finally { deviceProfileService.deleteDeviceProfile(tenantId, savedDeviceProfile.getId()); } @@ -636,12 +636,18 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmwareInfo.setType(FIRMWARE); firmwareInfo.setTitle(TITLE); firmwareInfo.setVersion(VERSION); + firmwareInfo.setUrl(" "); - thrown.expect(DataValidationException.class); - thrown.expectMessage("Ota package URL should be specified!"); - otaPackageService.saveOtaPackageInfo(firmwareInfo, true); + assertThatThrownBy(() -> otaPackageService.saveOtaPackageInfo(firmwareInfo, true)) + .as("firmwareInfo url set whitespaces") + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("Ota package URL should be specified!"); + firmwareInfo.setUrl(""); - otaPackageService.saveOtaPackageInfo(firmwareInfo, true); + assertThatThrownBy(() -> otaPackageService.saveOtaPackageInfo(firmwareInfo, true)) + .as("firmwareInfo url is empty") + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("Ota package URL should be specified!"); } @Test @@ -655,12 +661,10 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmwareInfo.setTenantId(tenantId); OtaPackageInfo savedFirmwareInfo = otaPackageService.saveOtaPackageInfo(firmwareInfo, true); - - thrown.expect(DataValidationException.class); - thrown.expectMessage("Updating otaPackage URL is prohibited!"); - savedFirmwareInfo.setUrl("https://newurl.com"); - otaPackageService.saveOtaPackageInfo(savedFirmwareInfo, true); + assertThatThrownBy(() -> otaPackageService.saveOtaPackageInfo(savedFirmwareInfo, true)) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("Updating otaPackage URL is prohibited!"); } @Test @@ -673,10 +677,9 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmwareInfo.setUrl(URL); firmwareInfo.setTenantId(tenantId); - thrown.expect(DataValidationException.class); - thrown.expectMessage("title length must be equal or less than 255"); - - otaPackageService.saveOtaPackageInfo(firmwareInfo, true); + assertThatThrownBy(() -> otaPackageService.saveOtaPackageInfo(firmwareInfo, true)) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("title length must be equal or less than 255"); } @Test @@ -687,11 +690,11 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmwareInfo.setUrl(URL); firmwareInfo.setTenantId(tenantId); firmwareInfo.setTitle(TITLE); - firmwareInfo.setVersion(StringUtils.random(257)); - thrown.expectMessage("version length must be equal or less than 255"); - otaPackageService.saveOtaPackageInfo(firmwareInfo, true); + assertThatThrownBy(() -> otaPackageService.saveOtaPackageInfo(firmwareInfo, true)) + .isInstanceOf(DataValidationException.class) + .hasMessageContaining("version length must be equal or less than 255"); } private OtaPackage createAndSaveFirmware(TenantId tenantId, String version) { diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseQueueServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseQueueServiceTest.java index af2f067c2e..9d2c2bac5a 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseQueueServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseQueueServiceTest.java @@ -20,6 +20,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.jupiter.api.Assertions; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.server.common.data.DataConstants; import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.TenantProfile; @@ -35,6 +36,8 @@ import org.thingsboard.server.common.data.queue.SubmitStrategyType; import org.thingsboard.server.common.data.tenant.profile.TenantProfileData; import org.thingsboard.server.common.data.tenant.profile.TenantProfileQueueConfiguration; import org.thingsboard.server.dao.exception.DataValidationException; +import org.thingsboard.server.dao.queue.QueueService; +import org.thingsboard.server.dao.tenant.TenantProfileService; import java.util.ArrayList; import java.util.Collections; @@ -42,6 +45,11 @@ import java.util.List; public abstract class BaseQueueServiceTest extends AbstractServiceTest { + @Autowired + TenantProfileService tenantProfileService; + @Autowired + QueueService queueService; + private IdComparator idComparator = new IdComparator<>(); private TenantId tenantId; @@ -83,7 +91,7 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest { Tenant tenant = new Tenant(); tenant.setTitle("My tenant"); - tenant.setTenantProfileId(tenantProfileId); + tenant.setTenantProfileId(tenantProfileId); //custom profile Tenant savedTenant = tenantService.saveTenant(tenant); Assert.assertNotNull(savedTenant); tenantId = savedTenant.getId(); diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseRelationServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseRelationServiceTest.java index c920b86bd5..d60a896aec 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseRelationServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseRelationServiceTest.java @@ -23,6 +23,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.jupiter.api.Assertions; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.id.AssetId; import org.thingsboard.server.common.data.id.DeviceId; @@ -33,6 +34,7 @@ import org.thingsboard.server.common.data.relation.RelationEntityTypeFilter; import org.thingsboard.server.common.data.relation.RelationTypeGroup; import org.thingsboard.server.common.data.relation.RelationsSearchParameters; import org.thingsboard.server.dao.exception.DataValidationException; +import org.thingsboard.server.dao.relation.RelationService; import java.util.ArrayList; import java.util.Collections; @@ -42,6 +44,9 @@ import java.util.concurrent.ExecutionException; public abstract class BaseRelationServiceTest extends AbstractServiceTest { + @Autowired + RelationService relationService; + @Before public void before() { } diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseRuleChainServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseRuleChainServiceTest.java index 16901119a6..e3c5234942 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseRuleChainServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseRuleChainServiceTest.java @@ -17,11 +17,10 @@ package org.thingsboard.server.dao.service; import com.datastax.oss.driver.api.core.uuid.Uuids; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.After; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import org.junit.jupiter.api.Assertions; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.edge.Edge; @@ -34,7 +33,9 @@ import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainMetaData; import org.thingsboard.server.common.data.rule.RuleChainType; import org.thingsboard.server.common.data.rule.RuleNode; +import org.thingsboard.server.dao.edge.EdgeService; import org.thingsboard.server.dao.exception.DataValidationException; +import org.thingsboard.server.dao.rule.RuleChainService; import java.io.IOException; import java.util.ArrayList; @@ -46,25 +47,14 @@ import java.util.List; */ public abstract class BaseRuleChainServiceTest extends AbstractServiceTest { + @Autowired + EdgeService edgeService; + @Autowired + RuleChainService ruleChainService; + private IdComparator idComparator = new IdComparator<>(); private IdComparator ruleNodeIdComparator = new IdComparator<>(); - private TenantId tenantId; - - @Before - public void before() { - Tenant tenant = new Tenant(); - tenant.setTitle("My tenant"); - Tenant savedTenant = tenantService.saveTenant(tenant); - Assert.assertNotNull(savedTenant); - tenantId = savedTenant.getId(); - } - - @After - public void after() { - tenantService.deleteTenant(tenantId); - } - @Test public void testSaveRuleChain() throws IOException { RuleChain ruleChain = new RuleChain(); @@ -133,12 +123,6 @@ public abstract class BaseRuleChainServiceTest extends AbstractServiceTest { @Test public void testFindRuleChainsByTenantId() { - Tenant tenant = new Tenant(); - tenant.setTitle("Test tenant"); - tenant = tenantService.saveTenant(tenant); - - TenantId tenantId = tenant.getId(); - List ruleChains = new ArrayList<>(); for (int i = 0; i < 165; i++) { RuleChain ruleChain = new RuleChain(); @@ -169,8 +153,6 @@ public abstract class BaseRuleChainServiceTest extends AbstractServiceTest { pageData = ruleChainService.findTenantRuleChainsByType(tenantId, RuleChainType.CORE, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertTrue(pageData.getData().isEmpty()); - - tenantService.deleteTenant(tenantId); } @Test diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseTenantProfileServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseTenantProfileServiceTest.java index 926f680e40..6d41da2965 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseTenantProfileServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseTenantProfileServiceTest.java @@ -18,8 +18,10 @@ package org.thingsboard.server.dao.service; import com.fasterxml.jackson.databind.node.NullNode; import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import org.junit.jupiter.api.Assertions; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.server.common.data.DataConstants; import org.thingsboard.server.common.data.EntityInfo; import org.thingsboard.server.common.data.Tenant; @@ -36,6 +38,7 @@ import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileCon import org.thingsboard.server.common.data.tenant.profile.TenantProfileData; import org.thingsboard.server.common.data.tenant.profile.TenantProfileQueueConfiguration; import org.thingsboard.server.dao.exception.DataValidationException; +import org.thingsboard.server.dao.tenant.TenantProfileService; import java.util.ArrayList; import java.util.Collections; @@ -44,9 +47,20 @@ import java.util.stream.Collectors; public abstract class BaseTenantProfileServiceTest extends AbstractServiceTest { + @Autowired + TenantProfileService tenantProfileService; + private IdComparator idComparator = new IdComparator<>(); private IdComparator tenantProfileInfoIdComparator = new IdComparator<>(); + @Before + public void before() { + //this test requires no Tenants in the database + tenantId = null; + tenantService.deleteTenants(); + tenantProfileService.deleteTenantProfiles(TenantId.SYS_TENANT_ID); + } + @After public void after() { tenantProfileService.deleteTenantProfiles(TenantId.SYS_TENANT_ID); diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseTenantServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseTenantServiceTest.java index 3f8c33c5e7..938272c0a8 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseTenantServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseTenantServiceTest.java @@ -23,24 +23,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.SpyBean; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.cache.TbTransactionalCache; -import org.thingsboard.server.common.data.Customer; -import org.thingsboard.server.common.data.Dashboard; -import org.thingsboard.server.common.data.DashboardInfo; -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.EntityView; -import org.thingsboard.server.common.data.OtaPackage; -import org.thingsboard.server.common.data.OtaPackageInfo; -import org.thingsboard.server.common.data.ResourceType; -import org.thingsboard.server.common.data.StringUtils; -import org.thingsboard.server.common.data.TbResource; -import org.thingsboard.server.common.data.TbResourceInfo; -import org.thingsboard.server.common.data.Tenant; -import org.thingsboard.server.common.data.TenantInfo; -import org.thingsboard.server.common.data.TenantProfile; -import org.thingsboard.server.common.data.User; +import org.thingsboard.server.common.data.*; import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.common.data.device.profile.DeviceProfileData; import org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration; @@ -54,8 +37,23 @@ import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainType; import org.thingsboard.server.common.data.security.Authority; import org.thingsboard.server.common.data.widget.WidgetsBundle; +import org.thingsboard.server.dao.asset.AssetService; +import org.thingsboard.server.dao.customer.CustomerService; +import org.thingsboard.server.dao.dashboard.DashboardService; +import org.thingsboard.server.dao.device.DeviceProfileService; +import org.thingsboard.server.dao.device.DeviceService; +import org.thingsboard.server.dao.edge.EdgeService; +import org.thingsboard.server.dao.entityview.EntityViewService; import org.thingsboard.server.dao.exception.DataValidationException; +import org.thingsboard.server.dao.ota.OtaPackageService; +import org.thingsboard.server.dao.resource.ResourceService; +import org.thingsboard.server.dao.rpc.RpcService; +import org.thingsboard.server.dao.rule.RuleChainService; import org.thingsboard.server.dao.tenant.TenantDao; +import org.thingsboard.server.dao.tenant.TenantProfileService; +import org.thingsboard.server.dao.usagerecord.ApiUsageStateService; +import org.thingsboard.server.dao.user.UserService; +import org.thingsboard.server.dao.widget.WidgetsBundleService; import java.util.ArrayList; import java.util.Collections; @@ -69,16 +67,45 @@ import static org.mockito.Mockito.verify; public abstract class BaseTenantServiceTest extends AbstractServiceTest { - private IdComparator idComparator = new IdComparator<>(); - @SpyBean - protected TenantDao tenantDao; + TenantDao tenantDao; @Autowired - protected TbTransactionalCache cache; - + ApiUsageStateService apiUsageStateService; + @Autowired + AssetService assetService; + @Autowired + CustomerService customerService; + @Autowired + DashboardService dashboardService; + @Autowired + DeviceProfileService deviceProfileService; + @Autowired + DeviceService deviceService; + @Autowired + EdgeService edgeService; + @Autowired + EntityViewService entityViewService; + @Autowired + OtaPackageService otaPackageService; @Autowired - protected TbTransactionalCache existsTenantCache; + ResourceService resourceService; + @Autowired + RpcService rpcService; + @Autowired + RuleChainService ruleChainService; + @Autowired + TbTransactionalCache existsTenantCache; + @Autowired + TbTransactionalCache cache; + @Autowired + TenantProfileService tenantProfileService; + @Autowired + UserService userService; + @Autowired + WidgetsBundleService widgetsBundleService; + + private final IdComparator idComparator = new IdComparator<>(); @Test public void testSaveTenant() { @@ -150,6 +177,7 @@ public abstract class BaseTenantServiceTest extends AbstractServiceTest { @Test public void testFindTenants() { + tenantService.deleteTenants(); List tenants = new ArrayList<>(); PageLink pageLink = new PageLink(17); PageData pageData = tenantService.findTenants(pageLink); @@ -264,7 +292,7 @@ public abstract class BaseTenantServiceTest extends AbstractServiceTest { @Test public void testFindTenantInfos() { - + tenantService.deleteTenants(); List tenants = new ArrayList<>(); PageLink pageLink = new PageLink(17); PageData pageData = tenantService.findTenantInfos(pageLink); diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseUserServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseUserServiceTest.java index 49ececc8e4..14917f6807 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseUserServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseUserServiceTest.java @@ -15,11 +15,11 @@ */ package org.thingsboard.server.dao.service; -import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.jupiter.api.Assertions; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.StringUtils; @@ -33,7 +33,9 @@ import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.security.Authority; import org.thingsboard.server.common.data.security.UserCredentials; import org.thingsboard.server.common.data.security.UserSettings; +import org.thingsboard.server.dao.customer.CustomerService; import org.thingsboard.server.dao.exception.DataValidationException; +import org.thingsboard.server.dao.user.UserService; import java.util.ArrayList; import java.util.Collections; @@ -41,19 +43,17 @@ import java.util.List; public abstract class BaseUserServiceTest extends AbstractServiceTest { + @Autowired + CustomerService customerService; + @Autowired + UserService userService; + private IdComparator idComparator = new IdComparator<>(); - private TenantId tenantId; private UserSettings userSettings; @Before public void before() { - Tenant tenant = new Tenant(); - tenant.setTitle("My tenant"); - Tenant savedTenant = tenantService.saveTenant(tenant); - Assert.assertNotNull(savedTenant); - tenantId = savedTenant.getId(); - User tenantAdmin = new User(); tenantAdmin.setAuthority(Authority.TENANT_ADMIN); tenantAdmin.setTenantId(tenantId); @@ -75,11 +75,6 @@ public abstract class BaseUserServiceTest extends AbstractServiceTest { userSettings = createUserSettings(customerUser.getId()); } - @After - public void after() { - tenantService.deleteTenant(tenantId); - } - @Test public void testFindUserByEmail() { User user = userService.findUserByEmail(SYSTEM_TENANT_ID, "sysadmin@thingsboard.org"); @@ -209,17 +204,13 @@ public abstract class BaseUserServiceTest extends AbstractServiceTest { Assert.assertEquals(1, users.size()); Assert.assertEquals(tenantAdminUser, users.get(0)); - Tenant tenant = new Tenant(); - tenant.setTitle("Test tenant"); - tenant = tenantService.saveTenant(tenant); - - TenantId tenantId = tenant.getId(); + TenantId secondTenantId = createTenant(); List tenantAdmins = new ArrayList<>(); for (int i = 0; i < 124; i++) { User user = new User(); user.setAuthority(Authority.TENANT_ADMIN); - user.setTenantId(tenantId); + user.setTenantId(secondTenantId); user.setEmail("testTenant" + i + "@thingsboard.org"); tenantAdmins.add(userService.saveUser(user)); } @@ -227,7 +218,7 @@ public abstract class BaseUserServiceTest extends AbstractServiceTest { List loadedTenantAdmins = new ArrayList<>(); PageLink pageLink = new PageLink(33); do { - pageData = userService.findTenantAdmins(tenantId, pageLink); + pageData = userService.findTenantAdmins(secondTenantId, pageLink); loadedTenantAdmins.addAll(pageData.getData()); if (pageData.hasNext()) { pageLink = pageLink.nextPageLink(); @@ -239,10 +230,10 @@ public abstract class BaseUserServiceTest extends AbstractServiceTest { Assert.assertEquals(tenantAdmins, loadedTenantAdmins); - tenantService.deleteTenant(tenantId); + tenantService.deleteTenant(secondTenantId); pageLink = new PageLink(33); - pageData = userService.findTenantAdmins(tenantId, pageLink); + pageData = userService.findTenantAdmins(secondTenantId, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertTrue(pageData.getData().isEmpty()); @@ -250,12 +241,6 @@ public abstract class BaseUserServiceTest extends AbstractServiceTest { @Test public void testFindTenantAdminsByEmail() { - Tenant tenant = new Tenant(); - tenant.setTitle("Test tenant"); - tenant = tenantService.saveTenant(tenant); - - TenantId tenantId = tenant.getId(); - String email1 = "testEmail1"; List tenantAdminsEmail1 = new ArrayList<>(); @@ -332,8 +317,6 @@ public abstract class BaseUserServiceTest extends AbstractServiceTest { pageData = userService.findTenantAdmins(tenantId, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertEquals(0, pageData.getData().size()); - - tenantService.deleteTenant(tenantId); } @Test @@ -346,12 +329,6 @@ public abstract class BaseUserServiceTest extends AbstractServiceTest { Assert.assertEquals(1, users.size()); Assert.assertEquals(customerUser, users.get(0)); - Tenant tenant = new Tenant(); - tenant.setTitle("Test tenant"); - tenant = tenantService.saveTenant(tenant); - - TenantId tenantId = tenant.getId(); - Customer customer = new Customer(); customer.setTitle("Test customer"); customer.setTenantId(tenantId); @@ -394,12 +371,6 @@ public abstract class BaseUserServiceTest extends AbstractServiceTest { @Test public void testFindCustomerUsersByEmail() { - Tenant tenant = new Tenant(); - tenant.setTitle("Test tenant"); - tenant = tenantService.saveTenant(tenant); - - TenantId tenantId = tenant.getId(); - Customer customer = new Customer(); customer.setTitle("Test customer"); customer.setTenantId(tenantId); @@ -485,8 +456,6 @@ public abstract class BaseUserServiceTest extends AbstractServiceTest { pageData = userService.findCustomerUsers(tenantId, customerId, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertEquals(0, pageData.getData().size()); - - tenantService.deleteTenant(tenantId); } private UserSettings createUserSettings(UserId userId) { diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseWidgetTypeServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseWidgetTypeServiceTest.java index 04cc1d8412..b04053f6d9 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseWidgetTypeServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseWidgetTypeServiceTest.java @@ -18,18 +18,18 @@ package org.thingsboard.server.dao.service; import com.datastax.oss.driver.api.core.uuid.Uuids; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.After; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import org.junit.jupiter.api.Assertions; -import org.thingsboard.server.common.data.Tenant; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.widget.WidgetType; import org.thingsboard.server.common.data.widget.WidgetTypeDetails; import org.thingsboard.server.common.data.widget.WidgetsBundle; import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.model.ModelConstants; +import org.thingsboard.server.dao.widget.WidgetTypeService; +import org.thingsboard.server.dao.widget.WidgetsBundleService; import java.io.IOException; import java.util.ArrayList; @@ -38,23 +38,12 @@ import java.util.List; public abstract class BaseWidgetTypeServiceTest extends AbstractServiceTest { - private IdComparator idComparator = new IdComparator<>(); - - private TenantId tenantId; - - @Before - public void before() { - Tenant tenant = new Tenant(); - tenant.setTitle("My tenant"); - Tenant savedTenant = tenantService.saveTenant(tenant); - Assert.assertNotNull(savedTenant); - tenantId = savedTenant.getId(); - } + @Autowired + WidgetsBundleService widgetsBundleService; + @Autowired + WidgetTypeService widgetTypeService; - @After - public void after() { - tenantService.deleteTenant(tenantId); - } + private IdComparator idComparator = new IdComparator<>(); @Test public void testSaveWidgetType() throws IOException { diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseWidgetsBundleServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseWidgetsBundleServiceTest.java index c28f5face7..8f04eaec62 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseWidgetsBundleServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseWidgetsBundleServiceTest.java @@ -16,18 +16,17 @@ package org.thingsboard.server.dao.service; import com.datastax.oss.driver.api.core.uuid.Uuids; -import org.junit.After; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import org.junit.jupiter.api.Assertions; -import org.thingsboard.server.common.data.Tenant; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.widget.WidgetsBundle; import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.model.ModelConstants; +import org.thingsboard.server.dao.widget.WidgetsBundleService; import java.io.IOException; import java.util.ArrayList; @@ -36,23 +35,10 @@ import java.util.List; public abstract class BaseWidgetsBundleServiceTest extends AbstractServiceTest { - private IdComparator idComparator = new IdComparator<>(); - - private TenantId tenantId; - - @Before - public void before() { - Tenant tenant = new Tenant(); - tenant.setTitle("My tenant"); - Tenant savedTenant = tenantService.saveTenant(tenant); - Assert.assertNotNull(savedTenant); - tenantId = savedTenant.getId(); - } + @Autowired + WidgetsBundleService widgetsBundleService; - @After - public void after() { - tenantService.deleteTenant(tenantId); - } + private IdComparator idComparator = new IdComparator<>(); @Test public void testSaveWidgetsBundle() throws IOException { @@ -249,11 +235,6 @@ public abstract class BaseWidgetsBundleServiceTest extends AbstractServiceTest { @Test public void testFindTenantWidgetsBundlesByTenantId() { - Tenant tenant = new Tenant(); - tenant.setTitle("Test tenant"); - tenant = tenantService.saveTenant(tenant); - - TenantId tenantId = tenant.getId(); List widgetsBundles = new ArrayList<>(); for (int i=0;i<127;i++) { @@ -286,7 +267,6 @@ public abstract class BaseWidgetsBundleServiceTest extends AbstractServiceTest { Assert.assertFalse(pageData.hasNext()); Assert.assertTrue(pageData.getData().isEmpty()); - tenantService.deleteTenant(tenantId); } @Test @@ -294,11 +274,6 @@ public abstract class BaseWidgetsBundleServiceTest extends AbstractServiceTest { List systemWidgetsBundles = widgetsBundleService.findSystemWidgetsBundles(tenantId); - Tenant tenant = new Tenant(); - tenant.setTitle("Test tenant"); - tenant = tenantService.saveTenant(tenant); - - TenantId tenantId = tenant.getId(); TenantId systemTenantId = TenantId.fromUUID(ModelConstants.NULL_UUID); List createdWidgetsBundles = new ArrayList<>(); @@ -371,8 +346,6 @@ public abstract class BaseWidgetsBundleServiceTest extends AbstractServiceTest { Collections.sort(loadedWidgetsBundles, idComparator); Assert.assertEquals(systemWidgetsBundles, loadedWidgetsBundles); - - tenantService.deleteTenant(tenantId); } @Test @@ -380,11 +353,6 @@ public abstract class BaseWidgetsBundleServiceTest extends AbstractServiceTest { List systemWidgetsBundles = widgetsBundleService.findSystemWidgetsBundles(tenantId); - Tenant tenant = new Tenant(); - tenant.setTitle("Test tenant"); - tenant = tenantService.saveTenant(tenant); - - TenantId tenantId = tenant.getId(); TenantId systemTenantId = TenantId.fromUUID(ModelConstants.NULL_UUID); List createdWidgetsBundles = new ArrayList<>(); @@ -432,8 +400,6 @@ public abstract class BaseWidgetsBundleServiceTest extends AbstractServiceTest { Collections.sort(loadedWidgetsBundles, idComparator); Assert.assertEquals(systemWidgetsBundles, loadedWidgetsBundles); - - tenantService.deleteTenant(tenantId); } } diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/event/BaseEventServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/event/BaseEventServiceTest.java index 4b0cf9a1b1..1b0825aff3 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/event/BaseEventServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/event/BaseEventServiceTest.java @@ -19,6 +19,7 @@ import com.datastax.oss.driver.api.core.uuid.Uuids; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.server.common.data.EventInfo; import org.thingsboard.server.common.data.event.Event; import org.thingsboard.server.common.data.event.EventType; @@ -31,6 +32,7 @@ import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.SortOrder; import org.thingsboard.server.common.data.page.TimePageLink; +import org.thingsboard.server.dao.event.EventService; import org.thingsboard.server.dao.service.AbstractServiceTest; import java.text.ParseException; @@ -39,6 +41,10 @@ import java.util.List; import static org.apache.commons.lang3.time.DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT; public abstract class BaseEventServiceTest extends AbstractServiceTest { + + @Autowired + EventService eventService; + long timeBeforeStartTime; long startTime; long eventTime; diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/timeseries/BaseTimeseriesServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/timeseries/BaseTimeseriesServiceTest.java index 924f515143..962dc7e246 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/timeseries/BaseTimeseriesServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/timeseries/BaseTimeseriesServiceTest.java @@ -21,6 +21,7 @@ import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.thingsboard.server.common.data.EntityView; import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.id.DeviceId; @@ -39,7 +40,9 @@ import org.thingsboard.server.common.data.kv.ReadTsKvQueryResult; import org.thingsboard.server.common.data.kv.StringDataEntry; import org.thingsboard.server.common.data.kv.TsKvEntry; import org.thingsboard.server.common.data.objects.TelemetryEntityView; +import org.thingsboard.server.dao.entityview.EntityViewService; import org.thingsboard.server.dao.service.AbstractServiceTest; +import org.thingsboard.server.dao.timeseries.TimeseriesService; import java.util.ArrayList; import java.util.Arrays; @@ -59,6 +62,13 @@ import static org.junit.Assert.assertNotNull; @Slf4j public abstract class BaseTimeseriesServiceTest extends AbstractServiceTest { + + @Autowired + TimeseriesService tsService; + + @Autowired + EntityViewService entityViewService; + static final int MAX_TIMEOUT = 30; private static final String STRING_KEY = "stringKey";