Search in sources :

Example 71 with NonStrictExpectations

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

the class HttpConnectionTest method constructorOpensConnection.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_001: [The constructor shall open a connection to the given URL.]
@Test
public void constructorOpensConnection() throws IOException {
    // Arrange
    final HttpMethod httpMethod = HttpMethod.PUT;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
        }
    };
    // Act
    new HttpConnection(mockUrl, httpMethod);
    // Assert
    new Verifications() {

        {
            mockUrl.openConnection();
        }
    };
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 72 with NonStrictExpectations

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

the class HttpConnectionTest method connectStreamsRequestBody.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_006: [The function shall stream the request body, if present, through the connection.]
@Test
public void connectStreamsRequestBody() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.PUT;
    byte[] body = { 1, 2, 3 };
    final byte[] expectedBody = { 1, 2, 3 };
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    // Act
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.writeOutput(body);
    body[0] = 5;
    conn.connect();
    // Assert
    new Verifications() {

        {
            mockUrl.openConnection().getOutputStream().write(expectedBody);
        }
    };
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 73 with NonStrictExpectations

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

the class HttpConnectionTest method getResponseStatusFailsIfDidNotReceiveResponse.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_021: [The function shall throw an IOException if no response was received.]
// Assert
@Test(expected = IOException.class)
public void getResponseStatusFailsIfDidNotReceiveResponse(@Mocked final InputStream mockIs) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getResponseCode();
            result = new IOException();
        }
    };
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.connect();
    // Act
    conn.getResponseStatus();
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) IOException(java.io.IOException) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 74 with NonStrictExpectations

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

the class HttpConnectionTest method getResponseStatusReturnsResponseStatus.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_020: [The function shall return the response status code.]
@Test
public void getResponseStatusReturnsResponseStatus(@Mocked final InputStream mockIs) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    final int status = 204;
    int expectedStatus = status;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getResponseCode();
            result = status;
        }
    };
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.connect();
    // Act
    int testStatus = conn.getResponseStatus();
    // Assert
    assertThat(testStatus, is(expectedStatus));
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 75 with NonStrictExpectations

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

the class HttpConnectionTest method constructorSetsRequestMethod.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_003: [The constructor shall set the HTTPS method to the given method.]
@Test
public void constructorSetsRequestMethod() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.PUT;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    // Act
    new HttpConnection(mockUrl, httpsMethod);
    // Assert
    new Verifications() {

        {
            mockUrl.openConnection();
        }
    };
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Aggregations

NonStrictExpectations (mockit.NonStrictExpectations)476 Test (org.junit.Test)470 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