Search in sources :

Example 41 with NonStrictExpectations

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();
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) IOException(java.io.IOException) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 42 with NonStrictExpectations

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));
}
Also used : SizeLimitExceededException(javax.naming.SizeLimitExceededException) HttpsBatchMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 43 with NonStrictExpectations

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));
}
Also used : HttpsBatchMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 44 with NonStrictExpectations

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)));
}
Also used : HashMap(java.util.HashMap) HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) List(java.util.List) LinkedList(java.util.LinkedList) NonStrictExpectations(mockit.NonStrictExpectations) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 45 with NonStrictExpectations

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));
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Aggregations

NonStrictExpectations (mockit.NonStrictExpectations)492 Test (org.junit.Test)472 Verifications (mockit.Verifications)154 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)77 IOException (java.io.IOException)64 HttpConnection (com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection)51 HttpMethod (com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod)51 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)47 MessageProperty (com.microsoft.azure.sdk.iot.device.MessageProperty)32 HttpsSingleMessage (com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage)31 HttpConnection (com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection)26 HttpMethod (com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod)26 HttpsConnection (com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection)26 HttpsMethod (com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod)26 HashMap (java.util.HashMap)26 JobClient (com.microsoft.azure.sdk.iot.service.jobs.JobClient)25 IotHubConnectionString (com.microsoft.azure.sdk.iot.device.IotHubConnectionString)17 Date (java.util.Date)16 HashSet (java.util.HashSet)16 Query (com.microsoft.azure.sdk.iot.service.devicetwin.Query)15