Search in sources :

Example 1 with GenericModel

use of com.ibm.watson.developer_cloud.service.model.GenericModel in project java-sdk by watson-developer-cloud.

the class ErrorResponseTest method testServiceUnavailable.

/**
 * Test HTTP status code 503 (ServiceUnavailable) error response.
 */
@Test
public void testServiceUnavailable() {
    String message = "The request failed because the moon is full.";
    server.enqueue(new MockResponse().setResponseCode(503).addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON).setBody("{\"error\": \"" + message + "\"}"));
    try {
        GenericModel response = service.testMethod().execute();
    } catch (Exception e) {
        assertTrue(e instanceof ServiceUnavailableException);
        ServiceUnavailableException ex = (ServiceUnavailableException) e;
        assertEquals(503, ex.getStatusCode());
        assertEquals(message, ex.getMessage());
    }
}
Also used : GenericModel(com.ibm.watson.developer_cloud.service.model.GenericModel) MockResponse(okhttp3.mockwebserver.MockResponse) ServiceUnavailableException(com.ibm.watson.developer_cloud.service.exception.ServiceUnavailableException) ForbiddenException(com.ibm.watson.developer_cloud.service.exception.ForbiddenException) BadRequestException(com.ibm.watson.developer_cloud.service.exception.BadRequestException) TooManyRequestsException(com.ibm.watson.developer_cloud.service.exception.TooManyRequestsException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) ConflictException(com.ibm.watson.developer_cloud.service.exception.ConflictException) UnsupportedException(com.ibm.watson.developer_cloud.service.exception.UnsupportedException) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) RequestTooLargeException(com.ibm.watson.developer_cloud.service.exception.RequestTooLargeException) InternalServerErrorException(com.ibm.watson.developer_cloud.service.exception.InternalServerErrorException) ServiceUnavailableException(com.ibm.watson.developer_cloud.service.exception.ServiceUnavailableException) Test(org.junit.Test) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest)

Example 2 with GenericModel

use of com.ibm.watson.developer_cloud.service.model.GenericModel in project java-sdk by watson-developer-cloud.

the class ErrorResponseTest method testInternalServerError.

/**
 * Test HTTP status code 500 (InternalServerError) error response.
 */
@Test
public void testInternalServerError() {
    String message = "The request failed because the moon is full.";
    server.enqueue(new MockResponse().setResponseCode(500).addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON).setBody("{\"error\": \"" + message + "\"}"));
    try {
        GenericModel response = service.testMethod().execute();
    } catch (Exception e) {
        assertTrue(e instanceof InternalServerErrorException);
        InternalServerErrorException ex = (InternalServerErrorException) e;
        assertEquals(500, ex.getStatusCode());
        assertEquals(message, ex.getMessage());
    }
}
Also used : GenericModel(com.ibm.watson.developer_cloud.service.model.GenericModel) MockResponse(okhttp3.mockwebserver.MockResponse) InternalServerErrorException(com.ibm.watson.developer_cloud.service.exception.InternalServerErrorException) ForbiddenException(com.ibm.watson.developer_cloud.service.exception.ForbiddenException) BadRequestException(com.ibm.watson.developer_cloud.service.exception.BadRequestException) TooManyRequestsException(com.ibm.watson.developer_cloud.service.exception.TooManyRequestsException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) ConflictException(com.ibm.watson.developer_cloud.service.exception.ConflictException) UnsupportedException(com.ibm.watson.developer_cloud.service.exception.UnsupportedException) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) RequestTooLargeException(com.ibm.watson.developer_cloud.service.exception.RequestTooLargeException) InternalServerErrorException(com.ibm.watson.developer_cloud.service.exception.InternalServerErrorException) ServiceUnavailableException(com.ibm.watson.developer_cloud.service.exception.ServiceUnavailableException) Test(org.junit.Test) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest)

Example 3 with GenericModel

use of com.ibm.watson.developer_cloud.service.model.GenericModel in project java-sdk by watson-developer-cloud.

the class ErrorResponseTest method testUnsupported.

/**
 * Test HTTP status code 415 (Unsupported Media Type) error response.
 */
@Test
public void testUnsupported() {
    String message = "The request failed because the moon is full.";
    server.enqueue(new MockResponse().setResponseCode(415).addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON).setBody("{\"error\": \"" + message + "\"}"));
    try {
        GenericModel response = service.testMethod().execute();
    } catch (Exception e) {
        assertTrue(e instanceof UnsupportedException);
        UnsupportedException ex = (UnsupportedException) e;
        assertEquals(415, ex.getStatusCode());
        assertEquals(message, ex.getMessage());
    }
}
Also used : GenericModel(com.ibm.watson.developer_cloud.service.model.GenericModel) MockResponse(okhttp3.mockwebserver.MockResponse) UnsupportedException(com.ibm.watson.developer_cloud.service.exception.UnsupportedException) ForbiddenException(com.ibm.watson.developer_cloud.service.exception.ForbiddenException) BadRequestException(com.ibm.watson.developer_cloud.service.exception.BadRequestException) TooManyRequestsException(com.ibm.watson.developer_cloud.service.exception.TooManyRequestsException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) ConflictException(com.ibm.watson.developer_cloud.service.exception.ConflictException) UnsupportedException(com.ibm.watson.developer_cloud.service.exception.UnsupportedException) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) RequestTooLargeException(com.ibm.watson.developer_cloud.service.exception.RequestTooLargeException) InternalServerErrorException(com.ibm.watson.developer_cloud.service.exception.InternalServerErrorException) ServiceUnavailableException(com.ibm.watson.developer_cloud.service.exception.ServiceUnavailableException) Test(org.junit.Test) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest)

