Search in sources :

Example 41 with Customer

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

the class BaseCustomerServiceTest method testSaveCustomerWithEmptyTitle.

@Test(expected = DataValidationException.class)
public void testSaveCustomerWithEmptyTitle() {
    Customer customer = new Customer();
    customer.setTenantId(tenantId);
    customerService.saveCustomer(customer);
}
Also used : Customer(org.thingsboard.server.common.data.Customer) Test(org.junit.Test)

Example 42 with Customer

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

the class BaseUserServiceTest method testFindCustomerUsersByEmail.

@Test
public void testFindCustomerUsersByEmail() {
    Tenant tenant = new Tenant();
    tenant.setTitle("Test tenant");
    tenant = tenantService.saveTenant(tenant);
    TenantId tenantId = tenant.getId();
    Customer customer = new Customer();
    customer.setTitle("Test customer");
    customer.setTenantId(tenantId);
    customer = customerService.saveCustomer(customer);
    CustomerId customerId = customer.getId();
    String email1 = "testEmail1";
    List<User> customerUsersEmail1 = new ArrayList<>();
    for (int i = 0; i < 124; i++) {
        User user = new User();
        user.setAuthority(Authority.CUSTOMER_USER);
        user.setTenantId(tenantId);
        user.setCustomerId(customerId);
        String suffix = RandomStringUtils.randomAlphanumeric((int) (5 + Math.random() * 10));
        String email = email1 + suffix + "@thingsboard.org";
        email = i % 2 == 0 ? email.toLowerCase() : email.toUpperCase();
        user.setEmail(email);
        customerUsersEmail1.add(userService.saveUser(user));
    }
    String email2 = "testEmail2";
    List<User> customerUsersEmail2 = new ArrayList<>();
    for (int i = 0; i < 132; i++) {
        User user = new User();
        user.setAuthority(Authority.CUSTOMER_USER);
        user.setTenantId(tenantId);
        user.setCustomerId(customerId);
        String suffix = RandomStringUtils.randomAlphanumeric((int) (5 + Math.random() * 10));
        String email = email2 + suffix + "@thingsboard.org";
        email = i % 2 == 0 ? email.toLowerCase() : email.toUpperCase();
        user.setEmail(email);
        customerUsersEmail2.add(userService.saveUser(user));
    }
    List<User> loadedCustomerUsersEmail1 = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(33, email1);
    TextPageData<User> pageData = null;
    do {
        pageData = userService.findCustomerUsers(tenantId, customerId, pageLink);
        loadedCustomerUsersEmail1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(customerUsersEmail1, idComparator);
    Collections.sort(loadedCustomerUsersEmail1, idComparator);
    Assert.assertEquals(customerUsersEmail1, loadedCustomerUsersEmail1);
    List<User> loadedCustomerUsersEmail2 = new ArrayList<>();
    pageLink = new TextPageLink(16, email2);
    do {
        pageData = userService.findCustomerUsers(tenantId, customerId, pageLink);
        loadedCustomerUsersEmail2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(customerUsersEmail2, idComparator);
    Collections.sort(loadedCustomerUsersEmail2, idComparator);
    Assert.assertEquals(customerUsersEmail2, loadedCustomerUsersEmail2);
    for (User user : loadedCustomerUsersEmail1) {
        userService.deleteUser(user.getId());
    }
    pageLink = new TextPageLink(4, email1);
    pageData = userService.findCustomerUsers(tenantId, customerId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (User user : loadedCustomerUsersEmail2) {
        userService.deleteUser(user.getId());
    }
    pageLink = new TextPageLink(4, email2);
    pageData = userService.findCustomerUsers(tenantId, customerId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    tenantService.deleteTenant(tenantId);
}
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) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Test(org.junit.Test)

Example 43 with Customer

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

the class BaseUserServiceTest method before.

@Before
public void before() {
    Tenant tenant = new Tenant();
    tenant.setTitle("My tenant");
    Tenant savedTenant = tenantService.saveTenant(tenant);
    Assert.assertNotNull(savedTenant);
    tenantId = savedTenant.getId();
    User tenantAdmin = new User();
    tenantAdmin.setAuthority(Authority.TENANT_ADMIN);
    tenantAdmin.setTenantId(tenantId);
    tenantAdmin.setEmail("tenant@thingsboard.org");
    userService.saveUser(tenantAdmin);
    Customer customer = new Customer();
    customer.setTenantId(tenantId);
    customer.setTitle("My customer");
    Customer savedCustomer = customerService.saveCustomer(customer);
    User customerUser = new User();
    customerUser.setAuthority(Authority.CUSTOMER_USER);
    customerUser.setTenantId(tenantId);
    customerUser.setCustomerId(savedCustomer.getId());
    customerUser.setEmail("customer@thingsboard.org");
    userService.saveUser(customerUser);
}
Also used : Tenant(org.thingsboard.server.common.data.Tenant) User(org.thingsboard.server.common.data.User) Customer(org.thingsboard.server.common.data.Customer) Before(org.junit.Before)

Example 44 with Customer

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

the class BaseAssetServiceTest method testFindAssetsByTenantIdCustomerIdAndName.

@Test
public void testFindAssetsByTenantIdCustomerIdAndName() {
    Customer customer = new Customer();
    customer.setTitle("Test customer");
    customer.setTenantId(tenantId);
    customer = customerService.saveCustomer(customer);
    CustomerId customerId = customer.getId();
    String title1 = "Asset title 1";
    List<Asset> assetsTitle1 = new ArrayList<>();
    for (int i = 0; i < 175; i++) {
        Asset asset = new Asset();
        asset.setTenantId(tenantId);
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title1 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        asset.setName(name);
        asset.setType("default");
        asset = assetService.saveAsset(asset);
        assetsTitle1.add(assetService.assignAssetToCustomer(asset.getId(), customerId));
    }
    String title2 = "Asset title 2";
    List<Asset> assetsTitle2 = new ArrayList<>();
    for (int i = 0; i < 143; i++) {
        Asset asset = new Asset();
        asset.setTenantId(tenantId);
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title2 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        asset.setName(name);
        asset.setType("default");
        asset = assetService.saveAsset(asset);
        assetsTitle2.add(assetService.assignAssetToCustomer(asset.getId(), customerId));
    }
    List<Asset> loadedAssetsTitle1 = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(15, title1);
    TextPageData<Asset> pageData = null;
    do {
        pageData = assetService.findAssetsByTenantIdAndCustomerId(tenantId, customerId, pageLink);
        loadedAssetsTitle1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(assetsTitle1, idComparator);
    Collections.sort(loadedAssetsTitle1, idComparator);
    Assert.assertEquals(assetsTitle1, loadedAssetsTitle1);
    List<Asset> loadedAssetsTitle2 = new ArrayList<>();
    pageLink = new TextPageLink(4, title2);
    do {
        pageData = assetService.findAssetsByTenantIdAndCustomerId(tenantId, customerId, pageLink);
        loadedAssetsTitle2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(assetsTitle2, idComparator);
    Collections.sort(loadedAssetsTitle2, idComparator);
    Assert.assertEquals(assetsTitle2, loadedAssetsTitle2);
    for (Asset asset : loadedAssetsTitle1) {
        assetService.deleteAsset(asset.getId());
    }
    pageLink = new TextPageLink(4, title1);
    pageData = assetService.findAssetsByTenantIdAndCustomerId(tenantId, customerId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Asset asset : loadedAssetsTitle2) {
        assetService.deleteAsset(asset.getId());
    }
    pageLink = new TextPageLink(4, title2);
    pageData = assetService.findAssetsByTenantIdAndCustomerId(tenantId, customerId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    customerService.deleteCustomer(customerId);
}
Also used : TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Customer(org.thingsboard.server.common.data.Customer) ArrayList(java.util.ArrayList) Asset(org.thingsboard.server.common.data.asset.Asset) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Test(org.junit.Test)

Example 45 with Customer

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

the class BaseAssetServiceTest method testAssignAssetToCustomerFromDifferentTenant.

@Test(expected = DataValidationException.class)
public void testAssignAssetToCustomerFromDifferentTenant() {
    Asset asset = new Asset();
    asset.setName("My asset");
    asset.setType("default");
    asset.setTenantId(tenantId);
    asset = assetService.saveAsset(asset);
    Tenant tenant = new Tenant();
    tenant.setTitle("Test different tenant");
    tenant = tenantService.saveTenant(tenant);
    Customer customer = new Customer();
    customer.setTenantId(tenant.getId());
    customer.setTitle("Test different customer");
    customer = customerService.saveCustomer(customer);
    try {
        assetService.assignAssetToCustomer(asset.getId(), customer.getId());
    } finally {
        assetService.deleteAsset(asset.getId());
        tenantService.deleteTenant(tenant.getId());
    }
}
Also used : Tenant(org.thingsboard.server.common.data.Tenant) Customer(org.thingsboard.server.common.data.Customer) Asset(org.thingsboard.server.common.data.asset.Asset) 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