Search in sources :

Example 1 with UnauthorizedException

use of com.ibm.watson.developer_cloud.service.exception.UnauthorizedException 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)

Example 2 with UnauthorizedException

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

the class CustomizationsIT method testDeleteVoiceModel.

/**
 * Test delete voice model.
 */
@Test
public void testDeleteVoiceModel() {
    model = createVoiceModel();
    DeleteVoiceModelOptions deleteOptions = new DeleteVoiceModelOptions.Builder().customizationId(model.getCustomizationId()).build();
    service.deleteVoiceModel(deleteOptions).execute();
    try {
        GetVoiceModelOptions getOptions = new GetVoiceModelOptions.Builder().customizationId(model.getCustomizationId()).build();
        service.getVoiceModel(getOptions).execute();
        fail("deleting customization failed");
    } catch (UnauthorizedException e) {
    // success!
    }
}
Also used : GetVoiceModelOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.GetVoiceModelOptions) DeleteVoiceModelOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.DeleteVoiceModelOptions) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Aggregations

UnauthorizedException (com.ibm.watson.developer_cloud.service.exception.UnauthorizedException)2 Test (org.junit.Test)2 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)1 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)1 BadRequestException (com.ibm.watson.developer_cloud.service.exception.BadRequestException)1 ConflictException (com.ibm.watson.developer_cloud.service.exception.ConflictException)1 ForbiddenException (com.ibm.watson.developer_cloud.service.exception.ForbiddenException)1 InternalServerErrorException (com.ibm.watson.developer_cloud.service.exception.InternalServerErrorException)1 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)1 RequestTooLargeException (com.ibm.watson.developer_cloud.service.exception.RequestTooLargeException)1 ServiceUnavailableException (com.ibm.watson.developer_cloud.service.exception.ServiceUnavailableException)1 TooManyRequestsException (com.ibm.watson.developer_cloud.service.exception.TooManyRequestsException)1 UnsupportedException (com.ibm.watson.developer_cloud.service.exception.UnsupportedException)1 GenericModel (com.ibm.watson.developer_cloud.service.model.GenericModel)1 DeleteVoiceModelOptions (com.ibm.watson.developer_cloud.text_to_speech.v1.model.DeleteVoiceModelOptions)1 GetVoiceModelOptions (com.ibm.watson.developer_cloud.text_to_speech.v1.model.GetVoiceModelOptions)1 MockResponse (okhttp3.mockwebserver.MockResponse)1