Skip to content

Commit

Permalink
SED-526: Refactoring libraries to remove dependencies to other libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromebrongniart committed Oct 14, 2020
1 parent e69c5d3 commit 30c7205
Show file tree
Hide file tree
Showing 14 changed files with 245 additions and 198 deletions.
34 changes: 34 additions & 0 deletions keywords/java/step-library-commons/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!--
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 <http://www.gnu.org/licenses/>.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>step-library-commons</artifactId>

<parent>
<groupId>ch.exense.step.library</groupId>
<artifactId>step-library-parent</artifactId>
<version>0.0.0-SNAPSHOT</version>
<relativePath>../../../step-library-parent</relativePath>
</parent>

<dependencies>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
******************************************************************************/
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<String> cmd, int timeoutMs, OutputConfiguration outputConfiguration, Consumer<ManagedProcess> postProcess) throws Exception {
ManagedProcess process = new ManagedProcess(cmd);
executeManagedCommand(timeoutMs, outputConfiguration, postProcess, process);
}

protected void executeManagedCommand(String cmd, int timeoutMs, OutputConfiguration outputConfiguration, Consumer<ManagedProcess> postProcess) throws Exception {
ManagedProcess process = new ManagedProcess(cmd);
executeManagedCommand(timeoutMs, outputConfiguration, postProcess, process);
}

protected void executeManagedCommand(int timeoutMs, OutputConfiguration outputConfiguration,
Consumer<ManagedProcess> 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.");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
5 changes: 5 additions & 0 deletions keywords/java/step-library-kw-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
</parent>

<dependencies>
<dependency>
<groupId>ch.exense.step.library</groupId>
<artifactId>step-library-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions keywords/java/step-library-kw-monitoring-system/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
</parent>

<dependencies>
<dependency>
<groupId>ch.exense.step.library</groupId>
<artifactId>step-library-kw-system</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>ch.exense.step</groupId>
<artifactId>step-functions-base-functions</artifactId>
<artifactId>step-core</artifactId>
<version>${step.version}</version>
</dependency>
<dependency>
<groupId>ch.exense.step.library</groupId>
<artifactId>step-library-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-jsr223</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 4 additions & 4 deletions keywords/java/step-library-kw-system/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@

<dependencies>
<dependency>
<groupId>ch.exense.commons</groupId>
<artifactId>exense-basic-commons</artifactId>
<version>${exense-commons.version}</version>
<groupId>ch.exense.step.library</groupId>
<artifactId>step-library-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
Expand Down
Loading

0 comments on commit 30c7205

Please sign in to comment.