Skip to content

Commit

Permalink
Merge pull request #29 from reportportal/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
HardNorth authored Mar 7, 2024
2 parents 0db3923 + 15c457a commit b17dc0e
Show file tree
Hide file tree
Showing 16 changed files with 619 additions and 40 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Changelog

## [Unreleased]
### Removed
- Shutdown hook register on supplied Launch, by @HardNorth
### Added
- Implemented new feature: display last error log in scenario description, by @vrymar
- Implemented unit tests for the new feature, by @vrymar
### Changed
- Improved dependencies references in build.gradle, by @vrymar

## [5.0.3]
### Fixed
Expand Down
15 changes: 9 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,28 @@ repositories {
}

dependencies {
api 'com.epam.reportportal:client-java:5.2.5'
api "com.epam.reportportal:client-java:${project.client_java_version}"

compileOnly "com.intuit.karate:karate-core:${project.karate_version}"
implementation 'org.slf4j:slf4j-api:2.0.7'
implementation "org.slf4j:slf4j-api:${project.slf4j_api_version}"

testImplementation "com.intuit.karate:karate-core:${project.karate_version}"
testImplementation 'com.epam.reportportal:logger-java-logback:5.2.1'
testImplementation 'com.epam.reportportal:agent-java-test-utils:0.0.3'
testImplementation "com.epam.reportportal:logger-java-logback:${project.logger_java_logback_version}"
testImplementation "com.epam.reportportal:agent-java-test-utils:${project.agent_java_test_utils_version}"
testImplementation "org.junit.jupiter:junit-jupiter-api:${project.junit_version}"
testImplementation "org.junit.jupiter:junit-jupiter-engine:${project.junit_version}"
testImplementation "org.junit.jupiter:junit-jupiter-params:${project.junit_version}"
testImplementation "org.mockito:mockito-core:${project.mockito_version}"
testImplementation "org.mockito:mockito-junit-jupiter:${project.mockito_version}"
testImplementation 'org.hamcrest:hamcrest-core:2.2'
testImplementation 'com.squareup.okhttp3:okhttp:4.12.0'
testImplementation "org.hamcrest:hamcrest-core:${project.hamcrest_core_version}"
testImplementation "com.squareup.okhttp3:okhttp:${project.okhttp_version}"
}

