use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations 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.DeviceOperations in project azure-iot-sdk-java by Azure.
the class MqttDeviceMethodTest method sendSucceedsCallsPublish.
/*
Tests_SRS_MqttDeviceMethod_25_022: [**send method shall build the publish topic of the format mentioned in spec ($iothub/methods/res/{status}/?$rid={request id}) and publish if the operation is of type DEVICE_OPERATION_METHOD_SEND_RESPONSE.**]**
*/
@Test
public void sendSucceedsCallsPublish() throws IOException {
final byte[] actualPayload = "TestMessage".getBytes();
final DeviceMethodMessage testMessage = new DeviceMethodMessage(actualPayload);
testMessage.setDeviceOperationType(DEVICE_OPERATION_METHOD_SEND_RESPONSE);
testMessage.setRequestId("ReqId");
testMessage.setStatus("testStatus");
MqttDeviceMethod testMethod = new MqttDeviceMethod();
Map<String, DeviceOperations> testRequestMap = new HashMap<>();
testRequestMap.put("ReqId", DEVICE_OPERATION_METHOD_RECEIVE_REQUEST);
Deencapsulation.setField(testMethod, "requestMap", testRequestMap);
testMethod.start();
//act
testMethod.send(testMessage);
//assert
new Verifications() {
{
Deencapsulation.invoke(mockedMqtt, "publish", anyString, actualPayload);
maxTimes = 1;
}
};
assertTrue(testRequestMap.isEmpty());
}
use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations in project azure-iot-sdk-java by Azure.
the class MqttDeviceMethodTest method sendThrowsOnMismatchedRequestType.
/*
Tests_SRS_MqttDeviceMethod_25_019: [**send method shall throw an IoException if the getDeviceOperationType() is not of type DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST or DEVICE_OPERATION_METHOD_SEND_RESPONSE .**]**
*/
@Test(expected = IOException.class)
public void sendThrowsOnMismatchedRequestType() throws IOException {
final byte[] actualPayload = "TestMessage".getBytes();
final DeviceMethodMessage testMessage = new DeviceMethodMessage(actualPayload);
testMessage.setDeviceOperationType(DEVICE_OPERATION_METHOD_SEND_RESPONSE);
testMessage.setRequestId("ReqId");
testMessage.setStatus("testStatus");
MqttDeviceMethod testMethod = new MqttDeviceMethod();
Map<String, DeviceOperations> testRequestMap = new HashMap<>();
testRequestMap.put("ReqId", DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST);
Deencapsulation.setField(testMethod, "requestMap", testRequestMap);
testMethod.start();
//act
testMethod.send(testMessage);
}
use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations in project azure-iot-sdk-java by Azure.
the class Mqtt method deliveryComplete.
/**
* Event fired when the message arrived on the MQTT broker.
* @param iMqttDeliveryToken the MqttDeliveryToken for which the message was successfully sent.
*/
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
Message deliveredMessage = null;
log.trace("Mqtt message with message id {} was acknowledge by service", iMqttDeliveryToken.getMessageId());
synchronized (this.unacknowledgedSentMessagesLock) {
if (unacknowledgedSentMessages.containsKey(iMqttDeliveryToken.getMessageId())) {
log.trace("Mqtt message with message id {} that was acknowledge by service was sent by this client", iMqttDeliveryToken.getMessageId());
deliveredMessage = unacknowledgedSentMessages.remove(iMqttDeliveryToken.getMessageId());
} else {
log.warn("Mqtt message with message id {} that was acknowledge by service was not sent by this client, will be ignored", iMqttDeliveryToken.getMessageId());
}
}
if (deliveredMessage instanceof IotHubTransportMessage) {
DeviceOperations deviceOperation = ((IotHubTransportMessage) deliveredMessage).getDeviceOperationType();
if (deviceOperation == DeviceOperations.DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST || deviceOperation == DeviceOperations.DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST || deviceOperation == DeviceOperations.DEVICE_OPERATION_TWIN_UNSUBSCRIBE_DESIRED_PROPERTIES_REQUEST) {
// no need to alert the IotHubTransport layer about these messages as they are not tracked in the inProgressQueue
return;
}
}
if (this.listener != null) {
this.listener.onMessageSent(deliveredMessage, this.deviceId, null);
} else {
log.warn("Message sent, but no listener set");
}
}
use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations in project azure-iot-sdk-java by Azure.
the class AmqpsSessionHandler method sendMessage.
SendResult sendMessage(Message message) {
if (!this.deviceClientConfig.getDeviceId().equals(message.getConnectionDeviceId())) {
// This should never happen since this session handler was chosen from a map of device Id -> session handler
// so it should have the same device Id as in the map it was grabbed from.
log.warn("Failed to send the message because this session belongs to a different device");
return SendResult.WRONG_DEVICE;
}
MessageType messageType = message.getMessageType();
if (messageType == null) {
// Twin and method messages have a message type assigned to them when they are constructed by this SDK
// (users can't construct twin/method messages directly), but telemetry messages don't necessarily have this
// type assigned since users may create telemetry messages. By default, assume any messages with an
// unassigned type are telemetry messages.
messageType = DEVICE_TELEMETRY;
}
// Check if the message being sent is a subscription change message. If so, open the corresponding links.
if (message instanceof IotHubTransportMessage) {
IotHubTransportMessage transportMessage = (IotHubTransportMessage) message;
DeviceOperations subscriptionType = transportMessage.getDeviceOperationType();
if (subscriptionType == DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST) {
return handleMethodSubscriptionRequest(transportMessage);
}
if (subscriptionType == DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST) {
return handleTwinSubscriptionRequest(transportMessage);
}
}
AmqpsSenderLinkHandler senderLinkHandler = this.senderLinkHandlers.get(messageType);
if (senderLinkHandler == null) {
// before their respective subscription messages have already opened their links.
return SendResult.LINKS_NOT_OPEN;
}
if (messageType == DEVICE_TWIN) {
if (explicitInProgressTwinSubscriptionMessage != null) {
// has been acknowledged by the service before sending it.
return SendResult.SUBSCRIPTION_IN_PROGRESS;
}
for (SubscriptionType subscriptionType : this.implicitInProgressSubscriptionMessages.values()) {
if (subscriptionType == SubscriptionType.DESIRED_PROPERTIES_SUBSCRIPTION) {
// executes again.
return SendResult.SUBSCRIPTION_IN_PROGRESS;
}
}
}
AmqpsSendResult amqpsSendResult = senderLinkHandler.sendMessageAndGetDeliveryTag(message);
if (amqpsSendResult.isDeliverySuccessful()) {
return SendResult.SUCCESS;
}
return SendResult.UNKNOWN_FAILURE;
}
Aggregations