use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.
the class HttpsSingleMessageTest method getBodyAsStringsReturnsUtf8Body.
// Tests_SRS_HTTPSSINGLEMESSAGE_11_010: [The function shall return the message body as a string encoded using charset UTF-8.]
@Test
public void getBodyAsStringsReturnsUtf8Body(@Mocked final Message mockMsg, @Mocked final MessageProperty mockProperty) {
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;
}
};
HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockMsg);
String testBody = httpsMsg.getBodyAsString();
String expectedBody = "abc";
assertThat(testBody, is(expectedBody));
}
use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.
the class HttpsSingleMessageTest method getPropertiesReturnsCopyOfProperties.
// Tests_SRS_HTTPSSINGLEMESSAGE_11_013: [The function shall return a copy of the message properties.]
@Test
public void getPropertiesReturnsCopyOfProperties(@Mocked final Message mockMsg, @Mocked final MessageProperty mockProperty) {
final byte[] body = { 0x61, 0x62, 0x63 };
final MessageProperty[] properties = { mockProperty };
final String propertyName = "test-property-name";
final String httpsPropertyName = "iothub-app-test-property-name";
final String propertyValue = "test-property-value";
new NonStrictExpectations() {
{
mockMsg.getBytes();
result = body;
mockMsg.getProperties();
result = properties;
mockProperty.getName();
result = propertyName;
result = httpsPropertyName;
mockProperty.getValue();
result = propertyValue;
}
};
HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockMsg);
MessageProperty[] testProperties = httpsMsg.getProperties();
final MessageProperty[] expectedProperties = properties;
assertThat(testProperties.length, is(expectedProperties.length));
final String expectedPropertyName = httpsPropertyName;
final String expectedPropertyValue = propertyValue;
new Verifications() {
{
new MessageProperty(expectedPropertyName, expectedPropertyValue);
times = 2;
}
};
}
use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.
the class HttpsSingleMessageTest method getBodyReturnsCopyOfBody.
// Tests_SRS_HTTPSSINGLEMESSAGE_11_009: [The function shall return a copy of the message body.]
@Test
public void getBodyReturnsCopyOfBody(@Mocked final Message mockMsg, @Mocked final MessageProperty mockProperty) {
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;
}
};
HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockMsg);
byte[] testBody = httpsMsg.getBody();
byte[] expectedBody = body;
assertThat(testBody, is(expectedBody));
testBody[0] = 0x34;
testBody = httpsMsg.getBody();
assertThat(testBody, is(expectedBody));
}
use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.
the class HttpsSingleMessageTest method parseHttpsMessageFromMessageCopiesBody.
// Tests_SRS_HTTPSSINGLEMESSAGE_11_001: [The parsed HttpsSingleMessage shall have a copy of the original message body as its body.]
@Test
public void parseHttpsMessageFromMessageCopiesBody(@Mocked final Message mockMsg, @Mocked final MessageProperty mockProperty) {
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;
}
};
HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockMsg);
byte[] testBody = httpsMsg.getBody();
byte[] expectedBody = body;
assertThat(testBody, is(expectedBody));
expectedBody[0] = 0x34;
assertThat(testBody, is(not(expectedBody)));
}
use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method getTwinSucceeds.
/*
**Tests_SRS_DEVICETWIN_25_005: [** The function shall build the URL for this operation by calling getUrlTwin **]**
**Tests_SRS_DEVICETWIN_25_006: [** The function shall create a new SAS token **]**
**Tests_SRS_DEVICETWIN_25_007: [** The function shall create a new HttpRequest with http method as Get **]**
**Tests_SRS_DEVICETWIN_25_008: [** The function shall set the following HTTP headers specified in the IotHub DeviceTwin doc.
1. Key as authorization with value as sastoken
2. Key as request id with a new string value for every request
3. Key as User-Agent with value specified by the clientIdentifier and its version
4. Key as Accept with value as application/json
5. Key as Content-Type and value as application/json
6. Key as charset and value as utf-8
7. Key as If-Match and value as '*' **]**
**Tests_SRS_DEVICETWIN_25_009: [** The function shall send the created request and get the response **]**
**Tests_SRS_DEVICETWIN_25_011: [** The function shall deserialize the payload by calling updateTwin Api on the twin object **]**
**Tests_SRS_DEVICETWIN_25_012: [** The function shall set tags, desired property map, reported property map on the user device **]**
*/
@Test
public void getTwinSucceeds() throws Exception {
//arrange
final String connectionString = "testString";
DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
Map<String, Object> testMap = new HashMap<>();
new NonStrictExpectations() {
{
mockedDevice.getDeviceId();
result = "SomeDevID";
Deencapsulation.invoke(mockedDevice, "getTwinParser");
result = mockedTwinParser;
}
};
//act
testTwin.getTwin(mockedDevice);
//assert
new Verifications() {
{
mockedConnectionString.getUrlTwin(anyString);
times = 1;
mockedHttpRequest.setReadTimeoutMillis(anyInt);
times = 1;
mockedHttpRequest.setHeaderField(anyString, anyString);
times = 5;
mockedHttpRequest.send();
times = 1;
mockedTwinParser.updateTwin(anyString);
times = 1;
Deencapsulation.invoke(mockedDevice, "getTwinParser");
times = 4;
mockedTwinParser.getTagsMap();
times = 1;
Deencapsulation.invoke(mockedDevice, "setTags", testMap);
times = 1;
Deencapsulation.invoke(mockedDevice, "setDesiredProperties", testMap);
times = 1;
Deencapsulation.invoke(mockedDevice, "setReportedProperties", testMap);
times = 1;
}
};
}
Aggregations