From 65c91a1bf57721c89fba6198e08f30fbe4cafea3 Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Wed, 11 Sep 2024 19:11:51 +0300 Subject: [PATCH] Call logic fix: WIP --- .../reportportal/karate/ReportPortalHook.java | 39 +++++++++++++------ .../karate/ReportPortalUtils.java | 16 +++++++- .../utils/BlockingConcurrentHashMap.java | 2 +- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/epam/reportportal/karate/ReportPortalHook.java b/src/main/java/com/epam/reportportal/karate/ReportPortalHook.java index 5565ee8..2d21888 100644 --- a/src/main/java/com/epam/reportportal/karate/ReportPortalHook.java +++ b/src/main/java/com/epam/reportportal/karate/ReportPortalHook.java @@ -35,15 +35,13 @@ import com.intuit.karate.http.HttpRequest; import com.intuit.karate.http.Response; import io.reactivex.Maybe; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.Calendar; -import java.util.Date; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Supplier; @@ -58,6 +56,7 @@ */ public class ReportPortalHook implements RuntimeHook { private static final String FEATURE_TAG = "Feature: "; + private static final String SCENARIO_TAG = "Scenario: "; private static final Logger LOGGER = LoggerFactory.getLogger(ReportPortalHook.class); protected final MemoizingSupplier launch; private final BlockingConcurrentHashMap>> featureIdMap = new BlockingConcurrentHashMap<>(); @@ -66,6 +65,7 @@ public class ReportPortalHook implements RuntimeHook { private final Map backgroundStatusMap = new ConcurrentHashMap<>(); private final Map> stepIdMap = new ConcurrentHashMap<>(); private final Map, Date> stepStartTimeMap = new ConcurrentHashMap<>(); + private final Set> innerFeatures = Collections.newSetFromMap(new ConcurrentHashMap<>()); private volatile Thread shutDownHook; /** @@ -74,9 +74,9 @@ public class ReportPortalHook implements RuntimeHook { * @param reportPortal the ReportPortal instance */ public ReportPortalHook(ReportPortal reportPortal) { + ListenerParameters params = reportPortal.getParameters(); + StartLaunchRQ rq = buildStartLaunchRq(params); launch = new MemoizingSupplier<>(() -> { - ListenerParameters params = reportPortal.getParameters(); - StartLaunchRQ rq = buildStartLaunchRq(params); Launch newLaunch = reportPortal.newLaunch(rq); //noinspection ReactiveStreamsUnusedPublisher newLaunch.start(); @@ -165,10 +165,10 @@ protected StartTestItemRQ buildStartFeatureRq(@Nonnull FeatureRuntime fr) { @Override public boolean beforeFeature(FeatureRuntime fr) { + StartTestItemRQ rq = buildStartFeatureRq(fr); featureIdMap.computeIfAbsent(fr.featureCall.feature.getNameForReport(), f -> new MemoizingSupplier<>(() -> { - StartTestItemRQ rq = buildStartFeatureRq(fr); - if(fr.caller == null) { + if(fr.caller == null || fr.caller.depth == 0) { return launch.get().startTestItem(rq); } else { Maybe scenarioId = scenarioIdMap.get(fr.caller.parentRuntime.scenario.getUniqueId()); @@ -180,7 +180,10 @@ public boolean beforeFeature(FeatureRuntime fr) { rq.setHasStats(false); rq.setName(FEATURE_TAG + rq.getName()); Maybe itemId = launch.get().startTestItem(scenarioId, rq); - ReportPortalUtils.sendLog(itemId, rq.getDescription(), LogLevel.INFO); + innerFeatures.add(itemId); + if (StringUtils.isNotBlank(rq.getDescription())) { + ReportPortalUtils.sendLog(itemId, rq.getDescription(), LogLevel.INFO, rq.getStartTime()); + } return itemId; } })); @@ -207,6 +210,7 @@ public void afterFeature(FeatureRuntime fr) { optionalId.ifPresent(featureId -> { //noinspection ReactiveStreamsUnusedPublisher launch.get().finishTestItem(featureId, buildFinishFeatureRq(fr)); + innerFeatures.remove(featureId); }); } @@ -218,18 +222,31 @@ public void afterFeature(FeatureRuntime fr) { */ @Nonnull protected StartTestItemRQ buildStartScenarioRq(@Nonnull ScenarioRuntime sr) { - return ReportPortalUtils.buildStartScenarioRq(sr.result); + StartTestItemRQ rq = ReportPortalUtils.buildStartScenarioRq(sr.result); + ofNullable(featureIdMap.get(sr.featureRuntime.featureCall.feature.getNameForReport())) + .map(Supplier::get) + .map(featureId -> innerFeatures.contains(featureId) ? featureId : null) + .ifPresent(featureId -> { + rq.setType(ItemType.STEP.name()); + rq.setHasStats(false); + rq.setName(SCENARIO_TAG + rq.getName()); + }); + return rq; } @Override public boolean beforeScenario(ScenarioRuntime sr) { - Optional> optionalId = ofNullable(featureIdMap.get(sr.featureRuntime.featureCall.feature.getNameForReport())).map(Supplier::get); + Optional> optionalId = ofNullable(featureIdMap.get(sr.featureRuntime.featureCall.feature.getNameForReport())) + .map(Supplier::get); if (optionalId.isEmpty()) { LOGGER.error("ERROR: Trying to post unspecified feature."); } optionalId.ifPresent(featureId -> { StartTestItemRQ rq = buildStartScenarioRq(sr); Maybe scenarioId = launch.get().startTestItem(featureId, rq); + if (innerFeatures.contains(featureId) && StringUtils.isNotBlank(rq.getDescription())) { + ReportPortalUtils.sendLog(scenarioId, rq.getDescription(), LogLevel.INFO); + } scenarioIdMap.put(sr.scenario.getUniqueId(), scenarioId); }); return true; diff --git a/src/main/java/com/epam/reportportal/karate/ReportPortalUtils.java b/src/main/java/com/epam/reportportal/karate/ReportPortalUtils.java index a97231b..28e0d83 100644 --- a/src/main/java/com/epam/reportportal/karate/ReportPortalUtils.java +++ b/src/main/java/com/epam/reportportal/karate/ReportPortalUtils.java @@ -425,18 +425,30 @@ public static ItemStatus getStepStatus(String status) { * @param itemId item ID future * @param message log message to send * @param level log level + * @param logTime log time */ - public static void sendLog(Maybe itemId, String message, LogLevel level) { + public static void sendLog(Maybe itemId, String message, LogLevel level, Date logTime) { ReportPortal.emitLog(itemId, id -> { SaveLogRQ rq = new SaveLogRQ(); rq.setMessage(message); rq.setItemUuid(id); rq.setLevel(level.name()); - rq.setLogTime(Calendar.getInstance().getTime()); + rq.setLogTime(logTime); return rq; }); } + /** + * Send Step logs to ReportPortal. + * + * @param itemId item ID future + * @param message log message to send + * @param level log level + */ + public static void sendLog(Maybe itemId, String message, LogLevel level) { + sendLog(itemId, message, level, Calendar.getInstance().getTime()); + } + /** * Builds markdown representation of some code or script to be logged to ReportPortal * diff --git a/src/main/java/com/epam/reportportal/karate/utils/BlockingConcurrentHashMap.java b/src/main/java/com/epam/reportportal/karate/utils/BlockingConcurrentHashMap.java index d49290a..f699e6d 100644 --- a/src/main/java/com/epam/reportportal/karate/utils/BlockingConcurrentHashMap.java +++ b/src/main/java/com/epam/reportportal/karate/utils/BlockingConcurrentHashMap.java @@ -76,7 +76,7 @@ public T get(long timeout, TimeUnit unit) throws InterruptedException { private final Map> map = new ConcurrentHashMap<>(); - public void computeIfAbsent(@Nonnull K key, Function mappingFunction) { + public void computeIfAbsent(@Nonnull K key, Function mappingFunction) { map.computeIfAbsent(key, k -> new BlockingReference<>()).set(mappingFunction); }