|
| 1 | +/* |
| 2 | + * Copyright 2019 Qameta Software OÜ |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.qameta.allure.spock2; |
| 17 | + |
| 18 | +import io.qameta.allure.Allure; |
| 19 | +import io.qameta.allure.AllureLifecycle; |
| 20 | +import io.qameta.allure.model.Label; |
| 21 | +import io.qameta.allure.model.Link; |
| 22 | +import io.qameta.allure.model.Parameter; |
| 23 | +import io.qameta.allure.model.Status; |
| 24 | +import io.qameta.allure.model.StatusDetails; |
| 25 | +import io.qameta.allure.model.TestResult; |
| 26 | +import io.qameta.allure.util.AnnotationUtils; |
| 27 | +import io.qameta.allure.util.ResultsUtils; |
| 28 | +import org.spockframework.runtime.AbstractRunListener; |
| 29 | +import org.spockframework.runtime.extension.IGlobalExtension; |
| 30 | +import org.spockframework.runtime.model.ErrorInfo; |
| 31 | +import org.spockframework.runtime.model.FeatureInfo; |
| 32 | +import org.spockframework.runtime.model.IterationInfo; |
| 33 | +import org.spockframework.runtime.model.MethodInfo; |
| 34 | +import org.spockframework.runtime.model.SpecInfo; |
| 35 | + |
| 36 | +import java.lang.reflect.Method; |
| 37 | +import java.security.MessageDigest; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.Arrays; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Objects; |
| 42 | +import java.util.Set; |
| 43 | +import java.util.UUID; |
| 44 | +import java.util.stream.Collectors; |
| 45 | +import java.util.stream.IntStream; |
| 46 | + |
| 47 | +import static io.qameta.allure.util.ResultsUtils.bytesToHex; |
| 48 | +import static io.qameta.allure.util.ResultsUtils.createFrameworkLabel; |
| 49 | +import static io.qameta.allure.util.ResultsUtils.createHostLabel; |
| 50 | +import static io.qameta.allure.util.ResultsUtils.createLanguageLabel; |
| 51 | +import static io.qameta.allure.util.ResultsUtils.createPackageLabel; |
| 52 | +import static io.qameta.allure.util.ResultsUtils.createParameter; |
| 53 | +import static io.qameta.allure.util.ResultsUtils.createParentSuiteLabel; |
| 54 | +import static io.qameta.allure.util.ResultsUtils.createSubSuiteLabel; |
| 55 | +import static io.qameta.allure.util.ResultsUtils.createSuiteLabel; |
| 56 | +import static io.qameta.allure.util.ResultsUtils.createTestClassLabel; |
| 57 | +import static io.qameta.allure.util.ResultsUtils.createTestMethodLabel; |
| 58 | +import static io.qameta.allure.util.ResultsUtils.createThreadLabel; |
| 59 | +import static io.qameta.allure.util.ResultsUtils.firstNonEmpty; |
| 60 | +import static io.qameta.allure.util.ResultsUtils.getMd5Digest; |
| 61 | +import static io.qameta.allure.util.ResultsUtils.getProvidedLabels; |
| 62 | +import static io.qameta.allure.util.ResultsUtils.getStatus; |
| 63 | +import static io.qameta.allure.util.ResultsUtils.getStatusDetails; |
| 64 | +import static io.qameta.allure.util.ResultsUtils.md5; |
| 65 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 66 | +import static java.util.Comparator.comparing; |
| 67 | + |
| 68 | +/** |
| 69 | + * @author charlie (Dmitry Baev). |
| 70 | + */ |
| 71 | +@SuppressWarnings({ |
| 72 | + "PMD.NcssCount" |
| 73 | +}) |
| 74 | +public class AllureSpock2 extends AbstractRunListener implements IGlobalExtension { |
| 75 | + |
| 76 | + private final ThreadLocal<String> testResults = new InheritableThreadLocal<String>() { |
| 77 | + @Override |
| 78 | + protected String initialValue() { |
| 79 | + return UUID.randomUUID().toString(); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + private final AllureLifecycle lifecycle; |
| 84 | + |
| 85 | + @SuppressWarnings("unused") |
| 86 | + public AllureSpock2() { |
| 87 | + this(Allure.getLifecycle()); |
| 88 | + } |
| 89 | + |
| 90 | + public AllureSpock2(final AllureLifecycle lifecycle) { |
| 91 | + this.lifecycle = lifecycle; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void start() { |
| 96 | + //do nothing at this point |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void visitSpec(final SpecInfo spec) { |
| 101 | + spec.addListener(this); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void stop() { |
| 106 | + //do nothing at this point |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void beforeIteration(final IterationInfo iteration) { |
| 111 | + final String uuid = testResults.get(); |
| 112 | + |
| 113 | + final FeatureInfo feature = iteration.getFeature(); |
| 114 | + final MethodInfo methodInfo = feature.getFeatureMethod(); |
| 115 | + final Method method = methodInfo.getReflection(); |
| 116 | + final Set<Label> featureLabels = AnnotationUtils.getLabels(method); |
| 117 | + final Set<Link> featureLinks = AnnotationUtils.getLinks(method); |
| 118 | + final SpecInfo specInfo = feature.getSpec(); |
| 119 | + final Class<?> clazz = specInfo.getReflection(); |
| 120 | + final Set<Label> specLabels = AnnotationUtils.getLabels(clazz); |
| 121 | + final Set<Link> specLinks = AnnotationUtils.getLinks(clazz); |
| 122 | + final boolean flaky = AnnotationUtils.isFlaky(method) || AnnotationUtils.isFlaky(clazz); |
| 123 | + final boolean muted = AnnotationUtils.isMuted(method) || AnnotationUtils.isMuted(clazz); |
| 124 | + |
| 125 | + final List<Parameter> parameters = getParameters(feature.getDataVariables(), iteration.getDataValues()); |
| 126 | + final SpecInfo subSpec = specInfo.getSubSpec(); |
| 127 | + final SpecInfo superSpec = specInfo.getSuperSpec(); |
| 128 | + final String packageName = specInfo.getPackage(); |
| 129 | + final String specName = specInfo.getName(); |
| 130 | + final String testClassName = feature.getSpec().getReflection().getName(); |
| 131 | + final String testMethodName = iteration.getDisplayName(); |
| 132 | + |
| 133 | + final List<Label> labels = new ArrayList<>(Arrays.asList( |
| 134 | + createPackageLabel(packageName), |
| 135 | + createTestClassLabel(testClassName), |
| 136 | + createTestMethodLabel(testMethodName), |
| 137 | + createSuiteLabel(specName), |
| 138 | + createHostLabel(), |
| 139 | + createThreadLabel(), |
| 140 | + createFrameworkLabel("spock"), |
| 141 | + createLanguageLabel("java") |
| 142 | + )); |
| 143 | + |
| 144 | + if (Objects.nonNull(subSpec)) { |
| 145 | + labels.add(createSubSuiteLabel(subSpec.getName())); |
| 146 | + } |
| 147 | + |
| 148 | + if (Objects.nonNull(superSpec)) { |
| 149 | + labels.add(createParentSuiteLabel(superSpec.getName())); |
| 150 | + } |
| 151 | + |
| 152 | + labels.addAll(featureLabels); |
| 153 | + labels.addAll(specLabels); |
| 154 | + labels.addAll(getProvidedLabels()); |
| 155 | + |
| 156 | + final List<Link> links = new ArrayList<>(featureLinks); |
| 157 | + links.addAll(specLinks); |
| 158 | + |
| 159 | + final String qualifiedName = getQualifiedName(iteration); |
| 160 | + final TestResult result = new TestResult() |
| 161 | + .setUuid(uuid) |
| 162 | + .setHistoryId(getHistoryId(qualifiedName, parameters)) |
| 163 | + .setTestCaseName(iteration.getName()) |
| 164 | + .setTestCaseId(md5(qualifiedName)) |
| 165 | + .setFullName(qualifiedName) |
| 166 | + .setName( |
| 167 | + firstNonEmpty(testMethodName, iteration.getName(), qualifiedName) |
| 168 | + .orElse("Unknown") |
| 169 | + ) |
| 170 | + .setStatusDetails(new StatusDetails() |
| 171 | + .setFlaky(flaky) |
| 172 | + .setMuted(muted) |
| 173 | + ) |
| 174 | + .setParameters(parameters) |
| 175 | + .setLinks(links) |
| 176 | + .setLabels(labels); |
| 177 | + |
| 178 | + ResultsUtils.processDescription( |
| 179 | + getClass().getClassLoader(), |
| 180 | + method, |
| 181 | + result::setDescription, |
| 182 | + result::setDescriptionHtml |
| 183 | + ); |
| 184 | + |
| 185 | + getLifecycle().scheduleTestCase(result); |
| 186 | + getLifecycle().startTestCase(uuid); |
| 187 | + } |
| 188 | + |
| 189 | + private String getQualifiedName(final IterationInfo iteration) { |
| 190 | + return iteration.getFeature().getSpec().getReflection().getName() + "." + iteration.getName(); |
| 191 | + } |
| 192 | + |
| 193 | + private String getHistoryId(final String name, final List<Parameter> parameters) { |
| 194 | + final MessageDigest digest = getMd5Digest(); |
| 195 | + digest.update(name.getBytes(UTF_8)); |
| 196 | + parameters.stream() |
| 197 | + .sorted(comparing(Parameter::getName).thenComparing(Parameter::getValue)) |
| 198 | + .forEachOrdered(parameter -> { |
| 199 | + digest.update(parameter.getName().getBytes(UTF_8)); |
| 200 | + digest.update(parameter.getValue().getBytes(UTF_8)); |
| 201 | + }); |
| 202 | + final byte[] bytes = digest.digest(); |
| 203 | + return bytesToHex(bytes); |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public void error(final ErrorInfo error) { |
| 208 | + final String uuid = testResults.get(); |
| 209 | + getLifecycle().updateTestCase(uuid, testResult -> testResult |
| 210 | + .setStatus(getStatus(error.getException()).orElse(null)) |
| 211 | + .setStatusDetails(getStatusDetails(error.getException()).orElse(null)) |
| 212 | + ); |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public void afterIteration(final IterationInfo iteration) { |
| 217 | + final String uuid = testResults.get(); |
| 218 | + testResults.remove(); |
| 219 | + |
| 220 | + getLifecycle().updateTestCase(uuid, testResult -> { |
| 221 | + if (Objects.isNull(testResult.getStatus())) { |
| 222 | + testResult.setStatus(Status.PASSED); |
| 223 | + } |
| 224 | + }); |
| 225 | + getLifecycle().stopTestCase(uuid); |
| 226 | + getLifecycle().writeTestCase(uuid); |
| 227 | + } |
| 228 | + |
| 229 | + private List<Parameter> getParameters(final List<String> names, final Object... values) { |
| 230 | + return IntStream.range(0, Math.min(names.size(), values.length)) |
| 231 | + .mapToObj(index -> createParameter(names.get(index), values[index])) |
| 232 | + .collect(Collectors.toList()); |
| 233 | + } |
| 234 | + |
| 235 | + public AllureLifecycle getLifecycle() { |
| 236 | + return lifecycle; |
| 237 | + } |
| 238 | +} |
0 commit comments