Browse Source

Merge pull request #8174 from zzzeebra/feature/junit5-migration-exceptions

[3.5] junit5 migration - exception annotation refactoring
pull/8203/head
Andrew Shvayka 4 years ago
committed by GitHub
parent
commit
e058786956
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      application/src/test/java/org/thingsboard/server/service/resource/sql/BaseTbResourceServiceTest.java
  2. 7
      application/src/test/java/org/thingsboard/server/transport/mqtt/mqttv3/credentials/BasicMqttCredentialsTest.java
  3. 7
      common/data/src/test/java/org/thingsboard/server/common/data/UUIDConverterTest.java
  4. 24
      common/transport/transport-api/src/test/java/JsonConverterTest.java
  5. 13
      dao/src/test/java/org/thingsboard/server/dao/service/BaseAdminSettingsServiceTest.java
  6. 19
      dao/src/test/java/org/thingsboard/server/dao/service/BaseAssetProfileServiceTest.java
  7. 41
      dao/src/test/java/org/thingsboard/server/dao/service/BaseAssetServiceTest.java
  8. 25
      dao/src/test/java/org/thingsboard/server/dao/service/BaseCustomerServiceTest.java
  9. 63
      dao/src/test/java/org/thingsboard/server/dao/service/BaseDashboardServiceTest.java
  10. 37
      dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceCredentialsServiceTest.java
  11. 31
      dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceProfileServiceTest.java
  12. 47
      dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceServiceTest.java
  13. 41
      dao/src/test/java/org/thingsboard/server/dao/service/BaseEdgeServiceTest.java
  14. 7
      dao/src/test/java/org/thingsboard/server/dao/service/BaseOAuth2ConfigTemplateServiceTest.java
  15. 13
      dao/src/test/java/org/thingsboard/server/dao/service/BaseOAuth2ServiceTest.java
  16. 103
      dao/src/test/java/org/thingsboard/server/dao/service/BaseQueueServiceTest.java
  17. 19
      dao/src/test/java/org/thingsboard/server/dao/service/BaseRelationServiceTest.java
  18. 29
      dao/src/test/java/org/thingsboard/server/dao/service/BaseRuleChainServiceTest.java
  19. 27
      dao/src/test/java/org/thingsboard/server/dao/service/BaseTenantProfileServiceTest.java
  20. 32
      dao/src/test/java/org/thingsboard/server/dao/service/BaseTenantServiceTest.java
  21. 25
      dao/src/test/java/org/thingsboard/server/dao/service/BaseUserServiceTest.java
  22. 49
      dao/src/test/java/org/thingsboard/server/dao/service/BaseWidgetTypeServiceTest.java
  23. 25
      dao/src/test/java/org/thingsboard/server/dao/service/BaseWidgetsBundleServiceTest.java
  24. 41
      dao/src/test/java/org/thingsboard/server/dao/service/DataValidatorTest.java
  25. 2
      dao/src/test/java/org/thingsboard/server/dao/service/NoXssValidatorTest.java
  26. 13
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/TbGetTelemetryNodeTest.java

19
application/src/test/java/org/thingsboard/server/service/resource/sql/BaseTbResourceServiceTest.java

