io.qameta.allure.assertj.AllureAspectJ Maven / Gradle / Ivy
package io.qameta.allure.assertj;
import io.qameta.allure.Allure;
import io.qameta.allure.AllureLifecycle;
import io.qameta.allure.model.Status;
import io.qameta.allure.model.StepResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static io.qameta.allure.util.ResultsUtils.getStatus;
import static io.qameta.allure.util.ResultsUtils.getStatusDetails;
/**
* @author charlie (Dmitry Baev).
*/
@SuppressWarnings("all")
@Aspect
public class AllureAspectJ {
private static final Logger LOGGER = LoggerFactory.getLogger(AllureAspectJ.class);
private static AllureLifecycle lifecycle;
@Around("execution(* org.assertj.core.api.AbstractAssert+.*(..)) "
+ "|| execution(* org.assertj.core.api.Assertions.assertThat(..))")
public Object step(final ProceedingJoinPoint joinPoint) throws Throwable {
final MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
final String name = joinPoint.getArgs().length > 0
? String.format("%s \'%s\'", methodSignature.getName(), arrayToString(joinPoint.getArgs()))
: methodSignature.getName();
final String uuid = UUID.randomUUID().toString();
final StepResult result = new StepResult()
.withName(name);
getLifecycle().startStep(uuid, result);
try {
final Object proceed = joinPoint.proceed();
getLifecycle().updateStep(uuid, s -> s.withStatus(Status.PASSED));
return proceed;
} catch (Throwable e) {
getLifecycle().updateStep(uuid, s -> s
.withStatus(getStatus(e).orElse(Status.BROKEN))
.withStatusDetails(getStatusDetails(e).orElse(null)));
throw e;
} finally {
getLifecycle().stopStep(uuid);
}
}
/**
* For tests only.
*
* @param allure allure lifecycle to set.
*/
public static void setLifecycle(final AllureLifecycle allure) {
lifecycle = allure;
}
public static AllureLifecycle getLifecycle() {
if (Objects.isNull(lifecycle)) {
lifecycle = Allure.getLifecycle();
}
return lifecycle;
}
private static String arrayToString(final Object... array) {
return Stream.of(array)
.map(object -> {
if (object.getClass().isArray()) {
return arrayToString((Object[]) object);
}
return Objects.toString(object);
})
.collect(Collectors.joining(" "));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy