Search in sources :

Example 1 with TwinPropertiesCallback

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

the class DesiredPropertiesTests method testSubscribeToDesiredPropertiesBatch.

// This test is for the startDeviceTwin/startTwin API that takes the TwinPropertiesCallback rather than the TwinPropertyCallback
// This callback should receive the full twin update in one callback, rather than one callback per updated
// desired property
@Test
@StandardTierHubOnlyTest
public void testSubscribeToDesiredPropertiesBatch() throws Exception {
    super.setUpNewDeviceAndModule(false);
    String expectedKey1 = UUID.randomUUID().toString();
    String expectedValue1 = UUID.randomUUID().toString();
    String expectedKey2 = UUID.randomUUID().toString();
    String expectedValue2 = UUID.randomUUID().toString();
    TwinCollection expectedDesiredProperties = new TwinCollection();
    expectedDesiredProperties.putFinal(expectedKey1, expectedValue1);
    expectedDesiredProperties.putFinal(expectedKey2, expectedValue2);
    TwinPropertiesCallback twinPropertiesCallback = new TwinPropertiesCallbackImpl(expectedDesiredProperties);
    Success desiredPropertiesCallbackState = new Success();
    testInstance.testIdentity.getClient().open();
    if (testInstance.testIdentity.getClient() instanceof DeviceClient) {
        ((DeviceClient) testInstance.testIdentity.getClient()).startDeviceTwin(new DeviceTwinStatusCallBack(), testInstance.deviceUnderTest, twinPropertiesCallback, desiredPropertiesCallbackState);
    } else {
        ((ModuleClient) testInstance.testIdentity.getClient()).startTwin(new DeviceTwinStatusCallBack(), testInstance.deviceUnderTest, twinPropertiesCallback, desiredPropertiesCallbackState);
    }
    long startTime = System.currentTimeMillis();
    while (testInstance.deviceUnderTest.deviceTwinStatus != IotHubStatusCode.OK) {
        Thread.sleep(200);
        if (System.currentTimeMillis() - startTime > START_TWIN_TIMEOUT_MILLISECONDS) {
            fail("Timed out waiting for twin to start");
        }
    }
    DeviceTwinDevice serviceClientTwin;
    if (testInstance.clientType == ClientType.DEVICE_CLIENT) {
        serviceClientTwin = new DeviceTwinDevice(testInstance.testIdentity.getClient().getConfig().getDeviceId());
    } else {
        serviceClientTwin = new DeviceTwinDevice(testInstance.testIdentity.getClient().getConfig().getDeviceId(), testInstance.testIdentity.getClient().getConfig().getModuleId());
    }
    Set<com.microsoft.azure.sdk.iot.service.devicetwin.Pair> desiredProperties = new HashSet<>();
    desiredProperties.add(new com.microsoft.azure.sdk.iot.service.devicetwin.Pair(expectedKey1, expectedValue1));
    desiredProperties.add(new com.microsoft.azure.sdk.iot.service.devicetwin.Pair(expectedKey2, expectedValue2));
    serviceClientTwin.setDesiredProperties(desiredProperties);
    testInstance.twinServiceClient.updateTwin(serviceClientTwin);
    startTime = System.currentTimeMillis();
    while (!desiredPropertiesCallbackState.wasCallbackFired()) {
        Thread.sleep(200);
        if (System.currentTimeMillis() - startTime > DESIRED_PROPERTIES_PROPAGATION_TIME_MILLISECONDS) {
            fail("Timed out waiting for desired properties callback to execute");
        }
    }
    assertTrue("Desired properties callback executed, but with unexpected properties", desiredPropertiesCallbackState.getResult());
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) ModuleClient(com.microsoft.azure.sdk.iot.device.ModuleClient) TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) TwinPropertiesCallback(com.microsoft.azure.sdk.iot.device.DeviceTwin.TwinPropertiesCallback) Pair(com.microsoft.azure.sdk.iot.device.DeviceTwin.Pair) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test)

Aggregations

TwinCollection (com.microsoft.azure.sdk.iot.deps.twin.TwinCollection)1 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)1 Pair (com.microsoft.azure.sdk.iot.device.DeviceTwin.Pair)1 TwinPropertiesCallback (com.microsoft.azure.sdk.iot.device.DeviceTwin.TwinPropertiesCallback)1 ModuleClient (com.microsoft.azure.sdk.iot.device.ModuleClient)1 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)1 Test (org.junit.Test)1 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)1 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)1 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)1