|
| 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.junit5assert; |
| 17 | + |
| 18 | +import io.qameta.allure.Allure; |
| 19 | +import io.qameta.allure.AllureLifecycle; |
| 20 | +import io.qameta.allure.model.Status; |
| 21 | +import io.qameta.allure.model.StepResult; |
| 22 | +import io.qameta.allure.util.ObjectUtils; |
| 23 | +import org.aspectj.lang.JoinPoint; |
| 24 | +import org.aspectj.lang.annotation.*; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | + |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.List; |
| 30 | +import java.util.UUID; |
| 31 | +import java.util.stream.Collectors; |
| 32 | + |
| 33 | +import static io.qameta.allure.util.ResultsUtils.getStatus; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author legionivo (Andrey Konovka). |
| 37 | + */ |
| 38 | +@SuppressWarnings("all") |
| 39 | +@Aspect |
| 40 | +public class AllureJunit5Assert { |
| 41 | + |
| 42 | + private static final Logger LOGGER = LoggerFactory.getLogger(AllureJunit5Assert.class); |
| 43 | + private StepResult stepResult; |
| 44 | + |
| 45 | + |
| 46 | + private static InheritableThreadLocal<AllureLifecycle> lifecycle = new InheritableThreadLocal<AllureLifecycle>() { |
| 47 | + @Override |
| 48 | + protected AllureLifecycle initialValue() { |
| 49 | + return Allure.getLifecycle(); |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + @Pointcut("call(void org.junit.jupiter.api.Assertions.*(..)) || throwable()") |
| 54 | + public void anyAssert() { |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + @Pointcut("call(Throwable org.junit.jupiter.api.Assertions.*(..))") |
| 59 | + public void throwable() { |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + @Before("anyAssert()") |
| 64 | + public void stepStart(final JoinPoint joinPoint) { |
| 65 | + if (joinPoint.getArgs().length > 1) { |
| 66 | + final String uuid = UUID.randomUUID().toString(); |
| 67 | + final String assertName = joinPoint.getSignature().getName(); |
| 68 | + String name; |
| 69 | + if (joinPoint.getSignature().getName().equalsIgnoreCase("assertAll")) { |
| 70 | + name = String.format("assert All in " + " \'%s\'", joinPoint.getArgs()[0].toString()); |
| 71 | + } else { |
| 72 | + |
| 73 | + final String actual = joinPoint.getArgs().length > 0 |
| 74 | + ? ObjectUtils.toString(joinPoint.getArgs()[1]) |
| 75 | + : "<?>"; |
| 76 | + final String expected = joinPoint.getArgs().length > 0 |
| 77 | + ? ObjectUtils.toString(joinPoint.getArgs()[0]) |
| 78 | + : "<?>"; |
| 79 | + |
| 80 | + final List<String> assertArray = Arrays.asList(assertName.split("(?=[A-Z])")); |
| 81 | + if (assertArray.size() >= 3) { |
| 82 | + name = String.format(assertArray.get(0) + " " + assertArray.get(1) + " \'%s\'", expected) |
| 83 | + + " " + String.format(assertArray.stream() |
| 84 | + .skip(2) |
| 85 | + .collect(Collectors.joining(" ")) + " \'%s\'", actual); |
| 86 | + } else { |
| 87 | + name = String.format(assertArray.get(0) + " \'%s\'", expected) + " " + String.format(assertArray.get(1) + " \'%s\'", actual); |
| 88 | + } |
| 89 | + } |
| 90 | + final StepResult result = new StepResult() |
| 91 | + .setName(name) |
| 92 | + .setStatus(Status.PASSED); |
| 93 | + getLifecycle().startStep(uuid, result); |
| 94 | + } else if (joinPoint.getArgs().length > 0) { |
| 95 | + final String actual = joinPoint.getArgs().length > 0 |
| 96 | + ? ObjectUtils.toString(joinPoint.getArgs()[0]) |
| 97 | + : "<?>"; |
| 98 | + final String uuid = UUID.randomUUID().toString(); |
| 99 | + final String assertName = joinPoint.getSignature().getName(); |
| 100 | + final String name = String.format(assertName + " \'%s\'", actual); |
| 101 | + |
| 102 | + final StepResult result = new StepResult() |
| 103 | + .setName(name) |
| 104 | + .setStatus(Status.PASSED); |
| 105 | + getLifecycle().startStep(uuid, result); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @AfterThrowing(pointcut = "anyAssert()", throwing = "e") |
| 110 | + public void stepFailed(final Throwable e) { |
| 111 | + getLifecycle().updateStep(s -> s |
| 112 | + .setStatus(getStatus(e).orElse(Status.BROKEN))); |
| 113 | + getLifecycle().stopStep(); |
| 114 | + } |
| 115 | + |
| 116 | + @AfterReturning(pointcut = "anyAssert()") |
| 117 | + public void stepStop() { |
| 118 | + getLifecycle().updateStep(s -> s.setStatus(Status.PASSED)); |
| 119 | + getLifecycle().stopStep(); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * For tests only. |
| 124 | + * |
| 125 | + * @param allure allure lifecycle to set. |
| 126 | + */ |
| 127 | + public static void setLifecycle(final AllureLifecycle allure) { |
| 128 | + lifecycle.set(allure); |
| 129 | + } |
| 130 | + |
| 131 | + public static AllureLifecycle getLifecycle() { |
| 132 | + return lifecycle.get(); |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments