Search in sources :

Example 81 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project azure-iot-sdk-java by Azure.

the class MqttDeviceMethodTest method receiveReturnsEmptyPayLoadIfNullPayloadParsed.

@Test
public void receiveReturnsEmptyPayLoadIfNullPayloadParsed() throws TransportException {
    // arrange
    String topic = "$iothub/methods/POST/testMethod/?$rid=10";
    byte[] actualPayload = "".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(0, testMessage.getBytes().length);
    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 82 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project azure-iot-sdk-java by Azure.

the class MqttDeviceTwinTest method receiveSetsReqIdOnResTopic.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_040: [If the topic is of type response topic then this method shall parse further to look for request id which if found is set by calling setRequestId]
     */
@Test
public void receiveSetsReqIdOnResTopic() 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());
    }
}
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 83 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project azure-iot-sdk-java by Azure.

the class MqttDeviceTwinTest method receiveThrowsTransportExceptionOnAnythingOtherThenPatchOrResTopic.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_037: [This method shall parse topic to look for only either twin response topic or twin patch topic and thorw TransportException other wise.]
     */
@Test(expected = TransportException.class)
public void receiveThrowsTransportExceptionOnAnythingOtherThenPatchOrResTopic() throws TransportException {
    final byte[] actualPayload = "NotificationResponseDataContainingDesiredPropertiesDocument".getBytes(StandardCharsets.UTF_8);
    final String expectedTopic = "$iothub/twin/NOTPATCH_NOTRES/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 84 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project azure-iot-sdk-java by Azure.

the class MqttDeviceTwinTest method receiveParsesPatchTopicForDesiredPropertiesNotificationSucceeds.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_042: [If the topic is of type patch for desired properties then this method shall parse further to look for version which if found is set by calling setVersion]
     */
@Test
public void receiveParsesPatchTopicForDesiredPropertiesNotificationSucceeds() throws TransportException {
    final byte[] actualPayload = "UpdateDesiredPropertiesNotificationData".getBytes(StandardCharsets.UTF_8);
    final String expectedTopic = "$iothub/twin/PATCH/properties/desired/" + "?$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);
        // act
        receivedMessage = (IotHubTransportMessage) testTwin.receive();
    } finally {
        // assert
        assertNotNull(receivedMessage);
        assertSame(receivedMessage.getMessageType(), MessageType.DEVICE_TWIN);
        assertSame(receivedMessage.getDeviceOperationType(), DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE);
        assertEquals(receivedMessage.getVersion(), mockVersion);
        assertNull(receivedMessage.getRequestId());
        assertNull(receivedMessage.getStatus());
    }
}
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) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) 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 85 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project azure-iot-sdk-java by Azure.

the class MqttDeviceTwinTest method receiveDoesNotSetReqIdOnResTopicIfNotFound.

@Test
public void receiveDoesNotSetReqIdOnResTopicIfNotFound() throws TransportException {
    final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes(StandardCharsets.UTF_8);
    final String expectedTopic = "$iothub/twin/res/" + "200";
    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);
        Map<String, DeviceOperations> requestMap = new HashMap<>();
        requestMap.put(mockReqId, DEVICE_OPERATION_TWIN_GET_REQUEST);
        Deencapsulation.setField(testTwin, "requestMap", requestMap);
        Deencapsulation.setField(testTwin, "stateLock", new Object());
        // act
        receivedMessage = testTwin.receive();
    } finally {
        // assert
        assertNotNull(receivedMessage);
        assertSame(receivedMessage.getMessageType(), MessageType.DEVICE_TWIN);
        assertSame(receivedMessage.getDeviceOperationType(), DEVICE_OPERATION_UNKNOWN);
        assertNull(receivedMessage.getRequestId());
        assertEquals("200", receivedMessage.getStatus());
        assertNull(receivedMessage.getVersion());
    }
}
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)

Aggregations

MutablePair (org.apache.commons.lang3.tuple.MutablePair)116 Pair (org.apache.commons.lang3.tuple.Pair)49 ArrayList (java.util.ArrayList)32 Test (org.junit.Test)32 HashMap (java.util.HashMap)29 Message (com.microsoft.azure.sdk.iot.device.Message)27 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)27 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)23 List (java.util.List)20 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)17 Map (java.util.Map)14 IOException (java.io.IOException)13 WalkPosition (org.openbw.bwapi4j.WalkPosition)9 MiniTile (bwem.tile.MiniTile)8 DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)8 AreaId (bwem.area.typedef.AreaId)7 Collectors (java.util.stream.Collectors)7 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)7 HashSet (java.util.HashSet)6 TileImpl (bwem.tile.TileImpl)5