Search in sources :

Example 11 with CustomerId

use of org.thingsboard.server.common.data.id.CustomerId in project thingsboard by thingsboard.

the class CustomerEntity method toData.

@Override
public Customer toData() {
    Customer customer = new Customer(new CustomerId(getId()));
    customer.setCreatedTime(UUIDs.unixTimestamp(getId()));
    customer.setTenantId(new TenantId(UUIDConverter.fromString(tenantId)));
    customer.setTitle(title);
    customer.setCountry(country);
    customer.setState(state);
    customer.setCity(city);
    customer.setAddress(address);
    customer.setAddress2(address2);
    customer.setZip(zip);
    customer.setPhone(phone);
    customer.setEmail(email);
    customer.setAdditionalInfo(additionalInfo);
    return customer;
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Customer(org.thingsboard.server.common.data.Customer) CustomerId(org.thingsboard.server.common.data.id.CustomerId)

Example 12 with CustomerId

use of org.thingsboard.server.common.data.id.CustomerId in project thingsboard by thingsboard.

the class BaseAssetControllerTest method testFindCustomerAssetsByType.

@Test
public void testFindCustomerAssetsByType() throws Exception {
    Customer customer = new Customer();
    customer.setTitle("Test customer");
    customer = doPost("/api/customer", customer, Customer.class);
    CustomerId customerId = customer.getId();
    String title1 = "Asset title 1";
    String type1 = "typeC";
    List<Asset> assetsType1 = new ArrayList<>();
    for (int i = 0; i < 125; i++) {
        Asset asset = new Asset();
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title1 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        asset.setName(name);
        asset.setType(type1);
        asset = doPost("/api/asset", asset, Asset.class);
        assetsType1.add(doPost("/api/customer/" + customerId.getId().toString() + "/asset/" + asset.getId().getId().toString(), Asset.class));
    }
    String title2 = "Asset title 2";
    String type2 = "typeD";
    List<Asset> assetsType2 = new ArrayList<>();
    for (int i = 0; i < 143; i++) {
        Asset asset = new Asset();
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title2 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        asset.setName(name);
        asset.setType(type2);
        asset = doPost("/api/asset", asset, Asset.class);
        assetsType2.add(doPost("/api/customer/" + customerId.getId().toString() + "/asset/" + asset.getId().getId().toString(), Asset.class));
    }
    List<Asset> loadedAssetsType1 = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(15);
    TextPageData<Asset> pageData = null;
    do {
        pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/assets?type={type}&", new TypeReference<TextPageData<Asset>>() {
        }, pageLink, type1);
        loadedAssetsType1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(assetsType1, idComparator);
    Collections.sort(loadedAssetsType1, idComparator);
    Assert.assertEquals(assetsType1, loadedAssetsType1);
    List<Asset> loadedAssetsType2 = new ArrayList<>();
    pageLink = new TextPageLink(4);
    do {
        pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/assets?type={type}&", new TypeReference<TextPageData<Asset>>() {
        }, pageLink, type2);
        loadedAssetsType2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(assetsType2, idComparator);
    Collections.sort(loadedAssetsType2, idComparator);
    Assert.assertEquals(assetsType2, loadedAssetsType2);
    for (Asset asset : loadedAssetsType1) {
        doDelete("/api/customer/asset/" + asset.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new TextPageLink(4);
    pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/assets?type={type}&", new TypeReference<TextPageData<Asset>>() {
    }, pageLink, type1);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Asset asset : loadedAssetsType2) {
        doDelete("/api/customer/asset/" + asset.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new TextPageLink(4);
    pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/assets?type={type}&", new TypeReference<TextPageData<Asset>>() {
    }, pageLink, type2);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
}
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) Matchers.containsString(org.hamcrest.Matchers.containsString) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 13 with CustomerId

use of org.thingsboard.server.common.data.id.CustomerId in project thingsboard by thingsboard.

the class BaseDashboardControllerTest method testFindCustomerDashboards.

