Browse Source

junit5: BaseAssetServiceTest annotation exceptions refactoring

pull/8174/head
Oleksandra Matviienko 4 years ago
parent
commit
71c187542e
  1. 41
      dao/src/test/java/org/thingsboard/server/dao/service/BaseAssetServiceTest.java

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());
} }
} }

Loading…
Cancel
Save