use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage 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.HttpsBatchMessage in project azure-iot-sdk-java by Azure.
the class HttpsBatchMessageTest method constructorInitializesEmptyArrayBody.
// Tests_SRS_HTTPSBATCHMESSAGE_11_001: [The constructor shall initialize the batch message with the body as an empty JSON array.]
// Tests_SRS_HTTPSBATCHMESSAGE_11_006: [The function shall return the current batch message body as a byte array.]
// Tests_SRS_HTTPSBATCHMESSAGE_11_007: [The batch message body shall be encoded using UTF-8.]
@Test
public void constructorInitializesEmptyArrayBody() {
HttpsBatchMessage batchMsg = new HttpsBatchMessage();
String testBatchBody = new String(batchMsg.getBody(), UTF8);
final String expectedBatchBody = "[]";
assertThat(testBatchBody, is(expectedBatchBody));
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage in project azure-iot-sdk-java by Azure.
the class HttpsBatchMessageTest method numMessagesIncrementedCorrectly.
// Tests_SRS_HTTPSBATCHMESSAGE_11_010: [The function shall return the number of messages currently in the batch.]
@Test
public void numMessagesIncrementedCorrectly(@Mocked final HttpsSingleMessage mockMsg) throws SizeLimitExceededException {
final String msgBody = "test-msg-body";
final boolean isBase64Encoded = false;
new NonStrictExpectations() {
{
mockMsg.getBodyAsString();
result = msgBody;
mockMsg.isBase64Encoded();
result = isBase64Encoded;
}
};
HttpsBatchMessage batchMsg = new HttpsBatchMessage();
batchMsg.addMessage(mockMsg);
batchMsg.addMessage(mockMsg);
batchMsg.addMessage(mockMsg);
int testNumMessages = batchMsg.numMessages();
final int expectedNumMessages = 3;
assertThat(testNumMessages, is(expectedNumMessages));
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage in project azure-iot-sdk-java by Azure.
the class HttpsBatchMessageTest method addMessageRejectsOverflowingMessageAndPreservesOldBatchState.
// Tests_SRS_HTTPSBATCHMESSAGE_11_009: [If the function throws a SizeLimitExceededException, the batched message shall remain as if the message was never added.]
@Test
public void addMessageRejectsOverflowingMessageAndPreservesOldBatchState(@Mocked final HttpsSingleMessage mockMsg) throws SizeLimitExceededException {
final int msgBodySize = SERVICEBOUND_MESSAGE_MAX_SIZE_BYTES / 2 + 1;
final byte[] msgBodyBytes = new byte[msgBodySize];
final String msgBody = new String(msgBodyBytes, UTF8);
final boolean isBase64Encoded = false;
new NonStrictExpectations() {
{
mockMsg.getBodyAsString();
result = msgBody;
mockMsg.isBase64Encoded();
result = isBase64Encoded;
}
};
HttpsBatchMessage batchMsg = new HttpsBatchMessage();
batchMsg.addMessage(mockMsg);
try {
batchMsg.addMessage(mockMsg);
} catch (SizeLimitExceededException e) {
final int expectedTwoMsgBodySize = 2 * msgBodySize;
assertThat(batchMsg.getBody().length, is(lessThan(expectedTwoMsgBodySize)));
}
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage in project azure-iot-sdk-java by Azure.
the class HttpsBatchMessageTest method addMessageSetsPropertiesCorrectly.
// Tests_SRS_HTTPSBATCHMESSAGE_11_005: [The JSON object shall have the field "properties" set to a JSON object which has a key-value pair for each message property, where the key is the HTTPS property name and the value is the property value.]
@Test
public void addMessageSetsPropertiesCorrectly(@Mocked final HttpsSingleMessage mockMsg, @Mocked final MessageProperty mockProperty) throws SizeLimitExceededException {
final String msgBody = "test-msg-body";
final boolean isBase64Encoded = false;
final String propertyHttpsName = "test-property-name";
final String propertyValue = "test-property-value";
final MessageProperty[] properties = { mockProperty };
new NonStrictExpectations() {
{
mockMsg.getBodyAsString();
result = msgBody;
mockMsg.getProperties();
result = properties;
mockProperty.getName();
result = propertyHttpsName;
mockProperty.getValue();
result = propertyValue;
mockMsg.isBase64Encoded();
result = isBase64Encoded;
}
};
HttpsBatchMessage batchMsg = new HttpsBatchMessage();
batchMsg.addMessage(mockMsg);
String testBatchBody = new String(batchMsg.getBody(), UTF8).replaceAll("\\s", "");
final String expectedMsgProperties = "\"properties\":{\"" + propertyHttpsName + "\":\"" + propertyValue + "\"}";
assertThat(testBatchBody, containsString(expectedMsgProperties));
}
Aggregations