-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathAllureAspectJ.java
More file actions
174 lines (150 loc) · 6.22 KB
/
AllureAspectJ.java
File metadata and controls
174 lines (150 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright 2019 Qameta Software OÜ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 io.qameta.allure.util.ObjectUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.assertj.core.api.DefaultAssertionErrorCollector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
import static io.qameta.allure.util.StepsUtils.getStepStatus;
/**
* @author charlie (Dmitry Baev).
* @author sskorol (Sergey Korol).
*/
@SuppressWarnings("all")
@Aspect
public class AllureAspectJ {
private static final Logger LOGGER = LoggerFactory.getLogger(AllureAspectJ.class);
private static InheritableThreadLocal<AllureLifecycle> lifecycle = new InheritableThreadLocal<AllureLifecycle>() {
@Override
protected AllureLifecycle initialValue() {
return Allure.getLifecycle();
}
};
@Pointcut("execution(!private org.assertj.core.api.AbstractAssert.new(..))")
public void anyAssertCreation() {
//pointcut body, should be empty
}
@Pointcut("execution(* org.assertj.core.api.AssertJProxySetup.*(..))")
public void proxyMethod() {
//pointcut body, should be empty
}
@Pointcut("execution(* org.assertj.core.api.*ByteBuddy*.*(..))")
public void generatedByteCode() {
//pointcut body, should be empty
}
@Pointcut("execution(public * org.assertj.core.api.AbstractAssert+.*(..)) " +
"&& !proxyMethod() && !generatedByteCode()")
public void anyAssert() {
//pointcut body, should be empty
}
@Pointcut("execution(public org.assertj.core.api.DefaultAssertionErrorCollector.new())")
public void softAssertCreation() {
//pointcut body, should be empty
}
@After("softAssertCreation()")
public void setStepHookOnCollectedError(final JoinPoint joinPoint) {
final DefaultAssertionErrorCollector errorCollector = (DefaultAssertionErrorCollector) joinPoint.getTarget();
errorCollector.setAfterAssertionErrorCollected(error -> {
if (getLifecycle().getCurrentStep().isPresent()) {
getLifecycle().updateStep(step -> {
step.setStatus(getStatus(error).orElse(Status.BROKEN))
.setStatusDetails(getStatusDetails(error).orElse(null));
});
}
});
}
@After("anyAssertCreation()")
public void logAssertCreation(final JoinPoint joinPoint) {
final String actual = joinPoint.getArgs().length > 0
? ObjectUtils.toString(joinPoint.getArgs()[0])
: "<?>";
final String uuid = UUID.randomUUID().toString();
final String name = String.format("assertThat \'%s\'", actual);
final StepResult result = new StepResult()
.setName(name)
.setStatus(Status.PASSED);
getLifecycle().startStep(uuid, result);
getLifecycle().stopStep(uuid);
}
@Before("anyAssert()")
public void stepStart(final JoinPoint joinPoint) {
final MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
final String uuid = UUID.randomUUID().toString();
final String name = joinPoint.getArgs().length > 0
? String.format("%s \'%s\'", methodSignature.getName(), arrayToString(joinPoint.getArgs()))
: methodSignature.getName();
final StepResult result = new StepResult()
.setName(name);
getLifecycle().startStep(uuid, result);
}
@AfterThrowing(pointcut = "anyAssert()", throwing = "e")
public void stepFailed(final Throwable e) {
getLifecycle().updateStep(s -> s
.setStatus(getStatus(e).orElse(Status.BROKEN))
.setStatusDetails(getStatusDetails(e).orElse(null)));
getLifecycle().stopStep();
// Outer method can catch exception so outer step status should be also changed (soft assertions case).
if (getLifecycle().getCurrentStep().isPresent()) {
getLifecycle().updateStep(s -> s.setStatus(getStatus(e).orElse(Status.BROKEN)));
}
}
@AfterReturning(pointcut = "anyAssert()")
public void stepStop() {
final Status currentStepStatus = getStepStatus();
if (currentStepStatus == null) {
getLifecycle().updateStep(s -> s.setStatus(Status.PASSED));
getLifecycle().stopStep();
} else {
getLifecycle().stopStep();
if (getLifecycle().getCurrentStep().isPresent()) {
getLifecycle().updateStep(outerStep -> outerStep.setStatus(currentStepStatus));
}
}
}
/**
* For tests only.
*
* @param allure allure lifecycle to set.
*/
public static void setLifecycle(final AllureLifecycle allure) {
lifecycle.set(allure);
}
public static AllureLifecycle getLifecycle() {
return lifecycle.get();
}
private static String arrayToString(final Object... array) {
return Stream.of(array)
.map(ObjectUtils::toString)
.collect(Collectors.joining(" "));
}
}