Search in sources :

Example 11 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 isEmptyReturnsFalseIfCallbackListIsNotEmpty.

// Tests_SRS_MqttTransport_11_019: [The function shall return true if the waiting list
// and callback list are all empty, and false otherwise.]
@Test
public void isEmptyReturnsFalseIfCallbackListIsNotEmpty(@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.addMessage(mockMsg, mockCallback, context);
    transport.addMessage(mockMsg, mockCallback, context);
    transport.addMessage(mockMsg, mockCallback, context);
    transport.sendMessages();
    boolean testIsEmpty = transport.isEmpty();
    final boolean expectedIsEmpty = false;
    assertThat(testIsEmpty, is(expectedIsEmpty));
}
Also used : HashMap(java.util.HashMap) MqttTransport(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport) Test(org.junit.Test)

Example 12 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 addMessageAddsToTransportQueue.

// Tests_SRS_MQTTTRANSPORT_15_001: [The constructor shall initialize an empty transport queue
// for adding messages to be sent as a batch.]
// Tests_SRS_MQTTTRANSPORT_15_007: [The function shall add a packet containing the message,
// callback, and callback context to the transport queue.]
@Test
public <T extends Queue> void addMessageAddsToTransportQueue(@Mocked final Message mockMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final IotHubOutboundPacket mockPacket) throws IOException {
    final Queue mockQueue = new MockUp<T>() {
    }.getMockInstance();
    final Map<String, Object> context = new HashMap<>();
    MqttTransport transport = new MqttTransport(mockConfig);
    transport.open();
    transport.addMessage(mockMsg, mockCallback, context);
    new VerificationsInOrder() {

        {
            new IotHubOutboundPacket(mockMsg, mockCallback, context);
            mockQueue.add(mockPacket);
        }
    };
}
Also used : HashMap(java.util.HashMap) 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 MqttTransport

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

the class MqttTransportTest method handleMessageInvokesCallbackIfMessageReceived.

// Tests_SRS_MQTTTRANSPORT_15_017: [If a message is found and a message callback is registered,
// the function shall invoke the callback on the message.]
@Test
public void handleMessageInvokesCallbackIfMessageReceived(@Mocked final MessageCallback mockCallback, @Mocked final Message mockMsg) throws IOException {
    final Object context = new Object();
    new NonStrictExpectations() {

        {
            mockConfig.getMessageCallback();
            result = mockCallback;
            mockConfig.getMessageContext();
            result = context;
            mockConnection.receiveMessage();
            result = mockMsg;
        }
    };
    MqttTransport transport = new MqttTransport(mockConfig);
    transport.open();
    transport.handleMessage();
    final MessageCallback expectedCallback = mockCallback;
    final Message expectedMsg = mockMsg;
    final Object expectedContext = context;
    new Verifications() {

        {
            expectedCallback.execute(expectedMsg, expectedContext);
        }
    };
}
Also used : MqttTransport(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport) Test(org.junit.Test)

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

// Tests_SRS_MqttTransport_11_019: [The function shall return true if the waiting list
// and callback list are all empty, and false otherwise.]
@Test
public void isEmptyReturnsTrueIfEmpty(@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.addMessage(mockMsg, mockCallback, context);
    transport.addMessage(mockMsg, mockCallback, context);
    transport.addMessage(mockMsg, mockCallback, context);
    transport.sendMessages();
    transport.invokeCallbacks();
    boolean testIsEmpty = transport.isEmpty();
    final boolean expectedIsEmpty = true;
    assertThat(testIsEmpty, is(expectedIsEmpty));
}
Also used : HashMap(java.util.HashMap) MqttTransport(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport) Test(org.junit.Test)

Example 15 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 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)

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