Search in sources :

Example 1 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 messageSentBuffersPreviouslySentMessageIfNotSuccessfullyDelivered.

// Tests_SRS_AMQPSTRANSPORT_15_031: [If the message was not delivered successfully, it is buffered to be sent again.]
@Test
public void messageSentBuffersPreviouslySentMessageIfNotSuccessfullyDelivered() 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, false);
    new Verifications() {

        {
            new IotHubCallbackPacket(IotHubStatusCode.OK_EMPTY, (IotHubEventCallback) any, any);
            times = 0;
        }
    };
    Queue<IotHubOutboundPacket> waitingMessages = Deencapsulation.getField(transport, "waitingMessages");
    Queue<IotHubCallbackPacket> callbackList = Deencapsulation.getField(transport, "callbackList");
    Assert.assertTrue(inProgressMessages.size() == 1);
    Assert.assertTrue(waitingMessages.size() == 1);
    Assert.assertTrue(callbackList.size() == 0);
}
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)

Example 2 with AmqpsIotHubConnection

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

the class AmqpsIotHubConnectionTest method onLinkInitReceive.

// Tests_SRS_AMQPSIOTHUBCONNECTION_14_046: [If the link is the Receiver link, the event handler shall create a new Source (Proton) object using the receiver endpoint address member variable.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_14_047: [If the link is the Receiver link, the event handler shall set its source to the created Source (Proton) object.]
@Test
public void onLinkInitReceive() throws IOException {
    baseExpectations();
    new NonStrictExpectations() {

        {
            mockEvent.getLink();
            result = mockReceiver;
            mockReceiver.getName();
            result = "receiver";
            new Source();
            result = mockSource;
            mockSource.setAddress(anyString);
            mockReceiver.setSource(mockSource);
        }
    };
    final AmqpsIotHubConnection connection = new AmqpsIotHubConnection(mockConfig, false);
    connection.onLinkInit(mockEvent);
    new Verifications() {

        {
            mockEvent.getLink();
            times = 1;
            mockReceiver.getName();
            times = 1;
            new Source();
            times = 1;
            mockSource.setAddress(anyString);
            times = 1;
            mockReceiver.setSource((org.apache.qpid.proton.amqp.transport.Source) any);
            times = 1;
        }
    };
}
Also used : AmqpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection) Source(org.apache.qpid.proton.amqp.messaging.Source) Test(org.junit.Test)

Example 3 with AmqpsIotHubConnection

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

the class AmqpsIotHubConnectionTest method onLinkRemoteClose.

// Tests_SRS_AMQPSIOTHUBCONNECTION_15_042 [The event handler shall attempt to reconnect to the IoTHub.]
@Test
public void onLinkRemoteClose() throws IOException {
    baseExpectations();
    new NonStrictExpectations() {

        {
            mockEvent.getLink();
            result = mockSender;
            mockSender.getName();
            result = "sender";
            mockServerListener.connectionLost();
        }
    };
    final AmqpsIotHubConnection connection = new AmqpsIotHubConnection(mockConfig, false);
    final Boolean[] openAsyncCalled = { false };
    final Boolean[] closeAsyncCalled = { false };
    new MockUp<AmqpsIotHubConnection>() {

        @Mock
        void openAsync() {
            openAsyncCalled[0] = true;
            Deencapsulation.setField(connection, "state", State.OPEN);
        }

        @Mock
        void closeAsync() {
            closeAsyncCalled[0] = true;
            Deencapsulation.setField(connection, "state", State.CLOSED);
        }
    };
    connection.addListener(mockServerListener);
    connection.onLinkRemoteClose(mockEvent);
    assertEquals(true, closeAsyncCalled[0]);
    assertEquals(false, openAsyncCalled[0]);
    new Verifications() {

        {
            mockEvent.getLink();
            times = 1;
            mockSender.getName();
            times = 1;
            mockServerListener.connectionLost();
            times = 1;
        }
    };
}
Also used : AmqpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection) Test(org.junit.Test)

Example 4 with AmqpsIotHubConnection

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

the class AmqpsIotHubConnectionTest method sendMessage.

// Tests_SRS_AMQPSIOTHUBCONNECTION_15_016: [The function shall encode the message and copy the contents to the byte buffer.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_017: [The function shall set the delivery tag for the sender.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_018: [The function shall attempt to send the message using the sender link.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_019: [The function shall advance the sender link.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_020: [The function shall set the delivery hash to the value returned by the sender link.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_021: [The function shall return the delivery hash.]
@Test
public void sendMessage() throws IOException {
    baseExpectations();
    new NonStrictExpectations() {

        {
            mockProtonMessage.encode((byte[]) any, anyInt, anyInt);
            mockSender.delivery((byte[]) any);
            result = mockDelivery;
        }
    };
    final AmqpsIotHubConnection connection = new AmqpsIotHubConnection(mockConfig, false);
    Deencapsulation.setField(connection, "state", State.OPEN);
    Deencapsulation.setField(connection, "linkCredit", 100);
    Deencapsulation.setField(connection, "sender", mockSender);
    Integer expectedDeliveryHash = mockDelivery.hashCode();
    Integer actualDeliveryHash = connection.sendMessage(mockProtonMessage);
    assertEquals(expectedDeliveryHash, actualDeliveryHash);
    new Verifications() {

        {
            mockProtonMessage.encode((byte[]) any, anyInt, anyInt);
            times = 1;
            mockSender.delivery((byte[]) any);
            times = 1;
            mockSender.send((byte[]) any, anyInt, anyInt);
            times = 1;
            mockSender.advance();
            times = 1;
            mockDelivery.hashCode();
            times = 1;
        }
    };
}
Also used : AmqpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection) Test(org.junit.Test)

Example 5 with AmqpsIotHubConnection

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

the class AmqpsIotHubConnectionTest method sendMessageFreesDeliveryIfSendFails.

@Test
public void sendMessageFreesDeliveryIfSendFails() throws IOException {
    baseExpectations();
    new NonStrictExpectations() {

        {
            mockProtonMessage.encode((byte[]) any, anyInt, anyInt);
            mockSender.delivery((byte[]) any);
            result = mockDelivery;
            mockSender.send((byte[]) any, anyInt, anyInt);
            result = new Exception();
        }
    };
    final AmqpsIotHubConnection connection = new AmqpsIotHubConnection(mockConfig, false);
    Deencapsulation.setField(connection, "state", State.OPEN);
    Deencapsulation.setField(connection, "linkCredit", 100);
    Deencapsulation.setField(connection, "sender", mockSender);
    Integer expectedDeliveryHash = -1;
    Integer actualDeliveryHash = connection.sendMessage(mockProtonMessage);
    assertEquals(expectedDeliveryHash, actualDeliveryHash);
    new Verifications() {

        {
            mockProtonMessage.encode((byte[]) any, anyInt, anyInt);
            times = 1;
            mockSender.delivery((byte[]) any);
            times = 1;
            mockSender.send((byte[]) any, anyInt, anyInt);
            times = 1;
            mockSender.advance();
            times = 1;
            mockDelivery.free();
            times = 1;
        }
    };
}
Also used : AmqpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection) IOException(java.io.IOException) 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