Search in sources :

Example 56 with Customer

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

the class AssetController method assignAssetToCustomer.

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer/{customerId}/asset/{assetId}", method = RequestMethod.POST)
@ResponseBody
public Asset assignAssetToCustomer(@PathVariable("customerId") String strCustomerId, @PathVariable(ASSET_ID) String strAssetId) throws ThingsboardException {
    checkParameter("customerId", strCustomerId);
    checkParameter(ASSET_ID, strAssetId);
    try {
        CustomerId customerId = new CustomerId(toUUID(strCustomerId));
        Customer customer = checkCustomerId(customerId);
        AssetId assetId = new AssetId(toUUID(strAssetId));
        checkAssetId(assetId);
        Asset savedAsset = checkNotNull(assetService.assignAssetToCustomer(assetId, customerId));
        logEntityAction(assetId, savedAsset, savedAsset.getCustomerId(), ActionType.ASSIGNED_TO_CUSTOMER, null, strAssetId, strCustomerId, customer.getName());
        return savedAsset;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.ASSET), null, null, ActionType.ASSIGNED_TO_CUSTOMER, e, strAssetId, strCustomerId);
        throw handleException(e);
    }
}
Also used : Customer(org.thingsboard.server.common.data.Customer) Asset(org.thingsboard.server.common.data.asset.Asset) CustomerId(org.thingsboard.server.common.data.id.CustomerId) AssetId(org.thingsboard.server.common.data.id.AssetId) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 57 with Customer

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

the class AbstractControllerTest method setup.

@Before
public void setup() throws Exception {
    log.info("Executing setup");
    if (this.mockMvc == null) {
        this.mockMvc = webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
    }
    loginSysAdmin();
    Tenant tenant = new Tenant();
    tenant.setTitle(TEST_TENANT_NAME);
    Tenant savedTenant = doPost("/api/tenant", tenant, Tenant.class);
    Assert.assertNotNull(savedTenant);
    tenantId = savedTenant.getId();
    User tenantAdmin = new User();
    tenantAdmin.setAuthority(Authority.TENANT_ADMIN);
    tenantAdmin.setTenantId(tenantId);
    tenantAdmin.setEmail(TENANT_ADMIN_EMAIL);
    createUserAndLogin(tenantAdmin, TENANT_ADMIN_PASSWORD);
    Customer customer = new Customer();
    customer.setTitle("Customer");
    customer.setTenantId(tenantId);
    Customer savedCustomer = doPost("/api/customer", customer, Customer.class);
    User customerUser = new User();
    customerUser.setAuthority(Authority.CUSTOMER_USER);
    customerUser.setTenantId(tenantId);
    customerUser.setCustomerId(savedCustomer.getId());
    customerUser.setEmail(CUSTOMER_USER_EMAIL);
    createUserAndLogin(customerUser, CUSTOMER_USER_PASSWORD);
    logout();
    log.info("Executed setup");
}
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 58 with Customer

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

the class BaseAssetControllerTest method testAssignUnassignAssetToCustomer.

@Test
public void testAssignUnassignAssetToCustomer() throws Exception {
    Asset asset = new Asset();
    asset.setName("My asset");
    asset.setType("default");
    Asset savedAsset = doPost("/api/asset", asset, Asset.class);
    Customer customer = new Customer();
    customer.setTitle("My customer");
    Customer savedCustomer = doPost("/api/customer", customer, Customer.class);
    Asset assignedAsset = doPost("/api/customer/" + savedCustomer.getId().getId().toString() + "/asset/" + savedAsset.getId().getId().toString(), Asset.class);
    Assert.assertEquals(savedCustomer.getId(), assignedAsset.getCustomerId());
    Asset foundAsset = doGet("/api/asset/" + savedAsset.getId().getId().toString(), Asset.class);
    Assert.assertEquals(savedCustomer.getId(), foundAsset.getCustomerId());
    Asset unassignedAsset = doDelete("/api/customer/asset/" + savedAsset.getId().getId().toString(), Asset.class);
    Assert.assertEquals(ModelConstants.NULL_UUID, unassignedAsset.getCustomerId().getId());
    foundAsset = doGet("/api/asset/" + savedAsset.getId().getId().toString(), Asset.class);
    Assert.assertEquals(ModelConstants.NULL_UUID, foundAsset.getCustomerId().getId());
}
Also used : Customer(org.thingsboard.server.common.data.Customer) Asset(org.thingsboard.server.common.data.asset.Asset) Test(org.junit.Test)

Example 59 with Customer

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

the class BaseUserControllerTest method testFindCustomerUsersByEmail.

@Test
public void testFindCustomerUsersByEmail() 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");
    Customer customer = new Customer();
    customer.setTitle("My customer");
    Customer savedCustomer = doPost("/api/customer", customer, Customer.class);
    CustomerId customerId = savedCustomer.getId();
    String email1 = "testEmail1";
    List<User> customerUsersEmail1 = new ArrayList<>();
    for (int i = 0; i < 74; i++) {
        User user = new User();
        user.setAuthority(Authority.CUSTOMER_USER);
        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(doPost("/api/user", user, User.class));
    }
    String email2 = "testEmail2";
    List<User> customerUsersEmail2 = new ArrayList<>();
    for (int i = 0; i < 92; i++) {
        User user = new User();
        user.setAuthority(Authority.CUSTOMER_USER);
        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(doPost("/api/user", user, User.class));
    }
    List<User> loadedCustomerUsersEmail1 = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(33, email1);
    TextPageData<User> pageData = null;
    do {
        pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/users?", new TypeReference<TextPageData<User>>() {
        }, 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 = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/users?", new TypeReference<TextPageData<User>>() {
        }, 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) {
        doDelete("/api/user/" + user.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new TextPageLink(4, email1);
    pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/users?", new TypeReference<TextPageData<User>>() {
    }, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (User user : loadedCustomerUsersEmail2) {
        doDelete("/api/user/" + user.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new TextPageLink(4, email2);
    pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/users?", new TypeReference<TextPageData<User>>() {
    }, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    doDelete("/api/customer/" + customerId.getId().toString()).andExpect(status().isOk());
    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) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Matchers.containsString(org.hamcrest.Matchers.containsString) TypeReference(com.fasterxml.jackson.core.type.TypeReference) 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