All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.testra.java.reporters.jbehave.Testra Maven / Gradle / Ivy

The newest version!
package io.github.testra.java.reporters.jbehave;

import io.github.testra.java.client.model.DataTableCell;
import io.github.testra.java.client.model.DataTableRow;
import io.github.testra.java.client.model.ScenarioRequest;
import io.github.testra.java.client.model.StepResult;
import io.github.testra.java.client.model.StepResult.StatusEnum;
import io.github.testra.java.client.model.TestStep;
import io.github.testra.java.commons.cons.ResultStatus;
import io.github.testra.java.commons.pojos.TestData;
import io.github.testra.java.reporter.TestraScenarioReporter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.jbehave.core.annotations.AfterScenario.Outcome;
import org.jbehave.core.annotations.Scope;
import org.jbehave.core.configuration.Keywords;
import org.jbehave.core.model.ExamplesTable;
import org.jbehave.core.model.Lifecycle;
import org.jbehave.core.model.Scenario;
import org.jbehave.core.model.Story;
import org.jbehave.core.reporters.NullStoryReporter;

public class Testra extends NullStoryReporter {

  private static final TestraScenarioReporter testraReporter = new TestraJBehaveReporter();
  private static final ThreadLocal currentStory = new InheritableThreadLocal<>();
  private static final ThreadLocal currentScenario = new InheritableThreadLocal<>();
  private static final ThreadLocal stepStartTime = new ThreadLocal<>();
  private static final ThreadLocal> stepResults = new ThreadLocal<>();

  private static void setUpNewScenario(Scenario scenario, Map currentTableRow) {
    long startTime = System.currentTimeMillis();

    createTest(scenario, currentTableRow);

    TestData testData =
        TestData.builder()
                .startTimeInMs(startTime)
                .isExpectedToFail(scenario.getMeta()
                                          .getPropertyNames()
                                          .contains("ExpectedFailure"))
                .build();
    testraReporter.setTestData(testData);

    stepResults.set(new ArrayList<>());
  }

  private static void createTest(Scenario scenario, Map currentTableRow) {
    Story story = currentStory.get();

    List backgroundSteps = getBackgroundSteps(story);
    List steps = getSteps(backgroundSteps.size(), scenario);
    List afterSteps = getAfterSteps(backgroundSteps.size() + steps.size(), story);

    ScenarioRequest scenarioRequest =
        new ScenarioRequest()
            .featureName(story.getName())
            .namespace(story.getPath())
            .featureDescription(story.getNarrative()
                                     .asString(new Keywords()))
            .name(scenario.getTitle())
            .backgroundSteps(backgroundSteps)
            .steps(Stream.concat(steps.stream(), afterSteps.stream())
                         .collect(Collectors.toList()))
            .tags(getTags(story, scenario))
            .manual(scenario.getMeta()
                            .getPropertyNames()
                            .contains("Manual"));

    if (currentTableRow != null) {
      scenarioRequest.setDataRows(mapToDataRows(scenario.getExamplesTable(), currentTableRow));
    }

    testraReporter.setCurrentTest(testraReporter.createTest(scenarioRequest));
  }

  private static List mapToDataRows(
      ExamplesTable examplesTable, Map currentTableRow) {
    List headers = examplesTable.getHeaders();
    List> rows = examplesTable.getRows();

    List headerCells =
        IntStream.range(0, headers.size())
                 .mapToObj(index -> new DataTableCell().index(index)
                                                       .value(headers.get(index)))
                 .collect(Collectors.toList());

    DataTableRow headerRow = new DataTableRow().index(0)
                                               .cells(headerCells);

    List dataTableCells =
        IntStream.range(0, rows.size())
                 .mapToObj(
                     cellIndex ->
                         new DataTableCell()
                             .index(cellIndex)
                             .value(currentTableRow.get(rows.get(cellIndex))))
                 .collect(Collectors.toList());

    DataTableRow dataTableRow = new DataTableRow().index(1)
                                                  .cells(dataTableCells);

    return Arrays.asList(headerRow, dataTableRow);
  }

  private static List getSteps(int startIndex, Scenario scenario) {
    return IntStream.range(0, scenario.getSteps()
                                      .size())
                    .mapToObj(index -> new TestStep().index(index + startIndex)
                                                     .text(scenario.getSteps()
                                                                   .get(index)))
                    .collect(Collectors.toList());
  }

