use of com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket 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.IotHubCallbackPacket in project azure-iot-sdk-java by Azure.
the class MqttTransportTest method sendMessagesAddsToCallbackQueue.
// Tests_SRS_MQTTTRANSPORT_15_010: [For each message being sent, the function shall send the message
// and add the IoT Hub status code along with the callback and context to the callback list.]
@Test
public <T extends Queue> void sendMessagesAddsToCallbackQueue(@Mocked final Message mockMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final IotHubOutboundPacket mockPacket, @Mocked final IotHubCallbackPacket mockCallbackPacket) throws IOException {
final Queue mockQueue = new MockUp<T>() {
}.getMockInstance();
final Map<String, Object> context = new HashMap<>();
new NonStrictExpectations() {
{
new MqttIotHubConnection(mockConfig);
result = mockConnection;
new IotHubOutboundPacket(mockMsg, mockCallback, context);
result = mockPacket;
mockPacket.getCallback();
result = mockCallback;
mockPacket.getContext();
result = context;
mockConnection.sendEvent((Message) any);
returns(IotHubStatusCode.OK_EMPTY, IotHubStatusCode.ERROR);
new IotHubCallbackPacket(IotHubStatusCode.OK_EMPTY, mockCallback, context);
result = mockCallbackPacket;
new IotHubCallbackPacket(IotHubStatusCode.ERROR, mockCallback, context);
result = mockCallbackPacket;
}
};
MqttTransport transport = new MqttTransport(mockConfig);
transport.open();
transport.addMessage(mockMsg, mockCallback, context);
transport.addMessage(mockMsg, mockCallback, context);
transport.sendMessages();
new VerificationsInOrder() {
{
new IotHubCallbackPacket(IotHubStatusCode.OK_EMPTY, mockCallback, context);
mockQueue.add(mockCallbackPacket);
new IotHubCallbackPacket(IotHubStatusCode.ERROR, mockCallback, context);
mockQueue.add(mockCallbackPacket);
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket in project azure-iot-sdk-java by Azure.
the class HttpsTransportTest method sendMessagesAddsToCompletedQueue.
// Tests_SRS_HTTPSTRANSPORT_11_006: [The function shall add a packet containing the callbacks, contexts, and response for all sent messages to the callback queue.]
@Test
public <T extends Queue> void sendMessagesAddsToCompletedQueue(@Mocked final Message mockMsg, @Mocked final HttpsSingleMessage mockHttpsMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final HttpsBatchMessage mockBatch, @Mocked final ResponseMessage mockResponseMessage, @Mocked final IotHubCallbackPacket mockCallbackPacket) throws URISyntaxException, IOException {
final Queue mockQueue = new MockUp<T>() {
}.getMockInstance();
final IotHubStatusCode iotHubStatus = IotHubStatusCode.OK;
new NonStrictExpectations() {
{
HttpsSingleMessage.parseHttpsMessage(mockMsg);
result = mockHttpsMsg;
new HttpsBatchMessage();
result = mockBatch;
mockConn.sendEvent((HttpsMessage) any);
result = mockResponseMessage;
mockResponseMessage.getStatus();
result = iotHubStatus;
}
};
final Map<String, Object> context = new HashMap<>();
HttpsTransport transport = new HttpsTransport(mockConfig);
transport.open();
transport.addMessage(mockMsg, mockCallback, context);
transport.addMessage(mockMsg, mockCallback, context);
transport.sendMessages();
final IotHubStatusCode expectedStatus = iotHubStatus;
final Map<String, Object> expectedContext = context;
new VerificationsInOrder() {
{
new IotHubCallbackPacket(expectedStatus, mockCallback, expectedContext);
mockQueue.add(mockCallbackPacket);
times = 2;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket in project azure-iot-sdk-java by Azure.
the class HttpsTransport method addOutboundPacketToCallbackList.
private void addOutboundPacketToCallbackList(IotHubOutboundPacket packet, ResponseMessage responseMessage) {
IotHubEventCallback eventCallback = packet.getCallback();
if (eventCallback != null) {
IotHubCallbackPacket callbackPacket = new IotHubCallbackPacket(responseMessage.getStatus(), eventCallback, packet.getContext());
this.callbackList.add(callbackPacket);
}
IotHubResponseCallback responseCallback = packet.getResponseCallback();
if (responseCallback != null) {
IotHubCallbackPacket callbackPacket = new IotHubCallbackPacket(responseMessage, responseCallback, packet.getContext());
this.callbackList.add(callbackPacket);
}
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket in project azure-iot-sdk-java by Azure.
the class MqttTransport method close.
/**
* Closes all resources used to communicate with an IoT Hub. Once {@code close()} is
* called, the transport is no longer usable. If the transport is already
* closed, the function shall do nothing.
*/
public void close() throws IOException {
// Codes_SRS_MQTTTRANSPORT_15_006: [If the MQTT connection is closed, the function shall do nothing.]
if (this.state == State.CLOSED) {
return;
}
// Codes_SRS_MQTTTRANSPORT_99_020: [The method will remove all the messages which are in progress or waiting to be sent and add them to the callback list.]
while (!this.waitingList.isEmpty()) {
IotHubOutboundPacket packet = this.waitingList.remove();
IotHubCallbackPacket callbackPacket = new IotHubCallbackPacket(IotHubStatusCode.MESSAGE_CANCELLED_ONCLOSE, packet.getCallback(), packet.getContext());
this.callbackList.add(callbackPacket);
}
// Codes_SRS_MQTTTRANSPORT_99_021: [The method will invoke the callback list]
invokeCallbacks();
// Codes_SRS_MQTTTRANSPORT_15_005: [The function shall close the MQTT connection
// with the IoT Hub given in the configuration.]
this.mqttIotHubConnection.close();
this.state = State.CLOSED;
}
Aggregations