Search in sources :

Example 16 with DeviceClient

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

the class MultiplexingClientTests method registrationsUnwindForClientOfDifferentHostName.

@Test
public void registrationsUnwindForClientOfDifferentHostName() throws Exception {
    Device nonMultiplexedDevice = Tools.getTestDevice(iotHubConnectionString, testInstance.protocol, AuthenticationType.SAS, false).getDevice();
    String deviceConnectionString = registryManager.getDeviceConnectionString(nonMultiplexedDevice);
    // intentionally change the hostname of the device to simulate registering a device with a different hostname
    // to a multiplexing client. It shouldn't matter that the hostname itself isn't tied to an actual IoT Hub since
    // no network requests should be made before this hostname validation.
    String actualHostName = IotHubConnectionString.createConnectionString(iotHubConnectionString).getHostName();
    deviceConnectionString = deviceConnectionString.replace(actualHostName, "some-fake-host-name.azure-devices.net");
    DeviceClient deviceClientWithDifferentHostName = new DeviceClient(deviceConnectionString, testInstance.protocol);
    registrationsUnwindForUnsupportedOperationExceptions(deviceClientWithDifferentHostName);
}
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 17 with DeviceClient

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

the class MultiplexingClientTests method registrationsUnwindForDifferentProtocolClient.

@Test
public void registrationsUnwindForDifferentProtocolClient() throws Exception {
    // Protocol for the new client is AMQPS if the test parameters are for AMQPS_WS, and vice versa. MultiplexingClient
    // should throw an exception since this new client's protocol doesn't match, even though both AMQPS and AMQPS_WS are valid
    // protocols
    IotHubClientProtocol protocol = testInstance.protocol == IotHubClientProtocol.AMQPS ? IotHubClientProtocol.AMQPS_WS : IotHubClientProtocol.AMQPS;
    Device newDevice = Tools.getTestDevice(iotHubConnectionString, protocol, AuthenticationType.SAS, false).getDevice();
    String deviceConnectionString = registryManager.getDeviceConnectionString(newDevice);
    DeviceClient differentProtocolDeviceClient = new DeviceClient(deviceConnectionString, protocol);
    registrationsUnwindForUnsupportedOperationExceptions(differentProtocolDeviceClient);
}
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) IotHubClientProtocol(com.microsoft.azure.sdk.iot.device.IotHubClientProtocol) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 18 with DeviceClient

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

the class MultiplexingClientTests method registerDevicesWithIncorrectCredentialsBeforeOpenThrowsOnOpen.

@ContinuousIntegrationTest
@Test
public void registerDevicesWithIncorrectCredentialsBeforeOpenThrowsOnOpen() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        testInstance.multiplexingClient.unregisterDeviceClient(testInstance.deviceClientArray.get(i));
    }
    List<DeviceClient> clientsWithIncorrectCredentials = new ArrayList<>();
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        // shift the keys for each device so that device n uses key for device n + 1 (and final device uses key for device 0)
        String incorrectConnectionString;
        if (i == DEVICE_MULTIPLEX_COUNT - 1) {
            incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(0));
            incorrectConnectionString = incorrectConnectionString.replace(testInstance.deviceIdentityArray.get(0).getDeviceId(), testInstance.deviceIdentityArray.get(i).getDeviceId());
        } else {
            incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(i + 1));
            incorrectConnectionString = incorrectConnectionString.replace(testInstance.deviceIdentityArray.get(i + 1).getDeviceId(), testInstance.deviceIdentityArray.get(i).getDeviceId());
        }
        DeviceClient clientWithIncorrectCredentials = new DeviceClient(incorrectConnectionString, testInstance.protocol);
        clientsWithIncorrectCredentials.add(clientWithIncorrectCredentials);
    }
    testInstance.multiplexingClient.registerDeviceClients(clientsWithIncorrectCredentials);
    boolean expectedExceptionThrown = false;
    try {
        testInstance.multiplexingClient.open();
    } catch (MultiplexingClientDeviceRegistrationAuthenticationException e) {
        Map<String, Exception> registrationExceptions = e.getRegistrationExceptions();
        assertEquals(DEVICE_MULTIPLEX_COUNT, registrationExceptions.size());
        for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
            String deviceId = testInstance.deviceIdentityArray.get(i).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) ArrayList(java.util.ArrayList) 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 19 with DeviceClient

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

the class Tools method getSasTestDevice.

private static TestDeviceIdentity getSasTestDevice(String iotHubConnectionString, IotHubClientProtocol protocol, boolean needCleanTwin) throws URISyntaxException, IOException, IotHubException, GeneralSecurityException {
    // and the subsequent callers just used one of those devices
    synchronized (testSasDeviceQueueLock) {
        TestDeviceIdentity testDeviceIdentity;
        if (!needCleanTwin && testSasDeviceWithTwinQueue.size() > 0) {
            log.debug("Acquiring test device from testSasDeviceWithTwinQueue");
            testDeviceIdentity = testSasDeviceWithTwinQueue.remove();
        } else {
            if (testSasDeviceQueue.size() < 1) {
                // No cached devices to return, so create a new set of devices to cache, and return one of the newly created devices
                log.debug("Proactively adding another {} devices to the SAS test device queue", PROACTIVE_TEST_DEVICE_REGISRATION_COUNT);
                List<Device> devicesToAdd = new ArrayList<>();
                for (int i = 0; i < PROACTIVE_TEST_DEVICE_REGISRATION_COUNT; i++) {
                    Device deviceToAdd = Device.createDevice("test-device-" + UUID.randomUUID().toString(), AuthenticationType.SAS);
                    deviceToAdd.setSymmetricKey(new SymmetricKey());
                    devicesToAdd.add(deviceToAdd);
                }
                addDevices(devicesToAdd, iotHubConnectionString);
                for (int i = 0; i < PROACTIVE_TEST_DEVICE_REGISRATION_COUNT; i++) {
                    testSasDeviceQueue.add(new TestDeviceIdentity(null, devicesToAdd.get(i)));
                }
            }
            log.debug("Acquiring test device from testSasDeviceQueue");
            testDeviceIdentity = testSasDeviceQueue.remove();
        }
        testDeviceIdentity.setDeviceClient(new DeviceClient(getRegistyManager(iotHubConnectionString).getDeviceConnectionString(testDeviceIdentity.getDevice()), protocol));
        return testDeviceIdentity;
    }
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) ArrayList(java.util.ArrayList) SymmetricKey(com.microsoft.azure.sdk.iot.service.auth.SymmetricKey)

Example 20 with DeviceClient

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

the class SendMessagesIT method SendMessagesOverMqtt.

@Test
public void SendMessagesOverMqtt() throws URISyntaxException, IOException {
    String messageString = "Java client e2e test message over Mqtt protocol";
    Message msg = new Message(messageString);
    DeviceClient client = new DeviceClient(DeviceConnectionString.get(iotHubConnectionString, deviceMqtt), IotHubClientProtocol.MQTT);
    client.open();
    for (int i = 0; i < NUM_MESSAGES_PER_CONNECTION; ++i) {
        try {
            Success messageSent = new Success();
            EventCallback callback = new EventCallback();
            client.sendEventAsync(msg, callback, messageSent);
            Integer waitDuration = 0;
            while (!messageSent.getResult()) {
                Thread.sleep(RETRY_MILLISECONDS);
                if ((waitDuration += RETRY_MILLISECONDS) > SEND_TIMEOUT_MILLISECONDS) {
                    break;
                }
            }
            if (!messageSent.getResult()) {
                Assert.fail("Sending message over MQTT protocol failed");
            }
        } catch (Exception e) {
            Assert.fail("Sending message over MQTT protocol failed");
        }
    }
    client.closeNow();
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) DeviceConnectionString(tests.integration.com.microsoft.azure.sdk.iot.device.DeviceConnectionString) EventCallback(tests.integration.com.microsoft.azure.sdk.iot.device.EventCallback) Success(tests.integration.com.microsoft.azure.sdk.iot.device.Success) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) 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