Search in sources :

Example 11 with MqttDeviceTwin

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

the class MqttDeviceTwinTest method receiveParsesResponseTopicMandatoryStatusNotFoundException.

/*
    **SRS_MQTTDEVICETWIN_25_039: [**If the topic is of type response topic and if status is either a non 3 digit number or not found then receive shall throw IOException **]**
     */
@Test(expected = IOException.class)
public void receiveParsesResponseTopicMandatoryStatusNotFoundException(@Mocked final Mqtt mockMqtt) throws IOException {
    final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes();
    final String expectedTopic = "$iothub/twin/res/" + "?$rid=" + mockReqId;
    DeviceTwinMessage receivedMessage = null;
    try {
        //arrange
        MqttDeviceTwin testTwin = new MqttDeviceTwin();
        String insertTopic = expectedTopic;
        ConcurrentSkipListMap<String, byte[]> testMap = new ConcurrentSkipListMap<String, byte[]>();
        testMap.put(insertTopic, actualPayload);
        Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
        //act
        receivedMessage = (DeviceTwinMessage) testTwin.receive();
    } finally {
        //assert
        assertNull(receivedMessage);
    }
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) DeviceTwinMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) Test(org.junit.Test)

Example 12 with MqttDeviceTwin

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

the class MqttDeviceTwinTest method sendThrowsExceptionForGetTwinOnCorrectTopicIfReqIdIsNullOrEmpty.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_025: [**send method shall throw an exception if message contains a null or empty request id if the operation is of type DEVICE_OPERATION_TWIN_GET_REQUEST.**]**
     */
@Test(expected = IOException.class)
public void sendThrowsExceptionForGetTwinOnCorrectTopicIfReqIdIsNullOrEmpty(@Mocked final Mqtt mockMqtt, @Mocked final DeviceTwinMessage mockMessage) throws IOException {
    final byte[] actualPayload = { 0x61, 0x62, 0x63 };
    final String expectedTopic = "$iothub/twin/GET/?$rid=" + mockReqId;
    try {
        //arrange
        MqttDeviceTwin testTwin = new MqttDeviceTwin();
        new NonStrictExpectations() {

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

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

Example 13 with MqttDeviceTwin

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

the class MqttDeviceTwinTest method sendPublishesMessageForUpdateReportedPropertiesOnCorrectTopic.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_026: [**send method shall build the update reported properties request topic of the format mentioned in spec ($iothub/twin/PATCH/properties/reported/?$rid={request id}&$version={base version}) if the operation is of type DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST.**]**
     */
@Test
public void sendPublishesMessageForUpdateReportedPropertiesOnCorrectTopic(@Mocked final Mqtt mockMqtt, @Mocked final DeviceTwinMessage mockMessage) throws IOException {
    //arrange
    final byte[] actualPayload = { 0x61, 0x62, 0x63 };
    final String expectedTopic = "$iothub/twin/PATCH/properties/reported/?$rid=" + mockReqId + "&$version=" + mockVersion;
    MqttDeviceTwin testTwin = new MqttDeviceTwin();
    testTwin.start();
    new NonStrictExpectations() {

        {
            mockMessage.getBytes();
            result = actualPayload;
            mockMessage.getMessageType();
            result = MessageType.DeviceTwin;
            mockMessage.getDeviceOperationType();
            result = DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST;
            mockMessage.getRequestId();
            result = mockReqId;
            mockMessage.getVersion();
            result = mockVersion;
        }
    };
    //act
    testTwin.send(mockMessage);
    //assert
    new Verifications() {

        {
            mockMessage.getBytes();
            times = 2;
            Deencapsulation.invoke(mockMqtt, "publish", expectedTopic, actualPayload);
            times = 1;
        }
    };
}
Also used : MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) Test(org.junit.Test)

Example 14 with MqttDeviceTwin

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

the class MqttDeviceTwinTest method sendDoesNotThrowsIoExceptionIfMessageIsEmpty.

@Test
public void sendDoesNotThrowsIoExceptionIfMessageIsEmpty(@Mocked final Mqtt mockMqtt, @Mocked final DeviceTwinMessage mockMessage) throws IOException {
    final byte[] actualPayload = {};
    final String expectedTopic = "$iothub/twin/PATCH/properties/reported/?$rid=" + mockReqId;
    try {
        //arrange
        MqttDeviceTwin testTwin = new MqttDeviceTwin();
        testTwin.start();
        new NonStrictExpectations() {

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

            {
                mockMessage.getBytes();
                times = 2;
                Deencapsulation.invoke(mockMqtt, "publish", expectedTopic, actualPayload);
                times = 1;
            }
        };
    }
}
Also used : MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) Test(org.junit.Test)

Example 15 with MqttDeviceTwin

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

the class MqttDeviceTwinTest method receiveParsesResponseTopicInvalidStatusThrowsException.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_039: [**If the topic is of type response topic and if status is either a non 3 digit number or not found then receive shall throw IOException **]**
     */
@Test(expected = IOException.class)
public void receiveParsesResponseTopicInvalidStatusThrowsException(@Mocked final Mqtt mockMqtt) throws IOException {
    final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes();
    final String expectedTopic = "$iothub/twin/res/" + "abc/" + "?$rid=" + mockReqId;
    DeviceTwinMessage receivedMessage = null;
    try {
        //arrange
        MqttDeviceTwin testTwin = new MqttDeviceTwin();
        String insertTopic = expectedTopic;
        ConcurrentSkipListMap<String, byte[]> testMap = new ConcurrentSkipListMap<String, byte[]>();
        testMap.put(insertTopic, actualPayload);
        Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
        //act
        receivedMessage = (DeviceTwinMessage) testTwin.receive();
    } finally {
        //assert
        assertNull(receivedMessage);
    }
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) DeviceTwinMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) Test(org.junit.Test)

Aggregations

MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)42 Test (org.junit.Test)42 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)23 DeviceTwinMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage)17 DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)8 HashMap (java.util.HashMap)8 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)3 MqttIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection)3 Verifications (mockit.Verifications)3 State (com.microsoft.azure.sdk.iot.device.transport.State)2 MqttMessaging (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging)2 IotHubSasToken (com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken)1 IOException (java.io.IOException)1 NonStrictExpectations (mockit.NonStrictExpectations)1