Search in sources :

Example 6 with CustomerId

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

the class BaseDeviceServiceTest method testFindDevicesByTenantIdAndCustomerId.

@Test
public void testFindDevicesByTenantIdAndCustomerId() {
    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();
    List<Device> devices = new ArrayList<>();
    for (int i = 0; i < 278; i++) {
        Device device = new Device();
        device.setTenantId(tenantId);
        device.setName("Device" + i);
        device.setType("default");
        device = deviceService.saveDevice(device);
        devices.add(deviceService.assignDeviceToCustomer(device.getId(), customerId));
    }
    List<Device> loadedDevices = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(23);
    TextPageData<Device> pageData = null;
    do {
        pageData = deviceService.findDevicesByTenantIdAndCustomerId(tenantId, customerId, pageLink);
        loadedDevices.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devices, idComparator);
    Collections.sort(loadedDevices, idComparator);
    Assert.assertEquals(devices, loadedDevices);
    deviceService.unassignCustomerDevices(tenantId, customerId);
    pageLink = new TextPageLink(33);
    pageData = deviceService.findDevicesByTenantIdAndCustomerId(tenantId, customerId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertTrue(pageData.getData().isEmpty());
    tenantService.deleteTenant(tenantId);
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Tenant(org.thingsboard.server.common.data.Tenant) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Customer(org.thingsboard.server.common.data.Customer) Device(org.thingsboard.server.common.data.Device) ArrayList(java.util.ArrayList) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Test(org.junit.Test)

Example 7 with CustomerId

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

the class BaseAssetServiceTest method testAssignAssetToNonExistentCustomer.

@Test(expected = DataValidationException.class)
public void testAssignAssetToNonExistentCustomer() {
    Asset asset = new Asset();
    asset.setName("My asset");
    asset.setType("default");
    asset.setTenantId(tenantId);
    asset = assetService.saveAsset(asset);
    try {
        assetService.assignAssetToCustomer(asset.getId(), new CustomerId(UUIDs.timeBased()));
    } finally {
        assetService.deleteAsset(asset.getId());
    }
}
Also used : Asset(org.thingsboard.server.common.data.asset.Asset) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Test(org.junit.Test)

Example 8 with CustomerId

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

the class BaseAssetServiceTest method testFindAssetsByTenantIdCustomerIdAndType.

@Test
public void testFindAssetsByTenantIdCustomerIdAndType() {
    Customer customer = new Customer();
    customer.setTitle("Test customer");
    customer.setTenantId(tenantId);
    customer = customerService.saveCustomer(customer);
    CustomerId customerId = customer.getId();
    String title1 = "Asset title 1";
    String type1 = "typeC";
    List<Asset> assetsType1 = 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(type1);
        asset = assetService.saveAsset(asset);
        assetsType1.add(assetService.assignAssetToCustomer(asset.getId(), customerId));
    }
    String title2 = "Asset title 2";
    String type2 = "typeD";
    List<Asset> assetsType2 = 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(type2);
        asset = assetService.saveAsset(asset);
        assetsType2.add(assetService.assignAssetToCustomer(asset.getId(), customerId));
    }
    List<Asset> loadedAssetsType1 = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(15);
    TextPageData<Asset> pageData = null;
    do {
        pageData = assetService.findAssetsByTenantIdAndCustomerIdAndType(tenantId, customerId, type1, pageLink);
        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 = assetService.findAssetsByTenantIdAndCustomerIdAndType(tenantId, customerId, type2, pageLink);
        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) {
        assetService.deleteAsset(asset.getId());
    }
    pageLink = new TextPageLink(4);
    pageData = assetService.findAssetsByTenantIdAndCustomerIdAndType(tenantId, customerId, type1, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Asset asset : loadedAssetsType2) {
        assetService.deleteAsset(asset.getId());
    }
    pageLink = new TextPageLink(4);
    pageData = assetService.findAssetsByTenantIdAndCustomerIdAndType(tenantId, customerId, type2, 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 9 with CustomerId

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

the class JpaDashboardInfoDao method findDashboardsByTenantIdAndCustomerId.

@Override
public ListenableFuture<List<DashboardInfo>> findDashboardsByTenantIdAndCustomerId(UUID tenantId, UUID customerId, TimePageLink pageLink) {
    log.debug("Try to find dashboards by tenantId [{}], customerId[{}] and pageLink [{}]", tenantId, customerId, pageLink);
    ListenableFuture<List<EntityRelation>> relations = relationDao.findRelations(new CustomerId(customerId), EntityRelation.CONTAINS_TYPE, RelationTypeGroup.DASHBOARD, EntityType.DASHBOARD, pageLink);
    return Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<DashboardInfo>>) input -> {
        List<ListenableFuture<DashboardInfo>> dashboardFutures = new ArrayList<>(input.size());
        for (EntityRelation relation : input) {
            dashboardFutures.add(findByIdAsync(relation.getTo().getId()));
        }
        return Futures.successfulAsList(dashboardFutures);
    });
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Time(java.sql.Time) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) Autowired(org.springframework.beans.factory.annotation.Autowired) CrudRepository(org.springframework.data.repository.CrudRepository) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) NULL_UUID_STR(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID_STR) EntityType(org.thingsboard.server.common.data.EntityType) SqlDao(org.thingsboard.server.dao.util.SqlDao) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo) PageRequest(org.springframework.data.domain.PageRequest) UUID(java.util.UUID) RelationDao(org.thingsboard.server.dao.relation.RelationDao) Objects(java.util.Objects) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) JpaAbstractSearchTextDao(org.thingsboard.server.dao.sql.JpaAbstractSearchTextDao) List(java.util.List) DaoUtil(org.thingsboard.server.dao.DaoUtil) DashboardInfoDao(org.thingsboard.server.dao.dashboard.DashboardInfoDao) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) UUIDConverter(org.thingsboard.server.common.data.UUIDConverter) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) DashboardInfoEntity(org.thingsboard.server.dao.model.sql.DashboardInfoEntity) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) List(java.util.List) CustomerId(org.thingsboard.server.common.data.id.CustomerId) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo)

