Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Jan 30, 2024
1 parent f67f19b commit ba6d665
Showing 1 changed file with 44 additions and 33 deletions.
77 changes: 44 additions & 33 deletions src/test/java/com/epam/reportportal/jbehave/ExamplesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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.FinishTestItemRQ;
import com.epam.ta.reportportal.ws.model.ParameterResource;
import com.epam.ta.reportportal.ws.model.StartTestItemRQ;
Expand All @@ -49,11 +50,9 @@ public class ExamplesTest extends BaseTest {

public static final int STEPS_QUANTITY = 4;
private final String storyId = CommonUtils.namedId("story_");
private final String scenarioId = CommonUtils.namedId("scenario_");
private final List<String> exampleIds = Stream.generate(() -> CommonUtils.namedId("example_")).limit(2).collect(Collectors.toList());

private final List<Pair<String, String>> stepIds = exampleIds.stream()
.flatMap(e -> Stream.generate(() -> Pair.of(e, CommonUtils.namedId("step_"))).limit(STEPS_QUANTITY))
private final List<String> scenarioIds = Stream.generate(() -> CommonUtils.namedId("scenario_")).limit(2).collect(Collectors.toList());
private final List<Pair<String, List<String>>> stepIds = scenarioIds.stream()
.map(e -> Pair.of(e, Stream.generate(() -> CommonUtils.namedId("step_")).limit(STEPS_QUANTITY).collect(Collectors.toList())))
.collect(Collectors.toList());

private final ReportPortalClient client = mock(ReportPortalClient.class);
Expand All @@ -64,15 +63,25 @@ public class ExamplesTest extends BaseTest {

@BeforeEach
public void setupMock() {
mockLaunch(client, null, storyId, scenarioId, exampleIds);
mockNestedSteps(client, stepIds);
mockLaunch(client, null, storyId, stepIds);
mockBatchLogging(client);

}

private static final List<String> EXAMPLE_NAMES = asList(
"Example: [symbol: STK1$; threshold: 10.0; price: 5.0; status: OFF]",
"Example: [symbol: STK1$; threshold: 10.0; price: 11.0; status: ON]"
private static final String EXAMPLE_NAME = "Stock trade alert";
//@formatter:off
private static final String PARAMETERS_PREFIX =
"Parameters:\n\n"
+ MarkdownUtils.TABLE_INDENT + "|\u00A0symbol\u00A0|\u00A0threshold\u00A0|\u00A0price\u00A0|\u00A0status\u00A0|\n"
+ MarkdownUtils.TABLE_INDENT + "|--------|-----------|-------|--------|\n"
+ MarkdownUtils.TABLE_INDENT;
//@formatter:on

private static final List<String> EXAMPLE_DESCRIPTIONS = asList(
PARAMETERS_PREFIX
+ "|\u00A0STK1$\u00A0\u00A0|\u00A0\u00A0\u00A010.0\u00A0\u00A0\u00A0\u00A0|\u00A0\u00A05.0\u00A0\u00A0|\u00A0\u00A0OFF\u00A0\u00A0\u00A0|",
PARAMETERS_PREFIX
+ "|\u00A0STK1$\u00A0\u00A0|\u00A0\u00A0\u00A010.0\u00A0\u00A0\u00A0\u00A0|\u00A011.0\u00A0\u00A0|\u00A0\u00A0\u00A0ON\u00A0\u00A0\u00A0|"
);

private static final List<Map<String, String>> EXAMPLE_PARAMETERS = asList(new HashMap<String, String>() {{
Expand All @@ -87,8 +96,7 @@ public void setupMock() {
put("status", "ON");
}});

private static final List<String> STEP_NAMES = asList(
"Given a stock of symbol STK1$ and a threshold 10.0",
private static final List<String> STEP_NAMES = asList("Given a stock of symbol STK1$ and a threshold 10.0",
"When the stock is traded at price 5.0",
"Then the alert status should be status OFF",
"When I have first parameter STK1$ and second parameter STK1$",
Expand All @@ -98,40 +106,42 @@ public void setupMock() {
"When I have first parameter STK1$ and second parameter STK1$"
);

private static final List<List<ParameterResource>> STEP_PARAMETERS = asList(
asList(parameterOf("symbol", "STK1$"), parameterOf("threshold", "10.0")),
asList(parameterOf("price", "5.0")),
asList(parameterOf("status", "OFF")),
asList(parameterOf("symbol", "STK1$"), parameterOf("symbol", "STK1$")),
asList(parameterOf("symbol", "STK1$"), parameterOf("threshold", "10.0")),
asList(parameterOf("price", "11.0")),
asList(parameterOf("status", "ON")),
asList(parameterOf("symbol", "STK1$"), parameterOf("symbol", "STK1$"))
private static final List<List<ParameterResource>> STEP_PARAMETERS = asList(asList(
parameterOf("symbol", "STK1$"),
parameterOf("threshold", "10.0")
),
asList(parameterOf("price", "5.0")),
asList(parameterOf("status", "OFF")),
asList(parameterOf("symbol", "STK1$"), parameterOf("symbol", "STK1$")),
asList(parameterOf("symbol", "STK1$"), parameterOf("threshold", "10.0")),
asList(parameterOf("price", "11.0")),
asList(parameterOf("status", "ON")),
asList(parameterOf("symbol", "STK1$"), parameterOf("symbol", "STK1$"))
);

@Test
public void verify_story_with_examples_names_types_and_parameters() {
run(format, "stories/Examples.story", new StockSteps(), new ParameterizedSteps());

verify(client, times(1)).startTestItem(any());
verify(client, times(1)).startTestItem(same(storyId), any());
ArgumentCaptor<StartTestItemRQ> startCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client, times(2)).startTestItem(same(scenarioId), startCaptor.capture());
verify(client, times(STEPS_QUANTITY)).startTestItem(same(exampleIds.get(0)), startCaptor.capture());
verify(client, times(STEPS_QUANTITY)).startTestItem(same(exampleIds.get(1)), startCaptor.capture());
ArgumentCaptor<StartTestItemRQ> exampleCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client, times(2)).startTestItem(same(storyId), exampleCaptor.capture());
ArgumentCaptor<StartTestItemRQ> stepCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client, times(STEPS_QUANTITY)).startTestItem(same(scenarioIds.get(0)), stepCaptor.capture());
verify(client, times(STEPS_QUANTITY)).startTestItem(same(scenarioIds.get(1)), stepCaptor.capture());

// Start items verification
List<StartTestItemRQ> startItems = startCaptor.getAllValues();
List<StartTestItemRQ> examples = startItems.subList(0, 2);
List<StartTestItemRQ> examples = exampleCaptor.getAllValues();
IntStream.range(0, examples.size()).forEach(i -> {
StartTestItemRQ rq = examples.get(i);
assertThat(rq.getName(), equalTo(EXAMPLE_NAMES.get(i)));
assertThat(rq.getName(), equalTo(EXAMPLE_NAME));
assertThat(rq.getDescription(), equalTo(EXAMPLE_DESCRIPTIONS.get(i)));
assertThat(rq.getType(), equalTo(ItemType.TEST.name()));
assertThat(rq.getParameters(), hasSize(STEPS_QUANTITY));
rq.getParameters().forEach(p -> assertThat(EXAMPLE_PARAMETERS.get(i), hasEntry(p.getKey(), p.getValue())));
});

List<StartTestItemRQ> steps = startItems.subList(2, 2 + 2 * STEPS_QUANTITY);
List<StartTestItemRQ> steps = stepCaptor.getAllValues();
IntStream.range(0, steps.size()).forEach(i -> {
StartTestItemRQ rq = steps.get(i);
assertThat(rq.getName(), equalTo(STEP_NAMES.get(i)));
Expand All @@ -141,18 +151,19 @@ public void verify_story_with_examples_names_types_and_parameters() {

// Finish items verification
ArgumentCaptor<FinishTestItemRQ> finishStepCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class);
stepIds.forEach(s -> verify(client, times(1)).finishTestItem(same(s.getValue()), finishStepCaptor.capture()));
stepIds.stream()
.flatMap(p -> p.getValue().stream())
.forEach(s -> verify(client, times(1)).finishTestItem(same(s), finishStepCaptor.capture()));

List<FinishTestItemRQ> finishSteps = finishStepCaptor.getAllValues();
assertThat(finishSteps, hasSize(2 * STEPS_QUANTITY));
finishSteps.forEach(rq -> assertThat(rq.getStatus(), equalTo(ItemStatus.PASSED.name())));

ArgumentCaptor<FinishTestItemRQ> finishExampleCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class);
exampleIds.forEach(s -> verify(client, times(1)).finishTestItem(same(s), finishExampleCaptor.capture()));
scenarioIds.forEach(s -> verify(client, times(1)).finishTestItem(same(s), finishExampleCaptor.capture()));
finishExampleCaptor.getAllValues().forEach(rq -> assertThat(rq.getStatus(), equalTo(ItemStatus.PASSED.name())));

ArgumentCaptor<FinishTestItemRQ> finishCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class);
verify(client, times(1)).finishTestItem(same(scenarioId), finishCaptor.capture());
verify(client, times(1)).finishTestItem(same(storyId), finishCaptor.capture());
finishCaptor.getAllValues().forEach(rq -> assertThat(rq.getStatus(), equalTo(ItemStatus.PASSED.name())));
}
Expand Down

0 comments on commit ba6d665

Please sign in to comment.