Search in sources :

Example 21 with DeviceTwin

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

the class DeviceTwinTest method updateReportedPropCallsTwinAPIForSerialization.

/*
    **Tests_SRS_DEVICETWIN_25_011: [**The method shall send the property set to Twin Serializer for serilization by calling updateReportedProperty.**]**
     */
@Test
public void updateReportedPropCallsTwinAPIForSerialization(@Mocked final TwinParser mockedTwinParserObject, @Mocked final DeviceTwinMessage mockedDeviceTwinMessage) throws IOException {
    final String mockedSerilizedProp = "SerializedReportedProperties";
    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;
            mockedTwinParserObject.updateReportedProperty(withAny(new HashMap<String, Object>()));
            result = mockedSerilizedProp;
            new DeviceTwinMessage(withAny(new byte[0]));
            result = mockedDeviceTwinMessage;
        }
    };
    DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
    HashSet<Property> reportedProp = new HashSet<>();
    testTwin.updateReportedProperties(reportedProp);
    new Verifications() {

        {
            mockedTwinParserObject.updateReportedProperty(withAny(new HashMap<String, Object>()));
            times = 1;
            mockedDeviceTwinMessage.setRequestId(anyString);
            times = 1;
            mockedDeviceTwinMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST);
            times = 1;
            mockedDeviceIO.sendEventAsync(mockedDeviceTwinMessage, (IotHubEventCallback) any, null);
            times = 1;
        }
    };
}
Also used : TwinChangedCallback(com.microsoft.azure.sdk.iot.deps.serializer.TwinChangedCallback) HashMap(java.util.HashMap) 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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 22 with DeviceTwin

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

the class DeviceTwinTest method desiredPropResponseCallsTwinApiToDeserialize.

/*
    **Tests_SRS_DEVICETWIN_25_026: [**If the message is of type DeviceTwin and DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE then the payload is deserialize by calling updateDesiredProperty.**]**
     */
@Test
public void desiredPropResponseCallsTwinApiToDeserialize(@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(String.valueOf(200));
    testMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE);
    //act
    deviceTwinResponseMessageCallback.execute(testMessage, null);
    //assert
    final TwinParser actualTwinParserObj = Deencapsulation.getField(testTwin, "twinParser");
    final IotHubEventCallback actualStatusCB = Deencapsulation.getField(testTwin, "deviceTwinStatusCallback");
    new Verifications() {

        {
            actualTwinParserObj.updateTwin(anyString);
            times = 0;
            actualTwinParserObj.updateDesiredProperty(anyString);
            times = 1;
        }
    };
}
Also used : TwinParser(com.microsoft.azure.sdk.iot.deps.serializer.TwinParser) DeviceTwin(com.microsoft.azure.sdk.iot.device.DeviceTwin) Test(org.junit.Test)

Example 23 with DeviceTwin

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

the class DeviceTwinTest method getDeviceTwinSucceeds.

/*
    **Tests_SRS_DEVICETWIN_25_005: [**The method shall create a device twin message with empty payload to be sent IotHub.**]**
    **Tests_SRS_DEVICETWIN_25_008: [**This method shall send the message to the lower transport layers by calling sendEventAsync.**]**
     */
@Test
public void getDeviceTwinSucceeds(@Mocked final DeviceTwinMessage mockedDeviceTwinMessage) throws IOException {
    //arrange
    DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
    final byte[] body = {};
    new NonStrictExpectations() {

        {
            new DeviceTwinMessage(body);
            result = mockedDeviceTwinMessage;
        }
    };
    //act
    testTwin.getDeviceTwin();
    //assert
    new Verifications() {

        {
            mockedDeviceTwinMessage.setRequestId(anyString);
            times = 1;
            mockedDeviceTwinMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_GET_REQUEST);
            times = 1;
            mockedDeviceIO.sendEventAsync(mockedDeviceTwinMessage, (IotHubEventCallback) any, null);
            times = 1;
        }
    };
}
Also used : DeviceTwin(com.microsoft.azure.sdk.iot.device.DeviceTwin) Test(org.junit.Test)

Example 24 with DeviceTwin

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

Example 25 with DeviceTwin

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

the class DeviceClient method startDeviceTwin.

/**
     * Starts the device twin.
     *
     * @param deviceTwinStatusCallback the IotHubEventCallback callback for providing the status of Device Twin operations. Cannot be {@code null}.
     * @param deviceTwinStatusCallbackContext the context to be passed to the status callback. Can be {@code null}.
     * @param genericPropertyCallBack the PropertyCallBack callback for providing any changes in desired properties. Cannot be {@code null}.
     * @param genericPropertyCallBackContext the context to be passed to the property callback. Can be {@code null}.     *
     *
     * @throws IllegalArgumentException if the callback is {@code null}
     * @throws UnsupportedOperationException if called more than once on the same device
     * @throws IOException if called when client is not opened
     */
public void startDeviceTwin(IotHubEventCallback deviceTwinStatusCallback, Object deviceTwinStatusCallbackContext, PropertyCallBack genericPropertyCallBack, Object genericPropertyCallBackContext) throws IOException {
    if (!this.deviceIO.isOpen()) {
        /*
            **Codes_SRS_DEVICECLIENT_25_027: [**If the client has not been open, the function shall throw an IOException.**]**
             */
        throw new IOException("Open the client connection before using it.");
    }
    if (deviceTwinStatusCallback == null || genericPropertyCallBack == null) {
        /*
            **Codes_SRS_DEVICECLIENT_25_026: [**If the deviceTwinStatusCallback or genericPropertyCallBack is null, the function shall throw an IllegalArgumentException.**]**
             */
        throw new IllegalArgumentException("Callback cannot be null");
    }
    if (this.deviceTwin == null) {
        /*
            **Codes_SRS_DEVICECLIENT_25_025: [**The function shall create a new instance of class Device Twin and request all twin properties by calling getDeviceTwin**]**
             */
        deviceTwin = new DeviceTwin(this.deviceIO, this.config, deviceTwinStatusCallback, deviceTwinStatusCallbackContext, genericPropertyCallBack, genericPropertyCallBackContext);
        deviceTwin.getDeviceTwin();
    } else {
        /*
            **Codes_SRS_DEVICECLIENT_25_028: [**If this method is called twice on the same instance of the client then this method shall throw UnsupportedOperationException.**]**
             */
        throw new UnsupportedOperationException("You have already initialised twin");
    }
}
Also used : IOException(java.io.IOException) DeviceTwin(com.microsoft.azure.sdk.iot.device.DeviceTwin)

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