Example 4 with GenericModel

use of com.ibm.watson.developer_cloud.service.model.GenericModel in project java-sdk by watson-developer-cloud.

the class ErrorResponseTest method testBadRequest.

/**
 * Test HTTP status code 400 (Bad Request) error response.
 */
@Test
public void testBadRequest() {
    String message = "The request failed because the moon is full.";
    server.enqueue(new MockResponse().setResponseCode(400).addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON).setBody("{\"error\": \"" + message + "\"}"));
    try {
        GenericModel response = service.testMethod().execute();
    } catch (Exception e) {
        assertTrue(e instanceof BadRequestException);
        BadRequestException ex = (BadRequestException) e;
        assertEquals(400, ex.getStatusCode());
        assertEquals(message, ex.getMessage());
    }
}
Also used : GenericModel(com.ibm.watson.developer_cloud.service.model.GenericModel) MockResponse(okhttp3.mockwebserver.MockResponse) BadRequestException(com.ibm.watson.developer_cloud.service.exception.BadRequestException) ForbiddenException(com.ibm.watson.developer_cloud.service.exception.ForbiddenException) BadRequestException(com.ibm.watson.developer_cloud.service.exception.BadRequestException) TooManyRequestsException(com.ibm.watson.developer_cloud.service.exception.TooManyRequestsException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) ConflictException(com.ibm.watson.developer_cloud.service.exception.ConflictException) UnsupportedException(com.ibm.watson.developer_cloud.service.exception.UnsupportedException) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) RequestTooLargeException(com.ibm.watson.developer_cloud.service.exception.RequestTooLargeException) InternalServerErrorException(com.ibm.watson.developer_cloud.service.exception.InternalServerErrorException) ServiceUnavailableException(com.ibm.watson.developer_cloud.service.exception.ServiceUnavailableException) Test(org.junit.Test) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest)

Example 5 with GenericModel

use of com.ibm.watson.developer_cloud.service.model.GenericModel in project java-sdk by watson-developer-cloud.

the class ErrorResponseTest method testUnauthorized.

/**
 * Test HTTP status code 401 (Unauthorized) error response.
 */
@Test
public void testUnauthorized() {
    String message = "The request failed because the moon is full.";
    server.enqueue(new MockResponse().setResponseCode(401).addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON).setBody("{\"error\": \"" + message + "\"}"));
    try {
        GenericModel response = service.testMethod().execute();
    } catch (Exception e) {
        assertTrue(e instanceof UnauthorizedException);
        UnauthorizedException ex = (UnauthorizedException) e;
        assertEquals(401, ex.getStatusCode());
        assertTrue(ex.getMessage().startsWith("Unauthorized: Access is denied due to invalid credentials."));
    }
}
Also used : GenericModel(com.ibm.watson.developer_cloud.service.model.GenericModel) MockResponse(okhttp3.mockwebserver.MockResponse) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) ForbiddenException(com.ibm.watson.developer_cloud.service.exception.ForbiddenException) BadRequestException(com.ibm.watson.developer_cloud.service.exception.BadRequestException) TooManyRequestsException(com.ibm.watson.developer_cloud.service.exception.TooManyRequestsException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) ConflictException(com.ibm.watson.developer_cloud.service.exception.ConflictException) UnsupportedException(com.ibm.watson.developer_cloud.service.exception.UnsupportedException) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) RequestTooLargeException(com.ibm.watson.developer_cloud.service.exception.RequestTooLargeException) InternalServerErrorException(com.ibm.watson.developer_cloud.service.exception.InternalServerErrorException) ServiceUnavailableException(com.ibm.watson.developer_cloud.service.exception.ServiceUnavailableException) Test(org.junit.Test) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest)

Aggregations

WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)10 BadRequestException (com.ibm.watson.developer_cloud.service.exception.BadRequestException)10 ConflictException (com.ibm.watson.developer_cloud.service.exception.ConflictException)10 ForbiddenException (com.ibm.watson.developer_cloud.service.exception.ForbiddenException)10 InternalServerErrorException (com.ibm.watson.developer_cloud.service.exception.InternalServerErrorException)10 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)10 RequestTooLargeException (com.ibm.watson.developer_cloud.service.exception.RequestTooLargeException)10 ServiceUnavailableException (com.ibm.watson.developer_cloud.service.exception.ServiceUnavailableException)10 TooManyRequestsException (com.ibm.watson.developer_cloud.service.exception.TooManyRequestsException)10 UnauthorizedException (com.ibm.watson.developer_cloud.service.exception.UnauthorizedException)10 UnsupportedException (com.ibm.watson.developer_cloud.service.exception.UnsupportedException)10 GenericModel (com.ibm.watson.developer_cloud.service.model.GenericModel)10 MockResponse (okhttp3.mockwebserver.MockResponse)10 Test (org.junit.Test)10