Search in sources :

Example 46 with Device

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

the class DeviceTest method constructor_sets_status_and_symmetrickey.

// Tests_SRS_SERVICE_SDK_JAVA_DEVICE_28_001: [The constructor shall set the deviceId, status and symmetricKey.]
@Test
public void constructor_sets_status_and_symmetrickey() throws NoSuchAlgorithmException {
    // Arrange
    String deviceId = "xxx-device";
    DeviceStatus expectedDeviceStatus = DeviceStatus.Disabled;
    SymmetricKey expectedSymmetricKey = new SymmetricKey();
    // Act
    Device device = Deencapsulation.newInstance(Device.class, new Class[] { String.class, DeviceStatus.class, SymmetricKey.class }, deviceId, expectedDeviceStatus, expectedSymmetricKey);
    // Assert
    assertEquals(deviceId, device.getDeviceId());
    assertEquals(expectedDeviceStatus, device.getStatus());
    assertEquals(expectedSymmetricKey, device.getSymmetricKey());
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) SymmetricKey(com.microsoft.azure.sdk.iot.service.auth.SymmetricKey) DeviceStatus(com.microsoft.azure.sdk.iot.service.DeviceStatus) Test(org.junit.Test)

Example 47 with Device

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

the class MultiplexingClientTests method disableDeviceAfterOpenBeforeRegister.

// If you register a disabled device to an active multiplexed connection, the other devices on the connection
// should not be affected nor should the multiplexed connection itself.
@ContinuousIntegrationTest
@Test
public void disableDeviceAfterOpenBeforeRegister() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    // Only register the soon-to-be-disabled device after opening the multiplexing client
    testInstance.multiplexingClient.unregisterDeviceClient(testInstance.deviceClientArray.get(0));
    ConnectionStatusChangeTracker multiplexedConnectionStatusChangeTracker = new ConnectionStatusChangeTracker();
    testInstance.multiplexingClient.registerConnectionStatusChangeCallback(multiplexedConnectionStatusChangeTracker, null);
    ConnectionStatusChangeTracker[] connectionStatusChangeTrackers = new ConnectionStatusChangeTracker[DEVICE_MULTIPLEX_COUNT];
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        connectionStatusChangeTrackers[i] = new ConnectionStatusChangeTracker();
        testInstance.deviceClientArray.get(i).registerConnectionStatusChangeCallback(connectionStatusChangeTrackers[i], null);
    }
    testInstance.multiplexingClient.open();
    // Disable a device that will be on the multiplexed connection
    Device deviceToDisable = registryManager.getDevice(testInstance.deviceIdentityArray.get(0).getDeviceId());
    deviceToDisable.setStatus(DeviceStatus.Disabled);
    registryManager.updateDevice(deviceToDisable);
    try {
        try {
            testInstance.multiplexingClient.registerDeviceClient(testInstance.deviceClientArray.get(0));
            fail("Registering a disabled device to an active multiplexing connection should have thrown an exception");
        } catch (MultiplexingClientDeviceRegistrationAuthenticationException ex) {
            assertTrue(ex.getRegistrationExceptions().containsKey(deviceToDisable.getDeviceId()));
        }
        // verify that the disabled device eventually loses its device session
        long startTime = System.currentTimeMillis();
        while (!connectionStatusChangeTrackers[0].wentDisconnectedRetrying) {
            Thread.sleep(200);
            if (System.currentTimeMillis() - startTime > FAULT_INJECTION_TIMEOUT_MILLIS) {
                fail("Timed out waiting for the disabled device's client to report DISCONNECTED_RETRYING");
            }
        }
        assertFalse("Device failed to be registered, but the multiplexing client still reports it as registered", testInstance.multiplexingClient.isDeviceRegistered(deviceToDisable.getDeviceId()));
        for (int i = 1; i < DEVICE_MULTIPLEX_COUNT; i++) {
            assertTrue("One device failed to be registered, but the other devices should still have been registered.", testInstance.multiplexingClient.isDeviceRegistered(testInstance.deviceClientArray.get(i).getConfig().getDeviceId()));
        }
        // Verify that the other devices on the multiplexed connection were unaffected
        for (int i = 1; i < DEVICE_MULTIPLEX_COUNT; i++) {
            assertFalse(connectionStatusChangeTrackers[i].wentDisconnectedRetrying);
            assertTrue(connectionStatusChangeTrackers[i].isOpen);
        }
        // Verify that the multiplexed connection itself was unaffected
        assertFalse(multiplexedConnectionStatusChangeTracker.wentDisconnectedRetrying);
        assertTrue(multiplexedConnectionStatusChangeTracker.isOpen);
        // Verify that the other devices can still send telemetry
        testSendingMessagesFromMultiplexedClients(testInstance.deviceClientArray.subList(1, DEVICE_MULTIPLEX_COUNT));
        testInstance.multiplexingClient.close();
    } finally {
        // re enable the device in case it gets recycled
        deviceToDisable.setStatus(DeviceStatus.Enabled);
        registryManager.updateDevice(deviceToDisable);
    }
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) MultiplexingClientDeviceRegistrationAuthenticationException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 48 with Device

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

the class MultiplexingClientTests method registrationsUnwindForMqttClient.

@Test
public void registrationsUnwindForMqttClient() throws Exception {
    Device mqttDevice = Tools.getTestDevice(iotHubConnectionString, IotHubClientProtocol.MQTT, AuthenticationType.SAS, false).getDevice();
    String deviceConnectionString = registryManager.getDeviceConnectionString(mqttDevice);
    // MQTT clients should throw UnsupportedOperationException when registered
    DeviceClient mqttDeviceClient = new DeviceClient(deviceConnectionString, IotHubClientProtocol.MQTT);
    registrationsUnwindForUnsupportedOperationExceptions(mqttDeviceClient);
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 49 with Device

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

the class MultiplexingClientTests method registrationsUnwindForX509Client.

@Test
public void registrationsUnwindForX509Client() throws Exception {
    // Create a new device client that uses x509 auth, which should throw an UnsupportedOperationException
    // since x509 auth isn't supported while multiplexing
    Device x509Device = Tools.getTestDevice(iotHubConnectionString, IotHubClientProtocol.MQTT, AuthenticationType.SELF_SIGNED, false).getDevice();
    String deviceConnectionString = registryManager.getDeviceConnectionString(x509Device);
    DeviceClient x509DeviceClient = new DeviceClient(deviceConnectionString, testInstance.protocol, new IotHubSSLContext().getSSLContext());
    registrationsUnwindForUnsupportedOperationExceptions(x509DeviceClient);
}
Also used : IotHubSSLContext(com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext) Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 50 with Device

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

the class MultiplexingClientTests method registrationsUnwindForAlreadyOpenClient.

@Test
public void registrationsUnwindForAlreadyOpenClient() throws Exception {
    Device nonMultiplexedDevice = Tools.getTestDevice(iotHubConnectionString, testInstance.protocol, AuthenticationType.SAS, false).getDevice();
    String deviceConnectionString = registryManager.getDeviceConnectionString(nonMultiplexedDevice);
    DeviceClient nonMultiplexedDeviceClient = new DeviceClient(deviceConnectionString, testInstance.protocol);
    // By opening the client once, this client can no longer be registered to a multiplexing client
    nonMultiplexedDeviceClient.open();
    registrationsUnwindForUnsupportedOperationExceptions(nonMultiplexedDeviceClient);
    nonMultiplexedDeviceClient.closeNow();
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

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