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;
}
};
}
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);
}
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);
}
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;
}
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);
}
}
Aggregations