use of com.microsoft.azure.sdk.iot.device.MessageProperty in project azure-iot-sdk-java by Azure.
the class MqttMessagingTest method sendShallIncludeUserIdInPublishTopic.
// Tests_SRS_MqttMessaging_34_030: [If the message has a UserId, this method shall append that userId to publishTopic before publishing using the key name `$.uid`.]
@Test
public void sendShallIncludeUserIdInPublishTopic(@Mocked final Mqtt mockMqtt) throws TransportException {
// arrange
final byte[] messageBody = { 0x61, 0x62, 0x63 };
final MessageProperty[] messageProperties = new MessageProperty[] {};
final String userId = "test-user-id";
final String publishTopicWithUserId = String.format("devices/%s/messages/events/$.uid=%s", CLIENT_ID, userId);
new NonStrictExpectations() {
{
mockedMessage.getBytes();
result = messageBody;
mockedMessage.getProperties();
result = messageProperties;
mockedMessage.getUserId();
result = userId;
}
};
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", publishTopicWithUserId, mockedMessage);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.MessageProperty in project azure-iot-sdk-java by Azure.
the class MqttMessagingTest method sendShallIncludeAllSystemPropertiesAndAllCustomPropertiesInPublishTopic.
// Tests_SRS_MqttMessaging_34_029: [If the message has a To, this method shall append that To to publishTopic before publishing using the key name `$.to`.]
// Tests_SRS_MqttMessaging_34_030: [If the message has a UserId, this method shall append that userId to publishTopic before publishing using the key name `$.uid`.]
// Tests_SRS_MqttMessaging_34_028: [If the message has a correlationId, this method shall append that correlationid to publishTopic before publishing using the key name `$.cid`.]
// Tests_SRS_MqttMessaging_21_027: [send method shall append the messageid to publishTopic before publishing using the key name `$.mid`.]
// Tests_SRS_MqttMessaging_34_026: [This method shall append each custom property's name and value to the publishTopic before publishing.]
// Tests_SRS_MqttMessaging_34_032: [If the message has a OutputName, this method shall append that to publishTopic before publishing using the key name `$.on`.]
// Tests_SRS_MqttMessaging_34_032: [If the message has a content type, this method shall append that to publishTopic before publishing using the key name `$.ct`.]
// Tests_SRS_MqttMessaging_34_034: [If the message has a creation time utc, this method shall append that to publishTopic before publishing using the key name `$.ctime`.]
@Test
public void sendShallIncludeAllSystemPropertiesAndAllCustomPropertiesInPublishTopic(@Mocked final Mqtt mockMqtt) throws TransportException, UnsupportedEncodingException {
final byte[] messageBody = { 0x61, 0x62, 0x63 };
final String propertyName1 = "key1";
final String propertyValue1 = "value1";
final String propertyName2 = "key2";
final String propertyValue2 = "value2";
final String messageId = "test-message-id";
final String correlationId = "test-correlation-id";
final String userId = "test-user-id";
final String to = "test-to";
final String outputName = "outputName";
final String contentType = "application/json";
final String contentTypeEncoded = URLEncoder.encode(contentType, StandardCharsets.UTF_8.name());
final String contentEncoding = "utf-8";
final String creationTimeUtc = "2008-10-01T17:04:32.0000000";
final String creationTimeUtcEncoded = URLEncoder.encode(creationTimeUtc, StandardCharsets.UTF_8.name());
final MessageProperty[] messageProperties = new MessageProperty[] { new MessageProperty(propertyName1, propertyValue1), new MessageProperty(propertyName2, propertyValue2) };
new NonStrictExpectations() {
{
mockedMessage.getBytes();
result = messageBody;
mockedMessage.getMessageId();
result = messageId;
mockedMessage.getCorrelationId();
result = correlationId;
mockedMessage.getUserId();
result = userId;
mockedMessage.getTo();
result = to;
mockedMessage.getContentType();
result = contentType;
mockedMessage.getContentEncoding();
result = contentEncoding;
mockedMessage.getProperties();
result = messageProperties;
mockedMessage.getOutputName();
result = outputName;
mockedMessage.getCreationTimeUTC();
result = new Date(1234);
mockedMessage.getCreationTimeUTCString();
result = creationTimeUtc;
}
};
MqttMessaging testMqttMessaging = new MqttMessaging(CLIENT_ID, null, "", false, mockConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
final String publishTopicWithAllSystemAndCustomProperties = String.format("devices/%s/messages/events/$.mid=%s&$.cid=%s&$.uid=%s&$.to=%s&$.on=%s&$.ce=%s&$.ct=%s&$.ctime=%s&%s=%s&%s=%s", CLIENT_ID, messageId, correlationId, userId, to, outputName, contentEncoding, contentTypeEncoded, creationTimeUtcEncoded, propertyName1, propertyValue1, propertyName2, propertyValue2);
// act
testMqttMessaging.send(mockedMessage);
new Verifications() {
{
Deencapsulation.invoke(mockMqtt, "publish", publishTopicWithAllSystemAndCustomProperties, mockedMessage);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.MessageProperty in project azure-iot-sdk-java by Azure.
the class MqttMessagingTest method sendShallIncludeMessageIdInPublishTopic.
// Tests_SRS_MqttMessaging_21_027: [send method shall append the messageid to publishTopic before publishing using the key name `$.mid`.]
@Test
public void sendShallIncludeMessageIdInPublishTopic(@Mocked final Mqtt mockMqtt) throws TransportException {
// arrange
final byte[] messageBody = { 0x61, 0x62, 0x63 };
final MessageProperty[] messageProperties = new MessageProperty[] {};
final String messageId = "test-message-id";
final String publishTopicWithMessageId = String.format("devices/%s/messages/events/$.mid=%s", CLIENT_ID, messageId);
new NonStrictExpectations() {
{
mockedMessage.getBytes();
result = messageBody;
mockedMessage.getProperties();
result = messageProperties;
mockedMessage.getMessageId();
result = messageId;
}
};
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", publishTopicWithMessageId, mockedMessage);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.MessageProperty in project azure-iot-sdk-java by Azure.
the class HttpsSingleMessage method getProperties.
/**
* Returns a copy of the message properties.
*
* @return a copy of the message properties.
*/
public MessageProperty[] getProperties() {
// Codes_SRS_HTTPSSINGLEMESSAGE_11_013: [The function shall return a copy of the message properties.]
int propertiesSize = this.properties.length;
MessageProperty[] propertiesCopy = new MessageProperty[propertiesSize];
for (int i = 0; i < propertiesSize; ++i) {
MessageProperty property = this.properties[i];
MessageProperty propertyCopy = new MessageProperty(property.getName(), property.getValue());
propertiesCopy[i] = propertyCopy;
}
return propertiesCopy;
}
use of com.microsoft.azure.sdk.iot.device.MessageProperty in project azure-iot-sdk-java by Azure.
the class HttpsSingleMessage method toMessage.
/**
* Returns the Iot Hub message represented by the HTTPS message.
*
* @return the IoT Hub message represented by the HTTPS message.
*/
public Message toMessage() {
// Codes_SRS_HTTPSSINGLEMESSAGE_11_007: [The function shall return an IoT Hub message with a copy of the message body as its body.]
Message msg = new Message(this.getBody());
// Codes_SRS_HTTPSSINGLEMESSAGE_11_008: [The function shall return an IoT Hub message with application-defined properties that have the prefix 'iothub-app' removed.]
for (MessageProperty property : this.properties) {
String propertyName = httpsAppPropertyToAppProperty(property.getName());
String propertyValue = property.getValue();
msg.setProperty(propertyName, propertyValue);
}
// Codes_SRS_HTTPSSINGLEMESSAGE_34_020: [The function shall return an IoT Hub message with all system properties set accordingly.]
if (this.systemProperties.containsKey(MESSAGE_ID_KEY)) {
msg.setMessageId(this.systemProperties.get(MESSAGE_ID_KEY));
}
if (this.systemProperties.containsKey(USER_ID_KEY)) {
msg.setProperty(HTTPS_APP_PROPERTY_PREFIX + USER_ID_KEY, this.systemProperties.get(USER_ID_KEY));
}
if (this.systemProperties.containsKey(CORRELATION_ID_KEY)) {
msg.setCorrelationId(this.systemProperties.get(CORRELATION_ID_KEY));
}
if (this.systemProperties.containsKey(CONTENT_TYPE_KEY)) {
msg.setContentTypeFinal(this.systemProperties.get(CONTENT_TYPE_KEY));
}
if (this.systemProperties.containsKey(CONTENT_ENCODING_KEY)) {
msg.setContentEncoding(this.systemProperties.get(CONTENT_ENCODING_KEY));
}
if (this.systemProperties.containsKey(TO_KEY)) {
msg.setProperty(HTTPS_APP_PROPERTY_PREFIX + TO_KEY, this.systemProperties.get(TO_KEY));
}
return msg;
}
Aggregations