Search in sources :

Example 31 with ContractApiHttp

use of com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp in project azure-iot-sdk-java by Azure.

the class ContractApiHttpTest method requestThrowsOnEmptyPath.

/* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_009: [If the provided path contains not valid characters, the request shall throw IllegalArgumentException.*/
@Test(expected = IllegalArgumentException.class)
public void requestThrowsOnEmptyPath() throws ProvisioningServiceClientException, IOException {
    // arrange
    new NonStrictExpectations() {

        {
            new ProvisioningSasToken(mockedProvisioningConnectionString);
            result = mockedProvisioningSasToken;
            mockedProvisioningSasToken.toString();
            result = VALID_SASTOKEN;
            mockedProvisioningConnectionString.getHostName();
            result = VALID_HOST_NAME;
        }
    };
    ContractApiHttp contractApiHttp = ContractApiHttp.createFromConnectionString(mockedProvisioningConnectionString);
    // act
    contractApiHttp.request(HttpMethod.PUT, "", VALID_HEADER, VALID_PAYLOAD);
// assert
}
Also used : ProvisioningSasToken(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken) ContractApiHttp(com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp) Test(org.junit.Test)

Example 32 with ContractApiHttp

use of com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp in project azure-iot-sdk-java by Azure.

the class ContractApiHttpTest method requestCreatesHttpRequest.

/* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_010: [The request shall create a new HttpRequest.*/
@Test
public void requestCreatesHttpRequest() throws ProvisioningServiceClientException, IOException {
    // arrange
    new Expectations() {

        {
            new ProvisioningSasToken(mockedProvisioningConnectionString);
            result = mockedProvisioningSasToken;
            mockedProvisioningSasToken.toString();
            result = VALID_SASTOKEN;
            mockedProvisioningConnectionString.getHostName();
            result = VALID_HOST_NAME;
            new URL((String) any);
            result = mockedURL;
            new HttpRequest(mockedURL, HttpMethod.PUT, VALID_PAYLOAD.getBytes(StandardCharsets.UTF_8));
            result = mockedHttpRequest;
            mockedHttpRequest.send();
            result = mockedHttpResponse;
            mockedHttpResponse.getStatus();
            result = VALID_SUCCESS_STATUS;
            mockedHttpResponse.getErrorReason();
            result = VALID_SUCCESS_MESSAGE.getBytes(StandardCharsets.UTF_8);
            mockedHttpResponse.getBody();
            result = VALID_BODY;
            mockedHttpResponse.getHeaderFields();
            result = VALID_HEADER;
            ProvisioningServiceClientExceptionManager.httpResponseVerification(VALID_SUCCESS_STATUS, VALID_SUCCESS_MESSAGE);
        }
    };
    ContractApiHttp contractApiHttp = ContractApiHttp.createFromConnectionString(mockedProvisioningConnectionString);
    requestNonStrictExpectations();
    // act
    contractApiHttp.request(HttpMethod.PUT, VALID_PATH, VALID_HEADER, VALID_PAYLOAD);
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) ProvisioningSasToken(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken) ContractApiHttp(com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp) URL(java.net.URL) Test(org.junit.Test)

Example 33 with ContractApiHttp

use of com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp in project azure-iot-sdk-java by Azure.

the class ContractApiHttpTest method requestAddHttpHeader.

/* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_013: [The request shall add the headerParameters to the http header, if provided.] */
@Test
public void requestAddHttpHeader() throws ProvisioningServiceClientException, IOException {
    // arrange
    final Map<String, String> header = new HashMap<String, String>() {

        {
            put("key1", "val1");
            put("key2", "val2");
            put("key3", "val3");
        }
    };
    requestNonStrictExpectations();
    ContractApiHttp contractApiHttp = ContractApiHttp.createFromConnectionString(mockedProvisioningConnectionString);
    // act
    contractApiHttp.request(HttpMethod.PUT, VALID_PATH, header, VALID_PAYLOAD);
    // assert
    new Verifications() {

        {
            mockedHttpRequest.setHeaderField("key1", "val1");
            times = 1;
            mockedHttpRequest.setHeaderField("key2", "val2");
            times = 1;
            mockedHttpRequest.setHeaderField("key3", "val3");
            times = 1;
        }
    };
}
Also used : HashMap(java.util.HashMap) ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) ContractApiHttp(com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp) Test(org.junit.Test)

