Search in sources :

Example 86 with MutablePair

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

the class MqttDeviceTwinTest method receiveReturnsNullMessageIfTopicNotFound.

/*
     * Tests_SRS_MQTTDEVICETWIN_34_034: [If the call peekMessage returns null or empty string then this method shall do nothing and return null]
     */
@Test
public void receiveReturnsNullMessageIfTopicNotFound(@Mocked final Mqtt mockMqtt) throws TransportException {
    // 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<String, byte[]>(null, new byte[5]));
    Deencapsulation.setField(testTwin, "receivedMessages", testreceivedMessages);
    Deencapsulation.setField(testTwin, "stateLock", new Object());
    Deencapsulation.setField(testTwin, "receivedMessagesLock", new Object());
    // act
    // assert
    assertNull(testTwin.receive());
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) 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 87 with MutablePair

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

the class MqttDeviceTwinTest method receiveSetsDataForDesiredPropNotifResp.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_046: [If the topic is of type patch for desired properties then this method shall set the data and operation type as DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE if data is not null or empty]
     */
@Test
public void receiveSetsDataForDesiredPropNotifResp() throws TransportException {
    final byte[] actualPayload = "NotificationResponseDataContainingDesiredPropertiesDocument".getBytes(StandardCharsets.UTF_8);
    final String expectedTopic = "$iothub/twin/PATCH/properties/desired/";
    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 = testTwin.receive();
    } finally {
        // assert
        assertNotNull(receivedMessage);
        assertSame(receivedMessage.getMessageType(), MessageType.DEVICE_TWIN);
        assertSame(receivedMessage.getDeviceOperationType(), DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE);
        assertNull(receivedMessage.getRequestId());
        assertNull(receivedMessage.getStatus());
        assertNull(receivedMessage.getVersion());
        byte[] receivedMessageBytes = receivedMessage.getBytes();
        assertEquals(receivedMessageBytes.length, actualPayload.length);
        for (int i = 0; i < receivedMessageBytes.length; i++) {
            assertEquals(receivedMessageBytes[i], actualPayload[i]);
        }
    }
}
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 88 with MutablePair

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

the class MqttDeviceTwinTest method receiveThrowsTransportExceptionOnAnythingOtherThenPatchDesiredProp.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_043: [If the topic is not of type response for desired properties then this method shall throw TransportException]
     */
@Test(expected = TransportException.class)
public void receiveThrowsTransportExceptionOnAnythingOtherThenPatchDesiredProp() throws TransportException {
    final byte[] actualPayload = "NotificationResponseDataContainingDesiredPropertiesDocument".getBytes(StandardCharsets.UTF_8);
    final String expectedTopic = "$iothub/twin/PATCH/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 89 with MutablePair

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

the class MqttDeviceTwinTest method receiveSetsDataForGetTwinResp.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_044: [If the topic is of type response then this method shall set data and operation type as DEVICE_OPERATION_TWIN_GET_RESPONSE if data is not null]
     */
@Test
public void receiveSetsDataForGetTwinResp() 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());
        byte[] receivedMessageBytes = receivedMessage.getBytes();
        assertEquals(receivedMessageBytes.length, actualPayload.length);
        for (int i = 0; i < receivedMessageBytes.length; i++) {
            assertEquals(receivedMessageBytes[i], actualPayload[i]);
        }
    }
}
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 90 with MutablePair

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

the class MqttTest method receiveSuccessWithSystemProperties.

// Tests_SRS_Mqtt_34_057: [This function shall parse the messageId, correlationId, outputname, content encoding and content type from the provided property string]
@Test
public void receiveSuccessWithSystemProperties() throws TransportException, MqttException, UnsupportedEncodingException {
    // arrange
    final byte[] payload = { 0x61, 0x62, 0x63 };
    final String msgId = "69ea4caf-d83e-454b-81f2-caafda4c81c8";
    final String corId = "169c34b3-99b0-49f9-b0f6-8fa9d2c99345";
    final String expTime = "1234";
    final String contentEncoding = "utf-8";
    final String contentType = "application/json";
    final String outputName = "outputChannel1";
    final String to = "/devices/deviceID/messages/deviceBound";
    final String mockParseTopicWithUnusualCharacters = "devices/deviceID/messages/devicebound/%24.mid=" + msgId + "&%24.exp=" + expTime + "&%24.to=" + URLEncoder.encode(to, StandardCharsets.UTF_8.name()) + "&%24.cid=" + corId + "&iothub-ack=full&%24.ce=" + contentEncoding + "&%24.ct=" + URLEncoder.encode(contentType, StandardCharsets.UTF_8.name()) + "&%24.on=" + outputName + "&property1=%24&property2=%26&%25=_&finalProperty=.";
    baseConnectExpectation();
    final Mqtt mockMqtt = new MqttMessaging(CLIENT_ID, null, "", false, mockMqttConnectionOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
    Deencapsulation.invoke(mockMqtt, "setMqttAsyncClient", mockMqttAsyncClient);
    new NonStrictExpectations() {

        {
            mockMqttAsyncClient.isConnected();
            result = true;
        }
    };
    Deencapsulation.invoke(mockMqtt, "connect");
    Queue<Pair<String, byte[]>> testreceivedMessages = new ConcurrentLinkedQueue<>();
    Deencapsulation.setField(mockMqtt, "receivedMessages", testreceivedMessages);
    testreceivedMessages.add(new MutablePair<>(mockParseTopicWithUnusualCharacters, payload));
    // act
    Message receivedMessage = mockMqtt.receive();
    // assert
    byte[] actualPayload = receivedMessage.getBytes();
    assertEquals(actualPayload.length, payload.length);
    for (int i = 0; i < payload.length; i++) {
        assertEquals(actualPayload[i], payload[i]);
    }
    assertEquals(msgId, receivedMessage.getMessageId());
    assertEquals(corId, receivedMessage.getCorrelationId());
    assertEquals(contentEncoding, receivedMessage.getContentEncoding());
    assertEquals(contentType, receivedMessage.getContentType());
    assertEquals(outputName, receivedMessage.getOutputName());
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) 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