io.qameta.allure.allure1.Allure1Plugin Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of allure-generator Show documentation
Show all versions of allure-generator Show documentation
Module allure-generator of Allure Framework.
package io.qameta.allure.allure1;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.qameta.allure.Reader;
import io.qameta.allure.context.RandomUidContext;
import io.qameta.allure.core.Configuration;
import io.qameta.allure.core.ResultsVisitor;
import io.qameta.allure.entity.Attachment;
import io.qameta.allure.entity.Label;
import io.qameta.allure.entity.LabelName;
import io.qameta.allure.entity.Link;
import io.qameta.allure.entity.Parameter;
import io.qameta.allure.entity.StageResult;
import io.qameta.allure.entity.Status;
import io.qameta.allure.entity.StatusDetails;
import io.qameta.allure.entity.Step;
import io.qameta.allure.entity.TestResult;
import io.qameta.allure.entity.Time;
import org.allurefw.allure1.AllureUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.yandex.qatools.allure.model.Description;
import ru.yandex.qatools.allure.model.DescriptionType;
import ru.yandex.qatools.allure.model.Failure;
import ru.yandex.qatools.allure.model.ParameterKind;
import ru.yandex.qatools.allure.model.TestCaseResult;
import ru.yandex.qatools.allure.model.TestSuiteResult;
import javax.xml.bind.JAXB;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static io.qameta.allure.entity.LabelName.ISSUE;
import static io.qameta.allure.entity.LabelName.PACKAGE;
import static io.qameta.allure.entity.LabelName.PARENT_SUITE;
import static io.qameta.allure.entity.LabelName.SUB_SUITE;
import static io.qameta.allure.entity.LabelName.SUITE;
import static io.qameta.allure.entity.LabelName.TEST_CLASS;
import static io.qameta.allure.entity.LabelName.TEST_METHOD;
import static io.qameta.allure.entity.Status.BROKEN;
import static io.qameta.allure.entity.Status.FAILED;
import static io.qameta.allure.entity.Status.PASSED;
import static io.qameta.allure.entity.Status.SKIPPED;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Comparator.comparing;
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.nullsFirst;
import static java.util.stream.Collectors.toList;
import static org.allurefw.allure1.AllureUtils.unmarshalTestSuite;
/**
* Plugin that reads results from Allure1 data format.
*
* @since 2.0
*/
@SuppressWarnings({"PMD.ExcessiveImports", "PMD.GodClass", "PMD.TooManyMethods"})
public class Allure1Plugin implements Reader {
private static final Logger LOGGER = LoggerFactory.getLogger(Allure1Plugin.class);
private static final String UNKNOWN = "unknown";
private static final String MD_5 = "md5";
private static final String ISSUE_URL_PROPERTY = "allure.issues.tracker.pattern";
private static final String TMS_LINK_PROPERTY = "allure.tests.management.pattern";
private static final Comparator PARAMETER_COMPARATOR =
comparing(Parameter::getName, nullsFirst(naturalOrder()))
.thenComparing(Parameter::getValue, nullsFirst(naturalOrder()));
public static final String ENVIRONMENT_BLOCK_NAME = "environment";
private final ObjectMapper mapper = new ObjectMapper();
@Override
public void readResults(final Configuration configuration,
final ResultsVisitor visitor,
final Path resultsDirectory) {
final Properties allureProperties = loadAllureProperties(resultsDirectory);
final RandomUidContext context = configuration.requireContext(RandomUidContext.class);
final Map environment = processEnvironment(resultsDirectory);
getStreamOfAllure1Results(resultsDirectory).forEach(testSuite -> testSuite.getTestCases()
.forEach(testCase -> {
convert(context.getValue(), resultsDirectory, visitor, testSuite, testCase, allureProperties);
getEnvironmentParameters(testCase).forEach(param ->
environment.put(param.getName(), param.getValue())
);
})
);
visitor.visitExtra(ENVIRONMENT_BLOCK_NAME, environment);
}
private List getEnvironmentParameters(final TestCaseResult testCase) {
return testCase.getParameters().stream().filter(this::hasEnvType).collect(toList());
}
private Properties loadAllureProperties(final Path resultsDirectory) {
final Path propertiesFile = resultsDirectory.resolve("allure.properties");
final Properties properties = new Properties();
if (Files.exists(propertiesFile)) {
try (InputStream propFile = Files.newInputStream(propertiesFile)) {
properties.load(propFile);
} catch (IOException e) {
LOGGER.error("Error while reading allure.properties file: %s", e.getMessage());
}
}
properties.putAll(System.getProperties());
return properties;
}
@SuppressWarnings("PMD.ExcessiveMethodLength")
private void convert(final Supplier randomUid,
final Path directory,
final ResultsVisitor visitor,
final TestSuiteResult testSuite,
final TestCaseResult source,
final Properties properties) {
final TestResult dest = new TestResult();
final String suiteName = firstNonNull(testSuite.getTitle(), testSuite.getName(), "unknown test suite");
final String testClass = firstNonNull(
findLabel(source.getLabels(), TEST_CLASS.value()),
findLabel(testSuite.getLabels(), TEST_CLASS.value()),
testSuite.getName(),
UNKNOWN
);
final String testMethod = firstNonNull(
findLabel(source.getLabels(), TEST_METHOD.value()),
source.getName(),
UNKNOWN
);
final String name = firstNonNull(source.getTitle(), source.getName(), "unknown test case");
final List parameters = getParameters(source);
dest.setHistoryId(getHistoryId(String.format("%s#%s", testClass, name), parameters));
dest.setUid(randomUid.get());
dest.setName(name);
dest.setFullName(String.format("%s.%s", testClass, testMethod));
dest.setStatus(convert(source.getStatus()));
dest.setTime(source.getStart(), source.getStop());
dest.setParameters(parameters);
dest.setDescription(getDescription(testSuite.getDescription(), source.getDescription()));
dest.setDescriptionHtml(getDescriptionHtml(testSuite.getDescription(), source.getDescription()));
dest.setStatusDetails(convert(source.getFailure()));
if (!source.getSteps().isEmpty() || !source.getAttachments().isEmpty()) {
StageResult testStage = new StageResult();
if (!source.getSteps().isEmpty()) {
testStage.setSteps(convert(source.getSteps(), step -> convert(directory, visitor, step)));
}
if (!source.getAttachments().isEmpty()) {
testStage.setAttachments(convert(source.getAttachments(), at -> convert(directory, visitor, at)));
}
testStage.setStatus(convert(source.getStatus()));
testStage.setStatusDetails(convert(source.getFailure()));
dest.setTestStage(testStage);
}
final Set
© 2015 - 2025 Weber Informatics LLC | Privacy Policy