Search in sources :

Example 21 with MessageProperty

use of com.microsoft.azure.sdk.iot.device.MessageProperty in project azure-iot-sdk-java by Azure.

the class MqttMessagingTest method sendShallIncludeToInPublishTopic.

// Tests_SRS_MqttMessaging_34_029: [If the message has a To, this method shall append that To to publishTopic before publishing using the key name `$.to`.]
@Test
public void sendShallIncludeToInPublishTopic(@Mocked final Mqtt mockMqtt) throws TransportException {
    // arrange
    final byte[] messageBody = { 0x61, 0x62, 0x63 };
    final MessageProperty[] messageProperties = new MessageProperty[] {};
    final String to = "test-to";
    final String publishTopicWithTo = String.format("devices/%s/messages/events/$.to=%s", CLIENT_ID, to);
    new NonStrictExpectations() {

        {
            mockedMessage.getBytes();
            result = messageBody;
            mockedMessage.getProperties();
            result = messageProperties;
            mockedMessage.getTo();
            result = to;
        }
    };
    MqttMessaging testMqttMessaging = new MqttMessaging(CLIENT_ID, null, "", false, mockConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
    // act
    testMqttMessaging.send(mockedMessage);
    // assert
    new Verifications() {

        {
            Deencapsulation.invoke(mockMqtt, "publish", publishTopicWithTo, mockedMessage);
            times = 1;
        }
    };
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 22 with MessageProperty

use of com.microsoft.azure.sdk.iot.device.MessageProperty in project azure-iot-sdk-java by Azure.

the class HttpsSingleMessage method parser.

private static void parser(HttpsSingleMessage httpsMsg, Message message) {
    // Codes_SRS_HTTPSSINGLEMESSAGE_11_001: [The parsed HttpsSingleMessage shall have a copy of the original message body as its body.]
    // Codes_SRS_HTTPSSINGLEMESSAGE_21_016: [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_11_003: [The parsed HttpsSingleMessage shall add the prefix 'iothub-app-' to each of the message properties.]
    // Codes_SRS_HTTPSSINGLEMESSAGE_21_018: [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];
    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());
    }
    // Codes_SRS_HTTPSSINGLEMESSAGE_34_014: [If the message contains a system property, the parsed HttpsSingleMessage shall add the corresponding property with property value]
    // Codes_SRS_HTTPSSINGLEMESSAGE_34_019: [If the message contains a system property, the parsed HttpsSingleMessage shall add the corresponding property with property value]
    Map<String, String> sysProperties = new HashMap<>();
    if (message.getUserId() != null) {
        sysProperties.put(USER_ID_KEY, message.getUserId());
    }
    if (message.getMessageId() != null) {
        sysProperties.put(MESSAGE_ID_KEY, message.getMessageId());
    }
    if (message.getCorrelationId() != null) {
        sysProperties.put(CORRELATION_ID_KEY, message.getCorrelationId());
    }
    if (message.getTo() != null) {
        sysProperties.put(TO_KEY, message.getTo());
    }
    if (message.getContentEncoding() != null) {
        sysProperties.put(CONTENT_ENCODING_KEY, message.getContentEncoding());
    }
    if (message.getContentType() != null) {
        sysProperties.put(CONTENT_TYPE_KEY, message.getContentType());
    }
    httpsMsg.systemProperties = new HashMap<>(sysProperties);
}
Also used : HashMap(java.util.HashMap) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty)

Example 23 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 HTTPS response.
 *
 * @param response the HTTPS response.
 *
 * @return the HTTPS message represented by the HTTPS response.
 */
public static HttpsSingleMessage parseHttpsMessage(HttpsResponse response) {
    HttpsSingleMessage msg = new HttpsSingleMessage();
    // Codes_SRS_HTTPSSINGLEMESSAGE_11_004: [The parsed HttpsSingleMessage shall have a copy of the original response body as its body.]
    byte[] responseBody = response.getBody();
    msg.body = Arrays.copyOf(responseBody, responseBody.length);
    ArrayList<MessageProperty> properties = new ArrayList<>();
    Map<String, String> systemProperties = new HashMap<>();
    Map<String, String> headerFields = response.getHeaderFields();
    for (Map.Entry<String, String> field : headerFields.entrySet()) {
        String propertyName = field.getKey();
        String propertyValue = field.getValue();
        if (isValidHttpsAppProperty(propertyName, propertyValue)) {
            // Codes_SRS_HTTPSSINGLEMESSAGE_11_006: [The parsed HttpsSingleMessage shall include all valid HTTPS application-defined properties in the response header as message properties.]
            properties.add(new MessageProperty(propertyName, propertyValue));
        } else if (isValidHttpsSystemProperty(propertyName, propertyValue)) {
            // Codes_SRS_HTTPSSINGLEMESSAGE_34_021: [The parsed HttpsSingleMessage shall include all valid HTTPS system-defined properties in the response header as message properties.]
            String systemPropertyName = propertyName.substring(HTTPS_SYSTEM_PROPERTY_PREFIX.length());
            systemProperties.put(HTTPS_SYSTEM_PROPERTY_PREFIX + systemPropertyName.toLowerCase(), propertyValue);
        }
    }
    msg.properties = new MessageProperty[properties.size()];
    msg.properties = properties.toArray(msg.properties);
    msg.systemProperties = systemProperties;
    return msg;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) Map(java.util.Map) HashMap(java.util.HashMap)

