use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage 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 IotHubTransportMessage mockMessage) throws TransportException {
// arrange
final byte[] actualPayload = { 0x61, 0x62, 0x63 };
final String expectedTopic = "$iothub/twin/GET/?$rid=" + mockReqId;
MqttDeviceTwin testTwin = new MqttDeviceTwin("", mockedConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
testTwin.start();
new NonStrictExpectations() {
{
mockMessage.getBytes();
result = actualPayload;
mockMessage.getMessageType();
result = MessageType.DEVICE_TWIN;
mockMessage.getDeviceOperationType();
result = DEVICE_OPERATION_TWIN_GET_REQUEST;
mockMessage.getRequestId();
result = mockReqId;
}
};
// act
testTwin.send(mockMessage);
// assert
new Verifications() {
{
mockMessage.getBytes();
times = 1;
Deencapsulation.invoke(mockMqtt, "publish", expectedTopic, mockMessage);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveParsesResponseTopicForUpdateReportedPropertiesSucceeds.
@Test
public void receiveParsesResponseTopicForUpdateReportedPropertiesSucceeds() throws TransportException {
final byte[] actualPayload = "".getBytes(StandardCharsets.UTF_8);
/*
The following does not work
final byte[] actualPayload = null;
*/
final String expectedTopic = "$iothub/twin/res/" + "200" + "/?$rid=" + mockReqId + "&$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);
Map<String, DeviceOperations> requestMap = new HashMap<>();
requestMap.put(mockReqId, DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_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_UPDATE_REPORTED_PROPERTIES_RESPONSE);
assertEquals("200", receivedMessage.getStatus());
assertEquals(receivedMessage.getRequestId(), mockReqId);
assertEquals(receivedMessage.getVersion(), mockVersion);
}
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveDoesNotSetDataForUpdateReportedPropResp.
/*
** Tests_SRS_MQTTDEVICETWIN_25_045: [If the topic is of type response then this method shall set empty data and operation type as DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_RESPONSE if data is null or empty]
*/
@Test
public void receiveDoesNotSetDataForUpdateReportedPropResp() throws TransportException {
final byte[] actualPayload = "".getBytes(StandardCharsets.UTF_8);
/*
The following does not work
final byte[] actualPayload = null;
*/
final String expectedTopic = "$iothub/twin/res/" + "200" + "/?$rid=" + mockReqId + "&$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);
Map<String, DeviceOperations> requestMap = new HashMap<>();
requestMap.put(mockReqId, DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST);
Deencapsulation.setField(testTwin, "requestMap", requestMap);
Deencapsulation.setField(testTwin, "stateLock", new Object());
// act
receivedMessage = testTwin.receive();
} finally {
// assert
assertNotNull(receivedMessage);
assertSame(receivedMessage.getMessageType(), MessageType.DEVICE_TWIN);
assertSame(receivedMessage.getDeviceOperationType(), DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_RESPONSE);
assertEquals(receivedMessage.getRequestId(), mockReqId);
assertEquals("200", receivedMessage.getStatus());
assertEquals(receivedMessage.getVersion(), mockVersion);
byte[] receivedMessageBytes = receivedMessage.getBytes();
assertEquals(0, receivedMessageBytes.length);
}
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage 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() 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);
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());
}
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage 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 TransportException ]
*/
@Test(expected = TransportException.class)
public void receiveParsesResponseTopicMandatoryStatusNotFoundException() throws TransportException {
final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes(StandardCharsets.UTF_8);
final String expectedTopic = "$iothub/twin/res/" + "?$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);
// act
receivedMessage = (IotHubTransportMessage) testTwin.receive();
} finally {
// assert
assertNull(receivedMessage);
}
}
Aggregations