Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
RizkiMufrizal committed Apr 12, 2023
1 parent 0641268 commit 1a453ce
Show file tree
Hide file tree
Showing 27 changed files with 515 additions and 352 deletions.
76 changes: 56 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>com.axway.maven.plugins</groupId>
<artifactId>kps-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>1.0.0</version>
<version>1.1.0</version>
<name>KPS Maven Plugin</name>
<url>https://github.com/RizkiMufrizal/KPS-Maven-Plugin</url>

Expand All @@ -22,23 +22,27 @@
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven-plugin-api.version>3.8.6</maven-plugin-api.version>
<maven-core.version>3.8.6</maven-core.version>
<maven-plugin-annotations.version>3.6.4</maven-plugin-annotations.version>
<maven-plugin-plugin.version>3.6.4</maven-plugin-plugin.version>
<maven-site-plugin.version>3.12.0</maven-site-plugin.version>
<maven-project-info-reports-plugin.version>3.4.1</maven-project-info-reports-plugin.version>
<jackson.version>2.13.4.2</jackson.version>
<jackson-dataformat-csv.version>2.13.4</jackson-dataformat-csv.version>
<lombok.version>1.18.24</lombok.version>
<unirest-java.version>3.13.11</unirest-java.version>
<maven-plugin-api.version>3.9.1</maven-plugin-api.version>
<maven-core.version>3.9.1</maven-core.version>
<maven-plugin-annotations.version>3.8.1</maven-plugin-annotations.version>
<maven-plugin-plugin.version>3.8.1</maven-plugin-plugin.version>
<maven-site-plugin.version>4.0.0-M6</maven-site-plugin.version>
<maven-project-info-reports-plugin.version>3.4.2</maven-project-info-reports-plugin.version>
<spotbugs-maven-plugin.version>4.7.3.4</spotbugs-maven-plugin.version>
<spotbugs.version>4.7.3</spotbugs.version>
<findsecbugs-plugin.version>1.12.0</findsecbugs-plugin.version>
<maven-pmd-plugin.version>3.20.0</maven-pmd-plugin.version>
<jackson.version>2.14.2</jackson.version>
<jackson-dataformat-csv.version>2.14.2</jackson-dataformat-csv.version>
<lombok.version>1.18.26</lombok.version>
<httpclient5.version>5.2.1</httpclient5.version>
<ascii-table.version>1.8.0</ascii-table.version>
<slf4j.version>2.0.3</slf4j.version>
<junit-jupiter-api.version>5.9.1</junit-jupiter-api.version>
<junit-jupiter-engine.version>5.9.1</junit-jupiter-engine.version>
<wiremock-jre8.version>2.34.0</wiremock-jre8.version>
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
<maven-failsafe-plugin.version>3.0.0-M7</maven-failsafe-plugin.version>
<slf4j.version>2.0.7</slf4j.version>
<junit-jupiter-api.version>5.9.2</junit-jupiter-api.version>
<junit-jupiter-engine.version>5.9.2</junit-jupiter-engine.version>
<wiremock-jre8.version>2.35.0</wiremock-jre8.version>
<maven-surefire-plugin.version>3.0.0</maven-surefire-plugin.version>
<maven-failsafe-plugin.version>3.0.0</maven-failsafe-plugin.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -77,9 +81,9 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>${unirest-java.version}</version>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>${httpclient5.version}</version>
</dependency>
<dependency>
<groupId>com.github.freva</groupId>
Expand Down Expand Up @@ -172,6 +176,38 @@
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe-plugin.version}</version>
</plugin>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>${spotbugs-maven-plugin.version}</version>
<dependencies>
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs</artifactId>
<version>${spotbugs.version}</version>
</dependency>
</dependencies>
<configuration>
<includeFilterFile>spotbugs-security-include.xml</includeFilterFile>
<excludeFilterFile>spotbugs-security-exclude.xml</excludeFilterFile>
<plugins>
<plugin>
<groupId>com.h3xstream.findsecbugs</groupId>
<artifactId>findsecbugs-plugin</artifactId>
<version>${findsecbugs-plugin.version}</version>
</plugin>
</plugins>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${maven-pmd-plugin.version}</version>
<configuration>
<failOnViolation>true</failOnViolation>
<printFailingErrors>true</printFailingErrors>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
75 changes: 36 additions & 39 deletions src/main/java/com/axway/maven/kps/DeployMojo.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package com.axway.maven.kps;

