use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwin method receive.
@Override
public Message receive() throws IOException {
DeviceTwinMessage messsage;
/*
**Codes_SRS_MQTTDEVICETWIN_25_033: [**This method shall call parseTopic to parse the topic from the recevived Messages queue corresponding to the messaging client's operation.**]**
*/
String topic = parseTopic();
if (topic != null && topic.length() > 0) {
/*
**Codes_SRS_MQTTDEVICETWIN_25_035: [**This method shall call parsePayload to get the message payload from the recevived Messages queue corresponding to the messaging client's operation.**]**
*/
byte[] data = parsePayload(topic);
if (topic.length() > RES.length() && topic.startsWith(RES)) {
// Tokenize on backslash
String[] topicTokens = topic.split(Pattern.quote("/"));
if (data != null && data.length > 0) {
/*
**Codes_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**]**
*/
messsage = new DeviceTwinMessage(data);
messsage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_UNKNOWN);
} else {
// Case for $iothub/twin/res/{status}/?$rid={request id}
/*
**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**]**
*/
// empty body
messsage = new DeviceTwinMessage(new byte[0]);
messsage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_UNKNOWN);
}
if (topicTokens.length > STATUS_TOKEN) {
/*
**Codes_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**]**
*/
messsage.setStatus(getStatus(topicTokens[STATUS_TOKEN]));
} else {
throw new IOException("Message received without status");
}
if (topicTokens.length > REQID_TOKEN) {
/*
**Codes_SRS_MQTTDEVICETWIN_25_040: [**If the topic is of type response topic then this method shall parse further to look for request id which if found is set by calling setRequestId**]**
*/
String requestId = getRequestId(topicTokens[REQID_TOKEN]);
messsage.setRequestId(requestId);
if (requestMap.containsKey(requestId)) {
switch(requestMap.remove(requestId)) {
case DEVICE_OPERATION_TWIN_GET_REQUEST:
messsage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_GET_RESPONSE);
break;
case DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST:
messsage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_RESPONSE);
break;
default:
messsage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_UNKNOWN);
}
} else {
throw new UnsupportedOperationException();
}
}
if (topicTokens.length > VERSION_TOKEN) {
/*
**Codes_SRS_MQTTDEVICETWIN_25_041: [**If the topic is of type response topic then this method shall parse further to look for version which if found is set by calling setVersion**]**
*/
messsage.setVersion(getVersion(topicTokens[VERSION_TOKEN]));
}
} else if (topic.length() > PATCH.length() && topic.startsWith(PATCH)) {
if (topic.startsWith(PATCH + BACKSLASH + PROPERTIES + BACKSLASH + DESIRED)) {
if (data != null) {
/*
**Codes_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**]**
*/
messsage = new DeviceTwinMessage(data);
messsage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE);
} else {
/*
**Tests_SRS_MQTTDEVICETWIN_25_047: [**If the topic is of type patch for desired properties then this method shall throw unsupportedoperation exception if data is null or empty**]**
*/
throw new UnsupportedOperationException();
}
// Case for $iothub/twin/PATCH/properties/desired/?$version={new version}
// Tokenize on backslash
String[] topicTokens = topic.split(Pattern.quote("/"));
if (topicTokens.length > PATCH_VERSION_TOKEN) {
/*
**Codes_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**]**
*/
messsage.setVersion(getVersion(topicTokens[PATCH_VERSION_TOKEN]));
}
} else {
/*
**Codes_SRS_MQTTDEVICETWIN_25_043: [**If the topic is not of type response for desired properties then this method shall throw unsupportedoperation exception**]**
*/
throw new UnsupportedOperationException();
}
} else {
/*
**Codes_SRS_MQTTDEVICETWIN_25_037: [**This method shall parse topic to look for only either twin response topic or twin patch topic and thorw unsupportedoperation exception other wise.**]**
*/
throw new UnsupportedOperationException();
}
logger.LogInfo("Message received on DT " + messsage.getDeviceOperationType());
return messsage;
} else {
return null;
}
}
use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage 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.DeviceTwin.DeviceTwinMessage 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.DeviceTwin.DeviceTwinMessage in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveSetsVersionOnResTopic.
/*
**Tests_SRS_MQTTDEVICETWIN_25_041: [**If the topic is of type response topic then this method shall parse further to look for version which if found is set by calling setVersion**]**
*/
@Test
public void receiveSetsVersionOnResTopic(@Mocked final Mqtt mockMqtt) throws IOException {
final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes();
final String expectedTopic = "$iothub/twin/res/" + "201" + "/?$rid=" + mockReqId + "&$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);
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("201"));
assertTrue(receivedMessage.getVersion().equals(mockVersion));
}
}
use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveParsesResponseTopicForUpdateReportedPropertiesSucceeds.
@Test
public void receiveParsesResponseTopicForUpdateReportedPropertiesSucceeds(@Mocked final Mqtt mockMqtt) throws IOException {
final byte[] actualPayload = "".getBytes();
/*
The following does not work
final byte[] actualPayload = null;
*/
final String expectedTopic = "$iothub/twin/res/" + "200" + "/?$rid=" + mockReqId + "&$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);
Map<String, DeviceOperations> requestMap = new HashMap<>();
requestMap.put(mockReqId, DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_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_UPDATE_REPORTED_PROPERTIES_RESPONSE);
assertTrue(receivedMessage.getStatus().equals("200"));
assertTrue(receivedMessage.getRequestId().equals(mockReqId));
assertTrue(receivedMessage.getVersion().equals(mockVersion));
}
}
Aggregations