Search in sources :

Example 11 with IotHubTransportMessage

use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.

the class FileUploadTaskTest method runCreateRequestMessageThrows.

/* Tests_SRS_FILEUPLOADTASK_21_031: [If run failed to send the request, it shall call the userCallback with the status `ERROR`, and abort the upload.] */
@Test
public void runCreateRequestMessageThrows() throws IOException, IllegalArgumentException, URISyntaxException {
    // arrange
    new NonStrictExpectations() {

        {
            new FileUploadSasUriRequest(VALID_BLOB_NAME);
            result = mockFileUploadSasUriRequest;
            mockFileUploadSasUriRequest.toJson();
            result = VALID_REQUEST_JSON;
            new IotHubTransportMessage(VALID_REQUEST_JSON);
            result = new IllegalArgumentException();
        }
    };
    FileUploadTask fileUploadTask = Deencapsulation.newInstance(FileUploadTask.class, new Class[] { String.class, InputStream.class, long.class, HttpsTransportManager.class, IotHubEventCallback.class, Object.class }, VALID_BLOB_NAME, mockInputStream, VALID_STREAM_LENGTH, mockHttpsTransportManager, mockIotHubEventCallback, VALID_CALLBACK_CONTEXT);
    // act
    Deencapsulation.invoke(fileUploadTask, "run");
    // assert
    new Verifications() {

        {
            mockIotHubEventCallback.execute(IotHubStatusCode.ERROR, VALID_CALLBACK_CONTEXT);
            times = 1;
        }
    };
}
Also used : FileUploadTask(com.microsoft.azure.sdk.iot.device.fileupload.FileUploadTask) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) FileUploadSasUriRequest(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest) Test(org.junit.Test)

Example 12 with IotHubTransportMessage

use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.

the class MqttTest method deliveryCompleteDoesNotNotifyListenerIfSubscribeToDesiredProperties.

// Tests_SRS_Mqtt_34_056: [If the acknowledged message is of type
// DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST, DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST,
// or DEVICE_OPERATION_TWIN_UNSUBSCRIBE_DESIRED_PROPERTIES_REQUEST, this function shall not notify the saved
// listener that the message was sent.]
@Test
public void deliveryCompleteDoesNotNotifyListenerIfSubscribeToDesiredProperties() throws TransportException {
    // arrange
    final int expectedMessageId = 13;
    final Message otherMessage = new Message();
    final IotHubTransportMessage expectedMessage = new IotHubTransportMessage("some body");
    expectedMessage.setDeviceOperationType(DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST);
    Mqtt mockMqtt = instantiateMqtt(true, mockedIotHubListener);
    Map<Integer, Message> unacknowledgedMessages = new HashMap<>();
    unacknowledgedMessages.put(12, otherMessage);
    unacknowledgedMessages.put(expectedMessageId, expectedMessage);
    Deencapsulation.setField(mockMqtt, "unacknowledgedSentMessages", unacknowledgedMessages);
    new NonStrictExpectations() {

        {
            mockMqttDeliveryToken.getMessageId();
            result = expectedMessageId;
        }
    };
    // act
    mockMqtt.deliveryComplete(mockMqttDeliveryToken);
    // assert
    new Verifications() {

        {
            mockedIotHubListener.onMessageSent(expectedMessage, null, null);
            times = 0;
        }
    };
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) HashMap(java.util.HashMap) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) Test(org.junit.Test)

Example 13 with IotHubTransportMessage

use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.

the class MqttTest method deliveryCompleteDoesNotNotifyListenerIfUnsubscribeToDesiredProperties.

// Tests_SRS_Mqtt_34_056: [If the acknowledged message is of type
// DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST, DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST,
// or DEVICE_OPERATION_TWIN_UNSUBSCRIBE_DESIRED_PROPERTIES_REQUEST, this function shall not notify the saved
// listener that the message was sent.]
@Test
public void deliveryCompleteDoesNotNotifyListenerIfUnsubscribeToDesiredProperties() throws TransportException {
    // arrange
    final int expectedMessageId = 13;
    final Message otherMessage = new Message();
    final IotHubTransportMessage expectedMessage = new IotHubTransportMessage("some body");
    expectedMessage.setDeviceOperationType(DEVICE_OPERATION_TWIN_UNSUBSCRIBE_DESIRED_PROPERTIES_REQUEST);
    Mqtt mockMqtt = instantiateMqtt(true, mockedIotHubListener);
    Map<Integer, Message> unacknowledgedMessages = new HashMap<>();
    unacknowledgedMessages.put(12, otherMessage);
    unacknowledgedMessages.put(expectedMessageId, expectedMessage);
    Deencapsulation.setField(mockMqtt, "unacknowledgedSentMessages", unacknowledgedMessages);
    new NonStrictExpectations() {

        {
            mockMqttDeliveryToken.getMessageId();
            result = expectedMessageId;
        }
    };
    // act
    mockMqtt.deliveryComplete(mockMqttDeliveryToken);
    // assert
    new Verifications() {

        {
            mockedIotHubListener.onMessageSent(expectedMessage, null, null);
            times = 0;
        }
    };
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) HashMap(java.util.HashMap) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) Test(org.junit.Test)

