From b658b4956a4769de13172981aeb64f5bf1cee332 Mon Sep 17 00:00:00 2001 From: nickAS21 Date: Tue, 28 Jun 2022 22:18:58 +0300 Subject: [PATCH] refactoring: tests Alarm, Customer add .andExpect( --- .../controller/AbstractNotifyEntityTest.java | 2 ++ .../controller/BaseAlarmControllerTest.java | 36 ++++++++++++++----- .../BaseCustomerControllerTest.java | 34 +++++++++++++----- 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/AbstractNotifyEntityTest.java b/application/src/test/java/org/thingsboard/server/controller/AbstractNotifyEntityTest.java index b99d2dadf3..197d1dceeb 100644 --- a/application/src/test/java/org/thingsboard/server/controller/AbstractNotifyEntityTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/AbstractNotifyEntityTest.java @@ -57,6 +57,8 @@ public abstract class AbstractNotifyEntityTest extends AbstractWebTest { @SpyBean protected GatewayNotificationsService gatewayNotificationsService; + protected final String msgErrorPermission = "You don't have permission to perform this operation!"; + protected void testNotifyEntityAllOneTime(HasName entity, EntityId entityId, EntityId originatorId, TenantId tenantId, CustomerId customerId, UserId userId, String userName, ActionType actionType, Object... additionalInfo) { diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseAlarmControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseAlarmControllerTest.java index 95c90dea30..9eab1e56b3 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseAlarmControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseAlarmControllerTest.java @@ -36,6 +36,7 @@ import org.thingsboard.server.common.data.page.PageData; import java.util.LinkedList; import java.util.List; +import static org.hamcrest.Matchers.containsString; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @Slf4j @@ -132,7 +133,9 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest { Mockito.reset(tbClusterService, auditLogService); - doPost("/api/alarm", alarm).andExpect(status().isForbidden()); + doPost("/api/alarm", alarm) + .andExpect(status().isForbidden()) + .andExpect(statusReason(containsString(msgErrorPermission))); testNotifyEntityNever(alarm.getId(), alarm); } @@ -147,7 +150,9 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest { Mockito.reset(tbClusterService, auditLogService); - doPost("/api/alarm", alarm).andExpect(status().isForbidden()); + doPost("/api/alarm", alarm) + .andExpect(status().isForbidden()) + .andExpect(statusReason(containsString(msgErrorPermission))); testNotifyEntityNever(alarm.getId(), alarm); } @@ -187,7 +192,9 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest { Mockito.reset(tbClusterService, auditLogService); - doDelete("/api/alarm/" + alarm.getId()).andExpect(status().isForbidden()); + doDelete("/api/alarm/" + alarm.getId()) + .andExpect(status().isForbidden()) + .andExpect(statusReason(containsString(msgErrorPermission))); testNotifyEntityNever(alarm.getId(), alarm); } @@ -201,7 +208,9 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest { Mockito.reset(tbClusterService, auditLogService); - doDelete("/api/alarm/" + alarm.getId()).andExpect(status().isForbidden()); + doDelete("/api/alarm/" + alarm.getId()) + .andExpect(status().isForbidden()) + .andExpect(statusReason(containsString(msgErrorPermission))); testNotifyEntityNever(alarm.getId(), alarm); } @@ -265,7 +274,9 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest { Mockito.reset(tbClusterService, auditLogService); - doPost("/api/alarm/" + alarm.getId() + "/clear").andExpect(status().isForbidden()); + doPost("/api/alarm/" + alarm.getId() + "/clear") + .andExpect(status().isForbidden()) + .andExpect(statusReason(containsString(msgErrorPermission))); testNotifyEntityNever(alarm.getId(), alarm); } @@ -279,7 +290,9 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest { Mockito.reset(tbClusterService, auditLogService); - doPost("/api/alarm/" + alarm.getId() + "/clear").andExpect(status().isForbidden()); + doPost("/api/alarm/" + alarm.getId() + "/clear") + .andExpect(status().isForbidden()) + .andExpect(statusReason(containsString(msgErrorPermission))); testNotifyEntityNever(alarm.getId(), alarm); } @@ -293,7 +306,9 @@ 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))); testNotifyEntityNever(alarm.getId(), alarm); } @@ -307,7 +322,9 @@ 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))); } @Test @@ -355,7 +372,8 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest { loginDifferentCustomer(); doGet("/api/alarm/" + EntityType.DEVICE + "/" + customerDevice.getUuidId() + "?page=0&pageSize=" + size) - .andExpect(status().isForbidden()); + .andExpect(status().isForbidden()) + .andExpect(statusReason(containsString(msgErrorPermission))); } @Test diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseCustomerControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseCustomerControllerTest.java index bbba51bd60..1b1d269c79 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseCustomerControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseCustomerControllerTest.java @@ -127,7 +127,9 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest Mockito.reset(tbClusterService, auditLogService); String msgError = "length of title must be equal or less than 255"; - doPost("/api/customer", customer).andExpect(statusReason(containsString(msgError))); + doPost("/api/customer", customer) + .andExpect(status().isBadRequest()) + .andExpect(statusReason(containsString(msgError))); customer.setTenantId(savedTenant.getId()); testNotifyEntityEqualsOneTimeError(customer,savedTenant.getId(), @@ -137,7 +139,9 @@ 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"; - doPost("/api/customer", customer).andExpect(statusReason(containsString(msgError))); + doPost("/api/customer", customer) + .andExpect(status().isBadRequest()) + .andExpect(statusReason(containsString(msgError))); testNotifyEntityEqualsOneTimeError(customer,savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); @@ -146,7 +150,9 @@ 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"; - doPost("/api/customer", customer).andExpect(statusReason(containsString(msgError))); + doPost("/api/customer", customer) + .andExpect(status().isBadRequest()) + .andExpect(statusReason(containsString(msgError))); testNotifyEntityEqualsOneTimeError(customer,savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); @@ -155,7 +161,9 @@ 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"; - doPost("/api/customer", customer).andExpect(statusReason(containsString(msgError))); + doPost("/api/customer", customer) + .andExpect(status().isBadRequest()) + .andExpect(statusReason(containsString(msgError))); testNotifyEntityEqualsOneTimeError(customer,savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); @@ -164,7 +172,9 @@ 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"; - doPost("/api/customer", customer).andExpect(statusReason(containsString(msgError))); + doPost("/api/customer", customer) + .andExpect(status().isBadRequest()) + .andExpect(statusReason(containsString(msgError))); testNotifyEntityEqualsOneTimeError(customer,savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); @@ -173,7 +183,9 @@ 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"; - doPost("/api/customer", customer).andExpect(statusReason(containsString(msgError))); + doPost("/api/customer", customer) + .andExpect(status().isBadRequest()) + .andExpect(statusReason(containsString(msgError))); testNotifyEntityEqualsOneTimeError(customer,savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); @@ -195,7 +207,8 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest testNotifyEntityNever(savedCustomer.getId(), savedCustomer); doDelete("/api/customer/" + savedCustomer.getId().getId().toString()) - .andExpect(status().isForbidden()); + .andExpect(status().isForbidden()) + .andExpect(statusReason(containsString(msgErrorPermission))); testNotifyEntityNever(savedCustomer.getId(), savedCustomer); @@ -241,8 +254,11 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest savedCustomer.getId(), savedCustomer.getTenantId(), savedCustomer.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.DELETED, savedCustomer.getId().getId().toString()); - doGet("/api/customer/" + savedCustomer.getId().getId().toString()) - .andExpect(status().isNotFound()); + 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))); } @Test