Search in sources :

Example 41 with MessageProperty

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));
}
Also used : HashMap(java.util.HashMap) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) NonStrictExpectations(mockit.NonStrictExpectations) HttpsSingleMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage) Test(org.junit.Test)

Example 42 with MessageProperty

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;
}
Also used : MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty)

Example 43 with MessageProperty

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();
}
Also used : MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty)

Example 44 with MessageProperty

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;
        }
    };
}
Also used : MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) Test(org.junit.Test)

Example 45 with MessageProperty

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('}');
}
Also used : HashMap(java.util.HashMap) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String)

Aggregations

MessageProperty (com.microsoft.azure.sdk.iot.device.MessageProperty)66 Test (org.junit.Test)56 NonStrictExpectations (mockit.NonStrictExpectations)33 HttpsSingleMessage (com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage)25 Message (com.microsoft.azure.sdk.iot.device.Message)20 HashMap (java.util.HashMap)11 MqttMessaging (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging)9 Pair (org.apache.commons.lang3.tuple.Pair)7 HttpsBatchMessage (com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage)6 ArrayList (java.util.ArrayList)5 Verifications (mockit.Verifications)5 IotHubConnectionString (com.microsoft.azure.sdk.iot.device.IotHubConnectionString)4 Base64.encodeBase64String (org.apache.commons.codec.binary.Base64.encodeBase64String)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)4 IotHubMessageResult (com.microsoft.azure.sdk.iot.device.IotHubMessageResult)1 MessageType (com.microsoft.azure.sdk.iot.device.MessageType)1 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)1 Date (java.util.Date)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1