Search in sources :

Example 11 with IotHubOutboundPacket

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

the class HttpsTransport method moveInProgressListToCallbackList.

/**
     * Moves messages from the in progress list to the callback list, updating
     * the message packets with the status code from the response.
     *
     * @param responseMessage the result from the IoT Hub that includes the status and message.
     */
private void moveInProgressListToCallbackList(ResponseMessage responseMessage) {
    while (!this.inProgressList.isEmpty()) {
        IotHubOutboundPacket packet = this.inProgressList.remove();
        addOutboundPacketToCallbackList(packet, responseMessage);
    }
}
Also used : IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)

Example 12 with IotHubOutboundPacket

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

the class AmqpsTransportTest method sendMessagesAddsExpiredMessagesToCallbackListWithCorrectCode.

// Tests_SRS_AMQPSTRANSPORT_15_039: [If the message is expired, the function shall create a callback
// with the MESSAGE_EXPIRED status and add it to the callback list.]
@Test
public void sendMessagesAddsExpiredMessagesToCallbackListWithCorrectCode(@Mocked final Message mockMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final IotHubOutboundPacket mockPacket) throws IOException {
    final Map<String, Object> context = new HashMap<>();
    final byte[] messageBytes = new byte[] { 1, 2 };
    new NonStrictExpectations() {

        {
            new AmqpsIotHubConnection(mockConfig, false);
            result = mockConnection;
            new IotHubOutboundPacket(mockMsg, mockCallback, context);
            result = mockPacket;
            mockPacket.getMessage();
            result = mockMsg;
            mockMsg.getBytes();
            result = messageBytes;
            mockMsg.isExpired();
            returns(true, false);
            mockConnection.sendMessage((org.apache.qpid.proton.message.Message) any);
            result = 1;
        }
    };
    AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
    transport.open();
    transport.addMessage(mockMsg, mockCallback, context);
    transport.addMessage(mockMsg, mockCallback, context);
    transport.sendMessages();
    Map<Integer, IotHubOutboundPacket> inProgressMessages = Deencapsulation.getField(transport, "inProgressMessages");
    Assert.assertEquals(1, inProgressMessages.size());
    Queue<IotHubOutboundPacket> waitingMessages = Deencapsulation.getField(transport, "waitingMessages");
    Assert.assertEquals(0, waitingMessages.size());
    Queue<IotHubCallbackPacket> callbackList = Deencapsulation.getField(transport, "callbackList");
    Assert.assertEquals(1, callbackList.size());
    new Verifications() {

        {
            new IotHubOutboundPacket(mockMsg, mockCallback, context);
            times = 2;
            mockPacket.getMessage();
            times = 2;
            mockConnection.sendMessage((org.apache.qpid.proton.message.Message) any);
            times = 1;
            new IotHubCallbackPacket(IotHubStatusCode.MESSAGE_EXPIRED, (IotHubEventCallback) any, any);
            times = 1;
        }
    };
}
Also used : AmqpsTransport(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) AmqpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) Test(org.junit.Test)

Example 13 with IotHubOutboundPacket

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

the class AmqpsTransportTest method sendMessagesSendsAllMessages.

// Tests_SRS_AMQPSTRANSPORT_15_014: [The function shall attempt to send every message on its waiting list, one at a time.]
// Tests_SRS_AMQPSTRANSPORT_15_036: [The function shall create a new Proton message from the IoTHub message.]
// Tests_SRS_AMQPSTRANSPORT_15_037: [The function shall attempt to send the Proton message to IoTHub using the underlying AMQPS connection.]
@Test
public void sendMessagesSendsAllMessages(@Mocked final Message mockMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final IotHubOutboundPacket mockPacket) throws IOException {
    final Map<String, Object> context = new HashMap<>();
    final byte[] messageBytes = new byte[] { 1, 2 };
    new NonStrictExpectations() {

        {
            new AmqpsIotHubConnection(mockConfig, false);
            result = mockConnection;
            new IotHubOutboundPacket(mockMsg, mockCallback, context);
            result = mockPacket;
            mockPacket.getMessage();
            result = mockMsg;
            mockMsg.getBytes();
            result = messageBytes;
        }
    };
    AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
    transport.open();
    transport.addMessage(mockMsg, mockCallback, context);
    transport.addMessage(mockMsg, mockCallback, context);
    transport.sendMessages();
    new Verifications() {

        {
            new IotHubOutboundPacket(mockMsg, mockCallback, context);
            times = 2;
            mockPacket.getMessage();
            times = 2;
            mockConnection.sendMessage((org.apache.qpid.proton.message.Message) any);
            times = 2;
        }
    };
}
Also used : AmqpsTransport(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) AmqpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) Test(org.junit.Test)

