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