Skip to content

Commit

Permalink
Code format apply
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Jan 16, 2024
1 parent 17dc10a commit 1dbae8c
Show file tree
Hide file tree
Showing 39 changed files with 1,577 additions and 1,642 deletions.
84 changes: 38 additions & 46 deletions src/main/java/com/epam/reportportal/karate/ReportPortalHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,34 @@
*/
public class ReportPortalHook implements RuntimeHook {
private static final Logger LOGGER = LoggerFactory.getLogger(ReportPortalHook.class);

protected final MemoizingSupplier<Launch> launch;
private final Map<String, Maybe<String>> featureIdMap = new ConcurrentHashMap<>();
private final Map<String, Maybe<String>> scenarioIdMap = new ConcurrentHashMap<>();
private final Map<String, Maybe<String>> backgroundIdMap = new ConcurrentHashMap<>();
private final Map<String, Maybe<String>> stepIdMap = new ConcurrentHashMap<>();
private final Map<Maybe<String>, Date> stepStartTimeMap = new ConcurrentHashMap<>();
private volatile Thread shutDownHook;

protected final MemoizingSupplier<Launch> launch;
public ReportPortalHook(ReportPortal reportPortal) {
launch = new MemoizingSupplier<>(() -> {
ListenerParameters params = reportPortal.getParameters();
StartLaunchRQ rq = buildStartLaunchRq(params);
Launch newLaunch = reportPortal.newLaunch(rq);
//noinspection ReactiveStreamsUnusedPublisher
newLaunch.start();
shutDownHook = registerShutdownHook(this::finishLaunch);
return newLaunch;
});
}

private volatile Thread shutDownHook;
public ReportPortalHook() {
this(ReportPortal.builder().build());
}

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

/**
* Customize start launch event/request
Expand Down Expand Up @@ -93,35 +111,17 @@ public void finishLaunch() {
Launch launchObject = launch.get();
ListenerParameters parameters = launchObject.getParameters();
FinishExecutionRQ rq = buildFinishLaunchRq(parameters);
LOGGER.info("Launch URL: {}/ui/#{}/launches/all/{}", parameters.getBaseUrl(), parameters.getProjectName(),
System.getProperty("rp.launch.id"));
LOGGER.info("Launch URL: {}/ui/#{}/launches/all/{}",
parameters.getBaseUrl(),
parameters.getProjectName(),
System.getProperty("rp.launch.id")
);
launchObject.finish(rq);
if (Thread.currentThread() != shutDownHook) {
unregisterShutdownHook(shutDownHook);
}
}

public ReportPortalHook(ReportPortal reportPortal) {
launch = new MemoizingSupplier<>(() -> {
ListenerParameters params = reportPortal.getParameters();
StartLaunchRQ rq = buildStartLaunchRq(params);
Launch newLaunch = reportPortal.newLaunch(rq);
//noinspection ReactiveStreamsUnusedPublisher
newLaunch.start();
shutDownHook = registerShutdownHook(this::finishLaunch);
return newLaunch;
});
}

public ReportPortalHook() {
this(ReportPortal.builder().build());
}

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

/**
* Build ReportPortal request for start Feature event.
*
Expand All @@ -132,8 +132,10 @@ public ReportPortalHook(Supplier<Launch> launchSupplier) {
@SuppressWarnings("unchecked")
protected StartTestItemRQ buildStartFeatureRq(@Nonnull FeatureRuntime fr) {
StartTestItemRQ rq = ReportPortalUtils.buildStartFeatureRq(fr.featureCall.feature);
ofNullable(fr.caller).map(c -> c.arg).map(a -> (Map<String, Object>) a.getValue())
.filter(args -> !args.isEmpty()).ifPresent(args -> {
ofNullable(fr.caller).map(c -> c.arg)
.map(a -> (Map<String, Object>) a.getValue())
.filter(args -> !args.isEmpty())
.ifPresent(args -> {
// TODO: cover with tests
String parameters = String.format(PARAMETERS_PATTERN, formatParametersAsTable(getParameters(args)));
String description = rq.getDescription();
Expand Down Expand Up @@ -162,8 +164,7 @@ public boolean beforeFeature(FeatureRuntime fr) {
*/
@Nonnull
protected FinishTestItemRQ buildFinishFeatureRq(@Nonnull FeatureRuntime fr) {
return buildFinishTestItemRq(Calendar.getInstance().getTime(),
fr.result.isFailed() ? ItemStatus.FAILED : ItemStatus.PASSED);
return buildFinishTestItemRq(Calendar.getInstance().getTime(), fr.result.isFailed() ? ItemStatus.FAILED : ItemStatus.PASSED);
}

@Override
Expand Down Expand Up @@ -208,7 +209,8 @@ 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);
sr.result.getFailureMessageForDisplay() == null ? ItemStatus.PASSED : ItemStatus.FAILED
);
}

