use of com.microsoft.azure.sdk.iot.device.MessageProperty in project azure-iot-sdk-java by Azure.
the class HttpsSingleMessageTest method parseHttpsMessageFromResponseDoesNotIncludeNonAppProperties.
// Tests_SRS_HTTPSSINGLEMESSAGE_11_006: [The parsed HttpsSingleMessage shall include all valid HTTPS application-defined properties in the response header as message properties.]
@Test
public void parseHttpsMessageFromResponseDoesNotIncludeNonAppProperties(@Mocked final HttpsResponse mockResponse, @Mocked final MessageProperty mockProperty) {
final byte[] body = { 0x61, 0x62, 0x63 };
final Map<String, String> headerFields = new HashMap<>();
final String propertyName = "test-property-name";
final String propertyValue = "test-property-value";
headerFields.put(propertyName, propertyValue);
new NonStrictExpectations() {
{
mockResponse.getBody();
result = body;
mockResponse.getHeaderFields();
result = headerFields;
}
};
HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockResponse);
MessageProperty[] testProperties = httpsMsg.getProperties();
MessageProperty[] expectedProperties = {};
assertThat(testProperties, is(expectedProperties));
}
use of com.microsoft.azure.sdk.iot.device.MessageProperty in project azure-iot-sdk-java by Azure.
the class HttpsSingleMessage method parseHttpsMessage.
/**
* Returns the HTTPS message represented by the service-bound message.
*
* @param message the service-bound message to be mapped to its HTTPS message
* equivalent.
*
* @return the HTTPS message represented by the service-bound message.
*/
public static HttpsSingleMessage parseHttpsMessage(Message message) {
HttpsSingleMessage httpsMsg = new HttpsSingleMessage();
int systemPropertyLength = 0;
// Codes_SRS_HTTPSSINGLEMESSAGE_11_001: [The parsed HttpsSingleMessage shall have a copy of the original message body as its body.]
byte[] msgBody = message.getBytes();
httpsMsg.body = Arrays.copyOf(msgBody, msgBody.length);
// Codes_SRS_HTTPSSINGLEMESSAGE_21_014: [If the message contains messageId, the parsed HttpsSingleMessage shall add the property 'iothub-messageid' with the messageId value.]
if (message.getMessageId() != null) {
systemPropertyLength++;
}
// Codes_SRS_HTTPSSINGLEMESSAGE_11_003: [The parsed HttpsSingleMessage shall add the prefix 'iothub-app-' to each of the message properties.]
MessageProperty[] msgProperties = message.getProperties();
httpsMsg.properties = new MessageProperty[msgProperties.length + systemPropertyLength];
int countProperty;
for (countProperty = 0; countProperty < msgProperties.length; ++countProperty) {
MessageProperty property = msgProperties[countProperty];
httpsMsg.properties[countProperty] = new MessageProperty(HTTPS_APP_PROPERTY_PREFIX + property.getName(), property.getValue());
}
if (message.getMessageId() != null) {
httpsMsg.properties[countProperty++] = new MessageProperty(HTTPS_SYSTEM_PROPERTY_PREFIX + SYSTEM_PROPERTY_MESSAGE_ID, message.getMessageId());
}
return httpsMsg;
}
use of com.microsoft.azure.sdk.iot.device.MessageProperty in project azure-iot-sdk-java by Azure.
the class HttpsBatchMessage method msgToJson.
/**
* Converts a service-bound message to a JSON object with the correct
* format.
*
* @param msg the message to be converted to a corresponding JSON object.
*
* @return the JSON string representation of the message.
*/
private static String msgToJson(HttpsSingleMessage msg) {
StringBuilder jsonMsg = new StringBuilder("{");
// Codes_SRS_HTTPSBATCHMESSAGE_11_003: [The JSON object shall have the field "body" set to the raw message.]
jsonMsg.append("\"body\":");
jsonMsg.append("\"").append(msg.getBodyAsString()).append("\",");
// Codes_SRS_HTTPSBATCHMESSAGE_11_004: [The JSON object shall have the field "base64Encoded" set to whether the raw message was Base64-encoded.]
jsonMsg.append("\"base64Encoded\":");
jsonMsg.append(Boolean.toString(msg.isBase64Encoded()));
// Codes_SRS_HTTPSBATCHMESSAGE_11_005: [The JSON object shall have the field "properties" set to a JSON object which has the field "content-type" set to the content type of the raw message.]
MessageProperty[] properties = msg.getProperties();
int numProperties = properties.length;
if (numProperties > 0) {
jsonMsg.append(",");
jsonMsg.append("\"properties\":");
jsonMsg.append("{");
for (int i = 0; i < numProperties - 1; ++i) {
MessageProperty property = properties[i];
jsonMsg.append("\"").append(property.getName()).append("\":");
jsonMsg.append("\"").append(property.getValue()).append("\",");
}
if (numProperties > 0) {
MessageProperty property = properties[numProperties - 1];
jsonMsg.append("\"").append(property.getName()).append("\":");
jsonMsg.append("\"").append(property.getValue()).append("\"");
}
jsonMsg.append("}");
}
jsonMsg.append("}");
return jsonMsg.toString();
}
use of com.microsoft.azure.sdk.iot.device.MessageProperty in project azure-iot-sdk-java by Azure.
the class MqttMessagingTest method sendShallMessageWithPropsAndMessageIdToLowerLayer.
/*
**Tests_SRS_MqttMessaging_21_027: [**send method shall append the messageid to publishTopic before publishing using the key name `$.mid`.**]**
*/
@Test
public void sendShallMessageWithPropsAndMessageIdToLowerLayer(@Mocked final Mqtt mockMqtt) throws IOException {
final byte[] messageBody = { 0x61, 0x62, 0x63 };
final String propertyName = "key";
final String propertyValue = "value";
final MessageProperty[] messageProperties = new MessageProperty[] { new MessageProperty(propertyName, propertyValue) };
final String messageidValue = "test-message-id";
new NonStrictExpectations() {
{
mockMessage.getBytes();
result = messageBody;
mockMessage.getProperties();
result = messageProperties;
mockMessage.getMessageId();
result = messageidValue;
Deencapsulation.invoke(mockMqtt, "publish", anyString, messageBody);
}
};
MqttMessaging testMqttMessaging = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
;
testMqttMessaging.send(mockMessage);
final String publishTopicWithProperties = String.format("devices/%s/messages/events/%s=%s&$.mid=%s", clientId, propertyName, propertyValue, messageidValue);
new Verifications() {
{
mockMessage.getBytes();
times = 2;
mockMessage.getProperties();
Deencapsulation.invoke(mockMqtt, "publish", publishTopicWithProperties, messageBody);
times = 1;
mockMessage.getMessageId();
times = 2;
}
};
}
use of com.microsoft.azure.sdk.iot.device.MessageProperty in project azure-iot-sdk-java by Azure.
the class HttpsBatchMessage method addJsonToStringBuilder.
/**
* Converts a service-bound message to a JSON object with the correct
* format.
*
* @param msg the message to be converted to a corresponding JSON object.
*
* @return the JSON string representation of the message.
*/
private static void addJsonToStringBuilder(HttpsSingleMessage msg, StringBuilder jsonStringBuilder) {
jsonStringBuilder.append('{' + BODY + KEY_VALUE_SEPARATOR);
jsonStringBuilder.append('\"').append(encodeBase64String(msg.getBody())).append("\",");
jsonStringBuilder.append(BASE_ENCODED_KEY + KEY_VALUE_SEPARATOR);
jsonStringBuilder.append(true);
MessageProperty[] properties = msg.getProperties();
Map<String, String> allProperties = new HashMap<>(msg.getSystemProperties());
for (MessageProperty p : properties) {
allProperties.put(p.getName(), p.getValue());
}
int numProperties = allProperties.size();
if (numProperties > 0) {
jsonStringBuilder.append(',');
jsonStringBuilder.append(PROPERTIES + KEY_VALUE_SEPARATOR);
jsonStringBuilder.append('{');
for (String key : allProperties.keySet()) {
jsonStringBuilder.append('\"').append(key).append("\":");
jsonStringBuilder.append('\"').append(allProperties.get(key)).append("\",");
}
// remove last trailing comma
jsonStringBuilder.deleteCharAt(jsonStringBuilder.length() - 1);
jsonStringBuilder.append('}');
}
jsonStringBuilder.append('}');
}
Aggregations