use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveDoesNotSetVersionForDesiredPropNotifRespIfNotFound.
/*
**Tests_SRS_MQTTDEVICETWIN_25_042: [**If the topic is of type patch for desired properties then this method shall parse further to look for version which if found is set by calling setVersion**]**
*/
@Test
public void receiveDoesNotSetVersionForDesiredPropNotifRespIfNotFound(@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 parsePayloadThrowsExceptionIfQueueIsNull.
/*
**Tests_SRS_MQTTDEVICETWIN_25_012: [**If receiveMessage queue is null then this method shall throw IOException.**]**
*/
@Test(expected = IOException.class)
public void parsePayloadThrowsExceptionIfQueueIsNull(@Mocked final Mqtt mockMqtt) throws IOException {
//arrange
MqttDeviceTwin testTwin = new MqttDeviceTwin();
ConcurrentSkipListMap<String, byte[]> testMap = null;
Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
//act
byte[] parsedPayload = Deencapsulation.invoke(testTwin, "parsePayload", resTopic);
//assert
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin in project azure-iot-sdk-java by Azure.
the class MqttIotHubConnectionTest method openThrowsIOExceptionIfConnectionFailsInTwin.
@Test(expected = IOException.class)
public void openThrowsIOExceptionIfConnectionFailsInTwin() throws IOException {
baseExpectations();
new NonStrictExpectations() {
{
new IotHubSasToken(mockConfig, anyLong);
result = mockToken;
new MqttMessaging(sslPrefix + iotHubHostName + sslPortSuffix, deviceId, anyString, anyString, mockIotHubSSLContext);
result = mockDeviceMessaging;
new MqttDeviceMethod();
result = mockDeviceMethods;
new MqttDeviceTwin();
result = new IOException(anyString);
}
};
try {
MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
connection.open();
} catch (Exception e) {
new Verifications() {
{
mockDeviceMessaging.stop();
times = 1;
mockDeviceMethods.stop();
times = 1;
}
};
throw e;
}
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveSetVersionForDesiredPropNotifRespIfFound.
/*
**Tests_SRS_MQTTDEVICETWIN_25_042: [**If the topic is of type patch for desired properties then this method shall parse further to look for version which if found is set by calling setVersion**]**
*/
@Test
public void receiveSetVersionForDesiredPropNotifRespIfFound(@Mocked final Mqtt mockMqtt) throws IOException {
final byte[] actualPayload = "NotificationResponseDataContainingDesiredPropertiesDocument".getBytes();
final String expectedTopic = "$iothub/twin/PATCH/properties/desired/" + "?$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
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().equals(mockVersion));
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 sendPublishesMessageForGetTwinOnCorrectTopic.
/*
**Tests_SRS_MQTTDEVICETWIN_25_024: [**send method shall build the get request topic of the format mentioned in spec ($iothub/twin/GET/?$rid={request id}) if the operation is of type DEVICE_OPERATION_TWIN_GET_REQUEST.**]**
*/
@Test
public void sendPublishesMessageForGetTwinOnCorrectTopic(@Mocked final Mqtt mockMqtt, @Mocked final DeviceTwinMessage mockMessage) throws IOException {
//arrange
final byte[] actualPayload = { 0x61, 0x62, 0x63 };
final String expectedTopic = "$iothub/twin/GET/?$rid=" + mockReqId;
MqttDeviceTwin testTwin = new MqttDeviceTwin();
testTwin.start();
new NonStrictExpectations() {
{
mockMessage.getBytes();
result = actualPayload;
mockMessage.getMessageType();
result = MessageType.DeviceTwin;
mockMessage.getDeviceOperationType();
result = DEVICE_OPERATION_TWIN_GET_REQUEST;
mockMessage.getRequestId();
result = mockReqId;
}
};
//act
testTwin.send(mockMessage);
//assert
new Verifications() {
{
mockMessage.getBytes();
times = 2;
Deencapsulation.invoke(mockMqtt, "publish", expectedTopic, actualPayload);
times = 1;
}
};
}
Aggregations