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;
}
};
}
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();
}
};
}
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);
}
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;
}
};
}
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;
}
};
}
Aggregations