|
| 1 | +package io.qameta.allure.spring4; |
| 2 | + |
| 3 | +import io.qameta.allure.Allure; |
| 4 | +import io.qameta.allure.AllureLifecycle; |
| 5 | +import io.qameta.allure.Epic; |
| 6 | +import io.qameta.allure.Feature; |
| 7 | +import io.qameta.allure.Issue; |
| 8 | +import io.qameta.allure.Owner; |
| 9 | +import io.qameta.allure.Severity; |
| 10 | +import io.qameta.allure.Story; |
| 11 | +import io.qameta.allure.TmsLink; |
| 12 | +import io.qameta.allure.model.Label; |
| 13 | +import io.qameta.allure.model.Link; |
| 14 | +import io.qameta.allure.model.Status; |
| 15 | +import io.qameta.allure.model.StatusDetails; |
| 16 | +import io.qameta.allure.model.TestResult; |
| 17 | +import io.qameta.allure.util.ResultsUtils; |
| 18 | +import org.springframework.test.context.TestContext; |
| 19 | +import org.springframework.test.context.TestExecutionListener; |
| 20 | + |
| 21 | +import java.lang.annotation.Annotation; |
| 22 | +import java.lang.reflect.Method; |
| 23 | +import java.math.BigInteger; |
| 24 | +import java.security.MessageDigest; |
| 25 | +import java.security.NoSuchAlgorithmException; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Objects; |
| 28 | +import java.util.Optional; |
| 29 | +import java.util.UUID; |
| 30 | +import java.util.stream.Collectors; |
| 31 | +import java.util.stream.Stream; |
| 32 | + |
| 33 | +import static io.qameta.allure.util.ResultsUtils.getHostName; |
| 34 | +import static io.qameta.allure.util.ResultsUtils.getStatus; |
| 35 | +import static io.qameta.allure.util.ResultsUtils.getStatusDetails; |
| 36 | +import static io.qameta.allure.util.ResultsUtils.getThreadName; |
| 37 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 38 | + |
| 39 | +/** |
| 40 | + * @author charlie (Dmitry Baev). |
| 41 | + */ |
| 42 | +@SuppressWarnings("PMD.ExcessiveImports") |
| 43 | +public class AllureSpring4 implements TestExecutionListener { |
| 44 | + |
| 45 | + private static final String MD_5 = "md5"; |
| 46 | + |
| 47 | + private final ThreadLocal<String> testCases |
| 48 | + = InheritableThreadLocal.withInitial(() -> UUID.randomUUID().toString()); |
| 49 | + |
| 50 | + private final AllureLifecycle lifecycle; |
| 51 | + |
| 52 | + public AllureSpring4() { |
| 53 | + this.lifecycle = Allure.getLifecycle(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void beforeTestClass(final TestContext testContext) throws Exception { |
| 58 | + //do nothing |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void prepareTestInstance(final TestContext testContext) throws Exception { |
| 63 | + //do nothing |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void beforeTestMethod(final TestContext testContext) throws Exception { |
| 68 | + final String uuid = testCases.get(); |
| 69 | + final Class<?> testClass = testContext.getTestClass(); |
| 70 | + final Method testMethod = testContext.getTestMethod(); |
| 71 | + final String id = getHistoryId(testClass, testMethod); |
| 72 | + |
| 73 | + final TestResult result = new TestResult() |
| 74 | + .withUuid(uuid) |
| 75 | + .withHistoryId(id) |
| 76 | + .withName(testMethod.getName()) |
| 77 | + .withFullName(String.format("%s.%s", testClass.getCanonicalName(), testMethod.getName())) |
| 78 | + .withLinks(getLinks(testClass, testMethod)) |
| 79 | + .withLabels( |
| 80 | + new Label().withName("package").withValue(testClass.getCanonicalName()), |
| 81 | + new Label().withName("testClass").withValue(testClass.getCanonicalName()), |
| 82 | + new Label().withName("testMethod").withValue(testMethod.getName()), |
| 83 | + |
| 84 | + new Label().withName("suite").withValue(testClass.getName()), |
| 85 | + |
| 86 | + new Label().withName("host").withValue(getHostName()), |
| 87 | + new Label().withName("thread").withValue(getThreadName()) |
| 88 | + ); |
| 89 | + |
| 90 | + result.getLabels().addAll(getLabels(testClass, testMethod)); |
| 91 | + getDisplayName(testMethod).ifPresent(result::setName); |
| 92 | + getLifecycle().scheduleTestCase(result); |
| 93 | + getLifecycle().startTestCase(uuid); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void afterTestMethod(final TestContext testContext) throws Exception { |
| 98 | + final String uuid = testCases.get(); |
| 99 | + testCases.remove(); |
| 100 | + getLifecycle().updateTestCase(uuid, testResult -> { |
| 101 | + testResult.setStatus(getStatus(testContext.getTestException()).orElse(Status.PASSED)); |
| 102 | + if (Objects.isNull(testResult.getStatusDetails())) { |
| 103 | + testResult.setStatusDetails(new StatusDetails()); |
| 104 | + } |
| 105 | + getStatusDetails(testContext.getTestException()).ifPresent(statusDetails -> { |
| 106 | + testResult.getStatusDetails().setMessage(statusDetails.getMessage()); |
| 107 | + testResult.getStatusDetails().setTrace(statusDetails.getTrace()); |
| 108 | + }); |
| 109 | + }); |
| 110 | + getLifecycle().stopTestCase(uuid); |
| 111 | + getLifecycle().writeTestCase(uuid); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void afterTestClass(final TestContext testContext) throws Exception { |
| 116 | + //do nothing |
| 117 | + } |
| 118 | + |
| 119 | + public AllureLifecycle getLifecycle() { |
| 120 | + return lifecycle; |
| 121 | + } |
| 122 | + |
| 123 | + private Optional<String> getDisplayName(final Method method) { |
| 124 | + return Optional.ofNullable(method.getAnnotation(DisplayName.class)) |
| 125 | + .map(DisplayName::value); |
| 126 | + } |
| 127 | + |
| 128 | + private List<Link> getLinks(final Class<?> testClass, final Method testMethod) { |
| 129 | + return Stream.of( |
| 130 | + getAnnotations(testClass, testMethod, io.qameta.allure.Link.class).map(ResultsUtils::createLink), |
| 131 | + getAnnotations(testClass, testMethod, Issue.class).map(ResultsUtils::createLink), |
| 132 | + getAnnotations(testClass, testMethod, TmsLink.class).map(ResultsUtils::createLink) |
| 133 | + ).reduce(Stream::concat).orElseGet(Stream::empty).collect(Collectors.toList()); |
| 134 | + } |
| 135 | + |
| 136 | + private List<Label> getLabels(final Class<?> testClass, final Method testMethod) { |
| 137 | + return Stream.of( |
| 138 | + getAnnotations(testClass, testMethod, Epic.class).map(ResultsUtils::createLabel), |
| 139 | + getAnnotations(testClass, testMethod, Feature.class).map(ResultsUtils::createLabel), |
| 140 | + getAnnotations(testClass, testMethod, Story.class).map(ResultsUtils::createLabel), |
| 141 | + getAnnotations(testClass, testMethod, Severity.class).map(ResultsUtils::createLabel), |
| 142 | + getAnnotations(testClass, testMethod, Owner.class).map(ResultsUtils::createLabel) |
| 143 | + ).reduce(Stream::concat).orElseGet(Stream::empty).collect(Collectors.toList()); |
| 144 | + } |
| 145 | + |
| 146 | + private <T extends Annotation> Stream<T> getAnnotations( |
| 147 | + final Class<?> testClass, final Method testMethod, final Class<T> annotation) { |
| 148 | + return Stream.of(annotation) |
| 149 | + .flatMap(clazz -> Stream.concat( |
| 150 | + Stream.of(testClass.getAnnotationsByType(clazz)), |
| 151 | + Stream.of(testMethod.getAnnotationsByType(clazz))) |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + private String getHistoryId(final Class<?> testClass, final Method testMethod) { |
| 156 | + final MessageDigest digest = getMessageDigest(); |
| 157 | + digest.update(testClass.getCanonicalName().getBytes(UTF_8)); |
| 158 | + digest.update(testMethod.getName().getBytes(UTF_8)); |
| 159 | + Stream.of(testMethod.getParameterTypes()) |
| 160 | + .map(Class::getCanonicalName) |
| 161 | + .map(name -> name.getBytes(UTF_8)) |
| 162 | + .forEach(digest::update); |
| 163 | + return new BigInteger(1, digest.digest()).toString(16); |
| 164 | + } |
| 165 | + |
| 166 | + private MessageDigest getMessageDigest() { |
| 167 | + try { |
| 168 | + return MessageDigest.getInstance(MD_5); |
| 169 | + } catch (NoSuchAlgorithmException e) { |
| 170 | + throw new IllegalStateException("Could not find md5 hashing algorithm", e); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments