Search in sources :

Example 51 with Customer

use of org.thingsboard.server.common.data.Customer in project thingsboard by thingsboard.

the class BaseCustomerControllerTest method testSaveCustomerWithEmptyTitle.

@Test
public void testSaveCustomerWithEmptyTitle() throws Exception {
    loginSysAdmin();
    Tenant tenant = new Tenant();
    tenant.setTitle("My tenant");
    Tenant savedTenant = doPost("/api/tenant", tenant, Tenant.class);
    Assert.assertNotNull(savedTenant);
    User tenantAdmin = new User();
    tenantAdmin.setAuthority(Authority.TENANT_ADMIN);
    tenantAdmin.setTenantId(savedTenant.getId());
    tenantAdmin.setEmail("tenant2@thingsboard.org");
    tenantAdmin.setFirstName("Joe");
    tenantAdmin.setLastName("Downs");
    tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1");
    Customer customer = new Customer();
    doPost("/api/customer", customer).andExpect(status().isBadRequest()).andExpect(statusReason(containsString("Customer title should be specified")));
    loginSysAdmin();
    doDelete("/api/tenant/" + savedTenant.getId().getId().toString()).andExpect(status().isOk());
}
Also used : Tenant(org.thingsboard.server.common.data.Tenant) User(org.thingsboard.server.common.data.User) Customer(org.thingsboard.server.common.data.Customer) Test(org.junit.Test)

Example 52 with Customer

use of org.thingsboard.server.common.data.Customer in project thingsboard by thingsboard.

the class BaseCustomerControllerTest method testFindCustomers.

