Search in sources :

Example 16 with MqttDeviceMethod

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

the class MqttDeviceMethodTest method receiveSucceeds.

/*
    * Tests_SRS_MQTTDEVICEMETHOD_25_026: [**This method shall call peekMessage to get the message payload from the received Messages queue corresponding to the messaging client's operation.**]**
    * Tests_SRS_MQTTDEVICEMETHOD_25_028: [**If the topic is of type post topic then this method shall parse further for method name and set it for the message by calling setMethodName for the message**]**
    * Tests_SRS_MQTTDEVICEMETHOD_25_030: [**If the topic is of type post topic then this method shall parse further to look for request id which if found is set by calling setRequestId**]**
    * Tests_SRS_MQTTDEVICEMETHOD_25_032: [**If the topic is of type post topic and if method name and request id has been successfully parsed then this method shall set operation type as DEVICE_OPERATION_METHOD_RECEIVE_REQUEST **]**
    */
@Test
public void receiveSucceeds() throws TransportException {
    // arrange
    String topic = "$iothub/methods/POST/testMethod/?$rid=10";
    byte[] actualPayload = "TestPayload".getBytes(StandardCharsets.UTF_8);
    testreceivedMessages.add(new MutablePair<>(topic, actualPayload));
    MqttDeviceMethod testMethod = new MqttDeviceMethod("", mockConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
    Deencapsulation.setField(testMethod, "receivedMessages", testreceivedMessages);
    testMethod.start();
    // act
    Message testMessage = testMethod.receive();
    IotHubTransportMessage testDMMessage = (IotHubTransportMessage) testMessage;
    // assert
    assertNotNull(testMessage);
    assertEquals(testMessage.getMessageType(), MessageType.DEVICE_METHODS);
    assertEquals(testDMMessage.getRequestId(), String.valueOf(10));
    assertEquals("testMethod", testDMMessage.getMethodName());
    assertEquals(testDMMessage.getDeviceOperationType(), DEVICE_OPERATION_METHOD_RECEIVE_REQUEST);
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 17 with MqttDeviceMethod

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

the class MqttDeviceMethodTest method constructorSucceeds.

/*
    Tests_SRS_MqttDeviceMethod_25_001: [**The constructor shall instantiate super class without any parameters.**]**

    Tests_SRS_MqttDeviceMethod_25_002: [**The constructor shall create subscribe and response topics strings for device methods as per the spec.**]**
     */
@Test
public void constructorSucceeds(@Mocked final Mqtt mockMqtt) throws TransportException {
    // arrange
    String actualSubscribeTopic = "$iothub/methods/POST/#";
    String actualResTopic = "$iothub/methods/res";
    // act
    MqttDeviceMethod testMethod = new MqttDeviceMethod("", mockConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
    // assert
    String testSubscribeTopic = Deencapsulation.getField(testMethod, "subscribeTopic");
    String testResTopic = Deencapsulation.getField(testMethod, "responseTopic");
    assertNotNull(testSubscribeTopic);
    assertNotNull(testResTopic);
    assertEquals(testSubscribeTopic, actualSubscribeTopic);
    assertEquals(testResTopic, actualResTopic);
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 18 with MqttDeviceMethod

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

the class MqttDeviceMethodTest method sendSucceedsCallsSubscribe.

/*
    Tests_SRS_MqttDeviceMethod_25_004: [**parseTopic shall look for the method topic($iothub/methods) prefix from received message queue as per spec and if found shall return it as string.**]**

    Tests_SRS_MqttDeviceMethod_25_005: [**If none of the topics from the received queue match the methods topic prefix then this method shall return null string .**]**

    Tests_SRS_MqttDeviceMethod_25_006: [**If received messages queue is empty then parseTopic shall return null string.**]**

    Tests_SRS_MqttDeviceMethod_25_020: [**send method shall subscribe to topic from spec ($iothub/methods/POST/#) if the operation is of type DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST.**]**
     */
@Test
public void sendSucceedsCallsSubscribe() throws IOException {
    //arrange
    final String actualSubscribeTopic = "$iothub/methods/POST/#";
    byte[] actualPayload = "TestMessage".getBytes();
    DeviceMethodMessage testMessage = new DeviceMethodMessage(actualPayload);
    testMessage.setDeviceOperationType(DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST);
    MqttDeviceMethod testMethod = new MqttDeviceMethod();
    testMethod.start();
    //act
    testMethod.send(testMessage);
    //assert
    new Verifications() {

        {
            Deencapsulation.invoke(mockedMqtt, "subscribe", actualSubscribeTopic);
            maxTimes = 1;
        }
    };
}
Also used : DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage) MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) Verifications(mockit.Verifications) Test(org.junit.Test)

Example 19 with MqttDeviceMethod

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

the class MqttDeviceMethodTest method receiveReturnsNullMessageIfTopicNotFound.

/*
    Tests_SRS_MqttDeviceMethod_25_025: [**If the call parseTopic returns null or empty string then this method shall do nothing and return null**]**
     */
@Test
public void receiveReturnsNullMessageIfTopicNotFound() throws IOException {
    //arrange
    String topic = "$iothub/not_methods/POST/testMethod/?$rid=10";
    byte[] actualPayload = "TestPayload".getBytes();
    ConcurrentSkipListMap<String, byte[]> testAllReceivedMessages = new ConcurrentSkipListMap<>();
    testAllReceivedMessages.put(topic, actualPayload);
    Deencapsulation.setField(mockedMqtt, "allReceivedMessages", testAllReceivedMessages);
    MqttDeviceMethod testMethod = new MqttDeviceMethod();
    testMethod.start();
    //act
    Message testMessage = testMethod.receive();
    //assert
    assertNull(testMessage);
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) Message(com.microsoft.azure.sdk.iot.device.Message) DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage) MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) Test(org.junit.Test)

Example 20 with MqttDeviceMethod

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

the class MqttDeviceMethodTest method sendThrowsOnMessageNull.

/*
    Tests_SRS_MqttDeviceMethod_25_016: [**send method shall throw an exception if the message is null.**]**
     */
@Test(expected = IllegalArgumentException.class)
public void sendThrowsOnMessageNull() throws IOException {
    MqttDeviceMethod testMethod = new MqttDeviceMethod();
    testMethod.start();
    //act
    testMethod.send(null);
}
Also used : MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) Test(org.junit.Test)

Aggregations

MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)41 Test (org.junit.Test)41 Message (com.microsoft.azure.sdk.iot.device.Message)22 Verifications (mockit.Verifications)17 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)16 MutablePair (org.apache.commons.lang3.tuple.MutablePair)16 Pair (org.apache.commons.lang3.tuple.Pair)16 DeviceMethodMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage)14 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)6 MqttIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection)4 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)3 MqttMessaging (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging)3 DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)2 IotHubSasToken (com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken)2 State (com.microsoft.azure.sdk.iot.device.transport.State)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)2 NonStrictExpectations (mockit.NonStrictExpectations)2