@Test
public void testFindCustomerDashboards() throws Exception {
    Customer customer = new Customer();
    customer.setTitle("Test customer");
    customer = doPost("/api/customer", customer, Customer.class);
    CustomerId customerId = customer.getId();
    List<DashboardInfo> dashboards = new ArrayList<>();
    for (int i = 0; i < 173; i++) {
        Dashboard dashboard = new Dashboard();
        dashboard.setTitle("Dashboard" + i);
        dashboard = doPost("/api/dashboard", dashboard, Dashboard.class);
        dashboards.add(new DashboardInfo(doPost("/api/customer/" + customerId.getId().toString() + "/dashboard/" + dashboard.getId().getId().toString(), Dashboard.class)));
    }
    List<DashboardInfo> loadedDashboards = new ArrayList<>();
    TimePageLink pageLink = new TimePageLink(21);
    TimePageData<DashboardInfo> pageData = null;
    do {
        pageData = doGetTypedWithTimePageLink("/api/customer/" + customerId.getId().toString() + "/dashboards?", new TypeReference<TimePageData<DashboardInfo>>() {
        }, pageLink);
        loadedDashboards.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(dashboards, idComparator);
    Collections.sort(loadedDashboards, idComparator);
    Assert.assertEquals(dashboards, loadedDashboards);
}
Also used : ArrayList(java.util.ArrayList) CustomerId(org.thingsboard.server.common.data.id.CustomerId) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 14 with CustomerId

use of org.thingsboard.server.common.data.id.CustomerId in project thingsboard by thingsboard.

the class BaseUserControllerTest method testFindCustomerUsers.

@Test
public void testFindCustomerUsers() 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();
    List<User> customerUsers = new ArrayList<>();
    for (int i = 0; i < 56; i++) {
        User user = new User();
        user.setAuthority(Authority.CUSTOMER_USER);
        user.setCustomerId(customerId);
        user.setEmail("testCustomer" + i + "@thingsboard.org");
        customerUsers.add(doPost("/api/user", user, User.class));
    }
    List<User> loadedCustomerUsers = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(33);
    TextPageData<User> pageData = null;
    do {
        pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/users?", new TypeReference<TextPageData<User>>() {
        }, pageLink);
        loadedCustomerUsers.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(customerUsers, idComparator);
    Collections.sort(loadedCustomerUsers, idComparator);
    Assert.assertEquals(customerUsers, loadedCustomerUsers);
    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) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 15 with CustomerId

use of org.thingsboard.server.common.data.id.CustomerId in project thingsboard by thingsboard.

the class RefreshTokenAuthenticationProvider method authenticateByPublicId.

private SecurityUser authenticateByPublicId(String publicId) {
    CustomerId customerId;
    try {
        customerId = new CustomerId(UUID.fromString(publicId));
    } catch (Exception e) {
        throw new BadCredentialsException("Refresh token is not valid");
    }
    Customer publicCustomer = customerService.findCustomerById(customerId);
    if (publicCustomer == null) {
        throw new UsernameNotFoundException("Public entity not found by refresh token");
    }
    if (!publicCustomer.isPublic()) {
        throw new BadCredentialsException("Refresh token is not valid");
    }
    User user = new User(new UserId(EntityId.NULL_UUID));
    user.setTenantId(publicCustomer.getTenantId());
    user.setCustomerId(publicCustomer.getId());
    user.setEmail(publicId);
    user.setAuthority(Authority.CUSTOMER_USER);
    user.setFirstName("Public");
    user.setLastName("Public");
    UserPrincipal userPrincipal = new UserPrincipal(UserPrincipal.Type.PUBLIC_ID, publicId);
    SecurityUser securityUser = new SecurityUser(user, true, userPrincipal);
    return securityUser;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) User(org.thingsboard.server.common.data.User) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) Customer(org.thingsboard.server.common.data.Customer) UserId(org.thingsboard.server.common.data.id.UserId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) AuthenticationException(org.springframework.security.core.AuthenticationException) UserPrincipal(org.thingsboard.server.service.security.model.UserPrincipal)

Aggregations

CustomerId (org.thingsboard.server.common.data.id.CustomerId)59 TenantId (org.thingsboard.server.common.data.id.TenantId)30 Customer (org.thingsboard.server.common.data.Customer)24 ArrayList (java.util.ArrayList)22 Test (org.junit.Test)22 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)21 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)18 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)17 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)12 Asset (org.thingsboard.server.common.data.asset.Asset)11 User (org.thingsboard.server.common.data.User)10 TypeReference (com.fasterxml.jackson.core.type.TypeReference)9 UserId (org.thingsboard.server.common.data.id.UserId)9 Device (org.thingsboard.server.common.data.Device)8 Tenant (org.thingsboard.server.common.data.Tenant)7 DashboardId (org.thingsboard.server.common.data.id.DashboardId)6 TimePageLink (org.thingsboard.server.common.data.page.TimePageLink)6 SecurityUser (org.thingsboard.server.service.security.model.SecurityUser)6 List (java.util.List)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4