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());
}
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]);
}
}
}
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);
}
}
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]);
}
}
}
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());
}
Aggregations