|
| 1 | +package io.qameta.allure; |
| 2 | + |
| 3 | +import io.qameta.allure.model.Status; |
| 4 | +import io.qameta.allure.model.Attachment; |
| 5 | +import io.qameta.allure.model.ExecutableItem; |
| 6 | +import io.qameta.allure.model.StatusDetails; |
| 7 | +import io.qameta.allure.model.TestAfterResult; |
| 8 | +import io.qameta.allure.model.TestBeforeResult; |
| 9 | +import io.qameta.allure.model.TestCaseResult; |
| 10 | +import io.qameta.allure.model.TestGroupResult; |
| 11 | +import io.qameta.allure.model.TestStepResult; |
| 12 | +import io.qameta.allure.model.WithAfter; |
| 13 | +import io.qameta.allure.model.WithAttachments; |
| 14 | +import io.qameta.allure.model.WithBefore; |
| 15 | +import io.qameta.allure.model.WithSteps; |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | + |
| 19 | +import java.nio.file.Path; |
| 20 | +import java.nio.file.Paths; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.LinkedList; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Objects; |
| 25 | +import java.util.function.Consumer; |
| 26 | + |
| 27 | +import static io.qameta.allure.AllureUtils.write; |
| 28 | +import static io.qameta.allure.AllureUtils.writeAttachment; |
| 29 | + |
| 30 | +/** |
| 31 | + * @author charlie (Dmitry Baev). |
| 32 | + */ |
| 33 | +public class Allure { |
| 34 | + |
| 35 | + private static final Logger LOGGER = LoggerFactory.getLogger(Allure.class); |
| 36 | + |
| 37 | + private static final Path OUTPUT = Paths.get("target/allure-results"); |
| 38 | + |
| 39 | + private final Map<String, Object> storage = new HashMap<>(); |
| 40 | + |
| 41 | + private final ThreadLocal<LinkedList<String>> currentStepContext = |
| 42 | + InheritableThreadLocal.withInitial(LinkedList::new); |
| 43 | + |
| 44 | + public static final Allure LIFECYCLE = new Allure(); |
| 45 | + |
| 46 | + public void startTestGroup(String uuid, TestGroupResult result) { |
| 47 | + LOGGER.info("Start test group {}", uuid); |
| 48 | + put(uuid, result).withStart(System.currentTimeMillis()); |
| 49 | + } |
| 50 | + |
| 51 | + public void updateTestGroup(String uuid, Consumer<TestGroupResult> update) { |
| 52 | + LOGGER.info("Update test group {}", uuid); |
| 53 | + update.accept(get(uuid, TestGroupResult.class)); |
| 54 | + } |
| 55 | + |
| 56 | + public void stopTestGroup(String uuid) { |
| 57 | + LOGGER.info("Stop test group {}", uuid); |
| 58 | + TestGroupResult result = remove(uuid, TestGroupResult.class) |
| 59 | + .withStop(System.currentTimeMillis()); |
| 60 | + write(result, OUTPUT); |
| 61 | + } |
| 62 | + |
| 63 | + public void startTestBefore(String parentUuid, String uuid, TestBeforeResult result) { |
| 64 | + LOGGER.info("Start test before {} with parent {}", uuid, parentUuid); |
| 65 | + put(uuid, result).withStart(System.currentTimeMillis()); |
| 66 | + get(parentUuid, WithBefore.class).getBefores().add(result); |
| 67 | + currentStepContext.remove(); |
| 68 | + currentStepContext.get().push(uuid); |
| 69 | + } |
| 70 | + |
| 71 | + public void stopTestBefore() { |
| 72 | + stopTestBefore(currentStepContext.get().getLast()); |
| 73 | + } |
| 74 | + |
| 75 | + public void stopTestBefore(String uuid) { |
| 76 | + LOGGER.info("Stop test before {}", uuid); |
| 77 | + currentStepContext.remove(); |
| 78 | + remove(uuid, TestBeforeResult.class).withStop(System.currentTimeMillis()); |
| 79 | + } |
| 80 | + |
| 81 | + public void startTestAfter(String parentUuid, String uuid, TestAfterResult result) { |
| 82 | + LOGGER.info("Start test after {} with parent {}", uuid, parentUuid); |
| 83 | + put(uuid, result).withStart(System.currentTimeMillis()); |
| 84 | + get(parentUuid, WithAfter.class).getAfters().add(result); |
| 85 | + currentStepContext.remove(); |
| 86 | + currentStepContext.get().push(uuid); |
| 87 | + } |
| 88 | + |
| 89 | + public void stopTestAfter(String uuid) { |
| 90 | + LOGGER.info("Stop test after {}", uuid); |
| 91 | + currentStepContext.remove(); |
| 92 | + remove(uuid, TestAfterResult.class).withStop(System.currentTimeMillis()); |
| 93 | + } |
| 94 | + |
| 95 | + public void scheduleTestCase(String uuid, TestCaseResult result) { |
| 96 | + LOGGER.info("Schedule test case {}", uuid); |
| 97 | + put(uuid, result); |
| 98 | + } |
| 99 | + |
| 100 | + public void cancelTestCase(String uuid, StatusDetails details) { |
| 101 | + LOGGER.info("Cancel test case {}", uuid); |
| 102 | + long currentTimeMillis = System.currentTimeMillis(); |
| 103 | + get(uuid, TestCaseResult.class) |
| 104 | + .withStatus(Status.CANCELED) |
| 105 | + .withStatusDetails(details) |
| 106 | + .withStart(currentTimeMillis) |
| 107 | + .withStop(currentTimeMillis); |
| 108 | + } |
| 109 | + |
| 110 | + public void startTestCase(String uuid) { |
| 111 | + LOGGER.info("Start test case {}", uuid); |
| 112 | + get(uuid, TestCaseResult.class).setStart(System.currentTimeMillis()); |
| 113 | + currentStepContext.remove(); |
| 114 | + currentStepContext.get().push(uuid); |
| 115 | + } |
| 116 | + |
| 117 | + public void updateTestCase(Consumer<TestCaseResult> update) { |
| 118 | + updateTestCase(currentStepContext.get().getLast(), update); |
| 119 | + } |
| 120 | + |
| 121 | + public void updateTestCase(String uuid, Consumer<TestCaseResult> update) { |
| 122 | + LOGGER.info("Update test case {}", uuid); |
| 123 | + update.accept(get(uuid, TestCaseResult.class)); |
| 124 | + } |
| 125 | + |
| 126 | + public void stopTestCase() { |
| 127 | + stopTestCase(currentStepContext.get().getLast()); |
| 128 | + } |
| 129 | + |
| 130 | + public void stopTestCase(String uuid) { |
| 131 | + LOGGER.info("Stop test case {}", uuid); |
| 132 | + currentStepContext.remove(); |
| 133 | + get(uuid, TestCaseResult.class).setStop(System.currentTimeMillis()); |
| 134 | + } |
| 135 | + |
| 136 | + public void closeTestCase(String uuid) { |
| 137 | + LOGGER.info("Close test case {}", uuid); |
| 138 | + write(remove(uuid, TestCaseResult.class), OUTPUT); |
| 139 | + } |
| 140 | + |
| 141 | + public void addAttachment(byte[] content, String name, String type) { |
| 142 | + String uuid = currentStepContext.get().getLast(); |
| 143 | + LOGGER.info("Adding attachment for {}", uuid); |
| 144 | + Attachment attachment = writeAttachment(content, name, type, OUTPUT); |
| 145 | + get(uuid, WithAttachments.class).getAttachments().add(attachment); |
| 146 | + } |
| 147 | + |
| 148 | + public void startStep(String uuid, TestStepResult result) { |
| 149 | + LinkedList<String> uuids = currentStepContext.get(); |
| 150 | + startStep(uuids.isEmpty() ? null : uuids.getFirst(), uuid, result); |
| 151 | + } |
| 152 | + |
| 153 | + public void startStep(String parentUuid, String uuid, TestStepResult result) { |
| 154 | + LOGGER.info("Start step {} with parent {}", uuid, parentUuid); |
| 155 | + put(uuid, result).withStart(System.currentTimeMillis()); |
| 156 | + currentStepContext.get().push(uuid); |
| 157 | + |
| 158 | + if (Objects.nonNull(parentUuid)) { |
| 159 | + get(parentUuid, WithSteps.class).getSteps().add(result); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + public void updateStep(Consumer<TestStepResult> update) { |
| 164 | + updateStep(currentStepContext.get().getFirst(), update); |
| 165 | + } |
| 166 | + |
| 167 | + public void updateStep(String uuid, Consumer<TestStepResult> update) { |
| 168 | + LOGGER.info("Update step {}", uuid); |
| 169 | + update.accept(get(uuid, TestStepResult.class)); |
| 170 | + } |
| 171 | + |
| 172 | + public void stopStep() { |
| 173 | + stopStep(currentStepContext.get().getFirst()); |
| 174 | + } |
| 175 | + |
| 176 | + public void stopStep(String uuid) { |
| 177 | + LOGGER.info("Stop step {}", uuid); |
| 178 | + remove(uuid, TestStepResult.class).withStop(System.currentTimeMillis()); |
| 179 | + currentStepContext.get().pop(); |
| 180 | + } |
| 181 | + |
| 182 | + private <T> T put(String uuid, T item) { |
| 183 | + Objects.requireNonNull(uuid, "Uuid can't be null"); |
| 184 | + storage.put(uuid, item); |
| 185 | + return item; |
| 186 | + } |
| 187 | + |
| 188 | + private <T> T get(String uuid, Class<T> clazz) { |
| 189 | + Objects.requireNonNull(uuid, "Uuid can't be null"); |
| 190 | + T result = cast(storage.computeIfAbsent(uuid, u -> new TestCaseResult().withName("Unknown")), clazz); |
| 191 | + return Objects.requireNonNull(result, "Could not find " + clazz + " by uuid " + uuid); |
| 192 | + } |
| 193 | + |
| 194 | + private <T> T remove(String uuid, Class<T> clazz) { |
| 195 | + Objects.requireNonNull(uuid, "Uuid can't be null"); |
| 196 | + T result = cast(storage.remove(uuid), clazz); |
| 197 | + return Objects.requireNonNull(result, "Could not find " + clazz + " by uuid " + uuid); |
| 198 | + } |
| 199 | + |
| 200 | + private <T> T cast(Object obj, Class<T> clazz) { |
| 201 | + if (clazz.isInstance(obj)) { |
| 202 | + return clazz.cast(obj); |
| 203 | + } |
| 204 | + throw new IllegalStateException("Can not cast " + obj + " to " + clazz); |
| 205 | + } |
| 206 | +} |
0 commit comments