@Test
public void testFindCustomers() throws Exception {
    loginSysAdmin();
    Tenant tenant = new Tenant();
    tenant.setTitle("My tenant");
    Tenant savedTenant = doPost("/api/tenant", tenant, Tenant.class);
    Assert.assertNotNull(savedTenant);
    TenantId tenantId = savedTenant.getId();
    User tenantAdmin = new User();
    tenantAdmin.setAuthority(Authority.TENANT_ADMIN);
    tenantAdmin.setTenantId(tenantId);
    tenantAdmin.setEmail("tenant2@thingsboard.org");
    tenantAdmin.setFirstName("Joe");
    tenantAdmin.setLastName("Downs");
    tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1");
    List<Customer> customers = new ArrayList<>();
    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));
    }
    List<Customer> loadedCustomers = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(23);
    TextPageData<Customer> pageData = null;
    do {
        pageData = doGetTypedWithPageLink("/api/customers?", new TypeReference<TextPageData<Customer>>() {
        }, pageLink);
        loadedCustomers.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(customers, idComparator);
    Collections.sort(loadedCustomers, idComparator);
    Assert.assertEquals(customers, loadedCustomers);
    loginSysAdmin();
    doDelete("/api/tenant/" + savedTenant.getId().getId().toString()).andExpect(status().isOk());
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Tenant(org.thingsboard.server.common.data.Tenant) User(org.thingsboard.server.common.data.User) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Customer(org.thingsboard.server.common.data.Customer) ArrayList(java.util.ArrayList) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 53 with Customer

use of org.thingsboard.server.common.data.Customer in project thingsboard by thingsboard.

the class BaseCustomerControllerTest method testFindCustomersByTitle.

@Test
public void testFindCustomersByTitle() throws Exception {
    loginSysAdmin();
    Tenant tenant = new Tenant();
    tenant.setTitle("My tenant");
    Tenant savedTenant = doPost("/api/tenant", tenant, Tenant.class);
    Assert.assertNotNull(savedTenant);
    TenantId tenantId = savedTenant.getId();
    User tenantAdmin = new User();
    tenantAdmin.setAuthority(Authority.TENANT_ADMIN);
    tenantAdmin.setTenantId(tenantId);
    tenantAdmin.setEmail("tenant2@thingsboard.org");
    tenantAdmin.setFirstName("Joe");
    tenantAdmin.setLastName("Downs");
    tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1");
    String title1 = "Customer title 1";
    List<Customer> customersTitle1 = new ArrayList<>();
    for (int i = 0; i < 143; i++) {
        Customer customer = new Customer();
        customer.setTenantId(tenantId);
        String suffix = RandomStringUtils.randomAlphanumeric((int) (5 + Math.random() * 10));
        String title = title1 + suffix;
        title = i % 2 == 0 ? title.toLowerCase() : title.toUpperCase();
        customer.setTitle(title);
        customersTitle1.add(doPost("/api/customer", customer, Customer.class));
    }
    String title2 = "Customer title 2";
    List<Customer> customersTitle2 = new ArrayList<>();
    for (int i = 0; i < 175; i++) {
        Customer customer = new Customer();
        customer.setTenantId(tenantId);
        String suffix = RandomStringUtils.randomAlphanumeric((int) (5 + Math.random() * 10));
        String title = title2 + suffix;
        title = i % 2 == 0 ? title.toLowerCase() : title.toUpperCase();
        customer.setTitle(title);
        customersTitle2.add(doPost("/api/customer", customer, Customer.class));
    }
    List<Customer> loadedCustomersTitle1 = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(15, title1);
    TextPageData<Customer> pageData = null;
    do {
        pageData = doGetTypedWithPageLink("/api/customers?", new TypeReference<TextPageData<Customer>>() {
        }, pageLink);
        loadedCustomersTitle1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(customersTitle1, idComparator);
    Collections.sort(loadedCustomersTitle1, idComparator);
    Assert.assertEquals(customersTitle1, loadedCustomersTitle1);
    List<Customer> loadedCustomersTitle2 = new ArrayList<>();
    pageLink = new TextPageLink(4, title2);
    do {
        pageData = doGetTypedWithPageLink("/api/customers?", new TypeReference<TextPageData<Customer>>() {
        }, pageLink);
        loadedCustomersTitle2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(customersTitle2, idComparator);
    Collections.sort(loadedCustomersTitle2, idComparator);
    Assert.assertEquals(customersTitle2, loadedCustomersTitle2);
    for (Customer customer : loadedCustomersTitle1) {
        doDelete("/api/customer/" + customer.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new TextPageLink(4, title1);
    pageData = doGetTypedWithPageLink("/api/customers?", new TypeReference<TextPageData<Customer>>() {
    }, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Customer customer : loadedCustomersTitle2) {
        doDelete("/api/customer/" + customer.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new TextPageLink(4, title2);
    pageData = doGetTypedWithPageLink("/api/customers?", new TypeReference<TextPageData<Customer>>() {
    }, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    loginSysAdmin();
    doDelete("/api/tenant/" + savedTenant.getId().getId().toString()).andExpect(status().isOk());
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Tenant(org.thingsboard.server.common.data.Tenant) User(org.thingsboard.server.common.data.User) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Customer(org.thingsboard.server.common.data.Customer) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 54 with Customer

use of org.thingsboard.server.common.data.Customer in project thingsboard by thingsboard.

the class BaseCustomerControllerTest method testSaveCustomer.

@Test
public void testSaveCustomer() throws Exception {
    loginSysAdmin();
    Tenant tenant = new Tenant();
    tenant.setTitle("My tenant");
    Tenant savedTenant = doPost("/api/tenant", tenant, Tenant.class);
    Assert.assertNotNull(savedTenant);
    User tenantAdmin = new User();
    tenantAdmin.setAuthority(Authority.TENANT_ADMIN);
    tenantAdmin.setTenantId(savedTenant.getId());
    tenantAdmin.setEmail("tenant2@thingsboard.org");
    tenantAdmin.setFirstName("Joe");
    tenantAdmin.setLastName("Downs");
    tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1");
    Customer customer = new Customer();
    customer.setTitle("My customer");
    Customer savedCustomer = doPost("/api/customer", customer, Customer.class);
    Assert.assertNotNull(savedCustomer);
    Assert.assertNotNull(savedCustomer.getId());
    Assert.assertTrue(savedCustomer.getCreatedTime() > 0);
    Assert.assertEquals(customer.getTitle(), savedCustomer.getTitle());
    savedCustomer.setTitle("My new customer");
    doPost("/api/customer", savedCustomer, 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());
    loginSysAdmin();
    doDelete("/api/tenant/" + savedTenant.getId().getId().toString()).andExpect(status().isOk());
}
Also used : Tenant(org.thingsboard.server.common.data.Tenant) User(org.thingsboard.server.common.data.User) Customer(org.thingsboard.server.common.data.Customer) Test(org.junit.Test)

Example 55 with Customer

use of org.thingsboard.server.common.data.Customer in project thingsboard by thingsboard.

the class BaseCustomerControllerTest method testSaveCustomerWithInvalidEmail.

@Test
public void testSaveCustomerWithInvalidEmail() throws Exception {
    loginSysAdmin();
    Tenant tenant = new Tenant();
    tenant.setTitle("My tenant");
    Tenant savedTenant = doPost("/api/tenant", tenant, Tenant.class);
    Assert.assertNotNull(savedTenant);
    User tenantAdmin = new User();
    tenantAdmin.setAuthority(Authority.TENANT_ADMIN);
    tenantAdmin.setTenantId(savedTenant.getId());
    tenantAdmin.setEmail("tenant2@thingsboard.org");
    tenantAdmin.setFirstName("Joe");
    tenantAdmin.setLastName("Downs");
    tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1");
    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());
}
Also used : Tenant(org.thingsboard.server.common.data.Tenant) User(org.thingsboard.server.common.data.User) Customer(org.thingsboard.server.common.data.Customer) Test(org.junit.Test)

Aggregations

Customer (org.thingsboard.server.common.data.Customer)59 Test (org.junit.Test)37 CustomerId (org.thingsboard.server.common.data.id.CustomerId)24 Tenant (org.thingsboard.server.common.data.Tenant)21 ArrayList (java.util.ArrayList)18 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)18 User (org.thingsboard.server.common.data.User)16 TenantId (org.thingsboard.server.common.data.id.TenantId)14 Asset (org.thingsboard.server.common.data.asset.Asset)12 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)10 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)10 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)8 TypeReference (com.fasterxml.jackson.core.type.TypeReference)7 Device (org.thingsboard.server.common.data.Device)7 Matchers.containsString (org.hamcrest.Matchers.containsString)4 AssetId (org.thingsboard.server.common.data.id.AssetId)3 DeviceId (org.thingsboard.server.common.data.id.DeviceId)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 UUID (java.util.UUID)2 Before (org.junit.Before)2