use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage in project azure-iot-sdk-java by Azure.
the class HttpsBatchMessageTest method getContentTypeReturnsCorrectContentType.
// Tests_SRS_HTTPSBATCHMESSAGE_11_011: [The function shall return 'application/vnd.microsoft.iothub.json'.]
@Test
public void getContentTypeReturnsCorrectContentType() throws IotHubSizeExceededException {
List<HttpsSingleMessage> mockMessageList = new ArrayList<>();
HttpsBatchMessage batchMsg = new HttpsBatchMessage(mockMessageList);
String testContentType = batchMsg.getContentType();
final String expectedContentType = "application/vnd.microsoft.iothub.json";
assertThat(testContentType, is(expectedContentType));
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage in project azure-iot-sdk-java by Azure.
the class HttpsSingleMessageTest method parseHttpsMessageHandlesPropertiesCorrectlyAndToMessageExposesCorrectSystemProperties.
// Tests_SRS_HTTPSSINGLEMESSAGE_34_020: [The function shall return an IoT Hub message with all system properties set accordingly.]
// Tests_SRS_HTTPSSINGLEMESSAGE_34_021: [The parsed HttpsSingleMessage shall include all valid HTTPS system-defined properties in the response header as message properties.]
// Tests_SRS_HTTPSSINGLEMESSAGE_34_015: [The function shall return a copy of the message's system properties.]
@Test
public void parseHttpsMessageHandlesPropertiesCorrectlyAndToMessageExposesCorrectSystemProperties(@Mocked final HttpsResponse httpsResponse) {
// arrange
String correlationIdKey = HTTPS_SYSTEM_PROPERTY_PREFIX + "correlationid";
String userIdKey = HTTPS_SYSTEM_PROPERTY_PREFIX + "userid";
String messageIdKey = HTTPS_SYSTEM_PROPERTY_PREFIX + "messageid";
String toKey = HTTPS_SYSTEM_PROPERTY_PREFIX + "to";
String appPropertyKey = "app_property";
String correlationId = "1234";
String messageId = "3456";
String userId = "6789";
String to = "device4";
String appProperty = "app_property_value";
final Map<String, String> headerFields = new HashMap<>();
headerFields.put(correlationIdKey, correlationId);
headerFields.put(userIdKey, userId);
headerFields.put(messageIdKey, messageId);
headerFields.put(toKey, to);
headerFields.put(HTTPS_APP_PROPERTY_PREFIX + appPropertyKey, appProperty);
new NonStrictExpectations() {
{
httpsResponse.getBody();
result = "body".getBytes(StandardCharsets.UTF_8);
httpsResponse.getHeaderFields();
result = headerFields;
}
};
// act (parseHttpMessage)
final HttpsSingleMessage actualHttpMessage = HttpsSingleMessage.parseHttpsMessage(httpsResponse);
// assert (parseHttpMessage)
assertEquals(1, actualHttpMessage.getProperties().length);
assertTrue(propertyAssignedCorrectly(actualHttpMessage.getProperties(), HTTPS_APP_PROPERTY_PREFIX + appPropertyKey, appProperty));
assertTrue(systemPropertyAssignedCorrectly(actualHttpMessage.getSystemProperties(), correlationIdKey, correlationId));
assertTrue(systemPropertyAssignedCorrectly(actualHttpMessage.getSystemProperties(), messageIdKey, messageId));
assertTrue(systemPropertyAssignedCorrectly(actualHttpMessage.getSystemProperties(), userIdKey, userId));
assertTrue(systemPropertyAssignedCorrectly(actualHttpMessage.getSystemProperties(), toKey, to));
// act (toMessage)
Message actualMessage = actualHttpMessage.toMessage();
// assert (toMessage)
assertEquals(correlationId, actualMessage.getCorrelationId());
assertEquals(messageId, actualMessage.getMessageId());
assertEquals(3, actualMessage.getProperties().length);
assertTrue(propertyAssignedCorrectly(actualMessage.getProperties(), appPropertyKey, appProperty));
assertTrue(propertyAssignedCorrectly(actualMessage.getProperties(), HTTPS_APP_PROPERTY_PREFIX + userIdKey, userId));
assertTrue(propertyAssignedCorrectly(actualMessage.getProperties(), HTTPS_APP_PROPERTY_PREFIX + toKey, to));
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage 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);
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage in project azure-iot-sdk-java by Azure.
the class HttpsSingleMessageTest method parseHttpsJsonMessageFromMessageWithMessageId.
// Tests_SRS_HTTPSSINGLEMESSAGE_34_019: [If the message contains a system property, the parsed HttpsSingleMessage shall add the corresponding property with property value.]
@Test
public void parseHttpsJsonMessageFromMessageWithMessageId(@Mocked final Message mockMsg, @Mocked final MessageProperty mockProperty) {
// arrange
final byte[] body = { 0x61, 0x62, 0x63 };
final MessageProperty[] properties = { mockProperty };
final String messageidName = HTTPS_SYSTEM_PROPERTY_PREFIX + "messageid";
final String messageidValue = "test_messageid-value";
final String correlationidName = HTTPS_SYSTEM_PROPERTY_PREFIX + "correlationid";
final String correlationidValue = "1234";
final String useridName = HTTPS_SYSTEM_PROPERTY_PREFIX + "userid";
final String useridValue = "3456";
final String toName = HTTPS_SYSTEM_PROPERTY_PREFIX + "to";
final String toValue = "device4";
final String contentEncodingName = HTTPS_SYSTEM_PROPERTY_PREFIX + "contentencoding";
final String contentEncodingValue = "test_contentencoding-value";
final String contentTypeName = HTTPS_SYSTEM_PROPERTY_PREFIX + "contenttype";
final String contentTypeValue = "test_contenttype-value";
final String propertyName = "test-property-name";
final String propertyValue = "test-property-value";
new NonStrictExpectations() {
{
mockMsg.getBytes();
result = body;
mockMsg.getProperties();
result = properties;
mockMsg.getMessageId();
result = messageidValue;
mockProperty.getName();
result = propertyName;
mockProperty.getValue();
result = propertyValue;
mockMsg.getCorrelationId();
result = correlationidValue;
mockMsg.getUserId();
result = useridValue;
mockMsg.getTo();
result = toValue;
mockMsg.getContentType();
result = contentTypeValue;
mockMsg.getContentEncoding();
result = contentEncodingValue;
}
};
// act
HttpsSingleMessage httpsSingleMessage = HttpsSingleMessage.parseHttpsJsonMessage(mockMsg);
// assert
assertTrue(systemPropertyAssignedCorrectly(httpsSingleMessage.getSystemProperties(), messageidName, messageidValue));
assertTrue(systemPropertyAssignedCorrectly(httpsSingleMessage.getSystemProperties(), correlationidName, correlationidValue));
assertTrue(systemPropertyAssignedCorrectly(httpsSingleMessage.getSystemProperties(), useridName, useridValue));
assertTrue(systemPropertyAssignedCorrectly(httpsSingleMessage.getSystemProperties(), toName, toValue));
assertTrue(systemPropertyAssignedCorrectly(httpsSingleMessage.getSystemProperties(), contentTypeName, contentTypeValue));
assertTrue(systemPropertyAssignedCorrectly(httpsSingleMessage.getSystemProperties(), contentEncodingName, contentEncodingValue));
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage in project azure-iot-sdk-java by Azure.
the class HttpsSingleMessageTest method parseHttpsMessageFromMessageSetContentType.
// Tests_SRS_HTTPSSINGLEMESSAGE_21_002: [The parsed HttpsSingleMessage shall set the contentType as `binary/octet-stream`.]
@Test
public void parseHttpsMessageFromMessageSetContentType(@Mocked final Message mockMsg, @Mocked final MessageProperty mockProperty) {
// arrange
final byte[] body = { 0x61, 0x62, 0x63 };
final MessageProperty[] properties = { mockProperty };
final String propertyName = "test-property-name";
final String propertyValue = "test-property-value";
new NonStrictExpectations() {
{
mockMsg.getBytes();
result = body;
mockMsg.getProperties();
result = properties;
mockProperty.getName();
result = propertyName;
mockProperty.getValue();
result = propertyValue;
}
};
// act
HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockMsg);
// assert
String testContentType = httpsMsg.getContentType();
assertThat(testContentType, is("binary/octet-stream"));
}
Aggregations