import com.axway.maven.kps.config.Constant;
import com.axway.maven.kps.config.UnirestExecution;
import com.axway.maven.kps.client.KpsClient;
import com.axway.maven.kps.client.TopologyClient;
import com.axway.maven.kps.client.object.response.KPSResult;
import com.axway.maven.kps.client.object.response.ServiceResponse;
import com.axway.maven.kps.client.object.response.TopologyResponse;
import com.axway.maven.kps.client.rest.KpsRestClientImpl;
import com.axway.maven.kps.client.rest.TopologyRestClientImpl;
import com.axway.maven.kps.common.Constant;
import com.axway.maven.kps.csv.Convert;
import com.axway.maven.kps.csv.ReadCsvFile;
import com.axway.maven.kps.csv.impl.ConvertJsonImpl;
import com.axway.maven.kps.csv.impl.ReadCsvFileImpl;
import com.axway.maven.kps.restclient.KpsRestClient;
import com.axway.maven.kps.restclient.TopologyRestClient;
import com.axway.maven.kps.restclient.impl.KpsRestClientImpl;
import com.axway.maven.kps.restclient.impl.TopologyRestClientImpl;
import com.axway.maven.kps.restclient.mapper.Service;
import com.axway.maven.kps.restclient.mapper.Topology;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.freva.asciitable.AsciiTable;
import lombok.extern.slf4j.Slf4j;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -45,15 +44,15 @@ public class DeployMojo extends AbstractMojo {

private final Convert convert;
private final ReadCsvFile readCsvFile;
private final TopologyRestClient topologyRestClient;
private final KpsRestClient kpsRestClient;
private final TopologyClient topologyClient;
private final KpsClient kpsClient;

@Inject
public DeployMojo(ConvertJsonImpl convertJsonImpl, ReadCsvFileImpl readCsvFileImpl, TopologyRestClientImpl topologyRestClientImpl, KpsRestClientImpl kpsRestClientImpl) {
this.convert = convertJsonImpl;
this.readCsvFile = readCsvFileImpl;
this.topologyRestClient = topologyRestClientImpl;
this.kpsRestClient = kpsRestClientImpl;
this.topologyClient = topologyRestClientImpl;
this.kpsClient = kpsRestClientImpl;
}

@Parameter(defaultValue = "${project}", required = true, readonly = true)
Expand Down Expand Up @@ -124,12 +123,11 @@ public DeployMojo(ConvertJsonImpl convertJsonImpl, ReadCsvFileImpl readCsvFileIm
private Character csvSeparator;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
public void execute() throws MojoExecutionException {
try {
UnirestExecution.run(username, password);
String urlTopology = Constant.PROTOCOL + host + ":" + port + Constant.URL_TOPOLOGY;
Topology topology = topologyRestClient.getTopologies(urlTopology);
Optional<Service> optionalTopology = topology.getResult().getServices().stream().parallel().filter(t -> t.getName().equals(instance)).findFirst();
TopologyResponse topologyResponse = topologyClient.getTopologies(username, password, urlTopology);
Optional<ServiceResponse> optionalTopology = topologyResponse.getResultResponse().getServiceResponses().stream().parallel().filter(t -> t.getName().equals(instance)).findFirst();
if (optionalTopology.isPresent()) {

String urlKps = Constant.PROTOCOL + host + ":" + port + Constant.URL_KPS(optionalTopology.get().getId(), tableName);
Expand All @@ -143,16 +141,17 @@ public void execute() throws MojoExecutionException, MojoFailureException {
csvFileListMap.forEach(k -> {
log.info("====================================================================");
i.getAndSet(i.get() + 1);
Object keyKps = k.keySet().stream().findFirst().orElse(null);
Object keyKps = k.keySet().stream().parallel().findFirst().orElse(null);
Object valueKps = k.get(keyKps);
Object valueAction = k.get("action");
Boolean isKpsExist;
try {
isKpsExist = kpsRestClient.isExistingKps(urlKps + "/" + URLEncoder.encode(String.valueOf(valueKps), StandardCharsets.UTF_8.toString()));
isKpsExist = kpsClient.isExistingKps(username, password, urlKps + "/" + URLEncoder.encode(String.valueOf(valueKps), StandardCharsets.UTF_8.toString()));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
log.info("KPS with Key {} and Value {} is {}", keyKps, valueKps, (isKpsExist ? "Exist" : "Not Exist"));
String resultIsKpsExist = isKpsExist ? "Exist" : "Not Exist";
log.info("KPS with Key {} and Value {} is {}", keyKps, valueKps, resultIsKpsExist);
log.info("Action Process {}", valueAction);
k.remove("action");

Expand All @@ -161,7 +160,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
*/
if (String.valueOf(valueAction).equalsIgnoreCase("INSERT")) {
try {
this.createKps(urlKps + "/" + URLEncoder.encode(String.valueOf(valueKps), StandardCharsets.UTF_8.toString()), convert.toString(k), asciiTable, i, keyKps, valueKps, isKpsExist, valueAction);
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) {
throw new RuntimeException(e);
}
Expand All @@ -172,7 +171,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
*/
if (String.valueOf(valueAction).equalsIgnoreCase("UPDATE")) {
try {
this.updateKps(urlKps + "/" + URLEncoder.encode(String.valueOf(valueKps), StandardCharsets.UTF_8.toString()), convert.toString(k), asciiTable, i, keyKps, valueKps, isKpsExist, valueAction);
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) {
throw new RuntimeException(e);
}
Expand All @@ -183,7 +182,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
*/
if (String.valueOf(valueAction).equalsIgnoreCase("DELETE")) {
try {
this.deleteKps(urlKps + "/" + URLEncoder.encode(String.valueOf(valueKps), StandardCharsets.UTF_8.toString()), asciiTable, i, keyKps, valueKps, isKpsExist, valueAction);
this.deleteKps(username, password, urlKps + "/" + URLEncoder.encode(String.valueOf(valueKps), StandardCharsets.UTF_8.toString()), asciiTable, i, keyKps, valueKps, isKpsExist, valueAction);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
Expand All @@ -201,55 +200,53 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
} catch (Exception e) {
throw new MojoExecutionException(e);
} finally {
UnirestExecution.shutDown();
}
}

/**
* Create KPS.
*/
private void createKps(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, String 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);
} else {
log.info("Create KPS Process");
Map<String, Object> mapResponseKps = kpsRestClient.createKps(url, body);
addRow(i.get(), keyKps, valueKps, valueAction, mapResponseKps.get("Success"), mapResponseKps.get("Message"), asciiTable);
KPSResult mapResponseKps = kpsClient.createKps(username, password, url, body);
addRow(i.get(), keyKps, valueKps, valueAction, mapResponseKps.getSuccess(), mapResponseKps.getMessage(), asciiTable);
}
}

/**
* Update KPS.
*/
private void updateKps(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, String body, List<List<String>> asciiTable, AtomicReference<Integer> i, Object keyKps, Object valueKps, Boolean isKpsExist, Object valueAction) {
if (isKpsExist) {
log.info("Delete KPS Process");
Map<String, Object> mapResponseDeleteKps = kpsRestClient.deleteKps(url);
if (mapResponseDeleteKps.get("Success").equals(true)) {
KPSResult mapResponseDeleteKps = kpsClient.deleteKps(username, password, url);
if (mapResponseDeleteKps.getSuccess()) {
log.info("Update KPS Process");
Map<String, Object> mapResponseKps = kpsRestClient.createKps(url, body);
addRow(i.get(), keyKps, valueKps, valueAction, mapResponseKps.get("Success"), mapResponseKps.get("Message"), asciiTable);
KPSResult mapResponseKps = kpsClient.createKps(username, password, url, body);
addRow(i.get(), keyKps, valueKps, valueAction, mapResponseKps.getSuccess(), mapResponseKps.getMessage(), asciiTable);
} else {
log.info("Delete KPS Process Failed");
addRow(i.get(), keyKps, valueKps, valueAction + "-DELETE", mapResponseDeleteKps.get("Success"), mapResponseDeleteKps.get("Message"), asciiTable);
addRow(i.get(), keyKps, valueKps, valueAction + "-DELETE", mapResponseDeleteKps.getSuccess(), mapResponseDeleteKps.getMessage(), asciiTable);
}
} else {
log.info("KPS Not Exist, Create KPS Process");
Map<String, Object> mapResponseKps = kpsRestClient.createKps(url, body);
addRow(i.get(), keyKps, valueKps, valueAction, mapResponseKps.get("Success"), mapResponseKps.get("Message"), asciiTable);
KPSResult mapResponseKps = kpsClient.createKps(username, password, url, body);
addRow(i.get(), keyKps, valueKps, valueAction, mapResponseKps.getSuccess(), mapResponseKps.getMessage(), asciiTable);
}
}

/**
* Delete KPS.
*/
private void deleteKps(String url, List<List<String>> asciiTable, AtomicReference<Integer> i, Object keyKps, Object valueKps, Boolean isKpsExist, Object valueAction) {
private void deleteKps(String username, String password, String url, List<List<String>> asciiTable, AtomicReference<Integer> i, Object keyKps, Object valueKps, Boolean isKpsExist, Object valueAction) {
if (isKpsExist) {
log.info("Delete KPS Process");
Map<String, Object> mapResponseKps = kpsRestClient.deleteKps(url);
addRow(i.get(), keyKps, valueKps, valueAction, mapResponseKps.get("Success"), mapResponseKps.get("Message"), asciiTable);
KPSResult mapResponseKps = kpsClient.deleteKps(username, password, url);
addRow(i.get(), keyKps, valueKps, valueAction, mapResponseKps.getSuccess(), mapResponseKps.getMessage(), asciiTable);
} else {
log.info("KPS Not Exist, Delete KPS Process Failed");
addRow(i.get(), keyKps, valueKps, valueAction, false, "KPS Not Exist", asciiTable);
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/axway/maven/kps/client/KpsClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.axway.maven.kps.client;

import com.axway.maven.kps.client.object.response.KPSResult;

public interface KpsClient {
Boolean isExistingKps(String username, String password, String url);

KPSResult createKps(String username, String password, String url, String body);

KPSResult deleteKps(String username, String password, String url);
}
7 changes: 7 additions & 0 deletions src/main/java/com/axway/maven/kps/client/TopologyClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.axway.maven.kps.client;

import com.axway.maven.kps.client.object.response.TopologyResponse;

public interface TopologyClient {
TopologyResponse getTopologies(String username, String password, String url) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.axway.maven.kps.client.object.response;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class GeneralStringResponse extends HttpStatusResponse implements Serializable {
private String response;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.axway.maven.kps.client.object.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class HttpStatusResponse implements Serializable {
@JsonProperty("httpCode")
private Integer httpCode;
@JsonProperty("httpMessage")
private String httpMessage;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.axway.maven.kps.client.object.response;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Builder
@Getter
@Setter
public class KPSResult implements Serializable {
private Boolean success;
private String message;
}
Loading

0 comments on commit 1a453ce

Please sign in to comment.