use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin 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(@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);
byte[] receivedMessageBytes = receivedMessage.getBytes();
assertTrue(receivedMessageBytes.length == actualPayload.length);
for (int i = 0; i < receivedMessageBytes.length; i++) {
assertTrue(receivedMessageBytes[i] == actualPayload[i]);
}
}
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin 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(@Mocked final Mqtt mockMqtt) throws IOException {
final byte[] actualPayload = "NotificationResponseDataContainingDesiredPropertiesDocument".getBytes();
final String expectedTopic = "$iothub/twin/PATCH/properties/desired/";
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
assertNotNull(receivedMessage);
assertTrue(receivedMessage.getMessageType() == MessageType.DeviceTwin);
assertTrue(receivedMessage.getDeviceOperationType() == DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE);
assertTrue(receivedMessage.getRequestId() == null);
assertTrue(receivedMessage.getStatus() == null);
assertTrue(receivedMessage.getVersion() == null);
byte[] receivedMessageBytes = receivedMessage.getBytes();
assertTrue(receivedMessageBytes.length == actualPayload.length);
for (int i = 0; i < receivedMessageBytes.length; i++) {
assertTrue(receivedMessageBytes[i] == actualPayload[i]);
}
}
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method startThrowsExceptionIfSubscribesFails.
/*
**Tests_SRS_MQTTDEVICETWIN_25_019: [**start method shall subscribe to twin response topic ($iothub/twin/res/#) if connected and throw IoException otherwise.**]**
*/
@Test(expected = IOException.class)
public void startThrowsExceptionIfSubscribesFails(@Mocked final Mqtt mockMqtt) throws IOException {
try {
//arrange
new StrictExpectations() {
{
Deencapsulation.invoke(mockMqtt, "subscribe", resTopic);
result = mockIOException;
}
};
MqttDeviceTwin testTwin = new MqttDeviceTwin();
//act
testTwin.start();
} finally {
//assert
}
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method parseTopicReturnsNullIfNoDeviceTwinTopicFound.
/*
**Tests_SRS_MQTTDEVICETWIN_25_005: [**If none of the topics from the received queue match the twin topic prefix then this method shall return null string .**]**
*/
@Test
public void parseTopicReturnsNullIfNoDeviceTwinTopicFound(@Mocked final Mqtt mockMqtt) throws IOException {
//arrange
MqttDeviceTwin testTwin = new MqttDeviceTwin();
String insertTopic = "$iothub/Nottwin/res";
ConcurrentSkipListMap<String, byte[]> testMap = new ConcurrentSkipListMap<String, byte[]>();
testMap.put(insertTopic, "DataData".getBytes());
Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
//act
String parsedTopic = Deencapsulation.invoke(testTwin, "parseTopic");
//assert
assertNull(parsedTopic);
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveDoesNotSetReqIdOnResTopicIfNotFound.
@Test
public void receiveDoesNotSetReqIdOnResTopicIfNotFound(@Mocked final Mqtt mockMqtt) throws IOException {
final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes();
final String expectedTopic = "$iothub/twin/res/" + "200";
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_UNKNOWN);
assertTrue(receivedMessage.getRequestId() == null);
assertTrue(receivedMessage.getStatus().equals("200"));
assertTrue(receivedMessage.getVersion() == null);
}
}
Aggregations