Search in sources :

Example 21 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 receiveThrowsIfRIDCouldNotBeParsed.

/*
    Tests_SRS_MqttDeviceMethod_25_031: [**If request id is not found or is null then receive shall throw IOException **]**
     */
@Test(expected = IOException.class)
public void receiveThrowsIfRIDCouldNotBeParsed() throws IOException {
    //arrange
    String topic = "$iothub/methods/POST/testMethod/";
    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();
}
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 22 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 sendSucceedsCallsPublish.

/*
    Tests_SRS_MqttDeviceMethod_25_022: [**send method shall build the publish topic of the format mentioned in spec ($iothub/methods/res/{status}/?$rid={request id}) and publish if the operation is of type DEVICE_OPERATION_METHOD_SEND_RESPONSE.**]**
     */
@Test
public void sendSucceedsCallsPublish() throws IOException {
    final byte[] actualPayload = "TestMessage".getBytes();
    final DeviceMethodMessage testMessage = new DeviceMethodMessage(actualPayload);
    testMessage.setDeviceOperationType(DEVICE_OPERATION_METHOD_SEND_RESPONSE);
    testMessage.setRequestId("ReqId");
    testMessage.setStatus("testStatus");
    MqttDeviceMethod testMethod = new MqttDeviceMethod();
    Map<String, DeviceOperations> testRequestMap = new HashMap<>();
    testRequestMap.put("ReqId", DEVICE_OPERATION_METHOD_RECEIVE_REQUEST);
    Deencapsulation.setField(testMethod, "requestMap", testRequestMap);
    testMethod.start();
    //act
    testMethod.send(testMessage);
    //assert
    new Verifications() {

        {
            Deencapsulation.invoke(mockedMqtt, "publish", anyString, actualPayload);
            maxTimes = 1;
        }
    };
    assertTrue(testRequestMap.isEmpty());
}
Also used : HashMap(java.util.HashMap) DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) 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 23 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 sendThrowsOnInvalidOperation.

@Test(expected = IOException.class)
public void sendThrowsOnInvalidOperation() throws IOException {
    final byte[] actualPayload = "TestMessage".getBytes();
    final DeviceMethodMessage testMessage = new DeviceMethodMessage(actualPayload);
    testMessage.setDeviceOperationType(DEVICE_OPERATION_UNKNOWN);
    MqttDeviceMethod testMethod = new MqttDeviceMethod();
    testMethod.start();
    //act
    testMethod.send(testMessage);
}
Also used : DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage) MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) Test(org.junit.Test)

Example 24 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() throws IOException {
    //arrange
    String actualSubscribeTopic = "$iothub/methods/POST/#";
    String actualResTopic = "$iothub/methods/res";
    //act
    MqttDeviceMethod testMethod = new MqttDeviceMethod();
    //assert
    String testSubscribeTopic = Deencapsulation.getField(testMethod, "subscribeTopic");
    String testResTopic = Deencapsulation.getField(testMethod, "responseTopic");
    assertNotNull(testSubscribeTopic);
    assertNotNull(testResTopic);
    assertTrue(testSubscribeTopic.equals(actualSubscribeTopic));
    assertTrue(testResTopic.equals(actualResTopic));
}
Also used : MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) Test(org.junit.Test)

Example 25 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 sendThrowsOnSendingResponseWithoutReceivingMethodInvoke.

/*
    Tests_SRS_MqttDeviceMethod_25_023: [**send method shall throw an exception if a response is sent without having a method invoke on the request id if the operation is of type DEVICE_OPERATION_METHOD_SEND_RESPONSE.**]**
     */
@Test(expected = IOException.class)
public void sendThrowsOnSendingResponseWithoutReceivingMethodInvoke() throws IOException {
    final byte[] actualPayload = "TestMessage".getBytes();
    final DeviceMethodMessage testMessage = new DeviceMethodMessage(actualPayload);
    testMessage.setDeviceOperationType(DEVICE_OPERATION_METHOD_SEND_RESPONSE);
    testMessage.setRequestId("ReqId");
    testMessage.setStatus("testStatus");
    MqttDeviceMethod testMethod = new MqttDeviceMethod();
    testMethod.start();
    //act
    testMethod.send(testMessage);
}
Also used : DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage) 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