Search in sources :

Example 6 with MultiplexingClientDeviceRegistrationAuthenticationException

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

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

Aggregations

MultiplexingClientDeviceRegistrationAuthenticationException (com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException)7 Test (org.junit.Test)6 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)6 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)5 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)5 UnauthorizedException (com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ArrayList (java.util.ArrayList)2 MultiplexingClientException (com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientException)1 MultiplexingDeviceUnauthorizedException (com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingDeviceUnauthorizedException)1 TransportException (com.microsoft.azure.sdk.iot.device.exceptions.TransportException)1 Device (com.microsoft.azure.sdk.iot.service.Device)1 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)1 TestDeviceIdentity (tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity)1