Search in sources :

Example 51 with PageLink

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

the class BaseUserControllerTest method testFindCustomerUsers.

@Test
public void testFindCustomerUsers() throws Exception {
    loginSysAdmin();
    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<>();
    PageLink pageLink = new PageLink(33);
    PageData<User> pageData = null;
    do {
        pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/users?", new TypeReference<PageData<User>>() {
        }, pageLink);
        loadedCustomerUsers.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(customerUsers, idComparator);
    Collections.sort(loadedCustomerUsers, idComparator);
    Assert.assertEquals(customerUsers, loadedCustomerUsers);
    doDelete("/api/customer/" + customerId.getId().toString()).andExpect(status().isOk());
}
Also used : User(org.thingsboard.server.common.data.User) Customer(org.thingsboard.server.common.data.Customer) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) CustomerId(org.thingsboard.server.common.data.id.CustomerId) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 52 with PageLink

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

the class BaseWidgetsBundleControllerTest method testFindSystemWidgetsBundlesByPageLink.

@Test
public void testFindSystemWidgetsBundlesByPageLink() throws Exception {
    loginSysAdmin();
    List<WidgetsBundle> sysWidgetsBundles = doGetTyped("/api/widgetsBundles?", new TypeReference<List<WidgetsBundle>>() {
    });
    List<WidgetsBundle> createdWidgetsBundles = new ArrayList<>();
    for (int i = 0; i < 120; i++) {
        WidgetsBundle widgetsBundle = new WidgetsBundle();
        widgetsBundle.setTitle("Widgets bundle" + i);
        createdWidgetsBundles.add(doPost("/api/widgetsBundle", widgetsBundle, WidgetsBundle.class));
    }
    List<WidgetsBundle> widgetsBundles = new ArrayList<>(createdWidgetsBundles);
    widgetsBundles.addAll(sysWidgetsBundles);
    List<WidgetsBundle> loadedWidgetsBundles = new ArrayList<>();
    PageLink pageLink = new PageLink(14);
    PageData<WidgetsBundle> pageData;
    do {
        pageData = doGetTypedWithPageLink("/api/widgetsBundles?", new TypeReference<PageData<WidgetsBundle>>() {
        }, pageLink);
        loadedWidgetsBundles.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(widgetsBundles, idComparator);
    Collections.sort(loadedWidgetsBundles, idComparator);
    Assert.assertEquals(widgetsBundles, loadedWidgetsBundles);
    for (WidgetsBundle widgetsBundle : createdWidgetsBundles) {
        doDelete("/api/widgetsBundle/" + widgetsBundle.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new PageLink(17);
    loadedWidgetsBundles.clear();
    do {
        pageData = doGetTypedWithPageLink("/api/widgetsBundles?", new TypeReference<PageData<WidgetsBundle>>() {
        }, pageLink);
        loadedWidgetsBundles.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(sysWidgetsBundles, idComparator);
    Collections.sort(loadedWidgetsBundles, idComparator);
    Assert.assertEquals(sysWidgetsBundles, loadedWidgetsBundles);
}
Also used : ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) ArrayList(java.util.ArrayList) List(java.util.List) TypeReference(com.fasterxml.jackson.core.type.TypeReference) WidgetsBundle(org.thingsboard.server.common.data.widget.WidgetsBundle) Test(org.junit.Test)

Example 53 with PageLink

use of org.thingsboard.server.common.data.page.PageLink 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<DeviceInfo> 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(new DeviceInfo(deviceService.assignDeviceToCustomer(tenantId, device.getId(), customerId), customer.getTitle(), customer.isPublic(), "default"));
    }
    List<DeviceInfo> loadedDevices = new ArrayList<>();
    PageLink pageLink = new PageLink(23);
    PageData<DeviceInfo> pageData = null;
    do {
        pageData = deviceService.findDeviceInfosByTenantIdAndCustomerId(tenantId, customerId, pageLink);
        loadedDevices.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devices, idComparator);
    Collections.sort(loadedDevices, idComparator);
    Assert.assertEquals(devices, loadedDevices);
    deviceService.unassignCustomerDevices(tenantId, customerId);
    pageLink = new PageLink(33);
    pageData = deviceService.findDeviceInfosByTenantIdAndCustomerId(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) Device(org.thingsboard.server.common.data.Device) DeviceInfo(org.thingsboard.server.common.data.DeviceInfo) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Test(org.junit.Test)

Example 54 with PageLink

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

the class BaseDeviceServiceTest method testFindDevicesByTenantIdCustomerIdAndType.

@Test
public void testFindDevicesByTenantIdCustomerIdAndType() {
    Customer customer = new Customer();
    customer.setTitle("Test customer");
    customer.setTenantId(tenantId);
    customer = customerService.saveCustomer(customer);
    CustomerId customerId = customer.getId();
    String title1 = "Device title 1";
    String type1 = "typeC";
    List<Device> devicesType1 = new ArrayList<>();
    for (int i = 0; i < 175; i++) {
        Device device = new Device();
        device.setTenantId(tenantId);
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title1 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        device.setName(name);
        device.setType(type1);
        device = deviceService.saveDevice(device);
        devicesType1.add(deviceService.assignDeviceToCustomer(tenantId, device.getId(), customerId));
    }
    String title2 = "Device title 2";
    String type2 = "typeD";
    List<Device> devicesType2 = new ArrayList<>();
    for (int i = 0; i < 143; i++) {
        Device device = new Device();
        device.setTenantId(tenantId);
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title2 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        device.setName(name);
        device.setType(type2);
        device = deviceService.saveDevice(device);
        devicesType2.add(deviceService.assignDeviceToCustomer(tenantId, device.getId(), customerId));
    }
    List<Device> loadedDevicesType1 = new ArrayList<>();
    PageLink pageLink = new PageLink(15);
    PageData<Device> pageData = null;
    do {
        pageData = deviceService.findDevicesByTenantIdAndCustomerIdAndType(tenantId, customerId, type1, pageLink);
        loadedDevicesType1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesType1, idComparator);
    Collections.sort(loadedDevicesType1, idComparator);
    Assert.assertEquals(devicesType1, loadedDevicesType1);
    List<Device> loadedDevicesType2 = new ArrayList<>();
    pageLink = new PageLink(4);
    do {
        pageData = deviceService.findDevicesByTenantIdAndCustomerIdAndType(tenantId, customerId, type2, pageLink);
        loadedDevicesType2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesType2, idComparator);
    Collections.sort(loadedDevicesType2, idComparator);
    Assert.assertEquals(devicesType2, loadedDevicesType2);
    for (Device device : loadedDevicesType1) {
        deviceService.deleteDevice(tenantId, device.getId());
    }
    pageLink = new PageLink(4);
    pageData = deviceService.findDevicesByTenantIdAndCustomerIdAndType(tenantId, customerId, type1, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Device device : loadedDevicesType2) {
        deviceService.deleteDevice(tenantId, device.getId());
    }
    pageLink = new PageLink(4);
    pageData = deviceService.findDevicesByTenantIdAndCustomerIdAndType(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) Device(org.thingsboard.server.common.data.Device) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Test(org.junit.Test)

Example 55 with PageLink

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

the class BaseDeviceServiceTest method testFindDeviceInfoByTenantId.

@Test
public void testFindDeviceInfoByTenantId() {
    Customer customer = new Customer();
    customer.setTitle("Customer X");
    customer.setTenantId(tenantId);
    Customer savedCustomer = customerService.saveCustomer(customer);
    Device device = new Device();
    device.setTenantId(tenantId);
    device.setName("default");
    device.setType("default");
    device.setLabel("label");
    device.setCustomerId(savedCustomer.getId());
    Device savedDevice = deviceService.saveDevice(device);
    PageLink pageLinkWithLabel = new PageLink(100, 0, "label");
    List<DeviceInfo> deviceInfosWithLabel = deviceService.findDeviceInfosByTenantId(tenantId, pageLinkWithLabel).getData();
    Assert.assertFalse(deviceInfosWithLabel.isEmpty());
    Assert.assertTrue(deviceInfosWithLabel.stream().anyMatch(d -> d.getId().equals(savedDevice.getId()) && d.getTenantId().equals(tenantId) && d.getLabel().equals(savedDevice.getLabel())));
    PageLink pageLinkWithCustomer = new PageLink(100, 0, savedCustomer.getSearchText());
    List<DeviceInfo> deviceInfosWithCustomer = deviceService.findDeviceInfosByTenantId(tenantId, pageLinkWithCustomer).getData();
    Assert.assertFalse(deviceInfosWithCustomer.isEmpty());
    Assert.assertTrue(deviceInfosWithCustomer.stream().anyMatch(d -> d.getId().equals(savedDevice.getId()) && d.getTenantId().equals(tenantId) && d.getCustomerId().equals(savedCustomer.getId()) && d.getCustomerTitle().equals(savedCustomer.getTitle())));
    PageLink pageLinkWithType = new PageLink(100, 0, device.getType());
    List<DeviceInfo> deviceInfosWithType = deviceService.findDeviceInfosByTenantId(tenantId, pageLinkWithType).getData();
    Assert.assertFalse(deviceInfosWithType.isEmpty());
    Assert.assertTrue(deviceInfosWithType.stream().anyMatch(d -> d.getId().equals(savedDevice.getId()) && d.getTenantId().equals(tenantId) && d.getType().equals(device.getType())));
}
Also used : DeviceInfo(org.thingsboard.server.common.data.DeviceInfo) Device(org.thingsboard.server.common.data.Device) FIRMWARE(org.thingsboard.server.common.data.ota.OtaPackageType.FIRMWARE) Customer(org.thingsboard.server.common.data.Customer) Tenant(org.thingsboard.server.common.data.Tenant) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration) TenantId(org.thingsboard.server.common.data.id.TenantId) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) ChecksumAlgorithm(org.thingsboard.server.common.data.ota.ChecksumAlgorithm) TenantProfile(org.thingsboard.server.common.data.TenantProfile) Uuids(com.datastax.oss.driver.api.core.uuid.Uuids) After(org.junit.After) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) PageLink(org.thingsboard.server.common.data.page.PageLink) OtaPackage(org.thingsboard.server.common.data.OtaPackage) DeviceCredentialsType(org.thingsboard.server.common.data.security.DeviceCredentialsType) NULL_UUID(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Test(org.junit.Test) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) List(java.util.List) Rule(org.junit.Rule) PageData(org.thingsboard.server.common.data.page.PageData) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype) Assert(org.junit.Assert) Collections(java.util.Collections) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Customer(org.thingsboard.server.common.data.Customer) Device(org.thingsboard.server.common.data.Device) PageLink(org.thingsboard.server.common.data.page.PageLink) DeviceInfo(org.thingsboard.server.common.data.DeviceInfo) Test(org.junit.Test)

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