Search in sources :

Example 16 with MqttIotHubConnection

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

the class MqttTransportTest method closeDoesNothingIfConnectionAlreadyClosed.

// Tests_SRS_MQTTTRANSPORT_15_006: [If the MQTT connection is closed, the function shall do nothing.]
@Test
public void closeDoesNothingIfConnectionAlreadyClosed() throws IOException {
    MqttTransport transport = new MqttTransport(mockConfig);
    transport.open();
    transport.close();
    transport.close();
    final MqttIotHubConnection expectedConnection = mockConnection;
    new Verifications() {

        {
            expectedConnection.close();
            times = 1;
        }
    };
}
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)

Example 17 with MqttIotHubConnection

use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection 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 18 with MqttIotHubConnection

use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection 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 19 with MqttIotHubConnection

use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection 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)

Example 20 with MqttIotHubConnection

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

the class MqttTransportTest method openDoesNothingIfAlreadyOpened.

// SRS_MQTTTRANSPORT_15_004: [If the MQTT connection is already open, the function shall do nothing.]
@Test
public void openDoesNothingIfAlreadyOpened() throws IOException {
    MqttTransport transport = new MqttTransport(mockConfig);
    transport.open();
    transport.open();
    final MqttIotHubConnection expectedConnection = mockConnection;
    new Verifications() {

        {
            expectedConnection.open();
            times = 1;
        }
    };
}
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

MqttIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection)35 Test (org.junit.Test)35 NonStrictExpectations (mockit.NonStrictExpectations)14 Verifications (mockit.Verifications)14 MqttTransport (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport)12 MqttMessaging (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging)5 HashMap (java.util.HashMap)5 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)4 IOException (java.io.IOException)4 DeviceMethodMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage)3 DeviceTwinMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage)3 IotHubSasToken (com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken)3 IotHubOutboundPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)3 State (com.microsoft.azure.sdk.iot.device.transport.State)3 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)3 IotHubCallbackPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket)1 Field (java.lang.reflect.Field)1 Queue (java.util.Queue)1 AssertionFailedError (junit.framework.AssertionFailedError)1