Search in sources :

Example 21 with DeviceOperations

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);
    }
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) DeviceTwinMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage) HashMap(java.util.HashMap) DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) Test(org.junit.Test)

Example 22 with DeviceOperations

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());
}
Also used : HashMap(java.util.HashMap) DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage) MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) Verifications(mockit.Verifications) Test(org.junit.Test)

Example 23 with DeviceOperations

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);
}
Also used : HashMap(java.util.HashMap) DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage) MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) Test(org.junit.Test)

Example 24 with DeviceOperations

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");
    }
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)

Example 25 with DeviceOperations

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;
}
Also used : DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MessageType(com.microsoft.azure.sdk.iot.device.MessageType)

Aggregations

DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)29 Test (org.junit.Test)26 HashMap (java.util.HashMap)18 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)16 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)16 DeviceTwinMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage)11 Message (com.microsoft.azure.sdk.iot.device.Message)9 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)8 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)8 MutablePair (org.apache.commons.lang3.tuple.MutablePair)8 Pair (org.apache.commons.lang3.tuple.Pair)8 MessageType (com.microsoft.azure.sdk.iot.device.MessageType)3 DeviceMethodMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage)2 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)2 MessageCallback (com.microsoft.azure.sdk.iot.device.MessageCallback)1 Map (java.util.Map)1 Verifications (mockit.Verifications)1 Symbol (org.apache.qpid.proton.amqp.Symbol)1 MessageAnnotations (org.apache.qpid.proton.amqp.messaging.MessageAnnotations)1 Properties (org.apache.qpid.proton.amqp.messaging.Properties)1