Example 10 with CustomerId

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

the class CassandraDashboardInfoDao method findDashboardsByTenantIdAndCustomerId.

@Override
public ListenableFuture<List<DashboardInfo>> findDashboardsByTenantIdAndCustomerId(UUID tenantId, UUID customerId, TimePageLink pageLink) {
    log.debug("Try to find dashboards by tenantId [{}], customerId[{}] and pageLink [{}]", tenantId, customerId, pageLink);
    ListenableFuture<List<EntityRelation>> relations = relationDao.findRelations(new CustomerId(customerId), EntityRelation.CONTAINS_TYPE, RelationTypeGroup.DASHBOARD, EntityType.DASHBOARD, pageLink);
    return Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<DashboardInfo>>) input -> {
        List<ListenableFuture<DashboardInfo>> dashboardFutures = new ArrayList<>(input.size());
        for (EntityRelation relation : input) {
            dashboardFutures.add(findByIdAsync(relation.getTo().getId()));
        }
        return Futures.successfulAsList(dashboardFutures);
    });
}
Also used : DashboardInfoEntity(org.thingsboard.server.dao.model.nosql.DashboardInfoEntity) CassandraAbstractSearchTextDao(org.thingsboard.server.dao.nosql.CassandraAbstractSearchTextDao) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo) QueryBuilder.eq(com.datastax.driver.core.querybuilder.QueryBuilder.eq) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) NoSqlDao(org.thingsboard.server.dao.util.NoSqlDao) Autowired(org.springframework.beans.factory.annotation.Autowired) UUID(java.util.UUID) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) RelationDao(org.thingsboard.server.dao.relation.RelationDao) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) EntityType(org.thingsboard.server.common.data.EntityType) DaoUtil(org.thingsboard.server.dao.DaoUtil) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) Collections(java.util.Collections) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) CustomerId(org.thingsboard.server.common.data.id.CustomerId) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) List(java.util.List) CustomerId(org.thingsboard.server.common.data.id.CustomerId) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo)

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