use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging in project azure-iot-sdk-java by Azure.
the class MqttMessagingTest method startThrowsIoExceptionIfSubscribeFails.
@Test(expected = IOException.class)
public void startThrowsIoExceptionIfSubscribeFails(@Mocked final Mqtt mockMqtt) throws IOException {
new StrictExpectations() {
{
Deencapsulation.invoke(mockMqtt, "connect");
Deencapsulation.invoke(mockMqtt, "subscribe", anyString);
result = mockIOException;
}
};
MqttMessaging testMqttMessaging = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
testMqttMessaging.start();
new Verifications() {
{
Deencapsulation.invoke(mockMqtt, "connect");
times = 1;
Deencapsulation.invoke(mockMqtt, "subscribe", anyString);
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 parsePayloadShallThrowIOExceptionIfTopicIsNotFound.
/*
**Tests_SRS_MqttMessaging_25_012: [**If the topic is non-null and received messagesqueue could not locate the payload then this method shall throw IOException**]**
*/
@Test(expected = IOException.class)
public void parsePayloadShallThrowIOExceptionIfTopicIsNotFound(@Mocked final Mqtt mockMqtt) throws IOException {
MqttMessaging testMqttMessaging = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
;
final String insertTopic_actual = "$iothub/twin/PATCH/properties/desired/#";
final String insertTopic_messaging = "devices/" + clientId + "/messages/devicebound/abc";
final byte[] insertMessage = { 0x61, 0x62, 0x63 };
ConcurrentSkipListMap<String, byte[]> testMap = new ConcurrentSkipListMap<String, byte[]>();
testMap.put(insertTopic_actual, insertMessage);
Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
byte[] retrieveMessage = Deencapsulation.invoke(testMqttMessaging, "parsePayload", insertTopic_messaging);
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging in project azure-iot-sdk-java by Azure.
the class MqttMessagingTest method stopCallsDisconnectAndRestartBase.
/*
**Tests_SRS_MqttMessaging_25_022: [**stop method shall be call disconnect to tear down a connection to IOT Hub with the given configuration.**]**
**Tests_SRS_MqttMessaging_25_023: [**stop method shall be call restartBaseMqtt to tear down a the base class even if disconnect fails.**]**
*/
@Test
public void stopCallsDisconnectAndRestartBase(@Mocked final Mqtt mockMqtt) throws IOException {
new NonStrictExpectations() {
{
Deencapsulation.invoke(mockMqtt, "disconnect");
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;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging in project azure-iot-sdk-java by Azure.
the class MqttTest method receiveSuccess.
/*
**Tests_SRS_Mqtt_25_021: [**This method shall call parseTopic to parse the topic from the recevived Messages queue corresponding to the messaging client's operation.**]**
*/
/*
**Tests_SRS_Mqtt_25_023: [**This method shall call parsePayload to get the message payload from the recevived Messages queue corresponding to the messaging client's operation.**]**
*/
/*
Tests_SRS_Mqtt_25_024: [**This method shall construct new Message with the bytes obtained from parsePayload and return the message.**]**
*/
@Test
public void receiveSuccess() throws IOException, MqttException {
//arrange
final byte[] payload = { 0x61, 0x62, 0x63 };
baseConstructorExpectations(true);
baseConnectExpectation();
new MockUp<MqttMessaging>() {
@Mock
String parseTopic() throws IOException {
return mockParseTopic;
}
@Mock
byte[] parsePayload(String topic) throws IOException {
return payload;
}
};
final Mqtt mockMqtt = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
try {
new NonStrictExpectations() {
{
mockMqttAsyncClient.isConnected();
result = true;
}
};
Deencapsulation.invoke(mockMqtt, "connect");
//act
Message receivedMessage = mockMqtt.receive();
//assert
byte[] actualPayload = receivedMessage.getBytes();
assertTrue(actualPayload.length == payload.length);
for (int i = 0; i < payload.length; i++) {
assertEquals(actualPayload[i], payload[i]);
}
} finally {
testCleanUp(mockMqtt);
}
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging in project azure-iot-sdk-java by Azure.
the class MqttTest method receiveThrowsIOExceptionWhenParsePayloadReturnsNull.
/*
**Tests_SRS_Mqtt_25_025: [**If the call to parsePayload returns null when topic is non-null then this method will throw IOException**]**
*/
@Test(expected = IOException.class)
public void receiveThrowsIOExceptionWhenParsePayloadReturnsNull() throws IOException, MqttException {
//arrange
final byte[] payload = { 0x61, 0x62, 0x63 };
baseConstructorExpectations(true);
new MockUp<MqttMessaging>() {
@Mock
String parseTopic() throws IOException {
return mockParseTopic;
}
@Mock
byte[] parsePayload(String topic) throws IOException {
return null;
}
};
final Mqtt mockMqtt = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
//act
try {
Message receivedMessage = mockMqtt.receive();
} finally {
testCleanUp(mockMqtt);
}
}
Aggregations