From bab43cac5baf41b23f37e28a1f0e8d523dd1a44f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Z=C3=A9=20L=C3=BAcio=20Jr?= Date: Mon, 1 Apr 2024 20:31:43 -0300 Subject: [PATCH 1/2] as is: code migration --- .gitignore | 33 ++ LICENSE | 3 +- pom.xml | 207 ++++++++++ .../httpclient/ExceptionHandler.java | 8 + ...ceptionHandlersByExceptionTypeFactory.java | 10 + .../httpclient/HttpRequestBuilder.java | 12 + .../HttpRequestBuilderForHandlers.java | 12 + .../HttpRequestBuilderForRetrying.java | 16 + .../httpclient/HttpRequestHeaderFactory.java | 9 + .../httpclient/HttpRequestMethod.java | 11 + .../httpclient/HttpRequestModel.java | 10 + .../httpclient/HttpRequestStarter.java | 12 + .../julucinho/httpclient/HttpResponse.java | 15 + .../httpclient/HttpResponseHandler.java | 8 + ...tpResponseHandlersByStatusCodeFactory.java | 7 + .../julucinho/httpclient/RetrierModel.java | 22 + .../RetriersByStatusCodeFactory.java | 8 + .../julucinho/httpclient/RetryCounter.java | 6 + .../httpclient/commons/HandlersFactory.java | 7 + .../RetriersByExceptionTypeFactory.java | 9 + .../httpclient/commons/RetriersFactory.java | 5 + .../impl/AbstractHttpRequestBuilder.java | 11 + .../impl/AbstractHttpRequestModel.java | 26 ++ .../httpclient/impl/AbstractHttpResponse.java | 12 + .../ExceptionThrownByHttpRequestChecker.java | 27 ++ .../impl/FinalHttpRequestExecutor.java | 43 ++ .../impl/FinalHttpRequestFactory.java | 65 +++ .../impl/HttpRequestBuilderImpl.java | 105 +++++ .../impl/HttpRequestDeleteMethod.java | 22 + .../impl/HttpRequestExecutionManager.java | 30 ++ .../httpclient/impl/HttpRequestGetMethod.java | 22 + .../httpclient/impl/HttpRequestModelImpl.java | 37 ++ .../impl/HttpRequestPathVariable.java | 15 + .../impl/HttpRequestPostMethod.java | 22 + .../httpclient/impl/HttpRequestPutMethod.java | 22 + .../impl/HttpRequestQueryParameter.java | 15 + .../impl/HttpRequestStarterImpl.java | 30 ++ .../httpclient/impl/HttpResponseImpl.java | 60 +++ .../impl/HttpResponseStatusCodeChecker.java | 42 ++ .../httpclient/impl/ObjectMapperFactory.java | 16 + .../httpclient/impl/ProxyAddressModel.java | 13 + .../httpclient/impl/RetryCounterImpl.java | 26 ++ .../impl/exceptions/IORuntimeException.java | 11 + .../InterruptedRuntimeException.java | 7 + .../JsonProcessingRuntimeException.java | 7 + ...andlersAvailableForExecutionException.java | 17 + ...RetryNeededOnExceptionThrownException.java | 15 + .../RetryNeededOnHttpStatusCodeException.java | 16 + .../utils/ThreadIdRetrievementUtil.java | 16 + .../httpclient/RetrierModelTest.java | 26 ++ ...ceptionThrownByHttpRequestCheckerTest.java | 59 +++ .../impl/FinalHttpRequestFactoryTest.java | 82 ++++ .../impl/HttpRequestBuilderImplTest.java | 382 ++++++++++++++++++ .../impl/HttpRequestStarterImplTest.java | 41 ++ .../httpclient/impl/HttpResponseImplTest.java | 97 +++++ .../HttpResponseStatusCodeCheckerTest.java | 65 +++ .../utils/ThreadIdRetrievementUtilTest.java | 30 ++ 57 files changed, 1961 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/io/github/julucinho/httpclient/ExceptionHandler.java create mode 100644 src/main/java/io/github/julucinho/httpclient/HttpExceptionHandlersByExceptionTypeFactory.java create mode 100644 src/main/java/io/github/julucinho/httpclient/HttpRequestBuilder.java create mode 100644 src/main/java/io/github/julucinho/httpclient/HttpRequestBuilderForHandlers.java create mode 100644 src/main/java/io/github/julucinho/httpclient/HttpRequestBuilderForRetrying.java create mode 100644 src/main/java/io/github/julucinho/httpclient/HttpRequestHeaderFactory.java create mode 100644 src/main/java/io/github/julucinho/httpclient/HttpRequestMethod.java create mode 100644 src/main/java/io/github/julucinho/httpclient/HttpRequestModel.java create mode 100644 src/main/java/io/github/julucinho/httpclient/HttpRequestStarter.java create mode 100644 src/main/java/io/github/julucinho/httpclient/HttpResponse.java create mode 100644 src/main/java/io/github/julucinho/httpclient/HttpResponseHandler.java create mode 100644 src/main/java/io/github/julucinho/httpclient/HttpResponseHandlersByStatusCodeFactory.java create mode 100644 src/main/java/io/github/julucinho/httpclient/RetrierModel.java create mode 100644 src/main/java/io/github/julucinho/httpclient/RetriersByStatusCodeFactory.java create mode 100644 src/main/java/io/github/julucinho/httpclient/RetryCounter.java create mode 100644 src/main/java/io/github/julucinho/httpclient/commons/HandlersFactory.java create mode 100644 src/main/java/io/github/julucinho/httpclient/commons/RetriersByExceptionTypeFactory.java create mode 100644 src/main/java/io/github/julucinho/httpclient/commons/RetriersFactory.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpRequestBuilder.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpRequestModel.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpResponse.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/ExceptionThrownByHttpRequestChecker.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/FinalHttpRequestExecutor.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/FinalHttpRequestFactory.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/HttpRequestBuilderImpl.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/HttpRequestDeleteMethod.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/HttpRequestExecutionManager.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/HttpRequestGetMethod.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/HttpRequestModelImpl.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPathVariable.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPostMethod.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPutMethod.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/HttpRequestQueryParameter.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/HttpRequestStarterImpl.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/HttpResponseImpl.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/HttpResponseStatusCodeChecker.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/ObjectMapperFactory.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/ProxyAddressModel.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/RetryCounterImpl.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/exceptions/IORuntimeException.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/exceptions/InterruptedRuntimeException.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/exceptions/JsonProcessingRuntimeException.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/exceptions/NoResponseHandlersAvailableForExecutionException.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/exceptions/RetryNeededOnExceptionThrownException.java create mode 100644 src/main/java/io/github/julucinho/httpclient/impl/exceptions/RetryNeededOnHttpStatusCodeException.java create mode 100644 src/main/java/io/github/julucinho/httpclient/utils/ThreadIdRetrievementUtil.java create mode 100644 src/test/java/io/github/julucinho/httpclient/RetrierModelTest.java create mode 100644 src/test/java/io/github/julucinho/httpclient/impl/ExceptionThrownByHttpRequestCheckerTest.java create mode 100644 src/test/java/io/github/julucinho/httpclient/impl/FinalHttpRequestFactoryTest.java create mode 100644 src/test/java/io/github/julucinho/httpclient/impl/HttpRequestBuilderImplTest.java create mode 100644 src/test/java/io/github/julucinho/httpclient/impl/HttpRequestStarterImplTest.java create mode 100644 src/test/java/io/github/julucinho/httpclient/impl/HttpResponseImplTest.java create mode 100644 src/test/java/io/github/julucinho/httpclient/impl/HttpResponseStatusCodeCheckerTest.java create mode 100644 src/test/java/io/github/julucinho/httpclient/utils/ThreadIdRetrievementUtilTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/LICENSE b/LICENSE index 261eeb9..7a4a3ea 100644 --- a/LICENSE +++ b/LICENSE @@ -1,3 +1,4 @@ + Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ @@ -198,4 +199,4 @@ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and - limitations under the License. + limitations under the License. \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..5717e44 --- /dev/null +++ b/pom.xml @@ -0,0 +1,207 @@ + + + + 4.0.0 + + io.github.julucinho + httpClient + 1.0.2 + + httpClient + https://github.com/julucinho/http-client + + Lib for making implementation of http requests easy. + + + + The Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + + + + + J. Lúcio de Almeida Júnior + joselucioalmeidajunior@gmail.com + + + + scm:git:git@github.com:julucinho/http-client.git + scm:git:ssh://github.com:julucinho/http-client.git + https://github.com/julucinho/http-client/tree/main + + + + ossrh + https://s01.oss.sonatype.org/content/repositories/snapshots + + + ossrh + https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ + + + + UTF-8 + 11 + 11 + + + + + org.junit.jupiter + junit-jupiter + 5.7.1 + test + + + org.assertj + assertj-core + 3.19.0 + test + + + com.fasterxml.jackson.core + jackson-databind + 2.13.1 + + + org.mockito + mockito-junit-jupiter + 4.3.1 + test + + + org.projectlombok + lombok + 1.18.22 + + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.7 + true + + ossrh + https://s01.oss.sonatype.org/ + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 11 + 11 + + + + + + + release + + + performRelease + true + + + + C:\Program Files (x86)\GnuPG\bin\gpg.exe + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.6 + + + sign-artifacts + verify + + sign + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.2.0 + + + attach-javadocs + + jar + + + + + ${java.home}/bin/javadoc + + + + org.apache.maven.plugins + maven-source-plugin + 3.2.1 + + + attach-sources + + jar-no-fork + + + + + + + + + diff --git a/src/main/java/io/github/julucinho/httpclient/ExceptionHandler.java b/src/main/java/io/github/julucinho/httpclient/ExceptionHandler.java new file mode 100644 index 0000000..27eac10 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/ExceptionHandler.java @@ -0,0 +1,8 @@ +package io.github.julucinho.httpclient; + +@FunctionalInterface +public interface ExceptionHandler { + + void handle(Exception exception); + +} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpExceptionHandlersByExceptionTypeFactory.java b/src/main/java/io/github/julucinho/httpclient/HttpExceptionHandlersByExceptionTypeFactory.java new file mode 100644 index 0000000..5af4bf0 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/HttpExceptionHandlersByExceptionTypeFactory.java @@ -0,0 +1,10 @@ +package io.github.julucinho.httpclient; + +import io.github.julucinho.httpclient.commons.HandlersFactory; + +import java.util.Map; + +public interface HttpExceptionHandlersByExceptionTypeFactory extends HandlersFactory, ExceptionHandler>> { } + + + diff --git a/src/main/java/io/github/julucinho/httpclient/HttpRequestBuilder.java b/src/main/java/io/github/julucinho/httpclient/HttpRequestBuilder.java new file mode 100644 index 0000000..a86d067 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/HttpRequestBuilder.java @@ -0,0 +1,12 @@ +package io.github.julucinho.httpclient; + +public interface HttpRequestBuilder extends HttpRequestBuilderForHandlers, HttpRequestBuilderForRetrying{ + + HttpRequestBuilder andAddHeaderOf(String key, String value); + HttpRequestBuilder andAddHeadersFactory(HttpRequestHeaderFactory httpRequestHeaderFactory); + HttpRequestBuilder andAddPathVariableOf(String pathVariableName, String pathVariableValue); + HttpRequestBuilder andAddQueryParameterOf(String queryParameterName, String queryParameterValue); + HttpRequestBuilder andAddProxyAddress(String host, Integer port); + HttpRequestModel andFinishBuildingModel(); + +} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpRequestBuilderForHandlers.java b/src/main/java/io/github/julucinho/httpclient/HttpRequestBuilderForHandlers.java new file mode 100644 index 0000000..c8af121 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/HttpRequestBuilderForHandlers.java @@ -0,0 +1,12 @@ +package io.github.julucinho.httpclient; + + +public interface HttpRequestBuilderForHandlers { + + HttpRequestBuilder andAddResponseHandlerByHttpStatusCode(Integer statusCode, HttpResponseHandler httpResponseHandler); + HttpRequestBuilder andAddResponseHandlersByHttpStatusCodeFactory(HttpResponseHandlersByStatusCodeFactory httpResponseHandlersByStatusCodeFactory); + HttpRequestBuilder andAddResponseHandlerForAnyNotSuccessfulResponse(HttpResponseHandler httpResponseHandler); + HttpRequestBuilder andAddExceptionHandlersByExceptionTypeFactory(HttpExceptionHandlersByExceptionTypeFactory httpExceptionHandlersByExceptionTypeFactory); + HttpRequestBuilder andAddExceptionHandlerByExceptionType(Class exceptionType, ExceptionHandler exceptionHandler); + +} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpRequestBuilderForRetrying.java b/src/main/java/io/github/julucinho/httpclient/HttpRequestBuilderForRetrying.java new file mode 100644 index 0000000..d30b0a7 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/HttpRequestBuilderForRetrying.java @@ -0,0 +1,16 @@ +package io.github.julucinho.httpclient; + +import io.github.julucinho.httpclient.commons.RetriersByExceptionTypeFactory; + +public interface HttpRequestBuilderForRetrying { + + HttpRequestBuilder andAddRetrierByHttpStatusCode(Integer statusCode, RetrierModel retrierModel); + HttpRequestBuilder andAddRetriersByHttpStatusCodeFactory(RetriersByStatusCodeFactory retriersByStatusCodeFactory); + HttpRequestBuilder andAddRetriersByExceptionTypeFactory(RetriersByExceptionTypeFactory retriersByExceptionTypeFactory); + HttpRequestBuilder andAddRetrierByExceptionType(Class exceptionType, RetrierModel retrierModel); + +} + + + + diff --git a/src/main/java/io/github/julucinho/httpclient/HttpRequestHeaderFactory.java b/src/main/java/io/github/julucinho/httpclient/HttpRequestHeaderFactory.java new file mode 100644 index 0000000..5ee98f2 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/HttpRequestHeaderFactory.java @@ -0,0 +1,9 @@ +package io.github.julucinho.httpclient; + +import java.util.Map; + +public interface HttpRequestHeaderFactory { + + Map makeHeaders(); + +} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpRequestMethod.java b/src/main/java/io/github/julucinho/httpclient/HttpRequestMethod.java new file mode 100644 index 0000000..8b78b1f --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/HttpRequestMethod.java @@ -0,0 +1,11 @@ +package io.github.julucinho.httpclient; + + +import io.github.julucinho.httpclient.impl.AbstractHttpRequestModel; +import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnExceptionThrownException; + +public interface HttpRequestMethod { + + HttpResponse execute(AbstractHttpRequestModel httpRequestModel) throws RetryNeededOnExceptionThrownException; + +} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpRequestModel.java b/src/main/java/io/github/julucinho/httpclient/HttpRequestModel.java new file mode 100644 index 0000000..89b9aae --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/HttpRequestModel.java @@ -0,0 +1,10 @@ +package io.github.julucinho.httpclient; + +import com.fasterxml.jackson.core.type.TypeReference; + +public interface HttpRequestModel { + + T sendRequestReturning(Class typeOfExpectedResponseBody); + + T sendRequestReturning(TypeReference typeReference); +} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpRequestStarter.java b/src/main/java/io/github/julucinho/httpclient/HttpRequestStarter.java new file mode 100644 index 0000000..d305e04 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/HttpRequestStarter.java @@ -0,0 +1,12 @@ +package io.github.julucinho.httpclient; + +import java.net.http.HttpRequest; + +public interface HttpRequestStarter { + + HttpRequestBuilder startGetRequestModelFor(String url); + HttpRequestBuilder startPostRequestModelFor(String url, HttpRequest.BodyPublisher body); + HttpRequestBuilder startPutRequestModelFor(String url, HttpRequest.BodyPublisher body); + HttpRequestBuilder startDeleteRequestModelFor(String url); + +} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpResponse.java b/src/main/java/io/github/julucinho/httpclient/HttpResponse.java new file mode 100644 index 0000000..314febe --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/HttpResponse.java @@ -0,0 +1,15 @@ +package io.github.julucinho.httpclient; + +import com.fasterxml.jackson.core.type.TypeReference; + +import java.util.function.Consumer; + +public interface HttpResponse{ + + Integer getStatusCode(); + HttpRequestModel getHttpRequest(); + T getBodyAs(Class bodyType); + T getBodyAs(TypeReference typeOfExpectedResponseBody); + boolean needsHandling(); + void ifNeedsHandling(Consumer checkOnResponse); +} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpResponseHandler.java b/src/main/java/io/github/julucinho/httpclient/HttpResponseHandler.java new file mode 100644 index 0000000..e46b926 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/HttpResponseHandler.java @@ -0,0 +1,8 @@ +package io.github.julucinho.httpclient; + +@FunctionalInterface +public interface HttpResponseHandler { + + void handle(HttpResponse httpResponse); + +} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpResponseHandlersByStatusCodeFactory.java b/src/main/java/io/github/julucinho/httpclient/HttpResponseHandlersByStatusCodeFactory.java new file mode 100644 index 0000000..b9deb1c --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/HttpResponseHandlersByStatusCodeFactory.java @@ -0,0 +1,7 @@ +package io.github.julucinho.httpclient; + +import io.github.julucinho.httpclient.commons.HandlersFactory; + +import java.util.Map; + +public interface HttpResponseHandlersByStatusCodeFactory extends HandlersFactory> { } diff --git a/src/main/java/io/github/julucinho/httpclient/RetrierModel.java b/src/main/java/io/github/julucinho/httpclient/RetrierModel.java new file mode 100644 index 0000000..1004a5b --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/RetrierModel.java @@ -0,0 +1,22 @@ +package io.github.julucinho.httpclient; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@RequiredArgsConstructor(access = AccessLevel.PRIVATE) +public class RetrierModel { + + private final Integer originalLimitForRetrying; + private Integer retriesRemaining; + + public static RetrierModel withLimitOf(Integer limitForRetrying){ + var retrier = new RetrierModel(limitForRetrying); + retrier.setRetriesRemaining(retrier.getOriginalLimitForRetrying()); + return retrier; + } + +} diff --git a/src/main/java/io/github/julucinho/httpclient/RetriersByStatusCodeFactory.java b/src/main/java/io/github/julucinho/httpclient/RetriersByStatusCodeFactory.java new file mode 100644 index 0000000..22ab7e9 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/RetriersByStatusCodeFactory.java @@ -0,0 +1,8 @@ +package io.github.julucinho.httpclient; + +import io.github.julucinho.httpclient.commons.RetriersFactory; + +import java.util.Map; + +public interface RetriersByStatusCodeFactory extends RetriersFactory> { +} diff --git a/src/main/java/io/github/julucinho/httpclient/RetryCounter.java b/src/main/java/io/github/julucinho/httpclient/RetryCounter.java new file mode 100644 index 0000000..cccb70d --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/RetryCounter.java @@ -0,0 +1,6 @@ +package io.github.julucinho.httpclient; + +public interface RetryCounter { + boolean thereIsRetryAvailable(); + void decreaseRetriesAvailable(); +} diff --git a/src/main/java/io/github/julucinho/httpclient/commons/HandlersFactory.java b/src/main/java/io/github/julucinho/httpclient/commons/HandlersFactory.java new file mode 100644 index 0000000..4b337d4 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/commons/HandlersFactory.java @@ -0,0 +1,7 @@ +package io.github.julucinho.httpclient.commons; + +public interface HandlersFactory { + + T makeHandlers(); + +} diff --git a/src/main/java/io/github/julucinho/httpclient/commons/RetriersByExceptionTypeFactory.java b/src/main/java/io/github/julucinho/httpclient/commons/RetriersByExceptionTypeFactory.java new file mode 100644 index 0000000..0e85867 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/commons/RetriersByExceptionTypeFactory.java @@ -0,0 +1,9 @@ +package io.github.julucinho.httpclient.commons; + + +import io.github.julucinho.httpclient.RetrierModel; + +import java.util.Map; + +public interface RetriersByExceptionTypeFactory extends RetriersFactory, RetrierModel>> { +} diff --git a/src/main/java/io/github/julucinho/httpclient/commons/RetriersFactory.java b/src/main/java/io/github/julucinho/httpclient/commons/RetriersFactory.java new file mode 100644 index 0000000..93a69de --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/commons/RetriersFactory.java @@ -0,0 +1,5 @@ +package io.github.julucinho.httpclient.commons; + +public interface RetriersFactory { + T makeRetriers(); +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpRequestBuilder.java b/src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpRequestBuilder.java new file mode 100644 index 0000000..1d17a5e --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpRequestBuilder.java @@ -0,0 +1,11 @@ +package io.github.julucinho.httpclient.impl; + +import io.github.julucinho.httpclient.HttpRequestBuilder; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public abstract class AbstractHttpRequestBuilder implements HttpRequestBuilder { + + protected final AbstractHttpRequestModel httpRequest; + +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpRequestModel.java b/src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpRequestModel.java new file mode 100644 index 0000000..b4bf415 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpRequestModel.java @@ -0,0 +1,26 @@ +package io.github.julucinho.httpclient.impl; + +import io.github.julucinho.httpclient.*; + +import java.net.http.HttpRequest.BodyPublisher; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public abstract class AbstractHttpRequestModel implements HttpRequestModel { + + protected String uri; + protected final List pathVariables = new ArrayList<>(); + protected final List queryParameters = new ArrayList<>(); + protected final Map headers = new HashMap<>(); + protected BodyPublisher body; + protected HttpRequestMethod method; + protected ProxyAddressModel proxyAddress; + protected HttpResponseHandler genericResponseHandler; + protected final Map responseHandlersByStatusCode = new HashMap<>(); + protected final Map, ExceptionHandler> exceptionHandlersByExceptionType = new HashMap<>(); + protected final Map retryCountersByStatusCode = new HashMap<>(); + protected final Map, RetryCounter> retryCountersByExceptionType = new HashMap<>(); + +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpResponse.java b/src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpResponse.java new file mode 100644 index 0000000..9a5902b --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpResponse.java @@ -0,0 +1,12 @@ +package io.github.julucinho.httpclient.impl; + +import io.github.julucinho.httpclient.HttpRequestModel; +import io.github.julucinho.httpclient.HttpResponse; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public abstract class AbstractHttpResponse implements HttpResponse { + + protected final HttpRequestModel httpRequestModel; + protected final java.net.http.HttpResponse unwrappedHttpResponse; +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/ExceptionThrownByHttpRequestChecker.java b/src/main/java/io/github/julucinho/httpclient/impl/ExceptionThrownByHttpRequestChecker.java new file mode 100644 index 0000000..4a74138 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/ExceptionThrownByHttpRequestChecker.java @@ -0,0 +1,27 @@ +package io.github.julucinho.httpclient.impl; + +import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnExceptionThrownException; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; + +import static java.util.Optional.ofNullable; + +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class ExceptionThrownByHttpRequestChecker { + + private final AbstractHttpRequestModel httpRequestModel; + + public static ExceptionThrownByHttpRequestChecker of(AbstractHttpRequestModel httpRequestModel) { + return new ExceptionThrownByHttpRequestChecker(httpRequestModel); + } + + public void checkOn(Exception e) { + var retryCounterByExceptionType = ofNullable(httpRequestModel.retryCountersByExceptionType.get(e.getClass())); + if (retryCounterByExceptionType.isPresent() && retryCounterByExceptionType.get().thereIsRetryAvailable()) { + retryCounterByExceptionType.get().decreaseRetriesAvailable(); + throw new RetryNeededOnExceptionThrownException(e.getClass()); + } + var handlerByThisException = ofNullable(httpRequestModel.exceptionHandlersByExceptionType.get(e.getClass())); + handlerByThisException.ifPresent(handler -> handler.handle(e)); + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/FinalHttpRequestExecutor.java b/src/main/java/io/github/julucinho/httpclient/impl/FinalHttpRequestExecutor.java new file mode 100644 index 0000000..5503213 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/FinalHttpRequestExecutor.java @@ -0,0 +1,43 @@ +package io.github.julucinho.httpclient.impl; + +import io.github.julucinho.httpclient.impl.exceptions.IORuntimeException; +import io.github.julucinho.httpclient.impl.exceptions.InterruptedRuntimeException; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.ProxySelector; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.Optional; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class FinalHttpRequestExecutor { + + private AbstractHttpRequestModel httpRequestModel; + + public static FinalHttpRequestExecutor of(AbstractHttpRequestModel httpRequestModel){ + var executor = new FinalHttpRequestExecutor(); + executor.httpRequestModel = httpRequestModel; + return executor; + } + + public HttpResponse execute(HttpRequest finalRequest){ + try { + return this.createClient().send(finalRequest, HttpResponse.BodyHandlers.ofString()); + } catch (IOException e) { + throw new IORuntimeException(e); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new InterruptedRuntimeException(e); + } + } + + private HttpClient createClient() { + var client = HttpClient.newBuilder(); + Optional.ofNullable(this.httpRequestModel.proxyAddress).ifPresent(proxyAddress -> client.proxy(ProxySelector.of(new InetSocketAddress(proxyAddress.getHost(), proxyAddress.getPort())))); + return client.build(); + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/FinalHttpRequestFactory.java b/src/main/java/io/github/julucinho/httpclient/impl/FinalHttpRequestFactory.java new file mode 100644 index 0000000..86c5d27 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/FinalHttpRequestFactory.java @@ -0,0 +1,65 @@ +package io.github.julucinho.httpclient.impl; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +import java.net.URI; +import java.net.http.HttpRequest; +import java.util.List; +import java.util.stream.Collectors; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class FinalHttpRequestFactory { + + public static HttpRequest makeFinalRequestForGetMethodFrom(AbstractHttpRequestModel httpRequestModel) { + var finalHttpRequestBuilder = HttpRequest.newBuilder().GET(); + return finallyBuildIt(finalHttpRequestBuilder, httpRequestModel); + } + + public static HttpRequest makeFinalRequestForPostMethodFrom(AbstractHttpRequestModel httpRequestModel) { + var finalHttpRequestBuilder = HttpRequest.newBuilder().POST(httpRequestModel.body); + return finallyBuildIt(finalHttpRequestBuilder, httpRequestModel); + } + + public static HttpRequest makeFinalRequestForPutMethodFrom(AbstractHttpRequestModel httpRequestModel) { + var finalHttpRequestBuilder = HttpRequest.newBuilder().PUT(httpRequestModel.body); + return finallyBuildIt(finalHttpRequestBuilder, httpRequestModel); + } + + public static HttpRequest makeFinalRequestForDeleteMethodFrom(AbstractHttpRequestModel httpRequestModel) { + var finalHttpRequestBuilder = HttpRequest.newBuilder().DELETE(); + return finallyBuildIt(finalHttpRequestBuilder, httpRequestModel); + } + + private static HttpRequest finallyBuildIt(HttpRequest.Builder finalHttpRequestBuilder, AbstractHttpRequestModel httpRequestModel) { + setUriInto(finalHttpRequestBuilder, httpRequestModel); + setHeadersInto(finalHttpRequestBuilder, httpRequestModel); + return finalHttpRequestBuilder.build(); + } + + private static void setUriInto(HttpRequest.Builder finalHttpRequestBuilder, AbstractHttpRequestModel httpRequestModel) { + var uriBuilder = new StringBuilder(httpRequestModel.uri); + setPathVariablesInto(uriBuilder, httpRequestModel.pathVariables); + setQueryParametersInto(uriBuilder, httpRequestModel.queryParameters); + var finalUriString = uriBuilder.toString(); + var finalUri = URI.create(finalUriString); + finalHttpRequestBuilder.uri(finalUri); + } + + private static void setPathVariablesInto(StringBuilder uriBuilder, List pathVariables) { + pathVariables.stream().map(HttpRequestPathVariable::buildPathVariable).forEach(uriBuilder::append); + } + + private static void setQueryParametersInto(StringBuilder uriBuilder, List queryParameters) { + if (!queryParameters.isEmpty()){ + var firstQueryParam = queryParameters.get(0).buildQueryParameter(); + var otherQueryParams = queryParameters.subList(1, queryParameters.size()).stream().map(HttpRequestQueryParameter::buildQueryParameter).collect(Collectors.toList()); + uriBuilder.append("?".concat(firstQueryParam)); + otherQueryParams.stream().map("&"::concat).forEach(uriBuilder::append); + } + } + + private static void setHeadersInto(HttpRequest.Builder finalHttpRequestBuilder, AbstractHttpRequestModel httpRequestModel) { + httpRequestModel.headers.forEach(finalHttpRequestBuilder::header); + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestBuilderImpl.java b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestBuilderImpl.java new file mode 100644 index 0000000..3bde46a --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestBuilderImpl.java @@ -0,0 +1,105 @@ +package io.github.julucinho.httpclient.impl; + + +import io.github.julucinho.httpclient.*; +import io.github.julucinho.httpclient.commons.RetriersByExceptionTypeFactory; + +public class HttpRequestBuilderImpl extends AbstractHttpRequestBuilder { + + public static HttpRequestBuilder of(AbstractHttpRequestModel httpRequest){ + return new HttpRequestBuilderImpl(httpRequest); + } + + private HttpRequestBuilderImpl(AbstractHttpRequestModel httpRequest) { + super(httpRequest); + } + + @Override + public HttpRequestBuilder andAddHeaderOf(String key, String value) { + this.httpRequest.headers.put(key, value); + return this; + } + + @Override + public HttpRequestBuilder andAddHeadersFactory(HttpRequestHeaderFactory httpRequestHeaderFactory) { + httpRequestHeaderFactory.makeHeaders().forEach(this.httpRequest.headers::put); + return this; + } + + @Override + public HttpRequestBuilder andAddPathVariableOf(String pathVariableName, String pathVariableValue) { + this.httpRequest.pathVariables.add(new HttpRequestPathVariable(pathVariableName, pathVariableValue)); + return this; + } + + @Override + public HttpRequestBuilder andAddQueryParameterOf(String queryParameterName, String queryParameterValue) { + this.httpRequest.queryParameters.add(new HttpRequestQueryParameter(queryParameterName, queryParameterValue)); + return this; + } + + @Override + public HttpRequestBuilder andAddProxyAddress(String host, Integer port) { + this.httpRequest.proxyAddress = ProxyAddressModel.builder().host(host).port(port).build(); + return this; + } + + @Override + public HttpRequestModel andFinishBuildingModel() { + return this.httpRequest; + } + + @Override + public HttpRequestBuilder andAddResponseHandlerByHttpStatusCode(Integer statusCode, HttpResponseHandler httpResponseHandler) { + this.httpRequest.responseHandlersByStatusCode.put(statusCode, httpResponseHandler); + return this; + } + + @Override + public HttpRequestBuilder andAddResponseHandlersByHttpStatusCodeFactory(HttpResponseHandlersByStatusCodeFactory httpResponseHandlersByStatusCodeFactory) { + httpResponseHandlersByStatusCodeFactory.makeHandlers().forEach(this.httpRequest.responseHandlersByStatusCode::put); + return this; + } + + @Override + public HttpRequestBuilder andAddResponseHandlerForAnyNotSuccessfulResponse(HttpResponseHandler httpResponseHandler) { + this.httpRequest.genericResponseHandler = httpResponseHandler; + return this; + } + + @Override + public HttpRequestBuilder andAddExceptionHandlerByExceptionType(Class exceptionType, ExceptionHandler exceptionHandler) { + this.httpRequest.exceptionHandlersByExceptionType.put(exceptionType, exceptionHandler); + return this; + } + + @Override + public HttpRequestBuilder andAddExceptionHandlersByExceptionTypeFactory(HttpExceptionHandlersByExceptionTypeFactory httpExceptionHandlersByExceptionTypeFactory) { + httpExceptionHandlersByExceptionTypeFactory.makeHandlers().forEach(this.httpRequest.exceptionHandlersByExceptionType::put); + return this; + } + + @Override + public HttpRequestBuilder andAddRetrierByHttpStatusCode(Integer statusCode, RetrierModel retrierModel) { + this.httpRequest.retryCountersByStatusCode.put(statusCode, RetryCounterImpl.of(retrierModel)); + return this; + } + + @Override + public HttpRequestBuilder andAddRetriersByHttpStatusCodeFactory(RetriersByStatusCodeFactory retriersByStatusCodeFactory) { + retriersByStatusCodeFactory.makeRetriers().forEach((statusCode, retrierModel) -> this.httpRequest.retryCountersByStatusCode.put(statusCode, RetryCounterImpl.of(retrierModel))); + return this; + } + + @Override + public HttpRequestBuilder andAddRetrierByExceptionType(Class exceptionType, RetrierModel retrierModel) { + this.httpRequest.retryCountersByExceptionType.put(exceptionType, RetryCounterImpl.of(retrierModel)); + return this; + } + + @Override + public HttpRequestBuilder andAddRetriersByExceptionTypeFactory(RetriersByExceptionTypeFactory retriersByExceptionTypeFactory) { + retriersByExceptionTypeFactory.makeRetriers().forEach((exceptionType, retrierModel) -> this.httpRequest.retryCountersByExceptionType.put(exceptionType, RetryCounterImpl.of(retrierModel))); + return this; + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestDeleteMethod.java b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestDeleteMethod.java new file mode 100644 index 0000000..e1677ca --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestDeleteMethod.java @@ -0,0 +1,22 @@ +package io.github.julucinho.httpclient.impl; + + +import io.github.julucinho.httpclient.HttpRequestMethod; +import io.github.julucinho.httpclient.HttpResponse; +import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnExceptionThrownException; + +public class HttpRequestDeleteMethod implements HttpRequestMethod { + + @Override + public HttpResponse execute(AbstractHttpRequestModel httpRequestModel) throws RetryNeededOnExceptionThrownException { + var finalRequest = FinalHttpRequestFactory.makeFinalRequestForDeleteMethodFrom(httpRequestModel); + try { + var unwrappedResponse = FinalHttpRequestExecutor.of(httpRequestModel).execute(finalRequest); + return new HttpResponseImpl(httpRequestModel, unwrappedResponse); + } + catch (Exception e) { + ExceptionThrownByHttpRequestChecker.of(httpRequestModel).checkOn(e); + throw e; + } + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestExecutionManager.java b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestExecutionManager.java new file mode 100644 index 0000000..435da86 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestExecutionManager.java @@ -0,0 +1,30 @@ +package io.github.julucinho.httpclient.impl; + +import io.github.julucinho.httpclient.HttpResponse; +import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnExceptionThrownException; +import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnHttpStatusCodeException; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +class HttpRequestExecutionManager { + + protected AbstractHttpRequestModel httpRequest; + + static HttpRequestExecutionManager of(AbstractHttpRequestModel httpRequest){ + var manager = new HttpRequestExecutionManager(); + manager.httpRequest = httpRequest; + return manager; + } + + protected HttpResponse run() { + try { + var response = this.httpRequest.method.execute(this.httpRequest); + response.ifNeedsHandling(HttpResponseStatusCodeChecker.of(this.httpRequest)::checkOutHandlersFor); + return response; + } catch (RetryNeededOnHttpStatusCodeException | RetryNeededOnExceptionThrownException exception) { + return this.run(); + } + } + +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestGetMethod.java b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestGetMethod.java new file mode 100644 index 0000000..617be89 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestGetMethod.java @@ -0,0 +1,22 @@ +package io.github.julucinho.httpclient.impl; + + +import io.github.julucinho.httpclient.HttpRequestMethod; +import io.github.julucinho.httpclient.HttpResponse; +import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnExceptionThrownException; + +public class HttpRequestGetMethod implements HttpRequestMethod { + + @Override + public HttpResponse execute(AbstractHttpRequestModel httpRequestModel) throws RetryNeededOnExceptionThrownException { + var finalRequest = FinalHttpRequestFactory.makeFinalRequestForGetMethodFrom(httpRequestModel); + try { + var unwrappedResponse = FinalHttpRequestExecutor.of(httpRequestModel).execute(finalRequest); + return new HttpResponseImpl(httpRequestModel, unwrappedResponse); + } + catch (Exception exception) { + ExceptionThrownByHttpRequestChecker.of(httpRequestModel).checkOn(exception); + throw exception; + } + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestModelImpl.java b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestModelImpl.java new file mode 100644 index 0000000..caabef0 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestModelImpl.java @@ -0,0 +1,37 @@ +package io.github.julucinho.httpclient.impl; + +import com.fasterxml.jackson.core.type.TypeReference; +import io.github.julucinho.httpclient.HttpRequestMethod; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +import java.net.http.HttpRequest.BodyPublisher; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class HttpRequestModelImpl extends AbstractHttpRequestModel { + + public static AbstractHttpRequestModel of(String uri, HttpRequestMethod method){ + var httpRequest = new HttpRequestModelImpl(); + httpRequest.uri = uri; + httpRequest.method = method; + return httpRequest; + } + + public static AbstractHttpRequestModel of(String uri, HttpRequestMethod method, BodyPublisher body){ + var httpRequest = new HttpRequestModelImpl(); + httpRequest.uri = uri; + httpRequest.method = method; + httpRequest.body = body; + return httpRequest; + } + + @Override + public T sendRequestReturning(Class typeOfExpectedResponseBody) { + return HttpRequestExecutionManager.of(this).run().getBodyAs(typeOfExpectedResponseBody); + } + + @Override + public T sendRequestReturning(TypeReference typeOfExpectedResponseBody) { + return HttpRequestExecutionManager.of(this).run().getBodyAs(typeOfExpectedResponseBody); + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPathVariable.java b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPathVariable.java new file mode 100644 index 0000000..567e4c9 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPathVariable.java @@ -0,0 +1,15 @@ +package io.github.julucinho.httpclient.impl; + +import lombok.AllArgsConstructor; + +@AllArgsConstructor +class HttpRequestPathVariable { + + private final String pathVariableName; + private final String pathVariableValue; + + String buildPathVariable(){ + return "/".concat(this.pathVariableName).concat("/").concat(this.pathVariableValue); + } + +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPostMethod.java b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPostMethod.java new file mode 100644 index 0000000..83e2849 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPostMethod.java @@ -0,0 +1,22 @@ +package io.github.julucinho.httpclient.impl; + + +import io.github.julucinho.httpclient.HttpRequestMethod; +import io.github.julucinho.httpclient.HttpResponse; +import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnExceptionThrownException; + +public class HttpRequestPostMethod implements HttpRequestMethod { + + @Override + public HttpResponse execute(AbstractHttpRequestModel httpRequestModel) throws RetryNeededOnExceptionThrownException { + var finalRequest = FinalHttpRequestFactory.makeFinalRequestForPostMethodFrom(httpRequestModel); + try { + var unwrappedResponse = FinalHttpRequestExecutor.of(httpRequestModel).execute(finalRequest); + return new HttpResponseImpl(httpRequestModel, unwrappedResponse); + } + catch (Exception e) { + ExceptionThrownByHttpRequestChecker.of(httpRequestModel).checkOn(e); + throw e; + } + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPutMethod.java b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPutMethod.java new file mode 100644 index 0000000..37abd80 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPutMethod.java @@ -0,0 +1,22 @@ +package io.github.julucinho.httpclient.impl; + + +import io.github.julucinho.httpclient.HttpRequestMethod; +import io.github.julucinho.httpclient.HttpResponse; +import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnExceptionThrownException; + +public class HttpRequestPutMethod implements HttpRequestMethod { + + @Override + public HttpResponse execute(AbstractHttpRequestModel httpRequestModel) throws RetryNeededOnExceptionThrownException { + var finalRequest = FinalHttpRequestFactory.makeFinalRequestForPutMethodFrom(httpRequestModel); + try { + var unwrappedResponse = FinalHttpRequestExecutor.of(httpRequestModel).execute(finalRequest); + return new HttpResponseImpl(httpRequestModel, unwrappedResponse); + } + catch (Exception e) { + ExceptionThrownByHttpRequestChecker.of(httpRequestModel).checkOn(e); + throw e; + } + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestQueryParameter.java b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestQueryParameter.java new file mode 100644 index 0000000..a5f342c --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestQueryParameter.java @@ -0,0 +1,15 @@ +package io.github.julucinho.httpclient.impl; + +import lombok.AllArgsConstructor; + +@AllArgsConstructor +class HttpRequestQueryParameter { + + private final String queryParameterName; + private final String queryParameterValue; + + String buildQueryParameter(){ + return this.queryParameterName.concat("=").concat(this.queryParameterValue); + } + +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestStarterImpl.java b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestStarterImpl.java new file mode 100644 index 0000000..63e0785 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestStarterImpl.java @@ -0,0 +1,30 @@ +package io.github.julucinho.httpclient.impl; + + +import io.github.julucinho.httpclient.HttpRequestBuilder; +import io.github.julucinho.httpclient.HttpRequestStarter; + +import java.net.http.HttpRequest; + +public class HttpRequestStarterImpl implements HttpRequestStarter { + + @Override + public HttpRequestBuilder startGetRequestModelFor(String url) { + return HttpRequestBuilderImpl.of(HttpRequestModelImpl.of(url, new HttpRequestGetMethod())); + } + + @Override + public HttpRequestBuilder startPostRequestModelFor(String url, HttpRequest.BodyPublisher body) { + return HttpRequestBuilderImpl.of(HttpRequestModelImpl.of(url, new HttpRequestPostMethod(), body)); + } + + @Override + public HttpRequestBuilder startPutRequestModelFor(String url, HttpRequest.BodyPublisher body) { + return HttpRequestBuilderImpl.of(HttpRequestModelImpl.of(url, new HttpRequestPutMethod(), body)); + } + + @Override + public HttpRequestBuilder startDeleteRequestModelFor(String url) { + return HttpRequestBuilderImpl.of(HttpRequestModelImpl.of(url, new HttpRequestDeleteMethod())); + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpResponseImpl.java b/src/main/java/io/github/julucinho/httpclient/impl/HttpResponseImpl.java new file mode 100644 index 0000000..2351c80 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/HttpResponseImpl.java @@ -0,0 +1,60 @@ +package io.github.julucinho.httpclient.impl; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import io.github.julucinho.httpclient.HttpRequestModel; +import io.github.julucinho.httpclient.HttpResponse; +import io.github.julucinho.httpclient.impl.exceptions.JsonProcessingRuntimeException; + +import java.util.function.Consumer; + +public class HttpResponseImpl extends AbstractHttpResponse{ + + public HttpResponseImpl(HttpRequestModel httpRequestModel, java.net.http.HttpResponse unwrappedHttpResponse) { + super(httpRequestModel, unwrappedHttpResponse); + } + + @Override + public Integer getStatusCode() { + return this.unwrappedHttpResponse.statusCode(); + } + + @Override + public HttpRequestModel getHttpRequest() { + return this.httpRequestModel; + } + + @Override + public T getBodyAs(Class bodyType) { + try{ + return ObjectMapperFactory.createNewObjectMapper().readValue(this.unwrappedHttpResponse.body(), bodyType); + } catch (Exception exception){ + throw new JsonProcessingRuntimeException(exception.getMessage()); + } + } + + @Override + public T getBodyAs(TypeReference bodyType) { + try{ + return ObjectMapperFactory.createNewObjectMapper().readValue(this.unwrappedHttpResponse.body(), bodyType); + } catch (JsonProcessingException exception){ + throw new JsonProcessingRuntimeException(exception.getMessage()); + } + } + + @Override + public boolean needsHandling() { + return this.isNot2xx(); + } + + @Override + public void ifNeedsHandling(Consumer checkOnResponse) { + if (this.needsHandling()) + checkOnResponse.accept(this); + } + + private boolean isNot2xx() { + var statusString = this.getStatusCode().toString(); + return (statusString.charAt(0) != '2'); + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpResponseStatusCodeChecker.java b/src/main/java/io/github/julucinho/httpclient/impl/HttpResponseStatusCodeChecker.java new file mode 100644 index 0000000..d09a2c2 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/HttpResponseStatusCodeChecker.java @@ -0,0 +1,42 @@ +package io.github.julucinho.httpclient.impl; + +import io.github.julucinho.httpclient.HttpResponse; +import io.github.julucinho.httpclient.HttpResponseHandler; +import io.github.julucinho.httpclient.impl.exceptions.NoResponseHandlersAvailableForExecutionException; +import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnHttpStatusCodeException; +import lombok.AccessLevel; +import lombok.RequiredArgsConstructor; + +import static java.util.Optional.ofNullable; + +@RequiredArgsConstructor(access = AccessLevel.PRIVATE) +public class HttpResponseStatusCodeChecker { + + private final AbstractHttpRequestModel httpRequest; + private HttpResponse httpResponse; + + public static HttpResponseStatusCodeChecker of(AbstractHttpRequestModel httpRequest){ + return new HttpResponseStatusCodeChecker(httpRequest); + } + + public void checkOutHandlersFor(HttpResponse response) throws RetryNeededOnHttpStatusCodeException { + this.httpResponse = response; + var statusCodeReceived = response.getStatusCode(); + var retryCounterForStatusCodeReceived = ofNullable(this.httpRequest.retryCountersByStatusCode.get(statusCodeReceived)); + if (retryCounterForStatusCodeReceived.isPresent() && retryCounterForStatusCodeReceived.get().thereIsRetryAvailable()){ + retryCounterForStatusCodeReceived.get().decreaseRetriesAvailable(); + throw new RetryNeededOnHttpStatusCodeException(response); + } + this.executeHandlerIfAnyIsPresentForTheResponseReceived(); + } + + private void executeHandlerIfAnyIsPresentForTheResponseReceived() { + var statusCodeReceived = this.httpResponse.getStatusCode(); + var handler = ofNullable(this.httpRequest.responseHandlersByStatusCode.get(statusCodeReceived)).orElseGet(this::getGenericHandler); + handler.handle(this.httpResponse); + } + + private HttpResponseHandler getGenericHandler() { + return ofNullable(this.httpRequest.genericResponseHandler).orElseThrow(() -> new NoResponseHandlersAvailableForExecutionException(this.httpResponse)); + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/ObjectMapperFactory.java b/src/main/java/io/github/julucinho/httpclient/impl/ObjectMapperFactory.java new file mode 100644 index 0000000..dc31937 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/ObjectMapperFactory.java @@ -0,0 +1,16 @@ +package io.github.julucinho.httpclient.impl; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class ObjectMapperFactory { + + public static ObjectMapper createNewObjectMapper() { + return new ObjectMapper() + .configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true) + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/ProxyAddressModel.java b/src/main/java/io/github/julucinho/httpclient/impl/ProxyAddressModel.java new file mode 100644 index 0000000..03223d7 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/ProxyAddressModel.java @@ -0,0 +1,13 @@ +package io.github.julucinho.httpclient.impl; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class ProxyAddressModel { + + private final String host; + private final Integer port; + +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/RetryCounterImpl.java b/src/main/java/io/github/julucinho/httpclient/impl/RetryCounterImpl.java new file mode 100644 index 0000000..46eceef --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/RetryCounterImpl.java @@ -0,0 +1,26 @@ +package io.github.julucinho.httpclient.impl; + +import io.github.julucinho.httpclient.RetrierModel; +import io.github.julucinho.httpclient.RetryCounter; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; + +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class RetryCounterImpl implements RetryCounter { + + private final RetrierModel retrierModel; + + public static RetryCounter of(RetrierModel retrierModel){ + return new RetryCounterImpl(retrierModel); + } + + @Override + public boolean thereIsRetryAvailable() { + return this.retrierModel.getRetriesRemaining() > 0; + } + + @Override + public void decreaseRetriesAvailable() { + this.retrierModel.setRetriesRemaining(this.retrierModel.getRetriesRemaining()-1); + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/exceptions/IORuntimeException.java b/src/main/java/io/github/julucinho/httpclient/impl/exceptions/IORuntimeException.java new file mode 100644 index 0000000..f3c98dc --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/exceptions/IORuntimeException.java @@ -0,0 +1,11 @@ +package io.github.julucinho.httpclient.impl.exceptions; + +import java.io.IOException; + +public class IORuntimeException extends RuntimeException{ + + public IORuntimeException(IOException exception){ + super(exception.getMessage()); + } + +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/exceptions/InterruptedRuntimeException.java b/src/main/java/io/github/julucinho/httpclient/impl/exceptions/InterruptedRuntimeException.java new file mode 100644 index 0000000..f041fe1 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/exceptions/InterruptedRuntimeException.java @@ -0,0 +1,7 @@ +package io.github.julucinho.httpclient.impl.exceptions; + +public class InterruptedRuntimeException extends RuntimeException { + public InterruptedRuntimeException(InterruptedException e) { + super(e.getMessage()); + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/exceptions/JsonProcessingRuntimeException.java b/src/main/java/io/github/julucinho/httpclient/impl/exceptions/JsonProcessingRuntimeException.java new file mode 100644 index 0000000..f1cdb9f --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/exceptions/JsonProcessingRuntimeException.java @@ -0,0 +1,7 @@ +package io.github.julucinho.httpclient.impl.exceptions; + +public class JsonProcessingRuntimeException extends RuntimeException { + public JsonProcessingRuntimeException(String message) { + super(message); + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/exceptions/NoResponseHandlersAvailableForExecutionException.java b/src/main/java/io/github/julucinho/httpclient/impl/exceptions/NoResponseHandlersAvailableForExecutionException.java new file mode 100644 index 0000000..79ab178 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/exceptions/NoResponseHandlersAvailableForExecutionException.java @@ -0,0 +1,17 @@ +package io.github.julucinho.httpclient.impl.exceptions; + + +import io.github.julucinho.httpclient.HttpResponse; + +public class NoResponseHandlersAvailableForExecutionException extends RuntimeException { + + public NoResponseHandlersAvailableForExecutionException(HttpResponse httpResponse) { + super("No response handlers were available, not even the most generic one. " + .concat("For handling any not successful response set a generic handler at the request building process; ") + .concat("if you find interesting to have a specific response handler for the status code received (") + .concat(httpResponse.getStatusCode().toString()) + .concat("), set a specific one, also at the request building process. ") + ); + } + +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/exceptions/RetryNeededOnExceptionThrownException.java b/src/main/java/io/github/julucinho/httpclient/impl/exceptions/RetryNeededOnExceptionThrownException.java new file mode 100644 index 0000000..0541bd8 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/exceptions/RetryNeededOnExceptionThrownException.java @@ -0,0 +1,15 @@ +package io.github.julucinho.httpclient.impl.exceptions; + +import lombok.Getter; + +@Getter +public class RetryNeededOnExceptionThrownException extends RuntimeException{ + + private final Class exceptionType; + + public RetryNeededOnExceptionThrownException(Class exceptionType){ + super("Retry needed on ".concat(exceptionType.getName()).concat(" being thrown")); + this.exceptionType = exceptionType; + } + +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/exceptions/RetryNeededOnHttpStatusCodeException.java b/src/main/java/io/github/julucinho/httpclient/impl/exceptions/RetryNeededOnHttpStatusCodeException.java new file mode 100644 index 0000000..48d7dc6 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/impl/exceptions/RetryNeededOnHttpStatusCodeException.java @@ -0,0 +1,16 @@ +package io.github.julucinho.httpclient.impl.exceptions; + +import io.github.julucinho.httpclient.HttpResponse; +import lombok.Getter; + +@Getter +public class RetryNeededOnHttpStatusCodeException extends RuntimeException{ + + private final HttpResponse httpResponse; + + public RetryNeededOnHttpStatusCodeException(HttpResponse httpResponse){ + super("Retry needed on ".concat(httpResponse.getStatusCode().toString()).concat(" received")); + this.httpResponse = httpResponse; + } + +} diff --git a/src/main/java/io/github/julucinho/httpclient/utils/ThreadIdRetrievementUtil.java b/src/main/java/io/github/julucinho/httpclient/utils/ThreadIdRetrievementUtil.java new file mode 100644 index 0000000..8342686 --- /dev/null +++ b/src/main/java/io/github/julucinho/httpclient/utils/ThreadIdRetrievementUtil.java @@ -0,0 +1,16 @@ +package io.github.julucinho.httpclient.utils; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class ThreadIdRetrievementUtil { + + public static String retrieveThreadId(){ + return "Current thread: " + .concat(String.valueOf(Thread.currentThread().getId()) + .concat(":") + .concat(Thread.currentThread().getName())); + } + +} diff --git a/src/test/java/io/github/julucinho/httpclient/RetrierModelTest.java b/src/test/java/io/github/julucinho/httpclient/RetrierModelTest.java new file mode 100644 index 0000000..434f5af --- /dev/null +++ b/src/test/java/io/github/julucinho/httpclient/RetrierModelTest.java @@ -0,0 +1,26 @@ +package io.github.julucinho.httpclient; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class RetrierModelTest { + + @Test + @DisplayName("Should not return null reference when static constructor invoked") + void shouldNotReturnNullReferenceWhenStaticConstructorInvoked(){ + Assertions.assertNotNull(RetrierModel.withLimitOf(6)); + } + + @Test + @DisplayName("Should fill both fields when instantiated via static constructor") + void shouldFillBothFieldsWhenInstantiatedViaStaticConstructor(){ + var retrierModel = RetrierModel.withLimitOf(4); + Assertions.assertNotNull(retrierModel.getOriginalLimitForRetrying()); + Assertions.assertNotNull(retrierModel.getRetriesRemaining()); + } + +} diff --git a/src/test/java/io/github/julucinho/httpclient/impl/ExceptionThrownByHttpRequestCheckerTest.java b/src/test/java/io/github/julucinho/httpclient/impl/ExceptionThrownByHttpRequestCheckerTest.java new file mode 100644 index 0000000..5a5fccb --- /dev/null +++ b/src/test/java/io/github/julucinho/httpclient/impl/ExceptionThrownByHttpRequestCheckerTest.java @@ -0,0 +1,59 @@ +package io.github.julucinho.httpclient.impl; + +import io.github.julucinho.httpclient.RetrierModel; +import io.github.julucinho.httpclient.impl.exceptions.JsonProcessingRuntimeException; +import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnExceptionThrownException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Arrays; + +@ExtendWith(MockitoExtension.class) +class ExceptionThrownByHttpRequestCheckerTest { + + AbstractHttpRequestModel abstractHttpRequestModel; + + @BeforeEach + void setUp(){ + this.abstractHttpRequestModel = HttpRequestModelImpl.of("http://localhost:8080/tutstuts", new HttpRequestGetMethod()); + this.abstractHttpRequestModel.retryCountersByExceptionType.put(RuntimeException.class, RetryCounterImpl.of(RetrierModel.withLimitOf(4))); + this.abstractHttpRequestModel.exceptionHandlersByExceptionType.put(Exception.class, this::doNothing); + this.abstractHttpRequestModel.retryCountersByExceptionType.put(Exception.class, RetryCounterImpl.of(RetrierModel.withLimitOf(4))); + this.abstractHttpRequestModel.retryCountersByExceptionType.put(ClassNotFoundException.class, RetryCounterImpl.of(RetrierModel.withLimitOf(4))); + } + + private void doNothing(Exception exception) { } + + @Test + @DisplayName("Should not return null reference when static constructor invoked") + void shouldNotReturnNullReferenceWhenStaticConstructorInvoked(){ + Assertions.assertNotNull(ExceptionThrownByHttpRequestChecker.of(this.abstractHttpRequestModel)); + } + + @Test + @DisplayName("Should throw RetryNeededOnExceptionThrownException") + void shouldThrowRetryNeededOnExceptionThrownException(){ + Arrays.asList(new ClassNotFoundException(), new Exception(), new RuntimeException()).forEach(exceptionThrown -> { + var checker = ExceptionThrownByHttpRequestChecker.of(this.abstractHttpRequestModel); + Assertions.assertThrows(RetryNeededOnExceptionThrownException.class, () -> checker.checkOn(exceptionThrown)); + }); + + } + + @Test + @DisplayName("Should not throw RetryNeededOnExceptionThrownException") + void shouldNotThrowRetryNeededOnExceptionThrownException(){ + Arrays.asList(new JsonProcessingRuntimeException(""), new InterruptedException()).forEach(exceptionThrown -> { + var checker = ExceptionThrownByHttpRequestChecker.of(this.abstractHttpRequestModel); + Assertions.assertDoesNotThrow(() -> checker.checkOn(exceptionThrown)); + }); + + } + + + +} diff --git a/src/test/java/io/github/julucinho/httpclient/impl/FinalHttpRequestFactoryTest.java b/src/test/java/io/github/julucinho/httpclient/impl/FinalHttpRequestFactoryTest.java new file mode 100644 index 0000000..c67b258 --- /dev/null +++ b/src/test/java/io/github/julucinho/httpclient/impl/FinalHttpRequestFactoryTest.java @@ -0,0 +1,82 @@ +package io.github.julucinho.httpclient.impl; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.net.http.HttpRequest; + +@ExtendWith(MockitoExtension.class) +class FinalHttpRequestFactoryTest { + + @Test + @DisplayName("Should set query params right") + void shouldSetQueryParamsRight(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/uhg", new HttpRequestGetMethod()); + var queryParam1 = new HttpRequestQueryParameter("testKey", "testValue"); + var queryParam2 = new HttpRequestQueryParameter("testKey2", "testValue2"); + request.queryParameters.add(queryParam1); + request.queryParameters.add(queryParam2); + var finalHttpRequest = FinalHttpRequestFactory.makeFinalRequestForGetMethodFrom(request); + var queryParams = finalHttpRequest.uri().getQuery(); + Assertions.assertTrue(queryParams.contains(queryParam1.buildQueryParameter())); + Assertions.assertTrue(queryParams.contains(queryParam2.buildQueryParameter())); + } + + @Test + @DisplayName("Should set path variables right") + void shouldSetPathVariablesRight(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/uhg", new HttpRequestGetMethod()); + var pathVariable1 = new HttpRequestPathVariable("companies", "62333"); + var pathVariable2 = new HttpRequestPathVariable("users", "34234"); + request.pathVariables.add(pathVariable1); + request.pathVariables.add(pathVariable2); + var finalHttpRequest = FinalHttpRequestFactory.makeFinalRequestForGetMethodFrom(request); + var uri = finalHttpRequest.uri().toString(); + Assertions.assertTrue(uri.contains(pathVariable1.buildPathVariable())); + Assertions.assertTrue(uri.contains(pathVariable2.buildPathVariable())); + } + + @Test + @DisplayName("Should set body right") + void shouldSetBodyRight(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/uhg", new HttpRequestPostMethod()); + request.body = HttpRequest.BodyPublishers.ofString("{testField:\"testValue\"}"); + var finalHttpRequest = FinalHttpRequestFactory.makeFinalRequestForPostMethodFrom(request); + var bodyFromFinalRequest = finalHttpRequest.bodyPublisher().orElseThrow(); + Assertions.assertEquals(request.body, bodyFromFinalRequest); + } + + @Test + @DisplayName("Should make instance for get method") + void shouldMakeInstanceForGetMethod(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/uhg", new HttpRequestGetMethod()); + Assertions.assertNotNull(FinalHttpRequestFactory.makeFinalRequestForGetMethodFrom(request)); + } + + @Test + @DisplayName("Should make instance for delete method") + void shouldMakeInstanceForDeleteMethod(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/uhg", new HttpRequestDeleteMethod()); + Assertions.assertNotNull(FinalHttpRequestFactory.makeFinalRequestForDeleteMethodFrom(request)); + } + + @Test + @DisplayName("Should make instance for post method") + void shouldMakeInstanceForPostMethod(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/uhg", new HttpRequestPostMethod()); + request.body = HttpRequest.BodyPublishers.ofString("{testField:\"testValue\"}"); + Assertions.assertNotNull(FinalHttpRequestFactory.makeFinalRequestForPostMethodFrom(request)); + } + + @Test + @DisplayName("Should make instance for put method") + void shouldMakeInstanceForPutMethod(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/uhg", new HttpRequestPutMethod()); + request.body = HttpRequest.BodyPublishers.ofString("{testField:\"testValue\"}"); + Assertions.assertNotNull(FinalHttpRequestFactory.makeFinalRequestForPutMethodFrom(request)); + } + +} diff --git a/src/test/java/io/github/julucinho/httpclient/impl/HttpRequestBuilderImplTest.java b/src/test/java/io/github/julucinho/httpclient/impl/HttpRequestBuilderImplTest.java new file mode 100644 index 0000000..a55080f --- /dev/null +++ b/src/test/java/io/github/julucinho/httpclient/impl/HttpRequestBuilderImplTest.java @@ -0,0 +1,382 @@ +package io.github.julucinho.httpclient.impl; + +import io.github.julucinho.httpclient.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +@ExtendWith(MockitoExtension.class) +class HttpRequestBuilderImplTest { + + @Test + @DisplayName("Should add header correctly") + void shouldAddHeaderCorrectly(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var key = "chuchu"; + builder.andAddHeaderOf(key, "Duda"); + Assertions.assertNotNull(request.headers.get(key)); + } + + @Test + @DisplayName("Should return builder instance when finishing method of adding header") + void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingHeader(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var returnedBuilder = builder.andAddHeaderOf("chuchu", "Duda"); + Assertions.assertNotNull(returnedBuilder); + Assertions.assertEquals(builder, returnedBuilder); + } + + private static class HeadersFactoryForTestingMatters implements HttpRequestHeaderFactory{ + + static final String KEY_1 = "chuchu"; + static final String KEY_2 = "biridinho"; + static final String KEY_3 = "biridao"; + + @Override + public Map makeHeaders() { + var headers = new HashMap(); + headers.put(KEY_1, "Duda"); + headers.put(KEY_2, "Zena"); + headers.put(KEY_3, "Mingau"); + return headers; + } + } + + @Test + @DisplayName("Should add headers factory correctly") + void shouldAddHeadersFactoryCorrectly(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + builder.andAddHeadersFactory(new HeadersFactoryForTestingMatters()); + Arrays.asList(HeadersFactoryForTestingMatters.KEY_1, + HeadersFactoryForTestingMatters.KEY_2, + HeadersFactoryForTestingMatters.KEY_3) + .forEach(key -> Assertions.assertNotNull(request.headers.get(key))); + } + + @Test + @DisplayName("Should return builder instance when finishing method of adding headers factory") + void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingHeaderFactory(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var returnedBuilder = builder.andAddHeadersFactory(new HeadersFactoryForTestingMatters()); + Assertions.assertNotNull(returnedBuilder); + Assertions.assertEquals(builder, returnedBuilder); + } + + @Test + @DisplayName("Should add path variable correctly") + void shouldAddPathVariableCorrectly(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + builder.andAddPathVariableOf("test", "testValue"); + Assertions.assertFalse(request.pathVariables.isEmpty()); + Assertions.assertEquals("/test/testValue", request.pathVariables.get(0).buildPathVariable()); + } + + @Test + @DisplayName("Should return builder instance when finishing method of adding path variable") + void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingPathVariable(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var returnedBuilder = builder.andAddPathVariableOf("test", "testValue"); + Assertions.assertNotNull(returnedBuilder); + Assertions.assertEquals(builder, returnedBuilder); + } + + @Test + @DisplayName("Should add query param correctly") + void shouldAddQueryParamCorrectly(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + builder.andAddQueryParameterOf("test", "testValue"); + Assertions.assertFalse(request.queryParameters.isEmpty()); + Assertions.assertEquals("test=testValue", request.queryParameters.get(0).buildQueryParameter()); + } + + @Test + @DisplayName("Should return builder instance when finishing method of adding query param") + void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingQueryParam(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var returnedBuilder = builder.andAddQueryParameterOf("test", "testValue"); + Assertions.assertNotNull(returnedBuilder); + Assertions.assertEquals(builder, returnedBuilder); + } + + @Test + @DisplayName("Should finish building process returning request model instance") + void shouldFinishBuildingProcessReturningRequestModelInstance(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var resultFromBuilding = builder.andFinishBuildingModel(); + Assertions.assertNotNull(resultFromBuilding); + Assertions.assertEquals(request, resultFromBuilding); + } + + @Test + @DisplayName("Should add response handler by status code correctly") + void shouldAddResponseHandlerByStatusCodeCorrectly(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var statusCode = 400; + var handler = (HttpResponseHandler) response -> System.out.println("hello, 400"); + builder.andAddResponseHandlerByHttpStatusCode(statusCode, handler); + Assertions.assertFalse(request.responseHandlersByStatusCode.isEmpty()); + Assertions.assertEquals(handler, request.responseHandlersByStatusCode.get(statusCode)); + } + + @Test + @DisplayName("Should return builder instance when finishing method of adding handler by status code") + void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingResponseHandlerByStatusCode(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var statusCode = 400; + var handler = (HttpResponseHandler) response -> System.out.println("hello, 400"); + var returnedBuilder = builder.andAddResponseHandlerByHttpStatusCode(statusCode, handler); + Assertions.assertNotNull(returnedBuilder); + Assertions.assertEquals(builder, returnedBuilder); + } + + private static class HttpResponseHandlersByStatusCodeFactoryForTestingMatters implements HttpResponseHandlersByStatusCodeFactory{ + + static final Integer KEY_1 = 400; + static final Integer KEY_2 = 401; + static final Integer KEY_3 = 403; + + @Override + public Map makeHandlers() { + var handlers = new HashMap(); + handlers.put(KEY_1, response -> System.out.println("Hello, 400")); + handlers.put(KEY_2, response -> System.out.println("Hello, 401")); + handlers.put(KEY_3, response -> System.out.println("Hello, 403")); + return handlers; + } + } + + @Test + @DisplayName("Should add response handlers by status code factory correctly") + void shouldAddHttpResponseHandlersByStatusCodeFactoryCorrectly(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + builder.andAddResponseHandlersByHttpStatusCodeFactory(new HttpResponseHandlersByStatusCodeFactoryForTestingMatters()); + Arrays.asList(HttpResponseHandlersByStatusCodeFactoryForTestingMatters.KEY_1, + HttpResponseHandlersByStatusCodeFactoryForTestingMatters.KEY_2, + HttpResponseHandlersByStatusCodeFactoryForTestingMatters.KEY_3) + .forEach(key -> Assertions.assertNotNull(request.responseHandlersByStatusCode.get(key))); + } + + @Test + @DisplayName("Should return builder instance when finishing method of adding response handlers by status code factory") + void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingHttpResponseHandlersByStatusCodeFactory(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var returnedBuilder = builder.andAddResponseHandlersByHttpStatusCodeFactory(new HttpResponseHandlersByStatusCodeFactoryForTestingMatters()); + Assertions.assertNotNull(returnedBuilder); + Assertions.assertEquals(builder, returnedBuilder); + } + + @Test + @DisplayName("Should add response handler for any not successful response correctly") + void shouldAddResponseHandlerForAnyNotSuccessfulResponseCorrectly(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var handler = (HttpResponseHandler) response -> System.out.println("hello, 400"); + builder.andAddResponseHandlerForAnyNotSuccessfulResponse(handler); + Assertions.assertNotNull(request.genericResponseHandler); + Assertions.assertEquals(handler, request.genericResponseHandler); + } + + @Test + @DisplayName("Should return builder instance when finishing method of adding response handler for any not successful response") + void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingResponseHandlerForAnyNotSuccessfulResponse(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var handler = (HttpResponseHandler) response -> System.out.println("hello, 400"); + var returnedBuilder = builder.andAddResponseHandlerForAnyNotSuccessfulResponse(handler); + Assertions.assertNotNull(returnedBuilder); + Assertions.assertEquals(builder, returnedBuilder); + } + + @Test + @DisplayName("Should add response handler by exception type correctly") + void shouldAddResponseHandlerByExceptionTypeCorrectly(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var handler = (ExceptionHandler) exception -> System.out.println("hello, exception"); + var exceptionToHandle = RuntimeException.class; + builder.andAddExceptionHandlerByExceptionType(exceptionToHandle, handler); + Assertions.assertFalse(request.exceptionHandlersByExceptionType.isEmpty()); + Assertions.assertEquals(handler, request.exceptionHandlersByExceptionType.get(exceptionToHandle)); + } + + @Test + @DisplayName("Should return builder instance when finishing method of adding response handler by exception type") + void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingResponseHandlerByExceptionType(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var handler = (ExceptionHandler) exception -> System.out.println("hello, exception"); + var exceptionToHandle = RuntimeException.class; + var returnedBuilder = builder.andAddExceptionHandlerByExceptionType(exceptionToHandle, handler); + Assertions.assertNotNull(returnedBuilder); + Assertions.assertEquals(builder, returnedBuilder); + } + + @Test + @DisplayName("Should add retrier by status code correctly") + void shouldAddRetrierByHttpStatusCodeCorrectly(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var retrier = RetrierModel.withLimitOf(4); + var statusCode = 503; + builder.andAddRetrierByHttpStatusCode(statusCode, retrier); + Assertions.assertFalse(request.retryCountersByStatusCode.isEmpty()); + Assertions.assertTrue(request.retryCountersByStatusCode.get(statusCode).thereIsRetryAvailable()); + } + + @Test + @DisplayName("Should return builder instance when finishing method of adding retrier by status code") + void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingRetrierByHttpStatusCode(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var retrier = RetrierModel.withLimitOf(4); + var statusCode = 503; + var returnedBuilder = builder.andAddRetrierByHttpStatusCode(statusCode, retrier); + Assertions.assertNotNull(returnedBuilder); + Assertions.assertEquals(builder, returnedBuilder); + } + + @Test + @DisplayName("Should add retrier by exception type correctly") + void shouldAddRetrierByExceptionTypeCorrectly(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var retrier = RetrierModel.withLimitOf(4); + var exceptionType = RuntimeException.class; + builder.andAddRetrierByExceptionType(exceptionType, retrier); + Assertions.assertFalse(request.retryCountersByExceptionType.isEmpty()); + Assertions.assertTrue(request.retryCountersByExceptionType.get(exceptionType).thereIsRetryAvailable()); + } + + @Test + @DisplayName("Should return builder instance when finishing method of adding retrier by exception type") + void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingRetrierByExceptionType(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var retrier = RetrierModel.withLimitOf(4); + var exceptionType = RuntimeException.class; + var returnedBuilder = builder.andAddRetrierByExceptionType(exceptionType, retrier); + Assertions.assertNotNull(returnedBuilder); + Assertions.assertEquals(builder, returnedBuilder); + } + + private static class HttpExceptionHandlersByExceptionTypeFactoryForTestingMatters implements HttpExceptionHandlersByExceptionTypeFactory { + + static final Class KEY_1 = RuntimeException.class; + static final Class KEY_2 = InterruptedException.class; + static final Class KEY_3 = Exception.class; + + + @Override + public Map, ExceptionHandler> makeHandlers() { + var handlers = new HashMap, ExceptionHandler>(); + handlers.put(KEY_1, exception -> System.out.println("Hello, RuntimeException")); + handlers.put(KEY_2, exception -> System.out.println("Hello, InterruptedException")); + handlers.put(KEY_3, exception -> System.out.println("Hello, Exception")); + return handlers; + } + } + + @Test + @DisplayName("Should add http response handlers by exception type factory correctly") + void shouldAddHttpResponseHandlersByExceptionTypeFactoryCorrectly(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + builder.andAddExceptionHandlersByExceptionTypeFactory(new HttpExceptionHandlersByExceptionTypeFactoryForTestingMatters()); + Arrays.asList(HttpExceptionHandlersByExceptionTypeFactoryForTestingMatters.KEY_1, + HttpExceptionHandlersByExceptionTypeFactoryForTestingMatters.KEY_2, + HttpExceptionHandlersByExceptionTypeFactoryForTestingMatters.KEY_3) + .forEach(key -> Assertions.assertNotNull(request.exceptionHandlersByExceptionType.get(key))); + } + + @Test + @DisplayName("Should return builder instance when finishing method of adding http response handlers by exception type factory") + void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingHttpResponseHandlersByExceptionTypeFactory(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var returnedBuilder = builder.andAddExceptionHandlersByExceptionTypeFactory(new HttpExceptionHandlersByExceptionTypeFactoryForTestingMatters()); + Assertions.assertNotNull(returnedBuilder); + Assertions.assertEquals(builder, returnedBuilder); + } + + private static class RetriersByStatusCodeFactoryForTestingMatters implements RetriersByStatusCodeFactory { + + static final Integer KEY_1 = 400; + static final Integer KEY_2 = 401; + static final Integer KEY_3 = 403; + + + @Override + public Map makeRetriers() { + var retriers = new HashMap(); + retriers.put(KEY_1, RetrierModel.withLimitOf(4)); + retriers.put(KEY_2, RetrierModel.withLimitOf(8)); + retriers.put(KEY_3, RetrierModel.withLimitOf(16)); + return retriers; + } + } + + @Test + @DisplayName("Should add retriers by status code factory correctly") + void shouldAddRetriersByStatusCodeFactoryCorrectly(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + builder.andAddRetriersByHttpStatusCodeFactory(new RetriersByStatusCodeFactoryForTestingMatters()); + Arrays.asList(RetriersByStatusCodeFactoryForTestingMatters.KEY_1, + RetriersByStatusCodeFactoryForTestingMatters.KEY_2, + RetriersByStatusCodeFactoryForTestingMatters.KEY_3) + .forEach(key -> Assertions.assertNotNull(request.retryCountersByStatusCode.get(key))); + } + + @Test + @DisplayName("Should return builder instance when finishing method of adding retriers by status code factory") + void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingRetriersByStatusCodeFactory(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + var returnedBuilder = builder.andAddRetriersByHttpStatusCodeFactory(new RetriersByStatusCodeFactoryForTestingMatters()); + Assertions.assertNotNull(returnedBuilder); + Assertions.assertEquals(builder, returnedBuilder); + } + + @Test + @DisplayName("Should set proxy address to the request model") + void shouldSetProxyAddressToTheRequestModel(){ + var host = "tururu.com"; + var port = 8080; + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request); + builder.andAddProxyAddress(host, port); + Assertions.assertNotNull(request.proxyAddress); + Assertions.assertNotNull(request.proxyAddress.getHost()); + Assertions.assertNotNull(request.proxyAddress.getPort()); + Assertions.assertEquals(host, request.proxyAddress.getHost()); + Assertions.assertEquals(port, request.proxyAddress.getPort()); + } + + @Test + @DisplayName("Should return the builder instance after 'andAddProxyAddress' has been called") + void shouldReturnTheBuilderInstanceAfterAndAddProxyAddressHasBeenCalled(){ + var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImpl.of(request).andAddProxyAddress("tururu.com", 8080); + Assertions.assertNotNull(builder); + } + +} diff --git a/src/test/java/io/github/julucinho/httpclient/impl/HttpRequestStarterImplTest.java b/src/test/java/io/github/julucinho/httpclient/impl/HttpRequestStarterImplTest.java new file mode 100644 index 0000000..901be85 --- /dev/null +++ b/src/test/java/io/github/julucinho/httpclient/impl/HttpRequestStarterImplTest.java @@ -0,0 +1,41 @@ +package io.github.julucinho.httpclient.impl; + +import io.github.julucinho.httpclient.HttpRequestStarter; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.net.http.HttpRequest; + +@ExtendWith(MockitoExtension.class) +class HttpRequestStarterImplTest { + + HttpRequestStarter httpRequestStarter = new HttpRequestStarterImpl(); + + @Test + @DisplayName("Should return non null instance of builder when starting for delete method") + void shouldReturnNonNullInstanceOfBuilderWhenStartingForDeleteMethod(){ + Assertions.assertNotNull(this.httpRequestStarter.startDeleteRequestModelFor("http://localhost:2222")); + } + + @Test + @DisplayName("Should return non null instance of builder when starting for get method") + void shouldReturnNonNullInstanceOfBuilderWhenStartingForGetMethod(){ + Assertions.assertNotNull(this.httpRequestStarter.startGetRequestModelFor("http://localhost:2222")); + } + + @Test + @DisplayName("Should return non null instance of builder when starting for post method") + void shouldReturnNonNullInstanceOfBuilderWhenStartingForPostMethod(){ + Assertions.assertNotNull(this.httpRequestStarter.startPostRequestModelFor("http://localhost:2222", HttpRequest.BodyPublishers.ofString("{someRandomJsonField: \"someRandomValue\"}"))); + } + + @Test + @DisplayName("Should return non null instance of builder when starting for put method") + void shouldReturnNonNullInstanceOfBuilderWhenStartingForPutMethod(){ + Assertions.assertNotNull(this.httpRequestStarter.startPutRequestModelFor("http://localhost:2222", HttpRequest.BodyPublishers.ofString("{someRandomJsonField: \"someRandomValue\"}"))); + } + +} diff --git a/src/test/java/io/github/julucinho/httpclient/impl/HttpResponseImplTest.java b/src/test/java/io/github/julucinho/httpclient/impl/HttpResponseImplTest.java new file mode 100644 index 0000000..cade49a --- /dev/null +++ b/src/test/java/io/github/julucinho/httpclient/impl/HttpResponseImplTest.java @@ -0,0 +1,97 @@ +package io.github.julucinho.httpclient.impl; + +import com.fasterxml.jackson.core.type.TypeReference; +import io.github.julucinho.httpclient.HttpRequestModel; +import io.github.julucinho.httpclient.HttpResponse; +import lombok.Getter; +import lombok.Setter; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; + +@ExtendWith(MockitoExtension.class) +class HttpResponseImplTest { + + HttpResponseImpl httpResponse; + @Mock + HttpRequestModel httpRequestModel; + @Mock + java.net.http.HttpResponse unwrappedHttpResponse; + + @BeforeEach + void setUp(){ + this.httpResponse = new HttpResponseImpl(this.httpRequestModel, this.unwrappedHttpResponse); + } + + @Test + @DisplayName("Should return the status code correctly") + void shouldReturnTheStatusCodeCorrectly(){ + var expected = 200; + Mockito.when(this.unwrappedHttpResponse.statusCode()).thenReturn(expected); + var actual = this.httpResponse.getStatusCode(); + Assertions.assertEquals(expected, actual); + } + + @Test + @DisplayName("Should return the request model correctly") + void shouldReturnTheRequestModelCorrectly(){ + Assertions.assertEquals(this.httpRequestModel, this.httpResponse.getHttpRequest()); + } + + @Getter + @Setter + private static class TestingDecoding{ + private String test; + } + + @Test + @DisplayName("Should return body decoded to the specified type as parameter") + void shouldReturnBodyDecodedToTheSpecifiedTypeAsParameter(){ + Mockito.when(this.unwrappedHttpResponse.body()).thenReturn("{\"test\":\"value\"}"); + var result = this.httpResponse.getBodyAs(TestingDecoding.class); + Assertions.assertNotNull(result); + Assertions.assertEquals("value", result.getTest()); + } + + @Test + @DisplayName("Should return body decoded to the specified type reference as parameter") + void shouldReturnBodyDecodedToTheSpecifiedTypeReferenceAsParameter(){ + Mockito.when(this.unwrappedHttpResponse.body()).thenReturn("[{\"test\":\"value\"}]"); + var result = this.httpResponse.getBodyAs(new TypeReference>() {}); + Assertions.assertNotNull(result); + Assertions.assertEquals(1, result.size()); + Assertions.assertEquals("value", result.get(0).getTest()); + } + + @Test + @DisplayName("Should perform the action") + void shouldPerformTheAction(){ + var textBuilder = new StringBuilder("0"); + Mockito.when(this.unwrappedHttpResponse.statusCode()).thenReturn(400); + this.httpResponse.ifNeedsHandling(response -> this.concatSomeChars(response, textBuilder)); + var finalText = textBuilder.toString(); + Assertions.assertEquals("00", finalText); + } + + @Test + @DisplayName("Should not perform the action") + void shouldNotPerformTheAction(){ + var textBuilder = new StringBuilder("0"); + Mockito.when(this.unwrappedHttpResponse.statusCode()).thenReturn(200); + this.httpResponse.ifNeedsHandling(response -> this.concatSomeChars(response, textBuilder)); + var finalText = textBuilder.toString(); + Assertions.assertEquals("0", finalText); + } + + private void concatSomeChars(HttpResponse httpResponse, StringBuilder textBuilder) { + textBuilder.append("0"); + } + +} diff --git a/src/test/java/io/github/julucinho/httpclient/impl/HttpResponseStatusCodeCheckerTest.java b/src/test/java/io/github/julucinho/httpclient/impl/HttpResponseStatusCodeCheckerTest.java new file mode 100644 index 0000000..c6979ce --- /dev/null +++ b/src/test/java/io/github/julucinho/httpclient/impl/HttpResponseStatusCodeCheckerTest.java @@ -0,0 +1,65 @@ +package io.github.julucinho.httpclient.impl; + +import io.github.julucinho.httpclient.HttpResponse; +import io.github.julucinho.httpclient.RetrierModel; +import io.github.julucinho.httpclient.impl.exceptions.NoResponseHandlersAvailableForExecutionException; +import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnHttpStatusCodeException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class HttpResponseStatusCodeCheckerTest { + + HttpResponseStatusCodeChecker httpResponseStatusCodeChecker; + AbstractHttpRequestModel httpRequestModel; + @Mock + HttpResponse response; + + @BeforeEach + void setUp(){ + this.httpRequestModel = HttpRequestModelImpl.of("", new HttpRequestGetMethod()); + this.httpResponseStatusCodeChecker = HttpResponseStatusCodeChecker.of(this.httpRequestModel); + } + + @Test + @DisplayName("Should throw parameterized exception type for the received status code when there is no retrier set") + void shouldThrowParameterizedExceptionTypeForTheReceivedStatusCodeWhenThereIsNoRetrierSet(){ + Mockito.when(this.response.getStatusCode()).thenReturn(400); + this.httpRequestModel.responseHandlersByStatusCode.put(400, httpResponse -> {throw new RuntimeException();}); + Assertions.assertThrows(RuntimeException.class, () -> this.httpResponseStatusCodeChecker.checkOutHandlersFor(this.response)); + } + + @Test + @DisplayName("Should throw RetryNeededOnHttpStatusCodeException for the received status code when there is retrier set") + void shouldThrowRetryNeededOnHttpStatusCodeExceptionForTheReceivedStatusCodeWhenThereIsRetrierSet(){ + Mockito.when(this.response.getStatusCode()).thenReturn(400); + this.httpRequestModel.responseHandlersByStatusCode.put(400, httpResponse -> {throw new RuntimeException();}); + this.httpRequestModel.retryCountersByStatusCode.put(400, RetryCounterImpl.of(RetrierModel.withLimitOf(4))); + Assertions.assertThrows(RetryNeededOnHttpStatusCodeException.class, () -> this.httpResponseStatusCodeChecker.checkOutHandlersFor(this.response)); + } + + @Test + @DisplayName("Should throw no response handlers available for execution exception") + void shouldThrowNoResponseHandlersAvailableForExecutionException(){ + Mockito.when(this.response.getStatusCode()).thenReturn(400); + Assertions.assertThrows(NoResponseHandlersAvailableForExecutionException.class, () -> this.httpResponseStatusCodeChecker.checkOutHandlersFor(this.response)); + } + + @Test + @DisplayName("Should just execute the handler for the received status code when there is no retrier set") + void shouldJustExecuteTheHandlerForTheReceivedStatusCodeWhenThereIsNoRetrierSet(){ + var stringBuilder = new StringBuilder("0"); + Mockito.when(this.response.getStatusCode()).thenReturn(400); + this.httpRequestModel.responseHandlersByStatusCode.put(400, httpResponse -> stringBuilder.append("0")); + this.httpResponseStatusCodeChecker.checkOutHandlersFor(this.response); + var result = stringBuilder.toString(); + Assertions.assertEquals(2, result.length()); + } + +} diff --git a/src/test/java/io/github/julucinho/httpclient/utils/ThreadIdRetrievementUtilTest.java b/src/test/java/io/github/julucinho/httpclient/utils/ThreadIdRetrievementUtilTest.java new file mode 100644 index 0000000..4856b21 --- /dev/null +++ b/src/test/java/io/github/julucinho/httpclient/utils/ThreadIdRetrievementUtilTest.java @@ -0,0 +1,30 @@ +package io.github.julucinho.httpclient.utils; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class ThreadIdRetrievementUtilTest { + + @Test + @DisplayName("Should return filled string") + void shouldReturnFilledString(){ + Assertions.assertFalse(ThreadIdRetrievementUtil.retrieveThreadId().isEmpty()); + } + + @Test + @DisplayName("Should not return a blank string") + void shouldNotReturnBlankString(){ + Assertions.assertFalse(ThreadIdRetrievementUtil.retrieveThreadId().isBlank()); + } + + @Test + @DisplayName("Should not return null reference") + void shouldNotReturnNullReference(){ + Assertions.assertNotNull(ThreadIdRetrievementUtil.retrieveThreadId()); + } + +} From 8bc3fe47df347781de5f4a5964f6125ae7190ec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Z=C3=A9=20L=C3=BAcio=20Jr?= Date: Tue, 2 Apr 2024 09:01:05 -0300 Subject: [PATCH 2/2] codebase --- .gitignore | 2 + pom.xml | 241 ++++++------------ .../http_client/ExceptionHandler.java | 12 + ...ceptionHandlersByExceptionTypeFactory.java | 4 +- .../http_client/HttpRequestBuilder.java | 12 + .../HttpRequestBuilderForHandlers.java | 46 ++++ .../HttpRequestBuilderForRetrying.java | 16 ++ .../http_client/HttpRequestHeaderFactory.java | 16 ++ .../http_client/HttpRequestMethod.java | 11 + .../http_client}/HttpRequestModel.java | 2 +- .../http_client/HttpRequestStarter.java | 12 + .../http_client/HttpResponse.java | 48 ++++ .../http_client/HttpResponseHandler.java | 12 + ...tpResponseHandlersByStatusCodeFactory.java | 4 +- .../http_client}/RetrierModel.java | 2 +- .../RetriersByStatusCodeFactory.java | 4 +- .../http_client}/RetryCounter.java | 2 +- .../http_client/commons/HandlersFactory.java | 11 + .../RetriersByExceptionTypeFactory.java | 4 +- .../http_client}/commons/RetriersFactory.java | 2 +- .../AbstractHttpRequestBuilder.java | 4 +- .../AbstractHttpRequestModel.java | 4 +- .../AbstractHttpResponse.java | 6 +- .../ExceptionThrownByHttpRequestChecker.java | 8 +- .../FinalHttpRequestExecutor.java | 6 +- .../FinalHttpRequestFactory.java | 8 +- .../HttpRequestBuilderImplementation.java | 105 ++++++++ .../HttpRequestDeleteMethod.java | 10 +- .../HttpRequestExecutionManager.java | 8 +- .../HttpRequestGetMethod.java | 10 +- .../HttpRequestModelImplementation.java} | 10 +- .../HttpRequestPathVariable.java | 24 ++ .../HttpRequestPostMethod.java | 10 +- .../HttpRequestPutMethod.java | 10 +- .../HttpRequestQueryParameter.java | 4 +- .../HttpRequestStarterImplementation.java | 30 +++ .../HttpResponseImplementation.java} | 12 +- .../HttpResponseStatusCodeChecker.java | 10 +- .../implementations}/ObjectMapperFactory.java | 2 +- .../implementations}/ProxyAddressModel.java | 2 +- .../RetryCounterImplementation.java} | 10 +- .../exceptions/IORuntimeException.java | 2 +- .../InterruptedRuntimeException.java | 2 +- .../JsonProcessingRuntimeException.java | 2 +- ...andlersAvailableForExecutionException.java | 4 +- ...RetryNeededOnExceptionThrownException.java | 2 +- .../RetryNeededOnHttpStatusCodeException.java | 4 +- .../utils/ThreadIdRetrievementUtil.java | 2 +- .../httpclient/ExceptionHandler.java | 8 - .../httpclient/HttpRequestBuilder.java | 12 - .../HttpRequestBuilderForHandlers.java | 12 - .../HttpRequestBuilderForRetrying.java | 16 -- .../httpclient/HttpRequestHeaderFactory.java | 9 - .../httpclient/HttpRequestMethod.java | 11 - .../httpclient/HttpRequestStarter.java | 12 - .../julucinho/httpclient/HttpResponse.java | 15 -- .../httpclient/HttpResponseHandler.java | 8 - .../httpclient/commons/HandlersFactory.java | 7 - .../impl/HttpRequestBuilderImpl.java | 105 -------- .../impl/HttpRequestPathVariable.java | 15 -- .../impl/HttpRequestStarterImpl.java | 30 --- .../http_client}/RetrierModelTest.java | 2 +- ...ceptionThrownByHttpRequestCheckerTest.java | 16 +- .../FinalHttpRequestFactoryTest.java | 23 +- ...HttpRequestBuilderImplementationTest.java} | 173 ++++++------- ...HttpRequestStarterImplementationTest.java} | 16 +- .../HttpResponseImplementationTest.java} | 12 +- .../HttpResponseStatusCodeCheckerTest.java | 14 +- .../utils/ThreadIdRetrievementUtilTest.java | 2 +- 69 files changed, 651 insertions(+), 641 deletions(-) create mode 100644 src/main/java/com/clean_arch_enablers/http_client/ExceptionHandler.java rename src/main/java/{io/github/julucinho/httpclient => com/clean_arch_enablers/http_client}/HttpExceptionHandlersByExceptionTypeFactory.java (59%) create mode 100644 src/main/java/com/clean_arch_enablers/http_client/HttpRequestBuilder.java create mode 100644 src/main/java/com/clean_arch_enablers/http_client/HttpRequestBuilderForHandlers.java create mode 100644 src/main/java/com/clean_arch_enablers/http_client/HttpRequestBuilderForRetrying.java create mode 100644 src/main/java/com/clean_arch_enablers/http_client/HttpRequestHeaderFactory.java create mode 100644 src/main/java/com/clean_arch_enablers/http_client/HttpRequestMethod.java rename src/main/java/{io/github/julucinho/httpclient => com/clean_arch_enablers/http_client}/HttpRequestModel.java (83%) create mode 100644 src/main/java/com/clean_arch_enablers/http_client/HttpRequestStarter.java create mode 100644 src/main/java/com/clean_arch_enablers/http_client/HttpResponse.java create mode 100644 src/main/java/com/clean_arch_enablers/http_client/HttpResponseHandler.java rename src/main/java/{io/github/julucinho/httpclient => com/clean_arch_enablers/http_client}/HttpResponseHandlersByStatusCodeFactory.java (56%) rename src/main/java/{io/github/julucinho/httpclient => com/clean_arch_enablers/http_client}/RetrierModel.java (92%) rename src/main/java/{io/github/julucinho/httpclient => com/clean_arch_enablers/http_client}/RetriersByStatusCodeFactory.java (52%) rename src/main/java/{io/github/julucinho/httpclient => com/clean_arch_enablers/http_client}/RetryCounter.java (70%) create mode 100644 src/main/java/com/clean_arch_enablers/http_client/commons/HandlersFactory.java rename src/main/java/{io/github/julucinho/httpclient => com/clean_arch_enablers/http_client}/commons/RetriersByExceptionTypeFactory.java (57%) rename src/main/java/{io/github/julucinho/httpclient => com/clean_arch_enablers/http_client}/commons/RetriersFactory.java (54%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/AbstractHttpRequestBuilder.java (62%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/AbstractHttpRequestModel.java (91%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/AbstractHttpResponse.java (60%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/ExceptionThrownByHttpRequestChecker.java (67%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/FinalHttpRequestExecutor.java (84%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/FinalHttpRequestFactory.java (89%) create mode 100644 src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestBuilderImplementation.java rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/HttpRequestDeleteMethod.java (61%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/HttpRequestExecutionManager.java (71%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/HttpRequestGetMethod.java (61%) rename src/main/java/{io/github/julucinho/httpclient/impl/HttpRequestModelImpl.java => com/clean_arch_enablers/http_client/implementations/HttpRequestModelImplementation.java} (75%) create mode 100644 src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestPathVariable.java rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/HttpRequestPostMethod.java (60%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/HttpRequestPutMethod.java (60%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/HttpRequestQueryParameter.java (73%) create mode 100644 src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestStarterImplementation.java rename src/main/java/{io/github/julucinho/httpclient/impl/HttpResponseImpl.java => com/clean_arch_enablers/http_client/implementations/HttpResponseImplementation.java} (75%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/HttpResponseStatusCodeChecker.java (80%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/ObjectMapperFactory.java (89%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/ProxyAddressModel.java (72%) rename src/main/java/{io/github/julucinho/httpclient/impl/RetryCounterImpl.java => com/clean_arch_enablers/http_client/implementations/RetryCounterImplementation.java} (62%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/exceptions/IORuntimeException.java (72%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/exceptions/InterruptedRuntimeException.java (70%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/exceptions/JsonProcessingRuntimeException.java (69%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/exceptions/NoResponseHandlersAvailableForExecutionException.java (83%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/exceptions/RetryNeededOnExceptionThrownException.java (84%) rename src/main/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/exceptions/RetryNeededOnHttpStatusCodeException.java (74%) rename src/main/java/{io/github/julucinho/httpclient => com/clean_arch_enablers/http_client}/utils/ThreadIdRetrievementUtil.java (88%) delete mode 100644 src/main/java/io/github/julucinho/httpclient/ExceptionHandler.java delete mode 100644 src/main/java/io/github/julucinho/httpclient/HttpRequestBuilder.java delete mode 100644 src/main/java/io/github/julucinho/httpclient/HttpRequestBuilderForHandlers.java delete mode 100644 src/main/java/io/github/julucinho/httpclient/HttpRequestBuilderForRetrying.java delete mode 100644 src/main/java/io/github/julucinho/httpclient/HttpRequestHeaderFactory.java delete mode 100644 src/main/java/io/github/julucinho/httpclient/HttpRequestMethod.java delete mode 100644 src/main/java/io/github/julucinho/httpclient/HttpRequestStarter.java delete mode 100644 src/main/java/io/github/julucinho/httpclient/HttpResponse.java delete mode 100644 src/main/java/io/github/julucinho/httpclient/HttpResponseHandler.java delete mode 100644 src/main/java/io/github/julucinho/httpclient/commons/HandlersFactory.java delete mode 100644 src/main/java/io/github/julucinho/httpclient/impl/HttpRequestBuilderImpl.java delete mode 100644 src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPathVariable.java delete mode 100644 src/main/java/io/github/julucinho/httpclient/impl/HttpRequestStarterImpl.java rename src/test/java/{io/github/julucinho/httpclient => com/clean_arch_enablers/http_client}/RetrierModelTest.java (95%) rename src/test/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/ExceptionThrownByHttpRequestCheckerTest.java (73%) rename src/test/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/FinalHttpRequestFactoryTest.java (71%) rename src/test/java/{io/github/julucinho/httpclient/impl/HttpRequestBuilderImplTest.java => com/clean_arch_enablers/http_client/implementations/HttpRequestBuilderImplementationTest.java} (64%) rename src/test/java/{io/github/julucinho/httpclient/impl/HttpRequestStarterImplTest.java => com/clean_arch_enablers/http_client/implementations/HttpRequestStarterImplementationTest.java} (72%) rename src/test/java/{io/github/julucinho/httpclient/impl/HttpResponseImplTest.java => com/clean_arch_enablers/http_client/implementations/HttpResponseImplementationTest.java} (89%) rename src/test/java/{io/github/julucinho/httpclient/impl => com/clean_arch_enablers/http_client/implementations}/HttpResponseStatusCodeCheckerTest.java (83%) rename src/test/java/{io/github/julucinho/httpclient => com/clean_arch_enablers/http_client}/utils/ThreadIdRetrievementUtilTest.java (94%) diff --git a/.gitignore b/.gitignore index 549e00a..2c1946c 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,5 @@ build/ ### VS Code ### .vscode/ + +settings.xml \ No newline at end of file diff --git a/pom.xml b/pom.xml index 5717e44..5a0498c 100644 --- a/pom.xml +++ b/pom.xml @@ -3,205 +3,118 @@ 4.0.0 - - io.github.julucinho - httpClient - 1.0.2 - - httpClient - https://github.com/julucinho/http-client - - Lib for making implementation of http requests easy. - + CAE HTTP Client + Meant for enabling applications to use HTTP at ease, without having to compromise the clean state of the overall architecture. + https://github.com/clean-arch-enablers-project/cae-utils-http-client/blob/main/README.md + com.clean-arch-enablers + http-client + 0.0.1 + jar - The Apache License, Version 2.0 + Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt + + https://github.com/clean-arch-enablers-project/cae-utils-http-client + - J. Lúcio de Almeida Júnior + Zé Lúcio Jr. joselucioalmeidajunior@gmail.com + https://github.com/zeluciojr - - scm:git:git@github.com:julucinho/http-client.git - scm:git:ssh://github.com:julucinho/http-client.git - https://github.com/julucinho/http-client/tree/main - - - - ossrh - https://s01.oss.sonatype.org/content/repositories/snapshots - - - ossrh - https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ - - + - UTF-8 11 11 + UTF-8 - org.junit.jupiter - junit-jupiter - 5.7.1 - test - - - org.assertj - assertj-core - 3.19.0 - test + org.projectlombok + lombok + 1.18.24 com.fasterxml.jackson.core jackson-databind - 2.13.1 + 2.16.1 + + + org.junit.jupiter + junit-jupiter-engine + 5.9.0 + test org.mockito - mockito-junit-jupiter - 4.3.1 + mockito-core + 4.8.0 test - org.projectlombok - lombok - 1.18.22 + org.mockito + mockito-junit-jupiter + 4.8.0 + test - - - - - - maven-clean-plugin - 3.1.0 - - - - maven-resources-plugin - 3.0.2 - - - maven-compiler-plugin - 3.8.0 - - - maven-surefire-plugin - 2.22.1 - - - maven-jar-plugin - 3.0.2 - - - maven-install-plugin - 2.5.2 - - - maven-deploy-plugin - 2.8.2 - - - - maven-site-plugin - 3.7.1 - - - maven-project-info-reports-plugin - 3.0.0 - - - org.sonatype.plugins - nexus-staging-maven-plugin - 1.6.7 - true - - ossrh - https://s01.oss.sonatype.org/ - true - - - - - org.apache.maven.plugins - maven-compiler-plugin + org.sonatype.central + central-publishing-maven-plugin + 0.4.0 + true - 11 - 11 + central + true + + org.apache.maven.plugins + maven-javadoc-plugin + 3.3.0 + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.2.1 + + + attach-sources + + jar + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.6 + + + sign-artifacts + verify + + sign + + + + - - - release - - - performRelease - true - - - - C:\Program Files (x86)\GnuPG\bin\gpg.exe - - - - - org.apache.maven.plugins - maven-gpg-plugin - 1.6 - - - sign-artifacts - verify - - sign - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 3.2.0 - - - attach-javadocs - - jar - - - - - ${java.home}/bin/javadoc - - - - org.apache.maven.plugins - maven-source-plugin - 3.2.1 - - - attach-sources - - jar-no-fork - - - - - - - - diff --git a/src/main/java/com/clean_arch_enablers/http_client/ExceptionHandler.java b/src/main/java/com/clean_arch_enablers/http_client/ExceptionHandler.java new file mode 100644 index 0000000..210f0de --- /dev/null +++ b/src/main/java/com/clean_arch_enablers/http_client/ExceptionHandler.java @@ -0,0 +1,12 @@ +package com.clean_arch_enablers.http_client; + +@FunctionalInterface +public interface ExceptionHandler { + + /** + * Method which will be called when handling an exception + * @param exception the exception to handle + */ + void handle(Exception exception); + +} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpExceptionHandlersByExceptionTypeFactory.java b/src/main/java/com/clean_arch_enablers/http_client/HttpExceptionHandlersByExceptionTypeFactory.java similarity index 59% rename from src/main/java/io/github/julucinho/httpclient/HttpExceptionHandlersByExceptionTypeFactory.java rename to src/main/java/com/clean_arch_enablers/http_client/HttpExceptionHandlersByExceptionTypeFactory.java index 5af4bf0..93a5cb6 100644 --- a/src/main/java/io/github/julucinho/httpclient/HttpExceptionHandlersByExceptionTypeFactory.java +++ b/src/main/java/com/clean_arch_enablers/http_client/HttpExceptionHandlersByExceptionTypeFactory.java @@ -1,6 +1,6 @@ -package io.github.julucinho.httpclient; +package com.clean_arch_enablers.http_client; -import io.github.julucinho.httpclient.commons.HandlersFactory; +import com.clean_arch_enablers.http_client.commons.HandlersFactory; import java.util.Map; diff --git a/src/main/java/com/clean_arch_enablers/http_client/HttpRequestBuilder.java b/src/main/java/com/clean_arch_enablers/http_client/HttpRequestBuilder.java new file mode 100644 index 0000000..18d3125 --- /dev/null +++ b/src/main/java/com/clean_arch_enablers/http_client/HttpRequestBuilder.java @@ -0,0 +1,12 @@ +package com.clean_arch_enablers.http_client; + +public interface HttpRequestBuilder extends HttpRequestBuilderForHandlers, HttpRequestBuilderForRetrying{ + + HttpRequestBuilder headerOf(String key, String value); + HttpRequestBuilder headersFactory(HttpRequestHeaderFactory httpRequestHeaderFactory); + HttpRequestBuilder pathVariableOf(String pathVariablePlaceholder, String pathVariableValue); + HttpRequestBuilder queryParameterOf(String queryParameterName, String queryParameterValue); + HttpRequestBuilder proxyAddress(String host, Integer port); + HttpRequestModel finishBuildingModel(); + +} diff --git a/src/main/java/com/clean_arch_enablers/http_client/HttpRequestBuilderForHandlers.java b/src/main/java/com/clean_arch_enablers/http_client/HttpRequestBuilderForHandlers.java new file mode 100644 index 0000000..41d3385 --- /dev/null +++ b/src/main/java/com/clean_arch_enablers/http_client/HttpRequestBuilderForHandlers.java @@ -0,0 +1,46 @@ +package com.clean_arch_enablers.http_client; + +/** + * Builder component for attaching handlers to the request model. + */ +public interface HttpRequestBuilderForHandlers { + + + /** + * Attaches a handler by the number which represents the HTTP status code + * @param statusCode the number of the HTTP status code: 404, 400, 503... + * @param httpResponseHandler an arrow function which implements the Functional Interface of HttpResponseHandler + * @return the builder instance for further attachments + */ + HttpRequestBuilder handlerByHttpStatusCode(Integer statusCode, HttpResponseHandler httpResponseHandler); + + /** + * Attaches a group of handlers by the numbers which represent HTTP statuses code from within the factory + * @param httpResponseHandlersByStatusCodeFactory the factory from which the handlers will be made + * @return the builder instance for further attachments + */ + HttpRequestBuilder handlersByHttpStatusCodeFactory(HttpResponseHandlersByStatusCodeFactory httpResponseHandlersByStatusCodeFactory); + + /** + * Attaches a handler for any unsuccessful HTTP response + * @param httpResponseHandler an arrow function which implements the Functional Interface of HttpResponseHandler + * @return the builder instance for further attachments + */ + HttpRequestBuilder handlerForAnyUnsuccessfulResponse(HttpResponseHandler httpResponseHandler); + + /** + * Attaches a group of handlers by types of exceptions from within the factory + * @param httpExceptionHandlersByExceptionTypeFactory the factory from which the handlers will be made + * @return the builder instance for further attachments + */ + HttpRequestBuilder handlersByExceptionTypeFactory(HttpExceptionHandlersByExceptionTypeFactory httpExceptionHandlersByExceptionTypeFactory); + + /** + * Attaches a handler by type of exception + * @param exceptionType the type of the exception which will trigger the execution of the following handler + * @param exceptionHandler an arrow function which implements the Functional Interface of ExceptionHandler + * @return the builder instance for further attachments + */ + HttpRequestBuilder handlerByExceptionType(Class exceptionType, ExceptionHandler exceptionHandler); + +} diff --git a/src/main/java/com/clean_arch_enablers/http_client/HttpRequestBuilderForRetrying.java b/src/main/java/com/clean_arch_enablers/http_client/HttpRequestBuilderForRetrying.java new file mode 100644 index 0000000..dc9a268 --- /dev/null +++ b/src/main/java/com/clean_arch_enablers/http_client/HttpRequestBuilderForRetrying.java @@ -0,0 +1,16 @@ +package com.clean_arch_enablers.http_client; + +import com.clean_arch_enablers.http_client.commons.RetriersByExceptionTypeFactory; + +public interface HttpRequestBuilderForRetrying { + + HttpRequestBuilder retrierByHttpStatusCode(Integer statusCode, RetrierModel retrierModel); + HttpRequestBuilder retriersByHttpStatusCodeFactory(RetriersByStatusCodeFactory retriersByStatusCodeFactory); + HttpRequestBuilder retriersByExceptionTypeFactory(RetriersByExceptionTypeFactory retriersByExceptionTypeFactory); + HttpRequestBuilder retrierByExceptionType(Class exceptionType, RetrierModel retrierModel); + +} + + + + diff --git a/src/main/java/com/clean_arch_enablers/http_client/HttpRequestHeaderFactory.java b/src/main/java/com/clean_arch_enablers/http_client/HttpRequestHeaderFactory.java new file mode 100644 index 0000000..f19efce --- /dev/null +++ b/src/main/java/com/clean_arch_enablers/http_client/HttpRequestHeaderFactory.java @@ -0,0 +1,16 @@ +package com.clean_arch_enablers.http_client; + +import java.util.Map; + +/** + * Factory for HTTP Request Headers + */ +public interface HttpRequestHeaderFactory { + + /** + * This method will be called under the hood when an instance of this type is passed during the building process of a HttpRequestModel instance. + * @return a map in which the keys are the HTTP Headers and the values are their respective HTTP Header Values + */ + Map makeHeaders(); + +} diff --git a/src/main/java/com/clean_arch_enablers/http_client/HttpRequestMethod.java b/src/main/java/com/clean_arch_enablers/http_client/HttpRequestMethod.java new file mode 100644 index 0000000..48ce4f7 --- /dev/null +++ b/src/main/java/com/clean_arch_enablers/http_client/HttpRequestMethod.java @@ -0,0 +1,11 @@ +package com.clean_arch_enablers.http_client; + + +import com.clean_arch_enablers.http_client.implementations.AbstractHttpRequestModel; +import com.clean_arch_enablers.http_client.implementations.exceptions.RetryNeededOnExceptionThrownException; + +public interface HttpRequestMethod { + + HttpResponse execute(AbstractHttpRequestModel httpRequestModel) throws RetryNeededOnExceptionThrownException; + +} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpRequestModel.java b/src/main/java/com/clean_arch_enablers/http_client/HttpRequestModel.java similarity index 83% rename from src/main/java/io/github/julucinho/httpclient/HttpRequestModel.java rename to src/main/java/com/clean_arch_enablers/http_client/HttpRequestModel.java index 89b9aae..0aa4fc3 100644 --- a/src/main/java/io/github/julucinho/httpclient/HttpRequestModel.java +++ b/src/main/java/com/clean_arch_enablers/http_client/HttpRequestModel.java @@ -1,4 +1,4 @@ -package io.github.julucinho.httpclient; +package com.clean_arch_enablers.http_client; import com.fasterxml.jackson.core.type.TypeReference; diff --git a/src/main/java/com/clean_arch_enablers/http_client/HttpRequestStarter.java b/src/main/java/com/clean_arch_enablers/http_client/HttpRequestStarter.java new file mode 100644 index 0000000..79e5e6b --- /dev/null +++ b/src/main/java/com/clean_arch_enablers/http_client/HttpRequestStarter.java @@ -0,0 +1,12 @@ +package com.clean_arch_enablers.http_client; + +import java.net.http.HttpRequest; + +public interface HttpRequestStarter { + + HttpRequestBuilder startGetRequestFor(String url); + HttpRequestBuilder startPostRequestFor(String url, HttpRequest.BodyPublisher body); + HttpRequestBuilder startPutRequestFor(String url, HttpRequest.BodyPublisher body); + HttpRequestBuilder startDeleteRequestFor(String url); + +} diff --git a/src/main/java/com/clean_arch_enablers/http_client/HttpResponse.java b/src/main/java/com/clean_arch_enablers/http_client/HttpResponse.java new file mode 100644 index 0000000..202c111 --- /dev/null +++ b/src/main/java/com/clean_arch_enablers/http_client/HttpResponse.java @@ -0,0 +1,48 @@ +package com.clean_arch_enablers.http_client; + +import com.fasterxml.jackson.core.type.TypeReference; + +import java.util.function.Consumer; + +public interface HttpResponse{ + + /** + * Retrieve the status code of the response + * @return a number of some HTTP Status Code: 200, 204, 400, 403... + */ + Integer getStatusCode(); + + /** + * Gets the request responsible for generating this response + * @return the HTTP request model object + */ + HttpRequestModel getHttpRequest(); + + /** + * Gets the response body as the parameterized type + * @param bodyType the desired type for the body + * @return the instance of the desired type + * @param the known type to map the body to + */ + T getBodyAs(Class bodyType); + + /** + * Gets the response body as the parameterized type + * @param typeOfExpectedResponseBody the desired type for the body + * @return the instance of the desired type + * @param the known type to map the body to + */ + T getBodyAs(TypeReference typeOfExpectedResponseBody); + + /** + * + * @return whether the response needs handling + */ + boolean needsHandling(); + + /** + * If the instance needs handling it will execute the Consumer received as parameter + * @param checkOnResponse the action to be executed in case of needing handling + */ + void ifNeedsHandling(Consumer checkOnResponse); +} diff --git a/src/main/java/com/clean_arch_enablers/http_client/HttpResponseHandler.java b/src/main/java/com/clean_arch_enablers/http_client/HttpResponseHandler.java new file mode 100644 index 0000000..4c53038 --- /dev/null +++ b/src/main/java/com/clean_arch_enablers/http_client/HttpResponseHandler.java @@ -0,0 +1,12 @@ +package com.clean_arch_enablers.http_client; + +@FunctionalInterface +public interface HttpResponseHandler { + + /** + * Method called when handling an HTTP Response + * @param httpResponse the HTTP response to be handled + */ + void handle(HttpResponse httpResponse); + +} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpResponseHandlersByStatusCodeFactory.java b/src/main/java/com/clean_arch_enablers/http_client/HttpResponseHandlersByStatusCodeFactory.java similarity index 56% rename from src/main/java/io/github/julucinho/httpclient/HttpResponseHandlersByStatusCodeFactory.java rename to src/main/java/com/clean_arch_enablers/http_client/HttpResponseHandlersByStatusCodeFactory.java index b9deb1c..fb292a6 100644 --- a/src/main/java/io/github/julucinho/httpclient/HttpResponseHandlersByStatusCodeFactory.java +++ b/src/main/java/com/clean_arch_enablers/http_client/HttpResponseHandlersByStatusCodeFactory.java @@ -1,6 +1,6 @@ -package io.github.julucinho.httpclient; +package com.clean_arch_enablers.http_client; -import io.github.julucinho.httpclient.commons.HandlersFactory; +import com.clean_arch_enablers.http_client.commons.HandlersFactory; import java.util.Map; diff --git a/src/main/java/io/github/julucinho/httpclient/RetrierModel.java b/src/main/java/com/clean_arch_enablers/http_client/RetrierModel.java similarity index 92% rename from src/main/java/io/github/julucinho/httpclient/RetrierModel.java rename to src/main/java/com/clean_arch_enablers/http_client/RetrierModel.java index 1004a5b..811f2c3 100644 --- a/src/main/java/io/github/julucinho/httpclient/RetrierModel.java +++ b/src/main/java/com/clean_arch_enablers/http_client/RetrierModel.java @@ -1,4 +1,4 @@ -package io.github.julucinho.httpclient; +package com.clean_arch_enablers.http_client; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/io/github/julucinho/httpclient/RetriersByStatusCodeFactory.java b/src/main/java/com/clean_arch_enablers/http_client/RetriersByStatusCodeFactory.java similarity index 52% rename from src/main/java/io/github/julucinho/httpclient/RetriersByStatusCodeFactory.java rename to src/main/java/com/clean_arch_enablers/http_client/RetriersByStatusCodeFactory.java index 22ab7e9..d8af2b5 100644 --- a/src/main/java/io/github/julucinho/httpclient/RetriersByStatusCodeFactory.java +++ b/src/main/java/com/clean_arch_enablers/http_client/RetriersByStatusCodeFactory.java @@ -1,6 +1,6 @@ -package io.github.julucinho.httpclient; +package com.clean_arch_enablers.http_client; -import io.github.julucinho.httpclient.commons.RetriersFactory; +import com.clean_arch_enablers.http_client.commons.RetriersFactory; import java.util.Map; diff --git a/src/main/java/io/github/julucinho/httpclient/RetryCounter.java b/src/main/java/com/clean_arch_enablers/http_client/RetryCounter.java similarity index 70% rename from src/main/java/io/github/julucinho/httpclient/RetryCounter.java rename to src/main/java/com/clean_arch_enablers/http_client/RetryCounter.java index cccb70d..5b94fc0 100644 --- a/src/main/java/io/github/julucinho/httpclient/RetryCounter.java +++ b/src/main/java/com/clean_arch_enablers/http_client/RetryCounter.java @@ -1,4 +1,4 @@ -package io.github.julucinho.httpclient; +package com.clean_arch_enablers.http_client; public interface RetryCounter { boolean thereIsRetryAvailable(); diff --git a/src/main/java/com/clean_arch_enablers/http_client/commons/HandlersFactory.java b/src/main/java/com/clean_arch_enablers/http_client/commons/HandlersFactory.java new file mode 100644 index 0000000..5da9a99 --- /dev/null +++ b/src/main/java/com/clean_arch_enablers/http_client/commons/HandlersFactory.java @@ -0,0 +1,11 @@ +package com.clean_arch_enablers.http_client.commons; + +public interface HandlersFactory { + + /** + * Factory method for creating handlers + * @return the handlers + */ + T makeHandlers(); + +} diff --git a/src/main/java/io/github/julucinho/httpclient/commons/RetriersByExceptionTypeFactory.java b/src/main/java/com/clean_arch_enablers/http_client/commons/RetriersByExceptionTypeFactory.java similarity index 57% rename from src/main/java/io/github/julucinho/httpclient/commons/RetriersByExceptionTypeFactory.java rename to src/main/java/com/clean_arch_enablers/http_client/commons/RetriersByExceptionTypeFactory.java index 0e85867..1f4c7ea 100644 --- a/src/main/java/io/github/julucinho/httpclient/commons/RetriersByExceptionTypeFactory.java +++ b/src/main/java/com/clean_arch_enablers/http_client/commons/RetriersByExceptionTypeFactory.java @@ -1,7 +1,7 @@ -package io.github.julucinho.httpclient.commons; +package com.clean_arch_enablers.http_client.commons; -import io.github.julucinho.httpclient.RetrierModel; +import com.clean_arch_enablers.http_client.RetrierModel; import java.util.Map; diff --git a/src/main/java/io/github/julucinho/httpclient/commons/RetriersFactory.java b/src/main/java/com/clean_arch_enablers/http_client/commons/RetriersFactory.java similarity index 54% rename from src/main/java/io/github/julucinho/httpclient/commons/RetriersFactory.java rename to src/main/java/com/clean_arch_enablers/http_client/commons/RetriersFactory.java index 93a69de..b9ae425 100644 --- a/src/main/java/io/github/julucinho/httpclient/commons/RetriersFactory.java +++ b/src/main/java/com/clean_arch_enablers/http_client/commons/RetriersFactory.java @@ -1,4 +1,4 @@ -package io.github.julucinho.httpclient.commons; +package com.clean_arch_enablers.http_client.commons; public interface RetriersFactory { T makeRetriers(); diff --git a/src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpRequestBuilder.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/AbstractHttpRequestBuilder.java similarity index 62% rename from src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpRequestBuilder.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/AbstractHttpRequestBuilder.java index 1d17a5e..59c049b 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpRequestBuilder.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/AbstractHttpRequestBuilder.java @@ -1,6 +1,6 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; -import io.github.julucinho.httpclient.HttpRequestBuilder; +import com.clean_arch_enablers.http_client.HttpRequestBuilder; import lombok.RequiredArgsConstructor; @RequiredArgsConstructor diff --git a/src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpRequestModel.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/AbstractHttpRequestModel.java similarity index 91% rename from src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpRequestModel.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/AbstractHttpRequestModel.java index b4bf415..ae4ffed 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpRequestModel.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/AbstractHttpRequestModel.java @@ -1,6 +1,6 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; -import io.github.julucinho.httpclient.*; +import com.clean_arch_enablers.http_client.*; import java.net.http.HttpRequest.BodyPublisher; import java.util.ArrayList; diff --git a/src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpResponse.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/AbstractHttpResponse.java similarity index 60% rename from src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpResponse.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/AbstractHttpResponse.java index 9a5902b..2c86791 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/AbstractHttpResponse.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/AbstractHttpResponse.java @@ -1,7 +1,7 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; -import io.github.julucinho.httpclient.HttpRequestModel; -import io.github.julucinho.httpclient.HttpResponse; +import com.clean_arch_enablers.http_client.HttpRequestModel; +import com.clean_arch_enablers.http_client.HttpResponse; import lombok.RequiredArgsConstructor; @RequiredArgsConstructor diff --git a/src/main/java/io/github/julucinho/httpclient/impl/ExceptionThrownByHttpRequestChecker.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/ExceptionThrownByHttpRequestChecker.java similarity index 67% rename from src/main/java/io/github/julucinho/httpclient/impl/ExceptionThrownByHttpRequestChecker.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/ExceptionThrownByHttpRequestChecker.java index 4a74138..34e0349 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/ExceptionThrownByHttpRequestChecker.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/ExceptionThrownByHttpRequestChecker.java @@ -1,6 +1,6 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; -import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnExceptionThrownException; +import com.clean_arch_enablers.http_client.implementations.exceptions.RetryNeededOnExceptionThrownException; import lombok.AccessLevel; import lombok.AllArgsConstructor; @@ -16,12 +16,12 @@ public static ExceptionThrownByHttpRequestChecker of(AbstractHttpRequestModel ht } public void checkOn(Exception e) { - var retryCounterByExceptionType = ofNullable(httpRequestModel.retryCountersByExceptionType.get(e.getClass())); + var retryCounterByExceptionType = ofNullable(this.httpRequestModel.retryCountersByExceptionType.get(e.getClass())); if (retryCounterByExceptionType.isPresent() && retryCounterByExceptionType.get().thereIsRetryAvailable()) { retryCounterByExceptionType.get().decreaseRetriesAvailable(); throw new RetryNeededOnExceptionThrownException(e.getClass()); } - var handlerByThisException = ofNullable(httpRequestModel.exceptionHandlersByExceptionType.get(e.getClass())); + var handlerByThisException = ofNullable(this.httpRequestModel.exceptionHandlersByExceptionType.get(e.getClass())); handlerByThisException.ifPresent(handler -> handler.handle(e)); } } diff --git a/src/main/java/io/github/julucinho/httpclient/impl/FinalHttpRequestExecutor.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/FinalHttpRequestExecutor.java similarity index 84% rename from src/main/java/io/github/julucinho/httpclient/impl/FinalHttpRequestExecutor.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/FinalHttpRequestExecutor.java index 5503213..c74243f 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/FinalHttpRequestExecutor.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/FinalHttpRequestExecutor.java @@ -1,7 +1,7 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; -import io.github.julucinho.httpclient.impl.exceptions.IORuntimeException; -import io.github.julucinho.httpclient.impl.exceptions.InterruptedRuntimeException; +import com.clean_arch_enablers.http_client.implementations.exceptions.IORuntimeException; +import com.clean_arch_enablers.http_client.implementations.exceptions.InterruptedRuntimeException; import lombok.AccessLevel; import lombok.NoArgsConstructor; diff --git a/src/main/java/io/github/julucinho/httpclient/impl/FinalHttpRequestFactory.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/FinalHttpRequestFactory.java similarity index 89% rename from src/main/java/io/github/julucinho/httpclient/impl/FinalHttpRequestFactory.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/FinalHttpRequestFactory.java index 86c5d27..17c8983 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/FinalHttpRequestFactory.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/FinalHttpRequestFactory.java @@ -1,4 +1,4 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -38,16 +38,16 @@ private static HttpRequest finallyBuildIt(HttpRequest.Builder finalHttpRequestBu } private static void setUriInto(HttpRequest.Builder finalHttpRequestBuilder, AbstractHttpRequestModel httpRequestModel) { + setPathVariablesInto(httpRequestModel); var uriBuilder = new StringBuilder(httpRequestModel.uri); - setPathVariablesInto(uriBuilder, httpRequestModel.pathVariables); setQueryParametersInto(uriBuilder, httpRequestModel.queryParameters); var finalUriString = uriBuilder.toString(); var finalUri = URI.create(finalUriString); finalHttpRequestBuilder.uri(finalUri); } - private static void setPathVariablesInto(StringBuilder uriBuilder, List pathVariables) { - pathVariables.stream().map(HttpRequestPathVariable::buildPathVariable).forEach(uriBuilder::append); + private static void setPathVariablesInto(AbstractHttpRequestModel requestModel) { + requestModel.pathVariables.forEach(pathVariable -> pathVariable.buildPathVariableInto(requestModel)); } private static void setQueryParametersInto(StringBuilder uriBuilder, List queryParameters) { diff --git a/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestBuilderImplementation.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestBuilderImplementation.java new file mode 100644 index 0000000..f44c4d7 --- /dev/null +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestBuilderImplementation.java @@ -0,0 +1,105 @@ +package com.clean_arch_enablers.http_client.implementations; + + +import com.clean_arch_enablers.http_client.*; +import com.clean_arch_enablers.http_client.commons.RetriersByExceptionTypeFactory; + +public class HttpRequestBuilderImplementation extends AbstractHttpRequestBuilder { + + public static HttpRequestBuilder of(AbstractHttpRequestModel httpRequest){ + return new HttpRequestBuilderImplementation(httpRequest); + } + + private HttpRequestBuilderImplementation(AbstractHttpRequestModel httpRequest) { + super(httpRequest); + } + + @Override + public HttpRequestBuilder headerOf(String key, String value) { + this.httpRequest.headers.put(key, value); + return this; + } + + @Override + public HttpRequestBuilder headersFactory(HttpRequestHeaderFactory httpRequestHeaderFactory) { + httpRequestHeaderFactory.makeHeaders().forEach(this.httpRequest.headers::put); + return this; + } + + @Override + public HttpRequestBuilder pathVariableOf(String pathVariablePlaceholder, String pathVariableValue) { + this.httpRequest.pathVariables.add(new HttpRequestPathVariable(pathVariablePlaceholder, pathVariableValue)); + return this; + } + + @Override + public HttpRequestBuilder queryParameterOf(String queryParameterName, String queryParameterValue) { + this.httpRequest.queryParameters.add(new HttpRequestQueryParameter(queryParameterName, queryParameterValue)); + return this; + } + + @Override + public HttpRequestBuilder proxyAddress(String host, Integer port) { + this.httpRequest.proxyAddress = ProxyAddressModel.builder().host(host).port(port).build(); + return this; + } + + @Override + public HttpRequestModel finishBuildingModel() { + return this.httpRequest; + } + + @Override + public HttpRequestBuilder handlerByHttpStatusCode(Integer statusCode, HttpResponseHandler httpResponseHandler) { + this.httpRequest.responseHandlersByStatusCode.put(statusCode, httpResponseHandler); + return this; + } + + @Override + public HttpRequestBuilder handlersByHttpStatusCodeFactory(HttpResponseHandlersByStatusCodeFactory httpResponseHandlersByStatusCodeFactory) { + httpResponseHandlersByStatusCodeFactory.makeHandlers().forEach(this.httpRequest.responseHandlersByStatusCode::put); + return this; + } + + @Override + public HttpRequestBuilder handlerForAnyUnsuccessfulResponse(HttpResponseHandler httpResponseHandler) { + this.httpRequest.genericResponseHandler = httpResponseHandler; + return this; + } + + @Override + public HttpRequestBuilder handlerByExceptionType(Class exceptionType, ExceptionHandler exceptionHandler) { + this.httpRequest.exceptionHandlersByExceptionType.put(exceptionType, exceptionHandler); + return this; + } + + @Override + public HttpRequestBuilder handlersByExceptionTypeFactory(HttpExceptionHandlersByExceptionTypeFactory httpExceptionHandlersByExceptionTypeFactory) { + httpExceptionHandlersByExceptionTypeFactory.makeHandlers().forEach(this.httpRequest.exceptionHandlersByExceptionType::put); + return this; + } + + @Override + public HttpRequestBuilder retrierByHttpStatusCode(Integer statusCode, RetrierModel retrierModel) { + this.httpRequest.retryCountersByStatusCode.put(statusCode, RetryCounterImplementation.of(retrierModel)); + return this; + } + + @Override + public HttpRequestBuilder retriersByHttpStatusCodeFactory(RetriersByStatusCodeFactory retriersByStatusCodeFactory) { + retriersByStatusCodeFactory.makeRetriers().forEach((statusCode, retrierModel) -> this.httpRequest.retryCountersByStatusCode.put(statusCode, RetryCounterImplementation.of(retrierModel))); + return this; + } + + @Override + public HttpRequestBuilder retrierByExceptionType(Class exceptionType, RetrierModel retrierModel) { + this.httpRequest.retryCountersByExceptionType.put(exceptionType, RetryCounterImplementation.of(retrierModel)); + return this; + } + + @Override + public HttpRequestBuilder retriersByExceptionTypeFactory(RetriersByExceptionTypeFactory retriersByExceptionTypeFactory) { + retriersByExceptionTypeFactory.makeRetriers().forEach((exceptionType, retrierModel) -> this.httpRequest.retryCountersByExceptionType.put(exceptionType, RetryCounterImplementation.of(retrierModel))); + return this; + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestDeleteMethod.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestDeleteMethod.java similarity index 61% rename from src/main/java/io/github/julucinho/httpclient/impl/HttpRequestDeleteMethod.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestDeleteMethod.java index e1677ca..82cab0a 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestDeleteMethod.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestDeleteMethod.java @@ -1,9 +1,9 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; -import io.github.julucinho.httpclient.HttpRequestMethod; -import io.github.julucinho.httpclient.HttpResponse; -import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnExceptionThrownException; +import com.clean_arch_enablers.http_client.HttpRequestMethod; +import com.clean_arch_enablers.http_client.HttpResponse; +import com.clean_arch_enablers.http_client.implementations.exceptions.RetryNeededOnExceptionThrownException; public class HttpRequestDeleteMethod implements HttpRequestMethod { @@ -12,7 +12,7 @@ public HttpResponse execute(AbstractHttpRequestModel httpRequestModel) throws Re var finalRequest = FinalHttpRequestFactory.makeFinalRequestForDeleteMethodFrom(httpRequestModel); try { var unwrappedResponse = FinalHttpRequestExecutor.of(httpRequestModel).execute(finalRequest); - return new HttpResponseImpl(httpRequestModel, unwrappedResponse); + return new HttpResponseImplementation(httpRequestModel, unwrappedResponse); } catch (Exception e) { ExceptionThrownByHttpRequestChecker.of(httpRequestModel).checkOn(e); diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestExecutionManager.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestExecutionManager.java similarity index 71% rename from src/main/java/io/github/julucinho/httpclient/impl/HttpRequestExecutionManager.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestExecutionManager.java index 435da86..e36ea55 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestExecutionManager.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestExecutionManager.java @@ -1,8 +1,8 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; -import io.github.julucinho.httpclient.HttpResponse; -import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnExceptionThrownException; -import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnHttpStatusCodeException; +import com.clean_arch_enablers.http_client.HttpResponse; +import com.clean_arch_enablers.http_client.implementations.exceptions.RetryNeededOnExceptionThrownException; +import com.clean_arch_enablers.http_client.implementations.exceptions.RetryNeededOnHttpStatusCodeException; import lombok.AccessLevel; import lombok.NoArgsConstructor; diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestGetMethod.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestGetMethod.java similarity index 61% rename from src/main/java/io/github/julucinho/httpclient/impl/HttpRequestGetMethod.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestGetMethod.java index 617be89..ffcc896 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestGetMethod.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestGetMethod.java @@ -1,9 +1,9 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; -import io.github.julucinho.httpclient.HttpRequestMethod; -import io.github.julucinho.httpclient.HttpResponse; -import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnExceptionThrownException; +import com.clean_arch_enablers.http_client.HttpRequestMethod; +import com.clean_arch_enablers.http_client.HttpResponse; +import com.clean_arch_enablers.http_client.implementations.exceptions.RetryNeededOnExceptionThrownException; public class HttpRequestGetMethod implements HttpRequestMethod { @@ -12,7 +12,7 @@ public HttpResponse execute(AbstractHttpRequestModel httpRequestModel) throws Re var finalRequest = FinalHttpRequestFactory.makeFinalRequestForGetMethodFrom(httpRequestModel); try { var unwrappedResponse = FinalHttpRequestExecutor.of(httpRequestModel).execute(finalRequest); - return new HttpResponseImpl(httpRequestModel, unwrappedResponse); + return new HttpResponseImplementation(httpRequestModel, unwrappedResponse); } catch (Exception exception) { ExceptionThrownByHttpRequestChecker.of(httpRequestModel).checkOn(exception); diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestModelImpl.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestModelImplementation.java similarity index 75% rename from src/main/java/io/github/julucinho/httpclient/impl/HttpRequestModelImpl.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestModelImplementation.java index caabef0..95e0810 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestModelImpl.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestModelImplementation.java @@ -1,24 +1,24 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; +import com.clean_arch_enablers.http_client.HttpRequestMethod; import com.fasterxml.jackson.core.type.TypeReference; -import io.github.julucinho.httpclient.HttpRequestMethod; import lombok.AccessLevel; import lombok.NoArgsConstructor; import java.net.http.HttpRequest.BodyPublisher; @NoArgsConstructor(access = AccessLevel.PRIVATE) -public class HttpRequestModelImpl extends AbstractHttpRequestModel { +public class HttpRequestModelImplementation extends AbstractHttpRequestModel { public static AbstractHttpRequestModel of(String uri, HttpRequestMethod method){ - var httpRequest = new HttpRequestModelImpl(); + var httpRequest = new HttpRequestModelImplementation(); httpRequest.uri = uri; httpRequest.method = method; return httpRequest; } public static AbstractHttpRequestModel of(String uri, HttpRequestMethod method, BodyPublisher body){ - var httpRequest = new HttpRequestModelImpl(); + var httpRequest = new HttpRequestModelImplementation(); httpRequest.uri = uri; httpRequest.method = method; httpRequest.body = body; diff --git a/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestPathVariable.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestPathVariable.java new file mode 100644 index 0000000..ac00491 --- /dev/null +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestPathVariable.java @@ -0,0 +1,24 @@ +package com.clean_arch_enablers.http_client.implementations; + +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public class HttpRequestPathVariable { + + private final String pathVariablePlaceholder; + private final String pathVariableValue; + + void buildPathVariableInto(AbstractHttpRequestModel requestModel){ + var placeholder = this.pathVariablePlaceholder.replace("{", "").replace("}", ""); + if (requestModel.uri.contains(placeholder)) + requestModel.uri = requestModel.uri.replace("{" + placeholder + "}", this.pathVariableValue); + else + throw new NoPathVariablePlaceholderFound(placeholder, requestModel.uri); + } + + public static class NoPathVariablePlaceholderFound extends RuntimeException { + public NoPathVariablePlaceholderFound(String placeholder, String uri) { + super("No placeholder '" + placeholder + "' was found in the URI '" + uri + "'. Make sure such placeholder is present."); + } + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPostMethod.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestPostMethod.java similarity index 60% rename from src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPostMethod.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestPostMethod.java index 83e2849..5e1ae88 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPostMethod.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestPostMethod.java @@ -1,9 +1,9 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; -import io.github.julucinho.httpclient.HttpRequestMethod; -import io.github.julucinho.httpclient.HttpResponse; -import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnExceptionThrownException; +import com.clean_arch_enablers.http_client.HttpRequestMethod; +import com.clean_arch_enablers.http_client.HttpResponse; +import com.clean_arch_enablers.http_client.implementations.exceptions.RetryNeededOnExceptionThrownException; public class HttpRequestPostMethod implements HttpRequestMethod { @@ -12,7 +12,7 @@ public HttpResponse execute(AbstractHttpRequestModel httpRequestModel) throws Re var finalRequest = FinalHttpRequestFactory.makeFinalRequestForPostMethodFrom(httpRequestModel); try { var unwrappedResponse = FinalHttpRequestExecutor.of(httpRequestModel).execute(finalRequest); - return new HttpResponseImpl(httpRequestModel, unwrappedResponse); + return new HttpResponseImplementation(httpRequestModel, unwrappedResponse); } catch (Exception e) { ExceptionThrownByHttpRequestChecker.of(httpRequestModel).checkOn(e); diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPutMethod.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestPutMethod.java similarity index 60% rename from src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPutMethod.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestPutMethod.java index 37abd80..e9abedb 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPutMethod.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestPutMethod.java @@ -1,9 +1,9 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; -import io.github.julucinho.httpclient.HttpRequestMethod; -import io.github.julucinho.httpclient.HttpResponse; -import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnExceptionThrownException; +import com.clean_arch_enablers.http_client.HttpRequestMethod; +import com.clean_arch_enablers.http_client.HttpResponse; +import com.clean_arch_enablers.http_client.implementations.exceptions.RetryNeededOnExceptionThrownException; public class HttpRequestPutMethod implements HttpRequestMethod { @@ -12,7 +12,7 @@ public HttpResponse execute(AbstractHttpRequestModel httpRequestModel) throws Re var finalRequest = FinalHttpRequestFactory.makeFinalRequestForPutMethodFrom(httpRequestModel); try { var unwrappedResponse = FinalHttpRequestExecutor.of(httpRequestModel).execute(finalRequest); - return new HttpResponseImpl(httpRequestModel, unwrappedResponse); + return new HttpResponseImplementation(httpRequestModel, unwrappedResponse); } catch (Exception e) { ExceptionThrownByHttpRequestChecker.of(httpRequestModel).checkOn(e); diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestQueryParameter.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestQueryParameter.java similarity index 73% rename from src/main/java/io/github/julucinho/httpclient/impl/HttpRequestQueryParameter.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestQueryParameter.java index a5f342c..3163bdf 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestQueryParameter.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestQueryParameter.java @@ -1,9 +1,9 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; import lombok.AllArgsConstructor; @AllArgsConstructor -class HttpRequestQueryParameter { +public class HttpRequestQueryParameter { private final String queryParameterName; private final String queryParameterValue; diff --git a/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestStarterImplementation.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestStarterImplementation.java new file mode 100644 index 0000000..62ced0e --- /dev/null +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpRequestStarterImplementation.java @@ -0,0 +1,30 @@ +package com.clean_arch_enablers.http_client.implementations; + + +import com.clean_arch_enablers.http_client.HttpRequestBuilder; +import com.clean_arch_enablers.http_client.HttpRequestStarter; + +import java.net.http.HttpRequest; + +public class HttpRequestStarterImplementation implements HttpRequestStarter { + + @Override + public HttpRequestBuilder startGetRequestFor(String url) { + return HttpRequestBuilderImplementation.of(HttpRequestModelImplementation.of(url, new HttpRequestGetMethod())); + } + + @Override + public HttpRequestBuilder startPostRequestFor(String url, HttpRequest.BodyPublisher body) { + return HttpRequestBuilderImplementation.of(HttpRequestModelImplementation.of(url, new HttpRequestPostMethod(), body)); + } + + @Override + public HttpRequestBuilder startPutRequestFor(String url, HttpRequest.BodyPublisher body) { + return HttpRequestBuilderImplementation.of(HttpRequestModelImplementation.of(url, new HttpRequestPutMethod(), body)); + } + + @Override + public HttpRequestBuilder startDeleteRequestFor(String url) { + return HttpRequestBuilderImplementation.of(HttpRequestModelImplementation.of(url, new HttpRequestDeleteMethod())); + } +} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpResponseImpl.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpResponseImplementation.java similarity index 75% rename from src/main/java/io/github/julucinho/httpclient/impl/HttpResponseImpl.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/HttpResponseImplementation.java index 2351c80..b32a660 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/HttpResponseImpl.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpResponseImplementation.java @@ -1,16 +1,16 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; +import com.clean_arch_enablers.http_client.HttpRequestModel; +import com.clean_arch_enablers.http_client.HttpResponse; +import com.clean_arch_enablers.http_client.implementations.exceptions.JsonProcessingRuntimeException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; -import io.github.julucinho.httpclient.HttpRequestModel; -import io.github.julucinho.httpclient.HttpResponse; -import io.github.julucinho.httpclient.impl.exceptions.JsonProcessingRuntimeException; import java.util.function.Consumer; -public class HttpResponseImpl extends AbstractHttpResponse{ +public class HttpResponseImplementation extends AbstractHttpResponse{ - public HttpResponseImpl(HttpRequestModel httpRequestModel, java.net.http.HttpResponse unwrappedHttpResponse) { + public HttpResponseImplementation(HttpRequestModel httpRequestModel, java.net.http.HttpResponse unwrappedHttpResponse) { super(httpRequestModel, unwrappedHttpResponse); } diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpResponseStatusCodeChecker.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpResponseStatusCodeChecker.java similarity index 80% rename from src/main/java/io/github/julucinho/httpclient/impl/HttpResponseStatusCodeChecker.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/HttpResponseStatusCodeChecker.java index d09a2c2..3a7803d 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/HttpResponseStatusCodeChecker.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/HttpResponseStatusCodeChecker.java @@ -1,9 +1,9 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; -import io.github.julucinho.httpclient.HttpResponse; -import io.github.julucinho.httpclient.HttpResponseHandler; -import io.github.julucinho.httpclient.impl.exceptions.NoResponseHandlersAvailableForExecutionException; -import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnHttpStatusCodeException; +import com.clean_arch_enablers.http_client.HttpResponse; +import com.clean_arch_enablers.http_client.HttpResponseHandler; +import com.clean_arch_enablers.http_client.implementations.exceptions.NoResponseHandlersAvailableForExecutionException; +import com.clean_arch_enablers.http_client.implementations.exceptions.RetryNeededOnHttpStatusCodeException; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; diff --git a/src/main/java/io/github/julucinho/httpclient/impl/ObjectMapperFactory.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/ObjectMapperFactory.java similarity index 89% rename from src/main/java/io/github/julucinho/httpclient/impl/ObjectMapperFactory.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/ObjectMapperFactory.java index dc31937..ed16302 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/ObjectMapperFactory.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/ObjectMapperFactory.java @@ -1,4 +1,4 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/src/main/java/io/github/julucinho/httpclient/impl/ProxyAddressModel.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/ProxyAddressModel.java similarity index 72% rename from src/main/java/io/github/julucinho/httpclient/impl/ProxyAddressModel.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/ProxyAddressModel.java index 03223d7..1d5c5c7 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/ProxyAddressModel.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/ProxyAddressModel.java @@ -1,4 +1,4 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; import lombok.Builder; import lombok.Getter; diff --git a/src/main/java/io/github/julucinho/httpclient/impl/RetryCounterImpl.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/RetryCounterImplementation.java similarity index 62% rename from src/main/java/io/github/julucinho/httpclient/impl/RetryCounterImpl.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/RetryCounterImplementation.java index 46eceef..17c3de6 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/RetryCounterImpl.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/RetryCounterImplementation.java @@ -1,17 +1,17 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; -import io.github.julucinho.httpclient.RetrierModel; -import io.github.julucinho.httpclient.RetryCounter; +import com.clean_arch_enablers.http_client.RetrierModel; +import com.clean_arch_enablers.http_client.RetryCounter; import lombok.AccessLevel; import lombok.AllArgsConstructor; @AllArgsConstructor(access = AccessLevel.PRIVATE) -public class RetryCounterImpl implements RetryCounter { +public class RetryCounterImplementation implements RetryCounter { private final RetrierModel retrierModel; public static RetryCounter of(RetrierModel retrierModel){ - return new RetryCounterImpl(retrierModel); + return new RetryCounterImplementation(retrierModel); } @Override diff --git a/src/main/java/io/github/julucinho/httpclient/impl/exceptions/IORuntimeException.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/exceptions/IORuntimeException.java similarity index 72% rename from src/main/java/io/github/julucinho/httpclient/impl/exceptions/IORuntimeException.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/exceptions/IORuntimeException.java index f3c98dc..2c549f3 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/exceptions/IORuntimeException.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/exceptions/IORuntimeException.java @@ -1,4 +1,4 @@ -package io.github.julucinho.httpclient.impl.exceptions; +package com.clean_arch_enablers.http_client.implementations.exceptions; import java.io.IOException; diff --git a/src/main/java/io/github/julucinho/httpclient/impl/exceptions/InterruptedRuntimeException.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/exceptions/InterruptedRuntimeException.java similarity index 70% rename from src/main/java/io/github/julucinho/httpclient/impl/exceptions/InterruptedRuntimeException.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/exceptions/InterruptedRuntimeException.java index f041fe1..8dff32b 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/exceptions/InterruptedRuntimeException.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/exceptions/InterruptedRuntimeException.java @@ -1,4 +1,4 @@ -package io.github.julucinho.httpclient.impl.exceptions; +package com.clean_arch_enablers.http_client.implementations.exceptions; public class InterruptedRuntimeException extends RuntimeException { public InterruptedRuntimeException(InterruptedException e) { diff --git a/src/main/java/io/github/julucinho/httpclient/impl/exceptions/JsonProcessingRuntimeException.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/exceptions/JsonProcessingRuntimeException.java similarity index 69% rename from src/main/java/io/github/julucinho/httpclient/impl/exceptions/JsonProcessingRuntimeException.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/exceptions/JsonProcessingRuntimeException.java index f1cdb9f..456dac5 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/exceptions/JsonProcessingRuntimeException.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/exceptions/JsonProcessingRuntimeException.java @@ -1,4 +1,4 @@ -package io.github.julucinho.httpclient.impl.exceptions; +package com.clean_arch_enablers.http_client.implementations.exceptions; public class JsonProcessingRuntimeException extends RuntimeException { public JsonProcessingRuntimeException(String message) { diff --git a/src/main/java/io/github/julucinho/httpclient/impl/exceptions/NoResponseHandlersAvailableForExecutionException.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/exceptions/NoResponseHandlersAvailableForExecutionException.java similarity index 83% rename from src/main/java/io/github/julucinho/httpclient/impl/exceptions/NoResponseHandlersAvailableForExecutionException.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/exceptions/NoResponseHandlersAvailableForExecutionException.java index 79ab178..448bae5 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/exceptions/NoResponseHandlersAvailableForExecutionException.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/exceptions/NoResponseHandlersAvailableForExecutionException.java @@ -1,7 +1,7 @@ -package io.github.julucinho.httpclient.impl.exceptions; +package com.clean_arch_enablers.http_client.implementations.exceptions; -import io.github.julucinho.httpclient.HttpResponse; +import com.clean_arch_enablers.http_client.HttpResponse; public class NoResponseHandlersAvailableForExecutionException extends RuntimeException { diff --git a/src/main/java/io/github/julucinho/httpclient/impl/exceptions/RetryNeededOnExceptionThrownException.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/exceptions/RetryNeededOnExceptionThrownException.java similarity index 84% rename from src/main/java/io/github/julucinho/httpclient/impl/exceptions/RetryNeededOnExceptionThrownException.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/exceptions/RetryNeededOnExceptionThrownException.java index 0541bd8..9548254 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/exceptions/RetryNeededOnExceptionThrownException.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/exceptions/RetryNeededOnExceptionThrownException.java @@ -1,4 +1,4 @@ -package io.github.julucinho.httpclient.impl.exceptions; +package com.clean_arch_enablers.http_client.implementations.exceptions; import lombok.Getter; diff --git a/src/main/java/io/github/julucinho/httpclient/impl/exceptions/RetryNeededOnHttpStatusCodeException.java b/src/main/java/com/clean_arch_enablers/http_client/implementations/exceptions/RetryNeededOnHttpStatusCodeException.java similarity index 74% rename from src/main/java/io/github/julucinho/httpclient/impl/exceptions/RetryNeededOnHttpStatusCodeException.java rename to src/main/java/com/clean_arch_enablers/http_client/implementations/exceptions/RetryNeededOnHttpStatusCodeException.java index 48d7dc6..cc6de72 100644 --- a/src/main/java/io/github/julucinho/httpclient/impl/exceptions/RetryNeededOnHttpStatusCodeException.java +++ b/src/main/java/com/clean_arch_enablers/http_client/implementations/exceptions/RetryNeededOnHttpStatusCodeException.java @@ -1,6 +1,6 @@ -package io.github.julucinho.httpclient.impl.exceptions; +package com.clean_arch_enablers.http_client.implementations.exceptions; -import io.github.julucinho.httpclient.HttpResponse; +import com.clean_arch_enablers.http_client.HttpResponse; import lombok.Getter; @Getter diff --git a/src/main/java/io/github/julucinho/httpclient/utils/ThreadIdRetrievementUtil.java b/src/main/java/com/clean_arch_enablers/http_client/utils/ThreadIdRetrievementUtil.java similarity index 88% rename from src/main/java/io/github/julucinho/httpclient/utils/ThreadIdRetrievementUtil.java rename to src/main/java/com/clean_arch_enablers/http_client/utils/ThreadIdRetrievementUtil.java index 8342686..0508c52 100644 --- a/src/main/java/io/github/julucinho/httpclient/utils/ThreadIdRetrievementUtil.java +++ b/src/main/java/com/clean_arch_enablers/http_client/utils/ThreadIdRetrievementUtil.java @@ -1,4 +1,4 @@ -package io.github.julucinho.httpclient.utils; +package com.clean_arch_enablers.http_client.utils; import lombok.AccessLevel; import lombok.NoArgsConstructor; diff --git a/src/main/java/io/github/julucinho/httpclient/ExceptionHandler.java b/src/main/java/io/github/julucinho/httpclient/ExceptionHandler.java deleted file mode 100644 index 27eac10..0000000 --- a/src/main/java/io/github/julucinho/httpclient/ExceptionHandler.java +++ /dev/null @@ -1,8 +0,0 @@ -package io.github.julucinho.httpclient; - -@FunctionalInterface -public interface ExceptionHandler { - - void handle(Exception exception); - -} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpRequestBuilder.java b/src/main/java/io/github/julucinho/httpclient/HttpRequestBuilder.java deleted file mode 100644 index a86d067..0000000 --- a/src/main/java/io/github/julucinho/httpclient/HttpRequestBuilder.java +++ /dev/null @@ -1,12 +0,0 @@ -package io.github.julucinho.httpclient; - -public interface HttpRequestBuilder extends HttpRequestBuilderForHandlers, HttpRequestBuilderForRetrying{ - - HttpRequestBuilder andAddHeaderOf(String key, String value); - HttpRequestBuilder andAddHeadersFactory(HttpRequestHeaderFactory httpRequestHeaderFactory); - HttpRequestBuilder andAddPathVariableOf(String pathVariableName, String pathVariableValue); - HttpRequestBuilder andAddQueryParameterOf(String queryParameterName, String queryParameterValue); - HttpRequestBuilder andAddProxyAddress(String host, Integer port); - HttpRequestModel andFinishBuildingModel(); - -} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpRequestBuilderForHandlers.java b/src/main/java/io/github/julucinho/httpclient/HttpRequestBuilderForHandlers.java deleted file mode 100644 index c8af121..0000000 --- a/src/main/java/io/github/julucinho/httpclient/HttpRequestBuilderForHandlers.java +++ /dev/null @@ -1,12 +0,0 @@ -package io.github.julucinho.httpclient; - - -public interface HttpRequestBuilderForHandlers { - - HttpRequestBuilder andAddResponseHandlerByHttpStatusCode(Integer statusCode, HttpResponseHandler httpResponseHandler); - HttpRequestBuilder andAddResponseHandlersByHttpStatusCodeFactory(HttpResponseHandlersByStatusCodeFactory httpResponseHandlersByStatusCodeFactory); - HttpRequestBuilder andAddResponseHandlerForAnyNotSuccessfulResponse(HttpResponseHandler httpResponseHandler); - HttpRequestBuilder andAddExceptionHandlersByExceptionTypeFactory(HttpExceptionHandlersByExceptionTypeFactory httpExceptionHandlersByExceptionTypeFactory); - HttpRequestBuilder andAddExceptionHandlerByExceptionType(Class exceptionType, ExceptionHandler exceptionHandler); - -} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpRequestBuilderForRetrying.java b/src/main/java/io/github/julucinho/httpclient/HttpRequestBuilderForRetrying.java deleted file mode 100644 index d30b0a7..0000000 --- a/src/main/java/io/github/julucinho/httpclient/HttpRequestBuilderForRetrying.java +++ /dev/null @@ -1,16 +0,0 @@ -package io.github.julucinho.httpclient; - -import io.github.julucinho.httpclient.commons.RetriersByExceptionTypeFactory; - -public interface HttpRequestBuilderForRetrying { - - HttpRequestBuilder andAddRetrierByHttpStatusCode(Integer statusCode, RetrierModel retrierModel); - HttpRequestBuilder andAddRetriersByHttpStatusCodeFactory(RetriersByStatusCodeFactory retriersByStatusCodeFactory); - HttpRequestBuilder andAddRetriersByExceptionTypeFactory(RetriersByExceptionTypeFactory retriersByExceptionTypeFactory); - HttpRequestBuilder andAddRetrierByExceptionType(Class exceptionType, RetrierModel retrierModel); - -} - - - - diff --git a/src/main/java/io/github/julucinho/httpclient/HttpRequestHeaderFactory.java b/src/main/java/io/github/julucinho/httpclient/HttpRequestHeaderFactory.java deleted file mode 100644 index 5ee98f2..0000000 --- a/src/main/java/io/github/julucinho/httpclient/HttpRequestHeaderFactory.java +++ /dev/null @@ -1,9 +0,0 @@ -package io.github.julucinho.httpclient; - -import java.util.Map; - -public interface HttpRequestHeaderFactory { - - Map makeHeaders(); - -} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpRequestMethod.java b/src/main/java/io/github/julucinho/httpclient/HttpRequestMethod.java deleted file mode 100644 index 8b78b1f..0000000 --- a/src/main/java/io/github/julucinho/httpclient/HttpRequestMethod.java +++ /dev/null @@ -1,11 +0,0 @@ -package io.github.julucinho.httpclient; - - -import io.github.julucinho.httpclient.impl.AbstractHttpRequestModel; -import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnExceptionThrownException; - -public interface HttpRequestMethod { - - HttpResponse execute(AbstractHttpRequestModel httpRequestModel) throws RetryNeededOnExceptionThrownException; - -} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpRequestStarter.java b/src/main/java/io/github/julucinho/httpclient/HttpRequestStarter.java deleted file mode 100644 index d305e04..0000000 --- a/src/main/java/io/github/julucinho/httpclient/HttpRequestStarter.java +++ /dev/null @@ -1,12 +0,0 @@ -package io.github.julucinho.httpclient; - -import java.net.http.HttpRequest; - -public interface HttpRequestStarter { - - HttpRequestBuilder startGetRequestModelFor(String url); - HttpRequestBuilder startPostRequestModelFor(String url, HttpRequest.BodyPublisher body); - HttpRequestBuilder startPutRequestModelFor(String url, HttpRequest.BodyPublisher body); - HttpRequestBuilder startDeleteRequestModelFor(String url); - -} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpResponse.java b/src/main/java/io/github/julucinho/httpclient/HttpResponse.java deleted file mode 100644 index 314febe..0000000 --- a/src/main/java/io/github/julucinho/httpclient/HttpResponse.java +++ /dev/null @@ -1,15 +0,0 @@ -package io.github.julucinho.httpclient; - -import com.fasterxml.jackson.core.type.TypeReference; - -import java.util.function.Consumer; - -public interface HttpResponse{ - - Integer getStatusCode(); - HttpRequestModel getHttpRequest(); - T getBodyAs(Class bodyType); - T getBodyAs(TypeReference typeOfExpectedResponseBody); - boolean needsHandling(); - void ifNeedsHandling(Consumer checkOnResponse); -} diff --git a/src/main/java/io/github/julucinho/httpclient/HttpResponseHandler.java b/src/main/java/io/github/julucinho/httpclient/HttpResponseHandler.java deleted file mode 100644 index e46b926..0000000 --- a/src/main/java/io/github/julucinho/httpclient/HttpResponseHandler.java +++ /dev/null @@ -1,8 +0,0 @@ -package io.github.julucinho.httpclient; - -@FunctionalInterface -public interface HttpResponseHandler { - - void handle(HttpResponse httpResponse); - -} diff --git a/src/main/java/io/github/julucinho/httpclient/commons/HandlersFactory.java b/src/main/java/io/github/julucinho/httpclient/commons/HandlersFactory.java deleted file mode 100644 index 4b337d4..0000000 --- a/src/main/java/io/github/julucinho/httpclient/commons/HandlersFactory.java +++ /dev/null @@ -1,7 +0,0 @@ -package io.github.julucinho.httpclient.commons; - -public interface HandlersFactory { - - T makeHandlers(); - -} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestBuilderImpl.java b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestBuilderImpl.java deleted file mode 100644 index 3bde46a..0000000 --- a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestBuilderImpl.java +++ /dev/null @@ -1,105 +0,0 @@ -package io.github.julucinho.httpclient.impl; - - -import io.github.julucinho.httpclient.*; -import io.github.julucinho.httpclient.commons.RetriersByExceptionTypeFactory; - -public class HttpRequestBuilderImpl extends AbstractHttpRequestBuilder { - - public static HttpRequestBuilder of(AbstractHttpRequestModel httpRequest){ - return new HttpRequestBuilderImpl(httpRequest); - } - - private HttpRequestBuilderImpl(AbstractHttpRequestModel httpRequest) { - super(httpRequest); - } - - @Override - public HttpRequestBuilder andAddHeaderOf(String key, String value) { - this.httpRequest.headers.put(key, value); - return this; - } - - @Override - public HttpRequestBuilder andAddHeadersFactory(HttpRequestHeaderFactory httpRequestHeaderFactory) { - httpRequestHeaderFactory.makeHeaders().forEach(this.httpRequest.headers::put); - return this; - } - - @Override - public HttpRequestBuilder andAddPathVariableOf(String pathVariableName, String pathVariableValue) { - this.httpRequest.pathVariables.add(new HttpRequestPathVariable(pathVariableName, pathVariableValue)); - return this; - } - - @Override - public HttpRequestBuilder andAddQueryParameterOf(String queryParameterName, String queryParameterValue) { - this.httpRequest.queryParameters.add(new HttpRequestQueryParameter(queryParameterName, queryParameterValue)); - return this; - } - - @Override - public HttpRequestBuilder andAddProxyAddress(String host, Integer port) { - this.httpRequest.proxyAddress = ProxyAddressModel.builder().host(host).port(port).build(); - return this; - } - - @Override - public HttpRequestModel andFinishBuildingModel() { - return this.httpRequest; - } - - @Override - public HttpRequestBuilder andAddResponseHandlerByHttpStatusCode(Integer statusCode, HttpResponseHandler httpResponseHandler) { - this.httpRequest.responseHandlersByStatusCode.put(statusCode, httpResponseHandler); - return this; - } - - @Override - public HttpRequestBuilder andAddResponseHandlersByHttpStatusCodeFactory(HttpResponseHandlersByStatusCodeFactory httpResponseHandlersByStatusCodeFactory) { - httpResponseHandlersByStatusCodeFactory.makeHandlers().forEach(this.httpRequest.responseHandlersByStatusCode::put); - return this; - } - - @Override - public HttpRequestBuilder andAddResponseHandlerForAnyNotSuccessfulResponse(HttpResponseHandler httpResponseHandler) { - this.httpRequest.genericResponseHandler = httpResponseHandler; - return this; - } - - @Override - public HttpRequestBuilder andAddExceptionHandlerByExceptionType(Class exceptionType, ExceptionHandler exceptionHandler) { - this.httpRequest.exceptionHandlersByExceptionType.put(exceptionType, exceptionHandler); - return this; - } - - @Override - public HttpRequestBuilder andAddExceptionHandlersByExceptionTypeFactory(HttpExceptionHandlersByExceptionTypeFactory httpExceptionHandlersByExceptionTypeFactory) { - httpExceptionHandlersByExceptionTypeFactory.makeHandlers().forEach(this.httpRequest.exceptionHandlersByExceptionType::put); - return this; - } - - @Override - public HttpRequestBuilder andAddRetrierByHttpStatusCode(Integer statusCode, RetrierModel retrierModel) { - this.httpRequest.retryCountersByStatusCode.put(statusCode, RetryCounterImpl.of(retrierModel)); - return this; - } - - @Override - public HttpRequestBuilder andAddRetriersByHttpStatusCodeFactory(RetriersByStatusCodeFactory retriersByStatusCodeFactory) { - retriersByStatusCodeFactory.makeRetriers().forEach((statusCode, retrierModel) -> this.httpRequest.retryCountersByStatusCode.put(statusCode, RetryCounterImpl.of(retrierModel))); - return this; - } - - @Override - public HttpRequestBuilder andAddRetrierByExceptionType(Class exceptionType, RetrierModel retrierModel) { - this.httpRequest.retryCountersByExceptionType.put(exceptionType, RetryCounterImpl.of(retrierModel)); - return this; - } - - @Override - public HttpRequestBuilder andAddRetriersByExceptionTypeFactory(RetriersByExceptionTypeFactory retriersByExceptionTypeFactory) { - retriersByExceptionTypeFactory.makeRetriers().forEach((exceptionType, retrierModel) -> this.httpRequest.retryCountersByExceptionType.put(exceptionType, RetryCounterImpl.of(retrierModel))); - return this; - } -} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPathVariable.java b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPathVariable.java deleted file mode 100644 index 567e4c9..0000000 --- a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestPathVariable.java +++ /dev/null @@ -1,15 +0,0 @@ -package io.github.julucinho.httpclient.impl; - -import lombok.AllArgsConstructor; - -@AllArgsConstructor -class HttpRequestPathVariable { - - private final String pathVariableName; - private final String pathVariableValue; - - String buildPathVariable(){ - return "/".concat(this.pathVariableName).concat("/").concat(this.pathVariableValue); - } - -} diff --git a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestStarterImpl.java b/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestStarterImpl.java deleted file mode 100644 index 63e0785..0000000 --- a/src/main/java/io/github/julucinho/httpclient/impl/HttpRequestStarterImpl.java +++ /dev/null @@ -1,30 +0,0 @@ -package io.github.julucinho.httpclient.impl; - - -import io.github.julucinho.httpclient.HttpRequestBuilder; -import io.github.julucinho.httpclient.HttpRequestStarter; - -import java.net.http.HttpRequest; - -public class HttpRequestStarterImpl implements HttpRequestStarter { - - @Override - public HttpRequestBuilder startGetRequestModelFor(String url) { - return HttpRequestBuilderImpl.of(HttpRequestModelImpl.of(url, new HttpRequestGetMethod())); - } - - @Override - public HttpRequestBuilder startPostRequestModelFor(String url, HttpRequest.BodyPublisher body) { - return HttpRequestBuilderImpl.of(HttpRequestModelImpl.of(url, new HttpRequestPostMethod(), body)); - } - - @Override - public HttpRequestBuilder startPutRequestModelFor(String url, HttpRequest.BodyPublisher body) { - return HttpRequestBuilderImpl.of(HttpRequestModelImpl.of(url, new HttpRequestPutMethod(), body)); - } - - @Override - public HttpRequestBuilder startDeleteRequestModelFor(String url) { - return HttpRequestBuilderImpl.of(HttpRequestModelImpl.of(url, new HttpRequestDeleteMethod())); - } -} diff --git a/src/test/java/io/github/julucinho/httpclient/RetrierModelTest.java b/src/test/java/com/clean_arch_enablers/http_client/RetrierModelTest.java similarity index 95% rename from src/test/java/io/github/julucinho/httpclient/RetrierModelTest.java rename to src/test/java/com/clean_arch_enablers/http_client/RetrierModelTest.java index 434f5af..a4daa9a 100644 --- a/src/test/java/io/github/julucinho/httpclient/RetrierModelTest.java +++ b/src/test/java/com/clean_arch_enablers/http_client/RetrierModelTest.java @@ -1,4 +1,4 @@ -package io.github.julucinho.httpclient; +package com.clean_arch_enablers.http_client; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; diff --git a/src/test/java/io/github/julucinho/httpclient/impl/ExceptionThrownByHttpRequestCheckerTest.java b/src/test/java/com/clean_arch_enablers/http_client/implementations/ExceptionThrownByHttpRequestCheckerTest.java similarity index 73% rename from src/test/java/io/github/julucinho/httpclient/impl/ExceptionThrownByHttpRequestCheckerTest.java rename to src/test/java/com/clean_arch_enablers/http_client/implementations/ExceptionThrownByHttpRequestCheckerTest.java index 5a5fccb..024b176 100644 --- a/src/test/java/io/github/julucinho/httpclient/impl/ExceptionThrownByHttpRequestCheckerTest.java +++ b/src/test/java/com/clean_arch_enablers/http_client/implementations/ExceptionThrownByHttpRequestCheckerTest.java @@ -1,8 +1,8 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; -import io.github.julucinho.httpclient.RetrierModel; -import io.github.julucinho.httpclient.impl.exceptions.JsonProcessingRuntimeException; -import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnExceptionThrownException; +import com.clean_arch_enablers.http_client.RetrierModel; +import com.clean_arch_enablers.http_client.implementations.exceptions.JsonProcessingRuntimeException; +import com.clean_arch_enablers.http_client.implementations.exceptions.RetryNeededOnExceptionThrownException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -19,11 +19,11 @@ class ExceptionThrownByHttpRequestCheckerTest { @BeforeEach void setUp(){ - this.abstractHttpRequestModel = HttpRequestModelImpl.of("http://localhost:8080/tutstuts", new HttpRequestGetMethod()); - this.abstractHttpRequestModel.retryCountersByExceptionType.put(RuntimeException.class, RetryCounterImpl.of(RetrierModel.withLimitOf(4))); + this.abstractHttpRequestModel = HttpRequestModelImplementation.of("http://localhost:8080/tutstuts", new HttpRequestGetMethod()); + this.abstractHttpRequestModel.retryCountersByExceptionType.put(RuntimeException.class, RetryCounterImplementation.of(RetrierModel.withLimitOf(4))); this.abstractHttpRequestModel.exceptionHandlersByExceptionType.put(Exception.class, this::doNothing); - this.abstractHttpRequestModel.retryCountersByExceptionType.put(Exception.class, RetryCounterImpl.of(RetrierModel.withLimitOf(4))); - this.abstractHttpRequestModel.retryCountersByExceptionType.put(ClassNotFoundException.class, RetryCounterImpl.of(RetrierModel.withLimitOf(4))); + this.abstractHttpRequestModel.retryCountersByExceptionType.put(Exception.class, RetryCounterImplementation.of(RetrierModel.withLimitOf(4))); + this.abstractHttpRequestModel.retryCountersByExceptionType.put(ClassNotFoundException.class, RetryCounterImplementation.of(RetrierModel.withLimitOf(4))); } private void doNothing(Exception exception) { } diff --git a/src/test/java/io/github/julucinho/httpclient/impl/FinalHttpRequestFactoryTest.java b/src/test/java/com/clean_arch_enablers/http_client/implementations/FinalHttpRequestFactoryTest.java similarity index 71% rename from src/test/java/io/github/julucinho/httpclient/impl/FinalHttpRequestFactoryTest.java rename to src/test/java/com/clean_arch_enablers/http_client/implementations/FinalHttpRequestFactoryTest.java index c67b258..fe96e1f 100644 --- a/src/test/java/io/github/julucinho/httpclient/impl/FinalHttpRequestFactoryTest.java +++ b/src/test/java/com/clean_arch_enablers/http_client/implementations/FinalHttpRequestFactoryTest.java @@ -1,4 +1,4 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; @@ -14,7 +14,7 @@ class FinalHttpRequestFactoryTest { @Test @DisplayName("Should set query params right") void shouldSetQueryParamsRight(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/uhg", new HttpRequestGetMethod()); + var request = HttpRequestModelImplementation.of("http://localhost:8080/uhg", new HttpRequestGetMethod()); var queryParam1 = new HttpRequestQueryParameter("testKey", "testValue"); var queryParam2 = new HttpRequestQueryParameter("testKey2", "testValue2"); request.queryParameters.add(queryParam1); @@ -28,21 +28,20 @@ void shouldSetQueryParamsRight(){ @Test @DisplayName("Should set path variables right") void shouldSetPathVariablesRight(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/uhg", new HttpRequestGetMethod()); - var pathVariable1 = new HttpRequestPathVariable("companies", "62333"); - var pathVariable2 = new HttpRequestPathVariable("users", "34234"); + var request = HttpRequestModelImplementation.of("http://localhost:8080/uhg/companies/{companyId}/users/{userId}/history", new HttpRequestGetMethod()); + var pathVariable1 = new HttpRequestPathVariable("companyId", "62333"); + var pathVariable2 = new HttpRequestPathVariable("userId", "34234"); request.pathVariables.add(pathVariable1); request.pathVariables.add(pathVariable2); var finalHttpRequest = FinalHttpRequestFactory.makeFinalRequestForGetMethodFrom(request); var uri = finalHttpRequest.uri().toString(); - Assertions.assertTrue(uri.contains(pathVariable1.buildPathVariable())); - Assertions.assertTrue(uri.contains(pathVariable2.buildPathVariable())); + Assertions.assertEquals("http://localhost:8080/uhg/companies/62333/users/34234/history", uri); } @Test @DisplayName("Should set body right") void shouldSetBodyRight(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/uhg", new HttpRequestPostMethod()); + var request = HttpRequestModelImplementation.of("http://localhost:8080/uhg", new HttpRequestPostMethod()); request.body = HttpRequest.BodyPublishers.ofString("{testField:\"testValue\"}"); var finalHttpRequest = FinalHttpRequestFactory.makeFinalRequestForPostMethodFrom(request); var bodyFromFinalRequest = finalHttpRequest.bodyPublisher().orElseThrow(); @@ -52,21 +51,21 @@ void shouldSetBodyRight(){ @Test @DisplayName("Should make instance for get method") void shouldMakeInstanceForGetMethod(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/uhg", new HttpRequestGetMethod()); + var request = HttpRequestModelImplementation.of("http://localhost:8080/uhg", new HttpRequestGetMethod()); Assertions.assertNotNull(FinalHttpRequestFactory.makeFinalRequestForGetMethodFrom(request)); } @Test @DisplayName("Should make instance for delete method") void shouldMakeInstanceForDeleteMethod(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/uhg", new HttpRequestDeleteMethod()); + var request = HttpRequestModelImplementation.of("http://localhost:8080/uhg", new HttpRequestDeleteMethod()); Assertions.assertNotNull(FinalHttpRequestFactory.makeFinalRequestForDeleteMethodFrom(request)); } @Test @DisplayName("Should make instance for post method") void shouldMakeInstanceForPostMethod(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/uhg", new HttpRequestPostMethod()); + var request = HttpRequestModelImplementation.of("http://localhost:8080/uhg", new HttpRequestPostMethod()); request.body = HttpRequest.BodyPublishers.ofString("{testField:\"testValue\"}"); Assertions.assertNotNull(FinalHttpRequestFactory.makeFinalRequestForPostMethodFrom(request)); } @@ -74,7 +73,7 @@ void shouldMakeInstanceForPostMethod(){ @Test @DisplayName("Should make instance for put method") void shouldMakeInstanceForPutMethod(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/uhg", new HttpRequestPutMethod()); + var request = HttpRequestModelImplementation.of("http://localhost:8080/uhg", new HttpRequestPutMethod()); request.body = HttpRequest.BodyPublishers.ofString("{testField:\"testValue\"}"); Assertions.assertNotNull(FinalHttpRequestFactory.makeFinalRequestForPutMethodFrom(request)); } diff --git a/src/test/java/io/github/julucinho/httpclient/impl/HttpRequestBuilderImplTest.java b/src/test/java/com/clean_arch_enablers/http_client/implementations/HttpRequestBuilderImplementationTest.java similarity index 64% rename from src/test/java/io/github/julucinho/httpclient/impl/HttpRequestBuilderImplTest.java rename to src/test/java/com/clean_arch_enablers/http_client/implementations/HttpRequestBuilderImplementationTest.java index a55080f..17b7af2 100644 --- a/src/test/java/io/github/julucinho/httpclient/impl/HttpRequestBuilderImplTest.java +++ b/src/test/java/com/clean_arch_enablers/http_client/implementations/HttpRequestBuilderImplementationTest.java @@ -1,6 +1,6 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; -import io.github.julucinho.httpclient.*; +import com.clean_arch_enablers.http_client.*; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -12,29 +12,29 @@ import java.util.Map; @ExtendWith(MockitoExtension.class) -class HttpRequestBuilderImplTest { +class HttpRequestBuilderImplementationTest { @Test @DisplayName("Should add header correctly") void shouldAddHeaderCorrectly(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); var key = "chuchu"; - builder.andAddHeaderOf(key, "Duda"); + builder.headerOf(key, "Duda"); Assertions.assertNotNull(request.headers.get(key)); } @Test @DisplayName("Should return builder instance when finishing method of adding header") void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingHeader(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); - var returnedBuilder = builder.andAddHeaderOf("chuchu", "Duda"); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); + var returnedBuilder = builder.headerOf("chuchu", "Duda"); Assertions.assertNotNull(returnedBuilder); Assertions.assertEquals(builder, returnedBuilder); } - private static class HeadersFactoryForTestingMatters implements HttpRequestHeaderFactory{ + private static class HeadersFactoryForTestingMatters implements HttpRequestHeaderFactory { static final String KEY_1 = "chuchu"; static final String KEY_2 = "biridinho"; @@ -53,9 +53,9 @@ public Map makeHeaders() { @Test @DisplayName("Should add headers factory correctly") void shouldAddHeadersFactoryCorrectly(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); - builder.andAddHeadersFactory(new HeadersFactoryForTestingMatters()); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); + builder.headersFactory(new HeadersFactoryForTestingMatters()); Arrays.asList(HeadersFactoryForTestingMatters.KEY_1, HeadersFactoryForTestingMatters.KEY_2, HeadersFactoryForTestingMatters.KEY_3) @@ -65,9 +65,9 @@ void shouldAddHeadersFactoryCorrectly(){ @Test @DisplayName("Should return builder instance when finishing method of adding headers factory") void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingHeaderFactory(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); - var returnedBuilder = builder.andAddHeadersFactory(new HeadersFactoryForTestingMatters()); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); + var returnedBuilder = builder.headersFactory(new HeadersFactoryForTestingMatters()); Assertions.assertNotNull(returnedBuilder); Assertions.assertEquals(builder, returnedBuilder); } @@ -75,19 +75,20 @@ void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingHeaderFactory(){ @Test @DisplayName("Should add path variable correctly") void shouldAddPathVariableCorrectly(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); - builder.andAddPathVariableOf("test", "testValue"); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users/test/{testId}", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); + builder.pathVariableOf("testId", "testValue"); Assertions.assertFalse(request.pathVariables.isEmpty()); - Assertions.assertEquals("/test/testValue", request.pathVariables.get(0).buildPathVariable()); + request.pathVariables.get(0).buildPathVariableInto(request); + Assertions.assertEquals("http://localhost:8080/users/test/testValue", request.uri); } @Test @DisplayName("Should return builder instance when finishing method of adding path variable") void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingPathVariable(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); - var returnedBuilder = builder.andAddPathVariableOf("test", "testValue"); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); + var returnedBuilder = builder.pathVariableOf("test", "testValue"); Assertions.assertNotNull(returnedBuilder); Assertions.assertEquals(builder, returnedBuilder); } @@ -95,9 +96,9 @@ void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingPathVariable(){ @Test @DisplayName("Should add query param correctly") void shouldAddQueryParamCorrectly(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); - builder.andAddQueryParameterOf("test", "testValue"); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); + builder.queryParameterOf("test", "testValue"); Assertions.assertFalse(request.queryParameters.isEmpty()); Assertions.assertEquals("test=testValue", request.queryParameters.get(0).buildQueryParameter()); } @@ -105,9 +106,9 @@ void shouldAddQueryParamCorrectly(){ @Test @DisplayName("Should return builder instance when finishing method of adding query param") void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingQueryParam(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); - var returnedBuilder = builder.andAddQueryParameterOf("test", "testValue"); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); + var returnedBuilder = builder.queryParameterOf("test", "testValue"); Assertions.assertNotNull(returnedBuilder); Assertions.assertEquals(builder, returnedBuilder); } @@ -115,9 +116,9 @@ void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingQueryParam(){ @Test @DisplayName("Should finish building process returning request model instance") void shouldFinishBuildingProcessReturningRequestModelInstance(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); - var resultFromBuilding = builder.andFinishBuildingModel(); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); + var resultFromBuilding = builder.finishBuildingModel(); Assertions.assertNotNull(resultFromBuilding); Assertions.assertEquals(request, resultFromBuilding); } @@ -125,11 +126,11 @@ void shouldFinishBuildingProcessReturningRequestModelInstance(){ @Test @DisplayName("Should add response handler by status code correctly") void shouldAddResponseHandlerByStatusCodeCorrectly(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); var statusCode = 400; var handler = (HttpResponseHandler) response -> System.out.println("hello, 400"); - builder.andAddResponseHandlerByHttpStatusCode(statusCode, handler); + builder.handlerByHttpStatusCode(statusCode, handler); Assertions.assertFalse(request.responseHandlersByStatusCode.isEmpty()); Assertions.assertEquals(handler, request.responseHandlersByStatusCode.get(statusCode)); } @@ -137,16 +138,16 @@ void shouldAddResponseHandlerByStatusCodeCorrectly(){ @Test @DisplayName("Should return builder instance when finishing method of adding handler by status code") void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingResponseHandlerByStatusCode(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); var statusCode = 400; var handler = (HttpResponseHandler) response -> System.out.println("hello, 400"); - var returnedBuilder = builder.andAddResponseHandlerByHttpStatusCode(statusCode, handler); + var returnedBuilder = builder.handlerByHttpStatusCode(statusCode, handler); Assertions.assertNotNull(returnedBuilder); Assertions.assertEquals(builder, returnedBuilder); } - private static class HttpResponseHandlersByStatusCodeFactoryForTestingMatters implements HttpResponseHandlersByStatusCodeFactory{ + private static class HttpResponseHandlersByStatusCodeFactoryForTestingMatters implements HttpResponseHandlersByStatusCodeFactory { static final Integer KEY_1 = 400; static final Integer KEY_2 = 401; @@ -165,9 +166,9 @@ public Map makeHandlers() { @Test @DisplayName("Should add response handlers by status code factory correctly") void shouldAddHttpResponseHandlersByStatusCodeFactoryCorrectly(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); - builder.andAddResponseHandlersByHttpStatusCodeFactory(new HttpResponseHandlersByStatusCodeFactoryForTestingMatters()); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); + builder.handlersByHttpStatusCodeFactory(new HttpResponseHandlersByStatusCodeFactoryForTestingMatters()); Arrays.asList(HttpResponseHandlersByStatusCodeFactoryForTestingMatters.KEY_1, HttpResponseHandlersByStatusCodeFactoryForTestingMatters.KEY_2, HttpResponseHandlersByStatusCodeFactoryForTestingMatters.KEY_3) @@ -177,9 +178,9 @@ void shouldAddHttpResponseHandlersByStatusCodeFactoryCorrectly(){ @Test @DisplayName("Should return builder instance when finishing method of adding response handlers by status code factory") void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingHttpResponseHandlersByStatusCodeFactory(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); - var returnedBuilder = builder.andAddResponseHandlersByHttpStatusCodeFactory(new HttpResponseHandlersByStatusCodeFactoryForTestingMatters()); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); + var returnedBuilder = builder.handlersByHttpStatusCodeFactory(new HttpResponseHandlersByStatusCodeFactoryForTestingMatters()); Assertions.assertNotNull(returnedBuilder); Assertions.assertEquals(builder, returnedBuilder); } @@ -187,10 +188,10 @@ void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingHttpResponseHandlersB @Test @DisplayName("Should add response handler for any not successful response correctly") void shouldAddResponseHandlerForAnyNotSuccessfulResponseCorrectly(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); var handler = (HttpResponseHandler) response -> System.out.println("hello, 400"); - builder.andAddResponseHandlerForAnyNotSuccessfulResponse(handler); + builder.handlerForAnyUnsuccessfulResponse(handler); Assertions.assertNotNull(request.genericResponseHandler); Assertions.assertEquals(handler, request.genericResponseHandler); } @@ -198,10 +199,10 @@ void shouldAddResponseHandlerForAnyNotSuccessfulResponseCorrectly(){ @Test @DisplayName("Should return builder instance when finishing method of adding response handler for any not successful response") void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingResponseHandlerForAnyNotSuccessfulResponse(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); var handler = (HttpResponseHandler) response -> System.out.println("hello, 400"); - var returnedBuilder = builder.andAddResponseHandlerForAnyNotSuccessfulResponse(handler); + var returnedBuilder = builder.handlerForAnyUnsuccessfulResponse(handler); Assertions.assertNotNull(returnedBuilder); Assertions.assertEquals(builder, returnedBuilder); } @@ -209,11 +210,11 @@ void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingResponseHandlerForAny @Test @DisplayName("Should add response handler by exception type correctly") void shouldAddResponseHandlerByExceptionTypeCorrectly(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); var handler = (ExceptionHandler) exception -> System.out.println("hello, exception"); var exceptionToHandle = RuntimeException.class; - builder.andAddExceptionHandlerByExceptionType(exceptionToHandle, handler); + builder.handlerByExceptionType(exceptionToHandle, handler); Assertions.assertFalse(request.exceptionHandlersByExceptionType.isEmpty()); Assertions.assertEquals(handler, request.exceptionHandlersByExceptionType.get(exceptionToHandle)); } @@ -221,11 +222,11 @@ void shouldAddResponseHandlerByExceptionTypeCorrectly(){ @Test @DisplayName("Should return builder instance when finishing method of adding response handler by exception type") void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingResponseHandlerByExceptionType(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); var handler = (ExceptionHandler) exception -> System.out.println("hello, exception"); var exceptionToHandle = RuntimeException.class; - var returnedBuilder = builder.andAddExceptionHandlerByExceptionType(exceptionToHandle, handler); + var returnedBuilder = builder.handlerByExceptionType(exceptionToHandle, handler); Assertions.assertNotNull(returnedBuilder); Assertions.assertEquals(builder, returnedBuilder); } @@ -233,11 +234,11 @@ void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingResponseHandlerByExce @Test @DisplayName("Should add retrier by status code correctly") void shouldAddRetrierByHttpStatusCodeCorrectly(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); var retrier = RetrierModel.withLimitOf(4); var statusCode = 503; - builder.andAddRetrierByHttpStatusCode(statusCode, retrier); + builder.retrierByHttpStatusCode(statusCode, retrier); Assertions.assertFalse(request.retryCountersByStatusCode.isEmpty()); Assertions.assertTrue(request.retryCountersByStatusCode.get(statusCode).thereIsRetryAvailable()); } @@ -245,11 +246,11 @@ void shouldAddRetrierByHttpStatusCodeCorrectly(){ @Test @DisplayName("Should return builder instance when finishing method of adding retrier by status code") void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingRetrierByHttpStatusCode(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); var retrier = RetrierModel.withLimitOf(4); var statusCode = 503; - var returnedBuilder = builder.andAddRetrierByHttpStatusCode(statusCode, retrier); + var returnedBuilder = builder.retrierByHttpStatusCode(statusCode, retrier); Assertions.assertNotNull(returnedBuilder); Assertions.assertEquals(builder, returnedBuilder); } @@ -257,11 +258,11 @@ void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingRetrierByHttpStatusCo @Test @DisplayName("Should add retrier by exception type correctly") void shouldAddRetrierByExceptionTypeCorrectly(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); var retrier = RetrierModel.withLimitOf(4); var exceptionType = RuntimeException.class; - builder.andAddRetrierByExceptionType(exceptionType, retrier); + builder.retrierByExceptionType(exceptionType, retrier); Assertions.assertFalse(request.retryCountersByExceptionType.isEmpty()); Assertions.assertTrue(request.retryCountersByExceptionType.get(exceptionType).thereIsRetryAvailable()); } @@ -269,11 +270,11 @@ void shouldAddRetrierByExceptionTypeCorrectly(){ @Test @DisplayName("Should return builder instance when finishing method of adding retrier by exception type") void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingRetrierByExceptionType(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); var retrier = RetrierModel.withLimitOf(4); var exceptionType = RuntimeException.class; - var returnedBuilder = builder.andAddRetrierByExceptionType(exceptionType, retrier); + var returnedBuilder = builder.retrierByExceptionType(exceptionType, retrier); Assertions.assertNotNull(returnedBuilder); Assertions.assertEquals(builder, returnedBuilder); } @@ -298,9 +299,9 @@ public Map, ExceptionHandler> makeHandlers() { @Test @DisplayName("Should add http response handlers by exception type factory correctly") void shouldAddHttpResponseHandlersByExceptionTypeFactoryCorrectly(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); - builder.andAddExceptionHandlersByExceptionTypeFactory(new HttpExceptionHandlersByExceptionTypeFactoryForTestingMatters()); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); + builder.handlersByExceptionTypeFactory(new HttpExceptionHandlersByExceptionTypeFactoryForTestingMatters()); Arrays.asList(HttpExceptionHandlersByExceptionTypeFactoryForTestingMatters.KEY_1, HttpExceptionHandlersByExceptionTypeFactoryForTestingMatters.KEY_2, HttpExceptionHandlersByExceptionTypeFactoryForTestingMatters.KEY_3) @@ -310,9 +311,9 @@ void shouldAddHttpResponseHandlersByExceptionTypeFactoryCorrectly(){ @Test @DisplayName("Should return builder instance when finishing method of adding http response handlers by exception type factory") void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingHttpResponseHandlersByExceptionTypeFactory(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); - var returnedBuilder = builder.andAddExceptionHandlersByExceptionTypeFactory(new HttpExceptionHandlersByExceptionTypeFactoryForTestingMatters()); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); + var returnedBuilder = builder.handlersByExceptionTypeFactory(new HttpExceptionHandlersByExceptionTypeFactoryForTestingMatters()); Assertions.assertNotNull(returnedBuilder); Assertions.assertEquals(builder, returnedBuilder); } @@ -337,9 +338,9 @@ public Map makeRetriers() { @Test @DisplayName("Should add retriers by status code factory correctly") void shouldAddRetriersByStatusCodeFactoryCorrectly(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); - builder.andAddRetriersByHttpStatusCodeFactory(new RetriersByStatusCodeFactoryForTestingMatters()); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); + builder.retriersByHttpStatusCodeFactory(new RetriersByStatusCodeFactoryForTestingMatters()); Arrays.asList(RetriersByStatusCodeFactoryForTestingMatters.KEY_1, RetriersByStatusCodeFactoryForTestingMatters.KEY_2, RetriersByStatusCodeFactoryForTestingMatters.KEY_3) @@ -349,9 +350,9 @@ void shouldAddRetriersByStatusCodeFactoryCorrectly(){ @Test @DisplayName("Should return builder instance when finishing method of adding retriers by status code factory") void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingRetriersByStatusCodeFactory(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); - var returnedBuilder = builder.andAddRetriersByHttpStatusCodeFactory(new RetriersByStatusCodeFactoryForTestingMatters()); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); + var returnedBuilder = builder.retriersByHttpStatusCodeFactory(new RetriersByStatusCodeFactoryForTestingMatters()); Assertions.assertNotNull(returnedBuilder); Assertions.assertEquals(builder, returnedBuilder); } @@ -361,9 +362,9 @@ void shouldReturnBuilderInstanceWhenFinishingMethodOfAddingRetriersByStatusCodeF void shouldSetProxyAddressToTheRequestModel(){ var host = "tururu.com"; var port = 8080; - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request); - builder.andAddProxyAddress(host, port); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request); + builder.proxyAddress(host, port); Assertions.assertNotNull(request.proxyAddress); Assertions.assertNotNull(request.proxyAddress.getHost()); Assertions.assertNotNull(request.proxyAddress.getPort()); @@ -374,8 +375,8 @@ void shouldSetProxyAddressToTheRequestModel(){ @Test @DisplayName("Should return the builder instance after 'andAddProxyAddress' has been called") void shouldReturnTheBuilderInstanceAfterAndAddProxyAddressHasBeenCalled(){ - var request = HttpRequestModelImpl.of("http://localhost:8080/users", new HttpRequestGetMethod()); - var builder = HttpRequestBuilderImpl.of(request).andAddProxyAddress("tururu.com", 8080); + var request = HttpRequestModelImplementation.of("http://localhost:8080/users", new HttpRequestGetMethod()); + var builder = HttpRequestBuilderImplementation.of(request).proxyAddress("tururu.com", 8080); Assertions.assertNotNull(builder); } diff --git a/src/test/java/io/github/julucinho/httpclient/impl/HttpRequestStarterImplTest.java b/src/test/java/com/clean_arch_enablers/http_client/implementations/HttpRequestStarterImplementationTest.java similarity index 72% rename from src/test/java/io/github/julucinho/httpclient/impl/HttpRequestStarterImplTest.java rename to src/test/java/com/clean_arch_enablers/http_client/implementations/HttpRequestStarterImplementationTest.java index 901be85..bab5584 100644 --- a/src/test/java/io/github/julucinho/httpclient/impl/HttpRequestStarterImplTest.java +++ b/src/test/java/com/clean_arch_enablers/http_client/implementations/HttpRequestStarterImplementationTest.java @@ -1,6 +1,6 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; -import io.github.julucinho.httpclient.HttpRequestStarter; +import com.clean_arch_enablers.http_client.HttpRequestStarter; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -10,32 +10,32 @@ import java.net.http.HttpRequest; @ExtendWith(MockitoExtension.class) -class HttpRequestStarterImplTest { +class HttpRequestStarterImplementationTest { - HttpRequestStarter httpRequestStarter = new HttpRequestStarterImpl(); + HttpRequestStarter httpRequestStarter = new HttpRequestStarterImplementation(); @Test @DisplayName("Should return non null instance of builder when starting for delete method") void shouldReturnNonNullInstanceOfBuilderWhenStartingForDeleteMethod(){ - Assertions.assertNotNull(this.httpRequestStarter.startDeleteRequestModelFor("http://localhost:2222")); + Assertions.assertNotNull(this.httpRequestStarter.startDeleteRequestFor("http://localhost:2222")); } @Test @DisplayName("Should return non null instance of builder when starting for get method") void shouldReturnNonNullInstanceOfBuilderWhenStartingForGetMethod(){ - Assertions.assertNotNull(this.httpRequestStarter.startGetRequestModelFor("http://localhost:2222")); + Assertions.assertNotNull(this.httpRequestStarter.startGetRequestFor("http://localhost:2222")); } @Test @DisplayName("Should return non null instance of builder when starting for post method") void shouldReturnNonNullInstanceOfBuilderWhenStartingForPostMethod(){ - Assertions.assertNotNull(this.httpRequestStarter.startPostRequestModelFor("http://localhost:2222", HttpRequest.BodyPublishers.ofString("{someRandomJsonField: \"someRandomValue\"}"))); + Assertions.assertNotNull(this.httpRequestStarter.startPostRequestFor("http://localhost:2222", HttpRequest.BodyPublishers.ofString("{someRandomJsonField: \"someRandomValue\"}"))); } @Test @DisplayName("Should return non null instance of builder when starting for put method") void shouldReturnNonNullInstanceOfBuilderWhenStartingForPutMethod(){ - Assertions.assertNotNull(this.httpRequestStarter.startPutRequestModelFor("http://localhost:2222", HttpRequest.BodyPublishers.ofString("{someRandomJsonField: \"someRandomValue\"}"))); + Assertions.assertNotNull(this.httpRequestStarter.startPutRequestFor("http://localhost:2222", HttpRequest.BodyPublishers.ofString("{someRandomJsonField: \"someRandomValue\"}"))); } } diff --git a/src/test/java/io/github/julucinho/httpclient/impl/HttpResponseImplTest.java b/src/test/java/com/clean_arch_enablers/http_client/implementations/HttpResponseImplementationTest.java similarity index 89% rename from src/test/java/io/github/julucinho/httpclient/impl/HttpResponseImplTest.java rename to src/test/java/com/clean_arch_enablers/http_client/implementations/HttpResponseImplementationTest.java index cade49a..64c4662 100644 --- a/src/test/java/io/github/julucinho/httpclient/impl/HttpResponseImplTest.java +++ b/src/test/java/com/clean_arch_enablers/http_client/implementations/HttpResponseImplementationTest.java @@ -1,8 +1,8 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; +import com.clean_arch_enablers.http_client.HttpRequestModel; +import com.clean_arch_enablers.http_client.HttpResponse; import com.fasterxml.jackson.core.type.TypeReference; -import io.github.julucinho.httpclient.HttpRequestModel; -import io.github.julucinho.httpclient.HttpResponse; import lombok.Getter; import lombok.Setter; import org.junit.jupiter.api.Assertions; @@ -17,9 +17,9 @@ import java.util.List; @ExtendWith(MockitoExtension.class) -class HttpResponseImplTest { +class HttpResponseImplementationTest { - HttpResponseImpl httpResponse; + HttpResponseImplementation httpResponse; @Mock HttpRequestModel httpRequestModel; @Mock @@ -27,7 +27,7 @@ class HttpResponseImplTest { @BeforeEach void setUp(){ - this.httpResponse = new HttpResponseImpl(this.httpRequestModel, this.unwrappedHttpResponse); + this.httpResponse = new HttpResponseImplementation(this.httpRequestModel, this.unwrappedHttpResponse); } @Test diff --git a/src/test/java/io/github/julucinho/httpclient/impl/HttpResponseStatusCodeCheckerTest.java b/src/test/java/com/clean_arch_enablers/http_client/implementations/HttpResponseStatusCodeCheckerTest.java similarity index 83% rename from src/test/java/io/github/julucinho/httpclient/impl/HttpResponseStatusCodeCheckerTest.java rename to src/test/java/com/clean_arch_enablers/http_client/implementations/HttpResponseStatusCodeCheckerTest.java index c6979ce..e6e1038 100644 --- a/src/test/java/io/github/julucinho/httpclient/impl/HttpResponseStatusCodeCheckerTest.java +++ b/src/test/java/com/clean_arch_enablers/http_client/implementations/HttpResponseStatusCodeCheckerTest.java @@ -1,9 +1,9 @@ -package io.github.julucinho.httpclient.impl; +package com.clean_arch_enablers.http_client.implementations; -import io.github.julucinho.httpclient.HttpResponse; -import io.github.julucinho.httpclient.RetrierModel; -import io.github.julucinho.httpclient.impl.exceptions.NoResponseHandlersAvailableForExecutionException; -import io.github.julucinho.httpclient.impl.exceptions.RetryNeededOnHttpStatusCodeException; +import com.clean_arch_enablers.http_client.HttpResponse; +import com.clean_arch_enablers.http_client.RetrierModel; +import com.clean_arch_enablers.http_client.implementations.exceptions.NoResponseHandlersAvailableForExecutionException; +import com.clean_arch_enablers.http_client.implementations.exceptions.RetryNeededOnHttpStatusCodeException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -23,7 +23,7 @@ class HttpResponseStatusCodeCheckerTest { @BeforeEach void setUp(){ - this.httpRequestModel = HttpRequestModelImpl.of("", new HttpRequestGetMethod()); + this.httpRequestModel = HttpRequestModelImplementation.of("", new HttpRequestGetMethod()); this.httpResponseStatusCodeChecker = HttpResponseStatusCodeChecker.of(this.httpRequestModel); } @@ -40,7 +40,7 @@ void shouldThrowParameterizedExceptionTypeForTheReceivedStatusCodeWhenThereIsNoR void shouldThrowRetryNeededOnHttpStatusCodeExceptionForTheReceivedStatusCodeWhenThereIsRetrierSet(){ Mockito.when(this.response.getStatusCode()).thenReturn(400); this.httpRequestModel.responseHandlersByStatusCode.put(400, httpResponse -> {throw new RuntimeException();}); - this.httpRequestModel.retryCountersByStatusCode.put(400, RetryCounterImpl.of(RetrierModel.withLimitOf(4))); + this.httpRequestModel.retryCountersByStatusCode.put(400, RetryCounterImplementation.of(RetrierModel.withLimitOf(4))); Assertions.assertThrows(RetryNeededOnHttpStatusCodeException.class, () -> this.httpResponseStatusCodeChecker.checkOutHandlersFor(this.response)); } diff --git a/src/test/java/io/github/julucinho/httpclient/utils/ThreadIdRetrievementUtilTest.java b/src/test/java/com/clean_arch_enablers/http_client/utils/ThreadIdRetrievementUtilTest.java similarity index 94% rename from src/test/java/io/github/julucinho/httpclient/utils/ThreadIdRetrievementUtilTest.java rename to src/test/java/com/clean_arch_enablers/http_client/utils/ThreadIdRetrievementUtilTest.java index 4856b21..d68d9da 100644 --- a/src/test/java/io/github/julucinho/httpclient/utils/ThreadIdRetrievementUtilTest.java +++ b/src/test/java/com/clean_arch_enablers/http_client/utils/ThreadIdRetrievementUtilTest.java @@ -1,4 +1,4 @@ -package io.github.julucinho.httpclient.utils; +package com.clean_arch_enablers.http_client.utils; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName;