Search in sources :

Example 26 with IotHubTransportMessage

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

the class DeviceTwinTest method desiredPropResponseDoesNotCallsUserStatusCBOnNotification.

/*
     **Tests_SRS_DEVICETWIN_25_025: [**On receiving a message from IOTHub with desired property changes, the callback deviceTwinResponseMessageCallback is triggered.**]**
     **Tests_SRS_DEVICETWIN_25_026: [**If the message is of type DEVICE_TWIN and DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE then the payload is deserialize by calling updateDesiredProperty.**]**
     */
@Test
public void desiredPropResponseDoesNotCallsUserStatusCBOnNotification(@Mocked final TwinState mockedTwinState, @Mocked final TwinCollection mockedTwinCollection) {
    // 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(String.valueOf(200));
    testMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE);
    new NonStrictExpectations() {

        {
            TwinState.createFromDesiredPropertyJson((String) any);
            result = mockedTwinState;
            times = 1;
            mockedTwinState.getDesiredProperty();
            result = mockedTwinCollection;
            times = 2;
        }
    };
    // act
    deviceTwinResponseMessageCallback.execute(testMessage, null);
    // assert
    final IotHubEventCallback actualStatusCB = Deencapsulation.getField(testTwin, "deviceTwinStatusCallback");
    new Verifications() {

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

Example 27 with IotHubTransportMessage

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

the class DeviceMethodTest method deviceMethodResponseCallbackSendsResponseOnlyIfNonNull.

/*
    **Tests_SRS_DEVICEMETHOD_25_014: [**If the user invoked callback failed for any reason then the user shall be notified on the status callback registered by the user as ERROR before marking the status of the sent message as Rejected.**]**
     */
@Test
public void deviceMethodResponseCallbackSendsResponseOnlyIfNonNull() throws IllegalArgumentException {
    // arrange
    DeviceMethod testMethod = new DeviceMethod(mockedDeviceIO, mockedConfig, mockedStatusCB, null);
    testMethod.subscribeToDeviceMethod(mockedDeviceMethodCB, null);
    byte[] testPayload = "TestPayload".getBytes(StandardCharsets.UTF_8);
    IotHubTransportMessage testMessage = new IotHubTransportMessage(testPayload, DEVICE_METHODS);
    testMessage.setDeviceOperationType(DEVICE_OPERATION_METHOD_RECEIVE_REQUEST);
    final DeviceMethodData testUserData = null;
    MessageCallback testDeviceMethodResponseMessageCallback = Deencapsulation.newInnerInstance("deviceMethodResponseCallback", testMethod);
    new NonStrictExpectations() {

        {
            mockedDeviceMethodCB.call(anyString, any, any);
            result = testUserData;
        }
    };
    // act
    IotHubMessageResult result = testDeviceMethodResponseMessageCallback.execute(testMessage, null);
    // assert
    assertNotSame(result, IotHubMessageResult.COMPLETE);
    new Verifications() {

        {
            mockedStatusCB.execute(IotHubStatusCode.ERROR, any);
            times = 1;
        }
    };
}
Also used : IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 28 with IotHubTransportMessage

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

the class DeviceMethodTest method deviceMethodResponseCallbackDoesNotHangOnUserCallBackHang.

@Test
public void deviceMethodResponseCallbackDoesNotHangOnUserCallBackHang() throws IllegalArgumentException {
    // arrange
    DeviceMethod testMethod = new DeviceMethod(mockedDeviceIO, mockedConfig, mockedStatusCB, null);
    testMethod.subscribeToDeviceMethod(mockedDeviceMethodCB, null);
    byte[] testPayload = "TestPayload".getBytes(StandardCharsets.UTF_8);
    IotHubTransportMessage testMessage = new IotHubTransportMessage(testPayload, DEVICE_METHODS);
    testMessage.setDeviceOperationType(DEVICE_OPERATION_METHOD_RECEIVE_REQUEST);
    MessageCallback testDeviceMethodResponseMessageCallback = Deencapsulation.newInnerInstance("deviceMethodResponseCallback", testMethod);
    new NonStrictExpectations() {

        {
            mockedDeviceMethodCB.call(anyString, any, any);
            result = new Exception("Test Exception");
        }
    };
    // act
    IotHubMessageResult result = testDeviceMethodResponseMessageCallback.execute(testMessage, null);
    // assert
    assertNotSame(result, IotHubMessageResult.COMPLETE);
    new Verifications() {

        {
            mockedStatusCB.execute(IotHubStatusCode.ERROR, any);
            times = 1;
        }
    };
}
Also used : IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 29 with IotHubTransportMessage

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

the class DeviceMethodTest method deviceMethodResponseCallbackAbandonsOnIncorrectMessage.

/*
    **Tests_SRS_DEVICEMETHOD_25_009: [**If the received message is not of type DeviceMethod and DEVICE_OPERATION_METHOD_RECEIVE_REQUEST then user shall be notified on the status callback registered by the user as ERROR before marking the status of the sent message as Abandon **]**
     */
@Test
public void deviceMethodResponseCallbackAbandonsOnIncorrectMessage() throws IllegalArgumentException {
    // arrange
    DeviceMethod testMethod = new DeviceMethod(mockedDeviceIO, mockedConfig, mockedStatusCB, null);
    testMethod.subscribeToDeviceMethod(mockedDeviceMethodCB, null);
    byte[] testPayload = "TestPayload".getBytes(StandardCharsets.UTF_8);
    IotHubTransportMessage testMessage = new IotHubTransportMessage(testPayload, MessageType.DEVICE_TWIN);
    testMessage.setDeviceOperationType(DEVICE_OPERATION_METHOD_RECEIVE_REQUEST);
    final DeviceMethodData testUserData = new DeviceMethodData(100, "Some test message");
    MessageCallback testDeviceMethodResponseMessageCallback = Deencapsulation.newInnerInstance("deviceMethodResponseCallback", testMethod);
    new NonStrictExpectations() {

        {
            mockedDeviceMethodCB.call(anyString, any, any);
            result = testUserData;
        }
    };
    // act
    IotHubMessageResult result = testDeviceMethodResponseMessageCallback.execute(testMessage, null);
    // assert
    assertNotSame(result, IotHubMessageResult.COMPLETE);
    new Verifications() {

        {
            mockedStatusCB.execute(IotHubStatusCode.ERROR, any);
        }
    };
}
Also used : IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 30 with IotHubTransportMessage

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

the class DeviceTwinMessageTest method getStatusGets.

/*
    **Tests_SRS_DEVICETWINMESSAGE_25_008: [**The function shall return the value of the status either set by the setter or the default (null) if unset so far.**]**
     */
@Test
public void getStatusGets() {
    // arrange
    final byte[] actualData = {};
    IotHubTransportMessage msg = new IotHubTransportMessage(actualData, MessageType.DEVICE_TWIN);
    msg.setStatus("12");
    // act
    String status = msg.getStatus();
    // assert
    assertEquals(status, "12");
}
Also used : IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) 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