use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveParsesResponseTopicMandatoryStatusNotFoundException.
/*
**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 IOException **]**
*/
@Test(expected = IOException.class)
public void receiveParsesResponseTopicMandatoryStatusNotFoundException(@Mocked final Mqtt mockMqtt) throws IOException {
final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes();
final String expectedTopic = "$iothub/twin/res/" + "?$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);
//act
receivedMessage = (DeviceTwinMessage) testTwin.receive();
} finally {
//assert
assertNull(receivedMessage);
}
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method sendThrowsExceptionForGetTwinOnCorrectTopicIfReqIdIsNullOrEmpty.
/*
**Tests_SRS_MQTTDEVICETWIN_25_025: [**send method shall throw an exception if message contains a null or empty request id if the operation is of type DEVICE_OPERATION_TWIN_GET_REQUEST.**]**
*/
@Test(expected = IOException.class)
public void sendThrowsExceptionForGetTwinOnCorrectTopicIfReqIdIsNullOrEmpty(@Mocked final Mqtt mockMqtt, @Mocked final DeviceTwinMessage mockMessage) throws IOException {
final byte[] actualPayload = { 0x61, 0x62, 0x63 };
final String expectedTopic = "$iothub/twin/GET/?$rid=" + mockReqId;
try {
//arrange
MqttDeviceTwin testTwin = new MqttDeviceTwin();
new NonStrictExpectations() {
{
mockMessage.getBytes();
result = actualPayload;
mockMessage.getMessageType();
result = MessageType.DeviceTwin;
mockMessage.getDeviceOperationType();
result = DEVICE_OPERATION_TWIN_GET_REQUEST;
mockMessage.getRequestId();
result = null;
}
};
//act
testTwin.send(mockMessage);
} finally {
//assert
new Verifications() {
{
mockMessage.getBytes();
times = 1;
Deencapsulation.invoke(mockMqtt, "publish", expectedTopic, actualPayload);
times = 0;
}
};
}
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method sendPublishesMessageForUpdateReportedPropertiesOnCorrectTopic.
/*
**Tests_SRS_MQTTDEVICETWIN_25_026: [**send method shall build the update reported properties request topic of the format mentioned in spec ($iothub/twin/PATCH/properties/reported/?$rid={request id}&$version={base version}) if the operation is of type DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST.**]**
*/
@Test
public void sendPublishesMessageForUpdateReportedPropertiesOnCorrectTopic(@Mocked final Mqtt mockMqtt, @Mocked final DeviceTwinMessage mockMessage) throws IOException {
//arrange
final byte[] actualPayload = { 0x61, 0x62, 0x63 };
final String expectedTopic = "$iothub/twin/PATCH/properties/reported/?$rid=" + mockReqId + "&$version=" + mockVersion;
MqttDeviceTwin testTwin = new MqttDeviceTwin();
testTwin.start();
new NonStrictExpectations() {
{
mockMessage.getBytes();
result = actualPayload;
mockMessage.getMessageType();
result = MessageType.DeviceTwin;
mockMessage.getDeviceOperationType();
result = DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST;
mockMessage.getRequestId();
result = mockReqId;
mockMessage.getVersion();
result = mockVersion;
}
};
//act
testTwin.send(mockMessage);
//assert
new Verifications() {
{
mockMessage.getBytes();
times = 2;
Deencapsulation.invoke(mockMqtt, "publish", expectedTopic, actualPayload);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method sendDoesNotThrowsIoExceptionIfMessageIsEmpty.
@Test
public void sendDoesNotThrowsIoExceptionIfMessageIsEmpty(@Mocked final Mqtt mockMqtt, @Mocked final DeviceTwinMessage mockMessage) throws IOException {
final byte[] actualPayload = {};
final String expectedTopic = "$iothub/twin/PATCH/properties/reported/?$rid=" + mockReqId;
try {
//arrange
MqttDeviceTwin testTwin = new MqttDeviceTwin();
testTwin.start();
new NonStrictExpectations() {
{
mockMessage.getBytes();
result = actualPayload;
mockMessage.getMessageType();
result = MessageType.DeviceTwin;
mockMessage.getDeviceOperationType();
result = DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST;
mockMessage.getRequestId();
result = mockReqId;
}
};
//act
testTwin.send(mockMessage);
} finally {
//assert
new Verifications() {
{
mockMessage.getBytes();
times = 2;
Deencapsulation.invoke(mockMqtt, "publish", expectedTopic, actualPayload);
times = 1;
}
};
}
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin 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 IOException **]**
*/
@Test(expected = IOException.class)
public void receiveParsesResponseTopicInvalidStatusThrowsException(@Mocked final Mqtt mockMqtt) throws IOException {
final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes();
final String expectedTopic = "$iothub/twin/res/" + "abc/" + "?$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);
//act
receivedMessage = (DeviceTwinMessage) testTwin.receive();
} finally {
//assert
assertNull(receivedMessage);
}
}
Aggregations