Skip to content

Commit

Permalink
update for serlize json
Browse files Browse the repository at this point in the history
  • Loading branch information
RizkiMufrizal committed Feb 28, 2024
1 parent 1a4006c commit fc98a35
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 63 deletions.
12 changes: 6 additions & 6 deletions src/main/java/com/axway/maven/kps/DeployMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -206,7 +206,7 @@ public void execute() throws MojoExecutionException {
/**
* Create KPS.
*/
private void createKps(String username, String password, String url, String body, List<List<String>> asciiTable, AtomicReference<Integer> i, Object keyKps, Object valueKps, Boolean isKpsExist, Object valueAction) {
private void createKps(String username, String password, String url, Object body, List<List<String>> asciiTable, AtomicReference<Integer> 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);
Expand All @@ -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<List<String>> asciiTable, AtomicReference<Integer> i, Object keyKps, Object valueKps, Boolean isKpsExist, Object valueAction) {
private void updateKps(String username, String password, String url, Object body, List<List<String>> asciiTable, AtomicReference<Integer> i, Object keyKps, Object valueKps, Boolean isKpsExist, Object valueAction) {
if (isKpsExist) {
log.info("Delete KPS Process");
KPSResult mapResponseDeleteKps = kpsClient.deleteKps(username, password, url);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/axway/maven/kps/client/KpsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> headers = HttpClientHeader.setAuthorization(username, password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class HttpClientHeader {
public static Map<String, String> setAuthorization(String username, String password) {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes()));
return headers;
}
Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions src/main/java/com/axway/maven/kps/csv/Convert.java
Original file line number Diff line number Diff line change
@@ -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<Map<?, ?>> toMapList(File file, Character separator) throws IOException;
}
16 changes: 0 additions & 16 deletions src/main/java/com/axway/maven/kps/csv/impl/ConvertJsonImpl.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Map<?, ?>> toMapList(File file, Character separator) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
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 {

@Inject
private Convert convert = new ConvertJsonImpl();

@Test
void testToString() throws JsonProcessingException {
Map<String, Object> 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();
Expand Down

0 comments on commit fc98a35

Please sign in to comment.