Search in sources :

Example 71 with RestTemplate

use of org.springframework.web.client.RestTemplate in project spring-cloud-sleuth by spring-cloud.

the class TraceFilterWebIntegrationTests method should_not_create_a_span_for_error_controller.

@Test
public void should_not_create_a_span_for_error_controller() {
    try {
        new RestTemplate().getForObject("http://localhost:" + port() + "/", String.class);
        BDDAssertions.fail("should fail due to runtime exception");
    } catch (Exception e) {
    }
    then(Tracing.current().tracer().currentSpan()).isNull();
    then(this.accumulator.getSpans()).hasSize(1);
    Span fromFirstTraceFilterFlow = this.accumulator.getSpans().get(0);
    then(fromFirstTraceFilterFlow.tags()).containsEntry("http.status_code", "500").containsEntry("http.method", "GET").containsEntry("mvc.controller.class", "ExceptionThrowingController").containsEntry("error", "Request processing failed; nested exception is java.lang.RuntimeException: Throwing exception");
    // issue#714
    String hex = fromFirstTraceFilterFlow.traceId();
    String[] split = capture.toString().split("\n");
    List<String> list = Arrays.stream(split).filter(s -> s.contains("Uncaught exception thrown")).filter(s -> s.contains(hex + "," + hex + ",true]")).collect(Collectors.toList());
    then(list).isNotEmpty();
}
Also used : BDDAssertions(org.assertj.core.api.BDDAssertions) Arrays(java.util.Arrays) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Span(zipkin2.Span) DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) Sampler(brave.sampler.Sampler) After(org.junit.After) ArrayListSpanReporter(org.springframework.cloud.sleuth.util.ArrayListSpanReporter) SpringRunner(org.springframework.test.context.junit4.SpringRunner) OutputCapture(org.springframework.boot.test.rule.OutputCapture) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) RestTemplate(org.springframework.web.client.RestTemplate) Before(org.junit.Before) HttpAdapter(brave.http.HttpAdapter) Tracing(brave.Tracing) HttpSampler(brave.http.HttpSampler) EnableAutoConfiguration(org.springframework.boot.autoconfigure.EnableAutoConfiguration) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) IOException(java.io.IOException) Test(org.junit.Test) BDDAssertions.then(org.assertj.core.api.BDDAssertions.then) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) Configuration(org.springframework.context.annotation.Configuration) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) List(java.util.List) Rule(org.junit.Rule) Assertions.fail(org.assertj.core.api.Assertions.fail) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Environment(org.springframework.core.env.Environment) ResponseEntity(org.springframework.http.ResponseEntity) Pattern(java.util.regex.Pattern) Bean(org.springframework.context.annotation.Bean) RestTemplate(org.springframework.web.client.RestTemplate) Span(zipkin2.Span) IOException(java.io.IOException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 72 with RestTemplate

use of org.springframework.web.client.RestTemplate in project spring-cloud-sleuth by spring-cloud.

the class TraceUserInfoRestTemplateCustomizer method postProcessAfterInitialization.

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (bean instanceof RestTemplate) {
        RestTemplate rt = (RestTemplate) bean;
        new RestTemplateInterceptorInjector(interceptor()).inject(rt);
    }
    return bean;
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate)

Example 73 with RestTemplate

use of org.springframework.web.client.RestTemplate in project crnk-framework by crnk-project.

the class BasicSpringBootTest method testRelationshipInclusion.

@Test
public void testRelationshipInclusion() {
    Project project = new Project();
    ProjectRepository projectRepository = new ProjectRepository();
    projectRepository.save(project);
    Task task = new Task();
    task.setProject(project);
    TaskRepository taskRepository = new TaskRepository();
    taskRepository.save(task);
    RestTemplate testRestTemplate = new RestTemplate();
    ResponseEntity<String> response = testRestTemplate.getForEntity("http://localhost:" + this.port + "/api/tasks?include[tasks]=schedule%2Cproject", String.class);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    JsonFluentAssert included = assertThatJson(response.getBody()).node("included");
    included.isArray().ofLength(1);
}
Also used : JsonFluentAssert(net.javacrumbs.jsonunit.fluent.JsonFluentAssert) Project(io.crnk.test.mock.models.Project) Task(io.crnk.test.mock.models.Task) ProjectRepository(io.crnk.test.mock.repository.ProjectRepository) TaskRepository(io.crnk.test.mock.repository.TaskRepository) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 74 with RestTemplate

use of org.springframework.web.client.RestTemplate in project crnk-framework by crnk-project.

the class BasicSpringBootTest method testTestEndpointWithQueryParams.

@Test
public void testTestEndpointWithQueryParams() {
    RestTemplate testRestTemplate = new RestTemplate();
    ResponseEntity<String> response = testRestTemplate.getForEntity("http://localhost:" + this.port + "/api/tasks?filter[tasks][name]=John", String.class);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertThatJson(response.getBody()).node("data").isPresent();
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 75 with RestTemplate

use of org.springframework.web.client.RestTemplate in project crnk-framework by crnk-project.

the class BasicSpringBootTest method testErrorsSerializedAsJsonApi.

@Test
public void testErrorsSerializedAsJsonApi() throws IOException {
    RestTemplate testRestTemplate = new RestTemplate();
    try {
        testRestTemplate.getForEntity("http://localhost:" + this.port + "/doesNotExist", String.class);
        Assert.fail();
    } catch (HttpClientErrorException e) {
        assertEquals(HttpStatus.NOT_FOUND, e.getStatusCode());
        String body = e.getResponseBodyAsString();
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(JacksonModule.createJacksonModule());
        Document document = mapper.readerFor(Document.class).readValue(body);
        Assert.assertEquals(1, document.getErrors().size());
        ErrorData errorData = document.getErrors().get(0);
        Assert.assertEquals("404", errorData.getStatus());
        Assert.assertEquals("Not Found", errorData.getTitle());
        Assert.assertEquals("No message available", errorData.getDetail());
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) RestTemplate(org.springframework.web.client.RestTemplate) Document(io.crnk.core.engine.document.Document) ErrorData(io.crnk.core.engine.document.ErrorData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

RestTemplate (org.springframework.web.client.RestTemplate)519 Test (org.junit.Test)135 Test (org.junit.jupiter.api.Test)78 HttpHeaders (org.springframework.http.HttpHeaders)77 HttpEntity (org.springframework.http.HttpEntity)76 URI (java.net.URI)73 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)45 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)44 HashMap (java.util.HashMap)42 ArrayList (java.util.ArrayList)40 IOException (java.io.IOException)36 Bean (org.springframework.context.annotation.Bean)35 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)32 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)29 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)27 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)27 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)24 RestTemplateBuilder (org.springframework.boot.web.client.RestTemplateBuilder)22 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)22 SimpleClientHttpRequestFactory (org.springframework.http.client.SimpleClientHttpRequestFactory)22