Search in sources :

Example 26 with HttpsSingleMessage

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage 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 IotHubSizeExceededException {
    final String msgBody = "test-msg-body";
    final String propertyHttpsName = "test-property-name";
    final String propertyValue = "test-property-value";
    final MessageProperty[] properties = { mockProperty };
    new NonStrictExpectations() {

        {
            mockMsg.getBody();
            result = msgBody.getBytes(StandardCharsets.UTF_8);
            mockMsg.getProperties();
            result = properties;
            mockProperty.getName();
            result = propertyHttpsName;
            mockProperty.getValue();
            result = propertyValue;
        }
    };
    List<HttpsSingleMessage> mockMessageList = new ArrayList<>();
    mockMessageList.add(mockMsg);
    HttpsBatchMessage batchMsg = new HttpsBatchMessage(mockMessageList);
    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) ArrayList(java.util.ArrayList) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String) NonStrictExpectations(mockit.NonStrictExpectations) HttpsSingleMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage) Test(org.junit.Test)

Example 27 with HttpsSingleMessage

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage in project azure-iot-sdk-java by Azure.

the class HttpsBatchMessageTest method addMessageSetsPropertiesCorrectlyWhenNoPropertiesPresent.

// 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 addMessageSetsPropertiesCorrectlyWhenNoPropertiesPresent(@Mocked final HttpsSingleMessage mockMsg) throws IotHubSizeExceededException {
    // This keyword is present in the json whenever at least one app/system property is present
    final String propertiesKeyword = "properties";
    final String msgBody = "test-msg-body";
    final boolean isBase64Encoded = false;
    final MessageProperty[] properties = {};
    new NonStrictExpectations() {

        {
            mockMsg.getBodyAsString();
            result = msgBody;
            mockMsg.getProperties();
            result = properties;
            mockMsg.isBase64Encoded();
            result = isBase64Encoded;
        }
    };
    List<HttpsSingleMessage> mockMessageList = new ArrayList<>();
    mockMessageList.add(mockMsg);
    HttpsBatchMessage batchMsg = new HttpsBatchMessage(mockMessageList);
    String testBatchBody = new String(batchMsg.getBody(), UTF8).replaceAll("\\s", "");
    assertFalse(testBatchBody.contains(propertiesKeyword));
}
Also used : HttpsBatchMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage) ArrayList(java.util.ArrayList) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String) NonStrictExpectations(mockit.NonStrictExpectations) HttpsSingleMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage) Test(org.junit.Test)

Example 28 with HttpsSingleMessage

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage in project azure-iot-sdk-java by Azure.

the class HttpsBatchMessageTest method addMessageSetsPropertiesCorrectlyWhenThereAreSystemProperties.

// 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 addMessageSetsPropertiesCorrectlyWhenThereAreSystemProperties(@Mocked final HttpsSingleMessage mockMsg, @Mocked final MessageProperty mockProperty) throws IotHubSizeExceededException {
    final String msgBody = "test-msg-body";
    final boolean isBase64Encoded = false;
    final String propertyHttpsName = "test-property-name";
    final String propertyValue = "test-property-value";
    final String correlationIdName = "correlationid";
    final String correlationIdValue = "1234";
    final String messageIdName = "messageid";
    final String messageIdValue = "5678";
    final MessageProperty[] properties = { mockProperty };
    final Map<String, String> systemProperties = new HashMap<>();
    systemProperties.put(correlationIdName, correlationIdValue);
    systemProperties.put(messageIdName, messageIdValue);
    new NonStrictExpectations() {

        {
            mockMsg.getBodyAsString();
            result = msgBody;
            mockMsg.getProperties();
            result = properties;
            mockProperty.getName();
            result = propertyHttpsName;
            mockProperty.getValue();
            result = propertyValue;
            mockMsg.isBase64Encoded();
            result = isBase64Encoded;
            mockMsg.getSystemProperties();
            result = systemProperties;
        }
    };
    List<HttpsSingleMessage> mockMessageList = new ArrayList<>();
    mockMessageList.add(mockMsg);
    HttpsBatchMessage batchMsg = new HttpsBatchMessage(mockMessageList);
    String testBatchBody = new String(batchMsg.getBody(), UTF8).replaceAll("\\s", "");
    final String expectedApplicationPropertyString = propertyHttpsName + "\":\"" + propertyValue;
    final String expectedCorrelationIdString = correlationIdName + "\":\"" + correlationIdValue;
    final String expectedMessageIdString = messageIdName + "\":\"" + messageIdValue;
    assertThat(testBatchBody, containsString(expectedApplicationPropertyString));
    assertThat(testBatchBody, containsString(expectedCorrelationIdString));
    assertThat(testBatchBody, containsString(expectedMessageIdString));
}
Also used : HashMap(java.util.HashMap) HttpsBatchMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage) ArrayList(java.util.ArrayList) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String) NonStrictExpectations(mockit.NonStrictExpectations) HttpsSingleMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage) Test(org.junit.Test)

