From 30c72058766088bb028036d390a43f813edb63ae Mon Sep 17 00:00:00 2001 From: Jerome Brongniart Date: Wed, 14 Oct 2020 15:58:31 +0200 Subject: [PATCH] SED-526: Refactoring libraries to remove dependencies to other libraries --- keywords/java/step-library-commons/pom.xml | 34 ++++ .../commons}/AbstractEnhancedKeyword.java | 40 ++--- .../commons/AbstractProcessKeyword.java | 155 ++++++++++++++++++ .../library/commons}/BusinessException.java | 18 +- keywords/java/step-library-kw-http/pom.xml | 5 + .../http/keywords/HttpClientKeyword.java | 4 +- .../step-library-kw-monitoring-system/pom.xml | 12 +- .../kw/monitoring/TypePerfKeywords.java | 2 +- .../WindowsServiceStatusKeywords.java | 2 +- .../library/kw/step/StepClientKeyword.java | 1 + keywords/java/step-library-kw-system/pom.xml | 8 +- .../kw/system/AbstractProcessKeyword.java | 155 ------------------ .../library/kw/system/ProcessKeywords.java | 1 + step-library-parent/pom.xml | 6 + 14 files changed, 245 insertions(+), 198 deletions(-) create mode 100644 keywords/java/step-library-commons/pom.xml rename keywords/java/{step-library-kw-http/src/main/java/ch/exense/step/examples/common/helper => step-library-commons/src/main/java/ch/exense/step/library/commons}/AbstractEnhancedKeyword.java (84%) create mode 100644 keywords/java/step-library-commons/src/main/java/ch/exense/step/library/commons/AbstractProcessKeyword.java rename keywords/java/{step-library-kw-http/src/main/java/ch/exense/step/examples/common/helper => step-library-commons/src/main/java/ch/exense/step/library/commons}/BusinessException.java (69%) delete mode 100644 keywords/java/step-library-kw-system/src/main/java/ch/exense/step/library/kw/system/AbstractProcessKeyword.java diff --git a/keywords/java/step-library-commons/pom.xml b/keywords/java/step-library-commons/pom.xml new file mode 100644 index 0000000..dcedd15 --- /dev/null +++ b/keywords/java/step-library-commons/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + step-library-commons + + + ch.exense.step.library + step-library-parent + 0.0.0-SNAPSHOT + ../../../step-library-parent + + + + + diff --git a/keywords/java/step-library-kw-http/src/main/java/ch/exense/step/examples/common/helper/AbstractEnhancedKeyword.java b/keywords/java/step-library-commons/src/main/java/ch/exense/step/library/commons/AbstractEnhancedKeyword.java similarity index 84% rename from keywords/java/step-library-kw-http/src/main/java/ch/exense/step/examples/common/helper/AbstractEnhancedKeyword.java rename to keywords/java/step-library-commons/src/main/java/ch/exense/step/library/commons/AbstractEnhancedKeyword.java index b5269f3..d6d6732 100644 --- a/keywords/java/step-library-kw-http/src/main/java/ch/exense/step/examples/common/helper/AbstractEnhancedKeyword.java +++ b/keywords/java/step-library-commons/src/main/java/ch/exense/step/library/commons/AbstractEnhancedKeyword.java @@ -1,20 +1,20 @@ -package ch.exense.step.examples.common.helper; - -import step.handlers.javahandler.AbstractKeyword; - -/** - * @author Jonathan Rubiero - */ -public class AbstractEnhancedKeyword extends AbstractKeyword { - - @Override - public boolean onError(Exception e) { - if(e instanceof BusinessException) { - output.setBusinessError(e.getMessage()); - return false; - } else { - return super.onError(e); - } - } - -} +package ch.exense.step.library.commons; + +import step.handlers.javahandler.AbstractKeyword; + +/** + * @author Jonathan Rubiero + */ +public class AbstractEnhancedKeyword extends AbstractKeyword { + + @Override + public boolean onError(Exception e) { + if(e instanceof BusinessException) { + output.setBusinessError(e.getMessage()); + return false; + } else { + return super.onError(e); + } + } + +} diff --git a/keywords/java/step-library-commons/src/main/java/ch/exense/step/library/commons/AbstractProcessKeyword.java b/keywords/java/step-library-commons/src/main/java/ch/exense/step/library/commons/AbstractProcessKeyword.java new file mode 100644 index 0000000..47dceee --- /dev/null +++ b/keywords/java/step-library-commons/src/main/java/ch/exense/step/library/commons/AbstractProcessKeyword.java @@ -0,0 +1,155 @@ +/******************************************************************************* + * Copyright (C) 2020, exense GmbH + * + * This file is part of STEP + * + * STEP is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * STEP is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with STEP. If not, see . + ******************************************************************************/ +package ch.exense.step.library.commons; + +import ch.exense.commons.processes.ManagedProcess; +import ch.exense.commons.processes.ManagedProcess.ManagedProcessException; +import step.grid.io.Attachment; +import step.grid.io.AttachmentHelper; +import step.handlers.javahandler.AbstractKeyword; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.util.List; +import java.util.concurrent.TimeoutException; +import java.util.function.Consumer; + +public abstract class AbstractProcessKeyword extends AbstractKeyword { + + protected static final int PROCESS_TIMEOUT = 60000; + + public AbstractProcessKeyword() { + super(); + } + + public static class OutputConfiguration { + + private final boolean alwaysAttachOutput; + private final int maxOutputPayloadSize; + private final int maxOutputAttachmentSize; + private final boolean printExitCode; + private final boolean checkExitCode; + + public OutputConfiguration() { + this(true, 1000, 1000000, true, true); + } + + public OutputConfiguration(boolean alwaysAttachOutput, int maxOutputPayloadSize, int maxOutputAttachmentSize, + boolean printExitCode, boolean checkExitCode) { + super(); + this.alwaysAttachOutput = alwaysAttachOutput; + this.maxOutputPayloadSize = maxOutputPayloadSize; + this.maxOutputAttachmentSize = maxOutputAttachmentSize; + this.printExitCode = printExitCode; + this.checkExitCode = checkExitCode; + } + + protected boolean isAlwaysAttachOutput() { + return alwaysAttachOutput; + } + + protected int getMaxOutputPayloadSize() { + return maxOutputPayloadSize; + } + + protected int getMaxOutputAttachmentSize() { + return maxOutputAttachmentSize; + } + + protected boolean isCheckExitCode() { + return checkExitCode; + } + } + + protected void executeManagedCommand(String cmd, int timeoutMs) throws Exception { + executeManagedCommand(cmd, timeoutMs, new OutputConfiguration(), null); + } + + protected void executeManagedCommand(String cmd, int timeoutMs, OutputConfiguration outputConfiguration) throws Exception { + executeManagedCommand(cmd, timeoutMs, outputConfiguration, null); + } + + protected void executeManagedCommand(List cmd, int timeoutMs, OutputConfiguration outputConfiguration, Consumer postProcess) throws Exception { + ManagedProcess process = new ManagedProcess(cmd); + executeManagedCommand(timeoutMs, outputConfiguration, postProcess, process); + } + + protected void executeManagedCommand(String cmd, int timeoutMs, OutputConfiguration outputConfiguration, Consumer postProcess) throws Exception { + ManagedProcess process = new ManagedProcess(cmd); + executeManagedCommand(timeoutMs, outputConfiguration, postProcess, process); + } + + protected void executeManagedCommand(int timeoutMs, OutputConfiguration outputConfiguration, + Consumer postProcess, ManagedProcess process) + throws ManagedProcessException, InterruptedException, IOException { + try { + boolean hasError = false; + process.start(); + try { + int exitCode = process.waitFor(timeoutMs); + if (outputConfiguration.isCheckExitCode() && exitCode != 0) { + output.setBusinessError("Process exited with code " + exitCode); + hasError = true; + } + if(outputConfiguration.printExitCode) { + output.add("Exit_code", Integer.toString(exitCode)); + } + if(postProcess != null) { + postProcess.accept(process); + } + } catch (TimeoutException e) { + output.setBusinessError("Process didn't exit within the defined timeout of "+timeoutMs+"ms"); + hasError = true; + } + + if(hasError || outputConfiguration.isAlwaysAttachOutput()) { + attachOutputs(process, outputConfiguration); + } + } finally { + process.close(); + } + } + + protected void attachOutputs(ManagedProcess process, OutputConfiguration outputConfiguration) throws IOException { + attachOutput("stdout", process.getProcessOutputLog(), outputConfiguration); + attachOutput("stderr", process.getProcessErrorLog(), outputConfiguration); + } + + protected void attachOutput(String outputName, File file, OutputConfiguration outputConfiguration) throws IOException { + StringBuilder processOutputBuilder = new StringBuilder(); + Files.readAllLines(file.toPath(), Charset.defaultCharset()).forEach(l -> processOutputBuilder.append(l).append("\n")); + + String processOutput = processOutputBuilder.toString(); + + output.add(outputName, processOutput.substring(0, Math.min(processOutput.length(), outputConfiguration.maxOutputPayloadSize))); + + if(processOutput.length() > outputConfiguration.maxOutputPayloadSize) { + Attachment attachment = AttachmentHelper.generateAttachmentFromByteArray( + processOutput.substring(0, Math.min(processOutput.length(), outputConfiguration.maxOutputAttachmentSize)).getBytes(), outputName + ".log"); + output.addAttachment(attachment); + + if (file.length() > outputConfiguration.maxOutputAttachmentSize) { + output.add("technicalWarning", + outputName + " size exceeded. " + outputName + " has been attached and truncated."); + } + } + } +} diff --git a/keywords/java/step-library-kw-http/src/main/java/ch/exense/step/examples/common/helper/BusinessException.java b/keywords/java/step-library-commons/src/main/java/ch/exense/step/library/commons/BusinessException.java similarity index 69% rename from keywords/java/step-library-kw-http/src/main/java/ch/exense/step/examples/common/helper/BusinessException.java rename to keywords/java/step-library-commons/src/main/java/ch/exense/step/library/commons/BusinessException.java index e1b404a..e26ede6 100644 --- a/keywords/java/step-library-kw-http/src/main/java/ch/exense/step/examples/common/helper/BusinessException.java +++ b/keywords/java/step-library-commons/src/main/java/ch/exense/step/library/commons/BusinessException.java @@ -1,9 +1,9 @@ -package ch.exense.step.examples.common.helper; - -public class BusinessException extends RuntimeException { - - public BusinessException(String message) { - super(message); - } - -} +package ch.exense.step.library.commons; + +public class BusinessException extends RuntimeException { + + public BusinessException(String message) { + super(message); + } + +} diff --git a/keywords/java/step-library-kw-http/pom.xml b/keywords/java/step-library-kw-http/pom.xml index 23a20d2..413ad87 100644 --- a/keywords/java/step-library-kw-http/pom.xml +++ b/keywords/java/step-library-kw-http/pom.xml @@ -12,6 +12,11 @@ + + ch.exense.step.library + step-library-commons + ${project.version} + org.apache.httpcomponents httpclient diff --git a/keywords/java/step-library-kw-http/src/main/java/ch/exense/step/examples/http/keywords/HttpClientKeyword.java b/keywords/java/step-library-kw-http/src/main/java/ch/exense/step/examples/http/keywords/HttpClientKeyword.java index ad370ba..8600f82 100644 --- a/keywords/java/step-library-kw-http/src/main/java/ch/exense/step/examples/http/keywords/HttpClientKeyword.java +++ b/keywords/java/step-library-kw-http/src/main/java/ch/exense/step/examples/http/keywords/HttpClientKeyword.java @@ -13,11 +13,11 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import ch.exense.step.library.commons.AbstractEnhancedKeyword; +import ch.exense.step.library.commons.BusinessException; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; -import ch.exense.step.examples.common.helper.AbstractEnhancedKeyword; -import ch.exense.step.examples.common.helper.BusinessException; import ch.exense.step.examples.http.HttpClient; import ch.exense.step.examples.http.HttpRequest; import ch.exense.step.examples.http.HttpResponse; diff --git a/keywords/java/step-library-kw-monitoring-system/pom.xml b/keywords/java/step-library-kw-monitoring-system/pom.xml index 8cb467a..2af8a95 100644 --- a/keywords/java/step-library-kw-monitoring-system/pom.xml +++ b/keywords/java/step-library-kw-monitoring-system/pom.xml @@ -30,16 +30,16 @@ - - ch.exense.step.library - step-library-kw-system - ${project.version} - ch.exense.step - step-functions-base-functions + step-core ${step.version} + + ch.exense.step.library + step-library-commons + ${project.version} + org.codehaus.groovy groovy-jsr223 diff --git a/keywords/java/step-library-kw-monitoring-system/src/main/java/ch/exense/step/library/kw/monitoring/TypePerfKeywords.java b/keywords/java/step-library-kw-monitoring-system/src/main/java/ch/exense/step/library/kw/monitoring/TypePerfKeywords.java index cf33aa3..41219e2 100644 --- a/keywords/java/step-library-kw-monitoring-system/src/main/java/ch/exense/step/library/kw/monitoring/TypePerfKeywords.java +++ b/keywords/java/step-library-kw-monitoring-system/src/main/java/ch/exense/step/library/kw/monitoring/TypePerfKeywords.java @@ -35,7 +35,7 @@ import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; -import ch.exense.step.library.kw.system.AbstractProcessKeyword; +import ch.exense.step.library.commons.AbstractProcessKeyword; import step.handlers.javahandler.Keyword; public class TypePerfKeywords extends AbstractProcessKeyword { diff --git a/keywords/java/step-library-kw-monitoring-system/src/main/java/ch/exense/step/library/kw/monitoring/WindowsServiceStatusKeywords.java b/keywords/java/step-library-kw-monitoring-system/src/main/java/ch/exense/step/library/kw/monitoring/WindowsServiceStatusKeywords.java index b526b00..e45e43c 100644 --- a/keywords/java/step-library-kw-monitoring-system/src/main/java/ch/exense/step/library/kw/monitoring/WindowsServiceStatusKeywords.java +++ b/keywords/java/step-library-kw-monitoring-system/src/main/java/ch/exense/step/library/kw/monitoring/WindowsServiceStatusKeywords.java @@ -21,7 +21,7 @@ import java.io.File; import java.nio.file.Files; -import ch.exense.step.library.kw.system.AbstractProcessKeyword; +import ch.exense.step.library.commons.AbstractProcessKeyword; import step.handlers.javahandler.Keyword; public class WindowsServiceStatusKeywords extends AbstractProcessKeyword { diff --git a/keywords/java/step-library-kw-step-client/src/main/java/ch/exense/step/library/kw/step/StepClientKeyword.java b/keywords/java/step-library-kw-step-client/src/main/java/ch/exense/step/library/kw/step/StepClientKeyword.java index 55e826f..df72ddd 100644 --- a/keywords/java/step-library-kw-step-client/src/main/java/ch/exense/step/library/kw/step/StepClientKeyword.java +++ b/keywords/java/step-library-kw-step-client/src/main/java/ch/exense/step/library/kw/step/StepClientKeyword.java @@ -1,5 +1,6 @@ package ch.exense.step.library.kw.step; +import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeoutException; diff --git a/keywords/java/step-library-kw-system/pom.xml b/keywords/java/step-library-kw-system/pom.xml index d0db8f8..59ff7c3 100644 --- a/keywords/java/step-library-kw-system/pom.xml +++ b/keywords/java/step-library-kw-system/pom.xml @@ -31,12 +31,12 @@ - ch.exense.commons - exense-basic-commons - ${exense-commons.version} + ch.exense.step.library + step-library-commons + ${project.version} - org.apache.commons + commons-io commons-io 1.3.2 diff --git a/keywords/java/step-library-kw-system/src/main/java/ch/exense/step/library/kw/system/AbstractProcessKeyword.java b/keywords/java/step-library-kw-system/src/main/java/ch/exense/step/library/kw/system/AbstractProcessKeyword.java deleted file mode 100644 index a090e1e..0000000 --- a/keywords/java/step-library-kw-system/src/main/java/ch/exense/step/library/kw/system/AbstractProcessKeyword.java +++ /dev/null @@ -1,155 +0,0 @@ -/******************************************************************************* - * Copyright (C) 2020, exense GmbH - * - * This file is part of STEP - * - * STEP is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * STEP is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with STEP. If not, see . - ******************************************************************************/ -package ch.exense.step.library.kw.system; - -import java.io.File; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.file.Files; -import java.util.List; -import java.util.concurrent.TimeoutException; -import java.util.function.Consumer; - -import ch.exense.commons.processes.ManagedProcess; -import ch.exense.commons.processes.ManagedProcess.ManagedProcessException; -import step.grid.io.Attachment; -import step.grid.io.AttachmentHelper; -import step.handlers.javahandler.AbstractKeyword; - -public abstract class AbstractProcessKeyword extends AbstractKeyword { - - protected static final int PROCESS_TIMEOUT = 60000; - - public AbstractProcessKeyword() { - super(); - } - - public static class OutputConfiguration { - - private final boolean alwaysAttachOutput; - private final int maxOutputPayloadSize; - private final int maxOutputAttachmentSize; - private final boolean printExitCode; - private final boolean checkExitCode; - - public OutputConfiguration() { - this(true, 1000, 1000000, true, true); - } - - public OutputConfiguration(boolean alwaysAttachOutput, int maxOutputPayloadSize, int maxOutputAttachmentSize, - boolean printExitCode, boolean checkExitCode) { - super(); - this.alwaysAttachOutput = alwaysAttachOutput; - this.maxOutputPayloadSize = maxOutputPayloadSize; - this.maxOutputAttachmentSize = maxOutputAttachmentSize; - this.printExitCode = printExitCode; - this.checkExitCode = checkExitCode; - } - - protected boolean isAlwaysAttachOutput() { - return alwaysAttachOutput; - } - - protected int getMaxOutputPayloadSize() { - return maxOutputPayloadSize; - } - - protected int getMaxOutputAttachmentSize() { - return maxOutputAttachmentSize; - } - - protected boolean isCheckExitCode() { - return checkExitCode; - } - } - - protected void executeManagedCommand(String cmd, int timeoutMs) throws Exception { - executeManagedCommand(cmd, timeoutMs, new OutputConfiguration(), null); - } - - protected void executeManagedCommand(String cmd, int timeoutMs, OutputConfiguration outputConfiguration) throws Exception { - executeManagedCommand(cmd, timeoutMs, outputConfiguration, null); - } - - protected void executeManagedCommand(List cmd, int timeoutMs, OutputConfiguration outputConfiguration, Consumer postProcess) throws Exception { - ManagedProcess process = new ManagedProcess(cmd); - executeManagedCommand(timeoutMs, outputConfiguration, postProcess, process); - } - - protected void executeManagedCommand(String cmd, int timeoutMs, OutputConfiguration outputConfiguration, Consumer postProcess) throws Exception { - ManagedProcess process = new ManagedProcess(cmd); - executeManagedCommand(timeoutMs, outputConfiguration, postProcess, process); - } - - protected void executeManagedCommand(int timeoutMs, OutputConfiguration outputConfiguration, - Consumer postProcess, ManagedProcess process) - throws ManagedProcessException, InterruptedException, IOException { - try { - boolean hasError = false; - process.start(); - try { - int exitCode = process.waitFor(timeoutMs); - if (outputConfiguration.isCheckExitCode() && exitCode != 0) { - output.setBusinessError("Process exited with code " + exitCode); - hasError = true; - } - if(outputConfiguration.printExitCode) { - output.add("Exit_code", Integer.toString(exitCode)); - } - if(postProcess != null) { - postProcess.accept(process); - } - } catch (TimeoutException e) { - output.setBusinessError("Process didn't exit within the defined timeout of "+timeoutMs+"ms"); - hasError = true; - } - - if(hasError || outputConfiguration.isAlwaysAttachOutput()) { - attachOutputs(process, outputConfiguration); - } - } finally { - process.close(); - } - } - - protected void attachOutputs(ManagedProcess process, OutputConfiguration outputConfiguration) throws IOException { - attachOutput("stdout", process.getProcessOutputLog(), outputConfiguration); - attachOutput("stderr", process.getProcessErrorLog(), outputConfiguration); - } - - protected void attachOutput(String outputName, File file, OutputConfiguration outputConfiguration) throws IOException { - StringBuilder processOutputBuilder = new StringBuilder(); - Files.readAllLines(file.toPath(), Charset.defaultCharset()).forEach(l -> processOutputBuilder.append(l).append("\n")); - - String processOutput = processOutputBuilder.toString(); - - output.add(outputName, processOutput.substring(0, Math.min(processOutput.length(), outputConfiguration.maxOutputPayloadSize))); - - if(processOutput.length() > outputConfiguration.maxOutputPayloadSize) { - Attachment attachment = AttachmentHelper.generateAttachmentFromByteArray( - processOutput.substring(0, Math.min(processOutput.length(), outputConfiguration.maxOutputAttachmentSize)).getBytes(), outputName + ".log"); - output.addAttachment(attachment); - - if (file.length() > outputConfiguration.maxOutputAttachmentSize) { - output.add("technicalWarning", - outputName + " size exceeded. " + outputName + " has been attached and truncated."); - } - } - } -} diff --git a/keywords/java/step-library-kw-system/src/main/java/ch/exense/step/library/kw/system/ProcessKeywords.java b/keywords/java/step-library-kw-system/src/main/java/ch/exense/step/library/kw/system/ProcessKeywords.java index 78ad66f..c1b4736 100644 --- a/keywords/java/step-library-kw-system/src/main/java/ch/exense/step/library/kw/system/ProcessKeywords.java +++ b/keywords/java/step-library-kw-system/src/main/java/ch/exense/step/library/kw/system/ProcessKeywords.java @@ -20,6 +20,7 @@ import java.util.ArrayList; +import ch.exense.step.library.commons.AbstractProcessKeyword; import step.handlers.javahandler.Keyword; public class ProcessKeywords extends AbstractProcessKeyword { diff --git a/step-library-parent/pom.xml b/step-library-parent/pom.xml index 1e19b56..91f75bd 100644 --- a/step-library-parent/pom.xml +++ b/step-library-parent/pom.xml @@ -76,6 +76,7 @@ + ../keywords/java/step-library-commons ../keywords/java/step-library-kw-http ../keywords/java/step-library-kw-system ../keywords/java/step-library-kw-monitoring-system @@ -83,6 +84,11 @@ + + ch.exense.commons + exense-basic-commons + ${exense-commons.version} + ch.exense.step step-api-keyword