Skip to content

Commit

Permalink
Add feature parameters tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Jan 9, 2024
1 parent 4f9c469 commit 11cef43
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,7 @@ public void finishLaunch() {
*/
@Nonnull
protected StartTestItemRQ buildStartFeatureRq(@Nonnull FeatureResult featureResult) {
StartTestItemRQ rq = ReportPortalUtils.buildStartFeatureRq(featureResult.getFeature());
ofNullable(featureResult.getCallArg()).filter(args -> !args.isEmpty()).ifPresent(args -> {
// TODO: cover with tests
String parameters = String.format(PARAMETERS_PATTERN, formatParametersAsTable(getParameters(args)));
String description = rq.getDescription();
if (isNotBlank(description)) {
rq.setDescription(String.format(MARKDOWN_DELIMITER_PATTERN, parameters, description));
} else {
rq.setDescription(parameters);
}
});
return rq;
return ReportPortalUtils.buildStartFeatureRq(featureResult.getFeature());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2024 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.description;

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.reportportal.utils.markdown.MarkdownUtils;
import com.epam.ta.reportportal.ws.model.StartTestItemRQ;
import com.intuit.karate.Results;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import java.util.Collection;
import java.util.Collections;
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.endsWith;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.*;

public class CallWithParametersHookTest {
private static final String TEST_FEATURE = "classpath:feature/call.feature";
private final List<String> featureIds = Stream.generate(() -> CommonUtils.namedId("feature_"))
.limit(2).collect(Collectors.toList());
private final List<String> scenarioIds = Stream.generate(() -> CommonUtils.namedId("scenario_"))
.limit(2).collect(Collectors.toList());
private final List<String> stepIds = Stream.generate(() -> CommonUtils.namedId("step_"))
.limit(4).collect(Collectors.toList());

private final List<Pair<String, Collection<Pair<String, List<String>>>>> features =
Stream.of(
Pair.of(featureIds.get(0), (Collection<Pair<String, List<String>>>) Collections.singletonList(Pair.of(scenarioIds.get(0), Collections.singletonList(stepIds.get(0))))),
Pair.of(featureIds.get(1), (Collection<Pair<String, List<String>>>) Collections.singletonList(Pair.of(scenarioIds.get(1), stepIds.subList(1, stepIds.size()))))
).collect(Collectors.toList());

private static final String PARAMETERS_DESCRIPTION_PATTERN =
"Parameters:\n\n"
+ MarkdownUtils.TABLE_INDENT
+ "| vara | result |\n"
+ MarkdownUtils.TABLE_INDENT
+ "|------|--------|\n"
+ MarkdownUtils.TABLE_INDENT
+ "|  2   |   4    |\n\n"
+ MarkdownUtils.TABLE_ROW_SEPARATOR;

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

@BeforeEach
public void setupMock() {
mockLaunch(client, null);
mockFeatures(client, features);
mockBatchLogging(client);
}

@Test
public void test_call_feature_with_parameters_hook_reporting() {
Results results = TestUtils.runAsHook(rp, TEST_FEATURE);
assertThat(results.getFailCount(), equalTo(0));

ArgumentCaptor<StartTestItemRQ> featureCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client, times(2)).startTestItem(featureCaptor.capture());
ArgumentCaptor<StartTestItemRQ> scenarioCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client).startTestItem(same(featureIds.get(0)), scenarioCaptor.capture());
verify(client).startTestItem(same(featureIds.get(1)), scenarioCaptor.capture());
ArgumentCaptor<StartTestItemRQ> stepCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client).startTestItem(same(scenarioIds.get(0)), stepCaptor.capture());
verify(client, times(3)).startTestItem(same(scenarioIds.get(1)), stepCaptor.capture());

StartTestItemRQ calledFeature = featureCaptor.getAllValues().stream()
.filter(rq -> "a feature which is called with parameters".equals(rq.getName())).findAny().orElseThrow();

assertThat(calledFeature.getDescription(), allOf(endsWith("feature/called.feature"), startsWith(PARAMETERS_DESCRIPTION_PATTERN)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2024 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.description;

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.intuit.karate.Results;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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.equalTo;
import static org.mockito.Mockito.*;

public class CallWithParametersPublisherTest {
private static final String TEST_FEATURE = "classpath:feature/call.feature";
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(1).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, null, featureId, scenarioId, stepIds);
mockBatchLogging(client);
}

@Test
public void test_call_feature_with_parameters_publisher_reporting() {
Results results = TestUtils.runAsReport(rp, TEST_FEATURE);
assertThat(results.getFailCount(), equalTo(0));

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client).startTestItem(captor.capture());
verify(client).startTestItem(same(featureId), captor.capture());
ArgumentCaptor<StartTestItemRQ> stepCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client).startTestItem(same(scenarioId), stepCaptor.capture());
}
}
11 changes: 7 additions & 4 deletions src/test/java/com/epam/reportportal/karate/utils/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ public static <T extends Collection<String>> void mockLaunch(
mockFeature(client, featureUuid, scenarioSteps);
}

public static void mockLaunch(
@Nonnull final ReportPortalClient client, @Nullable final String launchUuid) {
String launch = ofNullable(launchUuid).orElse(CommonUtils.namedId("launch_"));
when(client.startLaunch(any())).thenReturn(Maybe.just(new StartLaunchRS(launch, 1L)));
when(client.finishLaunch(eq(launch), any())).thenReturn(Maybe.just(new OperationCompletionRS()));
}

public static <T extends Collection<String>> void mockFeature(
@Nonnull final ReportPortalClient client, @Nullable final String featureUuid,
@Nonnull final Collection<Pair<String, T>> scenarioSteps) {
Expand Down Expand Up @@ -165,10 +172,6 @@ public static void mockBatchLogging(final ReportPortalClient client) {
when(client.log(any(List.class))).thenReturn(Maybe.just(new BatchSaveOperatingRS()));
}

public static void mockNestedSteps(final ReportPortalClient client, final Pair<String, String> parentNestedPair) {
mockNestedSteps(client, Collections.singletonList(parentNestedPair));
}

@SuppressWarnings("unchecked")
public static void mockNestedSteps(final ReportPortalClient client, final List<Pair<String, String>> parentNestedPairs) {
Map<String, List<String>> responseOrders = parentNestedPairs.stream()
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/feature/call.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Feature: calling another feature file

Scenario: calling a feature with parameters
* def result = call read('called.feature') { vara: 2, result: 4 }
7 changes: 7 additions & 0 deletions src/test/resources/feature/called.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@ignore
Feature: a feature which is called with parameters

Scenario: Verify different maths
Given def varb = 2
Given def mathResult = vara + varb
Then assert mathResult == result

0 comments on commit 11cef43

Please sign in to comment.