From fc98a354b786dd3e0312a22d9018915caf7dea84 Mon Sep 17 00:00:00 2001 From: RizkiMufrizal Date: Wed, 28 Feb 2024 12:17:52 +0700 Subject: [PATCH] update for serlize json --- .../java/com/axway/maven/kps/DeployMojo.java | 12 +++++------ .../com/axway/maven/kps/client/KpsClient.java | 2 +- .../kps/client/rest/KpsRestClientImpl.java | 2 +- .../maven/kps/common/HttpClientHeader.java | 1 + .../kps/common/JacksonStringSerializer.java | 20 ------------------- .../java/com/axway/maven/kps/csv/Convert.java | 3 --- .../maven/kps/csv/impl/ConvertJsonImpl.java | 16 --------------- .../kps/csv/impl/ConvertJsonImplTest.java | 18 ++--------------- 8 files changed, 11 insertions(+), 63 deletions(-) delete mode 100644 src/main/java/com/axway/maven/kps/common/JacksonStringSerializer.java diff --git a/src/main/java/com/axway/maven/kps/DeployMojo.java b/src/main/java/com/axway/maven/kps/DeployMojo.java index 2def39d..55dee4e 100644 --- a/src/main/java/com/axway/maven/kps/DeployMojo.java +++ b/src/main/java/com/axway/maven/kps/DeployMojo.java @@ -160,8 +160,8 @@ public void execute() throws MojoExecutionException { */ if (String.valueOf(valueAction).equalsIgnoreCase("INSERT")) { try { - this.createKps(username, password, urlKps + "/" + URLEncoder.encode(String.valueOf(valueKps), StandardCharsets.UTF_8.toString()), convert.toString(k), asciiTable, i, keyKps, valueKps, isKpsExist, valueAction); - } catch (JsonProcessingException | UnsupportedEncodingException e) { + this.createKps(username, password, urlKps + "/" + URLEncoder.encode(String.valueOf(valueKps), StandardCharsets.UTF_8.toString()), k, asciiTable, i, keyKps, valueKps, isKpsExist, valueAction); + } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } @@ -171,8 +171,8 @@ public void execute() throws MojoExecutionException { */ if (String.valueOf(valueAction).equalsIgnoreCase("UPDATE")) { try { - this.updateKps(username, password, urlKps + "/" + URLEncoder.encode(String.valueOf(valueKps), StandardCharsets.UTF_8.toString()), convert.toString(k), asciiTable, i, keyKps, valueKps, isKpsExist, valueAction); - } catch (JsonProcessingException | UnsupportedEncodingException e) { + this.updateKps(username, password, urlKps + "/" + URLEncoder.encode(String.valueOf(valueKps), StandardCharsets.UTF_8.toString()), k, asciiTable, i, keyKps, valueKps, isKpsExist, valueAction); + } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } @@ -206,7 +206,7 @@ public void execute() throws MojoExecutionException { /** * Create KPS. */ - private void createKps(String username, String password, String url, String body, List> asciiTable, AtomicReference i, Object keyKps, Object valueKps, Boolean isKpsExist, Object valueAction) { + private void createKps(String username, String password, String url, Object body, List> asciiTable, AtomicReference i, Object keyKps, Object valueKps, Boolean isKpsExist, Object valueAction) { if (isKpsExist) { log.info("Cannot Create KPS because KPS Is Exist"); addRow(i.get(), keyKps, valueKps, valueAction, false, "KPS Is Exist", asciiTable); @@ -220,7 +220,7 @@ private void createKps(String username, String password, String url, String body /** * Update KPS. */ - private void updateKps(String username, String password, String url, String body, List> asciiTable, AtomicReference i, Object keyKps, Object valueKps, Boolean isKpsExist, Object valueAction) { + private void updateKps(String username, String password, String url, Object body, List> asciiTable, AtomicReference i, Object keyKps, Object valueKps, Boolean isKpsExist, Object valueAction) { if (isKpsExist) { log.info("Delete KPS Process"); KPSResult mapResponseDeleteKps = kpsClient.deleteKps(username, password, url); diff --git a/src/main/java/com/axway/maven/kps/client/KpsClient.java b/src/main/java/com/axway/maven/kps/client/KpsClient.java index da9f361..9690f87 100644 --- a/src/main/java/com/axway/maven/kps/client/KpsClient.java +++ b/src/main/java/com/axway/maven/kps/client/KpsClient.java @@ -5,7 +5,7 @@ public interface KpsClient { Boolean isExistingKps(String username, String password, String url); - KPSResult createKps(String username, String password, String url, String body); + KPSResult createKps(String username, String password, String url, Object body); KPSResult deleteKps(String username, String password, String url); } \ No newline at end of file diff --git a/src/main/java/com/axway/maven/kps/client/rest/KpsRestClientImpl.java b/src/main/java/com/axway/maven/kps/client/rest/KpsRestClientImpl.java index 2ded925..39a8fdc 100644 --- a/src/main/java/com/axway/maven/kps/client/rest/KpsRestClientImpl.java +++ b/src/main/java/com/axway/maven/kps/client/rest/KpsRestClientImpl.java @@ -33,7 +33,7 @@ public Boolean isExistingKps(String username, String password, String url) { } @Override - public KPSResult createKps(String username, String password, String url, String body) { + public KPSResult createKps(String username, String password, String url, Object body) { int connectTimeout = 30000; int socketTimeout = 30000; Map headers = HttpClientHeader.setAuthorization(username, password); diff --git a/src/main/java/com/axway/maven/kps/common/HttpClientHeader.java b/src/main/java/com/axway/maven/kps/common/HttpClientHeader.java index 5bd5881..3640f64 100644 --- a/src/main/java/com/axway/maven/kps/common/HttpClientHeader.java +++ b/src/main/java/com/axway/maven/kps/common/HttpClientHeader.java @@ -7,6 +7,7 @@ public class HttpClientHeader { public static Map setAuthorization(String username, String password) { Map headers = new HashMap<>(); + headers.put("Content-Type", "application/json"); headers.put("Authorization", "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes())); return headers; } diff --git a/src/main/java/com/axway/maven/kps/common/JacksonStringSerializer.java b/src/main/java/com/axway/maven/kps/common/JacksonStringSerializer.java deleted file mode 100644 index 0ace486..0000000 --- a/src/main/java/com/axway/maven/kps/common/JacksonStringSerializer.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.axway.maven.kps.common; - -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.databind.JsonSerializer; -import com.fasterxml.jackson.databind.SerializerProvider; - -import java.io.IOException; - -public class JacksonStringSerializer extends JsonSerializer { - @Override - public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { - if (s.equalsIgnoreCase("true")) { - jsonGenerator.writeBoolean(true); - } else if (s.equalsIgnoreCase("false")) { - jsonGenerator.writeBoolean(false); - } else { - jsonGenerator.writeString(s); - } - } -} diff --git a/src/main/java/com/axway/maven/kps/csv/Convert.java b/src/main/java/com/axway/maven/kps/csv/Convert.java index 27d28b3..e1b168e 100644 --- a/src/main/java/com/axway/maven/kps/csv/Convert.java +++ b/src/main/java/com/axway/maven/kps/csv/Convert.java @@ -1,14 +1,11 @@ package com.axway.maven.kps.csv; -import com.fasterxml.jackson.core.JsonProcessingException; - import java.io.File; import java.io.IOException; import java.util.List; import java.util.Map; public interface Convert { - String toString(Map data) throws JsonProcessingException; List> toMapList(File file, Character separator) throws IOException; } \ No newline at end of file diff --git a/src/main/java/com/axway/maven/kps/csv/impl/ConvertJsonImpl.java b/src/main/java/com/axway/maven/kps/csv/impl/ConvertJsonImpl.java index e18dba8..51bc848 100644 --- a/src/main/java/com/axway/maven/kps/csv/impl/ConvertJsonImpl.java +++ b/src/main/java/com/axway/maven/kps/csv/impl/ConvertJsonImpl.java @@ -1,12 +1,7 @@ package com.axway.maven.kps.csv.impl; -import com.axway.maven.kps.common.JacksonObject; -import com.axway.maven.kps.common.JacksonStringSerializer; import com.axway.maven.kps.csv.Convert; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.MappingIterator; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.dataformat.csv.CsvMapper; import com.fasterxml.jackson.dataformat.csv.CsvSchema; import lombok.extern.slf4j.Slf4j; @@ -22,17 +17,6 @@ @Singleton @Slf4j public class ConvertJsonImpl implements Convert { - @Override - public String toString(Map data) throws JsonProcessingException { - ObjectMapper objectMapper = JacksonObject.objectMapper(); - objectMapper.writerWithDefaultPrettyPrinter(); - SimpleModule module = new SimpleModule(); - module.addSerializer(String.class, new JacksonStringSerializer()); - objectMapper.registerModule(module); - String json = objectMapper.writeValueAsString(data); - log.info("Convert To JSON String {}", json); - return json; - } @Override public List> toMapList(File file, Character separator) throws IOException { diff --git a/src/test/java/com/axway/maven/kps/csv/impl/ConvertJsonImplTest.java b/src/test/java/com/axway/maven/kps/csv/impl/ConvertJsonImplTest.java index a1aee0c..94b3a4b 100644 --- a/src/test/java/com/axway/maven/kps/csv/impl/ConvertJsonImplTest.java +++ b/src/test/java/com/axway/maven/kps/csv/impl/ConvertJsonImplTest.java @@ -1,20 +1,18 @@ package com.axway.maven.kps.csv.impl; import com.axway.maven.kps.csv.Convert; -import com.fasterxml.jackson.core.JsonProcessingException; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import javax.inject.Inject; - import java.io.File; import java.io.IOException; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; @Slf4j class ConvertJsonImplTest { @@ -22,18 +20,6 @@ class ConvertJsonImplTest { @Inject private Convert convert = new ConvertJsonImpl(); - @Test - void testToString() throws JsonProcessingException { - Map stringObjectMap = new HashMap<>(); - stringObjectMap.put("Message", "Success Message"); - stringObjectMap.put("Sucess", Boolean.TRUE); - - log.info("Convert Object Map {}", stringObjectMap); - String mapToJsonString = this.convert.toString(stringObjectMap); - assertNotNull(mapToJsonString); - assertFalse(mapToJsonString.isEmpty()); - } - @Test void toMapList() throws IOException { ClassLoader classLoader = getClass().getClassLoader();