Example 34 with ContractApiHttp

use of com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp in project azure-iot-sdk-java by Azure.

the class ContractApiHttpTest method requestThrowsOnDeviceProvisioningServiceError.

/* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_016: [If the Device Provisioning Service respond to the HttpRequest with any error code, the request shall throw the appropriated ProvisioningServiceClientException, by calling ProvisioningServiceClientExceptionManager.responseVerification().*/
@Test(expected = ProvisioningServiceClientException.class)
public void requestThrowsOnDeviceProvisioningServiceError() throws ProvisioningServiceClientException, IOException {
    // arrange
    new NonStrictExpectations() {

        {
            new ProvisioningSasToken(mockedProvisioningConnectionString);
            result = mockedProvisioningSasToken;
            mockedProvisioningSasToken.toString();
            result = VALID_SASTOKEN;
            mockedProvisioningConnectionString.getHostName();
            result = VALID_HOST_NAME;
            new URL((String) any);
            result = mockedURL;
            new HttpRequest(mockedURL, HttpMethod.PUT, VALID_PAYLOAD.getBytes(StandardCharsets.UTF_8));
            result = mockedHttpRequest;
            mockedHttpRequest.send();
            result = mockedHttpResponse;
            mockedHttpResponse.getStatus();
            result = 401;
            mockedHttpResponse.getErrorReason();
            result = VALID_SUCCESS_MESSAGE.getBytes(StandardCharsets.UTF_8);
        }
    };
    ContractApiHttp contractApiHttp = ContractApiHttp.createFromConnectionString(mockedProvisioningConnectionString);
    // act
    contractApiHttp.request(HttpMethod.PUT, VALID_PATH, VALID_HEADER, VALID_PAYLOAD);
// assert
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) ProvisioningSasToken(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken) ContractApiHttp(com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp) URL(java.net.URL) Test(org.junit.Test)

Example 35 with ContractApiHttp

use of com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp in project azure-iot-sdk-java by Azure.

the class ContractApiHttpTest method requestCreateHttpHeader.

/* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_012: [The request shall fill the http header with the standard parameters.] */
@Test
public void requestCreateHttpHeader() throws ProvisioningServiceClientException, IOException {
    // arrange
    ContractApiHttp contractApiHttp = ContractApiHttp.createFromConnectionString(mockedProvisioningConnectionString);
    requestNonStrictExpectations();
    // act
    contractApiHttp.request(HttpMethod.PUT, VALID_PATH, VALID_HEADER, VALID_PAYLOAD);
    // assert
    new Verifications() {

        {
            mockedHttpRequest.setHeaderField("authorization", VALID_SASTOKEN);
            times = 1;
            mockedHttpRequest.setHeaderField("Request-Id", "1001");
            times = 1;
            mockedHttpRequest.setHeaderField("Accept", "application/json");
            times = 1;
            mockedHttpRequest.setHeaderField("Content-Type", "application/json");
            times = 1;
            mockedHttpRequest.setHeaderField("Content-Length", String.valueOf(VALID_PAYLOAD.length()));
            times = 1;
            mockedHttpRequest.setHeaderField("charset", "utf-8");
            times = 1;
        }
    };
}
Also used : ContractApiHttp(com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp) Test(org.junit.Test)

Aggregations

ContractApiHttp (com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp)38 Test (org.junit.Test)38 HashMap (java.util.HashMap)10 ProvisioningSasToken (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken)9 URL (java.net.URL)7 HttpRequest (com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest)5 ProvisioningConnectionString (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString)3 ProvisioningServiceClientException (com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientException)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1