Search in sources :

Example 11 with TransportException

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

the class AmqpsIotHubConnectionTest method openClearsLocalStateIfOpenInterrupted.

@Test
public void openClearsLocalStateIfOpenInterrupted() throws TransportException, InterruptedException {
    // arrange
    baseExpectations();
    final CountDownLatch workerLinkLatch = new CountDownLatch(0);
    final AmqpsIotHubConnection connection = new AmqpsIotHubConnection(mockConfig, "");
    connection.setListener(mockedIotHubListener);
    new Expectations() {

        {
            new CountDownLatch(anyInt);
            result = workerLinkLatch;
            workerLinkLatch.await(anyLong, TimeUnit.SECONDS);
            // simulates getting interrupted while opening
            result = new InterruptedException("test interrupted exception");
        }
    };
    // act
    try {
        connection.open();
        fail("expected an exception to be thrown");
    } catch (TransportException e) {
    // expected exception
    }
    Map<String, AmqpsSessionHandler> sessionHandlers = Deencapsulation.getField(connection, "sessionHandlers");
    Queue<AmqpsSasTokenRenewalHandler> sasTokenRenewalHandlers = Deencapsulation.getField(connection, "sasTokenRenewalHandlers");
    assertTrue(sessionHandlers.isEmpty());
    assertTrue(sasTokenRenewalHandlers.isEmpty());
}
Also used : NonStrictExpectations(mockit.NonStrictExpectations) Expectations(mockit.Expectations) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) CountDownLatch(java.util.concurrent.CountDownLatch) TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException) Test(org.junit.Test)

Example 12 with TransportException

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

the class PahoExceptionTranslatorTest method onConnectionLostMapsFailedAuthenticationException.

// Tests_SRS_PahoExceptionTranslator_34_144: [When deriving the TransportException from the provided MqttException, this function shall map REASON_CODE_FAILED_AUTHENTICATION to MqttBadUsernameOrPasswordException.]
@Test
public void onConnectionLostMapsFailedAuthenticationException() throws IOException, TransportException {
    // arrange
    new NonStrictExpectations() {

        {
            mockedMqttException.getReasonCode();
            result = MqttException.REASON_CODE_FAILED_AUTHENTICATION;
        }
    };
    // act
    Exception e = PahoExceptionTranslator.convertToMqttException(new MqttException(MqttException.REASON_CODE_FAILED_AUTHENTICATION), "");
    // assert
    assertTrue(e instanceof MqttBadUsernameOrPasswordException);
}
Also used : MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttException(org.eclipse.paho.client.mqttv3.MqttException) ProtocolException(com.microsoft.azure.sdk.iot.device.exceptions.ProtocolException) IOException(java.io.IOException) TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException) Test(org.junit.Test)

Example 13 with TransportException

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

the class PahoExceptionTranslatorTest method onConnectionLostMapsUnexpectedConnectCodeException.

// Tests_SRS_PahoExceptionTranslator_34_147: [When deriving the TransportException from the provided MqttException, this function shall map any connect codes between 6 and 255 inclusive to MqttUnexpectedErrorException.]
@Test
public void onConnectionLostMapsUnexpectedConnectCodeException() throws IOException, TransportException {
    // arrange
    new NonStrictExpectations() {

        {
            mockedMqttException.getReasonCode();
            result = MqttException.REASON_CODE_UNEXPECTED_ERROR;
        }
    };
    // act
    Exception e = PahoExceptionTranslator.convertToMqttException(new MqttException(MqttException.REASON_CODE_UNEXPECTED_ERROR), "");
    // assert
    assertTrue(e instanceof MqttUnexpectedErrorException);
}
Also used : MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttException(org.eclipse.paho.client.mqttv3.MqttException) ProtocolException(com.microsoft.azure.sdk.iot.device.exceptions.ProtocolException) IOException(java.io.IOException) TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException) Test(org.junit.Test)

Example 14 with TransportException

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

the class PahoExceptionTranslatorTest method onConnectionLostMapsInvalidProtocolVersionException.

// Tests_SRS_PahoExceptionTranslator_34_141: [When deriving the TransportException from the provided MqttException, this function shall map REASON_CODE_INVALID_PROTOCOL_VERSION to MqttRejectedProtocolVersionException.]
@Test
public void onConnectionLostMapsInvalidProtocolVersionException() {
    // arrange
    new NonStrictExpectations() {

        {
            mockedMqttException.getReasonCode();
            result = MqttException.REASON_CODE_INVALID_PROTOCOL_VERSION;
        }
    };
    // act
    Exception e = PahoExceptionTranslator.convertToMqttException(new MqttException(MqttException.REASON_CODE_INVALID_PROTOCOL_VERSION), "");
    // assert
    assertTrue(e instanceof MqttRejectedProtocolVersionException);
}
Also used : MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttException(org.eclipse.paho.client.mqttv3.MqttException) ProtocolException(com.microsoft.azure.sdk.iot.device.exceptions.ProtocolException) IOException(java.io.IOException) TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException) Test(org.junit.Test)

Example 15 with TransportException

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

the class PahoExceptionTranslatorTest method onConnectionLostMapsUnknownPahoException.

// Tests_SRS_PahoExceptionTranslator_34_148: [When deriving the TransportException from the provided MqttException, this function shall map all other MqttExceptions to ProtocolException.]
@Test
public void onConnectionLostMapsUnknownPahoException() throws IOException, TransportException {
    // arrange
    new NonStrictExpectations() {

        {
            mockedMqttException.getReasonCode();
            result = MqttException.REASON_CODE_SSL_CONFIG_ERROR;
        }
    };
    // act
    Exception e = PahoExceptionTranslator.convertToMqttException(new MqttException(MqttException.REASON_CODE_SSL_CONFIG_ERROR), "");
    // assert
    assertTrue(e instanceof ProtocolException);
}
Also used : ProtocolException(com.microsoft.azure.sdk.iot.device.exceptions.ProtocolException) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttException(org.eclipse.paho.client.mqttv3.MqttException) ProtocolException(com.microsoft.azure.sdk.iot.device.exceptions.ProtocolException) IOException(java.io.IOException) 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