Example 14 with IotHubTransportMessage

use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.

the class MqttTest method deliveryCompleteDoesNotNotifyListenerIfSubscribeToMethods.

// Tests_SRS_Mqtt_34_056: [If the acknowledged message is of type
// DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST, DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST,
// or DEVICE_OPERATION_TWIN_UNSUBSCRIBE_DESIRED_PROPERTIES_REQUEST, this function shall not notify the saved
// listener that the message was sent.]
@Test
public void deliveryCompleteDoesNotNotifyListenerIfSubscribeToMethods() throws TransportException {
    // arrange
    final int expectedMessageId = 13;
    final Message otherMessage = new Message();
    final IotHubTransportMessage expectedMessage = new IotHubTransportMessage("some body");
    expectedMessage.setDeviceOperationType(DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST);
    Mqtt mockMqtt = instantiateMqtt(true, mockedIotHubListener);
    Map<Integer, Message> unacknowledgedMessages = new HashMap<>();
    unacknowledgedMessages.put(12, otherMessage);
    unacknowledgedMessages.put(expectedMessageId, expectedMessage);
    Deencapsulation.setField(mockMqtt, "unacknowledgedSentMessages", unacknowledgedMessages);
    new NonStrictExpectations() {

        {
            mockMqttDeliveryToken.getMessageId();
            result = expectedMessageId;
        }
    };
    // act
    mockMqtt.deliveryComplete(mockMqttDeliveryToken);
    // assert
    new Verifications() {

        {
            mockedIotHubListener.onMessageSent(expectedMessage, null, null);
            times = 0;
        }
    };
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) HashMap(java.util.HashMap) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) Test(org.junit.Test)

Example 15 with IotHubTransportMessage

use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.

the class DeviceTwin method updateReportedProperties.

public synchronized void updateReportedProperties(Set<Property> reportedProperties, Integer version, CorrelatingMessageCallback correlatingMessageCallback, Object correlatingMessageCallbackContext, IotHubEventCallback reportedPropertiesCallback, Object callbackContext) throws IOException {
    if (reportedProperties == null) {
        throw new IllegalArgumentException("Reported properties cannot be null");
    }
    TwinCollection reportedPropertiesMap = new TwinCollection();
    for (Property p : reportedProperties) {
        if (reportedPropertiesMap.containsKey(p.getKey())) {
            throw new IOException("Duplicate keys found in reported properties: " + p.getKey());
        }
        reportedPropertiesMap.putFinal(p.getKey(), p.getValue());
    }
    String serializedReportedProperties = reportedPropertiesMap.toJsonElement().toString();
    if (serializedReportedProperties == null) {
        return;
    }
    IotHubTransportMessage updateReportedPropertiesRequest = new IotHubTransportMessage(serializedReportedProperties.getBytes(StandardCharsets.UTF_8), MessageType.DEVICE_TWIN);
    updateReportedPropertiesRequest.setCorrelatingMessageCallback(correlatingMessageCallback);
    updateReportedPropertiesRequest.setCorrelatingMessageCallbackContext(correlatingMessageCallbackContext);
    updateReportedPropertiesRequest.setConnectionDeviceId(this.config.getDeviceId());
    // MQTT does not have the concept of correlationId for request/response handling but it does have a requestId
    // To handle this we are setting the correlationId to the requestId to better handle correlation
    // whether we use MQTT or AMQP.
    updateReportedPropertiesRequest.setRequestId(UUID.randomUUID().toString());
    updateReportedPropertiesRequest.setCorrelationId(updateReportedPropertiesRequest.getRequestId());
    if (version != null) {
        updateReportedPropertiesRequest.setVersion(Integer.toString(version));
    }
    updateReportedPropertiesRequest.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST);
    this.deviceIO.sendEventAsync(updateReportedPropertiesRequest, reportedPropertiesCallback, callbackContext, this.config.getDeviceId());
}
Also used : TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) IOException(java.io.IOException) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)

Aggregations

IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)129 Test (org.junit.Test)108 Message (com.microsoft.azure.sdk.iot.device.Message)37 MutablePair (org.apache.commons.lang3.tuple.MutablePair)33 Pair (org.apache.commons.lang3.tuple.Pair)33 DeviceTwin (com.microsoft.azure.sdk.iot.device.DeviceTwin)30 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)25 HashMap (java.util.HashMap)24 DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)16 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)16 MessageType (com.microsoft.azure.sdk.iot.device.MessageType)14 Verifications (mockit.Verifications)10 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)8 NonStrictExpectations (mockit.NonStrictExpectations)8 IOException (java.io.IOException)5 FileUploadTask (com.microsoft.azure.sdk.iot.device.fileupload.FileUploadTask)4 MessageCallback (com.microsoft.azure.sdk.iot.device.MessageCallback)3 MethodResult (com.microsoft.azure.sdk.iot.device.edge.MethodResult)3 HttpsTransportManager (com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager)3 HashSet (java.util.HashSet)3