Search in sources :

Example 21 with AmqpsTransport

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

the class AmqpsTransportTest method isEmptyReturnsFalseIfWaitingListIsNotEmpty.

// Tests_SRS_AMQPSTRANSPORT_15_035: [The function shall return true if the waiting list,
// in progress list and callback list are all empty, and false otherwise.]
@Test
public void isEmptyReturnsFalseIfWaitingListIsNotEmpty() throws IOException {
    AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
    Queue<IotHubOutboundPacket> waitingMessages = new LinkedBlockingDeque<>();
    waitingMessages.add(new IotHubOutboundPacket(new Message(), mockIotHubEventCallback, new Object()));
    Deencapsulation.setField(transport, "waitingMessages", waitingMessages);
    Boolean isEmpty = transport.isEmpty();
    Assert.assertFalse(isEmpty);
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) AmqpsTransport(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport) AmqpsMessage(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) Test(org.junit.Test)

Example 22 with AmqpsTransport

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

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

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

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

the class AmqpsTransportTest method sendMessagesFailsIfTransportAlreadyClosed.

// Tests_SRS_AMQPSTRANSPORT_15_012: [If the AMQPS session is closed, the function shall throw an IllegalStateException.]
@Test(expected = IllegalStateException.class)
public void sendMessagesFailsIfTransportAlreadyClosed() throws IOException {
    AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
    transport.open();
    transport.close();
    transport.sendMessages();
}
Also used : AmqpsTransport(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport) Test(org.junit.Test)

Aggregations

AmqpsTransport (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport)42 Test (org.junit.Test)42 AmqpsIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection)25 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)16 IotHubOutboundPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)15 HashMap (java.util.HashMap)11 AmqpsMessage (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage)10 IotHubCallbackPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket)6 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)5 State (com.microsoft.azure.sdk.iot.device.transport.State)3 MessageImpl (org.apache.qpid.proton.message.impl.MessageImpl)3 LinkedList (java.util.LinkedList)2 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)2 Queue (java.util.Queue)1 ApplicationProperties (org.apache.qpid.proton.amqp.messaging.ApplicationProperties)1