@ -21,6 +21,7 @@ import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.thingsboard.server.common.data.EntityInfo; import org.thingsboard.server.common.data.EntityInfo;
@ -237,7 +238,7 @@ public class BaseTbResourceServiceTest extends AbstractControllerTest {
resourceService.delete(savedResource, null); resourceService.delete(savedResource, null);
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveTbResourceWithExistsFileName() throws Exception { public void testSaveTbResourceWithExistsFileName() throws Exception {
TbResource resource = new TbResource(); TbResource resource = new TbResource();
resource.setTenantId(tenantId); resource.setTenantId(tenantId);
@ -256,23 +257,27 @@ public class BaseTbResourceServiceTest extends AbstractControllerTest {
resource.setData("Test Data"); resource.setData("Test Data");
try { try {
resourceService.save(resource2); Assertions.assertThrows(DataValidationException.class, () -> {
resourceService.save(resource2);
});
} finally { } finally {
resourceService.delete(savedResource, null); resourceService.delete(savedResource, null);
} }
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveTbResourceWithEmptyTitle() throws Exception { public void testSaveTbResourceWithEmptyTitle() throws Exception {
TbResource resource = new TbResource(); TbResource resource = new TbResource();
resource.setTenantId(tenantId); resource.setTenantId(tenantId);
resource.setResourceType(ResourceType.JKS); resource.setResourceType(ResourceType.JKS);
resource.setFileName(DEFAULT_FILE_NAME); resource.setFileName(DEFAULT_FILE_NAME);
resource.setData("Test Data"); resource.setData("Test Data");
resourceService.save(resource); Assertions.assertThrows(DataValidationException.class, () -> {
resourceService.save(resource);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveTbResourceWithInvalidTenant() throws Exception { public void testSaveTbResourceWithInvalidTenant() throws Exception {
TbResource resource = new TbResource(); TbResource resource = new TbResource();
resource.setTenantId(TenantId.fromUUID(Uuids.timeBased())); resource.setTenantId(TenantId.fromUUID(Uuids.timeBased()));
@ -280,7 +285,9 @@ public class BaseTbResourceServiceTest extends AbstractControllerTest {
resource.setTitle("My resource"); resource.setTitle("My resource");
resource.setFileName(DEFAULT_FILE_NAME); resource.setFileName(DEFAULT_FILE_NAME);
resource.setData("Test Data"); resource.setData("Test Data");
resourceService.save(resource); Assertions.assertThrows(DataValidationException.class, () -> {
resourceService.save(resource);
});
} }
@Test @Test

7
application/src/test/java/org/thingsboard/server/transport/mqtt/mqttv3/credentials/BasicMqttCredentialsTest.java

@ -20,6 +20,7 @@ import org.eclipse.paho.client.mqttv3.MqttException;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.StringUtils;
@ -115,7 +116,7 @@ public class BasicMqttCredentialsTest extends AbstractMqttIntegrationTest {
testTelemetryIsDelivered(accessToken2Device, mqttTestClient5); testTelemetryIsDelivered(accessToken2Device, mqttTestClient5);
} }
@Test(expected = MqttException.class) @Test
public void testCorrectClientIdAndUserNameButWrongPassword() throws Exception { public void testCorrectClientIdAndUserNameButWrongPassword() throws Exception {
// Not correct. Correct clientId and username, but wrong password // Not correct. Correct clientId and username, but wrong password
MqttTestClient mqttTestClient = new MqttTestClient(CLIENT_ID); MqttTestClient mqttTestClient = new MqttTestClient(CLIENT_ID);
@ -125,7 +126,9 @@ public class BasicMqttCredentialsTest extends AbstractMqttIntegrationTest {
} catch (MqttException e) { } catch (MqttException e) {
Assert.assertEquals(4, e.getReasonCode()); // 4 - Reason code for bad username or password in MQTT v3 Assert.assertEquals(4, e.getReasonCode()); // 4 - Reason code for bad username or password in MQTT v3
} }
testTelemetryIsNotDelivered(clientIdAndUserNameAndPasswordDevice3, mqttTestClient); Assertions.assertThrows(MqttException.class, () -> {
testTelemetryIsNotDelivered(clientIdAndUserNameAndPasswordDevice3, mqttTestClient);
});
} }
private void testTelemetryIsDelivered(Device device, MqttTestClient client) throws Exception { private void testTelemetryIsDelivered(Device device, MqttTestClient client) throws Exception {

7
common/data/src/test/java/org/thingsboard/server/common/data/UUIDConverterTest.java

@ -18,6 +18,7 @@ package org.thingsboard.server.common.data;
import com.datastax.oss.driver.api.core.uuid.Uuids; import com.datastax.oss.driver.api.core.uuid.Uuids;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
@ -58,9 +59,11 @@ public class UUIDConverterTest {
Assert.assertEquals(UUID.fromString("58e0a7d7-eebc-11d8-9669-0800200c9a66"), result); Assert.assertEquals(UUID.fromString("58e0a7d7-eebc-11d8-9669-0800200c9a66"), result);
} }
@Test(expected = IllegalArgumentException.class) @Test
public void nonV1UuidToStringTest() { public void nonV1UuidToStringTest() {
UUIDConverter.fromTimeUUID(UUID.fromString("58e0a7d7-eebc-01d8-9669-0800200c9a66")); Assertions.assertThrows(IllegalArgumentException.class, () -> {
UUIDConverter.fromTimeUUID(UUID.fromString("58e0a7d7-eebc-01d8-9669-0800200c9a66"));
});
} }
@Test @Test

24
common/transport/transport-api/src/test/java/JsonConverterTest.java

@ -16,20 +16,18 @@
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException; import com.google.gson.JsonSyntaxException;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.jupiter.api.Assertions;
import org.junit.Test; import org.junit.jupiter.api.BeforeEach;
import org.junit.runner.RunWith; import org.junit.jupiter.api.Test;
import org.mockito.junit.MockitoJUnitRunner;
import org.thingsboard.server.common.transport.adaptor.JsonConverter; import org.thingsboard.server.common.transport.adaptor.JsonConverter;
import java.util.ArrayList; import java.util.ArrayList;
@RunWith(MockitoJUnitRunner.class)
public class JsonConverterTest { public class JsonConverterTest {
private static final JsonParser JSON_PARSER = new JsonParser(); private final JsonParser JSON_PARSER = new JsonParser();
@Before @BeforeEach
public void before() { public void before() {
JsonConverter.setTypeCastEnabled(true); JsonConverter.setTypeCastEnabled(true);
} }
@ -88,15 +86,19 @@ public class JsonConverterTest {
Assert.assertEquals("10000000000000000000", result.get(0L).get(0).getStrValue().get()); Assert.assertEquals("10000000000000000000", result.get(0L).get(0).getStrValue().get());
} }
@Test(expected = JsonSyntaxException.class) @Test
public void testParseBigDecimalOutOfLongRangeWithoutParsing() { public void testParseBigDecimalOutOfLongRangeWithoutParsing() {
JsonConverter.setTypeCastEnabled(false); JsonConverter.setTypeCastEnabled(false);
JsonConverter.convertToTelemetry(JSON_PARSER.parse("{\"meterReadingDelta\": 89701010051400054084}"), 0L); Assertions.assertThrows(JsonSyntaxException.class, () -> {
JsonConverter.convertToTelemetry(JSON_PARSER.parse("{\"meterReadingDelta\": 89701010051400054084}"), 0L);
});
} }
@Test(expected = JsonSyntaxException.class) @Test
public void testParseBigDecimalOutOfLongRangeWithoutParsing2() { public void testParseBigDecimalOutOfLongRangeWithoutParsing2() {
JsonConverter.setTypeCastEnabled(false); JsonConverter.setTypeCastEnabled(false);
JsonConverter.convertToTelemetry(JSON_PARSER.parse("{\"meterReadingDelta\": 9.9701010061400066E19}"), 0L); Assertions.assertThrows(JsonSyntaxException.class, () -> {
JsonConverter.convertToTelemetry(JSON_PARSER.parse("{\"meterReadingDelta\": 9.9701010061400066E19}"), 0L);
});
} }
} }

13
dao/src/test/java/org/thingsboard/server/dao/service/BaseAdminSettingsServiceTest.java

@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.thingsboard.server.common.data.AdminSettings; import org.thingsboard.server.common.data.AdminSettings;
import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.exception.DataValidationException;
@ -54,17 +55,21 @@ public abstract class BaseAdminSettingsServiceTest extends AbstractServiceTest {
Assert.assertEquals(adminSettings.getJsonValue(), savedAdminSettings.getJsonValue()); Assert.assertEquals(adminSettings.getJsonValue(), savedAdminSettings.getJsonValue());
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveAdminSettingsWithEmptyKey() { public void testSaveAdminSettingsWithEmptyKey() {
AdminSettings adminSettings = adminSettingsService.findAdminSettingsByKey(SYSTEM_TENANT_ID, "mail"); AdminSettings adminSettings = adminSettingsService.findAdminSettingsByKey(SYSTEM_TENANT_ID, "mail");
adminSettings.setKey(null); adminSettings.setKey(null);
adminSettingsService.saveAdminSettings(SYSTEM_TENANT_ID, adminSettings); Assertions.assertThrows(DataValidationException.class, () -> {
adminSettingsService.saveAdminSettings(SYSTEM_TENANT_ID, adminSettings);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testChangeAdminSettingsKey() { public void testChangeAdminSettingsKey() {
AdminSettings adminSettings = adminSettingsService.findAdminSettingsByKey(SYSTEM_TENANT_ID, "mail"); AdminSettings adminSettings = adminSettingsService.findAdminSettingsByKey(SYSTEM_TENANT_ID, "mail");
adminSettings.setKey("newKey"); adminSettings.setKey("newKey");
adminSettingsService.saveAdminSettings(SYSTEM_TENANT_ID, adminSettings); Assertions.assertThrows(DataValidationException.class, () -> {
adminSettingsService.saveAdminSettings(SYSTEM_TENANT_ID, adminSettings);
});
} }
} }

19
dao/src/test/java/org/thingsboard/server/dao/service/BaseAssetProfileServiceTest.java

@ -23,6 +23,7 @@ import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.thingsboard.common.util.ThingsBoardThreadFactory; import org.thingsboard.common.util.ThingsBoardThreadFactory;
import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.common.data.asset.Asset;
@ -150,22 +151,26 @@ public abstract class BaseAssetProfileServiceTest extends AbstractServiceTest {
Assert.assertEquals(savedAssetProfile2.getId(), defaultAssetProfile.getId()); Assert.assertEquals(savedAssetProfile2.getId(), defaultAssetProfile.getId());
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveAssetProfileWithEmptyName() { public void testSaveAssetProfileWithEmptyName() {
AssetProfile assetProfile = new AssetProfile(); AssetProfile assetProfile = new AssetProfile();
assetProfile.setTenantId(tenantId); assetProfile.setTenantId(tenantId);
assetProfileService.saveAssetProfile(assetProfile); Assertions.assertThrows(DataValidationException.class, () -> {
assetProfileService.saveAssetProfile(assetProfile);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveAssetProfileWithSameName() { public void testSaveAssetProfileWithSameName() {
AssetProfile assetProfile = this.createAssetProfile(tenantId, "Asset Profile"); AssetProfile assetProfile = this.createAssetProfile(tenantId, "Asset Profile");
assetProfileService.saveAssetProfile(assetProfile); assetProfileService.saveAssetProfile(assetProfile);
AssetProfile assetProfile2 = this.createAssetProfile(tenantId, "Asset Profile"); AssetProfile assetProfile2 = this.createAssetProfile(tenantId, "Asset Profile");
assetProfileService.saveAssetProfile(assetProfile2); Assertions.assertThrows(DataValidationException.class, () -> {
assetProfileService.saveAssetProfile(assetProfile2);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testDeleteAssetProfileWithExistingAsset() { public void testDeleteAssetProfileWithExistingAsset() {
AssetProfile assetProfile = this.createAssetProfile(tenantId, "Asset Profile"); AssetProfile assetProfile = this.createAssetProfile(tenantId, "Asset Profile");
AssetProfile savedAssetProfile = assetProfileService.saveAssetProfile(assetProfile); AssetProfile savedAssetProfile = assetProfileService.saveAssetProfile(assetProfile);
@ -174,7 +179,9 @@ public abstract class BaseAssetProfileServiceTest extends AbstractServiceTest {
asset.setName("Test asset"); asset.setName("Test asset");
asset.setAssetProfileId(savedAssetProfile.getId()); asset.setAssetProfileId(savedAssetProfile.getId());
assetService.saveAsset(asset); assetService.saveAsset(asset);
assetProfileService.deleteAssetProfile(tenantId, savedAssetProfile.getId()); Assertions.assertThrows(DataValidationException.class, () -> {
assetProfileService.deleteAssetProfile(tenantId, savedAssetProfile.getId());
});
} }
@Test @Test

41
dao/src/test/java/org/thingsboard/server/dao/service/BaseAssetServiceTest.java

@ -20,6 +20,7 @@ import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.EntitySubtype; import org.thingsboard.server.common.data.EntitySubtype;
import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.StringUtils;
@ -83,63 +84,73 @@ public abstract class BaseAssetServiceTest extends AbstractServiceTest {
assetService.deleteAsset(tenantId, savedAsset.getId()); assetService.deleteAsset(tenantId, savedAsset.getId());
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveAssetWithEmptyName() { public void testSaveAssetWithEmptyName() {
Asset asset = new Asset(); Asset asset = new Asset();
asset.setTenantId(tenantId); asset.setTenantId(tenantId);
asset.setType("default"); asset.setType("default");
assetService.saveAsset(asset); Assertions.assertThrows(DataValidationException.class, () -> {
assetService.saveAsset(asset);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveAssetWithEmptyTenant() { public void testSaveAssetWithEmptyTenant() {
Asset asset = new Asset(); Asset asset = new Asset();
asset.setName("My asset"); asset.setName("My asset");
asset.setType("default"); asset.setType("default");
assetService.saveAsset(asset); Assertions.assertThrows(DataValidationException.class, () -> {
assetService.saveAsset(asset);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveAssetWithInvalidTenant() { public void testSaveAssetWithInvalidTenant() {
Asset asset = new Asset(); Asset asset = new Asset();
asset.setName("My asset"); asset.setName("My asset");
asset.setType("default"); asset.setType("default");
asset.setTenantId(TenantId.fromUUID(Uuids.timeBased())); asset.setTenantId(TenantId.fromUUID(Uuids.timeBased()));
assetService.saveAsset(asset); Assertions.assertThrows(DataValidationException.class, () -> {
assetService.saveAsset(asset);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testAssignAssetToNonExistentCustomer() { public void testAssignAssetToNonExistentCustomer() {
Asset asset = new Asset(); Asset asset = new Asset();
asset.setName("My asset"); asset.setName("My asset");
asset.setType("default"); asset.setType("default");
asset.setTenantId(tenantId); asset.setTenantId(tenantId);
asset = assetService.saveAsset(asset); Asset savedAsset = assetService.saveAsset(asset);
try { try {
assetService.assignAssetToCustomer(tenantId, asset.getId(), new CustomerId(Uuids.timeBased())); Assertions.assertThrows(DataValidationException.class, () -> {
assetService.assignAssetToCustomer(tenantId, savedAsset.getId(), new CustomerId(Uuids.timeBased()));
});
} finally { } finally {
assetService.deleteAsset(tenantId, asset.getId()); assetService.deleteAsset(tenantId, savedAsset.getId());
} }
} }
@Test(expected = DataValidationException.class) @Test
public void testAssignAssetToCustomerFromDifferentTenant() { public void testAssignAssetToCustomerFromDifferentTenant() {
Asset asset = new Asset(); Asset asset = new Asset();
asset.setName("My asset"); asset.setName("My asset");
asset.setType("default"); asset.setType("default");
asset.setTenantId(tenantId); asset.setTenantId(tenantId);
asset = assetService.saveAsset(asset); Asset savedAsset = assetService.saveAsset(asset);
Tenant tenant = new Tenant(); Tenant tenant = new Tenant();
tenant.setTitle("Test different tenant"); tenant.setTitle("Test different tenant");
tenant = tenantService.saveTenant(tenant); tenant = tenantService.saveTenant(tenant);
Customer customer = new Customer(); Customer customer = new Customer();
customer.setTenantId(tenant.getId()); customer.setTenantId(tenant.getId());
customer.setTitle("Test different customer"); customer.setTitle("Test different customer");
customer = customerService.saveCustomer(customer); Customer savedCustomer = customerService.saveCustomer(customer);
try { try {
assetService.assignAssetToCustomer(tenantId, asset.getId(), customer.getId()); Assertions.assertThrows(DataValidationException.class, () -> {
assetService.assignAssetToCustomer(tenantId, savedAsset.getId(), savedCustomer.getId());
});
} finally { } finally {
assetService.deleteAsset(tenantId, asset.getId()); assetService.deleteAsset(tenantId, savedAsset.getId());
tenantService.deleteTenant(tenant.getId()); tenantService.deleteTenant(tenant.getId());
} }
} }

25
dao/src/test/java/org/thingsboard/server/dao/service/BaseCustomerServiceTest.java

@ -24,6 +24,7 @@ import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.thingsboard.common.util.ThingsBoardExecutors; import org.thingsboard.common.util.ThingsBoardExecutors;
import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.StringUtils;
@ -99,35 +100,43 @@ public abstract class BaseCustomerServiceTest extends AbstractServiceTest {
customerService.deleteCustomer(tenantId, savedCustomer.getId()); customerService.deleteCustomer(tenantId, savedCustomer.getId());
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveCustomerWithEmptyTitle() { public void testSaveCustomerWithEmptyTitle() {
Customer customer = new Customer(); Customer customer = new Customer();
customer.setTenantId(tenantId); customer.setTenantId(tenantId);
customerService.saveCustomer(customer); Assertions.assertThrows(DataValidationException.class, () -> {
customerService.saveCustomer(customer);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveCustomerWithEmptyTenant() { public void testSaveCustomerWithEmptyTenant() {
Customer customer = new Customer(); Customer customer = new Customer();
customer.setTitle("My customer"); customer.setTitle("My customer");
customerService.saveCustomer(customer); Assertions.assertThrows(DataValidationException.class, () -> {
customerService.saveCustomer(customer);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveCustomerWithInvalidTenant() { public void testSaveCustomerWithInvalidTenant() {
Customer customer = new Customer(); Customer customer = new Customer();
customer.setTitle("My customer"); customer.setTitle("My customer");
customer.setTenantId(TenantId.fromUUID(Uuids.timeBased())); customer.setTenantId(TenantId.fromUUID(Uuids.timeBased()));
customerService.saveCustomer(customer); Assertions.assertThrows(DataValidationException.class, () -> {
customerService.saveCustomer(customer);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveCustomerWithInvalidEmail() { public void testSaveCustomerWithInvalidEmail() {
Customer customer = new Customer(); Customer customer = new Customer();
customer.setTenantId(tenantId); customer.setTenantId(tenantId);
customer.setTitle("My customer"); customer.setTitle("My customer");
customer.setEmail("invalid@mail"); customer.setEmail("invalid@mail");
customerService.saveCustomer(customer); Assertions.assertThrows(DataValidationException.class, () -> {
customerService.saveCustomer(customer);
});
} }
@Test @Test

63
dao/src/test/java/org/thingsboard/server/dao/service/BaseDashboardServiceTest.java

@ -20,6 +20,7 @@ import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.Dashboard; import org.thingsboard.server.common.data.Dashboard;
import org.thingsboard.server.common.data.DashboardInfo; import org.thingsboard.server.common.data.DashboardInfo;
@ -82,58 +83,68 @@ public abstract class BaseDashboardServiceTest extends AbstractServiceTest {
dashboardService.deleteDashboard(tenantId, savedDashboard.getId()); dashboardService.deleteDashboard(tenantId, savedDashboard.getId());
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveDashboardWithEmptyTitle() { public void testSaveDashboardWithEmptyTitle() {
Dashboard dashboard = new Dashboard(); Dashboard dashboard = new Dashboard();
dashboard.setTenantId(tenantId); dashboard.setTenantId(tenantId);
dashboardService.saveDashboard(dashboard); Assertions.assertThrows(DataValidationException.class, () -> {
dashboardService.saveDashboard(dashboard);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveDashboardWithEmptyTenant() { public void testSaveDashboardWithEmptyTenant() {
Dashboard dashboard = new Dashboard(); Dashboard dashboard = new Dashboard();
dashboard.setTitle("My dashboard"); dashboard.setTitle("My dashboard");
dashboardService.saveDashboard(dashboard); Assertions.assertThrows(DataValidationException.class, () -> {
dashboardService.saveDashboard(dashboard);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveDashboardWithInvalidTenant() { public void testSaveDashboardWithInvalidTenant() {
Dashboard dashboard = new Dashboard(); Dashboard dashboard = new Dashboard();
dashboard.setTitle("My dashboard"); dashboard.setTitle("My dashboard");
dashboard.setTenantId(TenantId.fromUUID(Uuids.timeBased())); dashboard.setTenantId(TenantId.fromUUID(Uuids.timeBased()));
dashboardService.saveDashboard(dashboard); Assertions.assertThrows(DataValidationException.class, () -> {
dashboardService.saveDashboard(dashboard);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testAssignDashboardToNonExistentCustomer() { public void testAssignDashboardToNonExistentCustomer() {
Dashboard dashboard = new Dashboard(); Dashboard dashboard = new Dashboard();
dashboard.setTitle("My dashboard"); dashboard.setTitle("My dashboard");
dashboard.setTenantId(tenantId); dashboard.setTenantId(tenantId);
dashboard = dashboardService.saveDashboard(dashboard); Dashboard savedDashboard = dashboardService.saveDashboard(dashboard);
try { try {
dashboardService.assignDashboardToCustomer(tenantId, dashboard.getId(), new CustomerId(Uuids.timeBased())); Assertions.assertThrows(DataValidationException.class, () -> {
dashboardService.assignDashboardToCustomer(tenantId, savedDashboard.getId(), new CustomerId(Uuids.timeBased()));
});
} finally { } finally {
dashboardService.deleteDashboard(tenantId, dashboard.getId()); dashboardService.deleteDashboard(tenantId, savedDashboard.getId());
} }
} }
@Test(expected = DataValidationException.class) @Test
public void testAssignDashboardToCustomerFromDifferentTenant() { public void testAssignDashboardToCustomerFromDifferentTenant() {
Dashboard dashboard = new Dashboard(); Dashboard dashboard = new Dashboard();
dashboard.setTitle("My dashboard"); dashboard.setTitle("My dashboard");
dashboard.setTenantId(tenantId); dashboard.setTenantId(tenantId);
dashboard = dashboardService.saveDashboard(dashboard); Dashboard savedDashboard = dashboardService.saveDashboard(dashboard);
Tenant tenant = new Tenant(); Tenant tenant = new Tenant();
tenant.setTitle("Test different tenant [dashboard]"); tenant.setTitle("Test different tenant [dashboard]");
tenant = tenantService.saveTenant(tenant); tenant = tenantService.saveTenant(tenant);
Customer customer = new Customer(); Customer customer = new Customer();
customer.setTenantId(tenant.getId()); customer.setTenantId(tenant.getId());
customer.setTitle("Test different customer"); customer.setTitle("Test different customer");
customer = customerService.saveCustomer(customer); Customer savedCustomer = customerService.saveCustomer(customer);
try { try {
dashboardService.assignDashboardToCustomer(tenantId, dashboard.getId(), customer.getId()); Assertions.assertThrows(DataValidationException.class, () -> {
dashboardService.assignDashboardToCustomer(tenantId, savedDashboard.getId(), savedCustomer.getId());
});
} finally { } finally {
dashboardService.deleteDashboard(tenantId, dashboard.getId()); dashboardService.deleteDashboard(tenantId, savedDashboard.getId());
tenantService.deleteTenant(tenant.getId()); tenantService.deleteTenant(tenant.getId());
} }
} }
@ -389,25 +400,27 @@ public abstract class BaseDashboardServiceTest extends AbstractServiceTest {
tenantService.deleteTenant(tenantId); tenantService.deleteTenant(tenantId);
} }
@Test(expected = DataValidationException.class) @Test
public void testAssignDashboardToNonExistentEdge() { public void testAssignDashboardToNonExistentEdge() {
Dashboard dashboard = new Dashboard(); Dashboard dashboard = new Dashboard();
dashboard.setTitle("My dashboard"); dashboard.setTitle("My dashboard");
dashboard.setTenantId(tenantId); dashboard.setTenantId(tenantId);
dashboard = dashboardService.saveDashboard(dashboard); Dashboard savedDashboard = dashboardService.saveDashboard(dashboard);
try { try {
dashboardService.assignDashboardToEdge(tenantId, dashboard.getId(), new EdgeId(Uuids.timeBased())); Assertions.assertThrows(DataValidationException.class, () -> {
dashboardService.assignDashboardToEdge(tenantId, savedDashboard.getId(), new EdgeId(Uuids.timeBased()));
});
} finally { } finally {
dashboardService.deleteDashboard(tenantId, dashboard.getId()); dashboardService.deleteDashboard(tenantId, savedDashboard.getId());
} }
} }
@Test(expected = DataValidationException.class) @Test
public void testAssignDashboardToEdgeFromDifferentTenant() { public void testAssignDashboardToEdgeFromDifferentTenant() {
Dashboard dashboard = new Dashboard(); Dashboard dashboard = new Dashboard();
dashboard.setTitle("My dashboard"); dashboard.setTitle("My dashboard");
dashboard.setTenantId(tenantId); dashboard.setTenantId(tenantId);
dashboard = dashboardService.saveDashboard(dashboard); Dashboard savedDashboard = dashboardService.saveDashboard(dashboard);
Tenant tenant = new Tenant(); Tenant tenant = new Tenant();
tenant.setTitle("Test different tenant [edge]"); tenant.setTitle("Test different tenant [edge]");
tenant = tenantService.saveTenant(tenant); tenant = tenantService.saveTenant(tenant);
@ -418,11 +431,13 @@ public abstract class BaseDashboardServiceTest extends AbstractServiceTest {
edge.setType("default"); edge.setType("default");
edge.setSecret(StringUtils.randomAlphanumeric(20)); edge.setSecret(StringUtils.randomAlphanumeric(20));
edge.setRoutingKey(StringUtils.randomAlphanumeric(20)); edge.setRoutingKey(StringUtils.randomAlphanumeric(20));
edge = edgeService.saveEdge(edge); Edge savedEdge = edgeService.saveEdge(edge);
try { try {
dashboardService.assignDashboardToEdge(tenantId, dashboard.getId(), edge.getId()); Assertions.assertThrows(DataValidationException.class, () -> {
dashboardService.assignDashboardToEdge(tenantId, savedDashboard.getId(), savedEdge.getId());
});
} finally { } finally {
dashboardService.deleteDashboard(tenantId, dashboard.getId()); dashboardService.deleteDashboard(tenantId, savedDashboard.getId());
tenantService.deleteTenant(tenant.getId()); tenantService.deleteTenant(tenant.getId());
} }
} }

37
dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceCredentialsServiceTest.java

@ -20,6 +20,7 @@ import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.id.DeviceCredentialsId; import org.thingsboard.server.common.data.id.DeviceCredentialsId;
@ -47,13 +48,15 @@ public abstract class BaseDeviceCredentialsServiceTest extends AbstractServiceTe
tenantService.deleteTenant(tenantId); tenantService.deleteTenant(tenantId);
} }
@Test(expected = DataValidationException.class) @Test
public void testCreateDeviceCredentials() { public void testCreateDeviceCredentials() {
DeviceCredentials deviceCredentials = new DeviceCredentials(); DeviceCredentials deviceCredentials = new DeviceCredentials();
deviceCredentialsService.updateDeviceCredentials(tenantId, deviceCredentials); Assertions.assertThrows(DataValidationException.class, () -> {
deviceCredentialsService.updateDeviceCredentials(tenantId, deviceCredentials);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveDeviceCredentialsWithEmptyDevice() { public void testSaveDeviceCredentialsWithEmptyDevice() {
Device device = new Device(); Device device = new Device();
device.setName("My device"); device.setName("My device");
@ -63,13 +66,15 @@ public abstract class BaseDeviceCredentialsServiceTest extends AbstractServiceTe
DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(tenantId, device.getId()); DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(tenantId, device.getId());
deviceCredentials.setDeviceId(null); deviceCredentials.setDeviceId(null);
try { try {
deviceCredentialsService.updateDeviceCredentials(tenantId, deviceCredentials); Assertions.assertThrows(DataValidationException.class, () -> {
deviceCredentialsService.updateDeviceCredentials(tenantId, deviceCredentials);
});
} finally { } finally {
deviceService.deleteDevice(tenantId, device.getId()); deviceService.deleteDevice(tenantId, device.getId());
} }
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveDeviceCredentialsWithEmptyCredentialsType() { public void testSaveDeviceCredentialsWithEmptyCredentialsType() {
Device device = new Device(); Device device = new Device();
device.setName("My device"); device.setName("My device");
@ -79,13 +84,15 @@ public abstract class BaseDeviceCredentialsServiceTest extends AbstractServiceTe
DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(tenantId, device.getId()); DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(tenantId, device.getId());
deviceCredentials.setCredentialsType(null); deviceCredentials.setCredentialsType(null);
try { try {
deviceCredentialsService.updateDeviceCredentials(tenantId, deviceCredentials); Assertions.assertThrows(DataValidationException.class, () -> {
deviceCredentialsService.updateDeviceCredentials(tenantId, deviceCredentials);
});
} finally { } finally {
deviceService.deleteDevice(tenantId, device.getId()); deviceService.deleteDevice(tenantId, device.getId());
} }
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveDeviceCredentialsWithEmptyCredentialsId() { public void testSaveDeviceCredentialsWithEmptyCredentialsId() {
Device device = new Device(); Device device = new Device();
device.setName("My device"); device.setName("My device");
@ -95,13 +102,15 @@ public abstract class BaseDeviceCredentialsServiceTest extends AbstractServiceTe
DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(tenantId, device.getId()); DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(tenantId, device.getId());
deviceCredentials.setCredentialsId(null); deviceCredentials.setCredentialsId(null);
try { try {
deviceCredentialsService.updateDeviceCredentials(tenantId, deviceCredentials); Assertions.assertThrows(DataValidationException.class, () -> {
deviceCredentialsService.updateDeviceCredentials(tenantId, deviceCredentials);
});
} finally { } finally {
deviceService.deleteDevice(tenantId, device.getId()); deviceService.deleteDevice(tenantId, device.getId());
} }
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveNonExistentDeviceCredentials() { public void testSaveNonExistentDeviceCredentials() {
Device device = new Device(); Device device = new Device();
device.setName("My device"); device.setName("My device");
@ -115,13 +124,15 @@ public abstract class BaseDeviceCredentialsServiceTest extends AbstractServiceTe
newDeviceCredentials.setCredentialsType(deviceCredentials.getCredentialsType()); newDeviceCredentials.setCredentialsType(deviceCredentials.getCredentialsType());
newDeviceCredentials.setCredentialsId(deviceCredentials.getCredentialsId()); newDeviceCredentials.setCredentialsId(deviceCredentials.getCredentialsId());
try { try {
deviceCredentialsService.updateDeviceCredentials(tenantId, newDeviceCredentials); Assertions.assertThrows(DataValidationException.class, () -> {
deviceCredentialsService.updateDeviceCredentials(tenantId, newDeviceCredentials);
});
} finally { } finally {
deviceService.deleteDevice(tenantId, device.getId()); deviceService.deleteDevice(tenantId, device.getId());
} }
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveDeviceCredentialsWithNonExistentDevice() { public void testSaveDeviceCredentialsWithNonExistentDevice() {
Device device = new Device(); Device device = new Device();
device.setName("My device"); device.setName("My device");
@ -131,7 +142,9 @@ public abstract class BaseDeviceCredentialsServiceTest extends AbstractServiceTe
DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(tenantId, device.getId()); DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(tenantId, device.getId());
deviceCredentials.setDeviceId(new DeviceId(Uuids.timeBased())); deviceCredentials.setDeviceId(new DeviceId(Uuids.timeBased()));
try { try {
deviceCredentialsService.updateDeviceCredentials(tenantId, deviceCredentials); Assertions.assertThrows(DataValidationException.class, () -> {
deviceCredentialsService.updateDeviceCredentials(tenantId, deviceCredentials);
});
} finally { } finally {
deviceService.deleteDevice(tenantId, device.getId()); deviceService.deleteDevice(tenantId, device.getId());
} }

31
dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceProfileServiceTest.java

@ -24,6 +24,7 @@ import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.thingsboard.common.util.ThingsBoardThreadFactory; import org.thingsboard.common.util.ThingsBoardThreadFactory;
import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.DeviceProfile;
@ -196,23 +197,27 @@ public abstract class BaseDeviceProfileServiceTest extends AbstractServiceTest {
Assert.assertEquals(savedDeviceProfile2.getId(), defaultDeviceProfile.getId()); Assert.assertEquals(savedDeviceProfile2.getId(), defaultDeviceProfile.getId());
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveDeviceProfileWithEmptyName() { public void testSaveDeviceProfileWithEmptyName() {
DeviceProfile deviceProfile = new DeviceProfile(); DeviceProfile deviceProfile = new DeviceProfile();
deviceProfile.setTenantId(tenantId); deviceProfile.setTenantId(tenantId);
deviceProfileService.saveDeviceProfile(deviceProfile); Assertions.assertThrows(DataValidationException.class, () -> {
deviceProfileService.saveDeviceProfile(deviceProfile);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveDeviceProfileWithSameName() { public void testSaveDeviceProfileWithSameName() {
DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Device Profile"); DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Device Profile");
deviceProfileService.saveDeviceProfile(deviceProfile); deviceProfileService.saveDeviceProfile(deviceProfile);
DeviceProfile deviceProfile2 = this.createDeviceProfile(tenantId, "Device Profile"); DeviceProfile deviceProfile2 = this.createDeviceProfile(tenantId, "Device Profile");
deviceProfileService.saveDeviceProfile(deviceProfile2); Assertions.assertThrows(DataValidationException.class, () -> {
deviceProfileService.saveDeviceProfile(deviceProfile2);
});
} }
@Ignore @Ignore
@Test(expected = DataValidationException.class) @Test
public void testChangeDeviceProfileTypeWithExistingDevices() { public void testChangeDeviceProfileTypeWithExistingDevices() {
DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Device Profile"); DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Device Profile");
DeviceProfile savedDeviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile); DeviceProfile savedDeviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile);
@ -224,10 +229,12 @@ public abstract class BaseDeviceProfileServiceTest extends AbstractServiceTest {
deviceService.saveDevice(device); deviceService.saveDevice(device);
//TODO: once we have more profile types, we should test that we can not change profile type in runtime and uncomment the @Ignore. //TODO: once we have more profile types, we should test that we can not change profile type in runtime and uncomment the @Ignore.
// savedDeviceProfile.setType(DeviceProfileType.LWM2M); // savedDeviceProfile.setType(DeviceProfileType.LWM2M);
deviceProfileService.saveDeviceProfile(savedDeviceProfile); Assertions.assertThrows(DataValidationException.class, () -> {
deviceProfileService.saveDeviceProfile(savedDeviceProfile);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testChangeDeviceProfileTransportTypeWithExistingDevices() { public void testChangeDeviceProfileTransportTypeWithExistingDevices() {
DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Device Profile"); DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Device Profile");
DeviceProfile savedDeviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile); DeviceProfile savedDeviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile);
@ -238,10 +245,12 @@ public abstract class BaseDeviceProfileServiceTest extends AbstractServiceTest {
device.setDeviceProfileId(savedDeviceProfile.getId()); device.setDeviceProfileId(savedDeviceProfile.getId());
deviceService.saveDevice(device); deviceService.saveDevice(device);
savedDeviceProfile.setTransportType(DeviceTransportType.MQTT); savedDeviceProfile.setTransportType(DeviceTransportType.MQTT);
deviceProfileService.saveDeviceProfile(savedDeviceProfile); Assertions.assertThrows(DataValidationException.class, () -> {
deviceProfileService.saveDeviceProfile(savedDeviceProfile);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testDeleteDeviceProfileWithExistingDevice() { public void testDeleteDeviceProfileWithExistingDevice() {
DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Device Profile"); DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Device Profile");
DeviceProfile savedDeviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile); DeviceProfile savedDeviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile);
@ -251,7 +260,9 @@ public abstract class BaseDeviceProfileServiceTest extends AbstractServiceTest {
device.setType("default"); device.setType("default");
device.setDeviceProfileId(savedDeviceProfile.getId()); device.setDeviceProfileId(savedDeviceProfile.getId());
deviceService.saveDevice(device); deviceService.saveDevice(device);
deviceProfileService.deleteDeviceProfile(tenantId, savedDeviceProfile.getId()); Assertions.assertThrows(DataValidationException.class, () -> {
deviceProfileService.deleteDeviceProfile(tenantId, savedDeviceProfile.getId());
});
} }
@Test @Test

47
dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceServiceTest.java

@ -21,6 +21,7 @@ import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.Device;
@ -91,7 +92,7 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest {
deleteDevice(tenantId, device); deleteDevice(tenantId, device);
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveDevicesWithMaxDeviceOutOfLimit() { public void testSaveDevicesWithMaxDeviceOutOfLimit() {
TenantProfile defaultTenantProfile = tenantProfileService.findDefaultTenantProfile(tenantId); TenantProfile defaultTenantProfile = tenantProfileService.findDefaultTenantProfile(tenantId);
defaultTenantProfile.getProfileData().setConfiguration(DefaultTenantProfileConfiguration.builder().maxDevices(1).build()); defaultTenantProfile.getProfileData().setConfiguration(DefaultTenantProfileConfiguration.builder().maxDevices(1).build());
@ -102,7 +103,9 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest {
this.saveDevice(tenantId, "My first device"); this.saveDevice(tenantId, "My first device");
Assert.assertEquals(1, deviceService.countByTenantId(tenantId)); Assert.assertEquals(1, deviceService.countByTenantId(tenantId));
this.saveDevice(tenantId, "My second device that out of maxDeviceCount limit"); Assertions.assertThrows(DataValidationException.class, () -> {
this.saveDevice(tenantId, "My second device that out of maxDeviceCount limit");
});
} }
@Test @Test
@ -246,63 +249,73 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest {
deviceService.saveDevice(savedDevice); deviceService.saveDevice(savedDevice);
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveDeviceWithEmptyName() { public void testSaveDeviceWithEmptyName() {
Device device = new Device(); Device device = new Device();
device.setType("default"); device.setType("default");
device.setTenantId(tenantId); device.setTenantId(tenantId);
deviceService.saveDevice(device); Assertions.assertThrows(DataValidationException.class, () -> {
deviceService.saveDevice(device);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveDeviceWithEmptyTenant() { public void testSaveDeviceWithEmptyTenant() {
Device device = new Device(); Device device = new Device();
device.setName("My device"); device.setName("My device");
device.setType("default"); device.setType("default");
deviceService.saveDevice(device); Assertions.assertThrows(DataValidationException.class, () -> {
deviceService.saveDevice(device);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveDeviceWithInvalidTenant() { public void testSaveDeviceWithInvalidTenant() {
Device device = new Device(); Device device = new Device();
device.setName("My device"); device.setName("My device");
device.setType("default"); device.setType("default");
device.setTenantId(TenantId.fromUUID(Uuids.timeBased())); device.setTenantId(TenantId.fromUUID(Uuids.timeBased()));
deviceService.saveDevice(device); Assertions.assertThrows(DataValidationException.class, () -> {
deviceService.saveDevice(device);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testAssignDeviceToNonExistentCustomer() { public void testAssignDeviceToNonExistentCustomer() {
Device device = new Device(); Device device = new Device();
device.setName("My device"); device.setName("My device");
device.setType("default"); device.setType("default");
device.setTenantId(tenantId); device.setTenantId(tenantId);
device = deviceService.saveDevice(device); Device savedDevice = deviceService.saveDevice(device);
try { try {
deviceService.assignDeviceToCustomer(tenantId, device.getId(), new CustomerId(Uuids.timeBased())); Assertions.assertThrows(DataValidationException.class, () -> {
deviceService.assignDeviceToCustomer(tenantId, savedDevice.getId(), new CustomerId(Uuids.timeBased()));
});
} finally { } finally {
deviceService.deleteDevice(tenantId, device.getId()); deviceService.deleteDevice(tenantId, savedDevice.getId());
} }
} }
@Test(expected = DataValidationException.class) @Test
public void testAssignDeviceToCustomerFromDifferentTenant() { public void testAssignDeviceToCustomerFromDifferentTenant() {
Device device = new Device(); Device device = new Device();
device.setName("My device"); device.setName("My device");
device.setType("default"); device.setType("default");
device.setTenantId(tenantId); device.setTenantId(tenantId);
device = deviceService.saveDevice(device); Device savedDevice = deviceService.saveDevice(device);
Tenant tenant = new Tenant(); Tenant tenant = new Tenant();
tenant.setTitle("Test different tenant"); tenant.setTitle("Test different tenant");
tenant = tenantService.saveTenant(tenant); tenant = tenantService.saveTenant(tenant);
Customer customer = new Customer(); Customer customer = new Customer();
customer.setTenantId(tenant.getId()); customer.setTenantId(tenant.getId());
customer.setTitle("Test different customer"); customer.setTitle("Test different customer");
customer = customerService.saveCustomer(customer); Customer savedCustomer = customerService.saveCustomer(customer);
try { try {
deviceService.assignDeviceToCustomer(tenantId, device.getId(), customer.getId()); Assertions.assertThrows(DataValidationException.class, () -> {
deviceService.assignDeviceToCustomer(tenantId, savedDevice.getId(), savedCustomer.getId());
});
} finally { } finally {
deviceService.deleteDevice(tenantId, device.getId()); deviceService.deleteDevice(tenantId, savedDevice.getId());
tenantService.deleteTenant(tenant.getId()); tenantService.deleteTenant(tenant.getId());
} }
} }

41
dao/src/test/java/org/thingsboard/server/dao/service/BaseEdgeServiceTest.java

@ -21,6 +21,7 @@ import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.EntitySubtype; import org.thingsboard.server.common.data.EntitySubtype;
@ -86,57 +87,67 @@ public abstract class BaseEdgeServiceTest extends AbstractServiceTest {
edgeService.deleteEdge(tenantId, savedEdge.getId()); edgeService.deleteEdge(tenantId, savedEdge.getId());
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveEdgeWithEmptyName() { public void testSaveEdgeWithEmptyName() {
Edge edge = new Edge(); Edge edge = new Edge();
edge.setType("default"); edge.setType("default");
edge.setTenantId(tenantId); edge.setTenantId(tenantId);
edgeService.saveEdge(edge); Assertions.assertThrows(DataValidationException.class, () -> {
edgeService.saveEdge(edge);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveEdgeWithEmptyTenant() { public void testSaveEdgeWithEmptyTenant() {
Edge edge = new Edge(); Edge edge = new Edge();
edge.setName("My edge"); edge.setName("My edge");
edge.setType("default"); edge.setType("default");
edgeService.saveEdge(edge); Assertions.assertThrows(DataValidationException.class, () -> {
edgeService.saveEdge(edge);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveEdgeWithInvalidTenant() { public void testSaveEdgeWithInvalidTenant() {
Edge edge = new Edge(); Edge edge = new Edge();
edge.setName("My edge"); edge.setName("My edge");
edge.setType("default"); edge.setType("default");
edge.setTenantId(TenantId.fromUUID(Uuids.timeBased())); edge.setTenantId(TenantId.fromUUID(Uuids.timeBased()));
edgeService.saveEdge(edge); Assertions.assertThrows(DataValidationException.class, () -> {
edgeService.saveEdge(edge);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testAssignEdgeToNonExistentCustomer() { public void testAssignEdgeToNonExistentCustomer() {
Edge edge = constructEdge("My edge", "default"); Edge edge = constructEdge("My edge", "default");
edge = edgeService.saveEdge(edge); Edge savedEdge = edgeService.saveEdge(edge);
try { try {
edgeService.assignEdgeToCustomer(tenantId, edge.getId(), new CustomerId(Uuids.timeBased())); Assertions.assertThrows(DataValidationException.class, () -> {
edgeService.assignEdgeToCustomer(tenantId, savedEdge.getId(), new CustomerId(Uuids.timeBased()));
});
} finally { } finally {
edgeService.deleteEdge(tenantId, edge.getId()); edgeService.deleteEdge(tenantId, savedEdge.getId());
} }
} }
@Test(expected = DataValidationException.class) @Test
public void testAssignEdgeToCustomerFromDifferentTenant() { public void testAssignEdgeToCustomerFromDifferentTenant() {
Edge edge = constructEdge("My edge", "default"); Edge edge = constructEdge("My edge", "default");
edge = edgeService.saveEdge(edge); Edge savedEdge = edgeService.saveEdge(edge);
Tenant tenant = new Tenant(); Tenant tenant = new Tenant();
tenant.setTitle("Test different tenant"); tenant.setTitle("Test different tenant");
tenant = tenantService.saveTenant(tenant); tenant = tenantService.saveTenant(tenant);
Customer customer = new Customer(); Customer customer = new Customer();
customer.setTenantId(tenant.getId()); customer.setTenantId(tenant.getId());
customer.setTitle("Test different customer"); customer.setTitle("Test different customer");
customer = customerService.saveCustomer(customer); Customer savedCustomer = customerService.saveCustomer(customer);
try { try {
edgeService.assignEdgeToCustomer(tenantId, edge.getId(), customer.getId()); Assertions.assertThrows(DataValidationException.class, () -> {
edgeService.assignEdgeToCustomer(tenantId, savedEdge.getId(), savedCustomer.getId());
});
} finally { } finally {
edgeService.deleteEdge(tenantId, edge.getId()); edgeService.deleteEdge(tenantId, savedEdge.getId());
tenantService.deleteTenant(tenant.getId()); tenantService.deleteTenant(tenant.getId());
} }
} }

7
dao/src/test/java/org/thingsboard/server/dao/service/BaseOAuth2ConfigTemplateServiceTest.java

@ -19,6 +19,7 @@ import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.oauth2.MapperType; import org.thingsboard.server.common.data.oauth2.MapperType;
@ -51,12 +52,14 @@ public abstract class BaseOAuth2ConfigTemplateServiceTest extends AbstractServic
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveDuplicateProviderId() { public void testSaveDuplicateProviderId() {
OAuth2ClientRegistrationTemplate first = validClientRegistrationTemplate("providerId"); OAuth2ClientRegistrationTemplate first = validClientRegistrationTemplate("providerId");
OAuth2ClientRegistrationTemplate second = validClientRegistrationTemplate("providerId"); OAuth2ClientRegistrationTemplate second = validClientRegistrationTemplate("providerId");
oAuth2ConfigTemplateService.saveClientRegistrationTemplate(first); oAuth2ConfigTemplateService.saveClientRegistrationTemplate(first);
oAuth2ConfigTemplateService.saveClientRegistrationTemplate(second); Assertions.assertThrows(DataValidationException.class, () -> {
oAuth2ConfigTemplateService.saveClientRegistrationTemplate(second);
});
} }
@Test @Test

13
dao/src/test/java/org/thingsboard/server/dao/service/BaseOAuth2ServiceTest.java

@ -20,6 +20,7 @@ import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.StringUtils;
import org.thingsboard.server.common.data.oauth2.MapperType; import org.thingsboard.server.common.data.oauth2.MapperType;
@ -61,7 +62,7 @@ public abstract class BaseOAuth2ServiceTest extends AbstractServiceTest {
Assert.assertTrue(oAuth2Service.findOAuth2Info().getOauth2ParamsInfos().isEmpty()); Assert.assertTrue(oAuth2Service.findOAuth2Info().getOauth2ParamsInfos().isEmpty());
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveHttpAndMixedDomainsTogether() { public void testSaveHttpAndMixedDomainsTogether() {
OAuth2Info oAuth2Info = new OAuth2Info(true, Lists.newArrayList( OAuth2Info oAuth2Info = new OAuth2Info(true, Lists.newArrayList(
OAuth2ParamsInfo.builder() OAuth2ParamsInfo.builder()
@ -77,10 +78,12 @@ public abstract class BaseOAuth2ServiceTest extends AbstractServiceTest {
)) ))
.build() .build()
)); ));
oAuth2Service.saveOAuth2Info(oAuth2Info); Assertions.assertThrows(DataValidationException.class, () -> {
oAuth2Service.saveOAuth2Info(oAuth2Info);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveHttpsAndMixedDomainsTogether() { public void testSaveHttpsAndMixedDomainsTogether() {
OAuth2Info oAuth2Info = new OAuth2Info(true, Lists.newArrayList( OAuth2Info oAuth2Info = new OAuth2Info(true, Lists.newArrayList(
OAuth2ParamsInfo.builder() OAuth2ParamsInfo.builder()
@ -96,7 +99,9 @@ public abstract class BaseOAuth2ServiceTest extends AbstractServiceTest {
)) ))
.build() .build()
)); ));
oAuth2Service.saveOAuth2Info(oAuth2Info); Assertions.assertThrows(DataValidationException.class, () -> {
oAuth2Service.saveOAuth2Info(oAuth2Info);
});
} }
@Test @Test

103
dao/src/test/java/org/thingsboard/server/dao/service/BaseQueueServiceTest.java

@ -19,6 +19,7 @@ import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.thingsboard.server.common.data.DataConstants; import org.thingsboard.server.common.data.DataConstants;
import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.TenantProfile; import org.thingsboard.server.common.data.TenantProfile;
@ -139,7 +140,7 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest {
queueService.deleteQueue(tenantId, foundQueue.getId()); queueService.deleteQueue(tenantId, foundQueue.getId());
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveQueueWithEmptyName() { public void testSaveQueueWithEmptyName() {
Queue queue = new Queue(); Queue queue = new Queue();
queue.setTenantId(tenantId); queue.setTenantId(tenantId);
@ -149,10 +150,12 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest {
queue.setPackProcessingTimeout(2000); queue.setPackProcessingTimeout(2000);
queue.setSubmitStrategy(createTestSubmitStrategy()); queue.setSubmitStrategy(createTestSubmitStrategy());
queue.setProcessingStrategy(createTestProcessingStrategy()); queue.setProcessingStrategy(createTestProcessingStrategy());
queueService.saveQueue(queue); Assertions.assertThrows(DataValidationException.class, () -> {
queueService.saveQueue(queue);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveQueueWithInvalidName() { public void testSaveQueueWithInvalidName() {
Queue queue = new Queue(); Queue queue = new Queue();
queue.setTenantId(tenantId); queue.setTenantId(tenantId);
@ -163,10 +166,12 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest {
queue.setPackProcessingTimeout(2000); queue.setPackProcessingTimeout(2000);
queue.setSubmitStrategy(createTestSubmitStrategy()); queue.setSubmitStrategy(createTestSubmitStrategy());
queue.setProcessingStrategy(createTestProcessingStrategy()); queue.setProcessingStrategy(createTestProcessingStrategy());
queueService.saveQueue(queue); Assertions.assertThrows(DataValidationException.class, () -> {
queueService.saveQueue(queue);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveQueueWithEmptyTopic() { public void testSaveQueueWithEmptyTopic() {
Queue queue = new Queue(); Queue queue = new Queue();
queue.setTenantId(tenantId); queue.setTenantId(tenantId);
@ -176,10 +181,12 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest {
queue.setPackProcessingTimeout(2000); queue.setPackProcessingTimeout(2000);
queue.setSubmitStrategy(createTestSubmitStrategy()); queue.setSubmitStrategy(createTestSubmitStrategy());
queue.setProcessingStrategy(createTestProcessingStrategy()); queue.setProcessingStrategy(createTestProcessingStrategy());
queueService.saveQueue(queue); Assertions.assertThrows(DataValidationException.class, () -> {
queueService.saveQueue(queue);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveQueueWithInvalidTopic() { public void testSaveQueueWithInvalidTopic() {
Queue queue = new Queue(); Queue queue = new Queue();
queue.setTenantId(tenantId); queue.setTenantId(tenantId);
@ -190,10 +197,12 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest {
queue.setPackProcessingTimeout(2000); queue.setPackProcessingTimeout(2000);
queue.setSubmitStrategy(createTestSubmitStrategy()); queue.setSubmitStrategy(createTestSubmitStrategy());
queue.setProcessingStrategy(createTestProcessingStrategy()); queue.setProcessingStrategy(createTestProcessingStrategy());
queueService.saveQueue(queue); Assertions.assertThrows(DataValidationException.class, () -> {
queueService.saveQueue(queue);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveQueueWithEmptyPollInterval() { public void testSaveQueueWithEmptyPollInterval() {
Queue queue = new Queue(); Queue queue = new Queue();
queue.setTenantId(tenantId); queue.setTenantId(tenantId);
@ -203,10 +212,12 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest {
queue.setPackProcessingTimeout(2000); queue.setPackProcessingTimeout(2000);
queue.setSubmitStrategy(createTestSubmitStrategy()); queue.setSubmitStrategy(createTestSubmitStrategy());
queue.setProcessingStrategy(createTestProcessingStrategy()); queue.setProcessingStrategy(createTestProcessingStrategy());
queueService.saveQueue(queue); Assertions.assertThrows(DataValidationException.class, () -> {
queueService.saveQueue(queue);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveQueueWithEmptyPartitions() { public void testSaveQueueWithEmptyPartitions() {
Queue queue = new Queue(); Queue queue = new Queue();
queue.setTenantId(tenantId); queue.setTenantId(tenantId);
@ -216,10 +227,12 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest {
queue.setPackProcessingTimeout(2000); queue.setPackProcessingTimeout(2000);
queue.setSubmitStrategy(createTestSubmitStrategy()); queue.setSubmitStrategy(createTestSubmitStrategy());
queue.setProcessingStrategy(createTestProcessingStrategy()); queue.setProcessingStrategy(createTestProcessingStrategy());
queueService.saveQueue(queue); Assertions.assertThrows(DataValidationException.class, () -> {
queueService.saveQueue(queue);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveQueueWithEmptyPackProcessingTimeout() { public void testSaveQueueWithEmptyPackProcessingTimeout() {
Queue queue = new Queue(); Queue queue = new Queue();
queue.setTenantId(tenantId); queue.setTenantId(tenantId);
@ -229,10 +242,12 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest {
queue.setPartitions(1); queue.setPartitions(1);
queue.setSubmitStrategy(createTestSubmitStrategy()); queue.setSubmitStrategy(createTestSubmitStrategy());
queue.setProcessingStrategy(createTestProcessingStrategy()); queue.setProcessingStrategy(createTestProcessingStrategy());
queueService.saveQueue(queue); Assertions.assertThrows(DataValidationException.class, () -> {
queueService.saveQueue(queue);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveQueueWithEmptySubmitStrategy() { public void testSaveQueueWithEmptySubmitStrategy() {
Queue queue = new Queue(); Queue queue = new Queue();
queue.setTenantId(tenantId); queue.setTenantId(tenantId);
@ -242,10 +257,12 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest {
queue.setPartitions(1); queue.setPartitions(1);
queue.setPackProcessingTimeout(2000); queue.setPackProcessingTimeout(2000);
queue.setProcessingStrategy(createTestProcessingStrategy()); queue.setProcessingStrategy(createTestProcessingStrategy());
queueService.saveQueue(queue); Assertions.assertThrows(DataValidationException.class, () -> {
queueService.saveQueue(queue);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveQueueWithEmptyProcessingStrategy() { public void testSaveQueueWithEmptyProcessingStrategy() {
Queue queue = new Queue(); Queue queue = new Queue();
queue.setTenantId(tenantId); queue.setTenantId(tenantId);
@ -255,10 +272,12 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest {
queue.setPartitions(1); queue.setPartitions(1);
queue.setPackProcessingTimeout(2000); queue.setPackProcessingTimeout(2000);
queue.setSubmitStrategy(createTestSubmitStrategy()); queue.setSubmitStrategy(createTestSubmitStrategy());
queueService.saveQueue(queue); Assertions.assertThrows(DataValidationException.class, () -> {
queueService.saveQueue(queue);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveQueueWithEmptySubmitStrategyType() { public void testSaveQueueWithEmptySubmitStrategyType() {
Queue queue = new Queue(); Queue queue = new Queue();
queue.setTenantId(tenantId); queue.setTenantId(tenantId);
@ -270,10 +289,12 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest {
queue.setSubmitStrategy(createTestSubmitStrategy()); queue.setSubmitStrategy(createTestSubmitStrategy());
queue.getSubmitStrategy().setType(null); queue.getSubmitStrategy().setType(null);
queue.setProcessingStrategy(createTestProcessingStrategy()); queue.setProcessingStrategy(createTestProcessingStrategy());
queueService.saveQueue(queue); Assertions.assertThrows(DataValidationException.class, () -> {
queueService.saveQueue(queue);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveQueueWithEmptySubmitStrategyBatchSize() { public void testSaveQueueWithEmptySubmitStrategyBatchSize() {
Queue queue = new Queue(); Queue queue = new Queue();
queue.setTenantId(tenantId); queue.setTenantId(tenantId);
@ -286,10 +307,12 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest {
queue.getSubmitStrategy().setType(SubmitStrategyType.BATCH); queue.getSubmitStrategy().setType(SubmitStrategyType.BATCH);
queue.getSubmitStrategy().setBatchSize(0); queue.getSubmitStrategy().setBatchSize(0);
queue.setProcessingStrategy(createTestProcessingStrategy()); queue.setProcessingStrategy(createTestProcessingStrategy());
queueService.saveQueue(queue); Assertions.assertThrows(DataValidationException.class, () -> {
queueService.saveQueue(queue);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveQueueWithEmptyProcessingStrategyType() { public void testSaveQueueWithEmptyProcessingStrategyType() {
Queue queue = new Queue(); Queue queue = new Queue();
queue.setTenantId(tenantId); queue.setTenantId(tenantId);
@ -301,10 +324,12 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest {
queue.setSubmitStrategy(createTestSubmitStrategy()); queue.setSubmitStrategy(createTestSubmitStrategy());
queue.setProcessingStrategy(createTestProcessingStrategy()); queue.setProcessingStrategy(createTestProcessingStrategy());
queue.getProcessingStrategy().setType(null); queue.getProcessingStrategy().setType(null);
queueService.saveQueue(queue); Assertions.assertThrows(DataValidationException.class, () -> {
queueService.saveQueue(queue);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveQueueWithNegativeProcessingStrategyRetries() { public void testSaveQueueWithNegativeProcessingStrategyRetries() {
Queue queue = new Queue(); Queue queue = new Queue();
queue.setTenantId(tenantId); queue.setTenantId(tenantId);
@ -316,10 +341,12 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest {
queue.setSubmitStrategy(createTestSubmitStrategy()); queue.setSubmitStrategy(createTestSubmitStrategy());
queue.setProcessingStrategy(createTestProcessingStrategy()); queue.setProcessingStrategy(createTestProcessingStrategy());
queue.getProcessingStrategy().setRetries(-1); queue.getProcessingStrategy().setRetries(-1);
queueService.saveQueue(queue); Assertions.assertThrows(DataValidationException.class, () -> {
queueService.saveQueue(queue);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveQueueWithNegativeProcessingStrategyFailurePercentage() { public void testSaveQueueWithNegativeProcessingStrategyFailurePercentage() {
Queue queue = new Queue(); Queue queue = new Queue();
queue.setTenantId(tenantId); queue.setTenantId(tenantId);
@ -331,10 +358,12 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest {
queue.setSubmitStrategy(createTestSubmitStrategy()); queue.setSubmitStrategy(createTestSubmitStrategy());
queue.setProcessingStrategy(createTestProcessingStrategy()); queue.setProcessingStrategy(createTestProcessingStrategy());
queue.getProcessingStrategy().setFailurePercentage(-1); queue.getProcessingStrategy().setFailurePercentage(-1);
queueService.saveQueue(queue); Assertions.assertThrows(DataValidationException.class, () -> {
queueService.saveQueue(queue);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveQueueWithNegativeProcessingStrategyPauseBetweenRetries() { public void testSaveQueueWithNegativeProcessingStrategyPauseBetweenRetries() {
Queue queue = new Queue(); Queue queue = new Queue();
queue.setTenantId(tenantId); queue.setTenantId(tenantId);
@ -346,10 +375,12 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest {
queue.setSubmitStrategy(createTestSubmitStrategy()); queue.setSubmitStrategy(createTestSubmitStrategy());
queue.setProcessingStrategy(createTestProcessingStrategy()); queue.setProcessingStrategy(createTestProcessingStrategy());
queue.getProcessingStrategy().setPauseBetweenRetries(-1); queue.getProcessingStrategy().setPauseBetweenRetries(-1);
queueService.saveQueue(queue); Assertions.assertThrows(DataValidationException.class, () -> {
queueService.saveQueue(queue);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveQueueWithProcessingStrategyPauseBetweenRetriesBiggerThenMaxPauseBetweenRetries() { public void testSaveQueueWithProcessingStrategyPauseBetweenRetriesBiggerThenMaxPauseBetweenRetries() {
Queue queue = new Queue(); Queue queue = new Queue();
queue.setTenantId(tenantId); queue.setTenantId(tenantId);
@ -361,10 +392,12 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest {
queue.setSubmitStrategy(createTestSubmitStrategy()); queue.setSubmitStrategy(createTestSubmitStrategy());
queue.setProcessingStrategy(createTestProcessingStrategy()); queue.setProcessingStrategy(createTestProcessingStrategy());
queue.getProcessingStrategy().setPauseBetweenRetries(100); queue.getProcessingStrategy().setPauseBetweenRetries(100);
queueService.saveQueue(queue); Assertions.assertThrows(DataValidationException.class, () -> {
queueService.saveQueue(queue);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveQueueWithNotIsolatedTenant() { public void testSaveQueueWithNotIsolatedTenant() {
Tenant tenant = new Tenant(); Tenant tenant = new Tenant();
tenant.setTitle("Not isolated tenant"); tenant.setTitle("Not isolated tenant");
@ -381,7 +414,9 @@ public abstract class BaseQueueServiceTest extends AbstractServiceTest {
queue.setSubmitStrategy(createTestSubmitStrategy()); queue.setSubmitStrategy(createTestSubmitStrategy());
queue.setProcessingStrategy(createTestProcessingStrategy()); queue.setProcessingStrategy(createTestProcessingStrategy());
try { try {
queueService.saveQueue(queue); Assertions.assertThrows(DataValidationException.class, () -> {
queueService.saveQueue(queue);
});
} finally { } finally {
tenantService.deleteTenant(savedTenant.getId()); tenantService.deleteTenant(savedTenant.getId());
} }

19
dao/src/test/java/org/thingsboard/server/dao/service/BaseRelationServiceTest.java

@ -22,6 +22,7 @@ import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.id.AssetId; import org.thingsboard.server.common.data.id.AssetId;
import org.thingsboard.server.common.data.id.DeviceId; import org.thingsboard.server.common.data.id.DeviceId;
@ -334,28 +335,34 @@ public abstract class BaseRelationServiceTest extends AbstractServiceTest {
} }
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveRelationWithEmptyFrom() throws ExecutionException, InterruptedException { public void testSaveRelationWithEmptyFrom() throws ExecutionException, InterruptedException {
EntityRelation relation = new EntityRelation(); EntityRelation relation = new EntityRelation();
relation.setTo(new AssetId(Uuids.timeBased())); relation.setTo(new AssetId(Uuids.timeBased()));
relation.setType(EntityRelation.CONTAINS_TYPE); relation.setType(EntityRelation.CONTAINS_TYPE);
Assert.assertTrue(saveRelation(relation)); Assertions.assertThrows(DataValidationException.class, () -> {
Assert.assertTrue(saveRelation(relation));
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveRelationWithEmptyTo() throws ExecutionException, InterruptedException { public void testSaveRelationWithEmptyTo() throws ExecutionException, InterruptedException {
EntityRelation relation = new EntityRelation(); EntityRelation relation = new EntityRelation();
relation.setFrom(new AssetId(Uuids.timeBased())); relation.setFrom(new AssetId(Uuids.timeBased()));
relation.setType(EntityRelation.CONTAINS_TYPE); relation.setType(EntityRelation.CONTAINS_TYPE);
Assert.assertTrue(saveRelation(relation)); Assertions.assertThrows(DataValidationException.class, () -> {
Assert.assertTrue(saveRelation(relation));
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveRelationWithEmptyType() throws ExecutionException, InterruptedException { public void testSaveRelationWithEmptyType() throws ExecutionException, InterruptedException {
EntityRelation relation = new EntityRelation(); EntityRelation relation = new EntityRelation();
relation.setFrom(new AssetId(Uuids.timeBased())); relation.setFrom(new AssetId(Uuids.timeBased()));
relation.setTo(new AssetId(Uuids.timeBased())); relation.setTo(new AssetId(Uuids.timeBased()));
Assert.assertTrue(saveRelation(relation)); Assertions.assertThrows(DataValidationException.class, () -> {
Assert.assertTrue(saveRelation(relation));
});
} }
@Test @Test

29
dao/src/test/java/org/thingsboard/server/dao/service/BaseRuleChainServiceTest.java

@ -21,6 +21,7 @@ import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.StringUtils;
import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.edge.Edge;
@ -86,19 +87,23 @@ public abstract class BaseRuleChainServiceTest extends AbstractServiceTest {
ruleChainService.deleteRuleChainById(tenantId, savedRuleChain.getId()); ruleChainService.deleteRuleChainById(tenantId, savedRuleChain.getId());
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveRuleChainWithEmptyName() { public void testSaveRuleChainWithEmptyName() {
RuleChain ruleChain = new RuleChain(); RuleChain ruleChain = new RuleChain();
ruleChain.setTenantId(tenantId); ruleChain.setTenantId(tenantId);
ruleChainService.saveRuleChain(ruleChain); Assertions.assertThrows(DataValidationException.class, () -> {
ruleChainService.saveRuleChain(ruleChain);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveRuleChainWithInvalidTenant() { public void testSaveRuleChainWithInvalidTenant() {
RuleChain ruleChain = new RuleChain(); RuleChain ruleChain = new RuleChain();
ruleChain.setName("My RuleChain"); ruleChain.setName("My RuleChain");
ruleChain.setTenantId(TenantId.fromUUID(Uuids.timeBased())); ruleChain.setTenantId(TenantId.fromUUID(Uuids.timeBased()));
ruleChainService.saveRuleChain(ruleChain); Assertions.assertThrows(DataValidationException.class, () -> {
ruleChainService.saveRuleChain(ruleChain);
});
} }
@Test @Test
@ -321,14 +326,18 @@ public abstract class BaseRuleChainServiceTest extends AbstractServiceTest {
ruleChainService.deleteRuleChainById(tenantId, savedRuleChainMetaData.getRuleChainId()); ruleChainService.deleteRuleChainById(tenantId, savedRuleChainMetaData.getRuleChainId());
} }
@Test(expected = DataValidationException.class) @Test
public void testUpdateRuleChainMetaDataWithCirclingRelation() throws Exception { public void testUpdateRuleChainMetaDataWithCirclingRelation() {
ruleChainService.saveRuleChainMetaData(tenantId, createRuleChainMetadataWithCirclingRelation()); Assertions.assertThrows(DataValidationException.class, () -> {
ruleChainService.saveRuleChainMetaData(tenantId, createRuleChainMetadataWithCirclingRelation());
});
} }
@Test(expected = DataValidationException.class) @Test
public void testUpdateRuleChainMetaDataWithCirclingRelation2() throws Exception { public void testUpdateRuleChainMetaDataWithCirclingRelation2() {
ruleChainService.saveRuleChainMetaData(tenantId, createRuleChainMetadataWithCirclingRelation2()); Assertions.assertThrows(DataValidationException.class, () -> {
ruleChainService.saveRuleChainMetaData(tenantId, createRuleChainMetadataWithCirclingRelation2());
});
} }
@Test @Test

27
dao/src/test/java/org/thingsboard/server/dao/service/BaseTenantProfileServiceTest.java

@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.node.NullNode;
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.thingsboard.server.common.data.DataConstants; import org.thingsboard.server.common.data.DataConstants;
import org.thingsboard.server.common.data.EntityInfo; import org.thingsboard.server.common.data.EntityInfo;
import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.Tenant;
@ -154,30 +155,36 @@ public abstract class BaseTenantProfileServiceTest extends AbstractServiceTest {
Assert.assertEquals(savedTenantProfile2.getId(), defaultTenantProfile.getId()); Assert.assertEquals(savedTenantProfile2.getId(), defaultTenantProfile.getId());
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveTenantProfileWithEmptyName() { public void testSaveTenantProfileWithEmptyName() {
TenantProfile tenantProfile = new TenantProfile(); TenantProfile tenantProfile = new TenantProfile();
tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile); Assertions.assertThrows(DataValidationException.class, () -> {
tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveTenantProfileWithSameName() { public void testSaveTenantProfileWithSameName() {
TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile"); TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile");
tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile); tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile);
TenantProfile tenantProfile2 = this.createTenantProfile("Tenant Profile"); TenantProfile tenantProfile2 = this.createTenantProfile("Tenant Profile");
tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile2); Assertions.assertThrows(DataValidationException.class, () -> {
tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile2);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveSameTenantProfileWithDifferentIsolatedTbRuleEngine() { public void testSaveSameTenantProfileWithDifferentIsolatedTbRuleEngine() {
TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile"); TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile");
TenantProfile savedTenantProfile = tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile); TenantProfile savedTenantProfile = tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile);
savedTenantProfile.setIsolatedTbRuleEngine(true); savedTenantProfile.setIsolatedTbRuleEngine(true);
addMainQueueConfig(savedTenantProfile); addMainQueueConfig(savedTenantProfile);
tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, savedTenantProfile); Assertions.assertThrows(DataValidationException.class, () -> {
tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, savedTenantProfile);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testDeleteTenantProfileWithExistingTenant() { public void testDeleteTenantProfileWithExistingTenant() {
TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile"); TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile");
TenantProfile savedTenantProfile = tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile); TenantProfile savedTenantProfile = tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile);
@ -186,7 +193,9 @@ public abstract class BaseTenantProfileServiceTest extends AbstractServiceTest {
tenant.setTenantProfileId(savedTenantProfile.getId()); tenant.setTenantProfileId(savedTenantProfile.getId());
tenant = tenantService.saveTenant(tenant); tenant = tenantService.saveTenant(tenant);
try { try {
tenantProfileService.deleteTenantProfile(TenantId.SYS_TENANT_ID, savedTenantProfile.getId()); Assertions.assertThrows(DataValidationException.class, () -> {
tenantProfileService.deleteTenantProfile(TenantId.SYS_TENANT_ID, savedTenantProfile.getId());
});
} finally { } finally {
tenantService.deleteTenant(tenant.getId()); tenantService.deleteTenant(tenant.getId());
} }
@ -294,7 +303,7 @@ public abstract class BaseTenantProfileServiceTest extends AbstractServiceTest {
return tenantProfile; return tenantProfile;
} }
private void addMainQueueConfig(TenantProfile tenantProfile) { public static void addMainQueueConfig(TenantProfile tenantProfile) {
TenantProfileQueueConfiguration mainQueueConfiguration = new TenantProfileQueueConfiguration(); TenantProfileQueueConfiguration mainQueueConfiguration = new TenantProfileQueueConfiguration();
mainQueueConfiguration.setName(DataConstants.MAIN_QUEUE_NAME); mainQueueConfiguration.setName(DataConstants.MAIN_QUEUE_NAME);
mainQueueConfiguration.setTopic(DataConstants.MAIN_QUEUE_TOPIC); mainQueueConfiguration.setTopic(DataConstants.MAIN_QUEUE_TOPIC);

32
dao/src/test/java/org/thingsboard/server/dao/service/BaseTenantServiceTest.java

@ -17,6 +17,7 @@ package org.thingsboard.server.dao.service;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.boot.test.mock.mockito.SpyBean;
@ -52,8 +53,6 @@ import org.thingsboard.server.common.data.rpc.RpcStatus;
import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChain;
import org.thingsboard.server.common.data.rule.RuleChainType; import org.thingsboard.server.common.data.rule.RuleChainType;
import org.thingsboard.server.common.data.security.Authority; import org.thingsboard.server.common.data.security.Authority;
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration;
import org.thingsboard.server.common.data.tenant.profile.TenantProfileData;
import org.thingsboard.server.common.data.widget.WidgetsBundle; import org.thingsboard.server.common.data.widget.WidgetsBundle;
import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.exception.DataValidationException;
import org.thingsboard.server.dao.tenant.TenantDao; import org.thingsboard.server.dao.tenant.TenantDao;
@ -121,18 +120,22 @@ public abstract class BaseTenantServiceTest extends AbstractServiceTest {
tenantService.deleteTenant(savedTenant.getId()); tenantService.deleteTenant(savedTenant.getId());
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveTenantWithEmptyTitle() { public void testSaveTenantWithEmptyTitle() {
Tenant tenant = new Tenant(); Tenant tenant = new Tenant();
tenantService.saveTenant(tenant); Assertions.assertThrows(DataValidationException.class, () -> {
tenantService.saveTenant(tenant);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveTenantWithInvalidEmail() { public void testSaveTenantWithInvalidEmail() {
Tenant tenant = new Tenant(); Tenant tenant = new Tenant();
tenant.setTitle("My tenant"); tenant.setTitle("My tenant");
tenant.setEmail("invalid@mail"); tenant.setEmail("invalid@mail");
tenantService.saveTenant(tenant); Assertions.assertThrows(DataValidationException.class, () -> {
tenantService.saveTenant(tenant);
});
} }
@Test @Test
@ -301,23 +304,6 @@ public abstract class BaseTenantServiceTest extends AbstractServiceTest {
} }
@Test(expected = DataValidationException.class)
public void testSaveTenantWithIsolatedProfileInMonolithSetup() {
TenantProfile tenantProfile = new TenantProfile();
tenantProfile.setName("Isolated Tenant Profile");
TenantProfileData profileData = new TenantProfileData();
profileData.setConfiguration(new DefaultTenantProfileConfiguration());
tenantProfile.setProfileData(profileData);
tenantProfile.setDefault(false);
tenantProfile.setIsolatedTbRuleEngine(true);
TenantProfile isolatedTenantProfile = tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile);
Tenant tenant = new Tenant();
tenant.setTitle("Tenant");
tenant.setTenantProfileId(isolatedTenantProfile.getId());
tenantService.saveTenant(tenant);
}
@Test @Test
public void testGettingTenantAddingItToCache() { public void testGettingTenantAddingItToCache() {
Tenant tenant = new Tenant(); Tenant tenant = new Tenant();

25
dao/src/test/java/org/thingsboard/server/dao/service/BaseUserServiceTest.java

@ -19,6 +19,7 @@ import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.StringUtils;
@ -142,32 +143,40 @@ public abstract class BaseUserServiceTest extends AbstractServiceTest {
userService.deleteUser(tenantId, savedUser.getId()); userService.deleteUser(tenantId, savedUser.getId());
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveUserWithSameEmail() { public void testSaveUserWithSameEmail() {
User tenantAdminUser = userService.findUserByEmail(tenantId, "tenant@thingsboard.org"); User tenantAdminUser = userService.findUserByEmail(tenantId, "tenant@thingsboard.org");
tenantAdminUser.setEmail("sysadmin@thingsboard.org"); tenantAdminUser.setEmail("sysadmin@thingsboard.org");
userService.saveUser(tenantAdminUser); Assertions.assertThrows(DataValidationException.class, () -> {
userService.saveUser(tenantAdminUser);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveUserWithInvalidEmail() { public void testSaveUserWithInvalidEmail() {
User tenantAdminUser = userService.findUserByEmail(tenantId, "tenant@thingsboard.org"); User tenantAdminUser = userService.findUserByEmail(tenantId, "tenant@thingsboard.org");
tenantAdminUser.setEmail("tenant_thingsboard.org"); tenantAdminUser.setEmail("tenant_thingsboard.org");
userService.saveUser(tenantAdminUser); Assertions.assertThrows(DataValidationException.class, () -> {
userService.saveUser(tenantAdminUser);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveUserWithEmptyEmail() { public void testSaveUserWithEmptyEmail() {
User tenantAdminUser = userService.findUserByEmail(tenantId, "tenant@thingsboard.org"); User tenantAdminUser = userService.findUserByEmail(tenantId, "tenant@thingsboard.org");
tenantAdminUser.setEmail(null); tenantAdminUser.setEmail(null);
userService.saveUser(tenantAdminUser); Assertions.assertThrows(DataValidationException.class, () -> {
userService.saveUser(tenantAdminUser);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveUserWithoutTenant() { public void testSaveUserWithoutTenant() {
User tenantAdminUser = userService.findUserByEmail(tenantId, "tenant@thingsboard.org"); User tenantAdminUser = userService.findUserByEmail(tenantId, "tenant@thingsboard.org");
tenantAdminUser.setTenantId(null); tenantAdminUser.setTenantId(null);
userService.saveUser(tenantAdminUser); Assertions.assertThrows(DataValidationException.class, () -> {
userService.saveUser(tenantAdminUser);
});
} }
@Test @Test

49
dao/src/test/java/org/thingsboard/server/dao/service/BaseWidgetTypeServiceTest.java

@ -22,6 +22,7 @@ import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.widget.WidgetType; import org.thingsboard.server.common.data.widget.WidgetType;
@ -88,7 +89,7 @@ public abstract class BaseWidgetTypeServiceTest extends AbstractServiceTest {
widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId()); widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId());
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveWidgetTypeWithEmptyName() throws IOException { public void testSaveWidgetTypeWithEmptyName() throws IOException {
WidgetsBundle widgetsBundle = new WidgetsBundle(); WidgetsBundle widgetsBundle = new WidgetsBundle();
widgetsBundle.setTenantId(tenantId); widgetsBundle.setTenantId(tenantId);
@ -100,22 +101,26 @@ public abstract class BaseWidgetTypeServiceTest extends AbstractServiceTest {
widgetType.setBundleAlias(savedWidgetsBundle.getAlias()); widgetType.setBundleAlias(savedWidgetsBundle.getAlias());
widgetType.setDescriptor(new ObjectMapper().readValue("{ \"someKey\": \"someValue\" }", JsonNode.class)); widgetType.setDescriptor(new ObjectMapper().readValue("{ \"someKey\": \"someValue\" }", JsonNode.class));
try { try {
widgetTypeService.saveWidgetType(widgetType); Assertions.assertThrows(DataValidationException.class, () -> {
widgetTypeService.saveWidgetType(widgetType);
});
} finally { } finally {
widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId()); widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId());
} }
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveWidgetTypeWithEmptyBundleAlias() throws IOException { public void testSaveWidgetTypeWithEmptyBundleAlias() throws IOException {
WidgetTypeDetails widgetType = new WidgetTypeDetails(); WidgetTypeDetails widgetType = new WidgetTypeDetails();
widgetType.setTenantId(tenantId); widgetType.setTenantId(tenantId);
widgetType.setName("Widget Type"); widgetType.setName("Widget Type");
widgetType.setDescriptor(new ObjectMapper().readValue("{ \"someKey\": \"someValue\" }", JsonNode.class)); widgetType.setDescriptor(new ObjectMapper().readValue("{ \"someKey\": \"someValue\" }", JsonNode.class));
widgetTypeService.saveWidgetType(widgetType); Assertions.assertThrows(DataValidationException.class, () -> {
widgetTypeService.saveWidgetType(widgetType);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveWidgetTypeWithEmptyDescriptor() throws IOException { public void testSaveWidgetTypeWithEmptyDescriptor() throws IOException {
WidgetsBundle widgetsBundle = new WidgetsBundle(); WidgetsBundle widgetsBundle = new WidgetsBundle();
widgetsBundle.setTenantId(tenantId); widgetsBundle.setTenantId(tenantId);
@ -128,13 +133,15 @@ public abstract class BaseWidgetTypeServiceTest extends AbstractServiceTest {
widgetType.setBundleAlias(savedWidgetsBundle.getAlias()); widgetType.setBundleAlias(savedWidgetsBundle.getAlias());
widgetType.setDescriptor(new ObjectMapper().readValue("{}", JsonNode.class)); widgetType.setDescriptor(new ObjectMapper().readValue("{}", JsonNode.class));
try { try {
widgetTypeService.saveWidgetType(widgetType); Assertions.assertThrows(DataValidationException.class, () -> {
widgetTypeService.saveWidgetType(widgetType);
});
} finally { } finally {
widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId()); widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId());
} }
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveWidgetTypeWithInvalidTenant() throws IOException { public void testSaveWidgetTypeWithInvalidTenant() throws IOException {
WidgetsBundle widgetsBundle = new WidgetsBundle(); WidgetsBundle widgetsBundle = new WidgetsBundle();
widgetsBundle.setTenantId(tenantId); widgetsBundle.setTenantId(tenantId);
@ -147,23 +154,27 @@ public abstract class BaseWidgetTypeServiceTest extends AbstractServiceTest {
widgetType.setName("Widget Type"); widgetType.setName("Widget Type");
widgetType.setDescriptor(new ObjectMapper().readValue("{ \"someKey\": \"someValue\" }", JsonNode.class)); widgetType.setDescriptor(new ObjectMapper().readValue("{ \"someKey\": \"someValue\" }", JsonNode.class));
try { try {
widgetTypeService.saveWidgetType(widgetType); Assertions.assertThrows(DataValidationException.class, () -> {
widgetTypeService.saveWidgetType(widgetType);
});
} finally { } finally {
widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId()); widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId());
} }
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveWidgetTypeWithInvalidBundleAlias() throws IOException { public void testSaveWidgetTypeWithInvalidBundleAlias() throws IOException {
WidgetTypeDetails widgetType = new WidgetTypeDetails(); WidgetTypeDetails widgetType = new WidgetTypeDetails();
widgetType.setTenantId(tenantId); widgetType.setTenantId(tenantId);
widgetType.setBundleAlias("some_alias"); widgetType.setBundleAlias("some_alias");
widgetType.setName("Widget Type"); widgetType.setName("Widget Type");
widgetType.setDescriptor(new ObjectMapper().readValue("{ \"someKey\": \"someValue\" }", JsonNode.class)); widgetType.setDescriptor(new ObjectMapper().readValue("{ \"someKey\": \"someValue\" }", JsonNode.class));
widgetTypeService.saveWidgetType(widgetType); Assertions.assertThrows(DataValidationException.class, () -> {
widgetTypeService.saveWidgetType(widgetType);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testUpdateWidgetTypeTenant() throws IOException { public void testUpdateWidgetTypeTenant() throws IOException {
WidgetsBundle widgetsBundle = new WidgetsBundle(); WidgetsBundle widgetsBundle = new WidgetsBundle();
widgetsBundle.setTenantId(tenantId); widgetsBundle.setTenantId(tenantId);
@ -178,13 +189,15 @@ public abstract class BaseWidgetTypeServiceTest extends AbstractServiceTest {
WidgetTypeDetails savedWidgetType = widgetTypeService.saveWidgetType(widgetType); WidgetTypeDetails savedWidgetType = widgetTypeService.saveWidgetType(widgetType);
savedWidgetType.setTenantId(TenantId.fromUUID(ModelConstants.NULL_UUID)); savedWidgetType.setTenantId(TenantId.fromUUID(ModelConstants.NULL_UUID));
try { try {
widgetTypeService.saveWidgetType(savedWidgetType); Assertions.assertThrows(DataValidationException.class, () -> {
widgetTypeService.saveWidgetType(savedWidgetType);
});
} finally { } finally {
widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId()); widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId());
} }
} }
@Test(expected = DataValidationException.class) @Test
public void testUpdateWidgetTypeBundleAlias() throws IOException { public void testUpdateWidgetTypeBundleAlias() throws IOException {
WidgetsBundle widgetsBundle = new WidgetsBundle(); WidgetsBundle widgetsBundle = new WidgetsBundle();
widgetsBundle.setTenantId(tenantId); widgetsBundle.setTenantId(tenantId);
@ -199,13 +212,15 @@ public abstract class BaseWidgetTypeServiceTest extends AbstractServiceTest {
WidgetTypeDetails savedWidgetType = widgetTypeService.saveWidgetType(widgetType); WidgetTypeDetails savedWidgetType = widgetTypeService.saveWidgetType(widgetType);
savedWidgetType.setBundleAlias("some_alias"); savedWidgetType.setBundleAlias("some_alias");
try { try {
widgetTypeService.saveWidgetType(savedWidgetType); Assertions.assertThrows(DataValidationException.class, () -> {
widgetTypeService.saveWidgetType(savedWidgetType);
});
} finally { } finally {
widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId()); widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId());
} }
} }
@Test(expected = DataValidationException.class) @Test
public void testUpdateWidgetTypeAlias() throws IOException { public void testUpdateWidgetTypeAlias() throws IOException {
WidgetsBundle widgetsBundle = new WidgetsBundle(); WidgetsBundle widgetsBundle = new WidgetsBundle();
widgetsBundle.setTenantId(tenantId); widgetsBundle.setTenantId(tenantId);
@ -220,7 +235,9 @@ public abstract class BaseWidgetTypeServiceTest extends AbstractServiceTest {
WidgetTypeDetails savedWidgetType = widgetTypeService.saveWidgetType(widgetType); WidgetTypeDetails savedWidgetType = widgetTypeService.saveWidgetType(widgetType);
savedWidgetType.setAlias("some_alias"); savedWidgetType.setAlias("some_alias");
try { try {
widgetTypeService.saveWidgetType(savedWidgetType); Assertions.assertThrows(DataValidationException.class, () -> {
widgetTypeService.saveWidgetType(savedWidgetType);
});
} finally { } finally {
widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId()); widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId());
} }

25
dao/src/test/java/org/thingsboard/server/dao/service/BaseWidgetsBundleServiceTest.java

@ -20,6 +20,7 @@ import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageData;
@ -77,22 +78,26 @@ public abstract class BaseWidgetsBundleServiceTest extends AbstractServiceTest {
widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId()); widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId());
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveWidgetsBundleWithEmptyTitle() { public void testSaveWidgetsBundleWithEmptyTitle() {
WidgetsBundle widgetsBundle = new WidgetsBundle(); WidgetsBundle widgetsBundle = new WidgetsBundle();
widgetsBundle.setTenantId(tenantId); widgetsBundle.setTenantId(tenantId);
widgetsBundleService.saveWidgetsBundle(widgetsBundle); Assertions.assertThrows(DataValidationException.class, () -> {
widgetsBundleService.saveWidgetsBundle(widgetsBundle);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveWidgetsBundleWithInvalidTenant() { public void testSaveWidgetsBundleWithInvalidTenant() {
WidgetsBundle widgetsBundle = new WidgetsBundle(); WidgetsBundle widgetsBundle = new WidgetsBundle();
widgetsBundle.setTitle("My widgets bundle"); widgetsBundle.setTitle("My widgets bundle");
widgetsBundle.setTenantId(TenantId.fromUUID(Uuids.timeBased())); widgetsBundle.setTenantId(TenantId.fromUUID(Uuids.timeBased()));
widgetsBundleService.saveWidgetsBundle(widgetsBundle); Assertions.assertThrows(DataValidationException.class, () -> {
widgetsBundleService.saveWidgetsBundle(widgetsBundle);
});
} }
@Test(expected = DataValidationException.class) @Test
public void testUpdateWidgetsBundleTenant() { public void testUpdateWidgetsBundleTenant() {
WidgetsBundle widgetsBundle = new WidgetsBundle(); WidgetsBundle widgetsBundle = new WidgetsBundle();
widgetsBundle.setTitle("My widgets bundle"); widgetsBundle.setTitle("My widgets bundle");
@ -100,13 +105,15 @@ public abstract class BaseWidgetsBundleServiceTest extends AbstractServiceTest {
WidgetsBundle savedWidgetsBundle = widgetsBundleService.saveWidgetsBundle(widgetsBundle); WidgetsBundle savedWidgetsBundle = widgetsBundleService.saveWidgetsBundle(widgetsBundle);
savedWidgetsBundle.setTenantId(TenantId.fromUUID(ModelConstants.NULL_UUID)); savedWidgetsBundle.setTenantId(TenantId.fromUUID(ModelConstants.NULL_UUID));
try { try {
widgetsBundleService.saveWidgetsBundle(savedWidgetsBundle); Assertions.assertThrows(DataValidationException.class, () -> {
widgetsBundleService.saveWidgetsBundle(savedWidgetsBundle);
});
} finally { } finally {
widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId()); widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId());
} }
} }
@Test(expected = DataValidationException.class) @Test
public void testUpdateWidgetsBundleAlias() { public void testUpdateWidgetsBundleAlias() {
WidgetsBundle widgetsBundle = new WidgetsBundle(); WidgetsBundle widgetsBundle = new WidgetsBundle();
widgetsBundle.setTitle("My widgets bundle"); widgetsBundle.setTitle("My widgets bundle");
@ -114,7 +121,9 @@ public abstract class BaseWidgetsBundleServiceTest extends AbstractServiceTest {
WidgetsBundle savedWidgetsBundle = widgetsBundleService.saveWidgetsBundle(widgetsBundle); WidgetsBundle savedWidgetsBundle = widgetsBundleService.saveWidgetsBundle(widgetsBundle);
savedWidgetsBundle.setAlias("new_alias"); savedWidgetsBundle.setAlias("new_alias");
try { try {
widgetsBundleService.saveWidgetsBundle(savedWidgetsBundle); Assertions.assertThrows(DataValidationException.class, () -> {
widgetsBundleService.saveWidgetsBundle(savedWidgetsBundle);
});
} finally { } finally {
widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId()); widgetsBundleService.deleteWidgetsBundle(tenantId, savedWidgetsBundle.getId());
} }

41
dao/src/test/java/org/thingsboard/server/dao/service/DataValidatorTest.java

@ -15,7 +15,8 @@
*/ */
package org.thingsboard.server.dao.service; package org.thingsboard.server.dao.service;
import org.junit.Test; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.exception.DataValidationException;
public class DataValidatorTest { public class DataValidatorTest {
@ -26,38 +27,50 @@ public class DataValidatorTest {
DataValidator.validateEmail(email); DataValidator.validateEmail(email);
} }
@Test(expected = DataValidationException.class) @Test
public void validateInvalidEmail1() { public void validateInvalidEmail1() {
String email = "test:1@mail.io"; String email = "test:1@mail.io";
DataValidator.validateEmail(email); Assertions.assertThrows(DataValidationException.class, () -> {
DataValidator.validateEmail(email);
});
} }
@Test(expected = DataValidationException.class) @Test
public void validateInvalidEmail2() { public void validateInvalidEmail2() {
String email = "test()1@mail.io"; String email = "test()1@mail.io";
DataValidator.validateEmail(email); Assertions.assertThrows(DataValidationException.class, () -> {
DataValidator.validateEmail(email);
});
} }
@Test(expected = DataValidationException.class) @Test
public void validateInvalidEmail3() { public void validateInvalidEmail3() {
String email = "test[]1@mail.io"; String email = "test[]1@mail.io";
DataValidator.validateEmail(email); Assertions.assertThrows(DataValidationException.class, () -> {
DataValidator.validateEmail(email);
});
} }
@Test(expected = DataValidationException.class) @Test
public void validateInvalidEmail4() { public void validateInvalidEmail4() {
String email = "test\\1@mail.io"; String email = "test\\1@mail.io";
DataValidator.validateEmail(email); Assertions.assertThrows(DataValidationException.class, () -> {
DataValidator.validateEmail(email);
});
} }
@Test(expected = DataValidationException.class) @Test
public void validateInvalidEmail5() { public void validateInvalidEmail5() {
String email = "test\"1@mail.io"; String email = "test\"1@mail.io";
DataValidator.validateEmail(email); Assertions.assertThrows(DataValidationException.class, () -> {
DataValidator.validateEmail(email);
});
} }
@Test(expected = DataValidationException.class) @Test
public void validateInvalidEmail6() { public void validateInvalidEmail6() {
String email = "test<>1@mail.io"; String email = "test<>1@mail.io";
DataValidator.validateEmail(email); Assertions.assertThrows(DataValidationException.class, () -> {
DataValidator.validateEmail(email);
});
} }
} }

2
dao/src/test/java/org/thingsboard/server/dao/service/NoXssValidatorTest.java

@ -16,7 +16,7 @@
package org.thingsboard.server.dao.service; package org.thingsboard.server.dao.service;
import com.fasterxml.jackson.databind.node.TextNode; import com.fasterxml.jackson.databind.node.TextNode;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource; import org.junit.jupiter.params.provider.ValueSource;
import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.JacksonUtil;

13
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/TbGetTelemetryNodeTest.java

@ -18,6 +18,7 @@ package org.thingsboard.rule.engine.metadata;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.rule.engine.api.TbContext; import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.api.TbNodeConfiguration; import org.thingsboard.rule.engine.api.TbNodeConfiguration;
@ -70,14 +71,18 @@ public class TbGetTelemetryNodeTest {
} }
} }
@Test(expected = IllegalArgumentException.class) @Test
public void givenAggregationWhiteSpace_whenParseAggregation_thenException() { public void givenAggregationWhiteSpace_whenParseAggregation_thenException() {
node.parseAggregationConfig(" "); Assertions.assertThrows(IllegalArgumentException.class, () -> {
node.parseAggregationConfig(" ");
});
} }
@Test(expected = IllegalArgumentException.class) @Test
public void givenAggregationIncorrect_whenParseAggregation_thenException() { public void givenAggregationIncorrect_whenParseAggregation_thenException() {
node.parseAggregationConfig("TOP"); Assertions.assertThrows(IllegalArgumentException.class, () -> {
node.parseAggregationConfig("TOP");
});
} }
} }

Loading…
Cancel
Save