Search in sources :

Example 16 with Message

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

the class HttpsBatchMessageIT method batchMessageSetsFieldsCorrectly.

@Test
public void batchMessageSetsFieldsCorrectly() throws SizeLimitExceededException {
    String msgBytes0 = "abc";
    Message msg0 = new Message(msgBytes0);
    String messageid0 = msg0.getMessageId();
    msg0.setProperty("prop-0", "value-0");
    HttpsSingleMessage httpsMsg0 = HttpsSingleMessage.parseHttpsMessage(msg0);
    byte[] msgBytes1 = { 48, 49, 50 };
    Message msg1 = new Message(msgBytes1);
    String messageid1 = msg1.getMessageId();
    msg1.setProperty("prop-1", "value-1");
    HttpsSingleMessage httpsMsg1 = HttpsSingleMessage.parseHttpsMessage(msg1);
    HttpsBatchMessage batch = new HttpsBatchMessage();
    batch.addMessage(httpsMsg0);
    batch.addMessage(httpsMsg1);
    // JSON body with whitespace removed.
    String testBatchBody = new String(batch.getBody(), UTF8).replaceAll("\\s", "");
    String expectedBatchBody = "[" + "{\"body\":\"abc\"," + "\"base64Encoded\":false," + "\"properties\":{" + "\"iothub-app-prop-0\":\"value-0\"," + "\"iothub-messageid\":\"" + messageid0 + "\"}}," + "{\"body\":\"012\"," + "\"base64Encoded\":false," + "\"properties\":{" + "\"iothub-app-prop-1\":\"value-1\"," + "\"iothub-messageid\":\"" + messageid1 + "\"}}" + "]";
    assertThat(testBatchBody, is(expectedBatchBody));
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) HttpsBatchMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage) HttpsSingleMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage) HttpsBatchMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage) HttpsSingleMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage) Test(org.junit.Test)

Example 17 with Message

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

the class SendMessagesIT method SendMessagesOverHttps.

@Test
public void SendMessagesOverHttps() throws URISyntaxException, IOException {
    String messageString = "Java client e2e test message over Https protocol";
    Message msg = new Message(messageString);
    DeviceClient client = new DeviceClient(DeviceConnectionString.get(iotHubConnectionString, deviceHttps), IotHubClientProtocol.HTTPS);
    client.open();
    for (int i = 0; i < NUM_MESSAGES_PER_CONNECTION; ++i) {
        try {
            Success messageSent = new Success();
            EventCallback callback = new EventCallback();
            client.sendEventAsync(msg, callback, messageSent);
            Integer waitDuration = 0;
            while (!messageSent.getResult()) {
                Thread.sleep(RETRY_MILLISECONDS);
                if ((waitDuration += RETRY_MILLISECONDS) > SEND_TIMEOUT_MILLISECONDS) {
                    break;
                }
            }
            if (!messageSent.getResult()) {
                Assert.fail("Sending message over HTTPS protocol failed");
            }
        } catch (Exception e) {
            Assert.fail("Sending message over HTTPS protocol failed");
        }
    }
    client.closeNow();
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) DeviceConnectionString(tests.integration.com.microsoft.azure.sdk.iot.device.DeviceConnectionString) EventCallback(tests.integration.com.microsoft.azure.sdk.iot.device.EventCallback) Success(tests.integration.com.microsoft.azure.sdk.iot.device.Success) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) Test(org.junit.Test)

Example 18 with Message

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

the class SendMessagesIT method SendMessagesOverAmqps.

@Test
public void SendMessagesOverAmqps() throws URISyntaxException, IOException, InterruptedException {
    String messageString = "Java client e2e test message over Amqps protocol";
    Message msg = new Message(messageString);
    DeviceClient client = new DeviceClient(DeviceConnectionString.get(iotHubConnectionString, deviceAmqps), IotHubClientProtocol.AMQPS);
    client.open();
    for (int i = 0; i < NUM_MESSAGES_PER_CONNECTION; ++i) {
        try {
            Success messageSent = new Success();
            EventCallback callback = new EventCallback();
            client.sendEventAsync(msg, callback, messageSent);
            Integer waitDuration = 0;
            while (!messageSent.getResult()) {
                Thread.sleep(RETRY_MILLISECONDS);
                if ((waitDuration += RETRY_MILLISECONDS) > SEND_TIMEOUT_MILLISECONDS) {
                    break;
                }
            }
            if (!messageSent.getResult()) {
                Assert.fail("Sending message over AMQPS protocol failed");
            }
        } catch (Exception e) {
            Assert.fail("Sending message over AMQPS protocol failed");
        }
    }
    client.closeNow();
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) DeviceConnectionString(tests.integration.com.microsoft.azure.sdk.iot.device.DeviceConnectionString) EventCallback(tests.integration.com.microsoft.azure.sdk.iot.device.EventCallback) Success(tests.integration.com.microsoft.azure.sdk.iot.device.Success) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) Test(org.junit.Test)

Example 19 with Message

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

the class SendMessagesIT method SendMessagesOverMqtt.

@Test
public void SendMessagesOverMqtt() throws URISyntaxException, IOException {
    String messageString = "Java client e2e test message over Mqtt protocol";
    Message msg = new Message(messageString);
    DeviceClient client = new DeviceClient(DeviceConnectionString.get(iotHubConnectionString, deviceMqtt), IotHubClientProtocol.MQTT);
    client.open();
    for (int i = 0; i < NUM_MESSAGES_PER_CONNECTION; ++i) {
        try {
            Success messageSent = new Success();
            EventCallback callback = new EventCallback();
            client.sendEventAsync(msg, callback, messageSent);
            Integer waitDuration = 0;
            while (!messageSent.getResult()) {
                Thread.sleep(RETRY_MILLISECONDS);
                if ((waitDuration += RETRY_MILLISECONDS) > SEND_TIMEOUT_MILLISECONDS) {
                    break;
                }
            }
            if (!messageSent.getResult()) {
                Assert.fail("Sending message over MQTT protocol failed");
            }
        } catch (Exception e) {
            Assert.fail("Sending message over MQTT protocol failed");
        }
    }
    client.closeNow();
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) DeviceConnectionString(tests.integration.com.microsoft.azure.sdk.iot.device.DeviceConnectionString) EventCallback(tests.integration.com.microsoft.azure.sdk.iot.device.EventCallback) Success(tests.integration.com.microsoft.azure.sdk.iot.device.Success) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) Test(org.junit.Test)

Example 20 with Message

use of com.microsoft.azure.sdk.iot.device.Message 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)

Aggregations

Message (com.microsoft.azure.sdk.iot.device.Message)31 Test (org.junit.Test)29 DeviceMethodMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage)7 MessageProperty (com.microsoft.azure.sdk.iot.device.MessageProperty)7 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)6 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)6 NonStrictExpectations (mockit.NonStrictExpectations)6 Mqtt (com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt)4 MqttMessaging (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging)4 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)3 HttpsSingleMessage (com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage)3 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)3 IOException (java.io.IOException)3 URISyntaxException (java.net.URISyntaxException)3 HashMap (java.util.HashMap)3 DeviceConnectionString (tests.integration.com.microsoft.azure.sdk.iot.device.DeviceConnectionString)3 EventCallback (tests.integration.com.microsoft.azure.sdk.iot.device.EventCallback)3 Success (tests.integration.com.microsoft.azure.sdk.iot.device.Success)3 Verifications (mockit.Verifications)2 DeviceTwinMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage)1