use of com.microsoft.azure.sdk.iot.device.Message in project azure-iot-sdk-java by Azure.
the class MessageTest method getBodyAsStringReturnsUtf8Body.
// Tests_SRS_MESSAGE_11_022: [The function shall return the message body, encoded using charset UTF-8.]
@Test
public void getBodyAsStringReturnsUtf8Body() {
final byte[] body = { 0x61, 0x62, 0x63 };
Message msg = new Message(body);
String testBody = new String(msg.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET);
String expectedBody = new String(body, UTF8);
assertThat(testBody, is(expectedBody));
}
use of com.microsoft.azure.sdk.iot.device.Message in project azure-iot-sdk-java by Azure.
the class MessageTest method setPropertyRejectsIllegalName.
// Tests_SRS_MESSAGE_11_030: [If name contains a character not specified in RFC 2047, the function shall throw an IllegalArgumentException.]
@Test(expected = IllegalArgumentException.class)
public void setPropertyRejectsIllegalName(@Mocked final MessageProperty mockProperty) {
final byte[] body = { 0x61, 0x62, 0x63 };
final String invalidName = " ";
final String value = "test-value";
new NonStrictExpectations() {
{
new MessageProperty(invalidName, value);
result = new IllegalArgumentException();
}
};
Message msg = new Message(body);
msg.setProperty(invalidName, value);
}
use of com.microsoft.azure.sdk.iot.device.Message in project azure-iot-sdk-java by Azure.
the class HttpsSingleMessageTest method toMessageCopiesBody.
// Tests_SRS_HTTPSSINGLEMESSAGE_11_007: [The function shall return an IoT Hub message with a copy of the message body as its body.]
@Test
public void toMessageCopiesBody(@Mocked final HttpsResponse mockResponse, @Mocked final MessageProperty mockProperty, @Mocked final Message mockMsg) {
final byte[] body = { 0x61, 0x62, 0x63 };
final Map<String, String> headerFields = new HashMap<>();
final String propertyName = "iothub-app-test-property-name";
final String propertyValue = "test-property-value";
headerFields.put(propertyName, propertyValue);
new NonStrictExpectations() {
{
mockResponse.getBody();
result = body;
mockResponse.getHeaderFields();
result = headerFields;
MessageProperty.isValidAppProperty(propertyName, propertyValue);
result = true;
new MessageProperty(propertyName, propertyValue);
result = mockProperty;
mockProperty.getName();
result = propertyName;
mockProperty.getValue();
result = propertyValue;
new Message(body);
result = mockMsg;
}
};
HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockResponse);
httpsMsg.toMessage();
final byte[] expectedBody = body;
new Verifications() {
{
new Message(expectedBody);
}
};
}
use of com.microsoft.azure.sdk.iot.device.Message in project azure-iot-sdk-java by Azure.
the class MqttDeviceMethodTest method receiveSucceeds.
/*
Tests_SRS_MqttDeviceMethod_25_024: [**This method shall call parseTopic to parse the topic from the received Messages queue looking for presence of $iothub/methods/ in the topics .**]**
Tests_SRS_MqttDeviceMethod_25_026: [**This method shall call parsePayload to get the message payload from the recevived Messages queue corresponding to the messaging client's operation.**]**
Tests_SRS_MqttDeviceMethod_25_028: [**If the topic is of type post topic then this method shall parse further for method name and set it for the message by calling setMethodName for the message**]**
Tests_SRS_MqttDeviceMethod_25_030: [**If the topic is of type post topic then this method shall parse further to look for request id which if found is set by calling setRequestId**]**
Tests_SRS_MqttDeviceMethod_25_032: [**If the topic is of type post topic and if method name and request id has been successfully parsed then this method shall set operation type as DEVICE_OPERATION_METHOD_RECEIVE_REQUEST **]**
*/
@Test
public void receiveSucceeds() throws IOException {
//arrange
String topic = "$iothub/methods/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();
DeviceMethodMessage testDMMessage = (DeviceMethodMessage) testMessage;
//assert
assertNotNull(testMessage);
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.Message in project azure-iot-sdk-java by Azure.
the class MqttDeviceMethodTest method receiveThrowsIfMethodNameCouldNotBeParsed.
/*
Tests_SRS_MqttDeviceMethod_25_029: [**If method name not found or is null then receive shall throw IOException **]**
*/
@Test(expected = IOException.class)
public void receiveThrowsIfMethodNameCouldNotBeParsed() throws IOException {
//arrange
String topic = "$iothub/methods/POST/";
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();
}
Aggregations