Example usage for org.springframework.http ResponseEntity getStatusCode

List of usage examples for org.springframework.http ResponseEntity getStatusCode

Introduction

In this page you can find the example usage for org.springframework.http ResponseEntity getStatusCode.

Prototype

public HttpStatus getStatusCode() 

Source Link

Document

Return the HTTP status code of the response.

Usage

From source file:com.example.api.DatabaseServiceImpl.java

@NotNull
public Optional<Message> getMessageById(final long messageId) {
    try {//from  w ww .j  a v  a 2  s .c  om
        final ResponseEntity<Message> entity = restTemplate
                .getForEntity("http://localhost:5000/messageEntity/" + messageId, Message.class);
        if (entity.getStatusCode().is2xxSuccessful()) {
            return Optional.ofNullable(entity.getBody());
        }
        return Optional.empty();
    } catch (RestClientException e) {
        return Optional.empty();
    }
}

From source file:eu.freme.common.exception.ExceptionHandlerService.java

public void writeExceptionToResponse(HttpServletRequest request, HttpServletResponse response,
        Throwable exception) throws IOException {
    ResponseEntity<String> responseEntity = handleError(request, exception);
    response.setStatus(responseEntity.getStatusCode().value());
    response.getWriter().write(responseEntity.getBody());
    response.flushBuffer();/* w w w  . j  av  a 2s. co m*/
}

From source file:org.shaigor.rest.retro.service.security.IntegrationTest.java

/**
 * Access resource for given URI and user
 * @param uri resource URI to access/*from  www . ja  va  2s.  c om*/
 * @param formData 
 * @param pwd the password of the user
 * @throws IOException 
 */
protected void testPostResourceAccess(URIInfo testUri, MultiValueMap<String, String> formData)
        throws IOException {
    ResponseEntity<String> response = helper.postForString(testUri.getUri(), formData);

    assertEquals(HttpStatus.OK, response.getStatusCode());
    int expiry = context.getAccessToken().getExpiresIn();
    assertTrue("Expiry not overridden in config: " + expiry, expiry > 1500 && expiry <= EXPIRY_SEC);
    assertEquals("Rigth method should be called.", testUri.getResponse(), response.getHeaders().getETag());
}

From source file:org.zaizi.SensefySearchUiApplicationTests.java

@Test
public void homePageLoads() {
    ResponseEntity<String> response = template.getForEntity("http://localhost:" + port + "/", String.class);
    assertEquals(HttpStatus.FOUND, response.getStatusCode());
}

From source file:sample.RestTests.java

@Test
public void authenticateWithBasicWorks() {
    String auth = getAuth("user", "password");
    HttpHeaders headers = getHttpHeaders();
    headers.set(AUTHORIZATION, BASIC + auth);
    ResponseEntity<User> entity = getForUser(this.baseUrl + "/", headers, User.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(entity.getHeaders().containsKey(X_AUTH_TOKEN)).isTrue();
    assertThat(entity.getBody().getUsername()).isEqualTo("user");
}

From source file:biz.epa.jsp.SampleWebJspApplicationTests.java

@Test
public void testJspWithEl() throws Exception {
    ResponseEntity<String> entity = new TestRestTemplate().getForEntity("http://localhost:" + this.port,
            String.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("/resources/text.txt"));
}

From source file:comsat.sample.jpa.SampleJpaApplicationTests.java

@Test
public void testHome() throws Exception {
    ResponseEntity<String> entity = new TestRestTemplate().getForEntity("http://localhost:" + this.port,
            String.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    // TODO Check that //tbody/tr count is 4
}

From source file:org.lecture.unit.tutorial.controller.TutorialControllerUnitTest.java

@Test
public void patchShouldPatchADocumentWithNewContent() throws Exception {
    String original = new String(Files.readAllBytes(Paths.get(getClass().getResource("/original.md").toURI())));

    Tutorial instance = new Tutorial();
    instance.setId("1");
    instance.setContent(original);//from   w w w. ja  va 2  s . c  o  m
    instance.setFormat("MARKDOWN");
    TutorialResource testResource = new TutorialResource(instance);
    when(tutorialRepository.findOne("1")).thenReturn(instance);

    String modified = new String(Files.readAllBytes(Paths.get(getClass().getResource("/modified.md").toURI())));
    String patch = new DmpPatchService().createPatch(original, modified);
    ResponseEntity<?> response = testInstance.update("1", patch);

    assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());

    assertEquals(instance.getContent(), modified);

}

From source file:sample.RestTests.java

@Test(expected = HttpClientErrorException.class)
public void unauthenticatedUserSentToLogInPage() {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    ResponseEntity<String> entity = getForUser(this.baseUrl + "/", headers, String.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
}

From source file:com.github.ibm.domino.client.CalendarClientTest.java

@Test
public void test1PostEvents() {
    System.out.println("postEvents");
    DominoRestClient instance = initClient();
    CalendarEventsWrapper events = new CalendarEventsWrapper();
    CalendarEvent event = new CalendarEvent();
    event.setSummary("This is a new event");
    event.setLocation("here");
    ZonedDateTime zdt = ZonedDateTime.now().plusDays(10);
    event.getStart().setDateTime(zdt);//w  w  w.j  ava 2 s .  com
    event.getEnd().setDateTime(zdt.plusHours(2));
    events.getEvents().add(event);
    ResponseEntity<Object> response = instance.postEvent(events);
    assertTrue(response.getStatusCode().is2xxSuccessful());
}