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.company.eleave.leave.rest.TestLeaveTypeController.java

@Test
public void testCreateLeaveType() {
    //given// w  w w  .j ava 2  s .c om
    final long newLeaveTypeId = 10l;

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

    final LeaveType leaveType = new LeaveType();
    leaveType.setComment("testComment");
    leaveType.setDefaultDaysAllowed(20);
    leaveType.setLeaveTypeName("testLeaveTypeName");

    Mockito.when(mapperMock.toEntity(leaveTypeDTO)).thenReturn(leaveType);
    Mockito.when(leaveTypeServiceMock.createNewLeaveType(leaveType)).thenReturn(newLeaveTypeId);

    //when
    ResponseEntity<Long> result = testedObject.createNewLeaveType(leaveTypeDTO);

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

From source file:com.github.ffremont.microservices.springboot.node.services.MsService.java

/**
 * Retourne un MS/*w w w.  ja  v a 2 s. c  o  m*/
 *
 * @param msName
 * @return
 */
public MicroServiceRest getMs(String msName) {
    MasterUrlBuilder builder = new MasterUrlBuilder(cluster, node, masterhost, masterPort, masterCR);
    builder.setUri(msName);

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.parseMediaType(MS_JSON_TYPE_MIME)));
    HttpEntity<MicroServiceRest> entity = new HttpEntity<>(headers);

    ResponseEntity<MicroServiceRest> response = restTempate.exchange(builder.build(), HttpMethod.GET, entity,
            MicroServiceRest.class);

    return HttpStatus.OK.equals(response.getStatusCode()) ? response.getBody() : null;
}

From source file:de.qaware.cloud.nativ.zwitscher.board.ZwitscherBoardApplicationTests.java

@Test
public void componentNameIsAvailableUsingInfoEndpoint() {
    ResponseEntity<String> responseEntity = this.testRestTemplate.getForEntity(HOST + DEFINED_PORT + "/info",
            String.class);
    assertNotNull("responseEntity is null", responseEntity);
    assertEquals("wrong status code", HttpStatus.OK, responseEntity.getStatusCode());
    assertTrue(responseEntity.getBody().contains(environment.getProperty("info.component")));
}

From source file:org.obiba.mica.core.service.MailService.java

private synchronized void sendEmail(String subject, String templateName, Map<String, String> context,
        String recipient) {//w  w  w . j  av  a 2s  . c o m
    try {
        RestTemplate template = newRestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.set(APPLICATION_AUTH_HEADER, getApplicationAuth());
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        StringBuilder form = new StringBuilder(Strings.isNullOrEmpty(recipient) ? "" : recipient + "&");
        form.append("subject=").append(encode(subject, "UTF-8")).append("&template=")
                .append(encode(templateName, "UTF-8"));
        context.forEach((k, v) -> {
            try {
                form.append("&").append(k).append("=").append(encode(v, "UTF-8"));
            } catch (UnsupportedEncodingException ignored) {
            }
        });
        HttpEntity<String> entity = new HttpEntity<>(form.toString(), headers);

        ResponseEntity<String> response = template.exchange(getNotificationsUrl(), HttpMethod.POST, entity,
                String.class);

        if (response.getStatusCode().is2xxSuccessful()) {
            log.info("Email sent via Agate");
        } else {
            log.error("Sending email via Agate failed with status: {}", response.getStatusCode());
        }
    } catch (Exception e) {
        log.error("Agate connection failure: {}", e.getMessage());
    }
}

From source file:com.fredhopper.core.connector.index.upload.impl.RestPublishingStrategy.java

@Override
public String checkStatus(final InstanceConfig config, final URI triggerUrl) throws ResponseStatusException {
    Preconditions.checkArgument(config != null);
    Preconditions.checkArgument(triggerUrl != null);

    final RestTemplate restTemplate = restTemplateProvider.createTemplate(config.getHost(), config.getPort(),
            config.getUsername(), config.getPassword());
    final URI url = triggerUrl.resolve(triggerUrl.getPath() + STATUS_PATH);

    final HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.TEXT_PLAIN);
    final HttpEntity<String> httpEntity = new HttpEntity<>(headers);

    final ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, httpEntity,
            String.class);
    final HttpStatus status = response.getStatusCode();
    if (status.equals(HttpStatus.OK)) {
        return response.getBody();
    } else {/*  ww w  . j  a  v a2s.c  o  m*/
        throw new ResponseStatusException(
                "HttpStatus " + status.toString() + " response received. Load trigger monitoring failed.");
    }
}

From source file:de.zib.gndms.gndmc.test.gorfx.GORFXClientMain.java

private void getTaskFlowStatus(String id, String type, String wid) {
    Facets facets;/*  www .  j av  a2s  .com*/
    Facet f;
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:META-INF/client-context.xml");
    TaskFlowClient tfClient = (TaskFlowClient) context.getAutowireCapableBeanFactory()
            .createBean(TaskFlowClient.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
    tfClient.setServiceURL(gndmsEpUrl);

    System.out.println("Step 6: requesting facets of task flow, retrieving status results");
    ResponseEntity<Facets> res6 = tfClient.getFacets(type, id, dn);
    // should define facets status, result, errors
    facets = res6.getBody();
    f = facets.findFacet("status");
    if (f == null) {
        System.out.println("Status facet not found");
    } else {
        System.out.println(f.getName() + " " + f.getUrl());
        ResponseEntity<TaskFlowStatus> res7 = tfClient.getStatus(type, id, dn, wid);
        System.out.println(res7.getBody().toString());
    }

    f = facets.findFacet("result");
    if (f == null) {
        System.out.println("Result facet not found");
    } else {
        System.out.println(f.getName() + " " + f.getUrl());
        ResponseEntity<Specifier<TaskResult>> res8 = tfClient.getResult(type, id, dn, wid);
        System.out.println(res8.getBody().toString());
    }

    f = facets.findFacet("errors");
    if (f == null) {
        System.out.println("Errors facet not found");
    } else {
        System.out.println(f.getName() + " " + f.getUrl());
        ResponseEntity<TaskFlowFailure> res9 = tfClient.getErrors(type, id, dn, wid);
        if (res9.getStatusCode().equals(HttpStatus.OK)) {
            System.out.println("Failure" + res9.getBody().getFailureMessage());
        } else {
            System.out.println("No errors");

        }
    }
}

From source file:org.lanqiao.examples.library.functional.BookEndpointTest.java

private String login(String user) {
    Map<String, String> map = Maps.newHashMap();
    map.put("email", user);
    map.put("password", "springside");

    ResponseEntity<Map> response = restTemplate.getForEntity(loginUrl + "?email={email}&password={password}",
            Map.class, map);
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
    return (String) response.getBody().get("token");
}

From source file:com.fredhopper.core.connector.index.upload.impl.RestPublishingStrategy.java

@Override
public URI triggerDataLoad(final InstanceConfig config, final String dataId) throws ResponseStatusException {
    Preconditions.checkArgument(config != null);
    Preconditions.checkArgument(StringUtils.isNotBlank(dataId));

    final RestTemplate restTemplate = restTemplateProvider.createTemplate(config.getHost(), config.getPort(),
            config.getUsername(), config.getPassword());

    final URI url = createUri(config.getScheme(), config.getHost(), config.getPort(), config.getServername(),
            TRIGGER_PATH, Collections.emptyList());

    final HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.TEXT_PLAIN);
    final HttpEntity<String> httpEntity = new HttpEntity<>(dataId, headers);

    final ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.PUT, httpEntity,
            String.class);
    final HttpStatus status = response.getStatusCode();
    if (status.equals(HttpStatus.CREATED)) {
        return response.getHeaders().getLocation();
    } else {/*from   ww  w  .j  a  v a2s.c om*/
        throw new ResponseStatusException(
                "HttpStatus " + status.toString() + " response received. Load trigger creation failed.");
    }
}

From source file:com.github.ffremont.microservices.springboot.node.services.MsService.java

public List<MicroServiceRest> getMicroServices() {
    MasterUrlBuilder builder = new MasterUrlBuilder(cluster, node, masterhost, masterPort, masterCR);

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.parseMediaType(MS_JSON_TYPE_MIME)));
    HttpEntity<MicroServiceRest> entity = new HttpEntity<>(headers);

    ResponseEntity<MicroServiceRest[]> response = restTempate.exchange(builder.build(), HttpMethod.GET, entity,
            MicroServiceRest[].class);

    return HttpStatus.OK.equals(response.getStatusCode()) ? new ArrayList<>(Arrays.asList(response.getBody()))
            : null;//from ww  w  .  ja  v  a 2  s  .com
}