Search in sources :

Example 46 with NonStrictExpectations

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

the class HttpsConnectionTest method constructorOpensConnection.

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

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
        }
    };
    new HttpsConnection(mockUrl, httpsMethod);
    new Verifications() {

        {
            mockUrl.openConnection();
        }
    };
}
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 47 with NonStrictExpectations

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

the class HttpsConnectionTest method setRequestHeaderSetsRequestHeader.

// Tests_SRS_HTTPSCONNECTION_11_008: [The function shall set the given request header field.]
@Test
public void setRequestHeaderSetsRequestHeader() throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.POST;
    final String field = "test-field";
    final String value = "test-value";
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.setRequestHeader(field, value);
    new Verifications() {

        {
            mockUrl.openConnection().setRequestProperty(field, value);
        }
    };
}
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 48 with NonStrictExpectations

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

the class HttpsConnectionTest method constructorSetsRequestMethod.

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

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

        {
            mockUrl.openConnection();
        }
    };
}
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 49 with NonStrictExpectations

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

the class HttpsBatchMessageTest method addMessageSetsBase64Correctly.

// Tests_SRS_HTTPSBATCHMESSAGE_11_004: [The JSON object shall have the field "base64Encoded" set to whether the raw message was Base64-encoded.]
@Test
public void addMessageSetsBase64Correctly(@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 expectedMsgBase64 = "\"base64Encoded\":" + Boolean.toString(isBase64Encoded);
    assertThat(testBatchBody, containsString(expectedMsgBase64));
}
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 50 with NonStrictExpectations

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

the class HttpsConnectionTest method readErrorFailsIfCannotAccessErrorStream.

// Tests_SRS_HTTPSCONNECTION_11_014: [The function shall throw an IOException if the error stream could not be accessed.]
@Test(expected = IOException.class)
public void readErrorFailsIfCannotAccessErrorStream() 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 = new IOException();
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.connect();
    conn.readError();
}
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)

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