test {
outputs.upToDateWhen { return false }
useJUnitPlatform()
forkEvery(1)
maxParallelForks(1)
doFirst {
def weaver = configurations.testRuntimeClasspath.find { it.name.contains("aspectjweaver") }
jvmArgs += "-javaagent:$weaver"
Expand Down
6 changes: 6 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ gradle_version=8.2
karate_version=1.4.1
junit_version=5.10.1
mockito_version=5.4.0
agent_java_test_utils_version=0.0.3
client_java_version=5.2.5
slf4j_api_version=2.0.7
logger_java_logback_version=5.2.1
hamcrest_core_version=2.2
okhttp_version=4.12.0
scripts_url=https://raw.githubusercontent.com/reportportal/gradle-scripts
scripts_branch=develop
excludeTests=
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,55 @@
import com.intuit.karate.Results;
import com.intuit.karate.Runner;

/**
* Karate runner with ReportPortal integration
*/
public class KarateReportPortalRunner {

/**
* Create a new builder for the Karate runner with ReportPortal integration
*
* @param paths paths to feature files
* @param <T> type of the builder
* @return a new builder
*/
public static <T extends Builder<T>> Builder<T> path(String... paths) {
Builder<T> builder = new Builder<>();
return builder.path(paths);
}

/**
* Builder for the Karate runner with ReportPortal integration
*
* @param <T> type of the builder
*/
public static class Builder<T extends Builder<T>> extends Runner.Builder<T> {
private ReportPortal rp;

/**
* Create a new builder
*/
public Builder() {
super();
}

/**
* Set the ReportPortal instance to use
*
* @param reportPortal the ReportPortal instance
* @return the builder
*/
public Builder<T> withReportPortal(ReportPortal reportPortal) {
rp = reportPortal;
return this;
}

/**
* Run the tests in parallel
*
* @param threadCount number of threads to use
* @return the results of the tests
*/
@Override
public Results parallel(int threadCount) {
if (rp == null) {
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/com/epam/reportportal/karate/ReportPortalHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public class ReportPortalHook implements RuntimeHook {
private final Map<Maybe<String>, Date> stepStartTimeMap = new ConcurrentHashMap<>();
private volatile Thread shutDownHook;

/**
* Create a new instance of the ReportPortalHook with the specified ReportPortal instance.
*
* @param reportPortal the ReportPortal instance
*/
public ReportPortalHook(ReportPortal reportPortal) {
launch = new MemoizingSupplier<>(() -> {
ListenerParameters params = reportPortal.getParameters();
Expand All @@ -77,13 +82,17 @@ public ReportPortalHook(ReportPortal reportPortal) {
});
}

/**
* Default constructor. Create a new instance of the ReportPortalHook with default ReportPortal instance.
*/
@SuppressWarnings("unused")
public ReportPortalHook() {
this(ReportPortal.builder().build());
}

@SuppressWarnings("unused")
public ReportPortalHook(Supplier<Launch> launchSupplier) {
launch = new MemoizingSupplier<>(launchSupplier);
shutDownHook = registerShutdownHook(this::finishLaunch);
}

/**
Expand Down Expand Up @@ -120,7 +129,7 @@ public void finishLaunch() {
System.getProperty("rp.launch.id")
);
launchObject.finish(rq);
if (Thread.currentThread() != shutDownHook) {
if (shutDownHook != null && Thread.currentThread() != shutDownHook) {
unregisterShutdownHook(shutDownHook);
}
}
Expand Down Expand Up @@ -187,7 +196,7 @@ public void afterFeature(FeatureRuntime fr) {
*/
@Nonnull
protected StartTestItemRQ buildStartScenarioRq(@Nonnull ScenarioRuntime sr) {
return ReportPortalUtils.buildStartScenarioRq(sr.scenario);
return ReportPortalUtils.buildStartScenarioRq(sr.result);
}

@Override
Expand All @@ -207,9 +216,7 @@ public boolean beforeScenario(ScenarioRuntime sr) {
*/
@Nonnull
protected FinishTestItemRQ buildFinishScenarioRq(@Nonnull ScenarioRuntime sr) {
return buildFinishTestItemRq(Calendar.getInstance().getTime(),
sr.result.getFailureMessageForDisplay() == null ? ItemStatus.PASSED : ItemStatus.FAILED
);
return ReportPortalUtils.buildFinishScenarioRq(sr.result);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public ReportPortalPublisher(ReportPortal reportPortal) {

public ReportPortalPublisher(Supplier<Launch> launchSupplier) {
launch = new MemoizingSupplier<>(launchSupplier);
shutDownHook = registerShutdownHook(this::finishLaunch);
}

/**
Expand Down Expand Up @@ -114,7 +113,7 @@ public void finishLaunch() {
System.getProperty("rp.launch.id")
);
launchObject.finish(rq);
if (Thread.currentThread() != shutDownHook) {
if (shutDownHook != null && Thread.currentThread() != shutDownHook) {
unregisterShutdownHook(shutDownHook);
}
}
Expand Down Expand Up @@ -189,7 +188,7 @@ public void finishFeature(FeatureResult featureResult) {
*/
@Nonnull
protected StartTestItemRQ buildStartScenarioRq(@Nonnull ScenarioResult scenarioResult) {
return ReportPortalUtils.buildStartScenarioRq(scenarioResult.getScenario());
return ReportPortalUtils.buildStartScenarioRq(scenarioResult);
}

/**
Expand All @@ -213,10 +212,7 @@ public void startScenario(ScenarioResult scenarioResult, FeatureResult featureRe
*/
@Nonnull
protected FinishTestItemRQ buildFinishScenarioRq(@Nonnull ScenarioResult scenarioResult) {
return buildFinishTestItemRq(Calendar.getInstance().getTime(),
scenarioResult.getFailureMessageForDisplay() == null ? ItemStatus.PASSED : ItemStatus.FAILED
);

return ReportPortalUtils.buildFinishScenarioRq(scenarioResult);
}

/**
Expand Down
63 changes: 43 additions & 20 deletions src/main/java/com/epam/reportportal/karate/ReportPortalUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public class ReportPortalUtils {
public static final String SKIPPED_ISSUE_KEY = "skippedIssue";
public static final String SCENARIO_CODE_REFERENCE_PATTERN = "%s/[SCENARIO:%s]";
public static final String EXAMPLE_CODE_REFERENCE_PATTERN = "%s/[EXAMPLE:%s%s]";
public static final String MARKDOWN_DELIMITER_PATTERN = "%s\n\n---\n\n%s";
public static final String MARKDOWN_DELIMITER = "\n\n---\n\n";
public static final String MARKDOWN_DELIMITER_PATTERN = "%s" + MARKDOWN_DELIMITER + "%s";
private static final Logger LOGGER = LoggerFactory.getLogger(ReportPortalUtils.class);
private static final String PARAMETER_ITEMS_START = "[";
private static final String PARAMETER_ITEMS_END = "]";
Expand Down Expand Up @@ -294,35 +295,57 @@ public static TestCaseIdEntry getTestCaseId(@Nonnull Scenario scenario) {
/**
* Build ReportPortal request for start Scenario event
*
* @param scenario Karate's Scenario object instance
* @param result Karate's ScenarioResult object instance
* @return request to ReportPortal
*/
@Nonnull
public static StartTestItemRQ buildStartScenarioRq(@Nonnull Scenario scenario) {
public static StartTestItemRQ buildStartScenarioRq(@Nonnull ScenarioResult result) {
Scenario scenario = result.getScenario();
StartTestItemRQ rq = buildStartTestItemRq(scenario.getName(), Calendar.getInstance().getTime(), ItemType.STEP);
rq.setCodeRef(getCodeRef(scenario));
rq.setTestCaseId(ofNullable(getTestCaseId(scenario)).map(TestCaseIdEntry::getId).orElse(null));
rq.setAttributes(toAttributes(scenario.getTags()));
List<ParameterResource> parameters = getParameters(scenario);
boolean hasParameters = ofNullable(parameters).filter(p -> !p.isEmpty()).isPresent();
if (hasParameters) {
rq.setParameters(parameters);
rq.setParameters(getParameters(scenario));
rq.setDescription(buildDescription(scenario, result.getErrorMessage(), getParameters(scenario)));
return rq;
}

/**
* Build ReportPortal request for finish Scenario event
*
* @param result Karate's ScenarioResult object instance
* @return request to ReportPortal
*/
@Nonnull
public static FinishTestItemRQ buildFinishScenarioRq(@Nonnull ScenarioResult result) {
Scenario scenario = result.getScenario();
FinishTestItemRQ rq = buildFinishTestItemRq(Calendar.getInstance().getTime(), result.getFailureMessageForDisplay() == null ? ItemStatus.PASSED : ItemStatus.FAILED);
rq.setDescription(buildDescription(scenario, result.getErrorMessage(), getParameters(scenario)));
return rq;
}

@Nonnull
private static String buildDescription(@Nonnull Scenario scenario, @Nullable String errorMessage, @Nullable List<ParameterResource> parameters) {
StringBuilder descriptionBuilder = new StringBuilder();

if (parameters != null && !parameters.isEmpty()) {
descriptionBuilder.append(String.format(PARAMETERS_PATTERN, ParameterUtils.formatParametersAsTable(parameters)));
}
if (isNotBlank(scenario.getDescription())) {
appendWithDelimiter(descriptionBuilder, scenario.getDescription());
}
if (isNotBlank(errorMessage)) {
appendWithDelimiter(descriptionBuilder, String.format("Error:\n%s", errorMessage));
}

String description = scenario.getDescription();
if (isNotBlank(description)) {
if (hasParameters) {
rq.setDescription(String.format(MARKDOWN_DELIMITER_PATTERN,
String.format(PARAMETERS_PATTERN, ParameterUtils.formatParametersAsTable(parameters)),
description
));
} else {
rq.setDescription(description);
}
} else if (hasParameters) {
rq.setDescription(String.format(PARAMETERS_PATTERN, ParameterUtils.formatParametersAsTable(parameters)));
return descriptionBuilder.toString();
}

private static void appendWithDelimiter(StringBuilder builder, String text) {
if (builder.length() > 0) {
builder.append(MARKDOWN_DELIMITER);
}
return rq;
builder.append(text);
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/main/resources/META-INF/aop-ajc.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!--
~ Copyright 2021 EPAM Systems
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<aspectj>
<weaver options="-nowarn -Xset:weaveJavaPackages=false,weaveJavaxPackages=false" />
<aspects>
<aspect name="com.epam.reportportal.aspect.StepAspect"/>
</aspects>
</aspectj>
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void test_description_for_all_possible_items(boolean report) {
assertThat(featureStart.getDescription(), endsWith("feature/simple.feature"));

StartTestItemRQ scenarioStart = scenarioCaptor.getValue();
assertThat(scenarioStart.getDescription(), nullValue());
assertThat(scenarioStart.getDescription(), emptyString());

stepCaptor.getAllValues().forEach(step -> assertThat(step.getDescription(), nullValue()));
}
Expand Down
Loading

0 comments on commit b17dc0e

Please sign in to comment.