use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveThrowsUnsupportedExceptionOnAnythingOtherThenPatchOrResTopic.
/*
**Tests_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.**]**
*/
@Test(expected = UnsupportedOperationException.class)
public void receiveThrowsUnsupportedExceptionOnAnythingOtherThenPatchOrResTopic(@Mocked final Mqtt mockMqtt) throws IOException {
final byte[] actualPayload = "NotificationResponseDataContainingDesiredPropertiesDocument".getBytes();
final String expectedTopic = "$iothub/twin/NOTPATCH_NOTRES/properties/" + "?$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
assertNull(receivedMessage);
}
}
use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage 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(@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);
}
}
use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveReturnsNullMessageIfTopicNotFound.
/*
**Tests_SRS_MQTTDEVICETWIN_25_034: [**If the call parseTopic returns null or empty string then this method shall do nothing and return null**]**
*/
@Test
public void receiveReturnsNullMessageIfTopicNotFound(@Mocked final Mqtt mockMqtt) throws IOException {
final byte[] actualPayload = "NotificationResponseDataContainingDesiredPropertiesDocument".getBytes();
final String expectedTopic = "$iothub/NOTtwin/NOTPATCH_NOTRES/properties/" + "?$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
assertNull(receivedMessage);
}
}
use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage in project azure-iot-sdk-java by Azure.
the class MqttIotHubConnection method sendEvent.
/**
* Sends an event message.
*
* @param message the event message.
*
* @return the status code from sending the event message.
*
* @throws IllegalStateException if the MqttIotHubConnection is not open
*/
public IotHubStatusCode sendEvent(Message message) throws IllegalStateException {
synchronized (MQTT_CONNECTION_LOCK) {
// the function shall return status code BAD_FORMAT.]
if (message == null || message.getBytes() == null || ((message.getMessageType() != MessageType.DeviceTwin && message.getMessageType() != MessageType.DeviceMethods) && message.getBytes().length == 0)) {
return IotHubStatusCode.BAD_FORMAT;
}
// the function shall throw an IllegalStateException.]
if (this.state == State.CLOSED) {
throw new IllegalStateException("Cannot send event using a closed MQTT connection");
}
// Codes_SRS_MQTTIOTHUBCONNECTION_15_008: [The function shall send an event message
// to the IoT Hub given in the configuration.]
// Codes_SRS_MQTTIOTHUBCONNECTION_15_011: [If the message was successfully received by the service,
// the function shall return status code OK_EMPTY.]
IotHubStatusCode result = IotHubStatusCode.OK_EMPTY;
try {
// Codes_SRS_MQTTIOTHUBCONNECTION_15_009: [The function shall send the message payload.]
if (message.getMessageType() == MessageType.DeviceMethods) {
this.deviceMethod.start();
this.deviceMethod.send((DeviceMethodMessage) message);
} else if (message.getMessageType() == MessageType.DeviceTwin) {
this.deviceTwin.start();
this.deviceTwin.send((DeviceTwinMessage) message);
} else {
this.deviceMessaging.send(message);
}
}// received by the service, the function shall return status code ERROR.]
catch (Exception e) {
result = IotHubStatusCode.ERROR;
}
return result;
}
}
use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage in project azure-iot-sdk-java by Azure.
the class MqttIotHubConnectionTest method receiveDeviceTwinMessageSucceeds.
@Test
public void receiveDeviceTwinMessageSucceeds() throws IOException {
baseExpectations();
openExpectations();
final byte[] expectedMessageBody = { 0x61, 0x62, 0x63 };
new NonStrictExpectations() {
{
mockDeviceMethods.receive();
result = null;
mockDeviceTwin.receive();
result = new DeviceTwinMessage(expectedMessageBody);
}
};
MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
connection.open();
Message message = connection.receiveMessage();
byte[] actualMessageBody = message.getBytes();
assertNotNull(message);
for (int i = 0; i < expectedMessageBody.length; i++) {
assertEquals(expectedMessageBody[i], actualMessageBody[i]);
}
new Verifications() {
{
mockDeviceMethods.receive();
times = 1;
mockDeviceMessaging.receive();
times = 0;
}
};
}
Aggregations