Search in sources :

Example 1 with IotHubBadFormatException

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

the class DeviceOperationsTest method invoke_throwOnhttpResponseVerification_failed.

/* Tests_SRS_DEVICE_OPERATIONS_21_016: [If the resulted HttpResponseStatus represents fail, the request shall throw proper Exception by calling httpResponseVerification.] */
@Test(expected = IotHubBadFormatException.class)
public void invoke_throwOnhttpResponseVerification_failed(@Mocked IotHubServiceSasToken iotHubServiceSasToken, @Mocked HttpRequest httpRequest) throws Exception {
    //arrange
    final int status = 400;
    final byte[] body = { 1 };
    final Map<String, List<String>> headerFields = new HashMap<>();
    final byte[] errorReason = "{\"ExceptionMessage\":\"This is the error message\"}".getBytes();
    HttpResponse sendResponse = new HttpResponse(status, body, headerFields, errorReason);
    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 = httpRequest;
            httpRequest.setHeaderField(CONTENT_TYPE, ACCEPT_VALUE + "; " + ACCEPT_CHARSET);
            result = httpRequest;
            httpRequest.send();
            result = sendResponse;
            IotHubExceptionManager.httpResponseVerification(sendResponse);
            result = new IotHubBadFormatException();
        }
    };
    //act
    HttpResponse response = DeviceOperations.request(IOT_HUB_CONNECTION_STRING, new URL(STANDARD_URL), HttpMethod.POST, STANDARD_PAYLOAD, STANDARD_REQUEST_ID, 0);
}
Also used : IotHubBadFormatException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubBadFormatException) HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) List(java.util.List) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) URL(java.net.URL) Test(org.junit.Test)

Example 2 with IotHubBadFormatException

use of com.microsoft.azure.sdk.iot.service.exceptions.IotHubBadFormatException 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!"));
    }
}
Also used : IotHubBadFormatException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubBadFormatException) HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) List(java.util.List) Test(org.junit.Test)

Example 3 with IotHubBadFormatException

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

the class IotHubExceptionManagerTest method httpResponseVerification_400_withEmptyErrorReason.

// 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_withEmptyErrorReason() throws IotHubException {
    // Arrange
    final int status = 400;
    final byte[] body = { 1 };
    final Map<String, List<String>> headerFields = new HashMap<>();
    final byte[] errorReason = "{\"ExceptionMessage\":}".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!"));
    }
}
Also used : IotHubBadFormatException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubBadFormatException) HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) List(java.util.List) Test(org.junit.Test)

Example 4 with IotHubBadFormatException

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

the class IotHubExceptionManagerTest method httpResponseVerification_400_withErrorReason.

// 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_withErrorReason() throws IotHubException {
    // Arrange
    final int status = 400;
    final byte[] body = { 1 };
    final Map<String, List<String>> headerFields = new HashMap<>();
    final byte[] errorReason = "{\"ExceptionMessage\":\"This is the error message\"}".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! This is the error message"));
    }
}
Also used : IotHubBadFormatException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubBadFormatException) HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) List(java.util.List) Test(org.junit.Test)

Aggregations

IotHubBadFormatException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubBadFormatException)4 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Test (org.junit.Test)4 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)1 URL (java.net.URL)1