Search in sources :

Example 26 with UrlPathBuilder

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder in project azure-iot-sdk-java by Azure.

the class ContractAPIHttpTest method authenticateWithDPSThrowsTransportExceptionIfAnyOfTheTransportCallsFails.

@Test(expected = ProvisioningDeviceTransportException.class)
public void authenticateWithDPSThrowsTransportExceptionIfAnyOfTheTransportCallsFails() throws ProvisioningDeviceClientException {
    final byte[] expectedPayload = "testByte".getBytes(StandardCharsets.UTF_8);
    ContractAPIHttp contractAPIHttp = createContractClass();
    new NonStrictExpectations() {

        {
            mockedRequestData.getRegistrationId();
            result = TEST_REGISTRATION_ID;
            mockedRequestData.getSslContext();
            result = mockedSslContext;
            mockedRequestData.getSasToken();
            result = null;
            new UrlPathBuilder(TEST_HOST_NAME, TEST_SCOPE_ID, ProvisioningDeviceClientTransportProtocol.HTTPS);
            result = new IOException("test IOException");
        }
    };
    // act
    try {
        contractAPIHttp.authenticateWithProvisioningService(mockedRequestData, mockedResponseCallback, null);
    } finally {
        // assert
        new Verifications() {

            {
                mockedResponseCallback.run((ResponseData) any, any);
                times = 0;
            }
        };
    }
}
Also used : UrlPathBuilder(com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder) IOException(java.io.IOException) ContractAPIHttp(com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.http.ContractAPIHttp) Test(org.junit.Test)

Example 27 with UrlPathBuilder

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder in project azure-iot-sdk-java by Azure.

the class ContractAPIHttpTest method getRegistrationStatusWithOutAuthSucceeds.

@Test
public void getRegistrationStatusWithOutAuthSucceeds() throws IOException, ProvisioningDeviceClientException {
    // arrange
    ContractAPIHttp contractAPIHttp = createContractClass();
    prepareRequestExpectations();
    new NonStrictExpectations() {

        {
            mockedRequestData.getOperationId();
            result = TEST_OPERATION_ID;
            mockedRequestData.getRegistrationId();
            result = TEST_REGISTRATION_ID;
            mockedRequestData.getSslContext();
            result = mockedSslContext;
            mockedRequestData.getSasToken();
            result = null;
            mockedHttpRequest.send();
            result = mockedHttpResponse;
        }
    };
    // act
    contractAPIHttp.getRegistrationStatus(mockedRequestData, mockedResponseCallback, null);
    // assert
    prepareRequestVerifications(HttpMethod.GET, 0);
    new Verifications() {

        {
            new UrlPathBuilder(TEST_HOST_NAME, TEST_SCOPE_ID, ProvisioningDeviceClientTransportProtocol.HTTPS);
            times = 1;
            mockedUrlPathBuilder.generateRequestUrl(TEST_REGISTRATION_ID, TEST_OPERATION_ID);
            times = 1;
            mockedHttpRequest.setSSLContext(mockedSslContext);
            times = 1;
            mockedResponseCallback.run((ResponseData) any, null);
            times = 1;
        }
    };
}
Also used : UrlPathBuilder(com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder) ContractAPIHttp(com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.http.ContractAPIHttp) Test(org.junit.Test)

Example 28 with UrlPathBuilder

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder in project azure-iot-sdk-java by Azure.

the class ContractAPIHttpTest method authenticateWithDPSWithAuthAndRetryAfterSucceeds.

@Test
public void authenticateWithDPSWithAuthAndRetryAfterSucceeds() throws IOException, ProvisioningDeviceClientException {
    // arrange
    final byte[] expectedPayload = "testByte".getBytes(StandardCharsets.UTF_8);
    ContractAPIHttp contractAPIHttp = createContractClass();
    prepareRequestExpectations();
    new NonStrictExpectations() {

        {
            mockedRequestData.getRegistrationId();
            result = TEST_REGISTRATION_ID;
            mockedRequestData.getEndorsementKey();
            result = TEST_EK;
            mockedRequestData.getStorageRootKey();
            result = TEST_SRK;
            mockedRequestData.getSslContext();
            result = mockedSslContext;
            mockedRequestData.getSasToken();
            result = TEST_SAS_TOKEN;
            mockedHttpRequest.send();
            result = mockedHttpResponse;
            mockedHttpResponse.isFieldAvailable("retry-after");
            result = true;
            mockedHttpResponse.getHeaderField("retry-after");
            result = "3";
            mockedHttpResponse.getStatus();
            result = 400;
            new DeviceRegistrationParser(anyString, anyString, anyString, anyString);
            result = mockedDeviceRegistrationParser;
            mockedDeviceRegistrationParser.toJson();
            result = "some json";
        }
    };
    // act
    contractAPIHttp.authenticateWithProvisioningService(mockedRequestData, mockedResponseCallback, null);
    // assert
    prepareRequestVerifications(HttpMethod.PUT, 1);
    new Verifications() {

        {
            new UrlPathBuilder(TEST_HOST_NAME, TEST_SCOPE_ID, ProvisioningDeviceClientTransportProtocol.HTTPS);
            times = 1;
            mockedUrlPathBuilder.generateRegisterUrl(TEST_REGISTRATION_ID);
            times = 1;
            mockedHttpRequest.setSSLContext(mockedSslContext);
            times = 1;
            mockedResponseCallback.run((ResponseData) any, null);
            times = 1;
        }
    };
}
Also used : UrlPathBuilder(com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder) DeviceRegistrationParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.DeviceRegistrationParser) ContractAPIHttp(com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.http.ContractAPIHttp) Test(org.junit.Test)

