Search in sources :

Example 26 with Device

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

the class DigitalTwinClientTests method invokeRootLevelCommand.

@Test
@StandardTierHubOnlyTest
public void invokeRootLevelCommand() throws IOException {
    // arrange
    String commandName = "getMaxMinReport";
    String commandInput = "\"" + ZonedDateTime.now(ZoneOffset.UTC).minusMinutes(5).format(DateTimeFormatter.ISO_DATE_TIME) + "\"";
    String jsonStringInput = "{\"prop\":\"value\"}";
    DigitalTwinInvokeCommandRequestOptions options = new DigitalTwinInvokeCommandRequestOptions();
    options.setConnectTimeoutInSeconds(15);
    options.setResponseTimeoutInSeconds(15);
    // setup device callback
    Integer deviceSuccessResponseStatus = 200;
    Integer deviceFailureResponseStatus = 500;
    // Device method callback
    DeviceMethodCallback deviceMethodCallback = (methodName, methodData, context) -> {
        String jsonRequest = new String((byte[]) methodData, StandardCharsets.UTF_8);
        if (methodName.equalsIgnoreCase(commandName)) {
            return new DeviceMethodData(deviceSuccessResponseStatus, jsonRequest);
        } else {
            return new DeviceMethodData(deviceFailureResponseStatus, jsonRequest);
        }
    };
    // IotHub event callback
    IotHubEventCallback iotHubEventCallback = (responseStatus, callbackContext) -> {
    };
    deviceClient.subscribeToDeviceMethod(deviceMethodCallback, commandName, iotHubEventCallback, commandName);
    // act
    DigitalTwinCommandResponse responseWithNoPayload = this.digitalTwinClient.invokeCommand(deviceId, commandName, null);
    DigitalTwinCommandResponse responseWithJsonStringPayload = this.digitalTwinClient.invokeCommand(deviceId, commandName, jsonStringInput);
    DigitalTwinCommandResponse responseWithDatePayload = this.digitalTwinClient.invokeCommand(deviceId, commandName, commandInput);
    ServiceResponseWithHeaders<DigitalTwinCommandResponse, DigitalTwinInvokeCommandHeaders> datePayloadResponseWithHeaders = this.digitalTwinClient.invokeCommandWithResponse(deviceId, commandName, commandInput, options);
    // assert
    assertEquals(deviceSuccessResponseStatus, responseWithNoPayload.getStatus());
    assertEquals("\"\"", responseWithNoPayload.getPayload());
    assertEquals(deviceSuccessResponseStatus, responseWithJsonStringPayload.getStatus());
    assertEquals(jsonStringInput, responseWithJsonStringPayload.getPayload());
    assertEquals(deviceSuccessResponseStatus, responseWithDatePayload.getStatus());
    assertEquals(commandInput, responseWithDatePayload.getPayload());
    assertEquals(deviceSuccessResponseStatus, datePayloadResponseWithHeaders.body().getStatus());
    assertEquals(commandInput, datePayloadResponseWithHeaders.body().getPayload());
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) Device(com.microsoft.azure.sdk.iot.service.Device) SasTokenTools(tests.integration.com.microsoft.azure.sdk.iot.helpers.SasTokenTools) Arrays(java.util.Arrays) MultiplexingClient(com.microsoft.azure.sdk.iot.device.MultiplexingClient) ClientOptions(com.microsoft.azure.sdk.iot.device.ClientOptions) DigitalTwinClient(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient) IotHubClientProtocol(com.microsoft.azure.sdk.iot.device.IotHubClientProtocol) URISyntaxException(java.net.URISyntaxException) ZonedDateTime(java.time.ZonedDateTime) DigitalTwinClientOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClientOptions) MultiplexingClientException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientException) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DigitalTwinCommandResponse(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinCommandResponse) Proxy(java.net.Proxy) After(org.junit.After) Map(java.util.Map) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) ZoneOffset(java.time.ZoneOffset) DefaultHttpProxyServer(org.littleshoot.proxy.impl.DefaultHttpProxyServer) Parameterized(org.junit.runners.Parameterized) AfterClass(org.junit.AfterClass) Tools(tests.integration.com.microsoft.azure.sdk.iot.helpers.Tools) DigitalTwinTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DigitalTwinTest) AzureSasCredential(com.azure.core.credential.AzureSasCredential) DeviceMethodCallback(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodCallback) TwinPropertyCallBack(com.microsoft.azure.sdk.iot.device.DeviceTwin.TwinPropertyCallBack) ServiceResponseWithHeaders(com.microsoft.rest.ServiceResponseWithHeaders) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) DigitalTwinInvokeCommandHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinInvokeCommandHeaders) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) UpdateOperationUtility(com.microsoft.azure.sdk.iot.service.digitaltwin.UpdateOperationUtility) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) TokenCredential(com.azure.core.credential.TokenCredential) DigitalTwinInvokeCommandRequestOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinInvokeCommandRequestOptions) HttpProxyServer(org.littleshoot.proxy.HttpProxyServer) DeviceMethodData(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodData) BeforeClass(org.junit.BeforeClass) RegistryManagerOptions(com.microsoft.azure.sdk.iot.service.RegistryManagerOptions) RunWith(org.junit.runner.RunWith) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) DigitalTwinGetHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders) DigitalTwinUpdateRequestOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinUpdateRequestOptions) ArrayList(java.util.ArrayList) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) HashSet(java.util.HashSet) RestException(com.microsoft.rest.RestException) Timeout(org.junit.rules.Timeout) IotHubEventCallback(com.microsoft.azure.sdk.iot.device.IotHubEventCallback) Pair(com.microsoft.azure.sdk.iot.device.DeviceTwin.Pair) BasicDigitalTwin(com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin) Before(org.junit.Before) Property(com.microsoft.azure.sdk.iot.device.DeviceTwin.Property) TestCase.fail(junit.framework.TestCase.fail) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) E2ETestConstants(tests.integration.com.microsoft.azure.sdk.iot.digitaltwin.helpers.E2ETestConstants) AuthenticationType(com.microsoft.azure.sdk.iot.service.auth.AuthenticationType) Rule(org.junit.Rule) Ignore(org.junit.Ignore) IotHubConnectionStringBuilder(com.microsoft.azure.sdk.iot.service.IotHubConnectionStringBuilder) DateTimeFormatter(java.time.format.DateTimeFormatter) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) IotHubEventCallback(com.microsoft.azure.sdk.iot.device.IotHubEventCallback) DigitalTwinCommandResponse(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinCommandResponse) DigitalTwinInvokeCommandHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinInvokeCommandHeaders) DeviceMethodData(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodData) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DigitalTwinInvokeCommandRequestOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinInvokeCommandRequestOptions) DeviceMethodCallback(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodCallback) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) DigitalTwinTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DigitalTwinTest) 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 27 with Device

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

the class DigitalTwinClientTests method createDeviceClient.

private DeviceClient createDeviceClient(IotHubClientProtocol protocol, String modelId) throws IOException, IotHubException, URISyntaxException {
    ClientOptions options = new ClientOptions();
    options.setModelId(modelId);
    this.deviceId = DEVICE_ID_PREFIX.concat(UUID.randomUUID().toString());
    Device device = Device.createDevice(deviceId, AuthenticationType.SAS);
    Device registeredDevice = registryManager.addDevice(device);
    String deviceConnectionString = registryManager.getDeviceConnectionString(registeredDevice);
    return new DeviceClient(deviceConnectionString, protocol, options);
}
Also used : ClientOptions(com.microsoft.azure.sdk.iot.device.ClientOptions) DigitalTwinClientOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClientOptions) Device(com.microsoft.azure.sdk.iot.service.Device) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString)

Example 28 with Device

use of com.microsoft.azure.sdk.iot.service.Device 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 29 with Device

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

the class MultiplexingClientTests method disableDeviceBeforeOpen.

// If you register a disabled device to a multiplexed connection that hasn't opened yet, the open call should succeed
// but the disabled device's session should drop shortly afterwards and the other devices on the multiplexed connection
// should be unaffected.
@ContinuousIntegrationTest
@Test
public void disableDeviceBeforeOpen() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    String deviceIdToDisable = testInstance.deviceIdentityArray.get(0).getDeviceId();
    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);
    }
    // Disable a device that will be on the multiplexed connection when the multiplexed connection hasn't opened yet
    Device deviceToDisable = registryManager.getDevice(deviceIdToDisable);
    deviceToDisable.setStatus(DeviceStatus.Disabled);
    registryManager.updateDevice(deviceToDisable);
    try {
        testInstance.multiplexingClient.open();
        // 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");
            }
        }
        // Verify that the other devices on the multiplexed connection were unaffected
        for (int i = 1; i < DEVICE_MULTIPLEX_COUNT; i++) {
            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) 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 Device

use of com.microsoft.azure.sdk.iot.service.Device 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)

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