Search in sources :

Example 6 with DeviceTwin

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

the class DeviceTwinTest method getDeviceTwinResponseCallStusCBWithERRORIfStatusNull.

/*
    **Tests_SRS_DEVICETWIN_25_031: [**If the message is of type DeviceTwin and DEVICE_OPERATION_TWIN_GET_RESPONSE and if the status is null then the user is notified on the status callback registered by the user as ERROR.**]**
     */
@Test
public void getDeviceTwinResponseCallStusCBWithERRORIfStatusNull(@Mocked TwinParser mockedTwinParserObject) throws IOException {
    //arrange
    final byte[] body = {};
    DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
    MessageCallback deviceTwinResponseMessageCallback = Deencapsulation.newInnerInstance("deviceTwinResponseMessageCallback", testTwin);
    final DeviceTwinMessage testMessage = new DeviceTwinMessage(body);
    testMessage.setStatus(null);
    testMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_GET_RESPONSE);
    //act
    deviceTwinResponseMessageCallback.execute(testMessage, null);
    //assert
    final TwinParser actualTwinParserObj = Deencapsulation.getField(testTwin, "twinParser");
    final IotHubEventCallback actualStatusCB = Deencapsulation.getField(testTwin, "deviceTwinStatusCallback");
    new Verifications() {

        {
            actualStatusCB.execute(IotHubStatusCode.ERROR, withAny(new Object()));
            times = 1;
            actualTwinParserObj.updateTwin(anyString);
            times = 0;
        }
    };
}
Also used : TwinParser(com.microsoft.azure.sdk.iot.deps.serializer.TwinParser) DeviceTwin(com.microsoft.azure.sdk.iot.device.DeviceTwin) Test(org.junit.Test)

Example 7 with DeviceTwin

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

the class DeviceTwinTest method contructorSetsDTMessageResponseCB.

/*
    **Tests_SRS_DEVICETWIN_25_002: [**The constructor shall save the device twin message callback by calling setDeviceTwinMessageCallback where any further messages for device twin shall be delivered.**]**
     */
@Test
public void contructorSetsDTMessageResponseCB() throws IOException {
    DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
    new Verifications() {

        {
            mockedConfig.setDeviceTwinMessageCallback((MessageCallback) any, any);
            times = 1;
        }
    };
}
Also used : DeviceTwin(com.microsoft.azure.sdk.iot.device.DeviceTwin) Test(org.junit.Test)

Example 8 with DeviceTwin

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

the class DeviceTwinTest method subscribeToDesiredCallsGenericCBOnDesiredChangeIfNoUserCBFound.

/*
    **Codes_SRS_DEVICETWIN_25_023: [**OnDesiredPropertyChange callback shall look for the user registered call back on the property that changed and if no callback is registered or is null then OnDesiredPropertyChange shall call the user on generic callback providing with the desired property change key and value pair**]**
     */
@Test
public void subscribeToDesiredCallsGenericCBOnDesiredChangeIfNoUserCBFound(@Mocked final PropertyCallBack<String, Object> mockedDesiredCB) throws IOException {
    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);
    TwinChangedCallback onDesiredChange = Deencapsulation.newInnerInstance("OnDesiredPropertyChanged", testTwin);
    final HashMap<String, Object> desiredPropertyMap = new HashMap<>();
    desiredPropertyMap.put("DesiredProp2", "DesiredValue2");
    //act
    onDesiredChange.execute(desiredPropertyMap);
    //assert
    assertTrue(desiredPropertyMap.isEmpty());
    new Verifications() {

        {
            mockedGenericPropertyCB.PropertyCall("DesiredProp2", "DesiredValue2", null);
            times = 1;
            desiredPropertyMap.remove("DesiredProp1");
            times = 1;
        }
    };
}
Also used : TwinChangedCallback(com.microsoft.azure.sdk.iot.deps.serializer.TwinChangedCallback) HashMap(java.util.HashMap) DeviceTwin(com.microsoft.azure.sdk.iot.device.DeviceTwin) Test(org.junit.Test)

Example 9 with DeviceTwin

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

the class DeviceTwinTest method constructorCreatesNewTwinObject.

/*
    **Tests_SRS_DEVICETWIN_25_004: [**The constructor shall create a new twin object which will hence forth be used as a storage for all the properties provided by user.**]**
     */
@Test
public void constructorCreatesNewTwinObject(@Mocked final TwinParser mockedTwinParserObject) throws IOException {
    new StrictExpectations() {

        {
            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;
        }
    };
    //act
    DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
    TwinParser actualTwinParserObj = Deencapsulation.getField(testTwin, "twinParser");
    //assert
    assertNotNull(actualTwinParserObj);
}
Also used : TwinChangedCallback(com.microsoft.azure.sdk.iot.deps.serializer.TwinChangedCallback) TwinParser(com.microsoft.azure.sdk.iot.deps.serializer.TwinParser) 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)

Example 10 with DeviceTwin

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

the class DeviceTwinTest method subscribeToDesiredCallsUserCBOnDesiredChangeIfUserCBFound.

/*
    **Test_SRS_DEVICETWIN_25_022: [**OnDesiredPropertyChange callback shall look for the user registered call back on the property that changed provided in desiredPropertyMap and call the user providing the desired property change key and value pair**]**
     */
@Test
public void subscribeToDesiredCallsUserCBOnDesiredChangeIfUserCBFound(@Mocked final PropertyCallBack<String, Object> mockedDesiredCB) throws IOException {
    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);
    TwinChangedCallback onDesiredChange = Deencapsulation.newInnerInstance("OnDesiredPropertyChanged", testTwin);
    final HashMap<String, Object> desiredPropertyMap = new HashMap<>();
    desiredPropertyMap.put("DesiredProp1", "DesiredValue1");
    //act
    onDesiredChange.execute(desiredPropertyMap);
    //assert
    assertTrue(desiredPropertyMap.isEmpty());
    new Verifications() {

        {
            mockedDesiredCB.PropertyCall("DesiredProp1", "DesiredValue1", null);
            times = 1;
            desiredPropertyMap.remove("DesiredProp1");
            times = 1;
        }
    };
}
Also used : TwinChangedCallback(com.microsoft.azure.sdk.iot.deps.serializer.TwinChangedCallback) HashMap(java.util.HashMap) DeviceTwin(com.microsoft.azure.sdk.iot.device.DeviceTwin) Test(org.junit.Test)

Aggregations

DeviceTwin (com.microsoft.azure.sdk.iot.device.DeviceTwin)25 Test (org.junit.Test)24 TwinParser (com.microsoft.azure.sdk.iot.deps.serializer.TwinParser)14 TwinChangedCallback (com.microsoft.azure.sdk.iot.deps.serializer.TwinChangedCallback)9 HashMap (java.util.HashMap)9 Map (java.util.Map)5 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)5 HashSet (java.util.HashSet)2 IOException (java.io.IOException)1