use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging 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;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging in project azure-iot-sdk-java by Azure.
the class MqttMessagingTest method sendShallMessageToLowerLayer.
/*
**Tests_SRS_MqttMessaging_25_024: [**send method shall publish a message to the IOT Hub on the publish topic by calling method publish().**]**
*/
@Test
public void sendShallMessageToLowerLayer(@Mocked final Mqtt mockMqtt) throws IOException {
final byte[] messageBody = { 0x61, 0x62, 0x63 };
new NonStrictExpectations() {
{
mockMessage.getBytes();
result = messageBody;
Deencapsulation.invoke(mockMqtt, "publish", anyString, messageBody);
}
};
MqttMessaging testMqttMessaging = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
;
testMqttMessaging.send(mockMessage);
new Verifications() {
{
mockMessage.getBytes();
times = 2;
Deencapsulation.invoke(mockMqtt, "publish", anyString, messageBody);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging in project azure-iot-sdk-java by Azure.
the class MqttMessagingTest method sendShallThrowIOExceptionIfMessageIsNull.
/*
**Tests_SRS_MqttMessaging_25_025: [**send method shall throw an exception if the message is null.**]**
*/
@Test(expected = IOException.class)
public void sendShallThrowIOExceptionIfMessageIsNull(@Mocked final Mqtt mockMqtt) throws IOException {
MqttMessaging testMqttMessaging = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
;
testMqttMessaging.send(null);
new Verifications() {
{
mockMessage.getBytes();
times = 0;
Deencapsulation.invoke(mockMqtt, "publish", mockParseTopic, new byte[1]);
times = 0;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging in project azure-iot-sdk-java by Azure.
the class MqttMessagingTest method sendShallMessageWithPropsAndMessageIdToLowerLayer.
/*
**Tests_SRS_MqttMessaging_21_027: [**send method shall append the messageid to publishTopic before publishing using the key name `$.mid`.**]**
*/
@Test
public void sendShallMessageWithPropsAndMessageIdToLowerLayer(@Mocked final Mqtt mockMqtt) throws IOException {
final byte[] messageBody = { 0x61, 0x62, 0x63 };
final String propertyName = "key";
final String propertyValue = "value";
final MessageProperty[] messageProperties = new MessageProperty[] { new MessageProperty(propertyName, propertyValue) };
final String messageidValue = "test-message-id";
new NonStrictExpectations() {
{
mockMessage.getBytes();
result = messageBody;
mockMessage.getProperties();
result = messageProperties;
mockMessage.getMessageId();
result = messageidValue;
Deencapsulation.invoke(mockMqtt, "publish", anyString, messageBody);
}
};
MqttMessaging testMqttMessaging = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
;
testMqttMessaging.send(mockMessage);
final String publishTopicWithProperties = String.format("devices/%s/messages/events/%s=%s&$.mid=%s", clientId, propertyName, propertyValue, messageidValue);
new Verifications() {
{
mockMessage.getBytes();
times = 2;
mockMessage.getProperties();
Deencapsulation.invoke(mockMqtt, "publish", publishTopicWithProperties, messageBody);
times = 1;
mockMessage.getMessageId();
times = 2;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging in project azure-iot-sdk-java by Azure.
the class MqttMessagingTest method stopCallsRestartBaseEvenIfDisconnectFailsAndThrowsIOException.
@Test(expected = IOException.class)
public void stopCallsRestartBaseEvenIfDisconnectFailsAndThrowsIOException(@Mocked final Mqtt mockMqtt) throws IOException {
new StrictExpectations() {
{
Deencapsulation.invoke(mockMqtt, "connect");
Deencapsulation.invoke(mockMqtt, "subscribe", anyString);
Deencapsulation.invoke(mockMqtt, "disconnect");
result = mockIOException;
mockMqtt.restartBaseMqtt();
}
};
MqttMessaging testMqttMessaging = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
testMqttMessaging.start();
testMqttMessaging.stop();
new Verifications() {
{
Deencapsulation.invoke(mockMqtt, "disconnect");
times = 1;
mockMqtt.restartBaseMqtt();
times = 1;
}
};
}
Aggregations