Example 29 with UrlPathBuilder

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder in project azure-iot-sdk-java by Azure.

the class ContractAPIHttp method getRegistrationStatus.

/**
 * Gets the registration status over HTTP
 * @param requestData A non {@code null} value with all the request data
 * @param responseCallback A non {@code null} value for the callback
 * @param dpsAuthorizationCallbackContext An object for context. Can be {@code null}
 * @throws ProvisioningDeviceClientException If any of the parameters are invalid ({@code null} or empty)
 * @throws ProvisioningDeviceTransportException If any of the API calls to transport fail
 * @throws ProvisioningDeviceHubException If hub responds back with status other than 300 or less.
 */
public synchronized void getRegistrationStatus(RequestData requestData, ResponseCallback responseCallback, Object dpsAuthorizationCallbackContext) throws ProvisioningDeviceClientException {
    // SRS_ContractAPIHttp_25_018: [If either operationId, registrationId, sslcontext or responseCallback is null or if operationId, registrationId is empty then this method shall throw ProvisioningDeviceClientException.]
    if (requestData.getOperationId() == null || requestData.getOperationId().isEmpty()) {
        throw new ProvisioningDeviceClientException(new IllegalArgumentException("operationId cannot be null or empty"));
    }
    if (requestData.getRegistrationId() == null || requestData.getRegistrationId().isEmpty()) {
        throw new ProvisioningDeviceClientException(new IllegalArgumentException("registration Id cannot be null or empty"));
    }
    if (requestData.getSslContext() == null) {
        throw new ProvisioningDeviceClientException(new IllegalArgumentException("sslContext cannot be null"));
    }
    if (responseCallback == null) {
        throw new ProvisioningDeviceClientException(new IllegalArgumentException("responseCallback cannot be null"));
    }
    try {
        // SRS_ContractAPIHttp_25_019: [This method shall retrieve the Url by calling generateRequestUrl on an object for UrlPathBuilder.]
        String url = new UrlPathBuilder(this.hostName, this.idScope, ProvisioningDeviceClientTransportProtocol.HTTPS).generateRequestUrl(requestData.getRegistrationId(), requestData.getOperationId());
        Map<String, String> headersMap = null;
        if (requestData.getSasToken() != null) {
            headersMap = new HashMap<>();
            headersMap.put(AUTHORIZATION, requestData.getSasToken());
        }
        // SRS_ContractAPIHttp_25_020: [This method shall prepare the GET request by setting following headers on a HttpRequest 1. User-Agent : User Agent String for the SDK 2. Accept : "application/json" 3. Content-Type: "application/json; charset=utf-8" 4. Authorization: specified sas token as authorization if a non null value is given.]
        HttpRequest httpRequest = this.prepareRequest(new URL(url), HttpMethod.GET, new byte[0], headersMap);
        // SRS_ContractAPIHttp_25_021: [This method shall set the SSLContext for the Http Request.]
        httpRequest.setSSLContext(requestData.getSslContext());
        // SRS_ContractAPIHttp_25_022: [This method shall send http request and verify the status by calling 'ProvisioningDeviceClientExceptionManager.verifyHttpResponse'.]
        // SRS_ContractAPIHttp_25_024: [If service return any other status other than < 300 then this method shall throw ProvisioningDeviceHubException.]
        HttpResponse httpResponse = this.sendRequest(httpRequest);
        // Set the retry after value from the service
        processRetryAfterValue(httpResponse);
        // SRS_ContractAPIHttp_25_023: [If service return a status as < 300 then this method shall trigger the callback to the user with the response message.]
        responseCallback.run(new ResponseData(httpResponse.getBody(), ContractState.DPS_REGISTRATION_RECEIVED, 0), dpsAuthorizationCallbackContext);
    } catch (IOException e) {
        throw new ProvisioningDeviceTransportException(e);
    }
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) UrlPathBuilder(com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder) ResponseData(com.microsoft.azure.sdk.iot.provisioning.device.internal.task.ResponseData) HttpResponse(com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

UrlPathBuilder (com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder)29 Test (org.junit.Test)25 ContractAPIHttp (com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.http.ContractAPIHttp)12 DeviceRegistrationParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.DeviceRegistrationParser)7 IOException (java.io.IOException)6 HttpRequest (com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest)3 HttpResponse (com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse)3 ResponseData (com.microsoft.azure.sdk.iot.provisioning.device.internal.task.ResponseData)3 URL (java.net.URL)3 ProvisioningDeviceClientException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)1 ProvisioningDeviceSecurityException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceSecurityException)1 TpmRegistrationResultParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.TpmRegistrationResultParser)1 SecurityProviderSymmetricKey (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey)1 SecurityProviderTpm (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderTpm)1 Base64.encodeBase64String (org.apache.commons.codec.binary.Base64.encodeBase64String)1