Search in sources :

Example 56 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 57 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 58 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)

Example 59 with MutablePair

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

the class MqttDeviceTwinTest method receiveParsesResponseTopicInvalidStatusThrowsException.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_039: [If the topic is of type response topic and if status is either a non 3 digit number or not found then receive shall throw TransportException ]
     */
@Test(expected = TransportException.class)
public void receiveParsesResponseTopicInvalidStatusThrowsException() throws TransportException {
    final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes(StandardCharsets.UTF_8);
    final String expectedTopic = "$iothub/twin/res/" + "abc/" + "?$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());
        // 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 60 with MutablePair

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

the class MqttTest method receiveSuccessWithModuleIdAndInputName.

// Tests_SRS_Mqtt_34_050: [This function shall extract the inputName from the topic if the topic string fits the following convention: 'devices/<deviceId>/modules/<moduleId>/inputs/<inputName>']
// Tests_SRS_Mqtt_34_051: [This function shall extract the moduleId from the topic if the topic string fits the following convention: 'devices/<deviceId>/modules/<moduleId>']
@Test
public void receiveSuccessWithModuleIdAndInputName() throws MqttException, TransportException {
    // arrange
    final byte[] payload = { 0x61, 0x62, 0x63 };
    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;
        }
    };
    Queue<Pair<String, byte[]>> testreceivedMessages = new ConcurrentLinkedQueue<>();
    Deencapsulation.setField(mockMqtt, "receivedMessages", testreceivedMessages);
    testreceivedMessages.add(new MutablePair<>(MOCK_PARSE_TOPIC_WITH_INPUT_NAME, payload));
    Deencapsulation.invoke(mockMqtt, "connect");
    // act
    Message receivedMessage = mockMqtt.receive();
    // assert
    assertEquals(expectedInputName, receivedMessage.getInputName());
    assertEquals(expectedModuleId, receivedMessage.getConnectionModuleId());
}
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)67 Pair (org.apache.commons.lang3.tuple.Pair)33 Test (org.junit.Test)28 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 HashMap (java.util.HashMap)18 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)17 ArrayList (java.util.ArrayList)17 IOException (java.io.IOException)9 DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)8 AreaId (bwem.area.typedef.AreaId)7 List (java.util.List)7 Map (java.util.Map)6 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)5 MiniTile (bwem.tile.MiniTile)4 Altitude (bwem.typedef.Altitude)4 ContainerStartRequest (com.datatorrent.stram.StreamingContainerAgent.ContainerStartRequest)4 ContainerRequest (org.apache.hadoop.yarn.client.api.AMRMClient.ContainerRequest)4 WalkPosition (org.openbw.bwapi4j.WalkPosition)4