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

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

Introduction

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

Prototype

public HttpClientErrorException(HttpStatus statusCode) 

Source Link

Document

Constructor with a status code only.

Usage

From source file:hu.fnf.devel.wishbox.gateway.GatewayREST.java

@RequestMapping(value = "/persistence/**", method = RequestMethod.GET)
public @ResponseBody String proxy(HttpServletRequest request) {
    String req = "";
    StringBuilder content = new StringBuilder();
    try {/*from www. ja  va2s . c om*/
        while (request.getReader().ready()) {
            content.append(request.getReader().readLine());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    req = "user";
    try {
        req = request.getRequestURL().toString().split("/gateway/persistence")[1];
    } catch (ArrayIndexOutOfBoundsException e) {
        req = "/";
    }
    System.out.println(request.getRequestURL());

    System.out.println("Proxy call: " + req);
    System.out.println("Conent: " + content);

    RestTemplate restTemplate = new RestTemplate();
    try {
        return restTemplate.getForObject("http://localhost:9090/" + req, String.class);
    } catch (HttpClientErrorException e) {
        throw new HttpClientErrorException(e.getStatusCode());
    }
}

From source file:com.ns.retailmgr.connector.gmaps.GMapConnectorTest.java

@Test(expected = RuntimeException.class)
public void test_getLngLatByAddress_Failure() {
    when(restTemplate.getForObject(anyString(), eq(GeoCodeLocInfo.class)))
            .thenThrow(new HttpClientErrorException(HttpStatus.BAD_REQUEST));
    gmapConnector.getLngLatByAddress("TestShop");
}

From source file:org.openlmis.fulfillment.service.referencedata.BaseReferenceDataServiceTest.java

@Test
public void shouldReturnNullIfEntityCannotBeFoundById() throws Exception {
    // given/*w  w w .  j a va2 s .c  o m*/
    BaseReferenceDataService<T> service = prepareService();
    UUID id = UUID.randomUUID();

    // when
    when(restTemplate.exchange(any(URI.class), eq(HttpMethod.GET), any(HttpEntity.class),
            eq(service.getResultClass()))).thenThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND));

    T found = service.findOne(id);

    // then
    verify(restTemplate).exchange(uriCaptor.capture(), eq(HttpMethod.GET), entityCaptor.capture(),
            eq(service.getResultClass()));

    URI uri = uriCaptor.getValue();
    String url = service.getServiceUrl() + service.getUrl() + id;

    assertThat(uri.toString(), is(equalTo(url)));
    assertThat(found, is(nullValue()));

    assertAuthHeader(entityCaptor.getValue());
    assertThat(entityCaptor.getValue().getBody(), is(nullValue()));
}

From source file:org.ow2.proactive.scheduling.api.graphql.service.AuthenticationServiceTest.java

@Test
public void testAuthenticateInvalidSessionId() {
    HttpClientErrorException e = new HttpClientErrorException(HttpStatus.NOT_FOUND);
    when(restTemplate.getForObject(any(String.class), eq(String.class))).thenThrow(e);
    try {/*from  ww w.  j  a  v a2  s  .  c o m*/
        authenticationService.authenticate("sessionId");
    } catch (Exception ex) {
        assertThat(ex.getMessage().contains("session ID is invalid"), is(true));
    }
}

From source file:org.trustedanalytics.h2oscoringengine.publisher.http.FilesDownloaderTest.java

@Test
public void download_serverResponse401_excpetionWithProperMessageThrown() throws IOException {
    // given/*w  ww . j a va  2 s.  c om*/
    FilesDownloader downloader = new FilesDownloader(testCredentials, restTemplateMock);
    String expectedExceptionMessage = "Login to " + testCredentials.getHost() + " with credentials "
            + new String(Base64.decodeBase64(testCredentials.getBasicAuthToken().getBytes())) + " failed";

    // when
    when(restTemplateMock.exchange(testCredentials.getHost() + testResource, HttpMethod.GET,
            HttpCommunication.basicAuthRequest(testCredentials.getBasicAuthToken()), byte[].class))
                    .thenThrow(new HttpClientErrorException(HttpStatus.UNAUTHORIZED));

    // then
    thrown.expect(IOException.class);
    thrown.expectMessage(expectedExceptionMessage);
    downloader.download(testResource, testPath);
}

