Search in sources :

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

/* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_007: [The request shall create a HTTP URL based on the Device Registration path.*/
@Test
public void requestCreatesURL() throws ProvisioningServiceClientException, IOException {
    // arrange
    requestNonStrictExpectations();
    ContractApiHttp contractApiHttp = ContractApiHttp.createFromConnectionString(mockedProvisioningConnectionString);
    String expectedURL = "https://" + VALID_HOST_NAME + "/" + VALID_PATH + "?api-version=" + SDKUtils.getServiceApiVersion();
    // act
    contractApiHttp.request(HttpMethod.PUT, VALID_PATH, VALID_HEADER, VALID_PAYLOAD);
    // assert
    new Verifications() {

        {
            new URL(expectedURL);
            times = 1;
        }
    };
}
Also used : ContractApiHttp(com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp) ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) URL(java.net.URL) Test(org.junit.Test)

Example 8 with ContractApiHttp

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

the class QueryTest method constructorThrowsOnTargetPathNull.

/* SRS_QUERY_21_002: [The constructor shall throw IllegalArgumentException if the provided targetPath is null or empty.] */
@Test(expected = IllegalArgumentException.class)
public void constructorThrowsOnTargetPathNull() {
    // arrange
    final ContractApiHttp contractApiHttp = mockedContractApiHttp;
    final String targetPath = null;
    final QuerySpecification querySpecification = mockedQuerySpecification;
    final int pageSize = 10;
    // act
    Deencapsulation.newInstance(Query.class, new Class[] { ContractApiHttp.class, String.class, QuerySpecification.class, Integer.class }, contractApiHttp, targetPath, querySpecification, pageSize);
// assert
}
Also used : ContractApiHttp(com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp) Test(org.junit.Test)

Example 9 with ContractApiHttp

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

the class QueryTest method nextSendWithoutPageSizeOrContinuationTokenIntoHttpHeader.

/* SRS_QUERY_21_012: [If the pageSize is not 0, the next shall send the Http request with `x-ms-max-item-count=[pageSize]` in the header.] */
/* SRS_QUERY_21_013: [If the continuationToken is not null or empty, the next shall send the Http request with `x-ms-continuation=[continuationToken]` in the header.] */
@Test
public void nextSendWithoutPageSizeOrContinuationTokenIntoHttpHeader() throws ProvisioningServiceClientException {
    // arrange
    final ContractApiHttp contractApiHttp = mockedContractApiHttp;
    final String targetPath = "enrollments";
    final String queryPath = targetPath + "/query";
    final QuerySpecification querySpecification = mockedQuerySpecification;
    final String querySpecificationJson = "validJson";
    final Map<String, String> headersSend = new HashMap<>();
    final Map<String, String> headersResult = new HashMap<String, String>() {

        {
            put("x-ms-item-type", "enrollment");
        }
    };
    new StrictExpectations() {

        {
            mockedQuerySpecification.toJson();
            result = querySpecificationJson;
            mockedContractApiHttp.request(HttpMethod.POST, queryPath, headersSend, querySpecificationJson);
            times = 1;
            result = mockedHttpResponse;
            mockedHttpResponse.getBody();
            result = "result".getBytes(StandardCharsets.UTF_8);
            mockedHttpResponse.getHeaderFields();
            result = headersResult;
            times = 1;
        }
    };
    Query query = Deencapsulation.newInstance(Query.class, new Class[] { ContractApiHttp.class, String.class, QuerySpecification.class, Integer.class }, contractApiHttp, targetPath, querySpecification, 0);
    // act
    query.next();
// assert
}
Also used : HashMap(java.util.HashMap) ContractApiHttp(com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp) Test(org.junit.Test)

Example 10 with ContractApiHttp

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

the class QueryTest method nextSucceedOnNullType.

/* SRS_QUERY_21_016: [The next shall create and return a new instance of the QueryResult using the `x-ms-item-type` as type, `x-ms-continuation` as the next continuationToken, and the message body.] */
@Test
public void nextSucceedOnNullType() throws ProvisioningServiceClientException {
    // arrange
    final ContractApiHttp contractApiHttp = mockedContractApiHttp;
    final String targetPath = "enrollments";
    final String queryPath = targetPath + "/query";
    final QuerySpecification querySpecification = mockedQuerySpecification;
    final String querySpecificationJson = "validJson";
    final String continuationToken = "validToken";
    final String type = "validToken";
    final String bodyResult = "result";
    final Map<String, String> headersSend = new HashMap<>();
    final Map<String, String> headersResult = new HashMap<String, String>() {

        {
            put("x-ms-item-type", null);
            put("x-ms-continuation", continuationToken);
        }
    };
    new StrictExpectations() {

        {
            mockedQuerySpecification.toJson();
            result = querySpecificationJson;
            mockedContractApiHttp.request(HttpMethod.POST, queryPath, headersSend, querySpecificationJson);
            result = mockedHttpResponse;
            mockedHttpResponse.getBody();
            result = bodyResult.getBytes(StandardCharsets.UTF_8);
            mockedHttpResponse.getHeaderFields();
            result = headersResult;
            Deencapsulation.newInstance(QueryResult.class, new Class[] { String.class, String.class, String.class }, null, bodyResult, continuationToken);
        }
    };
    Query query = Deencapsulation.newInstance(Query.class, new Class[] { ContractApiHttp.class, String.class, QuerySpecification.class, Integer.class }, contractApiHttp, targetPath, querySpecification, 0);
    // act
    QueryResult queryResult = query.next();
    // assert
    assertNotNull(queryResult);
    assertTrue(query.hasNext());
}
Also used : HashMap(java.util.HashMap) 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