Search in sources :

Example 21 with IotHubTransportMessage

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

the class DeviceTwinTest method desiredChangePropertyWithNoCallback.

@Test
public void desiredChangePropertyWithNoCallback() {
    // arrange
    final String prop1 = "DesiredProp1";
    final String val2 = "DesiredValue2";
    final String json = "{\"" + prop1 + "\":\"" + val2 + "\"}";
    DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, (TwinPropertyCallBack) null, null);
    MessageCallback deviceTwinResponseMessageCallback = Deencapsulation.newInnerInstance("deviceTwinResponseMessageCallback", testTwin);
    final IotHubTransportMessage testMessage = new IotHubTransportMessage(json.getBytes(StandardCharsets.UTF_8), MessageType.DEVICE_TWIN);
    testMessage.setStatus(String.valueOf(200));
    testMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE);
    // act
    deviceTwinResponseMessageCallback.execute(testMessage, null);
// assert
}
Also used : IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) DeviceTwin(com.microsoft.azure.sdk.iot.device.DeviceTwin) Test(org.junit.Test)

Example 22 with IotHubTransportMessage

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

the class DeviceTwinTest method updateReportedPropOnResponseCallsStatusCBErrorIfNullStatus.

/*
     **Tests_SRS_DEVICETWIN_25_028: [**If the message is of type DEVICE_TWIN and DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_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 updateReportedPropOnResponseCallsStatusCBErrorIfNullStatus() {
    // arrange
    final byte[] body = {};
    DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
    MessageCallback deviceTwinResponseMessageCallback = Deencapsulation.newInnerInstance("deviceTwinResponseMessageCallback", testTwin);
    final IotHubTransportMessage testMessage = new IotHubTransportMessage(body, MessageType.DEVICE_TWIN);
    testMessage.setStatus(null);
    testMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_RESPONSE);
    // act
    deviceTwinResponseMessageCallback.execute(testMessage, null);
    // assert
    final IotHubEventCallback actualStatusCB = Deencapsulation.getField(testTwin, "deviceTwinStatusCallback");
    new Verifications() {

        {
            actualStatusCB.execute(IotHubStatusCode.ERROR, withAny(new Object()));
            times = 1;
        }
    };
}
Also used : IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) DeviceTwin(com.microsoft.azure.sdk.iot.device.DeviceTwin) Test(org.junit.Test)

Example 23 with IotHubTransportMessage

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

the class DeviceTwinTest method getDeviceTwinResponseCallStusCBWithERRORIfStatusNull.

/*
     **Tests_SRS_DEVICETWIN_25_031: [**If the message is of type DEVICE_TWIN 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() {
    // arrange
    final byte[] body = {};
    DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
    MessageCallback deviceTwinResponseMessageCallback = Deencapsulation.newInnerInstance("deviceTwinResponseMessageCallback", testTwin);
    final IotHubTransportMessage testMessage = new IotHubTransportMessage(body, MessageType.DEVICE_TWIN);
    testMessage.setStatus(null);
    testMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_GET_RESPONSE);
    // act
    deviceTwinResponseMessageCallback.execute(testMessage, null);
    // assert
    final IotHubEventCallback actualStatusCB = Deencapsulation.getField(testTwin, "deviceTwinStatusCallback");
    new Verifications() {

        {
            actualStatusCB.execute(IotHubStatusCode.ERROR, withAny(new Object()));
            times = 1;
        }
    };
}
Also used : IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) DeviceTwin(com.microsoft.azure.sdk.iot.device.DeviceTwin) Test(org.junit.Test)

Example 24 with IotHubTransportMessage

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

the class DeviceTwinTest method subscribeToDesiredCallsGenericCBOnDesiredChangeIfNoUserCBFound.

/*
     **Tests_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) {
    // arrange
    final String prop1 = "DesiredProp1";
    final String prop2 = "DesiredProp2";
    final String val2 = "DesiredValue2";
    final String json = "{\"" + prop2 + "\":\"" + val2 + "\"}";
    DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
    MessageCallback deviceTwinResponseMessageCallback = Deencapsulation.newInnerInstance("deviceTwinResponseMessageCallback", testTwin);
    final IotHubTransportMessage testMessage = new IotHubTransportMessage(json.getBytes(StandardCharsets.UTF_8), MessageType.DEVICE_TWIN);
    testMessage.setStatus(String.valueOf(200));
    testMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE);
    Map<Property, Pair<PropertyCallBack<String, Object>, Object>> desiredMap = new HashMap<>();
    desiredMap.put(new Property(prop1, null), new Pair<>(mockedDesiredCB, null));
    testTwin.subscribeDesiredPropertiesNotification(desiredMap);
    // act
    deviceTwinResponseMessageCallback.execute(testMessage, null);
    // assert
    new Verifications() {

        {
            mockedGenericPropertyCB.PropertyCall(prop2, val2, null);
            times = 1;
        }
    };
}
Also used : HashMap(java.util.HashMap) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) DeviceTwin(com.microsoft.azure.sdk.iot.device.DeviceTwin) Test(org.junit.Test)

Example 25 with IotHubTransportMessage

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

the class DeviceTwinTest method updateReportedPropWithVersionNullCallsTwinAPIForSerialization.

@Test
public void updateReportedPropWithVersionNullCallsTwinAPIForSerialization(@Mocked final IotHubTransportMessage mockedDeviceTwinMessage) throws IOException {
    // arrange
    final String prop1 = "prop1";
    final String prop2 = "prop2";
    final String val1 = "val1";
    final int val2 = 100;
    final HashSet<Property> reportedProp = new HashSet<Property>() {

        {
            add(new Property(prop1, val1));
            add(new Property(prop2, val2));
        }
    };
    final String json = "{\"" + prop2 + "\":" + val2 + ",\"" + prop1 + "\":\"" + val1 + "\"}";
    new NonStrictExpectations() {

        {
            new IotHubTransportMessage(json.getBytes(StandardCharsets.UTF_8), MessageType.DEVICE_TWIN);
            result = mockedDeviceTwinMessage;
        }
    };
    DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
    // act
    testTwin.updateReportedProperties(reportedProp, null);
    // assert
    new Verifications() {

        {
            mockedDeviceTwinMessage.setRequestId(anyString);
            times = 1;
            mockedDeviceTwinMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST);
            times = 1;
            mockedDeviceTwinMessage.setVersion((String) any);
            times = 0;
            mockedDeviceIO.sendEventAsync(mockedDeviceTwinMessage, (IotHubEventCallback) any, null, anyString);
            times = 1;
        }
    };
}
Also used : IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) HashSet(java.util.HashSet) DeviceTwin(com.microsoft.azure.sdk.iot.device.DeviceTwin) Test(org.junit.Test)

Aggregations

IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)129 Test (org.junit.Test)108 Message (com.microsoft.azure.sdk.iot.device.Message)37 MutablePair (org.apache.commons.lang3.tuple.MutablePair)33 Pair (org.apache.commons.lang3.tuple.Pair)33 DeviceTwin (com.microsoft.azure.sdk.iot.device.DeviceTwin)30 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)25 HashMap (java.util.HashMap)24 DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)16 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)16 MessageType (com.microsoft.azure.sdk.iot.device.MessageType)14 Verifications (mockit.Verifications)10 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)8 NonStrictExpectations (mockit.NonStrictExpectations)8 IOException (java.io.IOException)5 FileUploadTask (com.microsoft.azure.sdk.iot.device.fileupload.FileUploadTask)4 MessageCallback (com.microsoft.azure.sdk.iot.device.MessageCallback)3 MethodResult (com.microsoft.azure.sdk.iot.device.edge.MethodResult)3 HttpsTransportManager (com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager)3 HashSet (java.util.HashSet)3