Search in sources :

Example 36 with MessageProperty

use of com.microsoft.azure.sdk.iot.device.MessageProperty 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));
}
Also used : HttpsBatchMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 37 with MessageProperty

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

the class HttpsSingleMessageTest method toMessageRemovesPrefixFromProperties.

// Tests_SRS_HTTPSSINGLEMESSAGE_11_008: [The function shall return an IoT Hub message with application-defined properties that have the prefix 'iothub-app' removed.]
@Test
public void toMessageRemovesPrefixFromProperties(@Mocked final HttpsResponse mockResponse, @Mocked final MessageProperty mockProperty, @Mocked final Message mockMsg) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final Map<String, String> headerFields = new HashMap<>();
    final String propertyName = "iothub-app-test-property-name";
    final String propertyValue = "test-property-value";
    headerFields.put(propertyName, propertyValue);
    new NonStrictExpectations() {

        {
            mockResponse.getBody();
            result = body;
            mockResponse.getHeaderFields();
            result = headerFields;
            MessageProperty.isValidAppProperty(propertyName, propertyValue);
            result = true;
            new MessageProperty(propertyName, propertyValue);
            result = mockProperty;
            mockProperty.getName();
            result = propertyName;
            mockProperty.getValue();
            result = propertyValue;
            new Message(body);
            result = mockMsg;
        }
    };
    HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockResponse);
    httpsMsg.toMessage();
    final String expectedPropertyName = "test-property-name";
    final String expectedPropertyValue = propertyValue;
    new Verifications() {

        {
            mockMsg.setProperty(expectedPropertyName, expectedPropertyValue);
        }
    };
}
Also used : HttpsSingleMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage) Message(com.microsoft.azure.sdk.iot.device.Message) HashMap(java.util.HashMap) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) HttpsSingleMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage) Test(org.junit.Test)

Example 38 with MessageProperty

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

the class MessagePropertyTest method constructorSavesPropertyName.

// Tests_SRS_MESSAGEPROPERTY_11_001: [The constructor shall save the property name and value.]
// Tests_SRS_MESSAGEPROPERTY_11_004: [The function shall return the property name.]
@Test
public void constructorSavesPropertyName() {
    final String name = "test-name";
    final String value = "test-value";
    MessageProperty property = new MessageProperty(name, value);
    String testName = property.getName();
    final String expectedName = name;
    assertThat(testName, is(expectedName));
}
Also used : MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) Test(org.junit.Test)

Example 39 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 40 with MessageProperty

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

the class HttpsSingleMessageTest method getContentTypeReturnsCorrectContentType.

// Tests_SRS_HTTPSSINGLEMESSAGE_11_011: [The function shall return the message content-type as 'binary/octet-stream'.]
@Test
public void getContentTypeReturnsCorrectContentType(@Mocked final Message mockMsg, @Mocked final MessageProperty mockProperty) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final boolean base64Encoded = false;
    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);
    String testContentType = httpsMsg.getContentType();
    String expectedContentType = "binary/octet-stream";
    assertThat(testContentType, is(expectedContentType));
}
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)

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