|
| 1 | +package io.qameta.allure.assertj; |
| 2 | + |
| 3 | +import io.qameta.allure.Allure; |
| 4 | +import io.qameta.allure.AllureLifecycle; |
| 5 | +import io.qameta.allure.model.Status; |
| 6 | +import io.qameta.allure.model.StepResult; |
| 7 | +import org.aspectj.lang.ProceedingJoinPoint; |
| 8 | +import org.aspectj.lang.annotation.Around; |
| 9 | +import org.aspectj.lang.annotation.Aspect; |
| 10 | +import org.aspectj.lang.reflect.MethodSignature; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | + |
| 14 | +import java.util.Objects; |
| 15 | +import java.util.UUID; |
| 16 | +import java.util.stream.Collectors; |
| 17 | +import java.util.stream.Stream; |
| 18 | + |
| 19 | +import static io.qameta.allure.util.ResultsUtils.getStatus; |
| 20 | +import static io.qameta.allure.util.ResultsUtils.getStatusDetails; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author charlie (Dmitry Baev). |
| 24 | + */ |
| 25 | +@SuppressWarnings("all") |
| 26 | +@Aspect |
| 27 | +public class AllureAspectJ { |
| 28 | + |
| 29 | + private static final Logger LOGGER = LoggerFactory.getLogger(AllureAspectJ.class); |
| 30 | + |
| 31 | + private static AllureLifecycle lifecycle; |
| 32 | + |
| 33 | + @Around("execution(* org.assertj.core.api.AbstractAssert+.*(..)) " |
| 34 | + + "|| execution(* org.assertj.core.api.Assertions.assertThat(..))") |
| 35 | + public Object step(final ProceedingJoinPoint joinPoint) throws Throwable { |
| 36 | + final MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); |
| 37 | + final String name = joinPoint.getArgs().length > 0 |
| 38 | + ? String.format("%s \'%s\'", methodSignature.getName(), arrayToString(joinPoint.getArgs())) |
| 39 | + : methodSignature.getName(); |
| 40 | + final String uuid = UUID.randomUUID().toString(); |
| 41 | + final StepResult result = new StepResult() |
| 42 | + .withName(name); |
| 43 | + getLifecycle().startStep(uuid, result); |
| 44 | + try { |
| 45 | + final Object proceed = joinPoint.proceed(); |
| 46 | + getLifecycle().updateStep(uuid, s -> s.withStatus(Status.PASSED)); |
| 47 | + return proceed; |
| 48 | + } catch (Throwable e) { |
| 49 | + getLifecycle().updateStep(uuid, s -> s |
| 50 | + .withStatus(getStatus(e).orElse(Status.BROKEN)) |
| 51 | + .withStatusDetails(getStatusDetails(e).orElse(null))); |
| 52 | + throw e; |
| 53 | + } finally { |
| 54 | + getLifecycle().stopStep(uuid); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * For tests only. |
| 60 | + * |
| 61 | + * @param allure allure lifecycle to set. |
| 62 | + */ |
| 63 | + public static void setLifecycle(final AllureLifecycle allure) { |
| 64 | + lifecycle = allure; |
| 65 | + } |
| 66 | + |
| 67 | + public static AllureLifecycle getLifecycle() { |
| 68 | + if (Objects.isNull(lifecycle)) { |
| 69 | + lifecycle = Allure.getLifecycle(); |
| 70 | + } |
| 71 | + return lifecycle; |
| 72 | + } |
| 73 | + |
| 74 | + private static String arrayToString(final Object... array) { |
| 75 | + return Stream.of(array) |
| 76 | + .map(object -> { |
| 77 | + if (object.getClass().isArray()) { |
| 78 | + return arrayToString((Object[]) object); |
| 79 | + } |
| 80 | + return Objects.toString(object); |
| 81 | + }) |
| 82 | + .collect(Collectors.joining(" ")); |
| 83 | + } |
| 84 | +} |
0 commit comments