Search in sources :

Example 56 with MessageProperty

use of com.microsoft.azure.sdk.iot.device.MessageProperty 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();
    assertThat(testBody, is(body));
    body[0] = 0x34;
    assertThat(testBody, is(not(body)));
}
Also used : 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 57 with MessageProperty

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

the class MessagePropertyTest method constructorSavesPropertyValueSlash.

@Test
public void constructorSavesPropertyValueSlash() {
    final String name = "topic";
    final String value = "/news/sports";
    MessageProperty property = new MessageProperty(name, value);
    String testValue = property.getValue();
    assertThat(testValue, is(value));
}
Also used : MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) Test(org.junit.Test)

Example 58 with MessageProperty

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

the class MessagePropertyTest method hasNameMatchesNameInCaseInsensitiveManner.

// Tests_SRS_MESSAGEPROPERTY_11_006: [The function shall return true if and only if the property has the given name, where the names are compared in a case-insensitive manner.]
@Test
public void hasNameMatchesNameInCaseInsensitiveManner() {
    final String name = "test-name";
    final String value = "test-value";
    MessageProperty property = new MessageProperty(name, value);
    boolean testHasName = property.hasSameName("Test-Name");
    final boolean expectedHasName = true;
    assertThat(testHasName, is(expectedHasName));
}
Also used : MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) Test(org.junit.Test)

Example 59 with MessageProperty

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

the class SampleMessageReceiveCallback method execute.

@Override
public IotHubMessageResult execute(Message msg, Object context) {
    log.debug("Received message with content: {}", new String(msg.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET));
    for (MessageProperty messageProperty : msg.getProperties()) {
        log.debug("{}: {}", messageProperty.getName(), messageProperty.getValue());
    }
    IotHubMessageResult res = IotHubMessageResult.COMPLETE;
    log.debug("Responding to message with {}", res.name());
    return res;
}
Also used : IotHubMessageResult(com.microsoft.azure.sdk.iot.device.IotHubMessageResult) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty)

Example 60 with MessageProperty

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

the class MqttMessagingTest method sendShallIncludeCorrelationIdInPublishTopic.

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

        {
            mockedMessage.getBytes();
            result = messageBody;
            mockedMessage.getProperties();
            result = messageProperties;
            mockedMessage.getCorrelationId();
            result = correlationId;
        }
    };
    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", publishTopicWithCorrelationId, 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)

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