refactoring: tests magError from function

This commit is contained in:
nickAS21 2022-06-29 15:02:43 +03:00
parent 79483a601e
commit fb0a38fe16
5 changed files with 33 additions and 29 deletions

View File

@ -59,6 +59,8 @@ public abstract class AbstractNotifyEntityTest extends AbstractWebTest {
protected GatewayNotificationsService gatewayNotificationsService;
protected final String msgErrorPermission = "You don't have permission to perform this operation!";
protected final String msgErrorShouldBeSpecified = "should be specified";
protected void testNotifyEntityAllOneTime(HasName entity, EntityId entityId, EntityId originatorId,
TenantId tenantId, CustomerId customerId, UserId userId, String userName,
@ -490,4 +492,12 @@ public abstract class AbstractNotifyEntityTest extends AbstractWebTest {
.substring(entity.getClass().toString().lastIndexOf(".") + 1).toUpperCase(Locale.ENGLISH)),
ModelConstants.NULL_UUID);
}
protected String msgErrorFieldLength(String fieldName){
return "length of " + fieldName + " must be equal or less than 255";
}
protected String msgErrorNoFound(String entityClassName, String assetIdStr){
return entityClassName + " with id [" + assetIdStr + "] is not found";
}
}

View File

@ -322,8 +322,8 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest {
Mockito.reset(tbClusterService, auditLogService);
doPost("/api/alarm/" + alarm.getId() + "/ack").andExpect(status()
.isForbidden())
doPost("/api/alarm/" + alarm.getId() + "/ack")
.andExpect(status().isForbidden())
.andExpect(statusReason(containsString(msgErrorPermission)));
}

View File