Example 14 with IotHubOutboundPacket

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

the class AmqpsTransportTest method sendMessagesAddsUserPropertiesToProtonApplicationProperties.

// Tests_SRS_AMQPSTRANSPORT_15_038: [The function shall add all user properties to the application properties of the Proton message.]
@Test
public void sendMessagesAddsUserPropertiesToProtonApplicationProperties(@Mocked final Message mockMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final IotHubOutboundPacket mockPacket) throws IOException {
    final Map<String, Object> context = new HashMap<>();
    final byte[] messageBytes = new byte[] { 1, 2 };
    final MessageProperty[] iotHubMessageProperties = new MessageProperty[] { new MessageProperty("key1", "value1"), new MessageProperty("key2", "value2") };
    final Map<String, String> userProperties = new HashMap<>(2);
    userProperties.put(iotHubMessageProperties[0].getName(), iotHubMessageProperties[0].getValue());
    userProperties.put(iotHubMessageProperties[1].getName(), iotHubMessageProperties[1].getValue());
    new NonStrictExpectations() {

        {
            new AmqpsIotHubConnection(mockConfig, false);
            result = mockConnection;
            new IotHubOutboundPacket(mockMsg, mockCallback, context);
            result = mockPacket;
            mockPacket.getMessage();
            result = mockMsg;
            mockMsg.getBytes();
            result = messageBytes;
            new MessageImpl();
            result = mockProtonMessage;
            mockMsg.getProperties();
            result = iotHubMessageProperties;
            mockConnection.sendMessage(mockProtonMessage);
            result = 1;
            new ApplicationProperties(userProperties);
        }
    };
    AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
    transport.open();
    transport.addMessage(mockMsg, mockCallback, context);
    transport.sendMessages();
    new Verifications() {

        {
            new IotHubOutboundPacket(mockMsg, mockCallback, context);
            times = 1;
            mockPacket.getMessage();
            times = 1;
            mockConnection.sendMessage(mockProtonMessage);
            times = 1;
            new ApplicationProperties(userProperties);
            times = 1;
            mockProtonMessage.setApplicationProperties((ApplicationProperties) any);
            times = 1;
        }
    };
}
Also used : AmqpsTransport(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) AmqpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl) Test(org.junit.Test)

Example 15 with IotHubOutboundPacket

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

the class AmqpsTransportTest method messageSentRemovesSuccessfullyDeliveredMessageFromInProgressMap.

// Tests_SRS_AMQPSTRANSPORT_15_030: [If the message was successfully delivered,
// its callback is added to the list of callbacks to be executed.]
@Test
public void messageSentRemovesSuccessfullyDeliveredMessageFromInProgressMap() throws IOException {
    new NonStrictExpectations() {

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

        {
            new IotHubCallbackPacket(IotHubStatusCode.OK_EMPTY, (IotHubEventCallback) any, any);
            times = 1;
        }
    };
    Queue<IotHubOutboundPacket> waitingMessages = Deencapsulation.getField(transport, "waitingMessages");
    Queue<IotHubCallbackPacket> callbackList = Deencapsulation.getField(transport, "callbackList");
    Assert.assertTrue(inProgressMessages.size() == 1);
    Assert.assertTrue(waitingMessages.size() == 0);
    Assert.assertTrue(callbackList.size() == 1);
}
Also used : AmqpsTransport(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport) AmqpsMessage(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage) IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) AmqpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Aggregations

IotHubOutboundPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)37 Test (org.junit.Test)25 HashMap (java.util.HashMap)17 AmqpsTransport (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport)15 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)14 AmqpsIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection)12 IotHubCallbackPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket)10 AmqpsMessage (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage)5 Queue (java.util.Queue)5 MqttTransport (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport)4 MqttIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection)3 IotHubEventCallback (com.microsoft.azure.sdk.iot.device.IotHubEventCallback)2 IotHubResponseCallback (com.microsoft.azure.sdk.iot.device.IotHubResponseCallback)2 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)2 MessageImpl (org.apache.qpid.proton.message.impl.MessageImpl)2 Message (com.microsoft.azure.sdk.iot.device.Message)1 Map (java.util.Map)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 SizeLimitExceededException (javax.naming.SizeLimitExceededException)1 ApplicationProperties (org.apache.qpid.proton.amqp.messaging.ApplicationProperties)1