Example 29 with HttpsSingleMessage

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage in project azure-iot-sdk-java by Azure.

the class HttpsSingleMessageTest method parseHttpsMessageFromMessageWithMessageId.

// Tests_SRS_HTTPSSINGLEMESSAGE_34_014: [If the message contains a system property, the parsed HttpsSingleMessage shall add the corresponding property with property value]
@Test
public void parseHttpsMessageFromMessageWithMessageId(@Mocked final Message mockMsg, @Mocked final MessageProperty mockProperty) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final MessageProperty[] properties = { mockProperty };
    final String messageidName = HTTPS_SYSTEM_PROPERTY_PREFIX + "messageid";
    final String messageidValue = "test_messageid-value";
    final String correlationidName = HTTPS_SYSTEM_PROPERTY_PREFIX + "correlationid";
    final String correlationidValue = "1234";
    final String useridName = HTTPS_SYSTEM_PROPERTY_PREFIX + "userid";
    final String useridValue = "3456";
    final String toName = HTTPS_SYSTEM_PROPERTY_PREFIX + "to";
    final String toValue = "device4";
    final String propertyName = "test-property-name";
    final String propertyValue = "test-property-value";
    new NonStrictExpectations() {

        {
            mockMsg.getBytes();
            result = body;
            mockMsg.getProperties();
            result = properties;
            mockMsg.getMessageId();
            result = messageidValue;
            mockProperty.getName();
            result = propertyName;
            mockProperty.getValue();
            result = propertyValue;
            mockMsg.getCorrelationId();
            result = correlationidValue;
            mockMsg.getUserId();
            result = useridValue;
            mockMsg.getTo();
            result = toValue;
        }
    };
    HttpsSingleMessage httpsSingleMessage = HttpsSingleMessage.parseHttpsMessage(mockMsg);
    assertTrue(systemPropertyAssignedCorrectly(httpsSingleMessage.getSystemProperties(), messageidName, messageidValue));
    assertTrue(systemPropertyAssignedCorrectly(httpsSingleMessage.getSystemProperties(), correlationidName, correlationidValue));
    assertTrue(systemPropertyAssignedCorrectly(httpsSingleMessage.getSystemProperties(), useridName, useridValue));
    assertTrue(systemPropertyAssignedCorrectly(httpsSingleMessage.getSystemProperties(), toName, toValue));
}
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 30 with HttpsSingleMessage

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage in project azure-iot-sdk-java by Azure.

the class HttpsSingleMessageTest method getBodyAsStringsReturnsUtf8Body.

// Tests_SRS_HTTPSSINGLEMESSAGE_11_010: [The function shall return the message body as a string encoded using charset UTF-8.]
@Test
public void getBodyAsStringsReturnsUtf8Body(@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);
    String testBody = httpsMsg.getBodyAsString();
    String expectedBody = "abc";
    assertThat(testBody, is(expectedBody));
}
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

HttpsSingleMessage (com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage)38 Test (org.junit.Test)38 NonStrictExpectations (mockit.NonStrictExpectations)32 MessageProperty (com.microsoft.azure.sdk.iot.device.MessageProperty)25 HttpsBatchMessage (com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage)12 HashMap (java.util.HashMap)12 ArrayList (java.util.ArrayList)11 Base64.encodeBase64String (org.apache.commons.codec.binary.Base64.encodeBase64String)9 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)9 Message (com.microsoft.azure.sdk.iot.device.Message)6 Verifications (mockit.Verifications)5 Expectations (mockit.Expectations)1