Search in sources :

Example 16 with IotHubTransportMessage

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

the class DeviceTwinTest method updateReportedPropWithVersionCallsTwinAPIForSerialization.

/*
     **Tests_SRS_DEVICETWIN_21_024: [**If the version is provided, this method shall set the version for the message by calling setVersion .**]**
     */
@Test
public void updateReportedPropWithVersionCallsTwinAPIForSerialization(@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, 10);
    // assert
    new Verifications() {

        {
            mockedDeviceTwinMessage.setRequestId(anyString);
            times = 1;
            mockedDeviceTwinMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST);
            times = 1;
            mockedDeviceTwinMessage.setVersion("10");
            times = 1;
            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)

Example 17 with IotHubTransportMessage

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

the class DeviceTwinTest method getDeviceTwinSetsDeviceTwinOperation.

/*
     **Tests_SRS_DEVICETWIN_25_006: [**This method shall set the message type as DEVICE_OPERATION_TWIN_GET_REQUEST by calling setDeviceOperationType.**]**
     */
@Test
public void getDeviceTwinSetsDeviceTwinOperation(@Mocked final IotHubTransportMessage mockedDeviceTwinMessage) {
    DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
    final byte[] body = {};
    new NonStrictExpectations() {

        {
            new IotHubTransportMessage(body, MessageType.DEVICE_TWIN);
            result = mockedDeviceTwinMessage;
        }
    };
    testTwin.getDeviceTwin();
    new Verifications() {

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

Example 18 with IotHubTransportMessage

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

the class DeviceTwinTest method getDeviceTwinSetsEmptyPayload.

/*
     **Tests_SRS_DEVICETWIN_25_005: [**The method shall create a device twin message with empty payload to be sent IotHub.**]**
     */
@Test
public void getDeviceTwinSetsEmptyPayload(@Mocked final IotHubTransportMessage mockedDeviceTwinMessage) {
    DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
    final byte[] body = {};
    new NonStrictExpectations() {

        {
            new IotHubTransportMessage(body, MessageType.DEVICE_TWIN);
            result = mockedDeviceTwinMessage;
        }
    };
    testTwin.getDeviceTwin();
    new Verifications() {

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

Example 19 with IotHubTransportMessage

use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage 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_007: [**This method shall set the request id for the message by calling setRequestId .**]**
     **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 IotHubTransportMessage mockedDeviceTwinMessage) {
    // arrange
    DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
    final byte[] body = {};
    new NonStrictExpectations() {

        {
            new IotHubTransportMessage(body, MessageType.DEVICE_TWIN);
            result = mockedDeviceTwinMessage;
        }
    };
    // act
    testTwin.getDeviceTwin();
    // assert
    new Verifications() {

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

Example 20 with IotHubTransportMessage

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

the class DeviceTwinTest method getDeviceTwinResponseWithPropertiesCallsTwinPropertyCallBack.

@Test
public void getDeviceTwinResponseWithPropertiesCallsTwinPropertyCallBack(@Mocked final Property mockedProperty) {
    // arrange
    final String reportedProp1 = "ReportedProp1";
    final String reportedVal2 = "ReportedValue2";
    final Integer reportedVersion = 15;
    final String desiredProp1 = "DesiredProp1";
    final String desiredValue2 = "DesiredValue2";
    final Integer desiredVersion = 10;
    final String json = "{" + "\"desired\":{\"" + desiredProp1 + "\":\"" + desiredValue2 + "\", \"$version\":" + desiredVersion + "}," + "\"reported\":{\"" + reportedProp1 + "\":\"" + reportedVal2 + "\", \"$version\":" + reportedVersion + "}" + "}";
    DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericTwinPropertyCB, 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_GET_RESPONSE);
    // act
    deviceTwinResponseMessageCallback.execute(testMessage, null);
    // assert
    new Verifications() {

        {
            Deencapsulation.newInstance(Property.class, new Class[] { String.class, Object.class, Integer.class, boolean.class, Date.class, Integer.class, String.class, String.class }, desiredProp1, desiredValue2, desiredVersion, false, null, null, null, null);
            times = 1;
            Deencapsulation.newInstance(Property.class, new Class[] { String.class, Object.class, Integer.class, boolean.class, Date.class, Integer.class }, reportedProp1, reportedVal2, reportedVersion, true, null, null);
            times = 1;
            mockedGenericTwinPropertyCB.TwinPropertyCallBack((Property) any, null);
            times = 2;
        }
    };
}
Also used : IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) 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