use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.
the class HttpsConnectionTest method getResponseStatusFailsIfDidNotReceiveResponse.
// Tests_SRS_HTTPSCONNECTION_11_016: [The function shall throw an IOException if no response was received.]
@Test(expected = IOException.class)
public void getResponseStatusFailsIfDidNotReceiveResponse(@Mocked final InputStream mockIs) throws IOException {
final HttpsMethod httpsMethod = HttpsMethod.GET;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
mockUrlConn.getResponseCode();
result = new IOException();
}
};
HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
conn.connect();
conn.getResponseStatus();
}
use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.
the class HttpsBatchMessageTest method testAddMessage.
// Tests_SRS_HTTPSBATCHMESSAGE_11_008: [If adding the message causes the batched message to exceed 256 kb in size, the function shall throw a SizeLimitExceededException.]
// Tests_SRS_HTTPSBATCHMESSAGE_11_009: [If the function throws a SizeLimitExceedException, the batched message shall remain as if the message was never added.]
@Test
public void testAddMessage(@Mocked final HttpsSingleMessage mockMsg) throws SizeLimitExceededException {
// Note: this will currently result on a message size of 261154 bytes, considering the extra attributes contained on the json-serialized message.
// Note: so the current body size limit alone actually is (255 * 1024 - 36) bytes.
final byte[] validSizeBody = new byte[255 * 1024 - 1];
new NonStrictExpectations() {
{
mockMsg.getBodyAsString();
result = new String(validSizeBody, Message.DEFAULT_IOTHUB_MESSAGE_CHARSET);
}
};
boolean httpsBatchMessageSizeLimitVerified = false;
try {
HttpsBatchMessage batchMsg = new HttpsBatchMessage();
batchMsg.addMessage(mockMsg);
} catch (SizeLimitExceededException ex) {
httpsBatchMessageSizeLimitVerified = true;
}
assertThat(httpsBatchMessageSizeLimitVerified, is(true));
}
use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.
the class HttpsBatchMessageTest method addMessageSetsBodyCorrectly.
// Tests_SRS_HTTPSBATCHMESSAGE_11_002: [The function shall add the message as a JSON object appended to the current JSON array.]
// Tests_SRS_HTTPSBATCHMESSAGE_11_003: [The JSON object shall have the field "body" set to the raw message.]
@Test
public void addMessageSetsBodyCorrectly(@Mocked final HttpsSingleMessage mockMsg) throws SizeLimitExceededException {
final String msgBody = "test-msg-body";
final boolean isBase64Encoded = false;
new NonStrictExpectations() {
{
mockMsg.getBodyAsString();
result = msgBody;
mockMsg.isBase64Encoded();
result = isBase64Encoded;
}
};
HttpsBatchMessage batchMsg = new HttpsBatchMessage();
batchMsg.addMessage(mockMsg);
String testBatchBody = new String(batchMsg.getBody(), UTF8).replaceAll("\\s", "");
final String expectedMsgBody = "\"body\":\"" + msgBody + "\"";
assertThat(testBatchBody, containsString(expectedMsgBody));
}
use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.
the class HttpsConnectionTest method getResponseHeadersReturnsResponseHeaders.
// Tests_SRS_HTTPSCONNECTION_11_017: [The function shall return a mapping of header field names to the values associated with the header field name.]
@Test
public void getResponseHeadersReturnsResponseHeaders() throws IOException {
final HttpsMethod httpsMethod = HttpsMethod.GET;
final String field0 = "test-field0";
final String value0 = "test-value0";
final String field1 = "test-field1";
final String value1 = "test-value1";
final List<String> values0 = new LinkedList<>();
values0.add(value0);
final List<String> values1 = new LinkedList<>();
values1.add(value1);
final Map<String, List<String>> responseHeaders = new HashMap<>();
responseHeaders.put(field0, values0);
responseHeaders.put(field1, values1);
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
mockUrlConn.getHeaderFields();
result = responseHeaders;
}
};
HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
conn.connect();
Map<String, List<String>> testResponseHeaders = conn.getResponseHeaders();
final Map<String, List<String>> expectedResponseHeaders = responseHeaders;
assertThat(testResponseHeaders.size(), is(expectedResponseHeaders.size()));
// the list of values for each field is of size 1, so the lists
// can be directly compared.
assertThat(testResponseHeaders.get(field0), is(expectedResponseHeaders.get(field0)));
assertThat(testResponseHeaders.get(field1), is(expectedResponseHeaders.get(field1)));
}
use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.
the class HttpsConnectionTest method readErrorReturnsEmptyErrorReasonIfNoErrorReason.
// Tests_SRS_HTTPSCONNECTION_11_013: [The function shall read from the error stream and return the response.]
@Test
public void readErrorReturnsEmptyErrorReasonIfNoErrorReason() throws IOException {
final HttpsMethod httpsMethod = HttpsMethod.GET;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
mockUrlConn.getErrorStream();
result = null;
}
};
HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
conn.connect();
byte[] testError = conn.readError();
byte[] expectedError = {};
assertThat(testError, is(expectedError));
}
Aggregations