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);
}
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
}
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;
}
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
}
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);
}
Aggregations