Search in sources :

Example 16 with TwinParser

use of com.microsoft.azure.sdk.iot.deps.serializer.TwinParser in project azure-iot-sdk-java by Azure.

the class DeviceTwinTest method subscribeToDesiredDoesNotSubscribeIfAlreadySubscribed.

@Test
public void subscribeToDesiredDoesNotSubscribeIfAlreadySubscribed(@Mocked final TwinParser mockedTwinParserObject, @Mocked final DeviceTwinMessage mockedDeviceTwinMessage, @Mocked final PropertyCallBack<String, Object> mockedDesiredCB) throws IOException {
    new NonStrictExpectations() {

        {
            new TwinParser(withAny(new TwinChangedCallback() {

                @Override
                public void execute(Map<String, Object> map) {
                }
            }), withAny(new TwinChangedCallback() {

                @Override
                public void execute(Map<String, Object> map) {
                }
            }));
            result = mockedTwinParserObject;
            new DeviceTwinMessage(withAny(new byte[0]));
            result = mockedDeviceTwinMessage;
        }
    };
    DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
    Map<Property, Pair<PropertyCallBack<String, Object>, Object>> desiredMap = new HashMap<>();
    desiredMap.put(new Property("DesiredProp1", "DesiredValue1"), new Pair<>(mockedDesiredCB, null));
    testTwin.subscribeDesiredPropertiesNotification(desiredMap);
    Deencapsulation.setField(testTwin, "isSubscribed", true);
    desiredMap.put(new Property("DesiredProp2", "DesiredValue2"), new Pair<>(mockedDesiredCB, null));
    testTwin.subscribeDesiredPropertiesNotification(desiredMap);
    final ConcurrentSkipListMap<String, Pair<PropertyCallBack<String, Object>, Object>> actualMap = Deencapsulation.getField(testTwin, "onDesiredPropertyChangeMap");
    assertNotNull(actualMap);
    assertFalse(actualMap.isEmpty());
    assertTrue(actualMap.containsKey("DesiredProp1"));
    assertTrue(actualMap.containsKey("DesiredProp2"));
    assertEquals(actualMap.get("DesiredProp2").getKey(), mockedDesiredCB);
    new Verifications() {

        {
            mockedDeviceTwinMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST);
            times = 1;
            mockedDeviceIO.sendEventAsync(mockedDeviceTwinMessage, (IotHubEventCallback) any, null);
            times = 1;
        }
    };
}
Also used : HashMap(java.util.HashMap) TwinParser(com.microsoft.azure.sdk.iot.deps.serializer.TwinParser) TwinChangedCallback(com.microsoft.azure.sdk.iot.deps.serializer.TwinChangedCallback) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) Map(java.util.Map) HashMap(java.util.HashMap) DeviceTwin(com.microsoft.azure.sdk.iot.device.DeviceTwin) Test(org.junit.Test)

Aggregations

TwinParser (com.microsoft.azure.sdk.iot.deps.serializer.TwinParser)16 Test (org.junit.Test)16 DeviceTwin (com.microsoft.azure.sdk.iot.device.DeviceTwin)14 TwinChangedCallback (com.microsoft.azure.sdk.iot.deps.serializer.TwinChangedCallback)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)5 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)2 HashSet (java.util.HashSet)2