Search in sources :

Example 36 with NonStrictExpectations

use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.

the class HttpsConnectionTest method readInputFailsIfCannotAccessInputStream.

// Tests_SRS_HTTPSCONNECTION_11_012: [The function shall throw an IOException if the input stream could not be accessed.]
@Test(expected = IOException.class)
public void readInputFailsIfCannotAccessInputStream() throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getInputStream();
            result = new IOException();
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.connect();
    conn.readInput();
}
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 37 with NonStrictExpectations

use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.

the class HttpsConnectionTest method setReadTimeoutSetsRequestTimeout.

// Tests_SRS_HTTPSCONNECTION_11_023: [The function shall set the read timeout to the given value.]
@Test
public void setReadTimeoutSetsRequestTimeout() throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.POST;
    final String field = "test-field";
    final String value = "test-value";
    final int timeout = 1;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.setReadTimeoutMillis(timeout);
    final int expectedTimeout = timeout;
    new Verifications() {

        {
            mockUrl.openConnection().setReadTimeout(expectedTimeout);
        }
    };
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 38 with NonStrictExpectations

use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.

the class HttpsConnectionTest method readErrorClosesErrorStream.

// Tests_SRS_HTTPSCONNECTION_11_020: [The function shall close the error stream after it has been completely read.]
@Test
public void readErrorClosesErrorStream(@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.getErrorStream();
            result = mockIs;
            mockIs.read();
            result = -1;
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.connect();
    conn.readError();
    new Verifications() {

        {
            mockIs.close();
        }
    };
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 39 with NonStrictExpectations

use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.

the class HttpsConnectionTest method connectStreamsRequestBody.

// Tests_SRS_HTTPSCONNECTION_11_004: [The function shall stream the request body, if present, through the connection.]
// Tests_SRS_HTTPSCONNECTION_11_009: [The function shall save the body to be sent with the request.]
@Test
public void connectStreamsRequestBody() throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.PUT;
    byte[] body = { 1, 2, 3 };
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.writeOutput(body);
    body[0] = 5;
    conn.connect();
    final byte[] expectedBody = { 1, 2, 3 };
    new Verifications() {

        {
            mockUrl.openConnection().getOutputStream().write(expectedBody);
        }
    };
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 40 with NonStrictExpectations

use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.

the class HttpsConnectionTest method writeOutputDoesNotFailWhenBodyIsEmpty.

// Tests_SRS_HTTPSCONNECTION_11_010: [The function shall throw an IllegalArgumentException if the request does not currently use method POST or PUT and the body is non-empty.]
@Test
public void writeOutputDoesNotFailWhenBodyIsEmpty() throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    final byte[] body = new byte[0];
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.writeOutput(body);
}
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