Search in sources :

Example 61 with PageLink

use of org.thingsboard.server.common.data.page.PageLink 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(tenantId, 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(tenantId, asset.getId(), customerId));
    }
    List<Asset> loadedAssetsType1 = new ArrayList<>();
    PageLink pageLink = new PageLink(15);
    PageData<Asset> pageData = null;
    do {
        pageData = assetService.findAssetsByTenantIdAndCustomerIdAndType(tenantId, customerId, type1, pageLink);
        loadedAssetsType1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(assetsType1, idComparator);
    Collections.sort(loadedAssetsType1, idComparator);
    Assert.assertEquals(assetsType1, loadedAssetsType1);
    List<Asset> loadedAssetsType2 = new ArrayList<>();
    pageLink = new PageLink(4);
    do {
        pageData = assetService.findAssetsByTenantIdAndCustomerIdAndType(tenantId, customerId, type2, pageLink);
        loadedAssetsType2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(assetsType2, idComparator);
    Collections.sort(loadedAssetsType2, idComparator);
    Assert.assertEquals(assetsType2, loadedAssetsType2);
    for (Asset asset : loadedAssetsType1) {
        assetService.deleteAsset(tenantId, asset.getId());
    }
    pageLink = new PageLink(4);
    pageData = assetService.findAssetsByTenantIdAndCustomerIdAndType(tenantId, customerId, type1, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Asset asset : loadedAssetsType2) {
        assetService.deleteAsset(tenantId, asset.getId());
    }
    pageLink = new PageLink(4);
    pageData = assetService.findAssetsByTenantIdAndCustomerIdAndType(tenantId, customerId, type2, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    customerService.deleteCustomer(tenantId, customerId);
}
Also used : Customer(org.thingsboard.server.common.data.Customer) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) Asset(org.thingsboard.server.common.data.asset.Asset) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Test(org.junit.Test)

Example 62 with PageLink

use of org.thingsboard.server.common.data.page.PageLink in project thingsboard by thingsboard.

the class BaseDashboardServiceTest method testFindMobileDashboardsByTenantId.

@Test
public void testFindMobileDashboardsByTenantId() {
    Tenant tenant = new Tenant();
    tenant.setTitle("Test tenant");
    tenant = tenantService.saveTenant(tenant);
    TenantId tenantId = tenant.getId();
    List<DashboardInfo> mobileDashboards = new ArrayList<>();
    for (int i = 0; i < 165; i++) {
        Dashboard dashboard = new Dashboard();
        dashboard.setTenantId(tenantId);
        dashboard.setTitle("Dashboard" + i);
        dashboard.setMobileHide(i % 2 == 0);
        if (!dashboard.isMobileHide()) {
            dashboard.setMobileOrder(i % 4 == 0 ? (int) (Math.random() * 100) : null);
        }
        Dashboard savedDashboard = dashboardService.saveDashboard(dashboard);
        if (!dashboard.isMobileHide()) {
            mobileDashboards.add(new DashboardInfo(savedDashboard));
        }
    }
    List<DashboardInfo> loadedMobileDashboards = new ArrayList<>();
    PageLink pageLink = new PageLink(16, 0, null, new SortOrder("title", SortOrder.Direction.ASC));
    PageData<DashboardInfo> pageData = null;
    do {
        pageData = dashboardService.findMobileDashboardsByTenantId(tenantId, pageLink);
        loadedMobileDashboards.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(mobileDashboards, (o1, o2) -> {
        Integer order1 = o1.getMobileOrder();
        Integer order2 = o2.getMobileOrder();
        if (order1 == null && order2 == null) {
            return o1.getTitle().compareTo(o2.getTitle());
        } else if (order1 == null && order2 != null) {
            return 1;
        } else if (order2 == null) {
            return -1;
        } else {
            return order1 - order2;
        }
    });
    Assert.assertEquals(mobileDashboards, loadedMobileDashboards);
    dashboardService.deleteDashboardsByTenantId(tenantId);
    pageLink = new PageLink(31);
    pageData = dashboardService.findMobileDashboardsByTenantId(tenantId, 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) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) Dashboard(org.thingsboard.server.common.data.Dashboard) SortOrder(org.thingsboard.server.common.data.page.SortOrder) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo) Test(org.junit.Test)

Example 63 with PageLink

use of org.thingsboard.server.common.data.page.PageLink in project thingsboard by thingsboard.

the class BaseDashboardServiceTest method testFindDashboardsByTenantIdAndCustomerId.

@Test
public void testFindDashboardsByTenantIdAndCustomerId() throws ExecutionException, InterruptedException {
    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<DashboardInfo> dashboards = new ArrayList<>();
    for (int i = 0; i < 223; i++) {
        Dashboard dashboard = new Dashboard();
        dashboard.setTenantId(tenantId);
        dashboard.setTitle("Dashboard" + i);
        dashboard = dashboardService.saveDashboard(dashboard);
        dashboards.add(new DashboardInfo(dashboardService.assignDashboardToCustomer(tenantId, dashboard.getId(), customerId)));
    }
    List<DashboardInfo> loadedDashboards = new ArrayList<>();
    PageLink pageLink = new PageLink(23);
    PageData<DashboardInfo> pageData = null;
    do {
        pageData = dashboardService.findDashboardsByTenantIdAndCustomerId(tenantId, customerId, pageLink);
        loadedDashboards.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(dashboards, idComparator);
    Collections.sort(loadedDashboards, idComparator);
    Assert.assertEquals(dashboards, loadedDashboards);
    dashboardService.unassignCustomerDashboards(tenantId, customerId);
    pageLink = new PageLink(42);
    pageData = dashboardService.findDashboardsByTenantIdAndCustomerId(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) Customer(org.thingsboard.server.common.data.Customer) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) Dashboard(org.thingsboard.server.common.data.Dashboard) CustomerId(org.thingsboard.server.common.data.id.CustomerId) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo) Test(org.junit.Test)

Example 64 with PageLink

use of org.thingsboard.server.common.data.page.PageLink in project thingsboard by thingsboard.

the class BaseCustomerServiceTest method testFindCustomersByTenantId.

@Test
public void testFindCustomersByTenantId() {
    Tenant tenant = new Tenant();
    tenant.setTitle("Test tenant");
    tenant = tenantService.saveTenant(tenant);
    TenantId tenantId = tenant.getId();
    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(customerService.saveCustomer(customer));
    }
    List<Customer> loadedCustomers = new ArrayList<>();
    PageLink pageLink = new PageLink(23);
    PageData<Customer> pageData = null;
    do {
        pageData = customerService.findCustomersByTenantId(tenantId, pageLink);
        loadedCustomers.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(customers, idComparator);
    Collections.sort(loadedCustomers, idComparator);
    Assert.assertEquals(customers, loadedCustomers);
    customerService.deleteCustomersByTenantId(tenantId);
    pageLink = new PageLink(33);
    pageData = customerService.findCustomersByTenantId(tenantId, 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) Customer(org.thingsboard.server.common.data.Customer) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) Test(org.junit.Test)

Example 65 with PageLink

use of org.thingsboard.server.common.data.page.PageLink in project thingsboard by thingsboard.

the class DeviceController method getCustomerDeviceInfos.

@ApiOperation(value = "Get Customer Device Infos (getCustomerDeviceInfos)", notes = "Returns a page of devices info objects assigned to customer. " + PAGE_DATA_PARAMETERS + DEVICE_INFO_DESCRIPTION + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/customer/{customerId}/deviceInfos", params = { "pageSize", "page" }, method = RequestMethod.GET)
@ResponseBody
public PageData<DeviceInfo> getCustomerDeviceInfos(@ApiParam(value = CUSTOMER_ID_PARAM_DESCRIPTION, required = true) @PathVariable("customerId") String strCustomerId, @ApiParam(value = PAGE_SIZE_DESCRIPTION, required = true) @RequestParam int pageSize, @ApiParam(value = PAGE_NUMBER_DESCRIPTION, required = true) @RequestParam int page, @ApiParam(value = DEVICE_TYPE_DESCRIPTION) @RequestParam(required = false) String type, @ApiParam(value = DEVICE_PROFILE_ID_PARAM_DESCRIPTION) @RequestParam(required = false) String deviceProfileId, @ApiParam(value = DEVICE_TEXT_SEARCH_DESCRIPTION) @RequestParam(required = false) String textSearch, @ApiParam(value = SORT_PROPERTY_DESCRIPTION, allowableValues = DEVICE_SORT_PROPERTY_ALLOWABLE_VALUES) @RequestParam(required = false) String sortProperty, @ApiParam(value = SORT_ORDER_DESCRIPTION, allowableValues = SORT_ORDER_ALLOWABLE_VALUES) @RequestParam(required = false) String sortOrder) throws ThingsboardException {
    checkParameter("customerId", strCustomerId);
    try {
        TenantId tenantId = getCurrentUser().getTenantId();
        CustomerId customerId = new CustomerId(toUUID(strCustomerId));
        checkCustomerId(customerId, Operation.READ);
        PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder);
        if (type != null && type.trim().length() > 0) {
            return checkNotNull(deviceService.findDeviceInfosByTenantIdAndCustomerIdAndType(tenantId, customerId, type, pageLink));
        } else if (deviceProfileId != null && deviceProfileId.length() > 0) {
            DeviceProfileId profileId = new DeviceProfileId(toUUID(deviceProfileId));
            return checkNotNull(deviceService.findDeviceInfosByTenantIdAndCustomerIdAndDeviceProfileId(tenantId, customerId, profileId, pageLink));
        } else {
            return checkNotNull(deviceService.findDeviceInfosByTenantIdAndCustomerId(tenantId, customerId, pageLink));
        }
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) PageLink(org.thingsboard.server.common.data.page.PageLink) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) CustomerId(org.thingsboard.server.common.data.id.CustomerId) IOException(java.io.IOException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

PageLink (org.thingsboard.server.common.data.page.PageLink)187 Test (org.junit.Test)117 ArrayList (java.util.ArrayList)106 TenantId (org.thingsboard.server.common.data.id.TenantId)67 TypeReference (com.fasterxml.jackson.core.type.TypeReference)54 CustomerId (org.thingsboard.server.common.data.id.CustomerId)47 Tenant (org.thingsboard.server.common.data.Tenant)45 Customer (org.thingsboard.server.common.data.Customer)43 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)34 ApiOperation (io.swagger.annotations.ApiOperation)33 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)33 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)33 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)33 Edge (org.thingsboard.server.common.data.edge.Edge)32 Device (org.thingsboard.server.common.data.Device)28 PageData (org.thingsboard.server.common.data.page.PageData)28 Matchers.containsString (org.hamcrest.Matchers.containsString)22 UUID (java.util.UUID)20 Asset (org.thingsboard.server.common.data.asset.Asset)20 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)20