Search in sources :

Example 86 with IotHubTransportMessage

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

the class MqttDeviceTwinTest method sendThrowsIllegalArgumentExceptionForUpdateReportedPropertiesOnCorrectTopicIfReqIdIsNullOrEmpty.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_027: [send method shall throw an IllegalArgumentException if message contains a null or empty request id if the operation is of type DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST.]
     */
@Test(expected = IllegalArgumentException.class)
public void sendThrowsIllegalArgumentExceptionForUpdateReportedPropertiesOnCorrectTopicIfReqIdIsNullOrEmpty(@Mocked final Mqtt mockMqtt, @Mocked final IotHubTransportMessage mockMessage) throws TransportException {
    final byte[] actualPayload = { 0x61, 0x62, 0x63 };
    final String expectedTopic = "$iothub/twin/PATCH/properties/reported/?$rid=" + mockReqId + "&$version=" + mockVersion;
    try {
        // arrange
        MqttDeviceTwin testTwin = new MqttDeviceTwin("", mockedConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
        testTwin.start();
        new NonStrictExpectations() {

            {
                mockMessage.getBytes();
                result = actualPayload;
                mockMessage.getMessageType();
                result = MessageType.DEVICE_TWIN;
                mockMessage.getDeviceOperationType();
                result = DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST;
                mockMessage.getRequestId();
                result = null;
            }
        };
        // act
        testTwin.send(mockMessage);
    } finally {
        // assert
        new Verifications() {

            {
                mockMessage.getBytes();
                times = 1;
                Deencapsulation.invoke(mockMqtt, "publish", expectedTopic, actualPayload, mockMessage);
                times = 0;
            }
        };
    }
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 87 with IotHubTransportMessage

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

the class MqttDeviceTwinTest method receiveThrowsTransportExceptionOnAnythingOtherThenPatchDesiredProp.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_043: [If the topic is not of type response for desired properties then this method shall throw TransportException]
     */
@Test(expected = TransportException.class)
public void receiveThrowsTransportExceptionOnAnythingOtherThenPatchDesiredProp() throws TransportException {
    final byte[] actualPayload = "NotificationResponseDataContainingDesiredPropertiesDocument".getBytes(StandardCharsets.UTF_8);
    final String expectedTopic = "$iothub/twin/PATCH/properties/" + "?$version=" + mockVersion;
    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);
        Deencapsulation.setField(testTwin, "stateLock", new Object());
        // act
        receivedMessage = testTwin.receive();
    } finally {
        // assert
        assertNull(receivedMessage);
    }
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) 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)

Example 88 with IotHubTransportMessage

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

the class MqttDeviceTwinTest method sendPublishesMessage.

// Tests_SRS_MQTTDEVICETWIN_25_031: [send method shall publish a message to the IOT Hub on the respective publish topic by calling method publish().]
@Test
public void sendPublishesMessage(@Mocked final Mqtt mockMqtt, @Mocked final IotHubTransportMessage mockMessage) throws TransportException {
    // arrange
    final byte[] actualPayload = { 0x61, 0x62, 0x63 };
    MqttDeviceTwin testTwin = new MqttDeviceTwin("", mockedConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
    testTwin.start();
    new NonStrictExpectations() {

        {
            mockMessage.getBytes();
            result = actualPayload;
            mockMessage.getMessageType();
            result = MessageType.DEVICE_TWIN;
            mockMessage.getDeviceOperationType();
            result = DEVICE_OPERATION_TWIN_GET_REQUEST;
            mockMessage.getVersion();
            result = mockVersion;
            mockMessage.getRequestId();
            result = "some request id";
            Deencapsulation.invoke(mockMqtt, "publish", new Class[] { String.class, Message.class }, anyString, (Message) any);
        }
    };
    // act
    testTwin.send(mockMessage);
    // assert
    new Verifications() {

        {
            mockMessage.getBytes();
            times = 1;
            Deencapsulation.invoke(mockMqtt, "subscribe", anyString);
            times = 0;
            Deencapsulation.invoke(mockMqtt, "publish", anyString, mockMessage);
            times = 1;
        }
    };
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 89 with IotHubTransportMessage

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

the class MqttDeviceTwinTest method receiveSetsDataForGetTwinResp.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_044: [If the topic is of type response then this method shall set data and operation type as DEVICE_OPERATION_TWIN_GET_RESPONSE if data is not null]
     */
@Test
public void receiveSetsDataForGetTwinResp() throws TransportException {
    final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes(StandardCharsets.UTF_8);
    final String expectedTopic = "$iothub/twin/res/" + "200" + "/?$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);
        Deencapsulation.setField(testTwin, "stateLock", new Object());
        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("200", receivedMessage.getStatus());
        assertNull(receivedMessage.getVersion());
        byte[] receivedMessageBytes = receivedMessage.getBytes();
        assertEquals(receivedMessageBytes.length, actualPayload.length);
        for (int i = 0; i < receivedMessageBytes.length; i++) {
            assertEquals(receivedMessageBytes[i], actualPayload[i]);
        }
    }
}
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)

Example 90 with IotHubTransportMessage

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

the class MqttDeviceTwinTest method sendDoesNotPublishesMessageForSubscribeToDesiredPropertiesOnCorrectTopic.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_029: [send method shall build the subscribe to desired properties request topic of the format mentioned in spec ($iothub/twin/PATCH/properties/desired/?$version={new version}) if the operation is of type DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST.]
     */
@Test
public void sendDoesNotPublishesMessageForSubscribeToDesiredPropertiesOnCorrectTopic(@Mocked final Mqtt mockMqtt, @Mocked final IotHubTransportMessage mockMessage) throws TransportException {
    // arrange
    final byte[] actualPayload = { 0x61, 0x62, 0x63 };
    final String expectedTopic = "$iothub/twin/PATCH/properties/desired/#";
    MqttDeviceTwin testTwin = new MqttDeviceTwin("", mockedConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
    testTwin.start();
    new NonStrictExpectations() {

        {
            mockMessage.getBytes();
            result = actualPayload;
            mockMessage.getMessageType();
            result = MessageType.DEVICE_TWIN;
            mockMessage.getDeviceOperationType();
            result = DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST;
            mockMessage.getVersion();
            result = mockVersion;
        }
    };
    // act
    testTwin.send(mockMessage);
    // assert
    new Verifications() {

        {
            mockMessage.getBytes();
            times = 1;
            Deencapsulation.invoke(mockMqtt, "subscribe", expectedTopic);
            times = 1;
            Deencapsulation.invoke(mockMqtt, "publish", expectedTopic, mockMessage);
            times = 0;
        }
    };
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) 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