Search in sources :

Example 6 with Customer

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

the class BaseDeviceServiceTest method testFindDevicesByTenantIdCustomerIdAndName.

@Test
public void testFindDevicesByTenantIdCustomerIdAndName() {
    Customer customer = new Customer();
    customer.setTitle("Test customer");
    customer.setTenantId(tenantId);
    customer = customerService.saveCustomer(customer);
    CustomerId customerId = customer.getId();
    String title1 = "Device title 1";
    List<Device> devicesTitle1 = 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("default");
        device = deviceService.saveDevice(device);
        devicesTitle1.add(deviceService.assignDeviceToCustomer(device.getId(), customerId));
    }
    String title2 = "Device title 2";
    List<Device> devicesTitle2 = 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("default");
        device = deviceService.saveDevice(device);
        devicesTitle2.add(deviceService.assignDeviceToCustomer(device.getId(), customerId));
    }
    List<Device> loadedDevicesTitle1 = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(15, title1);
    TextPageData<Device> pageData = null;
    do {
        pageData = deviceService.findDevicesByTenantIdAndCustomerId(tenantId, customerId, pageLink);
        loadedDevicesTitle1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesTitle1, idComparator);
    Collections.sort(loadedDevicesTitle1, idComparator);
    Assert.assertEquals(devicesTitle1, loadedDevicesTitle1);
    List<Device> loadedDevicesTitle2 = new ArrayList<>();
    pageLink = new TextPageLink(4, title2);
    do {
        pageData = deviceService.findDevicesByTenantIdAndCustomerId(tenantId, customerId, pageLink);
        loadedDevicesTitle2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesTitle2, idComparator);
    Collections.sort(loadedDevicesTitle2, idComparator);
    Assert.assertEquals(devicesTitle2, loadedDevicesTitle2);
    for (Device device : loadedDevicesTitle1) {
        deviceService.deleteDevice(device.getId());
    }
    pageLink = new TextPageLink(4, title1);
    pageData = deviceService.findDevicesByTenantIdAndCustomerId(tenantId, customerId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Device device : loadedDevicesTitle2) {
        deviceService.deleteDevice(device.getId());
    }
    pageLink = new TextPageLink(4, title2);
    pageData = deviceService.findDevicesByTenantIdAndCustomerId(tenantId, customerId, 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) 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 Customer

use of org.thingsboard.server.common.data.Customer 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(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(device.getId(), customerId));
    }
    List<Device> loadedDevicesType1 = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(15);
    TextPageData<Device> pageData = null;
    do {
        pageData = deviceService.findDevicesByTenantIdAndCustomerIdAndType(tenantId, customerId, type1, pageLink);
        loadedDevicesType1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesType1, idComparator);
    Collections.sort(loadedDevicesType1, idComparator);
    Assert.assertEquals(devicesType1, loadedDevicesType1);
    List<Device> loadedDevicesType2 = new ArrayList<>();
    pageLink = new TextPageLink(4);
    do {
        pageData = deviceService.findDevicesByTenantIdAndCustomerIdAndType(tenantId, customerId, type2, pageLink);
        loadedDevicesType2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesType2, idComparator);
    Collections.sort(loadedDevicesType2, idComparator);
    Assert.assertEquals(devicesType2, loadedDevicesType2);
    for (Device device : loadedDevicesType1) {
        deviceService.deleteDevice(device.getId());
    }
    pageLink = new TextPageLink(4);
    pageData = deviceService.findDevicesByTenantIdAndCustomerIdAndType(tenantId, customerId, type1, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Device device : loadedDevicesType2) {
        deviceService.deleteDevice(device.getId());
    }
    pageLink = new TextPageLink(4);
    pageData = deviceService.findDevicesByTenantIdAndCustomerIdAndType(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) Device(org.thingsboard.server.common.data.Device) ArrayList(java.util.ArrayList) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Test(org.junit.Test)

Example 8 with Customer

use of org.thingsboard.server.common.data.Customer 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 9 with Customer

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

the class BaseDeviceServiceTest method testAssignDeviceToCustomerFromDifferentTenant.

@Test(expected = DataValidationException.class)
public void testAssignDeviceToCustomerFromDifferentTenant() {
    Device device = new Device();
    device.setName("My device");
    device.setType("default");
    device.setTenantId(tenantId);
    device = deviceService.saveDevice(device);
    Tenant tenant = new Tenant();
    tenant.setTitle("Test different tenant");
    tenant = tenantService.saveTenant(tenant);
    Customer customer = new Customer();
    customer.setTenantId(tenant.getId());
    customer.setTitle("Test different customer");
    customer = customerService.saveCustomer(customer);
    try {
        deviceService.assignDeviceToCustomer(device.getId(), customer.getId());
    } finally {
        deviceService.deleteDevice(device.getId());
        tenantService.deleteTenant(tenant.getId());
    }
}
Also used : Tenant(org.thingsboard.server.common.data.Tenant) Customer(org.thingsboard.server.common.data.Customer) Device(org.thingsboard.server.common.data.Device) Test(org.junit.Test)

Example 10 with Customer

use of org.thingsboard.server.common.data.Customer 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)

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