Search in sources :

Example 16 with MqttTransport

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

the class MqttTransportTest method invokeCallbacksInvokesAllCallbacks.

// Tests_SRS_MQTTTRANSPORT_15_013: [The function shall invoke all callbacks on the callback queue.]
@Test
public void invokeCallbacksInvokesAllCallbacks(@Mocked final Message mockMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final IotHubCallbackPacket mockCallbackPacket) throws IOException {
    final Map<String, Object> context = new HashMap<>();
    new NonStrictExpectations() {

        {
            new MqttIotHubConnection(mockConfig);
            result = mockConnection;
            mockCallbackPacket.getStatus();
            returns(IotHubStatusCode.OK_EMPTY, IotHubStatusCode.ERROR);
            mockCallbackPacket.getCallback();
            result = mockCallback;
            mockCallbackPacket.getContext();
            result = context;
        }
    };
    MqttTransport transport = new MqttTransport(mockConfig);
    transport.open();
    transport.addMessage(mockMsg, mockCallback, context);
    transport.addMessage(mockMsg, mockCallback, context);
    transport.sendMessages();
    transport.invokeCallbacks();
    final IotHubEventCallback expectedCallback = mockCallback;
    final Map<String, Object> expectedContext = context;
    new VerificationsInOrder() {

        {
            expectedCallback.execute(IotHubStatusCode.OK_EMPTY, expectedContext);
            expectedCallback.execute(IotHubStatusCode.ERROR, expectedContext);
        }
    };
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) HashMap(java.util.HashMap) MqttTransport(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport) Test(org.junit.Test)

Example 17 with MqttTransport

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

the class MqttTransportTest method closeClosesMqttsConnectionAndRemovePendingMessages.

// Tests_SRS_MQTTTRANSPORT_15_005: [The function shall close the MQTT connection with the IoT Hub given in the configuration.]
// Tests_SRS_MQTTTRANSPORT_99_020: [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_MQTTTRANSPORT_99_021: [The method shall invoke the callback list]
@Test
public void closeClosesMqttsConnectionAndRemovePendingMessages(@Mocked final Message mockMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final IotHubOutboundPacket mockedPacket) throws IOException, InterruptedException {
    final MqttTransport transport = new MqttTransport(mockConfig);
    final MqttIotHubConnection 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, "waitingList");
    assertEquals(actualWaitingMessages.size(), 0);
    new Verifications() {

        {
            mockCallback.execute((IotHubStatusCode) any, any);
            times = 1;
            expectedConnection.close();
            minTimes = 1;
        }
    };
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) MqttTransport(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) Test(org.junit.Test)

Example 18 with MqttTransport

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

the class MqttTransportTest method addMessageFailsIfTransportAlreadyClosed.

// Tests_SRS_MQTTTRANSPORT_15_008: [If the transport is closed, the function shall throw an IllegalStateException.]
@Test(expected = IllegalStateException.class)
public void addMessageFailsIfTransportAlreadyClosed(@Mocked final Message mockMsg, @Mocked final IotHubEventCallback mockCallback) throws IOException {
    final Map<String, Object> context = new HashMap<>();
    MqttTransport transport = new MqttTransport(mockConfig);
    transport.open();
    transport.close();
    transport.addMessage(mockMsg, mockCallback, context);
}
Also used : HashMap(java.util.HashMap) MqttTransport(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport) Test(org.junit.Test)

Example 19 with MqttTransport

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

the class MqttTransportTest method invokeCallbacksFailsIfTransportAlreadyClosed.

// Tests_SRS_MQTTTRANSPORT_15_014: [If the transport is closed, the function shall throw an IllegalStateException.]
@Test(expected = IllegalStateException.class)
public void invokeCallbacksFailsIfTransportAlreadyClosed() throws IOException {
    MqttTransport transport = new MqttTransport(mockConfig);
    transport.open();
    transport.close();
    transport.invokeCallbacks();
}
Also used : MqttTransport(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport) Test(org.junit.Test)

Example 20 with MqttTransport

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

the class MqttTransportTest method handleMessageAttemptsToReceiveMessage.

// Tests_SRS_MQTTTRANSPORT_15_016: [The function shall attempt to consume a message from the IoT Hub.]
@Test
public void handleMessageAttemptsToReceiveMessage(@Mocked final MessageCallback mockCallback) throws IOException {
    final Object context = new Object();
    new NonStrictExpectations() {

        {
            mockConfig.getMessageCallback();
            result = mockCallback;
            mockConfig.getMessageContext();
            result = context;
        }
    };
    MqttTransport transport = new MqttTransport(mockConfig);
    transport.open();
    transport.handleMessage();
    final MqttIotHubConnection expectedConnection = mockConnection;
    new Verifications() {

        {
            expectedConnection.receiveMessage();
        }
    };
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) MqttTransport(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport) Test(org.junit.Test)

Aggregations

MqttTransport (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport)26 Test (org.junit.Test)26 MqttIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection)12 HashMap (java.util.HashMap)12 IotHubOutboundPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)4 Queue (java.util.Queue)3 IotHubCallbackPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket)1 Field (java.lang.reflect.Field)1 AssertionFailedError (junit.framework.AssertionFailedError)1