Skip to content

Commit

Permalink
Add timing test and javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Jan 9, 2024
1 parent 3e8cf83 commit 4f9c469
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/main/java/com/epam/reportportal/karate/ReportPortalHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
import static java.util.Optional.ofNullable;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

/**
* ReportPortal test results reporting hook for Karate. This class publish results in the process of test pass.
*/
public class ReportPortalHook implements RuntimeHook {
private static final Logger LOGGER = LoggerFactory.getLogger(ReportPortalHook.class);

Expand Down Expand Up @@ -145,7 +148,6 @@ protected StartTestItemRQ buildStartFeatureRq(@Nonnull FeatureRuntime fr) {

@Override
public boolean beforeFeature(FeatureRuntime fr) {
// TODO: add item start time test
Maybe<String> featureId = launch.get().startTestItem(buildStartFeatureRq(fr));
Feature feature = fr.featureCall.feature;
featureIdMap.put(feature.getNameForReport(), featureId);
Expand Down Expand Up @@ -212,8 +214,8 @@ protected FinishTestItemRQ buildFinishScenarioRq(@Nonnull ScenarioRuntime sr) {
/**
* Build ReportPortal request for start Background event.
*
* @param step Karate's Step object instance
* @param sr Karate's ScenarioRuntime object instance
* @param step Karate's Step object instance
* @param sr Karate's ScenarioRuntime object instance
* @return request to ReportPortal
*/
@Nonnull
Expand All @@ -225,11 +227,11 @@ protected StartTestItemRQ buildStartBackgroundRq(@Nonnull Step step, @Nonnull Sc
/**
* Start sending Background data to ReportPortal.
*
* @param step Karate's Step object instance
* @param sr Karate's ScenarioRuntime object instance
* @param step Karate's Step object instance
* @param sr Karate's ScenarioRuntime object instance
*/
public Maybe<String> startBackground(@Nonnull Step step, @Nonnull ScenarioRuntime sr) {
return backgroundIdMap.computeIfAbsent(sr.scenario.getUniqueId(), k-> {
return backgroundIdMap.computeIfAbsent(sr.scenario.getUniqueId(), k -> {
StartTestItemRQ backgroundRq = buildStartBackgroundRq(step, sr);
return launch.get().startTestItem(scenarioIdMap.get(sr.scenario.getUniqueId()),
backgroundRq);
Expand All @@ -239,8 +241,8 @@ public Maybe<String> startBackground(@Nonnull Step step, @Nonnull ScenarioRuntim
/**
* Build ReportPortal request for finish Background event.
*
* @param step Karate's Step object instance
* @param sr Karate's ScenarioRuntime object instance
* @param step Karate's Step object instance
* @param sr Karate's ScenarioRuntime object instance
* @return request to ReportPortal
*/
@Nonnull
Expand All @@ -253,8 +255,8 @@ protected FinishTestItemRQ buildFinishBackgroundRq(@Nullable Step step, @Nonnull
/**
* Finish sending Scenario data to ReportPortal.
*
* @param step Karate's Step object instance
* @param sr Karate's ScenarioRuntime object instance
* @param step Karate's Step object instance
* @param sr Karate's ScenarioRuntime object instance
*/
public void finishBackground(@Nullable Step step, @Nonnull ScenarioRuntime sr) {
Maybe<String> backgroundId = backgroundIdMap.remove(sr.scenario.getUniqueId());
Expand Down Expand Up @@ -301,8 +303,8 @@ private Date getStepStartTime(@Nullable Maybe<String> stepId) {
/**
* Customize start Step test item event/request.
*
* @param step Karate's Step object instance
* @param sr Karate's ScenarioRuntime object instance
* @param step Karate's Step object instance
* @param sr Karate's ScenarioRuntime object instance
* @return request to ReportPortal
*/
@Nonnull
Expand Down Expand Up @@ -363,7 +365,7 @@ public boolean beforeStep(Step step, ScenarioRuntime sr) {
* Send Step execution results to ReportPortal.
*
* @param stepResult step execution results
* @param sr Karate's ScenarioRuntime object instance
* @param sr Karate's ScenarioRuntime object instance
*/
public void sendStepResults(StepResult stepResult, ScenarioRuntime sr) {
Maybe<String> stepId = stepIdMap.get(sr.scenario.getUniqueId());
Expand All @@ -382,8 +384,8 @@ public void sendStepResults(StepResult stepResult, ScenarioRuntime sr) {
/**
* Build ReportPortal request for finish Step event.
*
* @param stepResult Karate's StepResult class instance
* @param sr Karate's ScenarioRuntime object instance
* @param stepResult Karate's StepResult class instance
* @param sr Karate's ScenarioRuntime object instance
* @return request to ReportPortal
*/
@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
import static java.util.Optional.ofNullable;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

/**
* ReportPortal test results publisher for Karate. This class publish results after test pass.
*/
public class ReportPortalPublisher {
private static final Logger LOGGER = LoggerFactory.getLogger(ReportPortalPublisher.class);
private final Map<String, Maybe<String>> featureIdMap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2023 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
*
* https://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.
*/

package com.epam.reportportal.karate.timing;

import com.epam.reportportal.karate.utils.TestUtils;
import com.epam.reportportal.service.ReportPortal;
import com.epam.reportportal.service.ReportPortalClient;
import com.epam.reportportal.util.test.CommonUtils;
import com.epam.ta.reportportal.ws.model.StartTestItemRQ;
import com.epam.ta.reportportal.ws.model.launch.StartLaunchRQ;
import com.intuit.karate.Results;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.epam.reportportal.karate.utils.TestUtils.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.*;

public class SimpleTimingTest {
private static final String TEST_FEATURE = "classpath:feature/simple.feature";
private final String launchUuid = CommonUtils.namedId("launch_");
private final String featureId = CommonUtils.namedId("feature_");
private final String scenarioId = CommonUtils.namedId("scenario_");
private final List<String> stepIds = Stream.generate(() -> CommonUtils.namedId("step_"))
.limit(3).collect(Collectors.toList());

private final ReportPortalClient client = mock(ReportPortalClient.class);
private final ReportPortal rp = ReportPortal.create(client, standardParameters(), testExecutor());

@BeforeEach
public void setupMock() {
mockLaunch(client, launchUuid, featureId, scenarioId, stepIds);
mockBatchLogging(client);
}

@ParameterizedTest
@ValueSource(booleans = {true, false})
public void test_each_item_has_correct_start_date(boolean report) {
Results results;
if (report) {
results = TestUtils.runAsReport(rp, TEST_FEATURE);
} else {
results = TestUtils.runAsHook(rp, TEST_FEATURE);
}
assertThat(results.getFailCount(), equalTo(0));

ArgumentCaptor<StartLaunchRQ> launchCaptor = ArgumentCaptor.forClass(StartLaunchRQ.class);
verify(client).startLaunch(launchCaptor.capture());
ArgumentCaptor<StartTestItemRQ> featureCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client).startTestItem(featureCaptor.capture());
ArgumentCaptor<StartTestItemRQ> scenarioCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client).startTestItem(same(featureId), scenarioCaptor.capture());
ArgumentCaptor<StartTestItemRQ> stepCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client, times(3)).startTestItem(same(scenarioId), stepCaptor.capture());

StartLaunchRQ launchRq = launchCaptor.getValue();
StartTestItemRQ featureRq = featureCaptor.getValue();
StartTestItemRQ scenarioRq = scenarioCaptor.getValue();

assertThat("Launch start time is greater than Feature start time.",
featureRq.getStartTime(), greaterThanOrEqualTo(launchRq.getStartTime()));
assertThat("Feature start time is greater than Scenario start time.",
scenarioRq.getStartTime(), greaterThanOrEqualTo(featureRq.getStartTime()));

List<StartTestItemRQ> steps = stepCaptor.getAllValues();
StartTestItemRQ firstStep = steps.stream()
.filter(s -> "Given def four = 4".equals(s.getName())).findAny().orElseThrow();
StartTestItemRQ secondStep = steps.stream()
.filter(s -> "When def actualFour = 2 * 2".equals(s.getName())).findAny().orElseThrow();
StartTestItemRQ thirdStep = steps.stream()
.filter(s -> "Then assert actualFour == four".equals(s.getName())).findAny().orElseThrow();

assertThat("Scenario start time is greater than Step start time.",
firstStep.getStartTime(), greaterThanOrEqualTo(scenarioRq.getStartTime()));
assertThat("First Step start time is greater or equal than Second Step start time.",
secondStep.getStartTime(), greaterThan(firstStep.getStartTime()));
assertThat("Second Step start time is greater or equal than Third Step start time.",
thirdStep.getStartTime(), greaterThan(secondStep.getStartTime()));
}
}

0 comments on commit 4f9c469

Please sign in to comment.