Search in sources :

Example 26 with MessageProperty

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

the class MessageTest method setPropertyRejectsIllegalValue.

// Tests_SRS_MESSAGE_11_031: [If value name contains a character not specified in RFC 2047, the function shall throw an IllegalArgumentException.]
@Test(expected = IllegalArgumentException.class)
public void setPropertyRejectsIllegalValue(@Mocked final MessageProperty mockProperty) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final String name = "test-name";
    final String invalidValue = "test-value@";
    new NonStrictExpectations() {

        {
            new MessageProperty(name, invalidValue);
            result = new IllegalArgumentException();
        }
    };
    Message msg = new Message(body);
    msg.setProperty(name, invalidValue);
}
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)

Example 27 with MessageProperty

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

the class MessageTest method getPropertiesReturnsCopyOfProperties.

// Tests_SRS_MESSAGE_11_033: [The function shall return a copy of the message properties.]
@Test
public void getPropertiesReturnsCopyOfProperties(@Mocked final MessageProperty mockProperty) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final String name = "test-name";
    final String value = "test-value";
    final String httpsName = "test-https-name";
    new NonStrictExpectations() {

        {
            new MessageProperty(name, value);
            result = mockProperty;
            mockProperty.hasSameName(name);
            result = true;
            mockProperty.getValue();
            result = value;
            mockProperty.getName();
            result = httpsName;
        }
    };
    Message msg = new Message(body);
    msg.setProperty(name, value);
    MessageProperty[] testProperties = msg.getProperties();
    int expectedNumProperties = 1;
    assertThat(testProperties.length, is(expectedNumProperties));
    assertThat(testProperties[0], is(not(mockProperty)));
}
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)

Example 28 with MessageProperty

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

the class MqttMessagingTest method sendShallIncludeAllCustomPropertiesInPublishTopic.

// Tests_SRS_MqttMessaging_34_026: [This method shall append each custom property's name and value to the publishTopic before publishing.]
@Test
public void sendShallIncludeAllCustomPropertiesInPublishTopic(@Mocked final Mqtt mockMqtt) throws TransportException {
    final byte[] messageBody = { 0x61, 0x62, 0x63 };
    final String propertyName1 = "key1";
    final String propertyValue1 = "value1";
    final String propertyName2 = "key2";
    final String propertyValue2 = "value2";
    final MessageProperty[] messageProperties = new MessageProperty[] { new MessageProperty(propertyName1, propertyValue1), new MessageProperty(propertyName2, propertyValue2) };
    new NonStrictExpectations() {

        {
            mockedMessage.getBytes();
            result = messageBody;
            mockedMessage.getProperties();
            result = messageProperties;
        }
    };
    MqttMessaging testMqttMessaging = new MqttMessaging(CLIENT_ID, null, "", false, mockConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
    final String publishTopicWithCustomProperties = String.format("devices/%s/messages/events/%s=%s&%s=%s", CLIENT_ID, propertyName1, propertyValue1, propertyName2, propertyValue2);
    // act
    testMqttMessaging.send(mockedMessage);
    new Verifications() {

        {
            Deencapsulation.invoke(mockMqtt, "publish", publishTopicWithCustomProperties, 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 29 with MessageProperty

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

the class MqttMessagingTest method sendShallIncludeComponentNameInPublishTopic.

@Test
public void sendShallIncludeComponentNameInPublishTopic(@Mocked final Mqtt mockMqtt) throws TransportException {
    // arrange
    final byte[] messageBody = { 0x61, 0x62, 0x63 };
    final MessageProperty[] messageProperties = new MessageProperty[] {};
    final String componentName = "test-component-name";
    final String publishTopicWithCorrelationId = String.format("devices/%s/messages/events/$.sub=%s", CLIENT_ID, componentName);
    new NonStrictExpectations() {

        {
            mockedMessage.getBytes();
            result = messageBody;
            mockedMessage.getProperties();
            result = messageProperties;
            mockedMessage.getComponentName();
            result = componentName;
        }
    };
    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)

Example 30 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";
    new Verifications() {

        {
            mockMsg.setProperty(expectedPropertyName, propertyValue);
        }
    };
}
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)

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