Example 24 with MessageProperty

use of com.microsoft.azure.sdk.iot.device.MessageProperty in project azure-iot-sdk-java by Azure.

the class MqttMessaging method send.

/**
 * Sends the provided telemetry message over the mqtt connection
 *
 * @param message the message to send
 * @throws TransportException if any exception is encountered while sending the message
 */
public void send(Message message) throws TransportException {
    if (message == null || message.getBytes() == null) {
        throw new IllegalArgumentException("Message cannot be null");
    }
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(this.publishTopic);
    boolean separatorNeeded;
    separatorNeeded = appendPropertyIfPresent(stringBuilder, false, MESSAGE_ID, message.getMessageId(), false);
    separatorNeeded = appendPropertyIfPresent(stringBuilder, separatorNeeded, CORRELATION_ID, message.getCorrelationId(), false);
    separatorNeeded = appendPropertyIfPresent(stringBuilder, separatorNeeded, USER_ID, message.getUserId(), false);
    separatorNeeded = appendPropertyIfPresent(stringBuilder, separatorNeeded, TO, message.getTo(), false);
    separatorNeeded = appendPropertyIfPresent(stringBuilder, separatorNeeded, OUTPUT_NAME, message.getOutputName(), false);
    separatorNeeded = appendPropertyIfPresent(stringBuilder, separatorNeeded, CONNECTION_DEVICE_ID, message.getConnectionDeviceId(), false);
    separatorNeeded = appendPropertyIfPresent(stringBuilder, separatorNeeded, CONNECTION_MODULE_ID, message.getConnectionModuleId(), false);
    separatorNeeded = appendPropertyIfPresent(stringBuilder, separatorNeeded, CONTENT_ENCODING, message.getContentEncoding(), false);
    separatorNeeded = appendPropertyIfPresent(stringBuilder, separatorNeeded, CONTENT_TYPE, message.getContentType(), false);
    separatorNeeded = appendPropertyIfPresent(stringBuilder, separatorNeeded, CREATION_TIME_UTC, message.getCreationTimeUTCString(), false);
    if (message.isSecurityMessage()) {
        separatorNeeded = appendPropertyIfPresent(stringBuilder, separatorNeeded, MQTT_SECURITY_INTERFACE_ID, MessageProperty.IOTHUB_SECURITY_INTERFACE_ID_VALUE, false);
    }
    if (message.getComponentName() != null && !message.getComponentName().isEmpty()) {
        separatorNeeded = appendPropertyIfPresent(stringBuilder, separatorNeeded, COMPONENT_ID, message.getComponentName(), false);
    }
    for (MessageProperty property : message.getProperties()) {
        separatorNeeded = appendPropertyIfPresent(stringBuilder, separatorNeeded, property.getName(), property.getValue(), true);
    }
    if (this.moduleId != null && !this.moduleId.isEmpty()) {
        stringBuilder.append("/");
    }
    String messagePublishTopic = stringBuilder.toString();
    this.publish(messagePublishTopic, message);
}
Also used : MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty)

Example 25 with MessageProperty

use of com.microsoft.azure.sdk.iot.device.MessageProperty in project azure-iot-sdk-java by Azure.

the class MessageTest method setPropertyAndGetPropertyMatch.

// Tests_SRS_MESSAGE_11_026: [The function shall set the message property to the given value.]
// Tests_SRS_MESSAGE_11_032: [The function shall return the value associated with the message property name, where the name can be either the HTTPS or AMQPS property name.]
@Test
public void setPropertyAndGetPropertyMatch(@Mocked final MessageProperty mockProperty) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final String name = "test-name";
    final String value1 = "test-value1";
    final String value2 = "test-value2";
    new NonStrictExpectations() {

        {
            new MessageProperty(name, value1);
            result = mockProperty;
            mockProperty.hasSameName(name);
            result = true;
            mockProperty.getValue();
            result = value1;
        }
    };
    Message msg = new Message(body);
    msg.setProperty(name, value1);
    String testValue = msg.getProperty(name);
    String expectedValue = value1;
    assertThat(testValue, is(expectedValue));
    new NonStrictExpectations() {

        {
            new MessageProperty(name, value2);
            result = mockProperty;
            mockProperty.hasSameName(name);
            result = true;
            mockProperty.getValue();
            result = value2;
        }
    };
    msg.setProperty(name, value2);
    testValue = msg.getProperty(name);
    expectedValue = value2;
    assertThat(testValue, is(expectedValue));
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

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