Search in sources :

Example 16 with IotHubCallbackPacket

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

the class AmqpsTransportTest method messageSentReturnsIfThereAreNoMessagesInProgress.

// Tests_SRS_AMQPSTRANSPORT_15_029: [If the hash cannot be found in the list of keys for the messages in progress, the method returns.]
@Test
public void messageSentReturnsIfThereAreNoMessagesInProgress() throws IOException {
    new NonStrictExpectations() {

        {
            new AmqpsIotHubConnection(mockConfig, false);
            result = mockConnection;
        }
    };
    AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
    transport.open();
    Map<Integer, IotHubOutboundPacket> inProgressMessages = new ConcurrentHashMap<>();
    Deencapsulation.setField(transport, "inProgressMessages", inProgressMessages);
    transport.messageSent(1, true);
    new Verifications() {

        {
            new IotHubCallbackPacket(IotHubStatusCode.OK_EMPTY, (IotHubEventCallback) any, any);
            times = 0;
        }
    };
}
Also used : AmqpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection) AmqpsTransport(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport) IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 17 with IotHubCallbackPacket

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

the class MqttTransportTest method sendMessagesAddsToCallbackQueue.

// Tests_SRS_MQTTTRANSPORT_15_010: [For each message being sent, the function shall send the message
// and add the IoT Hub status code along with the callback and context to the callback list.]
@Test
public <T extends Queue> void sendMessagesAddsToCallbackQueue(@Mocked final Message mockMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final IotHubOutboundPacket mockPacket, @Mocked final IotHubCallbackPacket mockCallbackPacket) throws IOException {
    final Queue mockQueue = new MockUp<T>() {
    }.getMockInstance();
    final Map<String, Object> context = new HashMap<>();
    new NonStrictExpectations() {

        {
            new MqttIotHubConnection(mockConfig);
            result = mockConnection;
            new IotHubOutboundPacket(mockMsg, mockCallback, context);
            result = mockPacket;
            mockPacket.getCallback();
            result = mockCallback;
            mockPacket.getContext();
            result = context;
            mockConnection.sendEvent((Message) any);
            returns(IotHubStatusCode.OK_EMPTY, IotHubStatusCode.ERROR);
            new IotHubCallbackPacket(IotHubStatusCode.OK_EMPTY, mockCallback, context);
            result = mockCallbackPacket;
            new IotHubCallbackPacket(IotHubStatusCode.ERROR, mockCallback, context);
            result = mockCallbackPacket;
        }
    };
    MqttTransport transport = new MqttTransport(mockConfig);
    transport.open();
    transport.addMessage(mockMsg, mockCallback, context);
    transport.addMessage(mockMsg, mockCallback, context);
    transport.sendMessages();
    new VerificationsInOrder() {

        {
            new IotHubCallbackPacket(IotHubStatusCode.OK_EMPTY, mockCallback, context);
            mockQueue.add(mockCallbackPacket);
            new IotHubCallbackPacket(IotHubStatusCode.ERROR, mockCallback, context);
            mockQueue.add(mockCallbackPacket);
        }
    };
}
Also used : HashMap(java.util.HashMap) IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) MqttTransport(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) Queue(java.util.Queue) Test(org.junit.Test)

Example 18 with IotHubCallbackPacket

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

the class HttpsTransportTest method sendMessagesAddsToCompletedQueue.

