Search in sources :

Example 26 with AmqpsIotHubConnection

use of com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection 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 27 with AmqpsIotHubConnection

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

the class AmqpsTransportTest method openOpensAmqpsConnection.

// Tests_SRS_AMQPSTRANSPORT_15_004: [The function shall open an AMQPS connection with the IoT Hub given in the configuration.]
@Test
public void openOpensAmqpsConnection() throws IOException, InterruptedException {
    new NonStrictExpectations() {

        {
            new AmqpsIotHubConnection(mockConfig, false);
            result = mockConnection;
        }
    };
    AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
    transport.open();
    final AmqpsIotHubConnection expectedConnection = mockConnection;
    new Verifications() {

        {
            expectedConnection.open();
        }
    };
}
Also used : AmqpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection) AmqpsTransport(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport) Test(org.junit.Test)

Example 28 with AmqpsIotHubConnection

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

the class AmqpsTransportTest method handleMessageConsumesAMessage.

// Tests_SRS_AMQPSTRANSPORT_15_023: [The function shall attempt to consume a message from the IoT Hub.]
// Tests_SRS_AMQPSTRANSPORT_15_026: [The function shall invoke the callback on the message.]
// Tests_SRS_AMQPSTRANSPORT_15_027: [The function shall return the message result (one of COMPLETE, ABANDON, or REJECT) to the IoT Hub.]
@Test
public void handleMessageConsumesAMessage() throws IOException {
    new NonStrictExpectations() {

        {
            new AmqpsIotHubConnection(mockConfig, false);
            result = mockConnection;
            mockConfig.getMessageCallback();
            result = mockMessageCallback;
            mockMessageCallback.execute((Message) any, any);
            result = IotHubMessageResult.COMPLETE;
            mockConnection.sendMessageResult(mockAmqpsMessage, IotHubMessageResult.COMPLETE);
            result = true;
        }
    };
    new MockUp<AmqpsTransport>() {

        @Mock
        Message protonMessageToIoTHubMessage(MessageImpl protonMessage) {
            return new Message();
        }
    };
    AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
    transport.open();
    Queue<AmqpsMessage> receivedMessages = new LinkedBlockingQueue<>();
    receivedMessages.add(mockAmqpsMessage);
    receivedMessages.add(mockAmqpsMessage);
    Deencapsulation.setField(transport, "receivedMessages", receivedMessages);
    transport.handleMessage();
    Queue<AmqpsMessage> receivedTransportMessages = Deencapsulation.getField(transport, "receivedMessages");
    new Verifications() {

        {
            mockMessageCallback.execute((Message) any, any);
            times = 1;
            mockConnection.sendMessageResult(mockAmqpsMessage, IotHubMessageResult.COMPLETE);
            times = 1;
        }
    };
    Assert.assertTrue(receivedTransportMessages.size() == 1);
}
Also used : AmqpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection) AmqpsMessage(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage) AmqpsTransport(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl) AmqpsMessage(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage) Test(org.junit.Test)

Example 29 with AmqpsIotHubConnection

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

the class AmqpsTransportTest method closeClosesAmqpsConnectionAndRemovePendingMessages.

// Tests_SRS_AMQPSTRANSPORT_15_008: [The function shall close an AMQPS connection with the IoT Hub given in the configuration.]
// Tests_SRS_AMQPSTRANSPORT_99_036: [The method shall remove all the messages which are in progress or waiting to be sent and add them to the callback list.]
// Tests_SRS_AMQPSTRANSPORT_99_037: [The method shall invoke all the callbacks.]
@Test
public void closeClosesAmqpsConnectionAndRemovePendingMessages(@Mocked final Message mockMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final IotHubOutboundPacket mockedPacket) throws IOException, InterruptedException {
    final AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
    final AmqpsIotHubConnection expectedConnection = mockConnection;
    new NonStrictExpectations() {

        {
            mockedPacket.getMessage();
            result = mockMsg;
            mockMsg.getBytes();
            result = "AnyData".getBytes();
        }
    };
    transport.open();
    transport.addMessage(mockMsg, mockCallback, null);
    transport.close();
    Queue<IotHubOutboundPacket> actualWaitingMessages = Deencapsulation.getField(transport, "waitingMessages");
    Map<Integer, IotHubOutboundPacket> actualInProgressMessages = Deencapsulation.getField(transport, "inProgressMessages");
    assertEquals(actualWaitingMessages.size(), 0);
    assertEquals(actualInProgressMessages.size(), 0);
    new Verifications() {

        {
            mockCallback.execute((IotHubStatusCode) any, any);
            times = 1;
            expectedConnection.close();
            minTimes = 1;
        }
    };
}
Also used : AmqpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection) AmqpsTransport(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) Test(org.junit.Test)

Example 30 with AmqpsIotHubConnection

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

the class AmqpsTransportTest method sendMessagesAddsNotSentMessagesToInProgressMap.

// Tests_SRS_AMQPSTRANSPORT_15_017: [If the sent message hash is not valid, it shall be buffered to be sent in a subsequent attempt.]
@Test
public void sendMessagesAddsNotSentMessagesToInProgressMap(@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;
            mockConnection.sendMessage((org.apache.qpid.proton.message.Message) any);
            returns(1, -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(1, waitingMessages.size());
    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)

Aggregations

AmqpsIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection)52 Test (org.junit.Test)52 AmqpsTransport (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport)25 IotHubOutboundPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)12 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)11 AmqpsMessage (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage)10 HashMap (java.util.HashMap)7 IotHubCallbackPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket)5 State (com.microsoft.azure.sdk.iot.device.transport.State)4 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)4 IotHubSasToken (com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken)3 MessageImpl (org.apache.qpid.proton.message.impl.MessageImpl)3 WebSocketImpl (com.microsoft.azure.sdk.iot.deps.ws.impl.WebSocketImpl)1 DeviceClientConfig (com.microsoft.azure.sdk.iot.device.DeviceClientConfig)1 IotHubReactor (com.microsoft.azure.sdk.iot.device.transport.amqps.IotHubReactor)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)1 Symbol (org.apache.qpid.proton.amqp.Symbol)1 ApplicationProperties (org.apache.qpid.proton.amqp.messaging.ApplicationProperties)1