Skip to content

Commit 115d5c7

Browse files
committed
tech: add job parameter that will parse test run's test name to test name part and test parameters part
1 parent a4fc6a0 commit 115d5c7

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

src/main/java/com/microfocus/application/automation/tools/octane/tests/junit/JUnitExtension.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import java.nio.file.Path;
6666
import java.nio.file.Paths;
6767
import java.util.*;
68+
import java.util.regex.Pattern;
6869

6970
/**
7071
* Converter of Jenkins test report to ALM Octane test report format(junitResult.xml->mqmTests.xml)
@@ -75,6 +76,7 @@ public class JUnitExtension extends OctaneTestsExtension {
7576

7677
private static final String JUNIT_RESULT_XML = "junitResult.xml"; // NON-NLS
7778
public static final String TEMP_TEST_RESULTS_FILE_NAME_PREFIX = "GetJUnitTestResults";
79+
private static final String TEST_RESULT_NAME_REGEX_PATTERN_PARAMETER_NAME = "octane_test_result_name_run_regex_pattern";
7880

7981
@Inject
8082
private ResultFieldsDetectionService resultFieldsDetectionService;
@@ -164,6 +166,7 @@ private static class GetJUnitTestResults implements FilePath.FileCallable<FilePa
164166
private FilePath workspace;
165167
private boolean stripPackageAndClass;
166168
private String sharedCheckOutDirectory;
169+
private Pattern testParserRegEx;
167170

168171
//this class is run on master and JUnitXmlIterator is runnning on slave.
169172
//this object pass some master2slave data
@@ -220,6 +223,19 @@ public GetJUnitTestResults(Run<?, ?> build, HPRunnerType hpRunnerType, List<File
220223
logger.error("Failed to add log file for StormRunnerLoad :" + e.getMessage());
221224
}
222225
}
226+
if(build.getAction(ParametersAction.class) != null && build.getAction(ParametersAction.class).getParameter(TEST_RESULT_NAME_REGEX_PATTERN_PARAMETER_NAME) != null &&
227+
build.getAction(ParametersAction.class).getParameter(TEST_RESULT_NAME_REGEX_PATTERN_PARAMETER_NAME).getValue() != null) {
228+
try {
229+
this.testParserRegEx = Pattern.compile(Objects.requireNonNull(build.getAction(ParametersAction.class).getParameter(TEST_RESULT_NAME_REGEX_PATTERN_PARAMETER_NAME).getValue()).toString());
230+
} catch (IllegalArgumentException e){
231+
logger.error("Failed to parse regular expression pattern for test result name extractor.Job name: {}, Build {}, Input: {}, Error massage: {}.",
232+
this.jobName,
233+
this.buildId,
234+
Objects.requireNonNull(build.getAction(ParametersAction.class).getParameter(TEST_RESULT_NAME_REGEX_PATTERN_PARAMETER_NAME).getValue()).toString() +"\n",
235+
e.getMessage());
236+
}
237+
}
238+
223239
}
224240

225241
@Override
@@ -230,7 +246,7 @@ public FilePath invoke(File f, VirtualChannel channel) throws IOException, Inter
230246

231247
try {
232248
for (FilePath report : reports) {
233-
JUnitXmlIterator iterator = new JUnitXmlIterator(report.read(), moduleDetection, workspace, sharedCheckOutDirectory, jobName, buildId, buildStarted, stripPackageAndClass, hpRunnerType, jenkinsRootUrl, additionalContext);
249+
JUnitXmlIterator iterator = new JUnitXmlIterator(report.read(), moduleDetection, workspace, sharedCheckOutDirectory, jobName, buildId, buildStarted, stripPackageAndClass, hpRunnerType, jenkinsRootUrl, additionalContext,testParserRegEx);
234250
while (iterator.hasNext()) {
235251
oos.writeObject(iterator.next());
236252
}

src/main/java/com/microfocus/application/automation/tools/octane/tests/junit/JUnitTestResult.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ final public class JUnitTestResult implements Serializable, XmlWritableTestResul
5555
private final TestError testError;
5656
private final String externalReportUrl;
5757
private final HPRunnerType runnerType;
58+
private final String externalRunId;
5859

59-
60-
public JUnitTestResult(String moduleName, String packageName, String className, String testName, TestResultStatus result, long duration, long started, TestError testError, String externalReportUrl, String description, HPRunnerType runnerType) {
60+
public JUnitTestResult(String moduleName, String packageName, String className, String testName, TestResultStatus result, long duration, long started, TestError testError, String externalReportUrl, String description, HPRunnerType runnerType,String externalRunId) {
6161
this.moduleName = restrictSize(moduleName, DEFAULT_STRING_SIZE);
6262
this.packageName = restrictSize(packageName, DEFAULT_STRING_SIZE);
6363
this.className = restrictSize(className, DEFAULT_STRING_SIZE);
@@ -73,6 +73,7 @@ public JUnitTestResult(String moduleName, String packageName, String className,
7373
this.externalReportUrl = externalReportUrl;
7474
this.description = description;
7575
this.runnerType = runnerType;
76+
this.externalRunId = externalRunId;
7677
}
7778

7879
private String restrictSize(String value, int size) {
@@ -130,6 +131,9 @@ public void writeXmlElement(XMLStreamWriter writer) throws XMLStreamException {
130131
if(externalReportUrl != null && !externalReportUrl.isEmpty()) {
131132
writer.writeAttribute("external_report_url", externalReportUrl);
132133
}
134+
if(externalRunId != null && !externalRunId.isEmpty()){
135+
writer.writeAttribute("external_run_id", externalRunId);
136+
}
133137
if (HPRunnerType.UFT_MBT.equals(runnerType)) {
134138
writer.writeAttribute("run_type", MFToolsDetectionExtension.UFT_MBT);
135139
}

src/main/java/com/microfocus/application/automation/tools/octane/tests/junit/JUnitXmlIterator.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
import java.util.Collection;
5656
import java.util.List;
5757
import java.util.Optional;
58+
import java.util.regex.Matcher;
59+
import java.util.regex.Pattern;
5860

5961
/**
6062
* JUnit result parser and enricher according to HPRunnerType
@@ -87,8 +89,10 @@ public class JUnitXmlIterator extends AbstractXmlIterator<JUnitTestResult> {
8789
private Object additionalContext;
8890
private String filePath;
8991
public static final String SRL_REPORT_URL = "reportUrl";
92+
private Pattern testParserRegEx;
93+
private String externalRunId;
9094

91-
public JUnitXmlIterator(InputStream read, List<ModuleDetection> moduleDetection, FilePath workspace, String sharedCheckOutDirectory, String jobName, String buildId, long buildStarted, boolean stripPackageAndClass, HPRunnerType hpRunnerType, String jenkinsRootUrl, Object additionalContext) throws XMLStreamException {
95+
public JUnitXmlIterator(InputStream read, List<ModuleDetection> moduleDetection, FilePath workspace, String sharedCheckOutDirectory, String jobName, String buildId, long buildStarted, boolean stripPackageAndClass, HPRunnerType hpRunnerType, String jenkinsRootUrl, Object additionalContext,Pattern testParserRegEx) throws XMLStreamException {
9296
super(read);
9397
this.stripPackageAndClass = stripPackageAndClass;
9498
this.moduleDetection = moduleDetection;
@@ -100,6 +104,7 @@ public JUnitXmlIterator(InputStream read, List<ModuleDetection> moduleDetection,
100104
this.hpRunnerType = hpRunnerType;
101105
this.jenkinsRootUrl = jenkinsRootUrl;
102106
this.additionalContext = additionalContext;
107+
this.testParserRegEx = testParserRegEx;
103108
}
104109

105110
private static long parseTime(String timeString) {
@@ -281,16 +286,28 @@ protected void onEvent(XMLEvent event) throws XMLStreamException, IOException, I
281286

282287
if ("case".equals(localName)) { // NON-NLS
283288
TestError testError = new TestError(stackTraceStr, errorType, errorMsg);
289+
290+
if(this.testParserRegEx != null){
291+
splitTestNameByPattern();
292+
}
284293
if (stripPackageAndClass) {
285294
//workaround only for UFT - we do not want packageName="All-Tests" and className="&lt;None>" as it comes from JUnit report
286-
addItem(new JUnitTestResult(moduleName, "", "", testName, status, duration, buildStarted, testError, externalURL, description, hpRunnerType));
295+
addItem(new JUnitTestResult(moduleName, "", "", testName, status, duration, buildStarted, testError, externalURL, description, hpRunnerType,this.externalRunId));
287296
} else {
288-
addItem(new JUnitTestResult(moduleName, packageName, className, testName, status, duration, buildStarted, testError, externalURL, description, hpRunnerType));
297+
addItem(new JUnitTestResult(moduleName, packageName, className, testName, status, duration, buildStarted, testError, externalURL, description, hpRunnerType,this.externalRunId));
289298
}
290299
}
291300
}
292301
}
293302

303+
private void splitTestNameByPattern(){
304+
Matcher matcher = testParserRegEx.matcher(this.testName);
305+
if (matcher.find()) {
306+
this.externalRunId = this.testName.substring(matcher.start());
307+
this.testName = this.testName.substring(0, matcher.start());
308+
}
309+
}
310+
294311

295312
private void parseUftErrorMessages() {
296313
try {

src/test/java/com/microfocus/application/automation/tools/octane/tests/TestResultIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public boolean hasNext() {
7878
long duration = Long.valueOf(element.getAttributeByName(new QName("duration")).getValue());
7979
TestResultStatus status = TestResultStatus.fromPrettyName(element.getAttributeByName(new QName("status")).getValue());
8080
long started = Long.valueOf(element.getAttributeByName(new QName("started")).getValue());
81-
items.add(new JUnitTestResult(moduleName, packageName, className, testName, status, duration, started, null, null, null,null));
81+
items.add(new JUnitTestResult(moduleName, packageName, className, testName, status, duration, started, null, null, null,null,null));
8282
} else if ("build".equals(localName)) {
8383
attribute = element.getAttributeByName(new QName("server_id"));
8484
if (attribute != null) {

src/test/java/com/microfocus/application/automation/tools/octane/tests/xml/TestResultXmlWriterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class TestResultXmlWriterTest extends OctanePluginTestBase {
6969
@BeforeClass
7070
public static void initialize() {
7171
List<XmlWritableTestResult> testResults = new ArrayList<>();
72-
testResults.add(new JUnitTestResult("module", "package", "class", "testName", TestResultStatus.PASSED, 1, 2, null, null, null, null));
72+
testResults.add(new JUnitTestResult("module", "package", "class", "testName", TestResultStatus.PASSED, 1, 2, null, null, null, null,null));
7373
container = new TestResultContainer(testResults.iterator(), new ResultFields());
7474
OctaneServerMock serverMock = OctaneServerMock.getInstance();
7575
OctaneServerSettingsModel model = new OctaneServerSettingsModel(

0 commit comments

Comments
 (0)