use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod in project azure-iot-sdk-java by Azure.
the class MqttDeviceMethodTest method receiveReturnsEmptyPayLoadIfNullPayloadParsed.
@Test
public void receiveReturnsEmptyPayLoadIfNullPayloadParsed() throws IOException {
//arrange
String topic = "$iothub/methods/POST/testMethod/?$rid=10";
byte[] actualPayload = "".getBytes();
ConcurrentSkipListMap<String, byte[]> testAllReceivedMessages = new ConcurrentSkipListMap<>();
testAllReceivedMessages.put(topic, actualPayload);
Deencapsulation.setField(mockedMqtt, "allReceivedMessages", testAllReceivedMessages);
MqttDeviceMethod testMethod = new MqttDeviceMethod();
testMethod.start();
//act
Message testMessage = testMethod.receive();
DeviceMethodMessage testDMMessage = (DeviceMethodMessage) testMessage;
//assert
assertNotNull(testMessage);
assertTrue(testMessage.getBytes().length == 0);
assertTrue(testMessage.getMessageType().equals(MessageType.DeviceMethods));
assertTrue(testDMMessage.getRequestId().equals(String.valueOf(10)));
assertTrue(testDMMessage.getMethodName().equals("testMethod"));
assertTrue(testDMMessage.getDeviceOperationType().equals(DEVICE_OPERATION_METHOD_RECEIVE_REQUEST));
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod in project azure-iot-sdk-java by Azure.
the class MqttDeviceMethodTest method receiveReturnsNullMessageIfTopicWasNotPost.
/*
Tests_SRS_MqttDeviceMethod_25_027: [**This method shall parse topic to look for Post topic ($iothub/methods/POST/) and throw unsupportedoperation exception other wise.**]**
*/
@Test(expected = UnsupportedOperationException.class)
public void receiveReturnsNullMessageIfTopicWasNotPost() throws IOException {
//arrange
String topic = "$iothub/methods/Not_POST/testMethod/?$rid=10";
byte[] actualPayload = "TestPayload".getBytes();
ConcurrentSkipListMap<String, byte[]> testAllReceivedMessages = new ConcurrentSkipListMap<>();
testAllReceivedMessages.put(topic, actualPayload);
Deencapsulation.setField(mockedMqtt, "allReceivedMessages", testAllReceivedMessages);
MqttDeviceMethod testMethod = new MqttDeviceMethod();
testMethod.start();
//act
Message testMessage = testMethod.receive();
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod in project azure-iot-sdk-java by Azure.
the class MqttDeviceMethodTest method sendThrowsOnMismatchedRequestType.
/*
Tests_SRS_MqttDeviceMethod_25_019: [**send method shall throw an IoException if the getDeviceOperationType() is not of type DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST or DEVICE_OPERATION_METHOD_SEND_RESPONSE .**]**
*/
@Test(expected = IOException.class)
public void sendThrowsOnMismatchedRequestType() throws IOException {
final byte[] actualPayload = "TestMessage".getBytes();
final DeviceMethodMessage testMessage = new DeviceMethodMessage(actualPayload);
testMessage.setDeviceOperationType(DEVICE_OPERATION_METHOD_SEND_RESPONSE);
testMessage.setRequestId("ReqId");
testMessage.setStatus("testStatus");
MqttDeviceMethod testMethod = new MqttDeviceMethod();
Map<String, DeviceOperations> testRequestMap = new HashMap<>();
testRequestMap.put("ReqId", DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST);
Deencapsulation.setField(testMethod, "requestMap", testRequestMap);
testMethod.start();
//act
testMethod.send(testMessage);
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod 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.MqttDeviceMethod in project azure-iot-sdk-java by Azure.
the class MqttIotHubConnectionTest method closeClosesMqttConnection.
// Tests_SRS_MQTTIOTHUBCONNECTION_15_005: [The function shall close the MQTT connection.]
@Test
public void closeClosesMqttConnection() throws IOException {
baseExpectations();
openExpectations();
MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
connection.open();
connection.close();
State expectedState = State.CLOSED;
State actualState = Deencapsulation.getField(connection, "state");
assertEquals(expectedState, actualState);
MqttDeviceMethod actualDeviceMethods = Deencapsulation.getField(connection, "deviceMethod");
assertNull(actualDeviceMethods);
MqttDeviceTwin actualDeviceTwin = Deencapsulation.getField(connection, "deviceTwin");
assertNull(actualDeviceTwin);
MqttDeviceTwin actualDeviceMessaging = Deencapsulation.getField(connection, "deviceMessaging");
assertNull(actualDeviceMessaging);
new Verifications() {
{
mockDeviceMessaging.stop();
times = 1;
mockDeviceMethods.stop();
times = 1;
mockDeviceTwin.stop();
times = 1;
}
};
}
Aggregations