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.
/*
* Copyright 2016-2024 Qameta Software Inc
*
* 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
*
* http://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 io.qameta.allure.allure1;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
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.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.ParameterKind;
import ru.yandex.qatools.allure.model.TestCaseResult;
import ru.yandex.qatools.allure.model.TestSuiteResult;
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.Comparator;
import java.util.LinkedHashMap;
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.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.RESULT_FORMAT;
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 io.qameta.allure.util.ConvertUtils.convertList;
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;
/**
* Plugin that reads results from Allure1 data format.
*
* @since 2.0
*/
@SuppressWarnings({
"PMD.ExcessiveImports",
"PMD.GodClass",
"PMD.TooManyMethods",
"ClassDataAbstractionCoupling",
"ClassFanOutComplexity",
"MultipleStringLiterals"
})
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";
public static final String ALLURE1_RESULTS_FORMAT = "allure1";
private final ObjectMapper jsonMapper = JsonMapper.builder()
.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME)
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES)
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
.disable(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)
.build();
private final ObjectMapper xmlMapper = XmlMapper.builder()
.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME)
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES)
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
.disable(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)
.annotationIntrospector(new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()))
.addModule(new XmlParserModule())
.build();
@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: {}", e.getMessage());
}
}
properties.putAll(System.getProperties());
return properties;
}
@SuppressWarnings({
"PMD.ExcessiveMethodLength",
"JavaNCSS",
"ExecutableStatementCount",
"PMD.NcssCount"
})
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(
findLabelValue(source.getLabels(), TEST_CLASS.value()),
findLabelValue(testSuite.getLabels(), TEST_CLASS.value()),
testSuite.getName(),
UNKNOWN
);
final String testMethod = firstNonNull(
findLabelValue(source.getLabels(), TEST_METHOD.value()),
source.getName(),
UNKNOWN
);
final String name = firstNonNull(source.getTitle(), source.getName(), "unknown test case");
final List parameters = getParameters(source);
final Optional historyId = findLabel(source.getLabels(), "historyId");
if (historyId.isPresent()) {
dest.setHistoryId(historyId.get().getValue());
} else {
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));
final Status status = convert(source.getStatus());
dest.setStatus(status);
dest.setTime(Time.create(source.getStart(), source.getStop()));
dest.setParameters(parameters);
dest.setDescription(getDescription(testSuite.getDescription(), source.getDescription()));
dest.setDescriptionHtml(getDescriptionHtml(testSuite.getDescription(), source.getDescription()));
Optional.ofNullable(source.getFailure()).ifPresent(failure -> {
dest.setStatusMessage(failure.getMessage());
dest.setStatusTrace(failure.getStackTrace());
});
if (!source.getSteps().isEmpty() || !source.getAttachments().isEmpty()) {
final StageResult testStage = new StageResult();
if (!source.getSteps().isEmpty()) {
//@formatter:off
testStage.setSteps(convertList(
source.getSteps(),
step -> convert(directory, visitor, step, status, dest.getStatusMessage(), dest.getStatusTrace()))
);
//@formatter:on
}
if (!source.getAttachments().isEmpty()) {
testStage.setAttachments(convertList(
source.getAttachments(),
at -> convert(directory, visitor, at)
));
}
testStage.setStatus(status);
testStage.setStatusMessage(dest.getStatusMessage());
testStage.setStatusTrace(dest.getStatusTrace());
dest.setTestStage(testStage);
}
final Set