Search in sources :

Example 1 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 receiveMessageSucceeds.

// Tests_SRS_MQTTIOTHUBCONNECTION_15_014: [The function shall attempt to consume a message
// from the received messages queue.]
@Test
public void receiveMessageSucceeds() throws IOException {
    baseExpectations();
    openExpectations();
    final byte[] expectedMessageBody = { 0x61, 0x62, 0x63 };
    new NonStrictExpectations() {

        {
            mockDeviceMethods.receive();
            result = null;
            mockDeviceTwin.receive();
            result = null;
            mockDeviceMessaging.receive();
            result = new Message(expectedMessageBody);
        }
    };
    MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
    connection.open();
    Message message = connection.receiveMessage();
    byte[] actualMessageBody = message.getBytes();
    for (int i = 0; i < expectedMessageBody.length; i++) {
        assertEquals(expectedMessageBody[i], actualMessageBody[i]);
    }
    new Verifications() {

        {
            mockDeviceTwin.receive();
            times = 1;
            mockDeviceMethods.receive();
            times = 1;
            mockDeviceMessaging.receive();
            times = 1;
        }
    };
}
Also used : DeviceTwinMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage) DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage) MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 2 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 openDoesNothingIfAlreadyOpened.

// Tests_SRS_MQTTIOTHUBCONNECTION_15_006: [If the MQTT connection is already open, the function shall do nothing.]
@Test
public void openDoesNothingIfAlreadyOpened() throws IOException {
    baseExpectations();
    openExpectations();
    MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
    connection.open();
    connection.open();
    new Verifications() {

        {
            new MqttMessaging(sslPrefix + iotHubHostName + sslPortSuffix, deviceId, anyString, anyString, mockIotHubSSLContext);
            times = 1;
        }
    };
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) Verifications(mockit.Verifications) Test(org.junit.Test)

Example 3 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 sendEventReturnsBadFormatIfMessageHasEmptyBody.

// Tests_SRS_MQTTIOTHUBCONNECTION_15_010: [If the message is null or empty,
// the function shall return status code BAD_FORMAT.]
@Test
public void sendEventReturnsBadFormatIfMessageHasEmptyBody(@Mocked final Message mockMsg) throws IOException {
    baseExpectations();
    new NonStrictExpectations() {

        {
            mockMsg.getBytes();
            result = new byte[0];
        }
    };
    MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
    connection.open();
    IotHubStatusCode result = connection.sendEvent(mockMsg);
    assertEquals(IotHubStatusCode.BAD_FORMAT, result);
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 4 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 openThrowsIOExceptionIfConnectionFailsInTwin.

@Test(expected = IOException.class)
public void openThrowsIOExceptionIfConnectionFailsInTwin() throws IOException {
    baseExpectations();
    new NonStrictExpectations() {

        {
            new IotHubSasToken(mockConfig, anyLong);
            result = mockToken;
            new MqttMessaging(sslPrefix + iotHubHostName + sslPortSuffix, deviceId, anyString, anyString, mockIotHubSSLContext);
            result = mockDeviceMessaging;
            new MqttDeviceMethod();
            result = mockDeviceMethods;
            new MqttDeviceTwin();
            result = new IOException(anyString);
        }
    };
    try {
        MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
        connection.open();
    } catch (Exception e) {
        new Verifications() {

            {
                mockDeviceMessaging.stop();
                times = 1;
                mockDeviceMethods.stop();
                times = 1;
            }
        };
        throw e;
    }
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) IotHubSasToken(com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken) IOException(java.io.IOException) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) IOException(java.io.IOException) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) Test(org.junit.Test)

Example 5 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 sendEventSendsDeviceTwinMessage.

@Test
public void sendEventSendsDeviceTwinMessage(@Mocked final DeviceTwinMessage mockDeviceTwinMsg) throws IOException {
    baseExpectations();
    openExpectations();
    final byte[] msgBody = { 0x61, 0x62, 0x63 };
    new NonStrictExpectations() {

        {
            mockDeviceTwinMsg.getBytes();
            result = msgBody;
            mockDeviceTwinMsg.getMessageType();
            result = MessageType.DeviceTwin;
        }
    };
    MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
    connection.open();
    IotHubStatusCode result = connection.sendEvent(mockDeviceTwinMsg);
    assertEquals(IotHubStatusCode.OK_EMPTY, result);
    new Verifications() {

        {
            mockDeviceMethods.send((DeviceMethodMessage) any);
            times = 0;
            mockDeviceMessaging.send(mockDeviceTwinMsg);
            times = 0;
            mockDeviceTwin.start();
            times = 1;
            mockDeviceTwin.send(mockDeviceTwinMsg);
            times = 1;
        }
    };
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) 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