Search in sources :

Example 1 with AmqpsMessage

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

the class AmqpsMessageTest method acknowledgeSetsRejectedDispositionForReject.

// Tests_SRS_AMQPSMESSAGE_14_003: [If the ACK_TYPE is REJECT, the function shall set a Rejected disposition on the private Delivery object.]
@Test
public void acknowledgeSetsRejectedDispositionForReject(@Mocked final Rejected mockRejected) {
    new NonStrictExpectations() {

        {
            new Rejected();
            result = mockRejected;
        }
    };
    AmqpsMessage message = new AmqpsMessage();
    message.setDelivery(mockDelivery);
    message.acknowledge(AmqpsMessage.ACK_TYPE.REJECT);
    final Delivery expectedDelivery = mockDelivery;
    new Verifications() {

        {
            expectedDelivery.disposition(mockRejected);
        }
    };
}
Also used : Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) Delivery(org.apache.qpid.proton.engine.Delivery) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) AmqpsMessage(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage) Test(org.junit.Test)

Example 2 with AmqpsMessage

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

the class AmqpsMessageTest method acknowledgeSettlesDelivery.

// Tests_SRS_AMQPSMESSAGE_14_005: [The function shall settle the delivery after setting the proper disposition.]
@Test
public void acknowledgeSettlesDelivery() {
    AmqpsMessage message = new AmqpsMessage();
    message.setDelivery(mockDelivery);
    message.acknowledge(AmqpsMessage.ACK_TYPE.COMPLETE);
    message.acknowledge(AmqpsMessage.ACK_TYPE.ABANDON);
    message.acknowledge(AmqpsMessage.ACK_TYPE.REJECT);
    final Delivery expectedDelivery = mockDelivery;
    new Verifications() {

        {
            expectedDelivery.settle();
            times = 3;
        }
    };
}
Also used : Delivery(org.apache.qpid.proton.engine.Delivery) Verifications(mockit.Verifications) AmqpsMessage(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage) Test(org.junit.Test)

Example 3 with AmqpsMessage

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

the class AmqpsIotHubConnectionTest method onDeliveryReceive.

// Tests_SRS_AMQPSIOTHUBCONNECTION_15_034: [If this link is the Receiver link, the event handler shall get the Receiver and Delivery (Proton) objects from the event.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_035: [The event handler shall read the received buffer.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_036: [The event handler shall create an AmqpsMessage object from the decoded buffer.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_037: [The event handler shall set the AmqpsMessage Deliver (Proton) object.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_049: [All the listeners shall be notified that a message was received from the server.]
@Test
public void onDeliveryReceive() throws IOException {
    baseExpectations();
    new NonStrictExpectations() {

        {
            mockEvent.getLink();
            result = mockReceiver;
            mockReceiver.getName();
            result = "receiver";
            mockReceiver.current();
            result = mockDelivery;
            mockDelivery.isReadable();
            result = true;
            mockDelivery.isPartial();
            result = false;
            mockDelivery.pending();
            result = 10;
            mockReceiver.recv((byte[]) any, anyInt, anyInt);
            result = 10;
            mockReceiver.advance();
            new AmqpsMessage();
            result = mockAmqpsMessage;
            mockAmqpsMessage.setDelivery(mockDelivery);
            mockAmqpsMessage.decode((byte[]) any, anyInt, anyInt);
            mockServerListener.messageReceived(mockAmqpsMessage);
        }
    };
    final AmqpsIotHubConnection connection = new AmqpsIotHubConnection(mockConfig, false);
    connection.addListener(mockServerListener);
    connection.onDelivery(mockEvent);
    new Verifications() {

        {
            mockEvent.getLink();
            times = 2;
            mockReceiver.getName();
            times = 1;
            mockReceiver.current();
            times = 1;
            mockDelivery.isReadable();
            times = 1;
            mockDelivery.isPartial();
            times = 1;
            mockDelivery.pending();
            times = 1;
            mockReceiver.recv((byte[]) any, anyInt, anyInt);
            times = 1;
            mockReceiver.advance();
            times = 1;
            mockAmqpsMessage.setDelivery(mockDelivery);
            times = 1;
            mockAmqpsMessage.decode((byte[]) any, anyInt, anyInt);
            times = 1;
            mockServerListener.messageReceived(mockAmqpsMessage);
            times = 1;
        }
    };
}
Also used : AmqpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection) AmqpsMessage(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage) Test(org.junit.Test)

Example 4 with AmqpsMessage

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

the class AmqpsTransportTest method handleMessageReturnsIfConfigIsNull.

// Tests_SRS_AMQPSTRANSPORT_15_024: [If no message was received from IotHub, the function shall return.]
@Test
public void handleMessageReturnsIfConfigIsNull() throws IOException {
    new NonStrictExpectations() {

        {
            new AmqpsIotHubConnection(mockConfig, false);
            result = mockConnection;
            mockConfig.getMessageCallback();
            result = mockMessageCallback;
        }
    };
    AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
    transport.open();
    transport.handleMessage();
    Queue<AmqpsMessage> receivedMessages = Deencapsulation.getField(transport, "receivedMessages");
    Assert.assertEquals(0, receivedMessages.size());
    new Verifications() {

        {
            mockMessageCallback.execute((Message) 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) AmqpsMessage(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage) Test(org.junit.Test)

Example 5 with AmqpsMessage

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

the class AmqpsTransportTest method handleMessagePutsMessageBackIntoQueueIfCannotSendResultBackToServer.

// Tests_SRS_AMQPSTRANSPORT_15_028: [If the result could not be sent to IoTHub, the message shall be put back in the received messages queue to be processed again.]
// Tests_SRS_AMQPSTRANSPORT_15_028: [If the result could not be sent to IoTHub, the message shall be put back in the received messages queue to be processed again.]
@Test
public void handleMessagePutsMessageBackIntoQueueIfCannotSendResultBackToServer() 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 = false;
        }
    };
    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");
    Assert.assertTrue(receivedTransportMessages.size() == 2);
    new Verifications() {

        {
            mockMessageCallback.execute((Message) any, any);
            times = 1;
            mockConnection.sendMessageResult(mockAmqpsMessage, IotHubMessageResult.COMPLETE);
            times = 1;
        }
    };
    Assert.assertTrue(receivedTransportMessages.size() == 2);
}
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)

Aggregations

AmqpsMessage (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage)10 Test (org.junit.Test)10 AmqpsIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection)6 AmqpsTransport (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport)5 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)4 Verifications (mockit.Verifications)4 Delivery (org.apache.qpid.proton.engine.Delivery)4 MessageImpl (org.apache.qpid.proton.message.impl.MessageImpl)2 NonStrictExpectations (mockit.NonStrictExpectations)1 Rejected (org.apache.qpid.proton.amqp.messaging.Rejected)1