use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage in project azure-iot-sdk-java by Azure.
the class HttpsSingleMessageTest method getBodyReturnsCopyOfBody.
// Tests_SRS_HTTPSSINGLEMESSAGE_11_009: [The function shall return a copy of the message body.]
@Test
public void getBodyReturnsCopyOfBody(@Mocked final Message mockMsg, @Mocked final MessageProperty mockProperty) {
final byte[] body = { 0x61, 0x62, 0x63 };
final MessageProperty[] properties = { mockProperty };
final String propertyName = "test-property-name";
final String propertyValue = "test-property-value";
new NonStrictExpectations() {
{
mockMsg.getBytes();
result = body;
mockMsg.getProperties();
result = properties;
mockProperty.getName();
result = propertyName;
mockProperty.getValue();
result = propertyValue;
}
};
HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockMsg);
byte[] testBody = httpsMsg.getBody();
byte[] expectedBody = body;
assertThat(testBody, is(expectedBody));
testBody[0] = 0x34;
testBody = httpsMsg.getBody();
assertThat(testBody, is(expectedBody));
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage in project azure-iot-sdk-java by Azure.
the class HttpsSingleMessageTest method parseHttpsMessageFromMessageCopiesBody.
// Tests_SRS_HTTPSSINGLEMESSAGE_11_001: [The parsed HttpsSingleMessage shall have a copy of the original message body as its body.]
@Test
public void parseHttpsMessageFromMessageCopiesBody(@Mocked final Message mockMsg, @Mocked final MessageProperty mockProperty) {
final byte[] body = { 0x61, 0x62, 0x63 };
final MessageProperty[] properties = { mockProperty };
final String propertyName = "test-property-name";
final String propertyValue = "test-property-value";
new NonStrictExpectations() {
{
mockMsg.getBytes();
result = body;
mockMsg.getProperties();
result = properties;
mockProperty.getName();
result = propertyName;
mockProperty.getValue();
result = propertyValue;
}
};
HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockMsg);
byte[] testBody = httpsMsg.getBody();
byte[] expectedBody = body;
assertThat(testBody, is(expectedBody));
expectedBody[0] = 0x34;
assertThat(testBody, is(not(expectedBody)));
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage in project azure-iot-sdk-java by Azure.
the class HttpsBatchMessageIT method batchMessageSetsFieldsCorrectly.
@Test
public void batchMessageSetsFieldsCorrectly() throws SizeLimitExceededException {
String msgBytes0 = "abc";
Message msg0 = new Message(msgBytes0);
String messageid0 = msg0.getMessageId();
msg0.setProperty("prop-0", "value-0");
HttpsSingleMessage httpsMsg0 = HttpsSingleMessage.parseHttpsMessage(msg0);
byte[] msgBytes1 = { 48, 49, 50 };
Message msg1 = new Message(msgBytes1);
String messageid1 = msg1.getMessageId();
msg1.setProperty("prop-1", "value-1");
HttpsSingleMessage httpsMsg1 = HttpsSingleMessage.parseHttpsMessage(msg1);
HttpsBatchMessage batch = new HttpsBatchMessage();
batch.addMessage(httpsMsg0);
batch.addMessage(httpsMsg1);
// JSON body with whitespace removed.
String testBatchBody = new String(batch.getBody(), UTF8).replaceAll("\\s", "");
String expectedBatchBody = "[" + "{\"body\":\"abc\"," + "\"base64Encoded\":false," + "\"properties\":{" + "\"iothub-app-prop-0\":\"value-0\"," + "\"iothub-messageid\":\"" + messageid0 + "\"}}," + "{\"body\":\"012\"," + "\"base64Encoded\":false," + "\"properties\":{" + "\"iothub-app-prop-1\":\"value-1\"," + "\"iothub-messageid\":\"" + messageid1 + "\"}}" + "]";
assertThat(testBatchBody, is(expectedBatchBody));
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage in project azure-iot-sdk-java by Azure.
the class HttpsSingleMessageTest method toMessageRemovesPrefixFromProperties.
// Tests_SRS_HTTPSSINGLEMESSAGE_11_008: [The function shall return an IoT Hub message with application-defined properties that have the prefix 'iothub-app' removed.]
@Test
public void toMessageRemovesPrefixFromProperties(@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 String expectedPropertyName = "test-property-name";
final String expectedPropertyValue = propertyValue;
new Verifications() {
{
mockMsg.setProperty(expectedPropertyName, expectedPropertyValue);
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage in project azure-iot-sdk-java by Azure.
the class HttpsSingleMessageTest method getContentTypeReturnsCorrectContentType.
// Tests_SRS_HTTPSSINGLEMESSAGE_11_011: [The function shall return the message content-type as 'binary/octet-stream'.]
@Test
public void getContentTypeReturnsCorrectContentType(@Mocked final Message mockMsg, @Mocked final MessageProperty mockProperty) {
final byte[] body = { 0x61, 0x62, 0x63 };
final boolean base64Encoded = false;
final MessageProperty[] properties = { mockProperty };
final String propertyName = "test-property-name";
final String propertyValue = "test-property-value";
new NonStrictExpectations() {
{
mockMsg.getBytes();
result = body;
mockMsg.getProperties();
result = properties;
mockProperty.getName();
result = propertyName;
mockProperty.getValue();
result = propertyValue;
}
};
HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockMsg);
String testContentType = httpsMsg.getContentType();
String expectedContentType = "binary/octet-stream";
assertThat(testContentType, is(expectedContentType));
}
Aggregations