Search in sources :

Example 21 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 openOpensMqttConnection.

// Tests_SRS_MQTTTRANSPORT_15_003: [The function shall establish an MQTT connection
// with IoT Hub given in the configuration.]
@Test
public void openOpensMqttConnection() throws IOException, NoSuchFieldException, IllegalAccessException {
    new NonStrictExpectations() {

        {
            new MqttIotHubConnection(mockConfig);
            result = mockConnection;
        }
    };
    MqttTransport transport = new MqttTransport(mockConfig);
    transport.open();
    final MqttIotHubConnection expectedConnection = mockConnection;
    new Verifications() {

        {
            expectedConnection.open();
        }
    };
    Field sendMessagesLock = transport.getClass().getDeclaredField("sendMessagesLock");
    sendMessagesLock.setAccessible(true);
    assertNotNull(sendMessagesLock.get(transport));
    Field handleMessageLock = transport.getClass().getDeclaredField("handleMessageLock");
    handleMessageLock.setAccessible(true);
    assertNotNull(handleMessageLock.get(transport));
}
Also used : Field(java.lang.reflect.Field) 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 22 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 closeClosesMqttConnection.

// Tests_SRS_MQTTTRANSPORT_15_005: [The function shall close the MQTT connection
// with the IoT Hub given in the configuration.]
@Test
public void closeClosesMqttConnection() throws IOException {
    MqttTransport transport = new MqttTransport(mockConfig);
    transport.open();
    transport.close();
    final MqttIotHubConnection expectedConnection = mockConnection;
    new Verifications() {

        {
            expectedConnection.close();
        }
    };
}
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 23 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 invokeCallbacksDropsFailedCallback.

// Tests_SRS_MQTTTRANSPORT_15_015: [If an exception is thrown during the callback,
// the function shall drop the callback from the queue.]
@Test
public void invokeCallbacksDropsFailedCallback(@Mocked final Message mockMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final IotHubCallbackPacket mockCallbackPacket) throws IOException {
    final Map<String, Object> context = new HashMap<>();
    new NonStrictExpectations() {

        {
            new MqttIotHubConnection(mockConfig);
            result = mockConnection;
            mockCallbackPacket.getStatus();
            result = IotHubStatusCode.OK_EMPTY;
            mockCallbackPacket.getCallback();
            result = mockCallback;
            mockCallbackPacket.getContext();
            result = context;
            mockCallback.execute(IotHubStatusCode.OK_EMPTY, context);
            result = new IllegalStateException();
            result = null;
        }
    };
    MqttTransport transport = new MqttTransport(mockConfig);
    transport.open();
    transport.addMessage(mockMsg, mockCallback, context);
    transport.sendMessages();
    try {
        transport.invokeCallbacks();
        throw new AssertionFailedError();
    } catch (IllegalStateException e) {
        transport.invokeCallbacks();
    }
    final IotHubEventCallback expectedCallback = mockCallback;
    final Map<String, Object> expectedContext = context;
    new VerificationsInOrder() {

        {
            expectedCallback.execute(IotHubStatusCode.OK_EMPTY, expectedContext);
        }
    };
}
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) AssertionFailedError(junit.framework.AssertionFailedError) Test(org.junit.Test)

Example 24 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 sendEventReturnsErrorIfMessageNotReceived.

// Tests_SRS_MQTTIOTHUBCONNECTION_15_012: [If the message was not successfully received by the service,
// the function shall return status code ERROR.]
@Test
public void sendEventReturnsErrorIfMessageNotReceived(@Mocked final Message mockMsg) throws IOException {
    baseExpectations();
    final byte[] msgBody = { 0x61, 0x62, 0x63 };
    new NonStrictExpectations() {

        {
            mockMsg.getBytes();
            result = msgBody;
            mockDeviceMessaging.send(mockMsg);
            result = new IOException(anyString);
        }
    };
    MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
    connection.open();
    IotHubStatusCode actualStatus = connection.sendEvent(mockMsg);
    IotHubStatusCode expectedStatus = IotHubStatusCode.ERROR;
    assertEquals(expectedStatus, actualStatus);
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) IOException(java.io.IOException) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 25 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 openEstablishesConnectionUsingCorrectConfig.

// Tests_SRS_MQTTIOTHUBCONNECTION_15_004: [The function shall establish an MQTT connection with an IoT Hub
// using the provided host name, user name, device ID, and sas token.]
@Test
public void openEstablishesConnectionUsingCorrectConfig() throws IOException {
    baseExpectations();
    openExpectations();
    MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
    connection.open();
    final String actualIotHubUserName = Deencapsulation.getField(connection, "iotHubUserName");
    String clientIdentifier = "DeviceClientType=" + URLEncoder.encode(TransportUtils.JAVA_DEVICE_CLIENT_IDENTIFIER + TransportUtils.CLIENT_VERSION, "UTF-8");
    assertEquals(iotHubHostName + "/" + deviceId + "/" + API_VERSION + "/" + clientIdentifier, actualIotHubUserName);
    String expectedSasToken = mockToken.toString();
    String actualUserPassword = Deencapsulation.getField(connection, "iotHubUserPassword");
    assertEquals(expectedSasToken, actualUserPassword);
    State expectedState = State.OPEN;
    State actualState = Deencapsulation.getField(connection, "state");
    assertEquals(expectedState, actualState);
    new Verifications() {

        {
            new MqttDeviceMethod();
            times = 1;
            new MqttMessaging(sslPrefix + iotHubHostName + sslPortSuffix, deviceId, anyString, anyString, mockIotHubSSLContext);
            mockDeviceMessaging.start();
            times = 1;
            new MqttDeviceTwin();
            times = 1;
        }
    };
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) State(com.microsoft.azure.sdk.iot.device.transport.State) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) 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)

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