Search in sources :

Example 21 with TransportException

use of com.microsoft.azure.sdk.iot.device.exceptions.TransportException in project azure-iot-sdk-java by Azure.

the class MqttMessagingTest method stopIfDisconnectFailsThrowsIOException.

@Test(expected = TransportException.class)
public void stopIfDisconnectFailsThrowsIOException(@Mocked final Mqtt mockMqtt) throws TransportException {
    new StrictExpectations() {

        {
            Deencapsulation.invoke(mockMqtt, "connect");
            Deencapsulation.invoke(mockMqtt, "subscribe", anyString);
            Deencapsulation.invoke(mockMqtt, "disconnect");
            result = new TransportException();
        }
    };
    MqttMessaging testMqttMessaging = new MqttMessaging(CLIENT_ID, null, "", false, mockConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
    testMqttMessaging.start();
    testMqttMessaging.stop();
    new Verifications() {

        {
            Deencapsulation.invoke(mockMqtt, "disconnect");
            times = 1;
        }
    };
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 22 with TransportException

use of com.microsoft.azure.sdk.iot.device.exceptions.TransportException in project azure-iot-sdk-java by Azure.

the class MqttMessagingTest method startThrowsIoExceptionIfSubscribeFails.

@Test(expected = TransportException.class)
public void startThrowsIoExceptionIfSubscribeFails(@Mocked final Mqtt mockMqtt) throws TransportException {
    new StrictExpectations() {

        {
            Deencapsulation.invoke(mockMqtt, "connect");
            Deencapsulation.invoke(mockMqtt, "subscribe", anyString);
            result = new TransportException();
        }
    };
    MqttMessaging testMqttMessaging = new MqttMessaging(CLIENT_ID, null, "", false, mockConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
    testMqttMessaging.start();
    new Verifications() {

        {
            Deencapsulation.invoke(mockMqtt, "connect");
            times = 1;
            Deencapsulation.invoke(mockMqtt, "subscribe", anyString);
            times = 1;
        }
    };
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 23 with TransportException

use of com.microsoft.azure.sdk.iot.device.exceptions.TransportException in project azure-iot-sdk-java by Azure.

the class HttpsConnectionTest method readInputClosesStreamIfExceptionWhileReading.

// Tests_SRS_HTTPSCONNECTION_11_019: [The function shall close the input stream after it has been completely read.]
@Test
public void readInputClosesStreamIfExceptionWhileReading(@Mocked final InputStream mockIs) throws IOException, TransportException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getInputStream();
            result = mockIs;
            mockIs.read();
            result = new TransportException("This is a test exception");
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.connect();
    // act
    try {
        byte[] testResponse = conn.readInput();
    } catch (TransportException e) {
    // expected exception, but not testing for it, so it can be ignored
    }
    new Verifications() {

        {
            mockIs.close();
            times = 1;
        }
    };
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException) Test(org.junit.Test)

Example 24 with TransportException

use of com.microsoft.azure.sdk.iot.device.exceptions.TransportException in project azure-iot-sdk-java by Azure.

the class HttpsIotHubConnectionTest method receiveMessageThrowsProtocolConnectionExceptionIfRequestFails.

// Tests_SRS_HTTPSIOTHUBCONNECTION_11_023: [If the IoT Hub could not be reached, the function shall throw a TransportException.]
@Test(expected = TransportException.class)
public void receiveMessageThrowsProtocolConnectionExceptionIfRequestFails(@Mocked final IotHubMessageUri mockUri) throws TransportException {
    new NonStrictExpectations() {

        {
            mockRequest.send();
            result = new TransportException();
        }
    };
    HttpsIotHubConnection conn = new HttpsIotHubConnection(mockConfig);
    conn.receiveMessage();
}
Also used : TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException) Test(org.junit.Test)

Example 25 with TransportException

use of com.microsoft.azure.sdk.iot.device.exceptions.TransportException in project azure-iot-sdk-java by Azure.

the class HttpsConnectionTest method readInputFailsIfCannotAccessInputStreamAndIsNoRouteToHost.

// Tests_SRS_HTTPSCONNECTION_34_027: [If an UnknownHostException or a NoRouteToHostException is encountered, the thrown TransportException shall be retryable.]
@Test
public void readInputFailsIfCannotAccessInputStreamAndIsNoRouteToHost() throws IOException, TransportException {
    boolean exceptionCaught = false;
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getInputStream();
            result = new NoRouteToHostException();
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.connect();
    try {
        conn.readInput();
    } catch (TransportException e) {
        exceptionCaught = true;
        if (!e.isRetryable()) {
            fail("NoRouteToHostException should yield retriable TransportException");
        }
    }
    assertTrue(exceptionCaught);
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException) Test(org.junit.Test)

Aggregations

TransportException (com.microsoft.azure.sdk.iot.device.exceptions.TransportException)46 Test (org.junit.Test)18 IOException (java.io.IOException)14 ProtocolException (com.microsoft.azure.sdk.iot.device.exceptions.ProtocolException)8 MqttException (org.eclipse.paho.client.mqttv3.MqttException)7 CountDownLatch (java.util.concurrent.CountDownLatch)4 Expectations (mockit.Expectations)4 NonStrictExpectations (mockit.NonStrictExpectations)4 Message (com.microsoft.azure.sdk.iot.device.Message)3 HttpsConnection (com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection)3 HttpsMethod (com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod)3 MqttMessaging (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging)3 Pair (org.apache.commons.lang3.tuple.Pair)3 IotHubConnectionString (com.microsoft.azure.sdk.iot.device.IotHubConnectionString)2 ModuleClientException (com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException)2 URISyntaxException (java.net.URISyntaxException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 IotHubSSLContext (com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext)1 IotHubAuthenticationProvider (com.microsoft.azure.sdk.iot.device.auth.IotHubAuthenticationProvider)1 SignatureProvider (com.microsoft.azure.sdk.iot.device.auth.SignatureProvider)1