Example usage for org.springframework.boot.test.web.client TestRestTemplate TestRestTemplate

List of usage examples for org.springframework.boot.test.web.client TestRestTemplate TestRestTemplate

Introduction

In this page you can find the example usage for org.springframework.boot.test.web.client TestRestTemplate TestRestTemplate.

Prototype

TestRestTemplate

Source Link

Usage

From source file:com.crazyacking.learn.spring.actuator.ManagementPortAndPathSampleActuatorApplicationTests.java

@Test
public void testMetrics() {
    testHome(); // makes sure some requests have been made
    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> entity = new TestRestTemplate()
            .getForEntity("http://localhost:" + this.managementPort + "/admin/metrics", Map.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
}

From source file:de.codecentric.boot.admin.AdminApplicationTest.java

@Test
public void testReverseProxy() {
    String apiBaseUrl = "http://localhost:" + port + "/api/applications";

    Application application = Application.create("TestApp")
            .withHealthUrl("http://localhost:" + port + "/health").withManagementUrl("http://localhost:" + port)
            .build();//from  w  w w  . j a v  a  2  s.co m
    ResponseEntity<Application> entity = new TestRestTemplate().postForEntity(apiBaseUrl, application,
            Application.class);

    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> app = new TestRestTemplate().getForEntity(apiBaseUrl + "/" + entity.getBody().getId(),
            Map.class);
    assertEquals(HttpStatus.OK, app.getStatusCode());
    assertEquals("TestApp", app.getBody().get("name"));

    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> info = new TestRestTemplate()
            .getForEntity(apiBaseUrl + "/" + entity.getBody().getId() + "/info", Map.class);
    assertEquals(HttpStatus.OK, info.getStatusCode());

    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> health = new TestRestTemplate()
            .getForEntity(apiBaseUrl + "/" + entity.getBody().getId() + "/health", Map.class);
    assertEquals(HttpStatus.OK, health.getStatusCode());

}

From source file:com.crazyacking.learn.spring.actuator.ManagementPortSampleActuatorApplicationTests.java

@Test
public void testHealth() {
    ResponseEntity<String> entity = new TestRestTemplate().withBasicAuth("user", getPassword())
            .getForEntity("http://localhost:" + this.managementPort + "/actuator/health", String.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(entity.getBody()).contains("\"status\":\"UP\"");
    assertThat(entity.getBody()).contains("\"example\"");
    assertThat(entity.getBody()).contains("\"counter\":42");
}

From source file:org.springframework.cloud.netflix.zuul.ZuulProxyApplicationTests.java

@Test
public void getHasCorrectTransferEncoding() {
    ResponseEntity<String> result = new TestRestTemplate()
            .getForEntity("http://localhost:" + this.port + "/simple/transferencoding", String.class);
    assertEquals(HttpStatus.OK, result.getStatusCode());
    assertEquals("missing", result.getBody());
}

From source file:com.crazyacking.learn.spring.actuator.ManagementPortAndPathSampleActuatorApplicationTests.java

@Test
public void testHealth() {
    ResponseEntity<String> entity = new TestRestTemplate().withBasicAuth("user", getPassword())
            .getForEntity("http://localhost:" + this.managementPort + "/admin/health", String.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(entity.getBody()).isEqualTo("{\"status\":\"UP\"}");
}

From source file:be.ordina.msdashboard.MicroservicesDashboardServerApplicationTest.java

@Test
public void exposesGraph() throws IOException, InterruptedException {
    long startTime = System.currentTimeMillis();
    @SuppressWarnings("rawtypes")
    ResponseEntity<String> graph = new TestRestTemplate().getForEntity("http://localhost:" + port + "/graph",
            String.class);
    long totalTime = System.currentTimeMillis() - startTime;
    assertThat(HttpStatus.OK).isEqualTo(graph.getStatusCode());
    String body = removeBlankNodes(graph.getBody());
    // System.out.println("BODY: " + body);
    System.out.println("Time spent waiting for /graph: " + totalTime);

    JSONAssert.assertEquals(/*from w w w .  j a  v a  2s  .c  o m*/
            removeBlankNodes(
                    load("src/test/resources/MicroservicesDashboardServerApplicationTestGraphResponse.json")),
            body, JSONCompareMode.LENIENT);

    ObjectMapper m = new ObjectMapper();
    Map<String, List> r = m.readValue(body, Map.class);
    // printLinks(r);
    assertLinkBetweenIds(r, "svc1:svc1rsc1", "service1");
    assertLinkBetweenIds(r, "svc1:svc1rsc2", "service1");
    assertLinkBetweenIds(r, "svc1:svc1rsc3", "service1");
    assertLinkBetweenIds(r, "svc3:svc3rsc1", "service3");
    assertLinkBetweenIds(r, "svc3:svc3rsc2", "service3");
    assertLinkBetweenIds(r, "svc4:svc4rsc1", "service4");
    assertLinkBetweenIds(r, "svc4:svc4rsc2", "service4");
    assertLinkBetweenIds(r, "service1", "backend2");
    assertLinkBetweenIds(r, "service1", "discoveryComposite");
    assertLinkBetweenIds(r, "service1", "backend1");
    assertLinkBetweenIds(r, "service1", "svc3:svc3rsc1");
    assertLinkBetweenIds(r, "service1", "svc4:svc4rsc1");
    assertLinkBetweenIds(r, "service3", "discoveryComposite");
    assertLinkBetweenIds(r, "service3", "backend1");
    assertLinkBetweenIds(r, "service3", "backend3");
    assertLinkBetweenIds(r, "service3", "backend4");
    assertLinkBetweenIds(r, "service4", "discoveryComposite");
    assertLinkBetweenIds(r, "service4", "backend4");
    assertLinkBetweenIds(r, "service4", "backend10");
    assertLinkBetweenIds(r, "service4", "loyalty-program");
    assertLinkBetweenIds(r, "service4", "backend9");
    assertLinkBetweenIds(r, "service4", "db");
    assertThat(((List<Map>) r.get(LINKS)).size()).isEqualTo(22);

    ResponseEntity<String> errors = new TestRestTemplate().getForEntity("http://localhost:" + port + "/events",
            String.class);

    assertThat(HttpStatus.OK).isEqualTo(errors.getStatusCode());
    body = errors.getBody();
    // System.out.println("BODY: " + body);
    JSONAssert.assertEquals(
            load("src/test/resources/MicroservicesDashboardServerApplicationTestEventsResponse.json"), body,
            JSONCompareMode.LENIENT);
}

From source file:org.springframework.cloud.netflix.zuul.ZuulProxyApplicationTests.java

@Test
public void postHasCorrectTransferEncoding() {
    ResponseEntity<String> result = new TestRestTemplate().postForEntity(
            "http://localhost:" + this.port + "/simple/transferencoding", new HttpEntity<>("hello"),
            String.class);
    assertEquals(HttpStatus.OK, result.getStatusCode());
    assertEquals("missing", result.getBody());
}

From source file:org.springframework.boot.test.web.client.TestRestTemplateTests.java

@Test
public void simple() {
    // The Apache client is on the classpath so we get the fully-fledged factory
    assertThat(new TestRestTemplate().getRestTemplate().getRequestFactory())
            .isInstanceOf(HttpComponentsClientHttpRequestFactory.class);
}

From source file:org.springframework.boot.test.web.client.TestRestTemplateTests.java

@Test
public void withBasicAuthAddsBasicAuthInterceptorWhenNotAlreadyPresent() {
    TestRestTemplate originalTemplate = new TestRestTemplate();
    TestRestTemplate basicAuthTemplate = originalTemplate.withBasicAuth("user", "password");
    assertThat(basicAuthTemplate.getRestTemplate().getMessageConverters())
            .containsExactlyElementsOf(originalTemplate.getRestTemplate().getMessageConverters());
    assertThat(basicAuthTemplate.getRestTemplate().getRequestFactory())
            .isInstanceOf(InterceptingClientHttpRequestFactory.class);
    assertThat(ReflectionTestUtils.getField(basicAuthTemplate.getRestTemplate().getRequestFactory(),
            "requestFactory")).isInstanceOf(CustomHttpComponentsClientHttpRequestFactory.class);
    assertThat(basicAuthTemplate.getRestTemplate().getUriTemplateHandler())
            .isSameAs(originalTemplate.getRestTemplate().getUriTemplateHandler());
    assertThat(basicAuthTemplate.getRestTemplate().getInterceptors()).hasSize(1);
    assertBasicAuthorizationInterceptorCredentials(basicAuthTemplate, "user", "password");
}

From source file:org.springframework.cloud.netflix.hystrix.HystrixStreamEndpointTests.java

@Test
public void hystrixStreamWorks() throws Exception {
    String url = "http://localhost:" + port;
    // you have to hit a Hystrix circuit breaker before the stream sends anything
    ResponseEntity<String> response = new TestRestTemplate().getForEntity(url, String.class);
    assertEquals("bad response code", HttpStatus.OK, response.getStatusCode());

    URL hystrixUrl = new URL(url + "/admin/hystrix.stream");

    List<String> data = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        try (InputStream in = hystrixUrl.openStream()) {
            byte[] buffer = new byte[1024];
            in.read(buffer);//from  w w  w. j  a v  a2s. c o m
            data.add(new String(buffer));
        } catch (Exception e) {
            log.error("Error getting hystrix stream, try " + i, e);
        }
    }

    for (String item : data) {
        if (item.contains("data:")) {
            return; // test passed
        }
    }
    fail("/hystrix.stream didn't contain 'data:' was " + data);
}