Search in sources :

Example 11 with MqttIotHubConnection

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

the class MqttIotHubConnectionTest method constructorSavesCorrectConfig.

// Tests_SRS_MQTTIOTHUBCONNECTION_15_001: [The constructor shall save the configuration.]
@Test
public void constructorSavesCorrectConfig() throws IOException {
    baseExpectations();
    MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
    DeviceClientConfig actualClientConfig = Deencapsulation.getField(connection, "config");
    DeviceClientConfig expectedClientConfig = mockConfig;
    assertEquals(expectedClientConfig, actualClientConfig);
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) Test(org.junit.Test)

Example 12 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 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);
        }
    };
}
Also used : HashMap(java.util.HashMap) IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) 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) Queue(java.util.Queue) Test(org.junit.Test)

Example 13 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 sendMessagesSendsAllMessages.

// Tests_SRS_MQTTTRANSPORT_15_009: [The function shall attempt to send every message
// on its waiting list, one at a time.]
@Test
public void sendMessagesSendsAllMessages(@Mocked final Message mockMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final IotHubOutboundPacket mockPacket, @Mocked final MqttIotHubConnection mockConnection) throws IOException {
    final Map<String, Object> context = new HashMap<>();
    new NonStrictExpectations() {

        {
            new MqttIotHubConnection(mockConfig);
            result = mockConnection;
            new IotHubOutboundPacket(mockMsg, mockCallback, context);
            result = mockPacket;
            mockPacket.getMessage();
            result = mockMsg;
        }
    };
    MqttTransport transport = new MqttTransport(mockConfig);
    transport.open();
    transport.addMessage(mockMsg, mockCallback, context);
    transport.addMessage(mockMsg, mockCallback, context);
    transport.sendMessages();
    final MqttIotHubConnection expectedConnection = mockConnection;
    new Verifications() {

        {
            expectedConnection.sendEvent(mockMsg);
            times = 2;
        }
    };
}
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) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) Test(org.junit.Test)

Example 14 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 closeDoesNothingIfConnectionNeverOpened.

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

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

// Tests_SRS_MQTTTRANSPORT_15_011: [If the IoT Hub could not be reached, 
// the message shall be buffered to be sent again next time.]
@Test
public void sendMessagesBuffersFailedMessages(@Mocked final Message mockMsg, @Mocked final IotHubEventCallback mockCallback) throws IOException {
    final Map<String, Object> context = new HashMap<>();
    new NonStrictExpectations() {

        {
            mockConnection.sendEvent((Message) any);
            result = new IllegalStateException(anyString);
            result = IotHubStatusCode.OK_EMPTY;
        }
    };
    MqttTransport transport = new MqttTransport(mockConfig);
    transport.open();
    transport.addMessage(mockMsg, mockCallback, context);
    transport.sendMessages();
    transport.sendMessages();
    final MqttIotHubConnection expectedConnection = mockConnection;
    new Verifications() {

        {
            expectedConnection.sendEvent(mockMsg);
            times = 2;
        }
    };
}
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)

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