From a0ae752606b1d7e4e60eda93f4ee94f0d0d85984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Formento?= Date: Fri, 14 Jul 2017 21:53:32 -0300 Subject: [PATCH 1/2] =?UTF-8?q?Corrige=20erro=20de=20compila=C3=A7=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/br/com/concrete/mock/ApiApplication.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/br/com/concrete/mock/ApiApplication.java b/src/main/java/br/com/concrete/mock/ApiApplication.java index 698c7dc..4b88e59 100755 --- a/src/main/java/br/com/concrete/mock/ApiApplication.java +++ b/src/main/java/br/com/concrete/mock/ApiApplication.java @@ -2,6 +2,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; From a5c4ab00a00218cc5f5ff0f526cee77a30e97029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Formento?= Date: Fri, 14 Jul 2017 22:24:50 -0300 Subject: [PATCH 2/2] Corrige quebra de testes integrados --- build.gradle | 3 ++- .../br/com/concrete/mock/ApiApplication.java | 10 ++++++-- .../mock/infra/component/ExternalApi.java | 24 ++++--------------- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/build.gradle b/build.gradle index 218593f..1f76ae7 100644 --- a/build.gradle +++ b/build.gradle @@ -23,7 +23,7 @@ repositories { dependencies { - compile 'org.springframework.boot:spring-boot-starter' + compile('org.springframework.boot:spring-boot-starter') compile 'org.springframework.boot:spring-boot-starter-undertow' compile 'org.springframework.boot:spring-boot-starter-web' compile 'org.springframework.boot:spring-boot-starter-hateoas' @@ -33,6 +33,7 @@ dependencies { compile 'com.google.guava:guava:22.0' compile 'org.modelmapper:modelmapper:0.7.5' compile 'commons-io:commons-io:2.5' + compile 'org.apache.httpcomponents:httpclient:4.4.1' testCompile 'org.springframework.boot:spring-boot-starter-test' testCompile 'br.com.six2six:fixture-factory:3.1.0' diff --git a/src/main/java/br/com/concrete/mock/ApiApplication.java b/src/main/java/br/com/concrete/mock/ApiApplication.java index 4b88e59..484a50c 100755 --- a/src/main/java/br/com/concrete/mock/ApiApplication.java +++ b/src/main/java/br/com/concrete/mock/ApiApplication.java @@ -1,10 +1,14 @@ package br.com.concrete.mock; +import org.apache.http.client.HttpClient; +import org.apache.http.impl.client.HttpClientBuilder; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; +import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; @SpringBootApplication @@ -16,8 +20,10 @@ public static void main(String[] args) { } @Bean - public RestTemplate restTemplate(RestTemplateBuilder builder) { - return builder.build(); + public RestTemplate restTemplate() { + HttpClient httpClient = HttpClientBuilder.create().build(); + ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); + return new RestTemplate(requestFactory); } } diff --git a/src/main/java/br/com/concrete/mock/infra/component/ExternalApi.java b/src/main/java/br/com/concrete/mock/infra/component/ExternalApi.java index a8e6a56..81ecea8 100755 --- a/src/main/java/br/com/concrete/mock/infra/component/ExternalApi.java +++ b/src/main/java/br/com/concrete/mock/infra/component/ExternalApi.java @@ -6,6 +6,8 @@ import br.com.concrete.mock.generic.model.Request; import br.com.concrete.mock.infra.model.UriConfiguration; import br.com.concrete.mock.infra.property.ApiProperty; +import java.util.Optional; +import java.util.regex.Pattern; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -16,9 +18,6 @@ import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; -import java.util.Optional; -import java.util.regex.Pattern; - @Component public class ExternalApi { @@ -69,22 +68,9 @@ public Optional execute(final Request request) { .concat(parameters); LOGGER.info("URL => {}", url); - ResponseEntity apiResult = null; - try { - apiResult = restTemplate.exchange(url, HttpMethod.valueOf(request.getMethod().name().toUpperCase()), entity, - String.class); - return Optional.of(new ExternalApiResult(apiResult, uriConfiguration)); - } catch (Exception e) { - LOGGER.error("Erro chamada API " + HttpMethod.valueOf(request.getMethod().name().toUpperCase()) + " " + url, - e); - - LOGGER.error("Tentando mais uma vez " + HttpMethod.valueOf(request.getMethod().name().toUpperCase()) + " " - + url); - } - // } - - return Optional.empty(); - + final ResponseEntity apiResult = restTemplate.exchange(url, HttpMethod.valueOf(request.getMethod().name().toUpperCase()), entity, + String.class); + return Optional.of(new ExternalApiResult(apiResult, uriConfiguration)); } }