/**
Expand All @@ -233,8 +235,7 @@ protected StartTestItemRQ buildStartBackgroundRq(@Nonnull Step step, @Nonnull Sc
public Maybe<String> startBackground(@Nonnull Step step, @Nonnull ScenarioRuntime sr) {
return backgroundIdMap.computeIfAbsent(sr.scenario.getUniqueId(), k -> {
StartTestItemRQ backgroundRq = buildStartBackgroundRq(step, sr);
return launch.get().startTestItem(scenarioIdMap.get(sr.scenario.getUniqueId()),
backgroundRq);
return launch.get().startTestItem(scenarioIdMap.get(sr.scenario.getUniqueId()), backgroundRq);
});
}

Expand Down Expand Up @@ -339,21 +340,12 @@ public boolean beforeStep(Step step, ScenarioRuntime sr) {
StartTestItemRQ stepRq = buildStartStepRq(step, sr);

String scenarioId = sr.scenario.getUniqueId();
Maybe<String> stepId = launch.get()
.startTestItem(
background ? backgroundId : scenarioIdMap.get(scenarioId),
stepRq
);
Maybe<String> stepId = launch.get().startTestItem(background ? backgroundId : scenarioIdMap.get(scenarioId), stepRq);
stepStartTimeMap.put(stepId, stepRq.getStartTime());
stepIdMap.put(scenarioId, stepId);
ofNullable(stepRq.getParameters())
.filter(params -> !params.isEmpty())
.ifPresent(params ->
sendLog(stepId, String.format(PARAMETERS_PATTERN, formatParametersAsTable(params)),
LogLevel.INFO));
ofNullable(step.getTable())
.ifPresent(table ->
sendLog(stepId, "Table:\n\n" + formatDataTable(table.getRows()), LogLevel.INFO));
ofNullable(stepRq.getParameters()).filter(params -> !params.isEmpty())
.ifPresent(params -> sendLog(stepId, String.format(PARAMETERS_PATTERN, formatParametersAsTable(params)), LogLevel.INFO));
ofNullable(step.getTable()).ifPresent(table -> sendLog(stepId, "Table:\n\n" + formatDataTable(table.getRows()), LogLevel.INFO));
String docString = step.getDocString();
if (isNotBlank(docString)) {
sendLog(stepId, "Docstring:\n\n" + asMarkdownCode(step.getDocString()), LogLevel.INFO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,14 @@
*/
public class ReportPortalPublisher {
private static final Logger LOGGER = LoggerFactory.getLogger(ReportPortalPublisher.class);
protected final MemoizingSupplier<Launch> launch;
private final Map<String, Maybe<String>> featureIdMap = new HashMap<>();
private final Map<String, Maybe<String>> scenarioIdMap = new HashMap<>();
private final Map<Maybe<String>, Long> stepStartTimeMap = new HashMap<>();
private Maybe<String> backgroundId;
private Maybe<String> stepId;

protected final MemoizingSupplier<Launch> launch;

private Thread shutDownHook;

/**
* Customize start launch event/request
*
* @param parameters Launch configuration parameters
* @return request to ReportPortal
*/
protected StartLaunchRQ buildStartLaunchRq(ListenerParameters parameters) {
return ReportPortalUtils.buildStartLaunchRq(parameters);
}

public ReportPortalPublisher(ReportPortal reportPortal) {
launch = new MemoizingSupplier<>(() -> {
ListenerParameters params = reportPortal.getParameters();
Expand All @@ -82,6 +70,16 @@ public ReportPortalPublisher(Supplier<Launch> launchSupplier) {
shutDownHook = registerShutdownHook(this::finishLaunch);
}

/**
* Customize start launch event/request
*
* @param parameters Launch configuration parameters
* @return request to ReportPortal
*/
protected StartLaunchRQ buildStartLaunchRq(ListenerParameters parameters) {
return ReportPortalUtils.buildStartLaunchRq(parameters);
}

/**
* Start sending Launch data to ReportPortal.
*/
Expand All @@ -108,8 +106,11 @@ public void finishLaunch() {
Launch launchObject = launch.get();
ListenerParameters parameters = launchObject.getParameters();
FinishExecutionRQ rq = buildFinishLaunchRq(parameters);
LOGGER.info("Launch URL: {}/ui/#{}/launches/all/{}", parameters.getBaseUrl(), parameters.getProjectName(),
System.getProperty("rp.launch.id"));
LOGGER.info("Launch URL: {}/ui/#{}/launches/all/{}",
parameters.getBaseUrl(),
parameters.getProjectName(),
System.getProperty("rp.launch.id")
);
launchObject.finish(rq);
if (Thread.currentThread() != shutDownHook) {
unregisterShutdownHook(shutDownHook);
Expand Down Expand Up @@ -146,8 +147,7 @@ public void startFeature(@Nonnull FeatureResult featureResult) {
*/
@Nonnull
protected FinishTestItemRQ buildFinishFeatureRq(@Nonnull FeatureResult featureResult) {
return buildFinishTestItemRq(Calendar.getInstance().getTime(),
featureResult.isFailed() ? ItemStatus.FAILED : ItemStatus.PASSED);
return buildFinishTestItemRq(Calendar.getInstance().getTime(), featureResult.isFailed() ? ItemStatus.FAILED : ItemStatus.PASSED);
}

/**
Expand Down Expand Up @@ -212,7 +212,8 @@ 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);
scenarioResult.getFailureMessageForDisplay() == null ? ItemStatus.PASSED : ItemStatus.FAILED
);

}

Expand Down Expand Up @@ -255,8 +256,7 @@ protected StartTestItemRQ buildStartBackgroundRq(@Nonnull StepResult stepResult,
public void startBackground(@Nonnull StepResult stepResult, @Nonnull ScenarioResult scenarioResult) {
backgroundId = ofNullable(backgroundId).orElseGet(() -> {
StartTestItemRQ backgroundRq = buildStartBackgroundRq(stepResult, scenarioResult);
return launch.get().startTestItem(scenarioIdMap.get(scenarioResult.getScenario().getName()),
backgroundRq);
return launch.get().startTestItem(scenarioIdMap.get(scenarioResult.getScenario().getName()), backgroundRq);
});
}

Expand Down Expand Up @@ -339,19 +339,11 @@ public void startStep(StepResult stepResult, ScenarioResult scenarioResult) {
}
StartTestItemRQ stepRq = buildStartStepRq(stepResult, scenarioResult);
stepId = launch.get()
.startTestItem(
backgroundId != null ? backgroundId : scenarioIdMap.get(scenarioResult.getScenario().getName()),
stepRq
);
.startTestItem(backgroundId != null ? backgroundId : scenarioIdMap.get(scenarioResult.getScenario().getName()), stepRq);
stepStartTimeMap.put(stepId, stepRq.getStartTime().getTime());
ofNullable(stepRq.getParameters())
.filter(params -> !params.isEmpty())
.ifPresent(params ->
sendLog(stepId, String.format(PARAMETERS_PATTERN, formatParametersAsTable(params)),
LogLevel.INFO));
ofNullable(step.getTable())
.ifPresent(table ->
sendLog(stepId, "Table:\n\n" + formatDataTable(table.getRows()), LogLevel.INFO));
ofNullable(stepRq.getParameters()).filter(params -> !params.isEmpty())
.ifPresent(params -> sendLog(stepId, String.format(PARAMETERS_PATTERN, formatParametersAsTable(params)), LogLevel.INFO));
ofNullable(step.getTable()).ifPresent(table -> sendLog(stepId, "Table:\n\n" + formatDataTable(table.getRows()), LogLevel.INFO));
String docString = step.getDocString();
if (isNotBlank(docString)) {
sendLog(stepId, "Docstring:\n\n" + asMarkdownCode(step.getDocString()), LogLevel.INFO);
Expand Down
56 changes: 25 additions & 31 deletions src/main/java/com/epam/reportportal/karate/ReportPortalUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,19 @@
* Set of useful utils related to Karate -&gt; ReportPortal integration
*/
public class ReportPortalUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(ReportPortalUtils.class);

public static final String MARKDOWN_CODE_PATTERN = "```\n%s\n```";
public static final String PARAMETERS_PATTERN = "Parameters:\n\n%s";
private static final String PARAMETER_ITEMS_START = "[";
private static final String PARAMETER_ITEMS_END = "]";
private static final String PARAMETER_ITEMS_DELIMITER = ";";
private static final String KEY_VALUE_SEPARATOR = ":";
public static final String VARIABLE_PATTERN =
"(?:(?<=#\\()%1$s(?=\\)))|(?:(?<=[\\s=+-/*<>(]|^)%1$s(?=[\\s=+-/*<>)]|(?:\\r?\\n)|$))";

public static final String VARIABLE_PATTERN = "(?:(?<=#\\()%1$s(?=\\)))|(?:(?<=[\\s=+-/*<>(]|^)%1$s(?=[\\s=+-/*<>)]|(?:\\r?\\n)|$))";
public static final String AGENT_PROPERTIES_FILE = "agent.properties";
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";
private static final Logger LOGGER = LoggerFactory.getLogger(ReportPortalUtils.class);
private static final String PARAMETER_ITEMS_START = "[";
private static final String PARAMETER_ITEMS_END = "]";
private static final String PARAMETER_ITEMS_DELIMITER = ";";
private static final String KEY_VALUE_SEPARATOR = ":";

private ReportPortalUtils() {
throw new RuntimeException("No instances should exist for the class!");
Expand Down Expand Up @@ -152,8 +147,7 @@ public static StartLaunchRQ buildStartLaunchRq(@Nonnull ListenerParameters param
skippedIssueAttribute.setSystem(true);
rq.getAttributes().add(skippedIssueAttribute);
}
rq.getAttributes().addAll(SystemAttributesExtractor.extract(AGENT_PROPERTIES_FILE,
ReportPortalUtils.class.getClassLoader()));
rq.getAttributes().addAll(SystemAttributesExtractor.extract(AGENT_PROPERTIES_FILE, ReportPortalUtils.class.getClassLoader()));
return rq;
}

Expand All @@ -180,11 +174,16 @@ public static FinishExecutionRQ buildFinishLaunchRq(@Nonnull ListenerParameters
@Nonnull
public static String getCodeRef(@Nonnull Scenario scenario) {
if (scenario.isOutlineExample()) {
return String.format(EXAMPLE_CODE_REFERENCE_PATTERN, scenario.getFeature().getResource().getRelativePath(),
scenario.getName(), ReportPortalUtils.formatExampleKey(scenario.getExampleData()));
return String.format(EXAMPLE_CODE_REFERENCE_PATTERN,
scenario.getFeature().getResource().getRelativePath(),
scenario.getName(),
ReportPortalUtils.formatExampleKey(scenario.getExampleData())
);
} else {
return String.format(SCENARIO_CODE_REFERENCE_PATTERN, scenario.getFeature().getResource().getRelativePath(),
scenario.getName());
return String.format(SCENARIO_CODE_REFERENCE_PATTERN,
scenario.getFeature().getResource().getRelativePath(),
scenario.getName()
);
}
}

Expand All @@ -197,8 +196,7 @@ public static String getCodeRef(@Nonnull Scenario scenario) {
* @return request to ReportPortal
*/
@Nonnull
public static StartTestItemRQ buildStartTestItemRq(@Nonnull String name, @Nonnull Date startTime,
@Nonnull ItemType type) {
public static StartTestItemRQ buildStartTestItemRq(@Nonnull String name, @Nonnull Date startTime, @Nonnull ItemType type) {
StartTestItemRQ rq = new StartTestItemRQ();
rq.setName(name);
rq.setStartTime(startTime);
Expand Down Expand Up @@ -301,9 +299,7 @@ public static TestCaseIdEntry getTestCaseId(@Nonnull Scenario scenario) {
*/
@Nonnull
public static StartTestItemRQ buildStartScenarioRq(@Nonnull Scenario scenario) {
StartTestItemRQ rq = buildStartTestItemRq(scenario.getName(),
Calendar.getInstance().getTime(),
ItemType.STEP);
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()));
Expand All @@ -316,10 +312,10 @@ public static StartTestItemRQ buildStartScenarioRq(@Nonnull Scenario scenario) {
String description = scenario.getDescription();
if (isNotBlank(description)) {
if (hasParameters) {
rq.setDescription(
String.format(MARKDOWN_DELIMITER_PATTERN,
String.format(PARAMETERS_PATTERN, ParameterUtils.formatParametersAsTable(parameters)),
description));
rq.setDescription(String.format(MARKDOWN_DELIMITER_PATTERN,
String.format(PARAMETERS_PATTERN, ParameterUtils.formatParametersAsTable(parameters)),
description
));
} else {
rq.setDescription(description);
}
Expand Down Expand Up @@ -357,12 +353,10 @@ public static StartTestItemRQ buildStartStepRq(@Nonnull Step step, @Nonnull Scen
StartTestItemRQ rq = buildStartTestItemRq(stepName, Calendar.getInstance().getTime(), ItemType.STEP);
rq.setHasStats(false);
if (step.isOutline()) {
List<ParameterResource> parameters = scenario
.getExampleData()
List<ParameterResource> parameters = scenario.getExampleData()
.entrySet()
.stream()
.filter(e -> Pattern.compile(String.format(VARIABLE_PATTERN, e.getKey()))
.matcher(step.getText()).find())
.filter(e -> Pattern.compile(String.format(VARIABLE_PATTERN, e.getKey())).matcher(step.getText()).find())
.map(e -> {
ParameterResource param = new ParameterResource();
param.setKey(e.getKey());
Expand Down Expand Up @@ -423,7 +417,7 @@ public static void sendLog(Maybe<String> itemId, String message, LogLevel level)
/**
* Builds markdown representation of some code or script to be logged to ReportPortal
*
* @param code Code or Script
* @param code Code or Script
* @return Message to be sent to ReportPortal
*/
public static String asMarkdownCode(String code) {
Expand Down
Loading

0 comments on commit 1dbae8c

Please sign in to comment.