Search in sources :

Example 6 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 authenticateWithDPSThrowsOnSendFailure.

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

        {
            mockedHttpRequest.send();
            result = mockedHttpResponse;
            mockedRequestData.getRegistrationId();
            result = TEST_REGISTRATION_ID;
            mockedRequestData.getEndorsementKey();
            result = null;
            mockedRequestData.getSslContext();
            result = mockedSslContext;
            mockedRequestData.getSasToken();
            result = null;
            ProvisioningDeviceClientExceptionManager.verifyHttpResponse(mockedHttpResponse);
            result = new ProvisioningDeviceHubException("test Exception");
            new DeviceRegistrationParser(anyString, anyString);
            result = mockedDeviceRegistrationParser;
            mockedDeviceRegistrationParser.toJson();
            result = "some json";
        }
    };
    // act
    contractAPIHttp.authenticateWithProvisioningService(mockedRequestData, mockedResponseCallback, null);
    // assert
    prepareRequestVerifications(HttpMethod.PUT, 0);
    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 = 0;
        }
    };
}
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 7 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 getRegistrationStatusThrowsTransportExceptionIfAnyOfTheTransportCallsFails.

@Test(expected = ProvisioningDeviceTransportException.class)
public void getRegistrationStatusThrowsTransportExceptionIfAnyOfTheTransportCallsFails() throws ProvisioningDeviceClientException {
    ContractAPIHttp contractAPIHttp = createContractClass();
    new NonStrictExpectations() {

        {
            mockedRequestData.getOperationId();
            result = TEST_OPERATION_ID;
            mockedRequestData.getRegistrationId();
            result = TEST_REGISTRATION_ID;
            mockedRequestData.getSslContext();
            result = mockedSslContext;
            mockedRequestData.getSasToken();
            result = TEST_SAS_TOKEN;
            new UrlPathBuilder(TEST_HOST_NAME, TEST_SCOPE_ID, ProvisioningDeviceClientTransportProtocol.HTTPS);
            result = new IOException("test IOException");
        }
    };
    // act
    try {
        contractAPIHttp.getRegistrationStatus(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 8 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 requestNonceWithDPSTPMSucceeds.

// SRS_ContractAPIHttp_25_004: [This method shall retrieve the Url by calling 'generateRegisterUrl' on an object for UrlPathBuilder.]
// SRS_ContractAPIHttp_25_005: [This method shall prepare the PUT 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".]
// SRS_ContractAPIHttp_25_006: [This method shall set the SSLContext for the Http Request.]
// SRS_ContractAPIHttp_25_008: [If service return a status as 404 then this method shall trigger the callback to the user with the response message.]
@Test
public void requestNonceWithDPSTPMSucceeds() 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;
            mockedHttpRequest.send();
            result = mockedHttpResponse;
            ProvisioningDeviceClientExceptionManager.verifyHttpResponse(mockedHttpResponse);
            result = new ProvisioningDeviceHubException("test Exception");
            mockedHttpResponse.getStatus();
            result = 401;
            TpmRegistrationResultParser.createFromJson(new String(mockedHttpResponse.getBody()));
            result = mockedTpmRegistrationResultParser;
            mockedTpmRegistrationResultParser.getAuthenticationKey();
            result = encodeBase64String("some auth key".getBytes(StandardCharsets.UTF_8));
            new DeviceRegistrationParser(anyString, anyString, anyString, anyString);
            result = mockedDeviceRegistrationParser;
            mockedDeviceRegistrationParser.toJson();
            result = "some json";
        }
    };
    // act
    contractAPIHttp.requestNonceForTPM(mockedRequestData, mockedResponseCallback, null);
    // assert
    prepareRequestVerifications(HttpMethod.PUT, 0);
    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) Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String) 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 9 with UrlPathBuilder

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

the class UrlPathBuilderTest method generateRegisterUrlHttpThrowsOnNullRegID.

// SRS_UrlPathBuilder_25_007: [ This method shall throw IllegalArgumentException if the registration id is null or empty. ]
@Test(expected = IllegalArgumentException.class)
public void generateRegisterUrlHttpThrowsOnNullRegID() throws IOException {
    // arrange
    UrlPathBuilder urlPathBuilder = new UrlPathBuilder(TEST_HOST_NAME, TEST_SCOPE, ProvisioningDeviceClientTransportProtocol.HTTPS);
    // act
    urlPathBuilder.generateRegisterUrl(null);
}
Also used : UrlPathBuilder(com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder) Test(org.junit.Test)

Example 10 with UrlPathBuilder

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

the class UrlPathBuilderTest method generateRequestUrlHttpSucceeds.

// SRS_UrlPathBuilder_25_010: [ This method shall create a String using the following format: HTTP - https://<HostName>/<Scope>/registrations/<Registration ID>/operations/<operationId>?api-version=<Service API Version> MQTT - TBD AMQP - TBD ]
@Test
public void generateRequestUrlHttpSucceeds() throws IOException {
    // arrange
    // https://testHostName/testScope/registrations/testRegistrationId/operations/testOperationId?api-version=2017-08-31-preview
    final String expectedUrl = "https://" + TEST_HOST_NAME + "/" + TEST_SCOPE + "/registrations/" + TEST_REGISTRATION_ID + "/operations/" + TEST_OPERATION_ID + "?api-version=" + SERVICE_API_VERSION;
    UrlPathBuilder urlPathBuilder = new UrlPathBuilder(TEST_HOST_NAME, TEST_SCOPE, ProvisioningDeviceClientTransportProtocol.HTTPS);
    // act
    String generateRequestUrl = urlPathBuilder.generateRequestUrl(TEST_REGISTRATION_ID, TEST_OPERATION_ID);
    // assert
    assertEquals(expectedUrl, generateRequestUrl);
}
Also used : UrlPathBuilder(com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder) Test(org.junit.Test)

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