Search in sources :

Example 6 with ProvisioningSasToken

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

the class ContractApiHttpTest method requestCreatesSasToken.

/* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_005: [The request shall create a SAS token based on the connection string.*/
@Test
public void requestCreatesSasToken() 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 7 with ProvisioningSasToken

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

the class ContractApiHttpTest method requestThrowsOnHttpRequestFailed.

/* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_011: [If the request get problem creating the HttpRequest, it shall throw ProvisioningServiceClientTransportException.*/
@Test(expected = ProvisioningServiceClientTransportException.class)
public void requestThrowsOnHttpRequestFailed() 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 = new IOException();
        }
    };
    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) IOException(java.io.IOException) ContractApiHttp(com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp) URL(java.net.URL) Test(org.junit.Test)

Example 8 with ProvisioningSasToken

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

the class ContractApiHttp method request.

/**
 * This function sends a raw information to the Device Provisioning Service service using http protocol.
 * <p>
 *    The purpose of this function is be the base communication between the controllers and the
 *    Service, and should be used only if you have full understanding of the Device Provisioning Service rest APIs.
 *    We highly recommend that you uses the APis under <b>{@link ProvisioningServiceClient}</b>
 *    instead of directly access the rest API using this class.
 * </p>
 *
 * @param httpMethod is the http verb in the request (GET, POST, PUT, DELETE, PATCH).
 * @param path is the path to the resource in the service that will compose the URL.
 * @param headerParameters is a list of pairs key values that contains optional parameters in the http header.
 * @param payload is the body of the message.
 * @return the {@code HttpResponse} that contains the response of the request.
 * @throws ProvisioningServiceClientTransportException if the Service Client failed to exchange http messages with the Provisioning Service.
 * @throws ProvisioningServiceClientException if the Provisioning Service response contains an error message.
 * @throws IllegalArgumentException if the provided parameters are not correct.
 */
public synchronized HttpResponse request(HttpMethod httpMethod, String path, Map<String, String> headerParameters, String payload) throws ProvisioningServiceClientException {
    /* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_005: [The request shall create a SAS token based on the connection string.*/
    /* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_006: [If the request get problem to create the SAS token, it shall throw IllegalArgumentException.*/
    String sasTokenString = new ProvisioningSasToken(this.provisioningConnectionString).toString();
    /* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_007: [The request shall create a HTTP URL based on the Device Registration path.*/
    URL url = getUrlForPath(path);
    /* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_010: [The request shall create a new HttpRequest.*/
    HttpRequest request = createRequest(url, httpMethod, headerParameters, payload.getBytes(StandardCharsets.UTF_8), sasTokenString);
    /* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_014: [The request shall send the request to the Device Provisioning Service service by using the HttpRequest.send().*/
    /* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_015: [If the HttpRequest failed send the message, the request shall throw ProvisioningServiceClientTransportException, threw by the callee.*/
    HttpResponse httpResponse;
    try {
        httpResponse = request.send();
    } catch (IOException e) {
        throw new ProvisioningServiceClientTransportException(e);
    }
    /* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_016: [If the Device Provisioning Service service respond to the HttpRequest with any error code, the request shall throw the appropriated ProvisioningServiceClientException, by calling ProvisioningServiceClientExceptionManager.responseVerification().*/
    ProvisioningServiceClientExceptionManager.httpResponseVerification(httpResponse.getStatus(), new String(httpResponse.getErrorReason(), StandardCharsets.UTF_8));
    return httpResponse;
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) ProvisioningServiceClientTransportException(com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientTransportException) ProvisioningSasToken(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken) HttpResponse(com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse) ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) IOException(java.io.IOException) URL(java.net.URL)

Example 9 with ProvisioningSasToken

use of com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken 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 10 with ProvisioningSasToken

use of com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken 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)

Aggregations

ProvisioningSasToken (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken)14 Test (org.junit.Test)13 ContractApiHttp (com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp)9 URL (java.net.URL)7 HttpRequest (com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest)6 ProvisioningConnectionString (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString)5 Base64.encodeBase64String (org.apache.commons.codec.binary.Base64.encodeBase64String)3 IOException (java.io.IOException)2 HttpResponse (com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse)1 ProvisioningServiceClientException (com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientException)1 ProvisioningServiceClientTransportException (com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientTransportException)1 MalformedURLException (java.net.MalformedURLException)1 URLEncoder (java.net.URLEncoder)1 Mac (javax.crypto.Mac)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1 Expectations (mockit.Expectations)1