Search in sources :

Example 26 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 sendEventFailsIfConnectionClosed.

// Tests_SRS_MQTTIOTHUBCONNECTION_15_013: [If the MQTT connection is closed,
// the function shall throw an IllegalStateException.]
@Test(expected = IllegalStateException.class)
public void sendEventFailsIfConnectionClosed(@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.open();
    connection.close();
    connection.sendEvent(mockMsg);
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 27 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 closeDoesNothingIfConnectionNotYetOpened.

// Tests_SRS_MQTTIOTHUBCONNECTION_15_007: [If the MQTT connection is closed, the function shall do nothing.]
@Test
public void closeDoesNothingIfConnectionNotYetOpened() throws IOException {
    baseExpectations();
    MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
    connection.close();
    State expectedState = State.CLOSED;
    State actualState = Deencapsulation.getField(connection, "state");
    assertEquals(expectedState, actualState);
    new Verifications() {

        {
            mockDeviceMessaging.stop();
            times = 0;
            mockDeviceMethods.stop();
            times = 0;
            mockDeviceTwin.stop();
            times = 0;
        }
    };
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) State(com.microsoft.azure.sdk.iot.device.transport.State) Verifications(mockit.Verifications) Test(org.junit.Test)

Example 28 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 closeDoesNothingIfConnectionAlreadyClosed.

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

        {
            mockDeviceMessaging.stop();
            maxTimes = 1;
            mockDeviceMethods.stop();
            maxTimes = 1;
            mockDeviceTwin.stop();
            maxTimes = 1;
        }
    };
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) Verifications(mockit.Verifications) Test(org.junit.Test)

Example 29 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 closeClosesMqttConnection.

// Tests_SRS_MQTTIOTHUBCONNECTION_15_005: [The function shall close the MQTT connection.]
@Test
public void closeClosesMqttConnection() throws IOException {
    baseExpectations();
    openExpectations();
    MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
    connection.open();
    connection.close();
    State expectedState = State.CLOSED;
    State actualState = Deencapsulation.getField(connection, "state");
    assertEquals(expectedState, actualState);
    MqttDeviceMethod actualDeviceMethods = Deencapsulation.getField(connection, "deviceMethod");
    assertNull(actualDeviceMethods);
    MqttDeviceTwin actualDeviceTwin = Deencapsulation.getField(connection, "deviceTwin");
    assertNull(actualDeviceTwin);
    MqttDeviceTwin actualDeviceMessaging = Deencapsulation.getField(connection, "deviceMessaging");
    assertNull(actualDeviceMessaging);
    new Verifications() {

        {
            mockDeviceMessaging.stop();
            times = 1;
            mockDeviceMethods.stop();
            times = 1;
            mockDeviceTwin.stop();
            times = 1;
        }
    };
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) State(com.microsoft.azure.sdk.iot.device.transport.State) MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) Verifications(mockit.Verifications) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) Test(org.junit.Test)

Example 30 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 receiveDeviceTwinMessageSucceeds.

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

        {
            mockDeviceMethods.receive();
            result = null;
            mockDeviceTwin.receive();
            result = new DeviceTwinMessage(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;
            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) DeviceTwinMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage) 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