Search in sources :

Example 31 with DeviceCredentials

use of org.thingsboard.server.common.data.security.DeviceCredentials in project thingsboard by thingsboard.

the class BaseDeviceCredentialsServiceTest method testSaveNonExistentDeviceCredentials.

@Test(expected = DataValidationException.class)
public void testSaveNonExistentDeviceCredentials() {
    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 newDeviceCredentials = new DeviceCredentials(new DeviceCredentialsId(UUIDs.timeBased()));
    newDeviceCredentials.setCreatedTime(deviceCredentials.getCreatedTime());
    newDeviceCredentials.setDeviceId(deviceCredentials.getDeviceId());
    newDeviceCredentials.setCredentialsType(deviceCredentials.getCredentialsType());
    newDeviceCredentials.setCredentialsId(deviceCredentials.getCredentialsId());
    try {
        deviceCredentialsService.updateDeviceCredentials(newDeviceCredentials);
    } finally {
        deviceService.deleteDevice(device.getId());
    }
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceCredentialsId(org.thingsboard.server.common.data.id.DeviceCredentialsId) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Test(org.junit.Test)

Example 32 with DeviceCredentials

use of org.thingsboard.server.common.data.security.DeviceCredentials in project thingsboard by thingsboard.

the class BaseDeviceServiceTest method testDeleteDevice.

@Test
public void testDeleteDevice() {
    Device device = new Device();
    device.setTenantId(tenantId);
    device.setName("My device");
    device.setType("default");
    Device savedDevice = deviceService.saveDevice(device);
    Device foundDevice = deviceService.findDeviceById(savedDevice.getId());
    Assert.assertNotNull(foundDevice);
    deviceService.deleteDevice(savedDevice.getId());
    foundDevice = deviceService.findDeviceById(savedDevice.getId());
    Assert.assertNull(foundDevice);
    DeviceCredentials foundDeviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedDevice.getId());
    Assert.assertNull(foundDeviceCredentials);
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Test(org.junit.Test)

Example 33 with DeviceCredentials

use of org.thingsboard.server.common.data.security.DeviceCredentials in project thingsboard by thingsboard.

the class BaseDeviceControllerTest method testSaveDeviceCredentialsWithEmptyCredentialsType.

@Test
public void testSaveDeviceCredentialsWithEmptyCredentialsType() throws Exception {
    Device device = new Device();
    device.setName("My device");
    device.setType("default");
    Device savedDevice = doPost("/api/device", device, Device.class);
    DeviceCredentials deviceCredentials = doGet("/api/device/" + savedDevice.getId().getId().toString() + "/credentials", DeviceCredentials.class);
    deviceCredentials.setCredentialsType(null);
    doPost("/api/device/credentials", deviceCredentials).andExpect(status().isBadRequest()).andExpect(statusReason(containsString("Device credentials type should be specified")));
}
Also used : DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Test(org.junit.Test)

Example 34 with DeviceCredentials

use of org.thingsboard.server.common.data.security.DeviceCredentials in project thingsboard by thingsboard.

the class BaseDeviceControllerTest method testSaveDevice.

@Test
public void testSaveDevice() throws Exception {
    Device device = new Device();
    device.setName("My device");
    device.setType("default");
    Device savedDevice = doPost("/api/device", device, Device.class);
    Assert.assertNotNull(savedDevice);
    Assert.assertNotNull(savedDevice.getId());
    Assert.assertTrue(savedDevice.getCreatedTime() > 0);
    Assert.assertEquals(savedTenant.getId(), savedDevice.getTenantId());
    Assert.assertNotNull(savedDevice.getCustomerId());
    Assert.assertEquals(NULL_UUID, savedDevice.getCustomerId().getId());
    Assert.assertEquals(device.getName(), savedDevice.getName());
    DeviceCredentials deviceCredentials = doGet("/api/device/" + savedDevice.getId().getId().toString() + "/credentials", DeviceCredentials.class);
    Assert.assertNotNull(deviceCredentials);
    Assert.assertNotNull(deviceCredentials.getId());
    Assert.assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId());
    Assert.assertEquals(DeviceCredentialsType.ACCESS_TOKEN, deviceCredentials.getCredentialsType());
    Assert.assertNotNull(deviceCredentials.getCredentialsId());
    Assert.assertEquals(20, deviceCredentials.getCredentialsId().length());
    savedDevice.setName("My new device");
    doPost("/api/device", savedDevice, Device.class);
    Device foundDevice = doGet("/api/device/" + savedDevice.getId().getId().toString(), Device.class);
    Assert.assertEquals(foundDevice.getName(), savedDevice.getName());
}
Also used : DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Test(org.junit.Test)

Example 35 with DeviceCredentials

use of org.thingsboard.server.common.data.security.DeviceCredentials in project thingsboard by thingsboard.

the class BaseDeviceControllerTest method testSaveDeviceCredentialsWithEmptyCredentialsId.

@Test
public void testSaveDeviceCredentialsWithEmptyCredentialsId() throws Exception {
    Device device = new Device();
    device.setName("My device");
    device.setType("default");
    Device savedDevice = doPost("/api/device", device, Device.class);
    DeviceCredentials deviceCredentials = doGet("/api/device/" + savedDevice.getId().getId().toString() + "/credentials", DeviceCredentials.class);
    deviceCredentials.setCredentialsId(null);
    doPost("/api/device/credentials", deviceCredentials).andExpect(status().isBadRequest()).andExpect(statusReason(containsString("Device credentials id should be specified")));
}
Also used : DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Test(org.junit.Test)

Aggregations

DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)39 Test (org.junit.Test)21 Device (org.thingsboard.server.common.data.Device)18 DeviceId (org.thingsboard.server.common.data.id.DeviceId)7 DeviceCredentialsId (org.thingsboard.server.common.data.id.DeviceCredentialsId)6 AbstractControllerTest (org.thingsboard.server.controller.AbstractControllerTest)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)2 Before (org.junit.Before)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 AbstractJpaDaoTest (org.thingsboard.server.dao.AbstractJpaDaoTest)2 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)2 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 UUID (java.util.UUID)1 Cache (org.springframework.cache.Cache)1 CacheEvict (org.springframework.cache.annotation.CacheEvict)1 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)1 DeviceCredentialsService (org.thingsboard.server.dao.device.DeviceCredentialsService)1