// Tests_SRS_HTTPSTRANSPORT_11_006: [The function shall add a packet containing the callbacks, contexts, and response for all sent messages to the callback queue.]
@Test
public <T extends Queue> void sendMessagesAddsToCompletedQueue(@Mocked final Message mockMsg, @Mocked final HttpsSingleMessage mockHttpsMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final HttpsBatchMessage mockBatch, @Mocked final ResponseMessage mockResponseMessage, @Mocked final IotHubCallbackPacket mockCallbackPacket) throws URISyntaxException, IOException {
    final Queue mockQueue = new MockUp<T>() {
    }.getMockInstance();
    final IotHubStatusCode iotHubStatus = IotHubStatusCode.OK;
    new NonStrictExpectations() {

        {
            HttpsSingleMessage.parseHttpsMessage(mockMsg);
            result = mockHttpsMsg;
            new HttpsBatchMessage();
            result = mockBatch;
            mockConn.sendEvent((HttpsMessage) any);
            result = mockResponseMessage;
            mockResponseMessage.getStatus();
            result = iotHubStatus;
        }
    };
    final Map<String, Object> context = new HashMap<>();
    HttpsTransport transport = new HttpsTransport(mockConfig);
    transport.open();
    transport.addMessage(mockMsg, mockCallback, context);
    transport.addMessage(mockMsg, mockCallback, context);
    transport.sendMessages();
    final IotHubStatusCode expectedStatus = iotHubStatus;
    final Map<String, Object> expectedContext = context;
    new VerificationsInOrder() {

        {
            new IotHubCallbackPacket(expectedStatus, mockCallback, expectedContext);
            mockQueue.add(mockCallbackPacket);
            times = 2;
        }
    };
}
Also used : HashMap(java.util.HashMap) IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) Queue(java.util.Queue) Test(org.junit.Test)

Example 19 with IotHubCallbackPacket

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

the class HttpsTransport method addOutboundPacketToCallbackList.

private void addOutboundPacketToCallbackList(IotHubOutboundPacket packet, ResponseMessage responseMessage) {
    IotHubEventCallback eventCallback = packet.getCallback();
    if (eventCallback != null) {
        IotHubCallbackPacket callbackPacket = new IotHubCallbackPacket(responseMessage.getStatus(), eventCallback, packet.getContext());
        this.callbackList.add(callbackPacket);
    }
    IotHubResponseCallback responseCallback = packet.getResponseCallback();
    if (responseCallback != null) {
        IotHubCallbackPacket callbackPacket = new IotHubCallbackPacket(responseMessage, responseCallback, packet.getContext());
        this.callbackList.add(callbackPacket);
    }
}
Also used : IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket)

Example 20 with IotHubCallbackPacket

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

the class MqttTransport method close.

/**
     * Closes all resources used to communicate with an IoT Hub. Once {@code close()} is
     * called, the transport is no longer usable. If the transport is already
     * closed, the function shall do nothing.
     */
public void close() throws IOException {
    // Codes_SRS_MQTTTRANSPORT_15_006: [If the MQTT connection is closed, the function shall do nothing.]
    if (this.state == State.CLOSED) {
        return;
    }
    // Codes_SRS_MQTTTRANSPORT_99_020: [The method will remove all the messages which are in progress or waiting to be sent and add them to the callback list.]
    while (!this.waitingList.isEmpty()) {
        IotHubOutboundPacket packet = this.waitingList.remove();
        IotHubCallbackPacket callbackPacket = new IotHubCallbackPacket(IotHubStatusCode.MESSAGE_CANCELLED_ONCLOSE, packet.getCallback(), packet.getContext());
        this.callbackList.add(callbackPacket);
    }
    // Codes_SRS_MQTTTRANSPORT_99_021: [The method will invoke the callback list]
    invokeCallbacks();
    // Codes_SRS_MQTTTRANSPORT_15_005: [The function shall close the MQTT connection
    // with the IoT Hub given in the configuration.]
    this.mqttIotHubConnection.close();
    this.state = State.CLOSED;
}
Also used : IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)

Aggregations

IotHubCallbackPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket)23 Test (org.junit.Test)14 IotHubOutboundPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)10 HashMap (java.util.HashMap)9 AmqpsTransport (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport)6 AmqpsIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 IotHubStatusCode (com.microsoft.azure.sdk.iot.device.IotHubStatusCode)4 IotHubResponseCallback (com.microsoft.azure.sdk.iot.device.IotHubResponseCallback)2 ResponseMessage (com.microsoft.azure.sdk.iot.device.ResponseMessage)2 AmqpsMessage (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 Queue (java.util.Queue)2 IotHubEventCallback (com.microsoft.azure.sdk.iot.device.IotHubEventCallback)1 MqttIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection)1 MqttTransport (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport)1 MessageImpl (org.apache.qpid.proton.message.impl.MessageImpl)1