junit5: BaseTenantServiceTest annotation exceptions refactoring, testSaveTenantWithIsolatedProfileInMonolithSetup issue

This commit is contained in:
Oleksandra Matviienko 2023-03-06 13:08:49 +01:00
parent 99b60dff4e
commit 25e046dc79
2 changed files with 33 additions and 10 deletions

View File

@ -294,7 +294,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);

View File

@ -15,8 +15,10 @@
*/ */
package org.thingsboard.server.dao.service; package org.thingsboard.server.dao.service;
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.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;
@ -54,6 +56,7 @@ 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.DefaultTenantProfileConfiguration;
import org.thingsboard.server.common.data.tenant.profile.TenantProfileData; import org.thingsboard.server.common.data.tenant.profile.TenantProfileData;
import org.thingsboard.server.common.data.tenant.profile.TenantProfileQueueConfiguration;
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;
@ -81,6 +84,19 @@ public abstract class BaseTenantServiceTest extends AbstractServiceTest {
@Autowired @Autowired
protected TbTransactionalCache<TenantId, Boolean> existsTenantCache; protected TbTransactionalCache<TenantId, Boolean> existsTenantCache;
Tenant savedTenant;
TenantProfile savedTenantProfile;
@After
public void tearDown() throws Exception {
if (savedTenant != null) {
tenantService.deleteTenant(savedTenant.getId());
}
if (savedTenantProfile != null) {
tenantProfileService.deleteTenantProfile(TenantId.SYS_TENANT_ID, savedTenantProfile.getId());
}
}
@Test @Test
public void testSaveTenant() { public void testSaveTenant() {
Tenant tenant = new Tenant(); Tenant tenant = new Tenant();
@ -121,18 +137,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,7 +321,7 @@ public abstract class BaseTenantServiceTest extends AbstractServiceTest {
} }
@Test(expected = DataValidationException.class) @Test
public void testSaveTenantWithIsolatedProfileInMonolithSetup() { public void testSaveTenantWithIsolatedProfileInMonolithSetup() {
TenantProfile tenantProfile = new TenantProfile(); TenantProfile tenantProfile = new TenantProfile();
tenantProfile.setName("Isolated Tenant Profile"); tenantProfile.setName("Isolated Tenant Profile");
@ -310,12 +330,15 @@ public abstract class BaseTenantServiceTest extends AbstractServiceTest {
tenantProfile.setProfileData(profileData); tenantProfile.setProfileData(profileData);
tenantProfile.setDefault(false); tenantProfile.setDefault(false);
tenantProfile.setIsolatedTbRuleEngine(true); tenantProfile.setIsolatedTbRuleEngine(true);
TenantProfile isolatedTenantProfile = tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile); BaseTenantProfileServiceTest.addMainQueueConfig(tenantProfile);
this.savedTenantProfile = tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile);
Tenant tenant = new Tenant(); Tenant tenant = new Tenant();
tenant.setTitle("Tenant"); tenant.setTitle("Tenant with isolated profile");
tenant.setTenantProfileId(isolatedTenantProfile.getId()); tenant.setTenantProfileId(savedTenantProfile.getId());
tenantService.saveTenant(tenant); Assertions.assertThrows(DataValidationException.class, () -> {
this.savedTenant = tenantService.saveTenant(tenant);
});
} }
@Test @Test