Search in sources :

Example 26 with DeviceClient

use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method registerDeviceWithIncorrectCredentialsBeforeOpenThrowsOnOpen.

// Before opening the multiplexed connection, register a single device with incorrect credentials. Opening the client
// should throw and the thrown exception should have details on why the open failed
@ContinuousIntegrationTest
@Test
public void registerDeviceWithIncorrectCredentialsBeforeOpenThrowsOnOpen() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    testInstance.multiplexingClient.unregisterDeviceClient(testInstance.deviceClientArray.get(0));
    // Get a valid connection string, but swap out the deviceId for a deviceId that does exist, but whose symmetric key is different
    String incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(1)).replace(testInstance.deviceIdentityArray.get(1).getDeviceId(), testInstance.deviceIdentityArray.get(0).getDeviceId());
    DeviceClient clientWithIncorrectCredentials = new DeviceClient(incorrectConnectionString, testInstance.protocol);
    testInstance.multiplexingClient.registerDeviceClient(clientWithIncorrectCredentials);
    boolean expectedExceptionThrown = false;
    try {
        testInstance.multiplexingClient.open();
    } catch (MultiplexingClientDeviceRegistrationAuthenticationException e) {
        Map<String, Exception> registrationExceptions = e.getRegistrationExceptions();
        assertEquals(1, registrationExceptions.size());
        String deviceId = testInstance.deviceIdentityArray.get(0).getDeviceId();
        assertTrue(registrationExceptions.containsKey(deviceId));
        assertTrue(registrationExceptions.get(deviceId) instanceof UnauthorizedException);
        expectedExceptionThrown = true;
    }
    testInstance.multiplexingClient.close();
    assertTrue("Expected exception was not thrown", expectedExceptionThrown);
}
Also used : DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) MultiplexingClientDeviceRegistrationAuthenticationException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException) UnauthorizedException(com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Map(java.util.Map) HashMap(java.util.HashMap) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 27 with DeviceClient

use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method registerDeviceWithIncorrectCredentialsAfterOpenThrows.

// Attempt to register a single device with the wrong connection string. The thrown exception
// should contain all the exceptions thrown by the service.
@ContinuousIntegrationTest
@Test
public void registerDeviceWithIncorrectCredentialsAfterOpenThrows() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    testInstance.multiplexingClient.unregisterDeviceClient(testInstance.deviceClientArray.get(0));
    testInstance.multiplexingClient.open();
    // Get a valid connection string, but swap out the deviceId for a deviceId that does exist, but whose symmetric key is different
    String incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(1)).replace(testInstance.deviceIdentityArray.get(1).getDeviceId(), testInstance.deviceIdentityArray.get(0).getDeviceId());
    DeviceClient clientWithIncorrectCredentials = new DeviceClient(incorrectConnectionString, testInstance.protocol);
    boolean expectedExceptionThrown = false;
    try {
        testInstance.multiplexingClient.registerDeviceClient(clientWithIncorrectCredentials);
    } catch (MultiplexingClientDeviceRegistrationAuthenticationException e) {
        Map<String, Exception> registrationExceptions = e.getRegistrationExceptions();
        assertEquals(1, registrationExceptions.size());
        String deviceId = testInstance.deviceIdentityArray.get(0).getDeviceId();
        assertTrue(registrationExceptions.containsKey(deviceId));
        assertTrue(registrationExceptions.get(deviceId) instanceof UnauthorizedException);
        expectedExceptionThrown = true;
    }
    testInstance.multiplexingClient.close();
    assertTrue("Expected exception was not thrown", expectedExceptionThrown);
}
Also used : DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) MultiplexingClientDeviceRegistrationAuthenticationException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException) UnauthorizedException(com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Map(java.util.Map) HashMap(java.util.HashMap) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 28 with DeviceClient

use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method failedRegistrationDoesNotAffectSubsequentRegistrations.

@ContinuousIntegrationTest
@Test
public void failedRegistrationDoesNotAffectSubsequentRegistrations() throws Exception {
    testInstance.setup(0);
    testInstance.multiplexingClient.open();
    TestDeviceIdentity testDeviceIdentity = Tools.getTestDevice(iotHubConnectionString, this.testInstance.protocol, AuthenticationType.SAS, false);
    String deviceConnectionString = registryManager.getDeviceConnectionString(testDeviceIdentity.getDevice());
    String deviceNotFoundConnectionString = deviceConnectionString.replace(testDeviceIdentity.getDeviceId(), testDeviceIdentity.getDeviceId().toUpperCase());
    DeviceClient validDeviceClient = new DeviceClient(deviceConnectionString, testInstance.protocol);
    DeviceClient invalidDeviceClient = new DeviceClient(deviceNotFoundConnectionString, testInstance.protocol);
    try {
        testInstance.multiplexingClient.registerDeviceClient(invalidDeviceClient);
        fail("Expected multiplexingClient to throw since it registered a device that did not exist.");
    } catch (MultiplexingClientDeviceRegistrationAuthenticationException e) {
    // expected throw since the deviceId in the connection string does not exist, ignore
    }
    testInstance.multiplexingClient.registerDeviceClient(validDeviceClient);
    testSendingMessageFromDeviceClient(validDeviceClient);
    testInstance.multiplexingClient.close();
}
Also used : DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) MultiplexingClientDeviceRegistrationAuthenticationException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) TestDeviceIdentity(tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 29 with DeviceClient

use of com.microsoft.azure.sdk.iot.device.DeviceClient 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 30 with DeviceClient

use of com.microsoft.azure.sdk.iot.device.DeviceClient 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

DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)38 Test (org.junit.Test)21 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)16 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)14 Device (com.microsoft.azure.sdk.iot.service.Device)10 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)10 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 IotHubClientProtocol (com.microsoft.azure.sdk.iot.device.IotHubClientProtocol)6 URISyntaxException (java.net.URISyntaxException)6 Message (com.microsoft.azure.sdk.iot.device.Message)5 MultiplexingClientDeviceRegistrationAuthenticationException (com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException)5 ModuleClient (com.microsoft.azure.sdk.iot.device.ModuleClient)4 UnauthorizedException (com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException)4 Property (com.microsoft.azure.sdk.iot.device.DeviceTwin.Property)3 MultiplexingClient (com.microsoft.azure.sdk.iot.device.MultiplexingClient)3 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)3 DigitalTwinGetHeaders (com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders)3 BasicDigitalTwin (com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin)3