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));
}
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();
}
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();
}
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();
}
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);
}
};
}
Aggregations