From source file:org.openlmis.fulfillment.service.stockmanagement.StockEventStockManagementServiceTest.java

@Test(expected = ExternalApiException.class)
public void shouldThrowExceptionOnBadRequest() throws IOException {
    when(objectMapper.readValue(anyString(), eq(LocalizedMessageDto.class)))
            .thenReturn(new LocalizedMessageDto());
    when(restTemplate.exchange(any(URI.class), eq(HttpMethod.POST), any(HttpEntity.class), eq(UUID.class)))
            .thenThrow(new HttpClientErrorException(HttpStatus.BAD_REQUEST));

    service.submit(new StockEventDto());
}

From source file:access.test.PiazzaEnvironmentTests.java

@Test
public void testResourcesDoNotExist() {
    Mockito.when(this.restTemplate.exchange(Mockito.endsWith("/workspaces/piazza.json"),
            Mockito.eq(HttpMethod.GET), Mockito.any(), Mockito.eq(String.class)))
            .thenThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND));
    Mockito.when(this.restTemplate.exchange(Mockito.endsWith("/workspaces"), Mockito.eq(HttpMethod.POST),
            Mockito.any(), Mockito.eq(String.class)))
            .thenReturn(new ResponseEntity<String>("{\"result\": \"all good\"}", HttpStatus.OK));
    Mockito.when(this.restTemplate.exchange(Mockito.endsWith("/workspaces/piazza.xml"),
            Mockito.eq(HttpMethod.PUT), Mockito.any(), Mockito.eq(String.class)))
            .thenReturn(new ResponseEntity<String>("{\"result\": \"all good\"}", HttpStatus.OK));

    Mockito.when(this.restTemplate.exchange(Mockito.endsWith("/datastores/piazza.json"),
            Mockito.eq(HttpMethod.GET), Mockito.any(), Mockito.eq(String.class)))
            .thenReturn(new ResponseEntity<String>("{invalid JSON}", HttpStatus.OK));
    Mockito.when(this.restTemplate.exchange(Mockito.endsWith("/datastores"), Mockito.eq(HttpMethod.POST),
            Mockito.any(), Mockito.eq(String.class)))
            .thenReturn(new ResponseEntity<String>("{\"result\": \"all good\"}", HttpStatus.OK));

    piazzaEnvironment.initializeEnvironment();
    assertTrue(true); // no error occurred
}

From source file:org.openlmis.fulfillment.service.stockmanagement.StockEventStockManagementServiceTest.java

@Test(expected = DataRetrievalException.class)
public void shouldReturnDataRetrievalExceptionOnOtherErrorResponseCodes() throws IOException {
    when(restTemplate.exchange(any(URI.class), eq(HttpMethod.POST), any(HttpEntity.class), eq(UUID.class)))
            .thenThrow(new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR));

    service.submit(new StockEventDto());
}

From source file:org.trustedanalytics.h2oscoringengine.publisher.http.FilesDownloaderTest.java

@Test
public void download_serverResponse404_excpetionWithProperMessageThrown() throws IOException {
    // given/*from  w w w. j av  a2 s .c  o  m*/
    FilesDownloader downloader = new FilesDownloader(testCredentials, restTemplateMock);
    String expectedExceptionMessage = "Resource not found.";

    // when
    when(restTemplateMock.exchange(testCredentials.getHost() + testResource, HttpMethod.GET,
            HttpCommunication.basicAuthRequest(testCredentials.getBasicAuthToken()), byte[].class))
                    .thenThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND));

    // then
    thrown.expect(IOException.class);
    thrown.expectMessage(expectedExceptionMessage);
    downloader.download(testResource, testPath);
}

From source file:org.openlmis.fulfillment.service.referencedata.BaseReferenceDataServiceTest.java

@Test(expected = DataRetrievalException.class)
public void shouldThrowExceptionIfThereIsOtherProblemWithFindingById() throws Exception {
    // given/*from   w w  w.j  a v  a 2  s  .  c o  m*/
    BaseReferenceDataService<T> service = prepareService();
    UUID id = UUID.randomUUID();

    // when
    when(restTemplate.exchange(any(URI.class), eq(HttpMethod.GET), any(HttpEntity.class),
            eq(service.getResultClass()))).thenThrow(new HttpClientErrorException(HttpStatus.BAD_REQUEST));

    service.findOne(id);
}