Example usage for org.springframework.web.client HttpServerErrorException HttpServerErrorException

List of usage examples for org.springframework.web.client HttpServerErrorException HttpServerErrorException

Introduction

In this page you can find the example usage for org.springframework.web.client HttpServerErrorException HttpServerErrorException.

Prototype

public HttpServerErrorException(HttpStatus statusCode, String statusText, @Nullable byte[] body,
        @Nullable Charset charset) 

Source Link

Document

Constructor with a status code and status text, and content.

Usage

From source file:nl.gridshore.nosapi.impl.NosApiResponseErrorHandler.java

@Override
public void handleError(ClientHttpResponse response) throws IOException {
    logger.debug("Handle error '{}' received from the NOS server.", response.getStatusCode().name());
    HttpStatus statusCode = response.getStatusCode();
    MediaType contentType = response.getHeaders().getContentType();
    Charset charset = contentType != null ? contentType.getCharSet() : null;
    byte[] body = FileCopyUtils.copyToByteArray(response.getBody());

    switch (statusCode) {
    case BAD_REQUEST:
    case UNAUTHORIZED:
    case FORBIDDEN:
        throwClientException(charset, body);
    default:/* ww w .j ava 2s  .  c  om*/
        // do nothing, let the series resolving do it' work
    }

    switch (statusCode.series()) {
    case CLIENT_ERROR:
        throw new HttpClientErrorException(statusCode, response.getStatusText(), body, charset);
    case SERVER_ERROR:
        throw new HttpServerErrorException(statusCode, response.getStatusText(), body, charset);
    default:
        throw new RestClientException("Unknown status code [" + statusCode + "]");
    }
}

From source file:com.netflix.genie.web.tasks.leader.ClusterCheckerTaskUnitTests.java

/**
 * Make sure run method works./*from   w  w  w.  j a v a  2 s. c  om*/
 *
 * @throws IOException    on error
 * @throws GenieException on error
 */
