use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method parsePayloadReturnNullIfTopicIsNull.
/*
**Tests_SRS_MQTTDEVICETWIN_25_010: [**If the topic is null then parsePayload shall stop parsing for payload and return.**]**
*/
@Test
public void parsePayloadReturnNullIfTopicIsNull(@Mocked final Mqtt mockMqtt) throws IOException {
//arrange
MqttDeviceTwin testTwin = new MqttDeviceTwin();
//act
byte[] parsedPayload = Deencapsulation.invoke(testTwin, "parsePayload", String.class);
//assert
assertNull(parsedPayload);
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method parsePayloadReturnsBytesForSpecifiedTopic.
/*
**Tests_SRS_MQTTDEVICETWIN_25_009: [**This parsePayload method look for payload for the corresponding topic from the received messagesqueue.**]**
*/
@Test
public void parsePayloadReturnsBytesForSpecifiedTopic(@Mocked final Mqtt mockMqtt) throws IOException {
//arrange
MqttDeviceTwin testTwin = new MqttDeviceTwin();
String insertTopic = "$iothub/twin/" + anyString;
final byte[] insertMessage = { 0x61, 0x62, 0x63 };
ConcurrentSkipListMap<String, byte[]> testMap = new ConcurrentSkipListMap<String, byte[]>();
testMap.put(insertTopic, insertMessage);
Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
//act
byte[] parsedPayload = Deencapsulation.invoke(testTwin, "parsePayload", insertTopic);
//assert
assertNotNull(parsedPayload);
assertEquals(insertMessage.length, parsedPayload.length);
for (int i = 0; i < parsedPayload.length; i++) {
assertEquals(parsedPayload[i], insertMessage[i]);
}
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveParsesResponseTopicForGetTwinSucceeds.
/*
**Tests_SRS_MQTTDEVICETWIN_25_038: [**If the topic is of type response topic then this method shall parse further for status and set it for the message by calling setStatus for the message**]**
*/
@Test
public void receiveParsesResponseTopicForGetTwinSucceeds(@Mocked final Mqtt mockMqtt) throws IOException {
final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes();
final String expectedTopic = "$iothub/twin/res/" + "200" + "/?$rid=" + mockReqId;
DeviceTwinMessage receivedMessage = null;
try {
//arrange
MqttDeviceTwin testTwin = new MqttDeviceTwin();
String insertTopic = expectedTopic;
ConcurrentSkipListMap<String, byte[]> testMap = new ConcurrentSkipListMap<String, byte[]>();
testMap.put(insertTopic, actualPayload);
Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
Map<String, DeviceOperations> requestMap = new HashMap<>();
requestMap.put(mockReqId, DEVICE_OPERATION_TWIN_GET_REQUEST);
Deencapsulation.setField(testTwin, "requestMap", requestMap);
//act
receivedMessage = (DeviceTwinMessage) testTwin.receive();
} finally {
//assert
assertNotNull(receivedMessage);
assertTrue(receivedMessage.getMessageType() == MessageType.DeviceTwin);
assertTrue(receivedMessage.getDeviceOperationType() == DEVICE_OPERATION_TWIN_GET_RESPONSE);
assertTrue(receivedMessage.getRequestId().equals(mockReqId));
assertTrue(receivedMessage.getStatus().equals("200"));
assertTrue(receivedMessage.getVersion() == null);
}
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method parsePayloadThrowsExceptionTopicIsNotFound.
/*
**Tests_SRS_MQTTDEVICETWIN_25_011: [**If the topic is non-null and received messagesqueue could not locate the payload then this method shall throw IOException**]**
*/
@Test(expected = IOException.class)
public void parsePayloadThrowsExceptionTopicIsNotFound(@Mocked final Mqtt mockMqtt) throws IOException {
//arrange
MqttDeviceTwin testTwin = new MqttDeviceTwin();
String insertTopic = "$iothub/twin/res";
String notTwinTopic = "$iothub/NotTwin/res";
ConcurrentSkipListMap<String, byte[]> testMap = new ConcurrentSkipListMap<String, byte[]>();
testMap.put(insertTopic, "DataData".getBytes());
Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
//act
byte[] parsedPayload = Deencapsulation.invoke(testTwin, "parsePayload", notTwinTopic);
//assert
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveReturnsNullMessageIfTopicNotFound.
/*
**Tests_SRS_MQTTDEVICETWIN_25_034: [**If the call parseTopic returns null or empty string then this method shall do nothing and return null**]**
*/
@Test
public void receiveReturnsNullMessageIfTopicNotFound(@Mocked final Mqtt mockMqtt) throws IOException {
final byte[] actualPayload = "NotificationResponseDataContainingDesiredPropertiesDocument".getBytes();
final String expectedTopic = "$iothub/NOTtwin/NOTPATCH_NOTRES/properties/" + "?$version=" + mockVersion;
DeviceTwinMessage receivedMessage = null;
try {
//arrange
MqttDeviceTwin testTwin = new MqttDeviceTwin();
String insertTopic = expectedTopic;
ConcurrentSkipListMap<String, byte[]> testMap = new ConcurrentSkipListMap<String, byte[]>();
testMap.put(insertTopic, actualPayload);
Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
//act
receivedMessage = (DeviceTwinMessage) testTwin.receive();
} finally {
//assert
assertNull(receivedMessage);
}
}
Aggregations