|
| 1 | +package io.qameta.allure.aspects; |
| 2 | + |
| 3 | +import io.qameta.allure.model.Label; |
| 4 | +import io.qameta.allure.model.Parameter; |
| 5 | +import io.qameta.allure.model.TestResult; |
| 6 | +import org.apache.commons.lang3.reflect.FieldUtils; |
| 7 | +import org.aspectj.lang.reflect.MethodSignature; |
| 8 | +import ru.yandex.qatools.allure.annotations.Description; |
| 9 | +import ru.yandex.qatools.allure.annotations.Features; |
| 10 | +import ru.yandex.qatools.allure.annotations.Issue; |
| 11 | +import ru.yandex.qatools.allure.annotations.Issues; |
| 12 | +import ru.yandex.qatools.allure.annotations.Severity; |
| 13 | +import ru.yandex.qatools.allure.annotations.Stories; |
| 14 | +import ru.yandex.qatools.allure.annotations.TestCaseId; |
| 15 | +import ru.yandex.qatools.allure.annotations.Title; |
| 16 | +import ru.yandex.qatools.allure.model.DescriptionType; |
| 17 | + |
| 18 | +import java.lang.reflect.Field; |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.stream.Collectors; |
| 26 | + |
| 27 | +/** |
| 28 | + * Allure labels utils. |
| 29 | + */ |
| 30 | +final class Allure1Annotations { |
| 31 | + |
| 32 | + private static final String SUITE_LABEL = "suite"; |
| 33 | + |
| 34 | + private final MethodSignature signature; |
| 35 | + |
| 36 | + private final Object target; |
| 37 | + |
| 38 | + private final Object[] args; |
| 39 | + |
| 40 | + |
| 41 | + Allure1Annotations(final Object target, final MethodSignature signature, final Object... args) { |
| 42 | + this.args = Arrays.copyOf(args, args.length); |
| 43 | + this.signature = signature; |
| 44 | + this.target = target; |
| 45 | + } |
| 46 | + |
| 47 | + public void updateTitle(final TestResult result) { |
| 48 | + final Method method = getMethod(); |
| 49 | + if (method.isAnnotationPresent(Title.class)) { |
| 50 | + final Title title = method.getAnnotation(Title.class); |
| 51 | + result.setName(title.value()); |
| 52 | + } |
| 53 | + final Class<?> type = getType(); |
| 54 | + if (type.isAnnotationPresent(Title.class)) { |
| 55 | + final Title title = type.getAnnotation(Title.class); |
| 56 | + final List<Label> labels = result.getLabels().stream() |
| 57 | + .filter(label -> !label.getName().equals(SUITE_LABEL)) |
| 58 | + .collect(Collectors.toList()); |
| 59 | + labels.add(new Label().withName(SUITE_LABEL).withValue(title.value())); |
| 60 | + result.setLabels(labels); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public void updateDescription(final TestResult result) { |
| 65 | + final Method method = getMethod(); |
| 66 | + if (method.isAnnotationPresent(Description.class)) { |
| 67 | + final Description description = method.getAnnotation(Description.class); |
| 68 | + if (description.type().equals(DescriptionType.HTML)) { |
| 69 | + result.setDescriptionHtml(description.value()); |
| 70 | + } else { |
| 71 | + result.setDescription(description.value()); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public void updateLabels(final TestResult result) { |
| 77 | + result.getLabels().addAll(getLabels()); |
| 78 | + } |
| 79 | + |
| 80 | + public void updateParameters(final TestResult result) { |
| 81 | + final Map<String, String> parameters = getParameters(); |
| 82 | + result.getParameters().stream() |
| 83 | + .map(Parameter::getName) |
| 84 | + .filter(parameters::containsKey) |
| 85 | + .forEach(parameters::remove); |
| 86 | + parameters.forEach((n, v) -> result.getParameters().add(new Parameter().withName(n).withValue(v))); |
| 87 | + } |
| 88 | + |
| 89 | + private Map<String, String> getParameters() { |
| 90 | + final Map<String, String> parameters = new HashMap<>(); |
| 91 | + parameters.putAll(getMethodParameters()); |
| 92 | + parameters.putAll(getClassParameters()); |
| 93 | + return parameters; |
| 94 | + } |
| 95 | + |
| 96 | + private Map<String, String> getMethodParameters() { |
| 97 | + final Map<String, String> parameters = new HashMap<>(); |
| 98 | + final String[] names = signature.getParameterNames(); |
| 99 | + for (int i = 0; i < names.length; i++) { |
| 100 | + parameters.put(names[i], args[i].toString()); |
| 101 | + } |
| 102 | + return parameters; |
| 103 | + } |
| 104 | + |
| 105 | + private Map<String, String> getClassParameters() { |
| 106 | + final List<Field> fields = FieldUtils.getFieldsListWithAnnotation(getType(), |
| 107 | + ru.yandex.qatools.allure.annotations.Parameter.class); |
| 108 | + |
| 109 | + return fields.stream().collect( |
| 110 | + Collectors.toMap(Allure1Utils::getParameterName, f -> Allure1Utils.getParameterValue(f, target)) |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + private Class<?> getType() { |
| 115 | + return signature.getMethod().getDeclaringClass(); |
| 116 | + } |
| 117 | + |
| 118 | + private Method getMethod() { |
| 119 | + return signature.getMethod(); |
| 120 | + } |
| 121 | + |
| 122 | + private List<Label> getLabels() { |
| 123 | + final Method method = getMethod(); |
| 124 | + final List<Label> labels = new ArrayList<>(); |
| 125 | + labels.addAll(Allure1Utils.getLabels(method, Severity.class, Allure1Utils::createLabels)); |
| 126 | + labels.addAll(Allure1Utils.getLabels(method, TestCaseId.class, Allure1Utils::createLabels)); |
| 127 | + labels.addAll(Allure1Utils.getLabels(method, Issue.class, Allure1Utils::createLabels)); |
| 128 | + labels.addAll(Allure1Utils.getLabels(method, Issues.class, Allure1Utils::createLabels)); |
| 129 | + labels.addAll(Allure1Utils.getLabels(method, Stories.class, Allure1Utils::createLabels)); |
| 130 | + labels.addAll(Allure1Utils.getLabels(method, Features.class, Allure1Utils::createLabels)); |
| 131 | + return labels; |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments