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());
}
Aggregations