Search in sources :

Example 66 with Device

use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.

the class RegistryManagerTests method crud_module_e2e.

@Test
@StandardTierHubOnlyTest
public void crud_module_e2e() throws Exception {
    // Arrange
    RegistryManagerTestInstance testInstance = new RegistryManagerTestInstance();
    SymmetricKey expectedSymmetricKey = new SymmetricKey();
    Device deviceSetup = Device.createFromId(testInstance.deviceId, DeviceStatus.Enabled, null);
    Tools.addDeviceWithRetry(testInstance.registryManager, deviceSetup);
    deleteModuleIfItExistsAlready(testInstance.registryManager, testInstance.deviceId, testInstance.moduleId);
    // -Create-//
    Module moduleAdded = Module.createFromId(testInstance.deviceId, testInstance.moduleId, null);
    Tools.addModuleWithRetry(testInstance.registryManager, moduleAdded);
    // -Read-//
    Module moduleRetrieved = testInstance.registryManager.getModule(testInstance.deviceId, testInstance.moduleId);
    // -Update-//
    Module moduleUpdated = testInstance.registryManager.getModule(testInstance.deviceId, testInstance.moduleId);
    moduleUpdated.getSymmetricKey().setPrimaryKeyFinal(expectedSymmetricKey.getPrimaryKey());
    moduleUpdated = testInstance.registryManager.updateModule(moduleUpdated);
    // -Delete-//
    testInstance.registryManager.removeModule(testInstance.deviceId, testInstance.moduleId);
    testInstance.registryManager.removeDevice(testInstance.deviceId);
    // Assert
    assertEquals(buildExceptionMessage("", hostName), testInstance.deviceId, moduleAdded.getDeviceId());
    assertEquals(buildExceptionMessage("", hostName), testInstance.moduleId, moduleAdded.getId());
    assertEquals(buildExceptionMessage("", hostName), testInstance.deviceId, moduleRetrieved.getDeviceId());
    assertEquals(buildExceptionMessage("", hostName), testInstance.moduleId, moduleRetrieved.getId());
    assertEquals(buildExceptionMessage("", hostName), expectedSymmetricKey.getPrimaryKey(), moduleUpdated.getPrimaryKey());
    assertTrue(buildExceptionMessage("", hostName), moduleWasDeletedSuccessfully(testInstance.registryManager, testInstance.deviceId, testInstance.moduleId));
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) SymmetricKey(com.microsoft.azure.sdk.iot.service.auth.SymmetricKey) Module(com.microsoft.azure.sdk.iot.service.Module) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test)

Example 67 with Device

use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.

the class TokenCredentialTests method cloudToDeviceTelemetryWithTokenCredential.

@Test
public void cloudToDeviceTelemetryWithTokenCredential() throws Exception {
    // only run tests for standard tier hubs
    Assume.assumeFalse(isBasicTierHub);
    // We remove and recreate the device for a clean start
    RegistryManager registryManager = new RegistryManager(iotHubConnectionString);
    Device device = Device.createDevice("some-device-" + UUID.randomUUID(), AuthenticationType.SAS);
    registryManager.addDevice(device);
    Device deviceGetBefore = registryManager.getDevice(device.getDeviceId());
    // Create service client
    ServiceClient serviceClient = buildServiceClientWithTokenCredential(IotHubServiceClientProtocol.AMQPS);
    serviceClient.open();
    Message message = new Message("some message".getBytes(StandardCharsets.UTF_8));
    serviceClient.send(device.getDeviceId(), message);
    Device deviceGetAfter = registryManager.getDevice(device.getDeviceId());
    serviceClient.close();
    registryManager.removeDevice(device.getDeviceId());
    // Assert
    assertEquals(0, deviceGetBefore.getCloudToDeviceMessageCount());
    assertEquals(1, deviceGetAfter.getCloudToDeviceMessageCount());
    registryManager.close();
}
Also used : Message(com.microsoft.azure.sdk.iot.service.Message) Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) Test(org.junit.Test)

Example 68 with Device

use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.

the class DeviceManagerSample method GetDevice.

private static void GetDevice() throws Exception {
    RegistryManager registryManager = RegistryManager.createFromConnectionString(SampleUtils.iotHubConnectionString);
    Device returnDevice;
    try {
        returnDevice = registryManager.getDevice(SampleUtils.deviceId);
        System.out.println("Device: " + returnDevice.getDeviceId());
        System.out.println("Device primary key: " + returnDevice.getPrimaryKey());
        System.out.println("Device secondary key: " + returnDevice.getSecondaryKey());
        System.out.println("Device ETag: " + returnDevice.geteTag());
    } catch (IotHubException | IOException iote) {
        iote.printStackTrace();
    }
    registryManager.close();
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)

Aggregations

Device (com.microsoft.azure.sdk.iot.service.Device)68 Test (org.junit.Test)46 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)21 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)19 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)17 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)15 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)13 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)12 IOException (java.io.IOException)11 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)11 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)11 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)10 ArrayList (java.util.ArrayList)10 SymmetricKey (com.microsoft.azure.sdk.iot.service.auth.SymmetricKey)7 RegistryManagerOptions (com.microsoft.azure.sdk.iot.service.RegistryManagerOptions)5 AzureSasCredential (com.azure.core.credential.AzureSasCredential)4 DeviceCapabilities (com.microsoft.azure.sdk.iot.deps.twin.DeviceCapabilities)4 ClientOptions (com.microsoft.azure.sdk.iot.device.ClientOptions)4 DeviceStatus (com.microsoft.azure.sdk.iot.service.DeviceStatus)4 Module (com.microsoft.azure.sdk.iot.service.Module)4