Search in sources :

Example 1 with Device

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

the class BaseDeviceCredentialsServiceTest method testSaveDeviceCredentialsWithEmptyCredentialsId.

@Test(expected = DataValidationException.class)
public void testSaveDeviceCredentialsWithEmptyCredentialsId() {
    Device device = new Device();
    device.setName("My device");
    device.setType("default");
    device.setTenantId(tenantId);
    device = deviceService.saveDevice(device);
    DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(device.getId());
    deviceCredentials.setCredentialsId(null);
    try {
        deviceCredentialsService.updateDeviceCredentials(deviceCredentials);
    } finally {
        deviceService.deleteDevice(device.getId());
    }
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Test(org.junit.Test)

Example 2 with Device

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

the class BaseDeviceCredentialsServiceTest method testSaveDeviceCredentials.

@Test
public void testSaveDeviceCredentials() {
    Device device = new Device();
    device.setTenantId(tenantId);
    device.setName("My device");
    device.setType("default");
    Device savedDevice = deviceService.saveDevice(device);
    DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedDevice.getId());
    Assert.assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId());
    deviceCredentials.setCredentialsType(DeviceCredentialsType.ACCESS_TOKEN);
    deviceCredentials.setCredentialsId("access_token");
    deviceCredentialsService.updateDeviceCredentials(deviceCredentials);
    DeviceCredentials foundDeviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedDevice.getId());
    Assert.assertEquals(deviceCredentials, foundDeviceCredentials);
    deviceService.deleteDevice(savedDevice.getId());
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Test(org.junit.Test)

Example 3 with Device

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

the class BaseDeviceCredentialsServiceTest method testSaveDeviceCredentialsWithEmptyDevice.

@Test(expected = DataValidationException.class)
public void testSaveDeviceCredentialsWithEmptyDevice() {
    Device device = new Device();
    device.setName("My device");
    device.setType("default");
    device.setTenantId(tenantId);
    device = deviceService.saveDevice(device);
    DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(device.getId());
    deviceCredentials.setDeviceId(null);
    try {
        deviceCredentialsService.updateDeviceCredentials(deviceCredentials);
    } finally {
        deviceService.deleteDevice(device.getId());
    }
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Test(org.junit.Test)

Example 4 with Device

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

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

the class BaseDeviceServiceTest method testAssignDeviceToNonExistentCustomer.

@Test(expected = DataValidationException.class)
public void testAssignDeviceToNonExistentCustomer() {
    Device device = new Device();
    device.setName("My device");
    device.setType("default");
    device.setTenantId(tenantId);
    device = deviceService.saveDevice(device);
    try {
        deviceService.assignDeviceToCustomer(device.getId(), new CustomerId(UUIDs.timeBased()));
    } finally {
        deviceService.deleteDevice(device.getId());
    }
}
Also used : Device(org.thingsboard.server.common.data.Device) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Test(org.junit.Test)

Aggregations

Device (org.thingsboard.server.common.data.Device)53 Test (org.junit.Test)31 DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)18 ArrayList (java.util.ArrayList)11 DeviceId (org.thingsboard.server.common.data.id.DeviceId)11 CustomerId (org.thingsboard.server.common.data.id.CustomerId)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 Customer (org.thingsboard.server.common.data.Customer)7 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)7 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)7 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)7 TenantId (org.thingsboard.server.common.data.id.TenantId)6 UUID (java.util.UUID)5 AbstractControllerTest (org.thingsboard.server.controller.AbstractControllerTest)4 AbstractJpaDaoTest (org.thingsboard.server.dao.AbstractJpaDaoTest)4 Tenant (org.thingsboard.server.common.data.Tenant)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 List (java.util.List)2 Before (org.junit.Before)2