Search in sources :

Example 6 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 receiveSuccess.

/*
    **Tests_SRS_Mqtt_25_021: [**This method shall call parseTopic to parse the topic from the recevived Messages queue corresponding to the messaging client's operation.**]**
     */
/*
    **Tests_SRS_Mqtt_25_023: [**This method shall call parsePayload to get the message payload from the recevived Messages queue corresponding to the messaging client's operation.**]**
     */
/*
    Tests_SRS_Mqtt_25_024: [**This method shall construct new Message with the bytes obtained from parsePayload and return the message.**]**
     */
@Test
public void receiveSuccess() throws IOException, MqttException {
    //arrange
    final byte[] payload = { 0x61, 0x62, 0x63 };
    baseConstructorExpectations(true);
    baseConnectExpectation();
    new MockUp<MqttMessaging>() {

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

        @Mock
        byte[] parsePayload(String topic) throws IOException {
            return payload;
        }
    };
    final Mqtt mockMqtt = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
    try {
        new NonStrictExpectations() {

            {
                mockMqttAsyncClient.isConnected();
                result = true;
            }
        };
        Deencapsulation.invoke(mockMqtt, "connect");
        //act
        Message receivedMessage = mockMqtt.receive();
        //assert
        byte[] actualPayload = receivedMessage.getBytes();
        assertTrue(actualPayload.length == payload.length);
        for (int i = 0; i < payload.length; i++) {
            assertEquals(actualPayload[i], payload[i]);
        }
    } 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 7 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 publishThrowsExceptionWhenPublishTopicIsNull.

/*
    **Tests_SRS_Mqtt_25_013: [**If the either publishTopic or payload is null or empty, the function shall throw an IOException.**]**
     */
@Test(expected = IOException.class)
public void publishThrowsExceptionWhenPublishTopicIsNull() throws IOException, MqttException {
    //arrange
    Mqtt mockMqtt = null;
    try {
        baseConstructorExpectations(true);
        final byte[] payload = { 0x61, 0x62, 0x63 };
        new NonStrictExpectations() {

            {
                mockMqttAsyncClient.isConnected();
                result = true;
            }
        };
        mockMqtt = instantiateMqtt(true);
        Deencapsulation.invoke(mockMqtt, "connect");
        //act
        Deencapsulation.invoke(mockMqtt, "publish", String.class, payload);
        //assert
        new Verifications() {

            {
                mockMqttAsyncClient.isConnected();
                minTimes = 1;
            }
        };
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Test(org.junit.Test)

Example 8 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 subscribeThrowsIOExceptionWhenMqttAsyncThrows.

/*
    **Tests_SRS_Mqtt_25_048: [**If the Mqtt Client Async throws MqttException for any reason, the function shall throw an IOException with the message.**]**
     */
@Test(expected = IOException.class)
public void subscribeThrowsIOExceptionWhenMqttAsyncThrows() throws IOException, MqttException {
    //arrange
    Mqtt mockMqtt = null;
    try {
        baseConstructorExpectations(true);
        baseConnectExpectation();
        new NonStrictExpectations() {

            {
                mockMqttAsyncClient.isConnected();
                result = true;
                mockMqttAsyncClient.subscribe(mockParseTopic, anyInt);
                result = mockMqttException;
            }
        };
        mockMqtt = instantiateMqtt(true);
        Deencapsulation.invoke(mockMqtt, "connect");
        //act
        Deencapsulation.invoke(mockMqtt, "subscribe", mockParseTopic);
        new Verifications() {

            {
                mockMqttAsyncClient.isConnected();
                minTimes = 1;
                mockMqttAsyncClient.subscribe(mockParseTopic, anyInt);
                times = 1;
            }
        };
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Test(org.junit.Test)

Example 9 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 messageArrivedAddsToQueue.

/*
    **Tests_SRS_Mqtt_25_030: [**The payload of the message and the topic is added to the received messages queue .**]**
     */
@Test
public void messageArrivedAddsToQueue() throws IOException, MqttException {
    //arrange
    Mqtt mockMqtt = null;
    try {
        final byte[] actualPayload = { 0x61, 0x62, 0x63 };
        baseConstructorExpectations(true);
        baseConnectExpectation();
        new NonStrictExpectations() {

            {
                mockMqttMessage.getPayload();
                result = actualPayload;
            }
        };
        mockMqtt = instantiateMqtt(true);
        Deencapsulation.invoke(mockMqtt, "connect");
        //act
        mockMqtt.messageArrived(mockParseTopic, new MqttMessage(actualPayload));
        //assert
        ConcurrentSkipListMap<String, byte[]> actualMap = Deencapsulation.getField(mockMqtt, "allReceivedMessages");
        assertTrue(actualMap.containsKey(mockParseTopic));
        byte[] receivedPayload = actualMap.get(mockParseTopic);
        assertTrue(actualPayload.length == receivedPayload.length);
        for (int i = 0; i < actualPayload.length; i++) {
            assertEquals(actualPayload[i], receivedPayload[i]);
        }
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Test(org.junit.Test)

Example 10 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 subscribeFailsWhenNotConnected.

/*
    **Tests_SRS_Mqtt_25_015: [**If the MQTT connection is closed, the function shall throw an IOexception with message.**]**
     */
@Test(expected = IOException.class)
public void subscribeFailsWhenNotConnected() throws IOException, MqttException {
    //arrange
    Mqtt mockMqtt = null;
    try {
        baseConstructorExpectations(true);
        new NonStrictExpectations() {

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

            {
                mockMqttAsyncClient.isConnected();
                minTimes = 1;
            }
        };
    } 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