use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection in project azure-iot-sdk-java by Azure.
the class MqttTransportTest method openOpensMqttConnection.
// Tests_SRS_MQTTTRANSPORT_15_003: [The function shall establish an MQTT connection
// with IoT Hub given in the configuration.]
@Test
public void openOpensMqttConnection() throws IOException, NoSuchFieldException, IllegalAccessException {
new NonStrictExpectations() {
{
new MqttIotHubConnection(mockConfig);
result = mockConnection;
}
};
MqttTransport transport = new MqttTransport(mockConfig);
transport.open();
final MqttIotHubConnection expectedConnection = mockConnection;
new Verifications() {
{
expectedConnection.open();
}
};
Field sendMessagesLock = transport.getClass().getDeclaredField("sendMessagesLock");
sendMessagesLock.setAccessible(true);
assertNotNull(sendMessagesLock.get(transport));
Field handleMessageLock = transport.getClass().getDeclaredField("handleMessageLock");
handleMessageLock.setAccessible(true);
assertNotNull(handleMessageLock.get(transport));
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection in project azure-iot-sdk-java by Azure.
the class MqttTransportTest method closeClosesMqttConnection.
// Tests_SRS_MQTTTRANSPORT_15_005: [The function shall close the MQTT connection
// with the IoT Hub given in the configuration.]
@Test
public void closeClosesMqttConnection() throws IOException {
MqttTransport transport = new MqttTransport(mockConfig);
transport.open();
transport.close();
final MqttIotHubConnection expectedConnection = mockConnection;
new Verifications() {
{
expectedConnection.close();
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection in project azure-iot-sdk-java by Azure.
the class MqttTransportTest method invokeCallbacksDropsFailedCallback.
// Tests_SRS_MQTTTRANSPORT_15_015: [If an exception is thrown during the callback,
// the function shall drop the callback from the queue.]
@Test
public void invokeCallbacksDropsFailedCallback(@Mocked final Message mockMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final IotHubCallbackPacket mockCallbackPacket) throws IOException {
final Map<String, Object> context = new HashMap<>();
new NonStrictExpectations() {
{
new MqttIotHubConnection(mockConfig);
result = mockConnection;
mockCallbackPacket.getStatus();
result = IotHubStatusCode.OK_EMPTY;
mockCallbackPacket.getCallback();
result = mockCallback;
mockCallbackPacket.getContext();
result = context;
mockCallback.execute(IotHubStatusCode.OK_EMPTY, context);
result = new IllegalStateException();
result = null;
}
};
MqttTransport transport = new MqttTransport(mockConfig);
transport.open();
transport.addMessage(mockMsg, mockCallback, context);
transport.sendMessages();
try {
transport.invokeCallbacks();
throw new AssertionFailedError();
} catch (IllegalStateException e) {
transport.invokeCallbacks();
}
final IotHubEventCallback expectedCallback = mockCallback;
final Map<String, Object> expectedContext = context;
new VerificationsInOrder() {
{
expectedCallback.execute(IotHubStatusCode.OK_EMPTY, expectedContext);
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection in project azure-iot-sdk-java by Azure.
the class MqttIotHubConnectionTest method sendEventReturnsErrorIfMessageNotReceived.
// Tests_SRS_MQTTIOTHUBCONNECTION_15_012: [If the message was not successfully received by the service,
// the function shall return status code ERROR.]
@Test
public void sendEventReturnsErrorIfMessageNotReceived(@Mocked final Message mockMsg) throws IOException {
baseExpectations();
final byte[] msgBody = { 0x61, 0x62, 0x63 };
new NonStrictExpectations() {
{
mockMsg.getBytes();
result = msgBody;
mockDeviceMessaging.send(mockMsg);
result = new IOException(anyString);
}
};
MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
connection.open();
IotHubStatusCode actualStatus = connection.sendEvent(mockMsg);
IotHubStatusCode expectedStatus = IotHubStatusCode.ERROR;
assertEquals(expectedStatus, actualStatus);
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection in project azure-iot-sdk-java by Azure.
the class MqttIotHubConnectionTest method openEstablishesConnectionUsingCorrectConfig.
// Tests_SRS_MQTTIOTHUBCONNECTION_15_004: [The function shall establish an MQTT connection with an IoT Hub
// using the provided host name, user name, device ID, and sas token.]
@Test
public void openEstablishesConnectionUsingCorrectConfig() throws IOException {
baseExpectations();
openExpectations();
MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
connection.open();
final String actualIotHubUserName = Deencapsulation.getField(connection, "iotHubUserName");
String clientIdentifier = "DeviceClientType=" + URLEncoder.encode(TransportUtils.JAVA_DEVICE_CLIENT_IDENTIFIER + TransportUtils.CLIENT_VERSION, "UTF-8");
assertEquals(iotHubHostName + "/" + deviceId + "/" + API_VERSION + "/" + clientIdentifier, actualIotHubUserName);
String expectedSasToken = mockToken.toString();
String actualUserPassword = Deencapsulation.getField(connection, "iotHubUserPassword");
assertEquals(expectedSasToken, actualUserPassword);
State expectedState = State.OPEN;
State actualState = Deencapsulation.getField(connection, "state");
assertEquals(expectedState, actualState);
new Verifications() {
{
new MqttDeviceMethod();
times = 1;
new MqttMessaging(sslPrefix + iotHubHostName + sslPortSuffix, deviceId, anyString, anyString, mockIotHubSSLContext);
mockDeviceMessaging.start();
times = 1;
new MqttDeviceTwin();
times = 1;
}
};
}
Aggregations