Search in sources :

Example 41 with TransportException

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

the class MqttMessagingTest method startThrowsIoExceptionIfConnectFails.

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

        {
            Deencapsulation.invoke(mockMqtt, "connect");
            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 = 0;
        }
    };
}
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 42 with TransportException

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

the class PahoExceptionTranslatorTest method onConnectionLostMapsInvalidClientIdException.

// Tests_SRS_PahoExceptionTranslator_34_142: [When deriving the TransportException from the provided MqttException, this function shall map REASON_CODE_INVALID_CLIENT_ID to MqttIdentifierRejectedException.]
@Test
public void onConnectionLostMapsInvalidClientIdException() {
    // arrange
    new NonStrictExpectations() {

        {
            mockedMqttException.getReasonCode();
            result = MqttException.REASON_CODE_INVALID_CLIENT_ID;
        }
    };
    // act
    Exception e = PahoExceptionTranslator.convertToMqttException(new MqttException(MqttException.REASON_CODE_INVALID_CLIENT_ID), "");
    // assert
    assertTrue(e instanceof MqttIdentifierRejectedException);
}
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 43 with TransportException

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

the class PahoExceptionTranslatorTest method onConnectionLostMapsNotAuthorizedException.

// Tests_SRS_PahoExceptionTranslator_34_145: [When deriving the TransportException from the provided MqttException, this function shall map REASON_CODE_NOT_AUTHORIZED to MqttUnauthorizedException.]
@Test
public void onConnectionLostMapsNotAuthorizedException() throws IOException, TransportException {
    // arrange
    new NonStrictExpectations() {

        {
            mockedMqttException.getReasonCode();
            result = MqttException.REASON_CODE_NOT_AUTHORIZED;
        }
    };
    // act
    Exception e = PahoExceptionTranslator.convertToMqttException(new MqttException(MqttException.REASON_CODE_NOT_AUTHORIZED), "");
    // assert
    assertTrue(e instanceof MqttUnauthorizedException);
}
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 44 with TransportException

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

the class MqttDeviceMethod method throwMethodsTransportException.

private void throwMethodsTransportException(String message) throws TransportException {
    TransportException transportException = new TransportException(message);
    transportException.setIotHubService(TransportException.IotHubService.METHODS);
    throw transportException;
}
Also used : TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException)

Example 45 with TransportException

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

the class MqttIotHubConnection method open.

/**
 * Establishes a connection for the device and IoT Hub given in the client
 * configuration. If the connection is already open, the function shall do
 * nothing.
 *
 * @throws TransportException if a connection could not to be established.
 */
public void open() throws TransportException {
    synchronized (this.mqttConnectionStateLock) {
        this.connectionId = UUID.randomUUID().toString();
        this.deviceMessaging.setConnectionId(this.connectionId);
        this.deviceTwin.setConnectionId(this.connectionId);
        this.deviceMethod.setConnectionId(this.connectionId);
        if (this.state == IotHubConnectionStatus.CONNECTED) {
            return;
        }
        log.debug("Opening MQTT connection...");
        if (this.config.getSasTokenAuthentication() != null) {
            try {
                log.trace("Setting password for MQTT connection since it is a SAS token authenticated connection");
                this.deviceMessaging.updatePassword(this.config.getSasTokenAuthentication().getSasToken());
            } catch (IOException e) {
                throw new TransportException("Failed to open the MQTT connection because a SAS token could not be retrieved", e);
            }
        }
        // MqttAsyncClient's are unusable after they have been closed. This logic creates a new client
        // each time an open is called
        MqttAsyncClient mqttAsyncClient = buildMqttAsyncClient(this.serverUri, this.clientId);
        mqttAsyncClient.setCallback(this.deviceMessaging);
        this.deviceMessaging.setMqttAsyncClient(mqttAsyncClient);
        this.deviceTwin.setMqttAsyncClient(mqttAsyncClient);
        this.deviceMethod.setMqttAsyncClient(mqttAsyncClient);
        this.deviceMessaging.start();
        this.state = IotHubConnectionStatus.CONNECTED;
        log.debug("MQTT connection opened successfully");
        this.listener.onConnectionEstablished(this.connectionId);
    }
}
Also used : IOException(java.io.IOException) TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException) MqttAsyncClient(org.eclipse.paho.client.mqttv3.MqttAsyncClient)

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