Search in sources :

Example 16 with DeviceCredentials

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

the class AbstractMqttServerSideRpcIntegrationTest method testServerMqttTwoWayRpc.

@Test
public void testServerMqttTwoWayRpc() throws Exception {
    Device device = new Device();
    device.setName("Test Two-Way Server-Side RPC");
    device.setType("default");
    Device savedDevice = getSavedDevice(device);
    DeviceCredentials deviceCredentials = getDeviceCredentials(savedDevice);
    assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId());
    String accessToken = deviceCredentials.getCredentialsId();
    assertNotNull(accessToken);
    String clientId = MqttAsyncClient.generateClientId();
    MqttAsyncClient client = new MqttAsyncClient(MQTT_URL, clientId);
    MqttConnectOptions options = new MqttConnectOptions();
    options.setUserName(accessToken);
    client.connect(options).waitForCompletion();
    client.subscribe("v1/devices/me/rpc/request/+", 1);
    client.setCallback(new TestMqttCallback(client));
    String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}";
    String deviceId = savedDevice.getId().getId().toString();
    String result = doPostAsync("/api/plugins/rpc/twoway/" + deviceId, setGpioRequest, String.class, status().isOk());
    Assert.assertEquals("{\"value1\":\"A\",\"value2\":\"B\"}", result);
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest)

Example 17 with DeviceCredentials

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

the class BaseHttpDeviceApiTest method before.

@Before
public void before() throws Exception {
    loginTenantAdmin();
    device = new Device();
    device.setName("My device");
    device.setType("default");
    device = doPost("/api/device", device, Device.class);
    deviceCredentials = doGet("/api/device/" + device.getId().getId().toString() + "/credentials", DeviceCredentials.class);
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Before(org.junit.Before)

Example 18 with DeviceCredentials

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

the class JpaDeviceCredentialsDaoTest method findByCredentialsId.

@Test
@DatabaseSetup("classpath:dbunit/device_credentials.xml")
public void findByCredentialsId() {
    String credentialsId = "ID_2";
    DeviceCredentials deviceCredentials = deviceCredentialsDao.findByCredentialsId(credentialsId);
    assertNotNull(deviceCredentials);
    assertEquals("958e3c74-3215-11e7-93ae-92361f002671", deviceCredentials.getId().getId().toString());
}
Also used : DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test) DatabaseSetup(com.github.springtestdbunit.annotation.DatabaseSetup)

Example 19 with DeviceCredentials

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

the class JpaDeviceCredentialsDaoTest method testFindByDeviceId.

@Test
@DatabaseSetup("classpath:dbunit/device_credentials.xml")
public void testFindByDeviceId() {
    UUID deviceId = UUID.fromString("958e3a30-3215-11e7-93ae-92361f002671");
    DeviceCredentials deviceCredentials = deviceCredentialsDao.findByDeviceId(deviceId);
    assertNotNull(deviceCredentials);
    assertEquals("958e3314-3215-11e7-93ae-92361f002671", deviceCredentials.getId().getId().toString());
    assertEquals("ID_1", deviceCredentials.getCredentialsId());
}
Also used : UUID(java.util.UUID) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test) DatabaseSetup(com.github.springtestdbunit.annotation.DatabaseSetup)

Example 20 with DeviceCredentials

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

the class DefaultSystemDataLoaderService method createDevice.

private Device createDevice(TenantId tenantId, CustomerId customerId, String type, String name, String accessToken, String description) {
    Device device = new Device();
    device.setTenantId(tenantId);
    device.setCustomerId(customerId);
    device.setType(type);
    device.setName(name);
    if (description != null) {
        ObjectNode additionalInfo = objectMapper.createObjectNode();
        additionalInfo.put("description", description);
        device.setAdditionalInfo(additionalInfo);
    }
    device = deviceService.saveDevice(device);
    DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(device.getId());
    deviceCredentials.setCredentialsId(accessToken);
    deviceCredentialsService.updateDeviceCredentials(deviceCredentials);
    return device;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials)

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