|
|
@ -25,14 +25,17 @@ 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.mockito.Mockito; |
|
|
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.Tenant; |
|
|
import org.thingsboard.server.common.data.Tenant; |
|
|
import org.thingsboard.server.common.data.User; |
|
|
import org.thingsboard.server.common.data.User; |
|
|
|
|
|
import org.thingsboard.server.common.data.audit.ActionType; |
|
|
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; |
|
|
import org.thingsboard.server.common.data.page.PageLink; |
|
|
import org.thingsboard.server.common.data.page.PageLink; |
|
|
import org.thingsboard.server.common.data.security.Authority; |
|
|
import org.thingsboard.server.common.data.security.Authority; |
|
|
|
|
|
import org.thingsboard.server.dao.exception.DataValidationException; |
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
import java.util.ArrayList; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
@ -86,71 +89,164 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest |
|
|
public void testSaveCustomer() throws Exception { |
|
|
public void testSaveCustomer() throws Exception { |
|
|
Customer customer = new Customer(); |
|
|
Customer customer = new Customer(); |
|
|
customer.setTitle("My customer"); |
|
|
customer.setTitle("My customer"); |
|
|
|
|
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService); |
|
|
|
|
|
|
|
|
Customer savedCustomer = doPost("/api/customer", customer, Customer.class); |
|
|
Customer savedCustomer = doPost("/api/customer", customer, Customer.class); |
|
|
Assert.assertNotNull(savedCustomer); |
|
|
Assert.assertNotNull(savedCustomer); |
|
|
Assert.assertNotNull(savedCustomer.getId()); |
|
|
Assert.assertNotNull(savedCustomer.getId()); |
|
|
Assert.assertTrue(savedCustomer.getCreatedTime() > 0); |
|
|
Assert.assertTrue(savedCustomer.getCreatedTime() > 0); |
|
|
Assert.assertEquals(customer.getTitle(), savedCustomer.getTitle()); |
|
|
Assert.assertEquals(customer.getTitle(), savedCustomer.getTitle()); |
|
|
|
|
|
|
|
|
|
|
|
testNotifyEntityOneMsgToEdgeServiceNever(savedCustomer, savedCustomer.getId(), savedCustomer.getId(), |
|
|
|
|
|
savedCustomer.getTenantId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), |
|
|
|
|
|
tenantAdmin.getEmail(), ActionType.ADDED); |
|
|
|
|
|
|
|
|
savedCustomer.setTitle("My new customer"); |
|
|
savedCustomer.setTitle("My new customer"); |
|
|
|
|
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService); |
|
|
|
|
|
|
|
|
doPost("/api/customer", savedCustomer, Customer.class); |
|
|
doPost("/api/customer", savedCustomer, Customer.class); |
|
|
|
|
|
|
|
|
|
|
|
testNotifyEntityOne(savedCustomer, savedCustomer.getId(), savedCustomer.getId(), |
|
|
|
|
|
savedCustomer.getTenantId(), savedCustomer.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.UPDATED); |
|
|
|
|
|
|
|
|
Customer foundCustomer = doGet("/api/customer/" + savedCustomer.getId().getId().toString(), Customer.class); |
|
|
Customer foundCustomer = doGet("/api/customer/" + savedCustomer.getId().getId().toString(), Customer.class); |
|
|
Assert.assertEquals(foundCustomer.getTitle(), savedCustomer.getTitle()); |
|
|
Assert.assertEquals(foundCustomer.getTitle(), savedCustomer.getTitle()); |
|
|
|
|
|
|
|
|
doDelete("/api/customer/" + savedCustomer.getId().getId().toString()) |
|
|
doDelete("/api/customer/" + savedCustomer.getId().getId().toString()) |
|
|
.andExpect(status().isOk()); |
|
|
.andExpect(status().isOk()); |
|
|
|
|
|
|
|
|
|
|
|
testNotifyEntityBroadcastEntityStateChangeEventOneMsgToEdgeServiceNever(savedCustomer, savedCustomer.getId(), savedCustomer.getId(), |
|
|
|
|
|
savedCustomer.getTenantId(), savedCustomer.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
|
|
ActionType.DELETED, savedCustomer.getId().getId().toString()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
public void testSaveCustomerWithViolationOfValidation() throws Exception { |
|
|
public void testSaveCustomerWithViolationOfValidation() throws Exception { |
|
|
Customer customer = new Customer(); |
|
|
Customer customer = new Customer(); |
|
|
|
|
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService); |
|
|
|
|
|
|
|
|
|
|
|
doPost("/api/customer", customer) |
|
|
|
|
|
.andExpect(status().isBadRequest()) |
|
|
|
|
|
.andExpect(statusReason(containsString("Customer title should be specified"))); |
|
|
|
|
|
|
|
|
|
|
|
testNotifyEntityError(customer, savedTenant.getId(), |
|
|
|
|
|
tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException("Test: empty title")); |
|
|
|
|
|
|
|
|
customer.setTitle(RandomStringUtils.randomAlphabetic(300)); |
|
|
customer.setTitle(RandomStringUtils.randomAlphabetic(300)); |
|
|
|
|
|
|
|
|
doPost("/api/customer", customer).andExpect(statusReason(containsString("length of title must be equal or less than 255"))); |
|
|
doPost("/api/customer", customer).andExpect(statusReason(containsString("length of title must be equal or less than 255"))); |
|
|
|
|
|
|
|
|
|
|
|
testNotifyEntityError(customer, savedTenant.getId(), |
|
|
|
|
|
tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException("Test: bad title")); |
|
|
|
|
|
|
|
|
customer.setTitle("Normal title"); |
|
|
customer.setTitle("Normal title"); |
|
|
|
|
|
customer.setEmail("invalid@mail"); |
|
|
|
|
|
doPost("/api/customer", customer) |
|
|
|
|
|
.andExpect(status().isBadRequest()) |
|
|
|
|
|
.andExpect(statusReason(containsString("Invalid email address format 'invalid@mail'"))); |
|
|
|
|
|
|
|
|
|
|
|
testNotifyEntityError(customer, savedTenant.getId(), |
|
|
|
|
|
tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException("Test: invalid email")); |
|
|
|
|
|
|
|
|
|
|
|
customer.setEmail("normal@mail.com.ua"); |
|
|
|
|
|
|
|
|
customer.setCity(RandomStringUtils.randomAlphabetic(300)); |
|
|
customer.setCity(RandomStringUtils.randomAlphabetic(300)); |
|
|
doPost("/api/customer", customer).andExpect(statusReason(containsString("length of city must be equal or less than 255"))); |
|
|
doPost("/api/customer", customer).andExpect(statusReason(containsString("length of city must be equal or less than 255"))); |
|
|
|
|
|
|
|
|
|
|
|
testNotifyEntityError(customer, savedTenant.getId(), |
|
|
|
|
|
tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException("Test: bad City")); |
|
|
|
|
|
|
|
|
customer.setCity("Normal city"); |
|
|
customer.setCity("Normal city"); |
|
|
customer.setCountry(RandomStringUtils.randomAlphabetic(300)); |
|
|
customer.setCountry(RandomStringUtils.randomAlphabetic(300)); |
|
|
doPost("/api/customer", customer).andExpect(statusReason(containsString("length of country must be equal or less than 255"))); |
|
|
doPost("/api/customer", customer).andExpect(statusReason(containsString("length of country must be equal or less than 255"))); |
|
|
|
|
|
|
|
|
|
|
|
testNotifyEntityError(customer, savedTenant.getId(), |
|
|
|
|
|
tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException("Test: bad Country")); |
|
|
|
|
|
|
|
|
customer.setCountry("Ukraine"); |
|
|
customer.setCountry("Ukraine"); |
|
|
customer.setPhone(RandomStringUtils.randomAlphabetic(300)); |
|
|
customer.setPhone(RandomStringUtils.randomAlphabetic(300)); |
|
|
doPost("/api/customer", customer).andExpect(statusReason(containsString("length of phone must be equal or less than 255"))); |
|
|
doPost("/api/customer", customer).andExpect(statusReason(containsString("length of phone must be equal or less than 255"))); |
|
|
|
|
|
|
|
|
|
|
|
testNotifyEntityError(customer, savedTenant.getId(), |
|
|
|
|
|
tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException("Test: bad Phone")); |
|
|
|
|
|
|
|
|
customer.setPhone("+3892555554512"); |
|
|
customer.setPhone("+3892555554512"); |
|
|
customer.setState(RandomStringUtils.randomAlphabetic(300)); |
|
|
customer.setState(RandomStringUtils.randomAlphabetic(300)); |
|
|
doPost("/api/customer", customer).andExpect(statusReason(containsString("length of state must be equal or less than 255"))); |
|
|
doPost("/api/customer", customer).andExpect(statusReason(containsString("length of state must be equal or less than 255"))); |
|
|
|
|
|
|
|
|
|
|
|
testNotifyEntityError(customer, savedTenant.getId(), |
|
|
|
|
|
tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException("Test: bad state")); |
|
|
|
|
|
|
|
|
customer.setState("Normal state"); |
|
|
customer.setState("Normal state"); |
|
|
customer.setZip(RandomStringUtils.randomAlphabetic(300)); |
|
|
customer.setZip(RandomStringUtils.randomAlphabetic(300)); |
|
|
doPost("/api/customer", customer).andExpect(statusReason(containsString("length of zip or postal code must be equal or less than 255"))); |
|
|
doPost("/api/customer", customer).andExpect(statusReason(containsString("length of zip or postal code must be equal or less than 255"))); |
|
|
|
|
|
|
|
|
|
|
|
testNotifyEntityError(customer, savedTenant.getId(), |
|
|
|
|
|
tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException("Test: bad Zip")); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
public void testUpdateCustomerFromDifferentTenant() throws Exception { |
|
|
public void testUpdateCustomerFromDifferentTenant() throws Exception { |
|
|
Customer customer = new Customer(); |
|
|
Customer customer = new Customer(); |
|
|
customer.setTitle("My customer"); |
|
|
customer.setTitle("My customer"); |
|
|
|
|
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService); |
|
|
|
|
|
|
|
|
Customer savedCustomer = doPost("/api/customer", customer, Customer.class); |
|
|
Customer savedCustomer = doPost("/api/customer", customer, Customer.class); |
|
|
|
|
|
|
|
|
|
|
|
testNotifyEntityOneMsgToEdgeServiceNever(savedCustomer, savedCustomer.getId(), savedCustomer.getId(), |
|
|
|
|
|
savedCustomer.getTenantId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED); |
|
|
|
|
|
|
|
|
doPost("/api/customer", savedCustomer, Customer.class); |
|
|
doPost("/api/customer", savedCustomer, Customer.class); |
|
|
|
|
|
|
|
|
loginDifferentTenant(); |
|
|
loginDifferentTenant(); |
|
|
|
|
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService); |
|
|
|
|
|
|
|
|
doPost("/api/customer", savedCustomer, Customer.class, status().isForbidden()); |
|
|
doPost("/api/customer", savedCustomer, Customer.class, status().isForbidden()); |
|
|
|
|
|
|
|
|
|
|
|
testNotifyEntityNever(savedCustomer.getId(), savedCustomer); |
|
|
|
|
|
|
|
|
deleteDifferentTenant(); |
|
|
deleteDifferentTenant(); |
|
|
|
|
|
|
|
|
login(tenantAdmin.getName(), "testPassword1"); |
|
|
login(tenantAdmin.getName(), "testPassword1"); |
|
|
|
|
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService); |
|
|
|
|
|
|
|
|
doDelete("/api/customer/" + savedCustomer.getId().getId().toString()) |
|
|
doDelete("/api/customer/" + savedCustomer.getId().getId().toString()) |
|
|
.andExpect(status().isOk()); |
|
|
.andExpect(status().isOk()); |
|
|
|
|
|
|
|
|
|
|
|
testNotifyEntityDeleteOneMsgToEdgeServiceNever(savedCustomer, savedCustomer.getId(), savedCustomer.getId(), |
|
|
|
|
|
savedCustomer.getTenantId(), savedCustomer.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
|
|
ActionType.DELETED, savedCustomer.getId().getId().toString()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
public void testFindCustomerById() throws Exception { |
|
|
public void testFindCustomerById() throws Exception { |
|
|
Customer customer = new Customer(); |
|
|
Customer customer = new Customer(); |
|
|
customer.setTitle("My customer"); |
|
|
customer.setTitle("My customer"); |
|
|
|
|
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService); |
|
|
|
|
|
|
|
|
Customer savedCustomer = doPost("/api/customer", customer, Customer.class); |
|
|
Customer savedCustomer = doPost("/api/customer", customer, Customer.class); |
|
|
|
|
|
|
|
|
|
|
|
testNotifyEntityOneMsgToEdgeServiceNever(savedCustomer, savedCustomer.getId(), savedCustomer.getId(), |
|
|
|
|
|
savedCustomer.getTenantId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), |
|
|
|
|
|
tenantAdmin.getEmail(), ActionType.ADDED); |
|
|
|
|
|
|
|
|
Customer foundCustomer = doGet("/api/customer/" + savedCustomer.getId().getId().toString(), Customer.class); |
|
|
Customer foundCustomer = doGet("/api/customer/" + savedCustomer.getId().getId().toString(), Customer.class); |
|
|
Assert.assertNotNull(foundCustomer); |
|
|
Assert.assertNotNull(foundCustomer); |
|
|
Assert.assertEquals(savedCustomer, foundCustomer); |
|
|
Assert.assertEquals(savedCustomer, foundCustomer); |
|
|
|
|
|
|
|
|
doDelete("/api/customer/" + savedCustomer.getId().getId().toString()) |
|
|
doDelete("/api/customer/" + savedCustomer.getId().getId().toString()) |
|
|
.andExpect(status().isOk()); |
|
|
.andExpect(status().isOk()); |
|
|
|
|
|
|
|
|
|
|
|
testNotifyEntityBroadcastEntityStateChangeEventOneMsgToEdgeServiceNever(savedCustomer, savedCustomer.getId(), savedCustomer.getId(), |
|
|
|
|
|
savedCustomer.getTenantId(), savedCustomer.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
|
|
ActionType.DELETED, savedCustomer.getId().getId().toString()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
@ -159,36 +255,19 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest |
|
|
customer.setTitle("My customer"); |
|
|
customer.setTitle("My customer"); |
|
|
Customer savedCustomer = doPost("/api/customer", customer, Customer.class); |
|
|
Customer savedCustomer = doPost("/api/customer", customer, Customer.class); |
|
|
|
|
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService); |
|
|
|
|
|
|
|
|
doDelete("/api/customer/" + savedCustomer.getId().getId().toString()) |
|
|
doDelete("/api/customer/" + savedCustomer.getId().getId().toString()) |
|
|
.andExpect(status().isOk()); |
|
|
.andExpect(status().isOk()); |
|
|
|
|
|
|
|
|
|
|
|
testNotifyEntityBroadcastEntityStateChangeEventOneMsgToEdgeServiceNever(savedCustomer, savedCustomer.getId(), savedCustomer.getId(), |
|
|
|
|
|
savedCustomer.getTenantId(), savedCustomer.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
|
|
ActionType.DELETED, savedCustomer.getId().getId().toString()); |
|
|
|
|
|
|
|
|
doGet("/api/customer/" + savedCustomer.getId().getId().toString()) |
|
|
doGet("/api/customer/" + savedCustomer.getId().getId().toString()) |
|
|
.andExpect(status().isNotFound()); |
|
|
.andExpect(status().isNotFound()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
public void testSaveCustomerWithEmptyTitle() throws Exception { |
|
|
|
|
|
Customer customer = new Customer(); |
|
|
|
|
|
doPost("/api/customer", customer) |
|
|
|
|
|
.andExpect(status().isBadRequest()) |
|
|
|
|
|
.andExpect(statusReason(containsString("Customer title should be specified"))); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
public void testSaveCustomerWithInvalidEmail() throws Exception { |
|
|
|
|
|
Customer customer = new Customer(); |
|
|
|
|
|
customer.setTitle("My customer"); |
|
|
|
|
|
customer.setEmail("invalid@mail"); |
|
|
|
|
|
doPost("/api/customer", customer) |
|
|
|
|
|
.andExpect(status().isBadRequest()) |
|
|
|
|
|
.andExpect(statusReason(containsString("Invalid email address format 'invalid@mail'"))); |
|
|
|
|
|
|
|
|
|
|
|
// loginSysAdmin();
|
|
|
|
|
|
//
|
|
|
|
|
|
// doDelete("/api/tenant/"+savedTenant.getId().getId().toString())
|
|
|
|
|
|
// .andExpect(status().isOk());
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
public void testFindCustomers() throws Exception { |
|
|
public void testFindCustomers() throws Exception { |
|
|
TenantId tenantId = savedTenant.getId(); |
|
|
TenantId tenantId = savedTenant.getId(); |
|
|
|