Search in sources :

Example 11 with Mqtt

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

the class MqttTest method publishWithDifferentTopicsFromDifferentConcreteClassSucceeeds.

/*
    **Tests_SRS_Mqtt_25_014: [**The function shall publish message payload on the publishTopic specified to the IoT Hub given in the configuration.**]**
     */
@Test
public void publishWithDifferentTopicsFromDifferentConcreteClassSucceeeds() throws IOException, MqttException {
    //arrange
    baseConstructorExpectations(true);
    baseConstructorExpectations(false);
    baseConnectExpectation();
    basePublishExpectations();
    basePublishExpectations();
    final byte[] payload = { 0x61, 0x62, 0x63 };
    String mockParseTopic2 = mockParseTopic + 2;
    Mqtt mockMqtt1 = instantiateMqtt(true);
    Mqtt mockMqtt2 = instantiateMqtt(false);
    Deencapsulation.invoke(mockMqtt2, "connect");
    //act
    Deencapsulation.invoke(mockMqtt1, "publish", mockParseTopic, payload);
    Deencapsulation.invoke(mockMqtt2, "publish", mockParseTopic2, payload);
    //assert
    new Verifications() {

        {
            mockMqttAsyncClient.isConnected();
            minTimes = 3;
            mockMqttAsyncClient.publish(anyString, mockMqttMessage);
            times = 2;
        }
    };
    testCleanUp(mockMqtt1);
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Test(org.junit.Test)

Example 12 with Mqtt

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

the class MqttTest method receiveThrowsIOExceptionWhenParsePayloadReturnsNull.

/*
    **Tests_SRS_Mqtt_25_025: [**If the call to parsePayload returns null when topic is non-null then this method will throw IOException**]**
     */
@Test(expected = IOException.class)
public void receiveThrowsIOExceptionWhenParsePayloadReturnsNull() throws IOException, MqttException {
    //arrange
    final byte[] payload = { 0x61, 0x62, 0x63 };
    baseConstructorExpectations(true);
    new MockUp<MqttMessaging>() {

        @Mock
        String parseTopic() throws IOException {
            return mockParseTopic;
        }

        @Mock
        byte[] parsePayload(String topic) throws IOException {
            return null;
        }
    };
    final Mqtt mockMqtt = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
    //act
    try {
        Message receivedMessage = mockMqtt.receive();
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Message(com.microsoft.azure.sdk.iot.device.Message) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) Test(org.junit.Test)

Example 13 with Mqtt

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

the class MqttTest method constructorThrowsExceptionIfMqttAsyncClientFails.

/*
    **Tests_SRS_Mqtt_25_045: [**The constructor throws IOException if MqttException is thrown and doesn't instantiate this instance.**]**
     */
@Test(expected = IOException.class)
public void constructorThrowsExceptionIfMqttAsyncClientFails() throws IOException, MqttException {
    Mqtt mockMqtt = null;
    try {
        //arrange
        new NonStrictExpectations() {

            {
                new MemoryPersistence();
                result = mockMemoryPersistence;
                new MqttAsyncClient(serverUri, clientId, mockMemoryPersistence);
                result = mockMqttException;
            }
        };
        //act
        mockMqtt = instantiateMqtt(true);
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) Test(org.junit.Test)

Example 14 with Mqtt

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

the class MqttTest method disconnectDoesNothingWhenNotConnected.

/*
    **Tests_SRS_Mqtt_25_010: [**If the MQTT connection is closed, the function shall do nothing.**]**
     */
@Test
public void disconnectDoesNothingWhenNotConnected() throws IOException, MqttException {
    //arrange
    baseConstructorExpectations(true);
    baseConnectExpectation();
    new NonStrictExpectations() {

        {
            mockMqttAsyncClient.isConnected();
            result = false;
        }
    };
    Mqtt mockMqtt = instantiateMqtt(true);
    Deencapsulation.invoke(mockMqtt, "connect");
    //act
    Deencapsulation.invoke(mockMqtt, "disconnect");
    //assert
    new Verifications() {

        {
            mockMqttAsyncClient.isConnected();
            times = 2;
        }
    };
    Object actualInfoInstance = Deencapsulation.getField(mockMqtt, "info");
    MqttAsyncClient actualMqttAsyncClient = Deencapsulation.getField(actualInfoInstance, "mqttAsyncClient");
    assertNull(actualMqttAsyncClient);
    testCleanUp(mockMqtt);
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Test(org.junit.Test)

Example 15 with Mqtt

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

the class MqttTest method connectThrowsIoExceptionOnMqttException.

/*
    **Tests_SRS_Mqtt_25_007: [**If an MQTT connection is unable to be established for any reason, the function shall throw an IOException.**]**
     */
@Test(expected = IOException.class)
public void connectThrowsIoExceptionOnMqttException() throws IOException, MqttException {
    //arrange
    Mqtt mockMqtt = null;
    try {
        baseConstructorExpectations(true);
        new NonStrictExpectations() {

            {
                mockMqttAsyncClient.isConnected();
                result = false;
                mockMqttAsyncClient.connect(mockMqttConnectionOptions);
                result = mockMqttException;
            }
        };
        mockMqtt = instantiateMqtt(true);
        //act
        Deencapsulation.invoke(mockMqtt, "connect");
        //assert
        baseConnectVerifications();
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Test(org.junit.Test)

Aggregations

Mqtt (com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt)29 Test (org.junit.Test)29 Message (com.microsoft.azure.sdk.iot.device.Message)4 MqttMessaging (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging)4 MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)3 IOException (java.io.IOException)2 InvalidParameterException (java.security.InvalidParameterException)2