  private static List getBackgroundSteps(Story story) {
    List beforeSteps = story.getLifecycle()
                                    .getBeforeSteps(Scope.SCENARIO);
    if (beforeSteps.size() == 0) {
      return new ArrayList<>();
    } else {
      return IntStream.range(0, beforeSteps.size())
                      .mapToObj(index -> new TestStep().index(index)
                                                       .text(beforeSteps.get(index)))
                      .collect(Collectors.toList());
    }
  }

  private static List getAfterSteps(int startIndex, Story story) {
    List afterSteps = story.getLifecycle()
                                   .getAfterSteps(Scope.SCENARIO, Outcome.SUCCESS);
    afterSteps.addAll(story.getLifecycle()
                           .getAfterSteps(Scope.SCENARIO, Outcome.ANY));

    if (afterSteps.size() == 0) {
      return new ArrayList<>();
    } else {
      return IntStream.range(0, afterSteps.size())
                      .mapToObj(index -> new TestStep().index(index + startIndex)
                                                       .text(afterSteps.get(index)))
                      .collect(Collectors.toList());
    }
  }

  private static List getTags(Story story, Scenario scenario) {
    Function prefixTagWithAmbersand = tag -> "@" + tag;
    Stream storyTags = story.getMeta()
                                    .getPropertyNames()
                                    .stream()
                                    .map(prefixTagWithAmbersand);
    Stream scenarioTags = scenario.getMeta()
                                          .getPropertyNames()
                                          .stream()
                                          .map(prefixTagWithAmbersand);
    return Stream.concat(storyTags, scenarioTags)
                 .collect(Collectors.toList());
  }

  private static void createResult() {
    long endTime = System.currentTimeMillis();
    TestData testData = testraReporter.getTestData();
    testData.setEndTimeInMs(endTime);
    if (testraReporter.getCurrentTest()
                      .isManual()) {
      testData.setResultStatus(ResultStatus.SKIPPED);
    }
    testraReporter.createResult(stepResults.get());
  }

  private static void addToStepResults(long endTime, StatusEnum resultStatus) {
    StepResult stepResult =
        new StepResult()
            .durationInMs(endTime - stepStartTime.get())
            .index(stepResults.get()
                              .size())
            .status(resultStatus);
    stepResults.get()
               .add(stepResult);
  }

  @Override
  public void beforeStory(final Story story, final boolean givenStory) {
    currentStory.set(story);
  }

  @Override
  public void afterStory(final boolean givenStory) {
    currentStory.remove();
  }

  @Override
  public void beforeScenario(Scenario scenario) {
    if (scenario.getExamplesTable()
                .getRowCount() > 0) {
      currentScenario.set(scenario);
    } else {
      setUpNewScenario(scenario, null);
    }
  }

  @Override
  public void example(Map tableRow) {
    if (testraReporter.getCurrentTest() != null) {
      createResult();
    }
    setUpNewScenario(currentScenario.get(), tableRow);
  }

  @Override
  public void afterScenario() {
    createResult();
  }

  @Override
  public void beforeStep(final String step) {
    stepStartTime.set(System.currentTimeMillis());
  }

  @Override
  public void successful(final String step) {
    if (!testraReporter.getCurrentTest()
                       .isManual()) {
      addToStepResults(System.currentTimeMillis(), StatusEnum.PASSED);
    }
  }

  @Override
  public void ignorable(final String step) {
    if (!testraReporter.getCurrentTest()
                       .isManual()) {
      addToStepResults(System.currentTimeMillis(), StatusEnum.SKIPPED);
    }
  }

  @Override
  public void pending(final String step) {
    if (!testraReporter.getCurrentTest()
                       .isManual()) {
      addToStepResults(System.currentTimeMillis(), StatusEnum.PENDING);
    }
  }

  @Override
  public void failed(final String step, final Throwable cause) {
    if (!testraReporter.getCurrentTest()
                       .isManual()) {
      addToStepResults(System.currentTimeMillis(), StatusEnum.FAILED);
      TestData testData = testraReporter.getTestData();
      testData.setError(ExceptionUtils.getStackTrace(cause));
    }
  }

  @Override
  public void lifecyle(Lifecycle lifecycle) {
    stepStartTime.set(System.currentTimeMillis());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy