Search in sources :

Example 1 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 receiveThrowsExceptionWhenConfigurationIsNotSet.

@Test(expected = InvalidParameterException.class)
public void receiveThrowsExceptionWhenConfigurationIsNotSet() throws IOException, MqttException {
    //arrange
    final byte[] payload = { 0x61, 0x62, 0x63 };
    baseConstructorExpectations(false);
    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);
    Deencapsulation.setField(mockMqtt, "info", null);
    //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 2 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 constructorWithParametersIfCalledMultipleTimesDoesntReinitialize.

/*
    ** Tests_SRS_Mqtt_25_004: [**If an instance of the inner class MqttConnectionInfo is already created than it shall return doing nothing.**]**
     */
@Test
public void constructorWithParametersIfCalledMultipleTimesDoesntReinitialize() throws IOException, MqttException {
    //arrange
    baseConstructorExpectations(true);
    baseConstructorExpectations(true);
    //act
    Mqtt mockMqtt1 = instantiateMqtt(true);
    //assert
    Object actualInfoInstance1 = Deencapsulation.getField(mockMqtt1, "info");
    ConcurrentSkipListMap<String, byte[]> actualMap1 = Deencapsulation.getField(mockMqtt1, "allReceivedMessages");
    Mqtt mockMqtt2 = instantiateMqtt(false);
    Object actualInfoInstance2 = Deencapsulation.getField(mockMqtt2, "info");
    ConcurrentSkipListMap<String, byte[]> actualMap2 = Deencapsulation.getField(mockMqtt2, "allReceivedMessages");
    Object actualLock1 = Deencapsulation.getField(mockMqtt1, "MQTT_LOCK");
    Object actualLock2 = Deencapsulation.getField(mockMqtt2, "MQTT_LOCK");
    assertEquals(actualInfoInstance1, actualInfoInstance2);
    assertEquals(actualMap1, actualMap2);
    assertEquals(actualLock1, actualLock2);
    baseConstructorVerifications(true);
    baseConstructorVerifications(false);
    mockMqtt1.restartBaseMqtt();
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Test(org.junit.Test)

Example 3 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 subscribeThrowsExceptionWhenTopicIsNull.

/*
    **Tests_SRS_Mqtt_25_016: [**If the subscribeTopic is null or empty, the function shall throw an InvalidParameter Exception.**]**
     */
@Test(expected = InvalidParameterException.class)
public void subscribeThrowsExceptionWhenTopicIsNull() throws IOException, MqttException {
    //arrange
    Mqtt mockMqtt = null;
    try {
        baseConstructorExpectations(true);
        mockMqtt = instantiateMqtt(true);
        Deencapsulation.invoke(mockMqtt, "connect");
        //act
        Deencapsulation.invoke(mockMqtt, "subscribe", String.class);
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Test(org.junit.Test)

Example 4 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 disconnectSucceeds.

/*
    **Tests_SRS_Mqtt_25_009: [**The function shall close the MQTT connection.**]**
     */
@Test
public void disconnectSucceeds() throws IOException, MqttException {
    //arrange
    Mqtt mockMqtt = null;
    try {
        baseConstructorExpectations(true);
        baseConnectExpectation();
        baseDisconnectExpectations();
        mockMqtt = instantiateMqtt(true);
        Deencapsulation.invoke(mockMqtt, "connect");
        //act
        Deencapsulation.invoke(mockMqtt, "disconnect");
        //assert
        new Verifications() {

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

Example 5 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 publishFailsWhenNotConnected.

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

            {
                mockMqttAsyncClient.isConnected();
                result = false;
            }
        };
        mockMqtt = instantiateMqtt(true);
        //act
        Deencapsulation.invoke(mockMqtt, "publish", mockParseTopic, payload);
    } 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