Search in sources :

Example 1 with ProvisioningServiceClientException

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

the class ContractApiHttpTest method requestThrowsOnSendHttpRequestFailed.

/* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_015: [If the HttpRequest failed send the message, the request shall throw ProvisioningServiceClientTransportException, threw by the callee.*/
@Test(expected = ProvisioningServiceClientException.class)
public void requestThrowsOnSendHttpRequestFailed() 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 = new ProvisioningServiceClientException();
        }
    };
    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) ProvisioningServiceClientException(com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientException) Test(org.junit.Test)

Example 2 with ProvisioningServiceClientException

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

the class Query method next.

/**
 * Return the next page of result for the query.
 *
 * @return A {@link QueryResult} with the next page of items for the query.
 * @throws NoSuchElementException if the query does no have more pages to return.
 */
@Override
public QueryResult next() {
    if (!hasNext) {
        throw new NoSuchElementException("There are no more pending elements");
    }
    Map<String, String> headerParameters = new HashMap<>();
    if (pageSize != 0) {
        headerParameters.put(PAGE_SIZE_KEY, Integer.toString(pageSize));
    }
    if (!Tools.isNullOrEmpty(this.continuationToken)) {
        headerParameters.put(CONTINUATION_TOKEN_KEY, this.continuationToken);
    }
    HttpResponse httpResponse;
    try {
        httpResponse = contractApiHttp.request(HttpMethod.POST, queryPath, headerParameters, querySpecificationJson);
    } catch (ProvisioningServiceClientException e) {
        // Because Query implements the iterator interface, the next cannot throws ProvisioningServiceClientException.
        throw new IllegalArgumentException(e);
    }
    byte[] body = httpResponse.getBody();
    if (body == null) {
        throw new IllegalArgumentException("Http response for next cannot contains a null body");
    }
    String bodyStr = new String(body, StandardCharsets.UTF_8);
    Map<String, String> headers = httpResponse.getHeaderFields();
    String type = headers.get(ITEM_TYPE_KEY);
    this.continuationToken = headers.get(CONTINUATION_TOKEN_KEY);
    hasNext = (this.continuationToken != null);
    return new QueryResult(type, bodyStr, this.continuationToken);
}
Also used : QueryResult(com.microsoft.azure.sdk.iot.provisioning.service.configs.QueryResult) HttpResponse(com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse) ProvisioningServiceClientException(com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientException)

Aggregations

ProvisioningServiceClientException (com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientException)2 HttpRequest (com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest)1 HttpResponse (com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse)1 ProvisioningSasToken (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken)1 QueryResult (com.microsoft.azure.sdk.iot.provisioning.service.configs.QueryResult)1 ContractApiHttp (com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp)1 URL (java.net.URL)1 Test (org.junit.Test)1