@ -122,7 +122,7 @@ public abstract class BaseAssetControllerTest extends AbstractControllerTest {
Mockito.reset(tbClusterService, auditLogService);
String msgError = "length of name must be equal or less than 255";
String msgError = msgErrorFieldLength("name");
doPost("/api/asset", asset)
.andExpect(status().isBadRequest())
.andExpect(statusReason(containsString(msgError)));
@ -133,7 +133,7 @@ public abstract class BaseAssetControllerTest extends AbstractControllerTest {
asset.setName("Normal name");
asset.setType(RandomStringUtils.randomAlphabetic(300));
msgError = "length of type must be equal or less than 255";
msgError = msgErrorFieldLength("type");
doPost("/api/asset", asset)
.andExpect(status().isBadRequest())
.andExpect(statusReason(containsString(msgError)));
@ -144,7 +144,7 @@ public abstract class BaseAssetControllerTest extends AbstractControllerTest {
asset.setType("default");
asset.setLabel(RandomStringUtils.randomAlphabetic(300));
msgError = "length of label must be equal or less than 255";
msgError = msgErrorFieldLength("label");
doPost("/api/asset", asset)
.andExpect(status().isBadRequest())
.andExpect(statusReason(containsString(msgError)));
@ -245,10 +245,9 @@ public abstract class BaseAssetControllerTest extends AbstractControllerTest {
ActionType.DELETED, savedAsset.getId().getId().toString());
String assetIdStr = savedAsset.getId().getId().toString();
String msgError = "Asset with id [" + assetIdStr + "] is not found";
doGet("/api/asset/" + savedAsset.getId().getId().toString())
doGet("/api/asset/" + assetIdStr)
.andExpect(status().isNotFound())
.andExpect(statusReason(containsString(msgError)));
.andExpect(statusReason(containsString(msgErrorNoFound("Asset", assetIdStr))));
}
@Test
@ -288,10 +287,9 @@ public abstract class BaseAssetControllerTest extends AbstractControllerTest {
.andExpect(status().isOk());
String assetIdStr = savedAsset1.getId().getId().toString();
msgError = "Asset with id [" + assetIdStr + "] is not found";
doGet("/api/asset/" + assetIdStr)
.andExpect(status().isNotFound())
.andExpect(statusReason(containsString(msgError)));
.andExpect(statusReason(containsString(msgErrorNoFound("Asset", assetIdStr))));
}
@Test
@ -301,7 +299,7 @@ public abstract class BaseAssetControllerTest extends AbstractControllerTest {
Mockito.reset(tbClusterService, auditLogService);
String msgError = "Asset type should be specified";
String msgError = "Asset type " + msgErrorShouldBeSpecified;
doPost("/api/asset", asset)
.andExpect(status().isBadRequest())
.andExpect(statusReason(containsString(msgError)));
@ -317,7 +315,7 @@ public abstract class BaseAssetControllerTest extends AbstractControllerTest {
Mockito.reset(tbClusterService, auditLogService);
String msgError = "Asset name should be specified";
String msgError = "Asset name " + msgErrorShouldBeSpecified;
doPost("/api/asset", asset)
.andExpect(status().isBadRequest())
.andExpect(statusReason(containsString(msgError)));
@ -374,11 +372,10 @@ public abstract class BaseAssetControllerTest extends AbstractControllerTest {
Mockito.reset(tbClusterService, auditLogService);
String customerIdStr = Uuids.timeBased().toString();
String msgError = "Customer with id [" + customerIdStr + "] is not found";
doPost("/api/customer/" + customerIdStr
+ "/asset/" + savedAsset.getId().getId().toString())
.andExpect(status().isNotFound())
.andExpect(statusReason(containsString(msgError)));
.andExpect(statusReason(containsString(msgErrorNoFound("Customer", customerIdStr))));
testNotifyEntityNever(asset.getId(), asset);
}

View File

@ -126,7 +126,7 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest
Mockito.reset(tbClusterService, auditLogService);
String msgError = "length of title must be equal or less than 255";
String msgError = msgErrorFieldLength("title");
doPost("/api/customer", customer)
.andExpect(status().isBadRequest())
.andExpect(statusReason(containsString(msgError)));
@ -138,7 +138,7 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest
customer.setTitle("Normal title");
customer.setCity(RandomStringUtils.randomAlphabetic(300));
msgError = "length of city must be equal or less than 255";
msgError = msgErrorFieldLength("city");
doPost("/api/customer", customer)
.andExpect(status().isBadRequest())
.andExpect(statusReason(containsString(msgError)));
@ -149,7 +149,7 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest
customer.setCity("Normal city");
customer.setCountry(RandomStringUtils.randomAlphabetic(300));
msgError = "length of country must be equal or less than 255";
msgError = msgErrorFieldLength("country");
doPost("/api/customer", customer)
.andExpect(status().isBadRequest())
.andExpect(statusReason(containsString(msgError)));
@ -160,7 +160,7 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest
customer.setCountry("Ukraine");
customer.setPhone(RandomStringUtils.randomAlphabetic(300));
msgError = "length of phone must be equal or less than 255";
msgError = msgErrorFieldLength("phone");
doPost("/api/customer", customer)
.andExpect(status().isBadRequest())
.andExpect(statusReason(containsString(msgError)));
@ -171,7 +171,7 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest
customer.setPhone("+3892555554512");
customer.setState(RandomStringUtils.randomAlphabetic(300));
msgError = "length of state must be equal or less than 255";
msgError = msgErrorFieldLength("state");
doPost("/api/customer", customer)
.andExpect(status().isBadRequest())
.andExpect(statusReason(containsString(msgError)));
@ -182,7 +182,7 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest
customer.setState("Normal state");
customer.setZip(RandomStringUtils.randomAlphabetic(300));
msgError = "length of zip or postal code must be equal or less than 255";
msgError = msgErrorFieldLength("zip or postal code");
doPost("/api/customer", customer)
.andExpect(status().isBadRequest())
.andExpect(statusReason(containsString(msgError)));
@ -255,16 +255,15 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest
tenantAdmin.getEmail(), ActionType.DELETED, savedCustomer.getId().getId().toString());
String customerIdStr = savedCustomer.getId().getId().toString();
String msgError = "Customer with id [" + customerIdStr + "] is not found";
doGet("/api/customer/" + customerIdStr)
.andExpect(status().isNotFound())
.andExpect(statusReason(containsString(msgError)));
.andExpect(statusReason(containsString(msgErrorNoFound("Customer", customerIdStr))));
}
@Test
public void testSaveCustomerWithEmptyTitle() throws Exception {
Customer customer = new Customer();
String msgError = "Customer title should be specified";
String msgError = "Customer title " + msgErrorShouldBeSpecified;
Mockito.reset(tbClusterService, auditLogService);

View File

@ -112,7 +112,7 @@ public abstract class BaseDashboardControllerTest extends AbstractControllerTest
public void testSaveDashboardInfoWithViolationOfValidation() throws Exception {
Dashboard dashboard = new Dashboard();
dashboard.setTitle(RandomStringUtils.randomAlphabetic(300));
String msgError = "length of title must be equal or less than 255";
String msgError = msgErrorFieldLength("title");
Mockito.reset(tbClusterService, auditLogService);
@ -168,16 +168,15 @@ public abstract class BaseDashboardControllerTest extends AbstractControllerTest
savedDashboard.getId().getId().toString());
String dashboardIdStr = savedDashboard.getId().getId().toString();
String msgError = "Dashboard with id [" + dashboardIdStr + "] is not found";
doGet("/api/dashboard/" + savedDashboard.getId().getId().toString())
.andExpect(status().isNotFound())
.andExpect(statusReason(containsString(msgError)));
.andExpect(statusReason(containsString(msgErrorNoFound("Dashboard", dashboardIdStr))));
}
@Test
public void testSaveDashboardWithEmptyTitle() throws Exception {
Dashboard dashboard = new Dashboard();
String msgError = "Dashboard title should be specified";
String msgError = "Dashboard title " + msgErrorShouldBeSpecified;;
Mockito.reset(tbClusterService, auditLogService);
@ -236,11 +235,10 @@ public abstract class BaseDashboardControllerTest extends AbstractControllerTest
Dashboard savedDashboard = doPost("/api/dashboard", dashboard, Dashboard.class);
String customerIdStr = Uuids.timeBased().toString();
String msgError = "Customer with id [" + customerIdStr + "] is not found";
doPost("/api/customer/" + customerIdStr
+ "/dashboard/" + savedDashboard.getId().getId().toString())
.andExpect(status().isNotFound())
.andExpect(statusReason(containsString(msgError)));
.andExpect(statusReason(containsString(msgErrorNoFound("Customer", customerIdStr))));
Mockito.reset(tbClusterService, auditLogService);
testNotifyEntityNever(savedDashboard.getId(), savedDashboard);