Search in sources :

Example 31 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 sendEventSendsMessageCorrectlyToIotHub.

// Tests_SRS_MQTTIOTHUBCONNECTION_15_008: [The function shall send an event message to the IoT Hub
// given in the configuration.]
// Tests_SRS_MQTTIOTHUBCONNECTION_15_009: [The function shall send the message payload.]
// Tests_SRS_MQTTIOTHUBCONNECTION_15_011: [If the message was successfully received by the service,
// the function shall return status code OK_EMPTY.]
@Test
public void sendEventSendsMessageCorrectlyToIotHub(@Mocked final Message mockMsg) throws IOException {
    baseExpectations();
    openExpectations();
    final byte[] msgBody = { 0x61, 0x62, 0x63 };
    new NonStrictExpectations() {

        {
            mockMsg.getBytes();
            result = msgBody;
            mockDeviceMessaging.send(mockMsg);
        }
    };
    MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
    connection.open();
    IotHubStatusCode result = connection.sendEvent(mockMsg);
    assertEquals(IotHubStatusCode.OK_EMPTY, result);
    new Verifications() {

        {
            mockDeviceMessaging.send(mockMsg);
            times = 1;
        }
    };
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 32 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 sendEventFailsIfConnectionNotYetOpened.

// Tests_SRS_MQTTIOTHUBCONNECTION_15_013: [If the MQTT connection is closed,
// the function shall throw an IllegalStateException.]
@Test(expected = IllegalStateException.class)
public void sendEventFailsIfConnectionNotYetOpened(@Mocked final Message mockMsg) throws IOException {
    baseExpectations();
    final byte[] msgBody = { 0x61, 0x62, 0x63 };
    new NonStrictExpectations() {

        {
            mockMsg.getBytes();
            result = msgBody;
        }
    };
    MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
    connection.sendEvent(mockMsg);
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 33 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 receiveMessageFailsIfConnectionClosed.

// Tests_SRS_MQTTIOTHUBCONNECTION_15_015: [If the MQTT connection is closed,
// the function shall throw an IllegalStateException.]
@Test(expected = IllegalStateException.class)
public void receiveMessageFailsIfConnectionClosed(@Mocked final Message mockMsg) throws IOException {
    baseExpectations();
    MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
    connection.receiveMessage();
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) Test(org.junit.Test)

Example 34 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 receiveDeviceMethodMessageSucceeds.

@Test
public void receiveDeviceMethodMessageSucceeds() throws IOException {
    baseExpectations();
    openExpectations();
    final byte[] expectedMessageBody = { 0x61, 0x62, 0x63 };
    new NonStrictExpectations() {

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

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

Example 35 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 sendEventSendsDeviceMethodMessage.

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

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

        {
            mockDeviceMethods.start();
            times = 1;
            mockDeviceMethods.send(mockDeviceMethodMsg);
            times = 1;
            mockDeviceMessaging.send(mockDeviceMethodMsg);
            times = 0;
            mockDeviceTwin.send(mockDeviceMethodMsg);
            times = 0;
        }
    };
}
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