From 02fe73b0fa7a86d6214d3cff4837bbe6d37ffe65 Mon Sep 17 00:00:00 2001 From: the-gigi Date: Fri, 26 Jan 2024 13:29:22 -0800 Subject: [PATCH 01/13] urlBase -> baseUrl + make some fields final --- README.md | 8 ++++---- .../cleverclient/example/BasicExample.java | 4 ++-- .../example/FileDownloadExample.java | 4 ++-- .../cleverclient/example/HeaderExample.java | 4 ++-- .../example/MultiServiceExample.java | 4 ++-- .../cleverclient/example/StreamExample.java | 4 ++-- .../cleverclient/CleverClient.java | 18 +++++++++--------- .../cleverclient/http/HttpProcessor.java | 16 ++++++++-------- .../cleverclient/CleverClientTest.java | 8 ++++---- 9 files changed, 35 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index e28eef6..1fb0294 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ public interface PostService { // Use CleverClient to call the API var cleverClient = CleverClient.builder() - .urlBase("https://jsonplaceholder.typicode.com/") + .baseUrl("https://jsonplaceholder.typicode.com/") .build(); var postService = cleverClient.create(PostService.class); @@ -97,7 +97,7 @@ We have the following attributes to create a CleverClient object: | Attribute | Description | Required | | ----------- |----------------------------------------|-----------| -| urlBase | Api's url | mandatory | +| baseUrl | Api's url | mandatory | | headers | Pairs of headers name/value | optional | | httpClient | Java HttpClient object | optional | | endOfStream | Text used to mark the final of streams | optional | @@ -107,7 +107,7 @@ The attribute ```endOfStream``` is required when you have endpoints sending back Example: ```java -final var URL_BASE = "https://api.example.com"; +final var BASE_URL = "https://api.example.com"; final var HEADER_NAME = "Authorization"; final var HEADER_VALUE = "Bearer qwertyasdfghzxcvb"; final var END_OF_STREAM = "[DONE]"; @@ -121,7 +121,7 @@ var httpClient = HttpClient.newBuilder() .build(); var cleverClient = CleverClient.builder() - .urlBase(URL_BASE) + .baseUrl(BASE_URL) .headers(Arrays.asList(HEADER_NAME, HEADER_VALUE)) .httpClient(httpClient) .endOfStream(END_OF_STREAM) diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/BasicExample.java b/src/example/java/io/github/sashirestela/cleverclient/example/BasicExample.java index 758372c..6407feb 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/BasicExample.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/BasicExample.java @@ -7,10 +7,10 @@ public class BasicExample { public static void main(String[] args) { - final var URL_BASE = "https://jsonplaceholder.typicode.com"; + final var BASE_URL = "https://jsonplaceholder.typicode.com"; var cleverClient = CleverClient.builder() - .urlBase(URL_BASE) + .baseUrl(BASE_URL) .build(); var postService = cleverClient.create(PostService.class); diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/FileDownloadExample.java b/src/example/java/io/github/sashirestela/cleverclient/example/FileDownloadExample.java index a07bb71..42ded38 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/FileDownloadExample.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/FileDownloadExample.java @@ -17,10 +17,10 @@ static interface ImageService { } public static void main(String[] args) throws IOException { - final var URL_BASE = "https://via.placeholder.com"; + final var BASE_URL = "https://via.placeholder.com"; var cleverClient = CleverClient.builder() - .urlBase(URL_BASE) + .baseUrl(BASE_URL) .build(); var imageService = cleverClient.create(ImageService.class); diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/HeaderExample.java b/src/example/java/io/github/sashirestela/cleverclient/example/HeaderExample.java index 9d4a39e..3960a01 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/HeaderExample.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/HeaderExample.java @@ -27,10 +27,10 @@ static interface MethodHeaderService { } public static void main(String[] args) { - final var URL_BASE = "https://httpbin.org"; + final var BASE_URL = "https://httpbin.org"; var cleverClient = CleverClient.builder() - .urlBase(URL_BASE) + .baseUrl(BASE_URL) .build(); var classHeaderService = cleverClient.create(ClassHeaderService.class); diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/MultiServiceExample.java b/src/example/java/io/github/sashirestela/cleverclient/example/MultiServiceExample.java index 91de837..e16bbc4 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/MultiServiceExample.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/MultiServiceExample.java @@ -7,10 +7,10 @@ public class MultiServiceExample { public static void main(String[] args) { - final var URL_BASE = "https://jsonplaceholder.typicode.com"; + final var BASE_URL = "https://jsonplaceholder.typicode.com"; var cleverClient = CleverClient.builder() - .urlBase(URL_BASE) + .baseUrl(BASE_URL) .build(); var postService = cleverClient.create(PostService.class); var albumService = cleverClient.create(AlbumService.class); diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/StreamExample.java b/src/example/java/io/github/sashirestela/cleverclient/example/StreamExample.java index 139ea83..594a62c 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/StreamExample.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/StreamExample.java @@ -17,13 +17,13 @@ public class StreamExample { public static void main(String[] args) { - final var URL_BASE = "https://api.openai.com"; + final var BASE_URL = "https://api.openai.com"; final var AUTHORIZATION_HEADER = "Authorization"; final var BEARER_AUTHORIZATION = "Bearer " + System.getenv("OPENAI_API_KEY"); final var END_OF_STREAM = "[DONE]"; var cleverClient = CleverClient.builder() - .urlBase(URL_BASE) + .baseUrl(BASE_URL) .headers(Arrays.asList(AUTHORIZATION_HEADER, BEARER_AUTHORIZATION)) .endOfStream(END_OF_STREAM) .build(); diff --git a/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java b/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java index e1b9ad3..0069a55 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java +++ b/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java @@ -22,17 +22,17 @@ */ @Getter public class CleverClient { - private static Logger logger = LoggerFactory.getLogger(CleverClient.class); + private static final Logger logger = LoggerFactory.getLogger(CleverClient.class); - private String urlBase; - private List headers; - private HttpClient httpClient; - private HttpProcessor httpProcessor; + private final String baseUrl; + private final List headers; + private final HttpClient httpClient; + private final HttpProcessor httpProcessor; /** * Constructor to create an instance of CleverClient. * - * @param urlBase Root of the url of the API service to call. Mandatory. + * @param baseUrl Root of the url of the API service to call. Mandatory. * @param headers Http headers for all the API service. Header's name and * value must be individual entries in the list. Optional. * @param httpClient Custom Java's HttpClient component. One is created by @@ -41,16 +41,16 @@ public class CleverClient { * server sent events (SSE). Optional. */ @Builder - public CleverClient(@NonNull String urlBase, @Singular List headers, HttpClient httpClient, + public CleverClient(@NonNull String baseUrl, @Singular List headers, HttpClient httpClient, String endOfStream) { - this.urlBase = urlBase; + this.baseUrl = baseUrl; this.headers = Optional.ofNullable(headers).orElse(List.of()); if (this.headers.size() % 2 > 0) { throw new CleverClientException("Headers must be entered as pair of values in the list.", null, null); } this.httpClient = Optional.ofNullable(httpClient).orElse(HttpClient.newHttpClient()); CleverClientSSE.setEndOfStream(endOfStream); - this.httpProcessor = new HttpProcessor(this.urlBase, this.headers, this.httpClient); + this.httpProcessor = new HttpProcessor(this.baseUrl, this.headers, this.httpClient); logger.debug("CleverClient has been created."); } diff --git a/src/main/java/io/github/sashirestela/cleverclient/http/HttpProcessor.java b/src/main/java/io/github/sashirestela/cleverclient/http/HttpProcessor.java index d5baa46..01ba735 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/http/HttpProcessor.java +++ b/src/main/java/io/github/sashirestela/cleverclient/http/HttpProcessor.java @@ -22,19 +22,19 @@ public class HttpProcessor implements InvocationHandler { private static final Logger logger = LoggerFactory.getLogger(HttpProcessor.class); - private HttpClient httpClient; - private String urlBase; - private List headers; + private final HttpClient httpClient; + private final String baseUrl; + private final List headers; /** * Constructor to create an instance of HttpProcessor. * * @param httpClient Java's HttpClient component that solves the http calling. - * @param urlBase Root of the url of the API service to call. + * @param baseUrl Root of the url of the API service to call. * @param headers Http headers for all the API service. */ - public HttpProcessor(String urlBase, List headers, HttpClient httpClient) { - this.urlBase = urlBase; + public HttpProcessor(String baseUrl, List headers, HttpClient httpClient) { + this.baseUrl = baseUrl; this.headers = headers; this.httpClient = httpClient; } @@ -93,7 +93,7 @@ public Object invoke(Object proxy, Method method, Object[] arguments) throws Thr /** * Reads the interface method metadata from memory and uses them to prepare an * HttpConnector object that will resend the request to the Java's HttpClient - * and will receive the response. This method is called from the invoke mehod. + * and will receive the response. This method is called from the invoke method. * * @param method The Method instance corresponding to the interface method * invoked on the proxy instance. @@ -106,7 +106,7 @@ private Object resolve(Method method, Object[] arguments) { var interfaceMetadata = InterfaceMetadataStore.one().get(method.getDeclaringClass()); var methodMetadata = interfaceMetadata.getMethodBySignature().get(method.toString()); var urlMethod = interfaceMetadata.getFullUrlByMethod(methodMetadata); - var url = urlBase + URLBuilder.one().build(urlMethod, methodMetadata, arguments); + var url = baseUrl + URLBuilder.one().build(urlMethod, methodMetadata, arguments); var httpMethod = methodMetadata.getHttpAnnotationName(); var returnType = methodMetadata.getReturnType(); var isMultipart = methodMetadata.isMultipart(); diff --git a/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java b/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java index ad53c7c..08d05c7 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java @@ -19,18 +19,18 @@ class CleverClientTest { @Test void shouldSetPropertiesToDefaultValuesWhenBuilderIsCalledWithoutThoseProperties() { var cleverClient = CleverClient.builder() - .urlBase("https://test") + .baseUrl("https://test") .build(); assertEquals(List.of(), cleverClient.getHeaders()); assertEquals(HttpClient.Version.HTTP_2, cleverClient.getHttpClient().version()); - assertNotNull(cleverClient.getUrlBase()); + assertNotNull(cleverClient.getBaseUrl()); assertNotNull(cleverClient.getHttpProcessor()); } @Test void shouldImplementInterfaceWhenCallingCreate() { var cleverClient = CleverClient.builder() - .urlBase("https://test") + .baseUrl("https://test") .header("headerName") .header("headerValue") .httpClient(HttpClient.newHttpClient()) @@ -54,7 +54,7 @@ void shouldThrownExceptionWhenTryingToPassAnEmptyUrlBase() { @Test void shouldThrownExceptionWhenTryingToPassAnOddNumbersOfHeaders() { var cleverClientBuilder = CleverClient.builder() - .urlBase("http://test") + .baseUrl("http://test") .header("oneHeader"); Exception exception = assertThrows(CleverClientException.class, () -> cleverClientBuilder.build()); From a7dd09cd6791d318b64577e01482f4a8fe43f710 Mon Sep 17 00:00:00 2001 From: the-gigi Date: Fri, 26 Jan 2024 13:42:12 -0800 Subject: [PATCH 02/13] replace one more occurence of urlBase in the name of a test method + some minor refactoring --- .../sashirestela/cleverclient/CleverClientTest.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java b/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java index 08d05c7..91a7f03 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java @@ -41,14 +41,14 @@ void shouldImplementInterfaceWhenCallingCreate() { } @Test - void shouldThrownExceptionWhenTryingToPassAnEmptyUrlBase() { + void shouldThrownExceptionWhenTryingToPassAnEmptyBaseUrl() { var cleverClientBuilder = CleverClient.builder() .header("headerName") .header("headerValue") .httpClient(HttpClient.newHttpClient()) .endOfStream("[DONE]"); assertThrows(NullPointerException.class, - () -> cleverClientBuilder.build()); + cleverClientBuilder::build); } @Test @@ -57,8 +57,9 @@ void shouldThrownExceptionWhenTryingToPassAnOddNumbersOfHeaders() { .baseUrl("http://test") .header("oneHeader"); Exception exception = assertThrows(CleverClientException.class, - () -> cleverClientBuilder.build()); - assertTrue(exception.getMessage().equals("Headers must be entered as pair of values in the list.")); + cleverClientBuilder::build); + assertEquals("Headers must be entered as pair of values in the list.", + exception.getMessage()); } interface TestCleverClient { From 63763e9490a64c7af8cf51b88e945ff7655574ae Mon Sep 17 00:00:00 2001 From: the-gigi Date: Sat, 27 Jan 2024 22:22:14 -0800 Subject: [PATCH 03/13] added back the urlBase field (as deprecated) to CleverClient to avoid breaking the API --- .../cleverclient/CleverClient.java | 19 ++++++++++++++++--- .../cleverclient/CleverClientTest.java | 13 +++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java b/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java index 0069a55..816cd24 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java +++ b/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java @@ -25,6 +25,8 @@ public class CleverClient { private static final Logger logger = LoggerFactory.getLogger(CleverClient.class); private final String baseUrl; + @Deprecated + private final String urlBase = ""; // never need to use this deprecated private final List headers; private final HttpClient httpClient; private final HttpProcessor httpProcessor; @@ -32,7 +34,11 @@ public class CleverClient { /** * Constructor to create an instance of CleverClient. * - * @param baseUrl Root of the url of the API service to call. Mandatory. + * @param baseUrl Root of the url of the API service to call. + * at least one of baseUrl and the deprecated urlBase is mandatory. + * in case both are specified and different baseUrl takes precedence + * + * @param urlBase [[ Deprecated ]] Root of the url of the API service to call. * @param headers Http headers for all the API service. Header's name and * value must be individual entries in the list. Optional. * @param httpClient Custom Java's HttpClient component. One is created by @@ -41,9 +47,16 @@ public class CleverClient { * server sent events (SSE). Optional. */ @Builder - public CleverClient(@NonNull String baseUrl, @Singular List headers, HttpClient httpClient, + public CleverClient(String baseUrl, String urlBase, @Singular List headers, HttpClient httpClient, String endOfStream) { - this.baseUrl = baseUrl; + if ((baseUrl == null || baseUrl.isEmpty()) && (urlBase == null || urlBase.isEmpty())) { + throw new CleverClientException("At least one of baseUrl and urlBase is mandatory.", null, null); + } + if (baseUrl != null && !baseUrl.isEmpty()) { + this.baseUrl = baseUrl; + } else { + this.baseUrl = urlBase; + } this.headers = Optional.ofNullable(headers).orElse(List.of()); if (this.headers.size() % 2 > 0) { throw new CleverClientException("Headers must be entered as pair of values in the list.", null, null); diff --git a/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java b/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java index 91a7f03..4d791aa 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java @@ -27,6 +27,19 @@ void shouldSetPropertiesToDefaultValuesWhenBuilderIsCalledWithoutThoseProperties assertNotNull(cleverClient.getHttpProcessor()); } + @Test + void shouldSetPropertiesToDefaultValuesWhenBuilderIsCalledWithoutThoseProperties2() { + var baseUrl = "https://test"; + var cleverClient = CleverClient.builder() + .urlBase(baseUrl) + .build(); + assertEquals(List.of(), cleverClient.getHeaders()); + assertEquals(HttpClient.Version.HTTP_2, cleverClient.getHttpClient().version()); + // verify that baseUrl is set when building with the deprecated urlBase() method + assertEquals(cleverClient.getBaseUrl(), baseUrl); + assertNotNull(cleverClient.getHttpProcessor()); + } + @Test void shouldImplementInterfaceWhenCallingCreate() { var cleverClient = CleverClient.builder() From 3f3c094d86c958e530d6ea69e5e1302283ebb89d Mon Sep 17 00:00:00 2001 From: the-gigi Date: Sat, 27 Jan 2024 22:24:49 -0800 Subject: [PATCH 04/13] fix type of exception thrown when not passing at least one of baseUrl and urlBase --- .../io/github/sashirestela/cleverclient/CleverClientTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java b/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java index 4d791aa..e527251 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java @@ -60,7 +60,7 @@ void shouldThrownExceptionWhenTryingToPassAnEmptyBaseUrl() { .header("headerValue") .httpClient(HttpClient.newHttpClient()) .endOfStream("[DONE]"); - assertThrows(NullPointerException.class, + assertThrows(CleverClientException.class, cleverClientBuilder::build); } From ccd47d1e9793a1d4348fee4c3040c48030ee0bb9 Mon Sep 17 00:00:00 2001 From: the-gigi Date: Sun, 28 Jan 2024 12:17:44 -0800 Subject: [PATCH 05/13] address review comments --- .../cleverclient/CleverClient.java | 8 +- .../cleverclient/CleverClientTest.java | 118 +++++++++--------- 2 files changed, 66 insertions(+), 60 deletions(-) diff --git a/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java b/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java index 816cd24..991272b 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java +++ b/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java @@ -1,5 +1,7 @@ package io.github.sashirestela.cleverclient; +import static io.github.sashirestela.cleverclient.util.CommonUtil.isNullOrEmpty; + import java.net.http.HttpClient; import java.util.List; import java.util.Optional; @@ -26,7 +28,7 @@ public class CleverClient { private final String baseUrl; @Deprecated - private final String urlBase = ""; // never need to use this deprecated + private final String urlBase = null; private final List headers; private final HttpClient httpClient; private final HttpProcessor httpProcessor; @@ -39,6 +41,8 @@ public class CleverClient { * in case both are specified and different baseUrl takes precedence * * @param urlBase [[ Deprecated ]] Root of the url of the API service to call. + * it is here for backward compatibility only. It will be removed in + * a future version. use `baseUrl()` instead. * @param headers Http headers for all the API service. Header's name and * value must be individual entries in the list. Optional. * @param httpClient Custom Java's HttpClient component. One is created by @@ -49,7 +53,7 @@ public class CleverClient { @Builder public CleverClient(String baseUrl, String urlBase, @Singular List headers, HttpClient httpClient, String endOfStream) { - if ((baseUrl == null || baseUrl.isEmpty()) && (urlBase == null || urlBase.isEmpty())) { + if (isNullOrEmpty(baseUrl) && isNullOrEmpty(urlBase)) { throw new CleverClientException("At least one of baseUrl and urlBase is mandatory.", null, null); } if (baseUrl != null && !baseUrl.isEmpty()) { diff --git a/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java b/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java index e527251..3c933bb 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java @@ -16,67 +16,69 @@ class CleverClientTest { - @Test - void shouldSetPropertiesToDefaultValuesWhenBuilderIsCalledWithoutThoseProperties() { - var cleverClient = CleverClient.builder() - .baseUrl("https://test") - .build(); - assertEquals(List.of(), cleverClient.getHeaders()); - assertEquals(HttpClient.Version.HTTP_2, cleverClient.getHttpClient().version()); - assertNotNull(cleverClient.getBaseUrl()); - assertNotNull(cleverClient.getHttpProcessor()); - } - @Test - void shouldSetPropertiesToDefaultValuesWhenBuilderIsCalledWithoutThoseProperties2() { - var baseUrl = "https://test"; - var cleverClient = CleverClient.builder() - .urlBase(baseUrl) - .build(); - assertEquals(List.of(), cleverClient.getHeaders()); - assertEquals(HttpClient.Version.HTTP_2, cleverClient.getHttpClient().version()); - // verify that baseUrl is set when building with the deprecated urlBase() method - assertEquals(cleverClient.getBaseUrl(), baseUrl); - assertNotNull(cleverClient.getHttpProcessor()); - } + @Test + void shouldSetPropertiesToDefaultValuesWhenBuilderIsCalledWithoutThoseProperties() { + var cleverClient = CleverClient.builder() + .baseUrl("https://test") + .build(); + assertEquals(List.of(), cleverClient.getHeaders()); + assertEquals(HttpClient.Version.HTTP_2, cleverClient.getHttpClient().version()); + assertNotNull(cleverClient.getBaseUrl()); + assertNotNull(cleverClient.getHttpProcessor()); + } - @Test - void shouldImplementInterfaceWhenCallingCreate() { - var cleverClient = CleverClient.builder() - .baseUrl("https://test") - .header("headerName") - .header("headerValue") - .httpClient(HttpClient.newHttpClient()) - .endOfStream("[DONE]") - .build(); - var test = cleverClient.create(TestCleverClient.class); - assertNotNull(test); - } + @Test + void shouldBuildSuccessfullyUsingDeprecatedUrlBase() { + var baseUrl = "https://test"; + var cleverClient = CleverClient.builder() + .urlBase(baseUrl) + .build(); + assertEquals(List.of(), cleverClient.getHeaders()); + assertEquals(HttpClient.Version.HTTP_2, cleverClient.getHttpClient().version()); + // verify that baseUrl is set when building with the deprecated urlBase() method + assertEquals(cleverClient.getBaseUrl(), baseUrl); + assertNotNull(cleverClient.getHttpProcessor()); + } - @Test - void shouldThrownExceptionWhenTryingToPassAnEmptyBaseUrl() { - var cleverClientBuilder = CleverClient.builder() - .header("headerName") - .header("headerValue") - .httpClient(HttpClient.newHttpClient()) - .endOfStream("[DONE]"); - assertThrows(CleverClientException.class, - cleverClientBuilder::build); - } + @Test + void shouldImplementInterfaceWhenCallingCreate() { + var cleverClient = CleverClient.builder() + .baseUrl("https://test") + .header("headerName") + .header("headerValue") + .httpClient(HttpClient.newHttpClient()) + .endOfStream("[DONE]") + .build(); + var test = cleverClient.create(TestCleverClient.class); + assertNotNull(test); + } - @Test - void shouldThrownExceptionWhenTryingToPassAnOddNumbersOfHeaders() { - var cleverClientBuilder = CleverClient.builder() - .baseUrl("http://test") - .header("oneHeader"); - Exception exception = assertThrows(CleverClientException.class, - cleverClientBuilder::build); - assertEquals("Headers must be entered as pair of values in the list.", - exception.getMessage()); - } + @Test + void shouldThrownExceptionWhenTryingToPassAnEmptyBaseUrlAndUrlBase() { + var cleverClientBuilder = CleverClient.builder() + .header("headerName") + .header("headerValue") + .httpClient(HttpClient.newHttpClient()) + .endOfStream("[DONE]"); + assertThrows(CleverClientException.class, + cleverClientBuilder::build); + } - interface TestCleverClient { - @GET("/api/") - CompletableFuture getText(); - } + @Test + void shouldThrownExceptionWhenTryingToPassAnOddNumbersOfHeaders() { + var cleverClientBuilder = CleverClient.builder() + .baseUrl("http://test") + .header("oneHeader"); + Exception exception = assertThrows(CleverClientException.class, + cleverClientBuilder::build); + assertEquals("Headers must be entered as pair of values in the list.", + exception.getMessage()); + } + + interface TestCleverClient { + + @GET("/api/") + CompletableFuture getText(); + } } From 93cc1bbe908192aa0dafcfedd869b3ecdf62b96b Mon Sep 17 00:00:00 2001 From: the-gigi Date: Sun, 28 Jan 2024 12:48:10 -0800 Subject: [PATCH 06/13] use isNullOrEmpty one more time --- .../java/io/github/sashirestela/cleverclient/CleverClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java b/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java index 991272b..97521cd 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java +++ b/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java @@ -56,7 +56,7 @@ public CleverClient(String baseUrl, String urlBase, @Singular List heade if (isNullOrEmpty(baseUrl) && isNullOrEmpty(urlBase)) { throw new CleverClientException("At least one of baseUrl and urlBase is mandatory.", null, null); } - if (baseUrl != null && !baseUrl.isEmpty()) { + if (isNullOrEmpty( baseUrl)) { this.baseUrl = baseUrl; } else { this.baseUrl = urlBase; From 5370f7fb1f044d832072faa3b05f4228bef13cce Mon Sep 17 00:00:00 2001 From: the-gigi Date: Sun, 28 Jan 2024 12:50:08 -0800 Subject: [PATCH 07/13] reverse condition --- .../java/io/github/sashirestela/cleverclient/CleverClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java b/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java index 97521cd..b9bbd82 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java +++ b/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java @@ -56,7 +56,7 @@ public CleverClient(String baseUrl, String urlBase, @Singular List heade if (isNullOrEmpty(baseUrl) && isNullOrEmpty(urlBase)) { throw new CleverClientException("At least one of baseUrl and urlBase is mandatory.", null, null); } - if (isNullOrEmpty( baseUrl)) { + if (!isNullOrEmpty(baseUrl)) { this.baseUrl = baseUrl; } else { this.baseUrl = urlBase; From 5ba26281a13d371f9038e8af1be72d6625210a15 Mon Sep 17 00:00:00 2001 From: the-gigi Date: Sun, 28 Jan 2024 12:52:30 -0800 Subject: [PATCH 08/13] more concise setting of this.baseUrl --- .../io/github/sashirestela/cleverclient/CleverClient.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java b/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java index b9bbd82..9b152bc 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java +++ b/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java @@ -56,11 +56,7 @@ public CleverClient(String baseUrl, String urlBase, @Singular List heade if (isNullOrEmpty(baseUrl) && isNullOrEmpty(urlBase)) { throw new CleverClientException("At least one of baseUrl and urlBase is mandatory.", null, null); } - if (!isNullOrEmpty(baseUrl)) { - this.baseUrl = baseUrl; - } else { - this.baseUrl = urlBase; - } + this.baseUrl = isNullOrEmpty(baseUrl) ? urlBase : baseUrl; this.headers = Optional.ofNullable(headers).orElse(List.of()); if (this.headers.size() % 2 > 0) { throw new CleverClientException("Headers must be entered as pair of values in the list.", null, null); From 6891aa375461130569045464b9049f64c3ddd9ad Mon Sep 17 00:00:00 2001 From: the-gigi Date: Sun, 28 Jan 2024 14:23:47 -0800 Subject: [PATCH 09/13] remove unused imports --- .../java/io/github/sashirestela/cleverclient/CleverClient.java | 1 - .../io/github/sashirestela/cleverclient/CleverClientTest.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java b/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java index 9b152bc..5f31164 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java +++ b/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java @@ -14,7 +14,6 @@ import io.github.sashirestela.cleverclient.support.CleverClientSSE; import lombok.Builder; import lombok.Getter; -import lombok.NonNull; import lombok.Singular; /** diff --git a/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java b/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java index 3c933bb..f3b6566 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java @@ -3,7 +3,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; import java.net.http.HttpClient; import java.util.List; From 8fdcde5cd2af63dadbd9fa3df1b2da0637a7e384 Mon Sep 17 00:00:00 2001 From: the-gigi Date: Sun, 28 Jan 2024 14:27:08 -0800 Subject: [PATCH 10/13] bump version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index c1ed707..0e12b28 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.github.sashirestela cleverclient - 0.12.0 + 0.13.0 jar cleverclient From 1e5b9ba7fea7fef4d3676fdc90f1ff03fc899044 Mon Sep 17 00:00:00 2001 From: the-gigi Date: Sun, 28 Jan 2024 14:42:55 -0800 Subject: [PATCH 11/13] fixed a few typos + simplified assertions --- .../cleverclient/metadata/InterfaceMetadataStore.java | 4 ++-- .../cleverclient/metadata/InterfaceMetadataStoreTest.java | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStore.java b/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStore.java index f6c221f..36d00a0 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStore.java +++ b/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStore.java @@ -68,7 +68,7 @@ public InterfaceMetadata get(Class interfaceClass) { if (interfacesByFullName.containsKey(interfaceClass.getName())) { return interfacesByFullName.get(interfaceClass.getName()); } else { - throw new CleverClientException("The interaface {0} has not been saved yet.", interfaceClass.getSimpleName(), + throw new CleverClientException("The interface {0} has not been saved yet.", interfaceClass.getSimpleName(), null); } } @@ -125,7 +125,7 @@ private void validate(InterfaceMetadata interfaceMetadata) { interfaceMetadata.getMethodBySignature().forEach((methodSignature, methodMetadata) -> { if (!methodMetadata.isDefault()) { if (!methodMetadata.hasHttpAnnotation()) { - throw new CleverClientException("Missing HTTP anotation for the method {0}.", + throw new CleverClientException("Missing HTTP annotation for the method {0}.", methodMetadata.getName(), null); } } diff --git a/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java b/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java index c840e4c..97035cd 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java @@ -135,16 +135,15 @@ void shouldSaveInterfaceMetadataWhenAccomplishValidtion() { void shouldThrownExceptionWhenTryingToGetNotPreviouslySavedInterface() { Exception exception = assertThrows(CleverClientException.class, () -> store.get(ITest.SyncService.class)); - assertTrue(exception.getMessage().equals( - "The interaface SyncService has not been saved yet.")); + assertEquals("The interface SyncService has not been saved yet.", exception.getMessage()); } @Test void shouldThrownExceptionWhenMethodHasNotHttpAnnotation() { Exception exception = assertThrows(CleverClientException.class, () -> store.save(ITest.NotAnnotatedService.class)); - assertTrue(exception.getMessage().equals( - "Missing HTTP anotation for the method unannotatedMethod.")); + assertEquals("Missing HTTP annotation for the method unannotatedMethod.", + exception.getMessage()); } @Test From 39193ecf0d9c7bc42816367f9de0de4d328cfeef Mon Sep 17 00:00:00 2001 From: the-gigi Date: Sun, 28 Jan 2024 14:44:48 -0800 Subject: [PATCH 12/13] simplified another assertion --- .../cleverclient/metadata/InterfaceMetadataStoreTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java b/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java index 97035cd..8403ce6 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java @@ -150,7 +150,7 @@ void shouldThrownExceptionWhenMethodHasNotHttpAnnotation() { void shouldThrownExceptionWhenUrlPathParamAtMethodUnmatchesAnnotatedArguments() { Exception exception = assertThrows(CleverClientException.class, () -> store.save(ITest.BadPathParamService.class)); - assertTrue(exception.getMessage().equals( - "Path param demoId in the url cannot find an annotated argument in the method unmatchedPathParamMethod.")); + assertEquals(exception.getMessage(), + "Path param demoId in the url cannot find an annotated argument in the method unmatchedPathParamMethod."); } } \ No newline at end of file From 08f0e81735d770435c1d36cd5d3a85423ebd4b2b Mon Sep 17 00:00:00 2001 From: the-gigi Date: Sun, 28 Jan 2024 18:20:03 -0800 Subject: [PATCH 13/13] incorporate changes from main to fix race condition --- .../github/sashirestela/cleverclient/http/ITest.java | 7 +++++++ .../metadata/InterfaceMetadataStoreTest.java | 12 ++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/test/java/io/github/sashirestela/cleverclient/http/ITest.java b/src/test/java/io/github/sashirestela/cleverclient/http/ITest.java index 003c0d5..e37c591 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/http/ITest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/http/ITest.java @@ -114,6 +114,13 @@ interface SyncService { Stream getDemoStream(@Body RequestDemo request); } + interface NotSavedService { + + @GET("/demos") + CompletableFuture> goodMethod(); + + } + @NoArgsConstructor @AllArgsConstructor @Getter diff --git a/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java b/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java index 8403ce6..c836339 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java @@ -134,8 +134,8 @@ void shouldSaveInterfaceMetadataWhenAccomplishValidtion() { @Test void shouldThrownExceptionWhenTryingToGetNotPreviouslySavedInterface() { Exception exception = assertThrows(CleverClientException.class, - () -> store.get(ITest.SyncService.class)); - assertEquals("The interface SyncService has not been saved yet.", exception.getMessage()); + () -> store.get(ITest.NotSavedService.class)); + assertEquals("The interface NotSavedService has not been saved yet.", exception.getMessage()); } @Test @@ -149,8 +149,8 @@ void shouldThrownExceptionWhenMethodHasNotHttpAnnotation() { @Test void shouldThrownExceptionWhenUrlPathParamAtMethodUnmatchesAnnotatedArguments() { Exception exception = assertThrows(CleverClientException.class, - () -> store.save(ITest.BadPathParamService.class)); - assertEquals(exception.getMessage(), - "Path param demoId in the url cannot find an annotated argument in the method unmatchedPathParamMethod."); + () -> store.save(ITest.BadPathParamService.class)); + assertEquals("Path param demoId in the url cannot find an annotated argument in the method unmatchedPathParamMethod.", + exception.getMessage()); } -} \ No newline at end of file +}