@Test
public void canRun() throws IOException, GenieException {
    final String host1 = UUID.randomUUID().toString();
    final String host2 = UUID.randomUUID().toString();
    final String host3 = UUID.randomUUID().toString();

    // Mock the 9 invocations for 3 calls to run
    Mockito.when(this.restTemplate.getForObject(Mockito.anyString(), Mockito.anyObject())).thenReturn("")
            .thenThrow(new RestClientException("blah")).thenReturn("").thenReturn("")
            .thenThrow(new RestClientException("blah")).thenReturn("").thenReturn("")
            .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "",
                    ("{\"status\":\"OUT_OF_SERVICE\", \"genie\": { \"status\": \"OUT_OF_SERVICE\"}, "
                            + "\"db\": { \"status\": \"OUT_OF_SERVICE\"}}").getBytes(StandardCharsets.UTF_8),
                    StandardCharsets.UTF_8))
            .thenReturn("").thenReturn("")
            .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "",
                    ("{\"status\":\"OUT_OF_SERVICE\", \"genie\": { \"status\": \"OUT_OF_SERVICE\"}, "
                            + "\"db\": { \"status\": \"OUT_OF_SERVICE\"}}").getBytes(StandardCharsets.UTF_8),
                    StandardCharsets.UTF_8))
            .thenReturn("").thenReturn("")
            .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "",
                    ("{\"status\":\"OUT_OF_SERVICE\", \"genie\": { \"status\": \"OUT_OF_SERVICE\"}, "
                            + "\"db\": { \"status\": \"UP\"}}").getBytes(StandardCharsets.UTF_8),
                    StandardCharsets.UTF_8))
            .thenReturn("").thenReturn("")
            .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "",
                    ("{\"status\":\"OUT_OF_SERVICE\", \"genie\": { \"status\": \"OUT_OF_SERVICE\"}, "
                            + "\"db\": { \"status\": \"OUT_OF_SERVICE\"}}").getBytes(StandardCharsets.UTF_8),
                    StandardCharsets.UTF_8))
            .thenReturn("");

    final List<String> hostsRunningJobs = Lists.newArrayList(this.hostName, host1, host2, host3);
    Mockito.when(this.jobSearchService.getAllHostsWithActiveJobs()).thenReturn(hostsRunningJobs);

    final Job job1 = Mockito.mock(Job.class);
    final String job1Id = UUID.randomUUID().toString();
    Mockito.when(job1.getId()).thenReturn(Optional.of(job1Id));
    final Job job2 = Mockito.mock(Job.class);
    final String job2Id = UUID.randomUUID().toString();
    Mockito.when(job2.getId()).thenReturn(Optional.of(job2Id));
    final Job job3 = Mockito.mock(Job.class);
    final String job3Id = UUID.randomUUID().toString();
    Mockito.when(job3.getId()).thenReturn(Optional.of(job3Id));
    final Job job4 = Mockito.mock(Job.class);
    final String job4Id = UUID.randomUUID().toString();
    Mockito.when(job4.getId()).thenReturn(Optional.of(job4Id));

    Mockito.when(this.jobSearchService.getAllActiveJobsOnHost(host2)).thenReturn(Sets.newHashSet(job1, job2));
    Mockito.when(this.jobSearchService.getAllActiveJobsOnHost(host3)).thenReturn(Sets.newHashSet(job3, job4));

    Mockito.doThrow(new RuntimeException("blah")).doNothing().when(this.jobPersistenceService)
            .setJobCompletionInformation(Mockito.eq(job1Id), Mockito.eq(JobExecution.LOST_EXIT_CODE),
                    Mockito.eq(JobStatus.FAILED), Mockito.anyString(), Mockito.eq(null), Mockito.eq(null));

    this.task.run();
    Assert.assertThat(this.task.getErrorCountsSize(), Matchers.is(1));
    this.task.run();
    Assert.assertThat(this.task.getErrorCountsSize(), Matchers.is(1));
    this.task.run();
    Assert.assertThat(this.task.getErrorCountsSize(), Matchers.is(1));
    this.task.run();
    Assert.assertThat(this.task.getErrorCountsSize(), Matchers.is(0));
    this.task.run();
    Assert.assertThat(this.task.getErrorCountsSize(), Matchers.is(0));
    this.task.run();
    Assert.assertThat(this.task.getErrorCountsSize(), Matchers.is(1));

    Mockito.verify(this.jobPersistenceService, Mockito.times(2)).setJobCompletionInformation(Mockito.eq(job1Id),
            Mockito.eq(JobExecution.LOST_EXIT_CODE), Mockito.eq(JobStatus.FAILED), Mockito.anyString(),
            Mockito.eq(null), Mockito.eq(null));
    Mockito.verify(this.jobPersistenceService, Mockito.atLeast(1)).setJobCompletionInformation(
            Mockito.eq(job2Id), Mockito.eq(JobExecution.LOST_EXIT_CODE), Mockito.eq(JobStatus.FAILED),
            Mockito.anyString(), Mockito.eq(null), Mockito.eq(null));
    Mockito.verify(this.lostJobCounter, Mockito.atLeast(2)).increment();
    Mockito.verify(this.unableToUpdateJobCounter, Mockito.times(1)).increment();
}

From source file:gateway.test.EventTests.java

/**
 * Test GET /eventType/*www.ja v a  2s. c  o  m*/
 */
@Test
public void testEventTypes() {
    // Mock Response
    when(restTemplate.getForObject(anyString(), eq(String.class))).thenReturn("eventTypes");

    // Test
    ResponseEntity<?> response = eventController.getEventTypes(null, null, null, 0, 10, user);

    // Verify
    assertTrue(response.getBody().toString().equals("eventTypes"));
    assertTrue(response.getStatusCode().equals(HttpStatus.OK));

    // Test REST Exception
    when(restTemplate.getForObject(anyString(), eq(String.class)))
            .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, WORKFLOW_ERROR,
                    WORKFLOW_ERROR.getBytes(), Charset.defaultCharset()));
    response = eventController.getEventTypes(null, null, null, 0, 10, user);
    assertTrue(response.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR));

    // Test Exception
    when(restTemplate.getForObject(anyString(), eq(String.class)))
            .thenThrow(new RestClientException("event error"));
    response = eventController.getEventTypes(null, null, null, 0, 10, user);
    assertTrue(response.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR));
    assertTrue(response.getBody() instanceof ErrorResponse);
    assertTrue(((ErrorResponse) response.getBody()).message.contains("event error"));
}