Search in sources :

Example 31 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 receiveThrowsUnsupportedExceptionOnAnythingOtherThenPatchOrResTopic.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_037: [**This method shall parse topic to look for only either twin response topic or twin patch topic and thorw unsupportedoperation exception other wise.**]**
     */
@Test(expected = UnsupportedOperationException.class)
public void receiveThrowsUnsupportedExceptionOnAnythingOtherThenPatchOrResTopic(@Mocked final Mqtt mockMqtt) throws IOException {
    final byte[] actualPayload = "NotificationResponseDataContainingDesiredPropertiesDocument".getBytes();
    final String expectedTopic = "$iothub/twin/NOTPATCH_NOTRES/properties/" + "?$version=" + mockVersion;
    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 32 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 parseTopicThrowsExceptionIfQueueIsNull.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_007: [**If receiveMessage queue is null then parseTopic shall throw IOException.**]**
     */
@Test(expected = IOException.class)
public void parseTopicThrowsExceptionIfQueueIsNull(@Mocked final Mqtt mockMqtt) throws IOException {
    //arrange
    MqttDeviceTwin testTwin = new MqttDeviceTwin();
    ConcurrentSkipListMap<String, byte[]> testMap = null;
    Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
    //act
    String parsedTopic = Deencapsulation.invoke(testTwin, "parseTopic");
    //assert
    assertNull(parsedTopic);
}
Also used : MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) Test(org.junit.Test)

Example 33 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 sendThrowsExceptionForUpdateReportedPropertiesOnCorrectTopicIfReqIdIsNullOrEmpty.

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

            {
                mockMessage.getBytes();
                result = actualPayload;
                mockMessage.getMessageType();
                result = MessageType.DeviceTwin;
                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);
                times = 0;
            }
        };
    }
}
Also used : MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) Test(org.junit.Test)

Example 34 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 stopUnsubscribesFromDeviceTwinResponse.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_020: [**stop method shall unsubscribe from twin response topic ($iothub/twin/res/#).**]**
     */
@Test
public void stopUnsubscribesFromDeviceTwinResponse(@Mocked final Mqtt mockMqtt) throws IOException {
    //arrange
    MqttDeviceTwin testTwin = new MqttDeviceTwin();
    Deencapsulation.setField(testTwin, "isStarted", true);
    //act
    testTwin.stop();
    //assert
    new Verifications() {

        {
            Deencapsulation.invoke(mockMqtt, "unsubscribe", resTopic);
            times = 1;
        }
    };
}
Also used : MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) Test(org.junit.Test)

Example 35 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 constructorConstructsSubscribeTopicForTwin.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_001: [**The constructor shall instantiate super class without any parameters.**]**
    **Tests_SRS_MQTTDEVICETWIN_25_002: [**The constructor shall construct device twin response subscribeTopic.**]**
     */
@Test
public void constructorConstructsSubscribeTopicForTwin(@Mocked final Mqtt mockMqtt) throws IOException {
    //arrange
    //act
    MqttDeviceTwin testTwin = new MqttDeviceTwin();
    //assert
    String actualSubscribeTopic = Deencapsulation.getField(testTwin, "subscribeTopic");
    assertNotNull(actualSubscribeTopic);
    assertEquals(actualSubscribeTopic, resTopic);
}
Also used : 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