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:org.venice.piazza.servicecontroller.messaging.handlers.DescribeServiceHandlerTest.java

/**
 * Test what happens when the service cannot be described
 *///w  ww.j  a  v  a 2 s. com
@Test
public void testUnsuccessful() {
    DescribeServiceMetadataJob job = new DescribeServiceMetadataJob();

    String testServiceId = "a842aae2-bd74-4c4b-9a65-c45e8cd9060";
    job.serviceID = testServiceId;
    final DescribeServiceHandler dsMock = Mockito.spy(dsHandler);
    ResponseEntity<String> responseEntity = new ResponseEntity<String>("", HttpStatus.NOT_FOUND);

    Mockito.doReturn(responseEntity).when(dsMock).handle(testServiceId);
    ResponseEntity<String> result = dsMock.handle(job);

    assertEquals("The status code should be HttpStatus.NOT_FOUND.", result.getStatusCode(),
            HttpStatus.NOT_FOUND);

}

From source file:com.work.petclinic.SampleWebUiApplicationTests.java

@Test
public void testCreateUserAndLogin() throws Exception {
    ResponseEntity<String> page = getPage("http://localhost:" + this.port + "/users");

    assertTrue("Client or server error.",
            !page.getStatusCode().is4xxClientError() && !page.getStatusCode().is5xxServerError());

    if (page.getStatusCode() == HttpStatus.FOUND) {
        page = getPage(page.getHeaders().getLocation());
    }// w  w w.  java 2 s  .  c  om

    String body = page.getBody();
    assertNotNull("Body was null", body);

    String username = "newuser";
    String password = "password";
    String formAction = getFormAction(page);

    MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
    form.set("username", username);
    form.set("email", "newuser@newuser.org");
    form.set("name", "New User");
    form.set("uiPassword", password);
    form.set("verifyPassword", password);
    form.set("_eventId_saveUser", "Create User");
    form.set("_csrf", csrfValue);

    httpHeaders.set("X-CSRF-TOKEN", csrfValue);

    page = postPage(formAction, form);

    if (page.getStatusCode() == HttpStatus.FOUND) {
        page = getPage(page.getHeaders().getLocation());
    }

    assertEquals(HttpStatus.OK, page.getStatusCode());
    body = page.getBody();
    assertNotNull("Body was null", body);
    assertTrue("User not created:\n" + body, body.contains("User " + username + " saved"));

    executeLogin(username, password);
}

From source file:com.company.eleave.leave.rest.TestAnnualBalanceLeaveController.java

@Test
public void testaddLeaveForEmployeeSuccessfuly() {
    //given//from  ww  w . j a v  a2  s .  c  o  m
    final long createdAnnualBalanceId = 10l;
    final AnnualBalanceLeaveDTO newAnnualBalanceDTO = new AnnualBalanceLeaveDTO();

    final AnnualBalanceLeave createdAnnualBalanceLeave = new AnnualBalanceLeave();
    createdAnnualBalanceLeave.setId(createdAnnualBalanceId);

    Mockito.when(employeeServiceMock.getById(EMPLOYEE_ID)).thenReturn(new Employee());
    Mockito.when(mapperMock.toEntity(newAnnualBalanceDTO)).thenReturn(createdAnnualBalanceLeave);
    Mockito.when(annualBalanceServiceMock.createLeave(createdAnnualBalanceLeave))
            .thenReturn(createdAnnualBalanceId);

    //when
    ResponseEntity<Void> result = testedObject.addLeaveForEmployee(EMPLOYEE_ID, newAnnualBalanceDTO);

    //then
    Assert.assertEquals(HttpStatus.CREATED, result.getStatusCode());
    Assert.assertEquals("/annualBalanceLeaves/10", result.getHeaders().getLocation().toString());
}

From source file:com.work.petclinic.SampleWebUiApplicationTests.java

@Test
public void testVeterinariansPage() throws Exception {
    ResponseEntity<String> page = executeLogin("user", "user");

    page = getPage("http://localhost:" + this.port + "/vets/list.html");

    assertEquals(HttpStatus.OK, page.getStatusCode());

    String body = page.getBody();
    assertNotNull("Page body is null.", body);
    assertTrue("Wrong page", body.contains("<h2>Veterinarians</h2>"));

    String vetList = body.substring(body.indexOf("<tbody>"));
    vetList = vetList.substring(0, vetList.indexOf("</tbody>"));
    assertTrue("Missing expected data.", StringUtils.countMatches(vetList, "<tr>") == 6);
}

