Search in sources :

Example 36 with IotHubTransportMessage

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

the class DeviceMethodMessageTest method setMethodNameThrowsOnNull.

/*
    **Tests_SRS_DEVICEMETHODMESSAGE_25_004: [**This method shall throw IllegalArgumentException if the methodName is null.**]**
     */
@Test(expected = IllegalArgumentException.class)
public void setMethodNameThrowsOnNull() {
    // arrange
    IotHubTransportMessage testDMMessage = new IotHubTransportMessage(new byte[0], MessageType.DEVICE_METHODS);
    // act
    testDMMessage.setMethodName(null);
}
Also used : IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) Test(org.junit.Test)

Example 37 with IotHubTransportMessage

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

the class DeviceMethodMessageTest method constructorSucceeds.

/*
    **Tests_SRS_DEVICEMETHODMESSAGE_25_001: [**The constructor shall save the message body by calling super with the body as parameter.**]**
    **Tests_SRS_DEVICEMETHODMESSAGE_25_002: [**If the message body is null, the constructor shall throw an IllegalArgumentException thrown by base constructor.**]**
    */
@Test
public void constructorSucceeds() {
    // act
    IotHubTransportMessage testDMMessage = new IotHubTransportMessage(new byte[0], MessageType.DEVICE_METHODS);
    // assert
    assertSame(testDMMessage.getMessageType(), MessageType.DEVICE_METHODS);
}
Also used : IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) Test(org.junit.Test)

Example 38 with IotHubTransportMessage

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

the class DeviceTwinTest method subscribeToDesiredTwinPropertySetsCorrectOperation.

@Test
public void subscribeToDesiredTwinPropertySetsCorrectOperation(@Mocked final IotHubTransportMessage mockedDeviceTwinMessage, @Mocked final TwinPropertyCallBack mockedDesiredCB) {
    // arrange
    new NonStrictExpectations() {

        {
            new IotHubTransportMessage(withAny(new byte[0]), MessageType.DEVICE_TWIN);
            result = mockedDeviceTwinMessage;
        }
    };
    DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericTwinPropertyCB, null);
    Map<Property, Pair<TwinPropertyCallBack, Object>> desiredMap = new HashMap<>();
    desiredMap.put(new Property("DesiredProp", "DesiredValue"), new Pair<>(mockedDesiredCB, null));
    // act
    testTwin.subscribeDesiredPropertiesTwinPropertyNotification(desiredMap);
    // assert
    final ConcurrentSkipListMap<String, Pair<TwinPropertyCallBack, Object>> actualMap = Deencapsulation.getField(testTwin, "onDesiredTwinPropertyChangeMap");
    assertNotNull(actualMap);
    assertFalse(actualMap.isEmpty());
    assertTrue(actualMap.containsKey("DesiredProp"));
    assertEquals(actualMap.get("DesiredProp").getKey(), mockedDesiredCB);
    new Verifications() {

        {
            mockedDeviceTwinMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST);
            times = 1;
            mockedDeviceIO.sendEventAsync(mockedDeviceTwinMessage, (IotHubEventCallback) any, null, 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 39 with IotHubTransportMessage

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

the class DeviceTwinTest method desiredChangeResponseCallsUserGenericCBWithDesiredChangeIfUnsubscribedYet.

@Test
public void desiredChangeResponseCallsUserGenericCBWithDesiredChangeIfUnsubscribedYet() {
    // arrange
    final String prop1 = "DesiredProp1";
    final String val2 = "DesiredValue2";
    final String json = "{\"" + prop1 + "\":\"" + 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);
    // act
    deviceTwinResponseMessageCallback.execute(testMessage, null);
    // assert
    new Verifications() {

        {
            mockedGenericPropertyCB.PropertyCall(prop1, val2, null);
            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 40 with IotHubTransportMessage

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

the class MqttDeviceTwinTest method receiveDoesNotSetVersionOnResTopicIfNotFound.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_041: [If the topic is of type response topic then this method shall parse further to look for version which if found is set by calling setVersion]
     */
@Test
public void receiveDoesNotSetVersionOnResTopicIfNotFound() throws TransportException {
    final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes(StandardCharsets.UTF_8);
    final String expectedTopic = "$iothub/twin/res/" + "201" + "/?$rid=" + mockReqId;
    IotHubTransportMessage receivedMessage = null;
    try {
        // arrange
        MqttDeviceTwin testTwin = new MqttDeviceTwin("", mockedConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
        Queue<Pair<String, byte[]>> testreceivedMessages = new ConcurrentLinkedQueue<>();
        testreceivedMessages.add(new MutablePair<>(expectedTopic, actualPayload));
        Deencapsulation.setField(testTwin, "receivedMessages", testreceivedMessages);
        Map<String, DeviceOperations> requestMap = new HashMap<>();
        requestMap.put(mockReqId, DEVICE_OPERATION_TWIN_GET_REQUEST);
        Deencapsulation.setField(testTwin, "requestMap", requestMap);
        // act
        receivedMessage = testTwin.receive();
    } finally {
        // assert
        assertNotNull(receivedMessage);
        assertSame(receivedMessage.getMessageType(), MessageType.DEVICE_TWIN);
        assertSame(receivedMessage.getDeviceOperationType(), DEVICE_OPERATION_TWIN_GET_RESPONSE);
        assertEquals(receivedMessage.getRequestId(), mockReqId);
        assertEquals("201", receivedMessage.getStatus());
        assertNull(receivedMessage.getVersion());
    }
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) HashMap(java.util.HashMap) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) 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