Search in sources :

Example 31 with MessageProperty

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

the class HttpsSingleMessageTest method parseHttpsJsonMessageFromMessageWithMessageId.

// Tests_SRS_HTTPSSINGLEMESSAGE_34_019: [If the message contains a system property, the parsed HttpsSingleMessage shall add the corresponding property with property value.]
@Test
public void parseHttpsJsonMessageFromMessageWithMessageId(@Mocked final Message mockMsg, @Mocked final MessageProperty mockProperty) {
    // arrange
    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 contentEncodingName = HTTPS_SYSTEM_PROPERTY_PREFIX + "contentencoding";
    final String contentEncodingValue = "test_contentencoding-value";
    final String contentTypeName = HTTPS_SYSTEM_PROPERTY_PREFIX + "contenttype";
    final String contentTypeValue = "test_contenttype-value";
    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;
            mockMsg.getContentType();
            result = contentTypeValue;
            mockMsg.getContentEncoding();
            result = contentEncodingValue;
        }
    };
    // act
    HttpsSingleMessage httpsSingleMessage = HttpsSingleMessage.parseHttpsJsonMessage(mockMsg);
    // assert
    assertTrue(systemPropertyAssignedCorrectly(httpsSingleMessage.getSystemProperties(), messageidName, messageidValue));
    assertTrue(systemPropertyAssignedCorrectly(httpsSingleMessage.getSystemProperties(), correlationidName, correlationidValue));
    assertTrue(systemPropertyAssignedCorrectly(httpsSingleMessage.getSystemProperties(), useridName, useridValue));
    assertTrue(systemPropertyAssignedCorrectly(httpsSingleMessage.getSystemProperties(), toName, toValue));
    assertTrue(systemPropertyAssignedCorrectly(httpsSingleMessage.getSystemProperties(), contentTypeName, contentTypeValue));
    assertTrue(systemPropertyAssignedCorrectly(httpsSingleMessage.getSystemProperties(), contentEncodingName, contentEncodingValue));
}
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 32 with MessageProperty

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

the class HttpsSingleMessageTest method parseHttpsMessageFromMessageSetContentType.

// Tests_SRS_HTTPSSINGLEMESSAGE_21_002: [The parsed HttpsSingleMessage shall set the contentType as `binary/octet-stream`.]
@Test
public void parseHttpsMessageFromMessageSetContentType(@Mocked final Message mockMsg, @Mocked final MessageProperty mockProperty) {
    // arrange
    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;
        }
    };
    // act
    HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockMsg);
    // assert
    String testContentType = httpsMsg.getContentType();
    assertThat(testContentType, is("binary/octet-stream"));
}
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 33 with MessageProperty

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

the class HttpsSingleMessageTest method toMessageCopiesBody.

// Tests_SRS_HTTPSSINGLEMESSAGE_11_007: [The function shall return an IoT Hub message with a copy of the message body as its body.]
@Test
public void toMessageCopiesBody(@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 Expectations() {

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

Example 34 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)

Example 35 with MessageProperty

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

the class IotHubTransportMessageTest method constructorWithPropertiesSavesProperties.

// Tests_SRS_IOTHUBTRANSPORTMESSAGE_34_017: [This constructor shall return an instance of IotHubTransportMessage with provided bytes, messagetype, correlationid, messageid, and application properties.]
@Test
public void constructorWithPropertiesSavesProperties() {
    // arrange
    byte[] expectedData = new byte[] { 12, 34, 56 };
    MessageType expectedMessageType = MessageType.DEVICE_TELEMETRY;
    String expectedCorrelationId = "1234";
    String expectedMessageId = "5678";
    MessageProperty[] expectedProperties = new MessageProperty[2];
    expectedProperties[0] = new MessageProperty("bob", "job");
    expectedProperties[1] = new MessageProperty("john", "bill");
    // act
    IotHubTransportMessage transportMessage = new IotHubTransportMessage(expectedData, expectedMessageType, expectedMessageId, expectedCorrelationId, expectedProperties);
    // assert
    assertArrayEquals(expectedData, transportMessage.getBytes());
    assertEquals(expectedMessageType, transportMessage.getMessageType());
    assertEquals(expectedMessageId, transportMessage.getMessageId());
    assertEquals(expectedCorrelationId, transportMessage.getCorrelationId());
    assertEquals(expectedProperties.length, transportMessage.getProperties().length);
}
Also used : MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MessageType(com.microsoft.azure.sdk.iot.device.MessageType) 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