use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class IotHubExceptionManagerTest method httpResponseVerification_502.
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_21_008: [The function shall throw IotHubBadGatewayException if the Http response status equal 502]
// Assert
@Test(expected = IotHubBadGatewayException.class)
public void httpResponseVerification_502() throws IotHubException {
// Arrange
final int status = 502;
final byte[] body = { 1 };
final Map<String, List<String>> headerFields = new HashMap<>();
final byte[] errorReason = { 2, 3, 4, 5 };
HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
// Act
IotHubExceptionManager.httpResponseVerification(response);
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class IotHubExceptionManagerTest method httpResponseVerification_400_withNULLErrorReason.
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_21_013: [If the httpresponse contains a reason message, the function must print this reason in the error message]
// Assert
@Test
public void httpResponseVerification_400_withNULLErrorReason() throws IotHubException {
// Arrange
final int status = 400;
final byte[] body = { 1 };
final Map<String, List<String>> headerFields = new HashMap<>();
final byte[] errorReason = "{\"ExceptionMessage\":null}".getBytes();
HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
// Act
try {
IotHubExceptionManager.httpResponseVerification(response);
assert true;
} catch (IotHubBadFormatException expected) {
// Expected throw.
assertThat(expected.getMessage(), is("Bad message format!"));
}
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class DeviceTwin method getTwin.
/**
* This method retrieves device twin for the specified device.
*
* @param device The device with a valid id for which device twin is to be retrieved.
* @throws IOException This exception is thrown if the IO operation failed
* @throws IotHubException This exception is thrown if the response verification failed
*/
public void getTwin(DeviceTwinDevice device) throws IotHubException, IOException {
if (device == null || device.getDeviceId() == null || device.getDeviceId().length() == 0) {
/*
**Codes_SRS_DEVICETWIN_25_004: [** The function shall throw IllegalArgumentException if the input device is null or if deviceId is null or empty **]**
*/
throw new IllegalArgumentException("Instantiate a device and set device id to be used");
}
/*
**Codes_SRS_DEVICETWIN_25_005: [** The function shall build the URL for this operation by calling getUrlTwin **]**
*/
URL url = this.iotHubConnectionString.getUrlTwin(device.getDeviceId());
/*
**Codes_SRS_DEVICETWIN_25_006: [** The function shall create a new SAS token **]**
**Codes_SRS_DEVICETWIN_25_007: [** The function shall create a new HttpRequest with http method as Get **]**
**Codes_SRS_DEVICETWIN_25_008: [** The function shall set the following HTTP headers specified in the IotHub DeviceTwin doc.
1. Key as authorization with value as sastoken
2. Key as request id with a new string value for every request
3. Key as User-Agent with value specified by the clientIdentifier and its version
4. Key as Accept with value as application/json
5. Key as Content-Type and value as application/json
6. Key as charset and value as utf-8
7. Key as If-Match and value as '*' **]**
**Codes_SRS_DEVICETWIN_25_009: [** The function shall send the created request and get the response **]**
**Codes_SRS_DEVICETWIN_25_010: [** The function shall verify the response status and throw proper Exception **]**
*/
HttpResponse response = DeviceOperations.request(this.iotHubConnectionString, url, HttpMethod.GET, new byte[0], String.valueOf(requestId++), 0);
String twin = new String(response.getBody(), StandardCharsets.UTF_8);
/*
**Codes_SRS_DEVICETWIN_25_011: [** The function shall deserialize the payload by calling updateTwin Api on the twin object **]**
*/
device.getTwinParser().updateTwin(twin);
/*
**Codes_SRS_DEVICETWIN_25_012: [** The function shall set tags, desired property map, reported property map on the user device **]**
*/
device.setTags(device.getTwinParser().getTagsMap());
device.setDesiredProperties(device.getTwinParser().getDesiredPropertyMap());
device.setReportedProperties(device.getTwinParser().getReportedPropertyMap());
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class DeviceOperationsTest method request_nullUrl_failed.
/* Tests_SRS_DEVICE_OPERATIONS_21_002: [The request shall throw IllegalArgumentException if the provided `url` is null.] */
@Test(expected = IllegalArgumentException.class)
public void request_nullUrl_failed() throws Exception {
//arrange
final IotHubConnectionString iotHubConnectionString = IotHubConnectionStringBuilder.createConnectionString(STANDARD_CONNECTIONSTRING);
//act
HttpResponse response = DeviceOperations.request(IOT_HUB_CONNECTION_STRING, null, HttpMethod.POST, STANDARD_PAYLOAD, STANDARD_REQUEST_ID, 0);
//assert
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class DeviceOperationsTest method invoke_throwOnAccept_failed.
/* Tests_SRS_DEVICE_OPERATIONS_21_013: [The request shall add to the HTTP header a `Accept` key with `application/json`.] */
@Test(expected = IllegalArgumentException.class)
public void invoke_throwOnAccept_failed(@Mocked IotHubServiceSasToken iotHubServiceSasToken, @Mocked HttpRequest httpRequest) throws Exception {
//arrange
new NonStrictExpectations() {
{
iotHubServiceSasToken.toString();
result = STANDARD_SASTOKEN_STRING;
httpRequest.setReadTimeoutMillis(DEFAULT_HTTP_TIMEOUT_MS);
result = httpRequest;
httpRequest.setHeaderField(AUTHORIZATION, STANDARD_SASTOKEN_STRING);
result = httpRequest;
httpRequest.setHeaderField(REQUEST_ID, STANDARD_REQUEST_ID);
result = httpRequest;
httpRequest.setHeaderField(USER_AGENT, TransportUtils.getJavaServiceClientIdentifier() + TransportUtils.getServiceVersion());
result = httpRequest;
httpRequest.setHeaderField(ACCEPT, ACCEPT_VALUE);
result = new IllegalArgumentException();
}
};
//act
HttpResponse response = DeviceOperations.request(IOT_HUB_CONNECTION_STRING, new URL(STANDARD_URL), HttpMethod.POST, STANDARD_PAYLOAD, STANDARD_REQUEST_ID, 0);
}
Aggregations