From source file:io.jmnarloch.spring.boot.rxjava.async.ObservableDeferredResultTest.java

@Test
public void shouldRetrieveSingleValue() {

    // when/*from  ww w  .  ja  v  a  2  s .co  m*/
    ResponseEntity<List> response = restTemplate.getForEntity(path("/single"), List.class);

    // then
    assertNotNull(response);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals(Collections.singletonList("single value"), response.getBody());
}

From source file:it.reply.orchestrator.service.CmdbServiceImpl.java

@Override
public Provider getProviderById(String id) {
    ResponseEntity<Provider> response = restTemplate.getForEntity(url.concat(providerIdUrlPath).concat(id),
            Provider.class);
    if (response.getStatusCode().is2xxSuccessful()) {
        return response.getBody();
    }//from  w  w w. java2s. co m
    throw new DeploymentException("Unable to find provider <" + id + "> in the CMDB."
            + response.getStatusCode().toString() + " " + response.getStatusCode().getReasonPhrase());
}

From source file:io.jmnarloch.spring.boot.rxjava.async.ObservableDeferredResultTest.java

@Test
public void shouldRetrieveMultipleValues() {

    // when/* w  w  w  .  j a v a 2s.  c o m*/
    ResponseEntity<List> response = restTemplate.getForEntity(path("/multiple"), List.class);

    // then
    assertNotNull(response);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals(Arrays.asList("multiple", "values"), response.getBody());
}

From source file:com.expedia.client.WunderGroundClientTest.java

