|
|
|
@ -15,16 +15,18 @@ |
|
|
|
*/ |
|
|
|
package org.thingsboard.server.controller; |
|
|
|
|
|
|
|
import static org.hamcrest.Matchers.containsString; |
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.type.TypeReference; |
|
|
|
import com.google.common.util.concurrent.Futures; |
|
|
|
import com.google.common.util.concurrent.ListenableFuture; |
|
|
|
import com.google.common.util.concurrent.ListeningExecutorService; |
|
|
|
import com.google.common.util.concurrent.MoreExecutors; |
|
|
|
import org.apache.commons.lang3.RandomStringUtils; |
|
|
|
import org.junit.After; |
|
|
|
import org.junit.Assert; |
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Test; |
|
|
|
import org.springframework.test.web.servlet.ResultActions; |
|
|
|
import org.thingsboard.common.util.ThingsBoardExecutors; |
|
|
|
import org.thingsboard.server.common.data.Customer; |
|
|
|
import org.thingsboard.server.common.data.Tenant; |
|
|
|
import org.thingsboard.server.common.data.User; |
|
|
|
@ -32,20 +34,28 @@ import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
import org.thingsboard.server.common.data.page.PageData; |
|
|
|
import org.thingsboard.server.common.data.page.PageLink; |
|
|
|
import org.thingsboard.server.common.data.security.Authority; |
|
|
|
import org.junit.Assert; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.type.TypeReference; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.hamcrest.Matchers.containsString; |
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
|
|
|
|
|
|
|
public abstract class BaseCustomerControllerTest extends AbstractControllerTest { |
|
|
|
static final int TIMEOUT = 30; |
|
|
|
static final TypeReference<PageData<Customer>> PAGE_DATA_CUSTOMER_TYPE_REFERENCE = new TypeReference<>() {}; |
|
|
|
|
|
|
|
private IdComparator<Customer> idComparator = new IdComparator<>(); |
|
|
|
ListeningExecutorService executor; |
|
|
|
|
|
|
|
private Tenant savedTenant; |
|
|
|
private User tenantAdmin; |
|
|
|
|
|
|
|
@Before |
|
|
|
public void beforeTest() throws Exception { |
|
|
|
executor = MoreExecutors.listeningDecorator(ThingsBoardExecutors.newWorkStealingPool(8, getClass())); |
|
|
|
|
|
|
|
loginSysAdmin(); |
|
|
|
|
|
|
|
Tenant tenant = new Tenant(); |
|
|
|
@ -65,6 +75,8 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest |
|
|
|
|
|
|
|
@After |
|
|
|
public void afterTest() throws Exception { |
|
|
|
executor.shutdownNow(); |
|
|
|
|
|
|
|
loginSysAdmin(); |
|
|
|
|
|
|
|
doDelete("/api/tenant/" + savedTenant.getId().getId().toString()) |
|
|
|
@ -83,11 +95,11 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest |
|
|
|
savedCustomer.setTitle("My new customer"); |
|
|
|
doPost("/api/customer", savedCustomer, Customer.class); |
|
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
|
doDelete("/api/customer/"+savedCustomer.getId().getId().toString()) |
|
|
|
.andExpect(status().isOk()); |
|
|
|
doDelete("/api/customer/" + savedCustomer.getId().getId().toString()) |
|
|
|
.andExpect(status().isOk()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
@ -182,29 +194,28 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest |
|
|
|
public void testFindCustomers() throws Exception { |
|
|
|
TenantId tenantId = savedTenant.getId(); |
|
|
|
|
|
|
|
List<Customer> customers = new ArrayList<>(); |
|
|
|
List<ListenableFuture<Customer>> futures = new ArrayList<>(135); |
|
|
|
for (int i = 0; i < 135; i++) { |
|
|
|
Customer customer = new Customer(); |
|
|
|
customer.setTenantId(tenantId); |
|
|
|
customer.setTitle("Customer" + i); |
|
|
|
customers.add(doPost("/api/customer", customer, Customer.class)); |
|
|
|
futures.add(executor.submit(() -> |
|
|
|
doPost("/api/customer", customer, Customer.class))); |
|
|
|
} |
|
|
|
List<Customer> customers = Futures.allAsList(futures).get(TIMEOUT, TimeUnit.SECONDS); |
|
|
|
|
|
|
|
List<Customer> loadedCustomers = new ArrayList<>(); |
|
|
|
List<Customer> loadedCustomers = new ArrayList<>(135); |
|
|
|
PageLink pageLink = new PageLink(23); |
|
|
|
PageData<Customer> pageData = null; |
|
|
|
do { |
|
|
|
pageData = doGetTypedWithPageLink("/api/customers?", new TypeReference<PageData<Customer>>(){}, pageLink); |
|
|
|
pageData = doGetTypedWithPageLink("/api/customers?", PAGE_DATA_CUSTOMER_TYPE_REFERENCE, pageLink); |
|
|
|
loadedCustomers.addAll(pageData.getData()); |
|
|
|
if (pageData.hasNext()) { |
|
|
|
pageLink = pageLink.nextPageLink(); |
|
|
|
} |
|
|
|
} while (pageData.hasNext()); |
|
|
|
|
|
|
|
Collections.sort(customers, idComparator); |
|
|
|
Collections.sort(loadedCustomers, idComparator); |
|
|
|
|
|
|
|
Assert.assertEquals(customers, loadedCustomers); |
|
|
|
assertThat(customers).containsExactlyInAnyOrderElementsOf(loadedCustomers); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
@ -212,7 +223,7 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest |
|
|
|
TenantId tenantId = savedTenant.getId(); |
|
|
|
|
|
|
|
String title1 = "Customer title 1"; |
|
|
|
List<Customer> customersTitle1 = new ArrayList<>(); |
|
|
|
List<ListenableFuture<Customer>> futures = new ArrayList<>(143); |
|
|
|
for (int i = 0; i < 143; i++) { |
|
|
|
Customer customer = new Customer(); |
|
|
|
customer.setTenantId(tenantId); |
|
|
|
@ -220,10 +231,13 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest |
|
|
|
String title = title1 + suffix; |
|
|
|
title = i % 2 == 0 ? title.toLowerCase() : title.toUpperCase(); |
|
|
|
customer.setTitle(title); |
|
|
|
customersTitle1.add(doPost("/api/customer", customer, Customer.class)); |
|
|
|
futures.add(executor.submit(() -> |
|
|
|
doPost("/api/customer", customer, Customer.class))); |
|
|
|
} |
|
|
|
List<Customer> customersTitle1 = Futures.allAsList(futures).get(TIMEOUT, TimeUnit.SECONDS); |
|
|
|
|
|
|
|
String title2 = "Customer title 2"; |
|
|
|
List<Customer> customersTitle2 = new ArrayList<>(); |
|
|
|
futures = new ArrayList<>(175); |
|
|
|
for (int i = 0; i < 175; i++) { |
|
|
|
Customer customer = new Customer(); |
|
|
|
customer.setTenantId(tenantId); |
|
|
|
@ -231,57 +245,60 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest |
|
|
|
String title = title2 + suffix; |
|
|
|
title = i % 2 == 0 ? title.toLowerCase() : title.toUpperCase(); |
|
|
|
customer.setTitle(title); |
|
|
|
customersTitle2.add(doPost("/api/customer", customer, Customer.class)); |
|
|
|
futures.add(executor.submit(() -> |
|
|
|
doPost("/api/customer", customer, Customer.class))); |
|
|
|
} |
|
|
|
|
|
|
|
List<Customer> customersTitle2 = Futures.allAsList(futures).get(TIMEOUT, TimeUnit.SECONDS); |
|
|
|
|
|
|
|
List<Customer> loadedCustomersTitle1 = new ArrayList<>(); |
|
|
|
PageLink pageLink = new PageLink(15, 0, title1); |
|
|
|
PageData<Customer> pageData = null; |
|
|
|
do { |
|
|
|
pageData = doGetTypedWithPageLink("/api/customers?", new TypeReference<PageData<Customer>>(){}, pageLink); |
|
|
|
pageData = doGetTypedWithPageLink("/api/customers?", PAGE_DATA_CUSTOMER_TYPE_REFERENCE, pageLink); |
|
|
|
loadedCustomersTitle1.addAll(pageData.getData()); |
|
|
|
if (pageData.hasNext()) { |
|
|
|
pageLink = pageLink.nextPageLink(); |
|
|
|
} |
|
|
|
} while (pageData.hasNext()); |
|
|
|
|
|
|
|
Collections.sort(customersTitle1, idComparator); |
|
|
|
Collections.sort(loadedCustomersTitle1, idComparator); |
|
|
|
|
|
|
|
Assert.assertEquals(customersTitle1, loadedCustomersTitle1); |
|
|
|
assertThat(customersTitle1).as(title1).containsExactlyInAnyOrderElementsOf(loadedCustomersTitle1); |
|
|
|
|
|
|
|
List<Customer> loadedCustomersTitle2 = new ArrayList<>(); |
|
|
|
pageLink = new PageLink(4, 0, title2); |
|
|
|
do { |
|
|
|
pageData = doGetTypedWithPageLink("/api/customers?", new TypeReference<PageData<Customer>>(){}, pageLink); |
|
|
|
pageData = doGetTypedWithPageLink("/api/customers?", PAGE_DATA_CUSTOMER_TYPE_REFERENCE, pageLink); |
|
|
|
loadedCustomersTitle2.addAll(pageData.getData()); |
|
|
|
if (pageData.hasNext()) { |
|
|
|
pageLink = pageLink.nextPageLink(); |
|
|
|
} |
|
|
|
} while (pageData.hasNext()); |
|
|
|
|
|
|
|
Collections.sort(customersTitle2, idComparator); |
|
|
|
Collections.sort(loadedCustomersTitle2, idComparator); |
|
|
|
|
|
|
|
Assert.assertEquals(customersTitle2, loadedCustomersTitle2); |
|
|
|
assertThat(customersTitle2).as(title2).containsExactlyInAnyOrderElementsOf(loadedCustomersTitle2); |
|
|
|
|
|
|
|
List<ListenableFuture<ResultActions>> deleteFutures = new ArrayList<>(143); |
|
|
|
for (Customer customer : loadedCustomersTitle1) { |
|
|
|
doDelete("/api/customer/" + customer.getId().getId().toString()) |
|
|
|
.andExpect(status().isOk()); |
|
|
|
deleteFutures.add(executor.submit(() -> |
|
|
|
doDelete("/api/customer/" + customer.getId().getId().toString()) |
|
|
|
.andExpect(status().isOk()))); |
|
|
|
} |
|
|
|
Futures.allAsList(deleteFutures).get(TIMEOUT, TimeUnit.SECONDS); |
|
|
|
|
|
|
|
pageLink = new PageLink(4, 0, title1); |
|
|
|
pageData = doGetTypedWithPageLink("/api/customers?", new TypeReference<PageData<Customer>>(){}, pageLink); |
|
|
|
pageData = doGetTypedWithPageLink("/api/customers?", PAGE_DATA_CUSTOMER_TYPE_REFERENCE, pageLink); |
|
|
|
Assert.assertFalse(pageData.hasNext()); |
|
|
|
Assert.assertEquals(0, pageData.getData().size()); |
|
|
|
|
|
|
|
deleteFutures = new ArrayList<>(175); |
|
|
|
for (Customer customer : loadedCustomersTitle2) { |
|
|
|
doDelete("/api/customer/" + customer.getId().getId().toString()) |
|
|
|
.andExpect(status().isOk()); |
|
|
|
deleteFutures.add(executor.submit(() -> |
|
|
|
doDelete("/api/customer/" + customer.getId().getId().toString()) |
|
|
|
.andExpect(status().isOk()))); |
|
|
|
} |
|
|
|
Futures.allAsList(deleteFutures).get(TIMEOUT, TimeUnit.SECONDS); |
|
|
|
|
|
|
|
pageLink = new PageLink(4, 0, title2); |
|
|
|
pageData = doGetTypedWithPageLink("/api/customers?", new TypeReference<PageData<Customer>>(){}, pageLink); |
|
|
|
pageData = doGetTypedWithPageLink("/api/customers?", PAGE_DATA_CUSTOMER_TYPE_REFERENCE, pageLink); |
|
|
|
Assert.assertFalse(pageData.hasNext()); |
|
|
|
Assert.assertEquals(0, pageData.getData().size()); |
|
|
|
} |
|
|
|
|