Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for using external Json ObjectMapper #78

Merged
merged 3 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .devfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ components:
- name: m2
path: /home/user/.m2
mountSources: true
memoryRequest: 2G
memoryLimit: 6G
cpuRequest: 2000m
cpuLimit: 4000m
- name: m2
volume:
size: 5Gi
size: 8Gi
commands: []
36 changes: 23 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.sashirestela</groupId>
<artifactId>cleverclient</artifactId>
<version>1.4.4</version>
<version>1.5.0</version>
<packaging>jar</packaging>

<name>cleverclient</name>
Expand Down Expand Up @@ -52,21 +52,21 @@
<maven.compiler.release>11</maven.compiler.release>
<!-- Dependencies Versions -->
<slf4j.version>2.0.16</slf4j.version>
<lombok.version>1.18.34</lombok.version>
<jackson.version>2.17.2</jackson.version>
<junit.version>5.11.0</junit.version>
<mockito.version>5.13.0</mockito.version>
<lombok.version>1.18.36</lombok.version>
<jackson.version>2.18.1</jackson.version>
<junit.version>5.11.3</junit.version>
<mockito.version>5.14.2</mockito.version>
<!-- Plugins Versions -->
<compiler.version>3.13.0</compiler.version>
<enforcer.version>3.5.0</enforcer.version>
<surefire.version>3.5.0</surefire.version>
<exec.version>3.4.1</exec.version>
<surefire.version>3.5.2</surefire.version>
<exec.version>3.5.0</exec.version>
<jacoco.version>0.8.12</jacoco.version>
<helper.version>3.6.0</helper.version>
<versions.version>2.17.1</versions.version>
<versions.version>2.18.0</versions.version>
<source.version>3.3.1</source.version>
<javadoc.version>3.10.0</javadoc.version>
<gpg.version>3.2.5</gpg.version>
<javadoc.version>3.11.1</javadoc.version>
<gpg.version>3.2.7</gpg.version>
<sonatype.version>1.7.0</sonatype.version>
<spotless.version>2.43.0</spotless.version>
</properties>
Expand Down Expand Up @@ -180,7 +180,7 @@
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
</plugin>
</plugins>
</build>
</profile>
Expand Down Expand Up @@ -210,6 +210,12 @@
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down Expand Up @@ -246,6 +252,10 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down Expand Up @@ -290,7 +300,7 @@
<requireMavenVersion>
<version>3.6.3</version>
</requireMavenVersion>
</rules>
</rules>
</configuration>
</execution>
</executions>
Expand Down Expand Up @@ -375,4 +385,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.sashirestela.cleverclient;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.sashirestela.cleverclient.http.HttpProcessor;
import io.github.sashirestela.cleverclient.http.HttpRequestData;
import io.github.sashirestela.cleverclient.support.Configurator;
Expand Down Expand Up @@ -46,11 +47,12 @@ public class CleverClient {
* @param bodyInspector Function to inspect the Body request parameter.
* @param endsOfStream Texts used to mark the final of streams when handling server sent
* events (SSE). Optional.
* @param objectMapper Provides Json conversions either to and from objects. Optional.
*/
@Builder
public CleverClient(@NonNull String baseUrl, @Singular Map<String, String> headers, HttpClient httpClient,
UnaryOperator<HttpRequestData> requestInterceptor, Consumer<Object> bodyInspector,
@Singular("endOfStream") List<String> endsOfStream) {
@Singular("endOfStream") List<String> endsOfStream, ObjectMapper objectMapper) {
this.baseUrl = baseUrl;
this.headers = Optional.ofNullable(headers).orElse(Map.of());
this.httpClient = Optional.ofNullable(httpClient).orElse(HttpClient.newHttpClient());
Expand All @@ -65,6 +67,7 @@ public CleverClient(@NonNull String baseUrl, @Singular Map<String, String> heade
.build();
Configurator.builder()
.endsOfStream(Optional.ofNullable(endsOfStream).orElse(Arrays.asList()))
.objectMapper(Optional.ofNullable(objectMapper).orElse(new ObjectMapper()))
.build();
logger.debug("CleverClient has been created.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
package io.github.sashirestela.cleverclient.support;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.Singular;

import java.util.List;

@Getter
public class Configurator {

private static Configurator configurator = new Configurator();

@Getter
private List<String> endsOfStream;
private ObjectMapper objectMapper;

@Getter(AccessLevel.NONE)
private boolean wasBuilt = false;

private Configurator() {
}

@Builder
public Configurator(@Singular("endOfStream") List<String> endsOfStream) {
public Configurator(@Singular("endOfStream") List<String> endsOfStream, ObjectMapper objectMapper) {
if (configurator.wasBuilt) {
return;
}
configurator.endsOfStream = endsOfStream;
configurator.objectMapper = objectMapper;
configurator.wasBuilt = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.type.CollectionType;
import io.github.sashirestela.cleverclient.support.CleverClientException;
import io.github.sashirestela.cleverclient.support.Configurator;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -16,7 +17,7 @@

public class JsonUtil {

private static final ObjectMapper objectMapperStrict = new ObjectMapper();
private static final ObjectMapper objectMapperStrict = Configurator.one().getObjectMapper();

private static final ObjectReader objectReaderIgnoringUnknown = objectMapperStrict.reader()
.without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ void shouldImplementInterfaceWhenCallingCreate() {
.baseUrl("https://test")
.header("headerName", "headerValue")
.httpClient(HttpClient.newHttpClient())
.endOfStream("[DONE]")
.build();
var test = cleverClient.create(TestCleverClient.class);
assertNotNull(test);
Expand All @@ -62,8 +61,7 @@ void shouldImplementInterfaceWhenCallingCreate() {
void shouldThrownExceptionWhenTryingToPassAnEmptyBaseUrl() {
var cleverClientBuilder = CleverClient.builder()
.header("headerName", "headerValue")
.httpClient(HttpClient.newHttpClient())
.endOfStream("[DONE]");
.httpClient(HttpClient.newHttpClient());
assertThrows(NullPointerException.class,
cleverClientBuilder::build);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
package io.github.sashirestela.cleverclient.support;

import io.github.sashirestela.cleverclient.support.CleverClientSSE.LineRecord;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;

import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;

@Order(1)
class CleverClientSSETest {

Set<String> events = Set.of("process", "process2");

@BeforeAll
static void setup() {
Configurator.builder()
.endOfStream("END")
.build();
}

@Test
void shouldReturnExpectedValueWhenRawDataMeetsConditions() {
Object[][] testData = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.sashirestela.cleverclient.support;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

@Order(1)
class ConfiguratorTest {

@Test
void testBuilder() {
assertThrows(CleverClientException.class, () -> Configurator.one());
Configurator.builder()
.endOfStream("END")
.objectMapper(new ObjectMapper().registerModule(new JavaTimeModule()))
.build();
assertDoesNotThrow(() -> Configurator.one());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.experimental.SuperBuilder;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -130,6 +131,13 @@ void shouldThrowExceptionWhenConvertingObjectToMapWithIssues() {
assertThrows(CleverClientException.class, () -> JsonUtil.objectToMap(object));
}

@Test
void shouldHandleLocalDateWhenAnObjectMapperWithTimeModuleIsPassed() {
String expectedJson = "{\"date\":[2024,12,31]}";
String actualJson = JsonUtil.objectToJson(new DateClass(LocalDate.of(2024, 12, 31)));
assertEquals(expectedJson, actualJson);
}

@NoArgsConstructor
@AllArgsConstructor
@Getter
Expand Down Expand Up @@ -199,4 +207,13 @@ static class FailClass {

}

@NoArgsConstructor
@AllArgsConstructor
@Getter
static class DateClass {

private LocalDate date;

}

}