@Test
public void testAddReturnCorrectResponse() throws Exception {

    final String responseXml = "{\"response\": {\"version\": \"0.1\""
            + ",\"termsofService\": \"http://www.wunderground.com/weather/api/d/terms.html\""
            + ",\"features\": {\"conditions\": 1}},\"current_observation\": {" + "      \"image\": {"
            + "      \"url\":\"http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png\","
            + "      \"title\":\"Weather Underground\"," + "      \"link\":\"http://www.wunderground.com\""
            + "      }," + "      \"display_location\": {" + "      \"full\":\"Schenectady, NY\","
            + "      \"city\":\"Schenectady\"," + "      \"state\":\"NY\","
            + "      \"state_name\":\"New York\"," + "      \"country\":\"US\","
            + "      \"country_iso3166\":\"US\"," + "      \"zip\":\"12345\","
            + "      \"latitude\":\"42.81407928\"," + "      \"longitude\":\"-73.93997955\","
            + "      \"elevation\":\"78.00000000\"" + "      }," + "      \"observation_location\": {"
            + "      \"full\":\"APRSWXNET Schenectady NY US, Schenectady, New York\","
            + "      \"city\":\"APRSWXNET Schenectady NY US, Schenectady\"," + "      \"state\":\"New York\","
            + "      \"country\":\"US\"," + "      \"country_iso3166\":\"US\","
            + "      \"latitude\":\"42.817581\"," + "      \"longitude\":\"-73.927963\","
            + "      \"elevation\":\"344 ft\"" + "      }," + "      \"estimated\": {" + "      },"
            + "      \"station_id\":\"MD3701\","
            + "      \"observation_time\":\"Last Updated on June 19, 6:20 AM EDT\","
            + "      \"observation_time_rfc822\":\"Wed, 19 Jun 2013 06:20:00 -0400\","
            + "      \"observation_epoch\":\"1371637200\","
            + "      \"local_time_rfc822\":\"Wed, 19 Jun 2013 06:49:03 -0400\","
            + "      \"local_epoch\":\"1371638943\"," + "      \"local_tz_short\":\"EDT\","
            + "      \"local_tz_long\":\"America/New_York\"," + "      \"local_tz_offset\":\"-0400\","
            + "      \"weather\":\"Partly Cloudy\"," + "      \"temperature_string\":\"51 F (10.6 C)\","
            + "      \"temp_f\":51," + "      \"temp_c\":10.6," + "      \"relative_humidity\":\"91%\","
            + "      \"wind_string\":\"From the North at 1 MPH Gusting to 2.0 MPH\","
            + "      \"wind_dir\":\"North\"," + "      \"wind_degrees\":10," + "      \"wind_mph\":1,"
            + "      \"wind_gust_mph\":\"2.0\"," + "      \"wind_kph\":1.6,"
            + "      \"wind_gust_kph\":\"3.2\"," + "      \"pressure_mb\":\"1017\","
            + "      \"pressure_in\":\"30.02\"," + "      \"pressure_trend\":\"" + "\","
            + "      \"dewpoint_string\":\"48 F (9 C)\"," + "      \"dewpoint_f\":48,"
            + "      \"dewpoint_c\":9," + "      \"heat_index_string\":\"NA\","
            + "      \"heat_index_f\":\"NA\"," + "      \"heat_index_c\":\"NA\","
            + "      \"windchill_string\":\"NA\"," + "      \"windchill_f\":\"NA\","
            + "      \"windchill_c\":\"NA\"," + "      \"feelslike_string\":\"51 F (10.6 C)\","
            + "      \"feelslike_f\":\"51\"," + "      \"feelslike_c\":\"10.6\","
            + "      \"visibility_mi\":\"10.0\"," + "      \"visibility_km\":\"16.1\","
            + "      \"solarradiation\":\"97\"," + "      \"UV\":\"0\","
            + "      \"precip_1hr_string\":\"0.00 in ( 0 mm)\"," + "      \"precip_1hr_in\":\"0.00\","
            + "      \"precip_1hr_metric\":\" 0\"," + "      \"precip_today_string\":\"0.00 in (0 mm)\","
            + "      \"precip_today_in\":\"0.00\"," + "      \"precip_today_metric\":\"0\","
            + "      \"icon\":\"partlycloudy\","
            + "      \"icon_url\":\"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif\","
            + "      \"forecast_url\":\"http://www.wunderground.com/US/NY/Schenectady.html\","
            + "      \"history_url\":\"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=MD3701\","
            + "      \"ob_url\":\"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=42.817581,-73.927963\""
            + "   }" + "}" + "";

    mockServer.expect(requestTo("http://api.wunderground.com/api/ed044d75b91fb500/conditions/q/12345.json"))
            .andExpect(method(HttpMethod.GET)).andRespond(withSuccess(responseXml, MediaType.APPLICATION_JSON));
    Weather request = new Weather();
    request.setZipCode(12345);/*from  www.  j  av a2s .  c o m*/

    final ResponseEntity<ResponseWrapper> response = (ResponseEntity<ResponseWrapper>) serviceClient
            .getJSONResponse(request);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals(MediaType.APPLICATION_JSON.getSubtype(), response.getHeaders().getContentType().getSubtype());
    mockServer.verify();
}

From source file:com.work.petclinic.SampleWebUiApplicationTests.java

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

From source file:com.company.eleave.leave.rest.TestLeaveTypeController.java

@Test
public void testGetByIdSuccessfuly() {
    //given/*from  w w w  .  j av  a2 s .co m*/
    final LeaveType leaveType = new LeaveType();
    leaveType.setComment("testComment");
    leaveType.setLeaveTypeName("testLeaveTypeName");

    final LeaveTypeDTO leaveTypeDTO = new LeaveTypeDTO();
    leaveTypeDTO.setComment("testComment");
    leaveTypeDTO.setLeaveTypeName("testLeaveTypeName");

    Mockito.when(leaveTypeServiceMock.getById(LEAVE_TYPE_ID)).thenReturn(leaveType);
    Mockito.when(mapperMock.toDto(leaveType)).thenReturn(leaveTypeDTO);

    //when
    ResponseEntity<LeaveTypeDTO> result = testedObject.getById(LEAVE_TYPE_ID);

    //then
    Assert.assertEquals(HttpStatus.OK, result.getStatusCode());
    Assert.assertNotNull(result.getBody());
    Assert.assertEquals("testComment", result.getBody().getComment());
    Assert.assertEquals("testLeaveTypeName", result.getBody().getLeaveTypeName());
}