-
Notifications
You must be signed in to change notification settings - Fork 6
/
QaseEventListener.java
194 lines (176 loc) · 8 KB
/
QaseEventListener.java
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package io.qase.cucumber5;
import gherkin.AstBuilder;
import gherkin.Parser;
import gherkin.ast.*;
import io.cucumber.plugin.ConcurrentEventListener;
import io.cucumber.plugin.event.*;
import io.qase.api.QaseClient;
import io.qase.api.StepStorage;
import io.qase.api.config.QaseConfig;
import io.qase.api.services.QaseTestCaseListener;
import io.qase.api.utils.CucumberUtils;
import io.qase.api.utils.IntegrationUtils;
import io.qase.client.model.ResultCreate;
import io.qase.client.model.ResultCreate.StatusEnum;
import io.qase.client.model.ResultCreateCase;
import io.qase.client.model.ResultCreateStepsInner;
import io.qase.cucumber5.guice.module.Cucumber5Module;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import static io.qase.api.utils.CucumberUtils.getHash;
import static io.qase.api.utils.IntegrationUtils.getStacktrace;
@Slf4j
public class QaseEventListener implements ConcurrentEventListener {
private static final Map<Integer, Map<String, String>> EXAMPLES = new HashMap<>();
private static final String REPORTER_NAME = "Cucumber 5-JVM";
@Getter(lazy = true, value = AccessLevel.PRIVATE)
private final QaseTestCaseListener qaseTestCaseListener = createQaseListener();
static {
System.setProperty(QaseConfig.QASE_CLIENT_REPORTER_NAME_KEY, REPORTER_NAME);
}
@Override
public void setEventPublisher(EventPublisher publisher) {
if (QaseClient.isEnabled()) {
publisher.registerHandlerFor(TestCaseStarted.class, this::testCaseStarted);
publisher.registerHandlerFor(TestCaseFinished.class, this::testCaseFinished);
publisher.registerHandlerFor(TestRunFinished.class, this::testRunFinished);
publisher.registerHandlerFor(TestStepFinished.class, this::testStepFinished);
publisher.registerHandlerFor(TestStepStarted.class, this::testCaseStarted);
}
}
private void testCaseStarted(TestStepStarted event) {
if (EXAMPLES.get(getHash(event.getTestCase().getUri(), (long) event.getTestCase().getLine())) == null) {
TestCase testCase = event.getTestCase();
URI uri = testCase.getUri();
Parser<GherkinDocument> gherkinParser = new Parser<>(new AstBuilder());
try {
GherkinDocument gherkinDocument = gherkinParser.parse(new String(Files.readAllBytes(Paths.get(this.getClass().getClassLoader()
.getResource(uri.toString().replace("classpath:", "")).toURI()))));
parseExamples(uri, gherkinDocument);
} catch (URISyntaxException | IOException e) {
log.error(e.getMessage());
}
}
if (event.getTestStep() instanceof PickleStepTestStep) {
StepStorage.startStep();
}
}
private void parseExamples(URI uri, GherkinDocument gherkinDocument) {
Feature feature = gherkinDocument.getFeature();
List<ScenarioDefinition> children = feature.getChildren();
for (ScenarioDefinition child : children) {
if (child instanceof ScenarioOutline) {
ScenarioOutline scenario = (ScenarioOutline) child;
for (Examples exampleItem : scenario.getExamples()) {
List<String> headers = new ArrayList<>();
exampleItem.getTableHeader().getCells().forEach(h -> headers.add(h.getValue()));
List<TableRow> tableBody = exampleItem.getTableBody();
for (TableRow tableRow : tableBody) {
List<TableCell> cells = tableRow.getCells();
HashMap<String, String> example = new HashMap<>();
for (int k = 0; k < cells.size(); k++) {
String value = cells.get(k).getValue();
example.put(headers.get(k), value);
}
EXAMPLES.put(getHash(uri, (long) tableRow.getLocation().getLine()), example);
}
}
}
}
}
private void testStepFinished(TestStepFinished testStepFinished) {
if (testStepFinished.getTestStep() instanceof PickleStepTestStep) {
PickleStepTestStep step = (PickleStepTestStep) testStepFinished.getTestStep();
String stepText = step.getStep().getKeyWord() + step.getStep().getText();
Result result = testStepFinished.getResult();
switch (result.getStatus()) {
case PASSED:
StepStorage.getCurrentStep()
.action(stepText)
.status(ResultCreateStepsInner.StatusEnum.PASSED);
StepStorage.stopStep();
break;
case SKIPPED:
break;
case PENDING:
break;
case UNDEFINED:
break;
case AMBIGUOUS:
break;
case FAILED:
StepStorage.getCurrentStep()
.action(stepText)
.status(ResultCreateStepsInner.StatusEnum.FAILED)
.addAttachmentsItem(IntegrationUtils.getStacktrace(result.getError()));
StepStorage.stopStep();
break;
case UNUSED:
break;
}
}
}
private void testRunFinished(TestRunFinished testRunFinished) {
getQaseTestCaseListener().onTestCasesSetFinished();
}
private void testCaseStarted(TestCaseStarted event) {
getQaseTestCaseListener().onTestCaseStarted();
}
private void testCaseFinished(TestCaseFinished event) {
getQaseTestCaseListener().onTestCaseFinished(resultCreate -> setupResultItem(resultCreate, event));
}
private void setupResultItem(ResultCreate resultCreate, TestCaseFinished event) {
TestCase testCase = event.getTestCase();
List<String> tags = testCase.getTags();
Long caseId = CucumberUtils.getCaseId(tags);
String caseTitle = null;
if (caseId == null) {
caseTitle = testCase.getName();
}
StatusEnum status = convertStatus(event.getResult().getStatus());
Optional<Throwable> optionalThrowable = Optional.ofNullable(event.getResult().getError());
String comment = optionalThrowable
.flatMap(throwable -> Optional.of(throwable.toString())).orElse(null);
Boolean isDefect = optionalThrowable
.flatMap(throwable -> Optional.of(throwable instanceof AssertionError))
.orElse(false);
String stacktrace = optionalThrowable
.flatMap(throwable -> Optional.of(getStacktrace(throwable))).orElse(null);
LinkedList<ResultCreateStepsInner> steps = StepStorage.stopSteps();
resultCreate
._case(caseTitle == null ? null : new ResultCreateCase().title(caseTitle))
.caseId(caseId)
.status(status)
.comment(comment)
.stacktrace(stacktrace)
.steps(steps.isEmpty() ? null : steps)
.defect(isDefect);
Map<String, String> params = EXAMPLES.get(getHash(testCase.getUri(), (long) testCase.getLine()));
resultCreate.param(params);
}
private StatusEnum convertStatus(Status status) {
switch (status) {
case FAILED:
return StatusEnum.FAILED;
case PASSED:
return StatusEnum.PASSED;
case PENDING:
case SKIPPED:
case AMBIGUOUS:
case UNDEFINED:
case UNUSED:
default:
return StatusEnum.SKIPPED;
}
}
private static QaseTestCaseListener createQaseListener() {
return Cucumber5Module.getInjector().getInstance(QaseTestCaseListener.class);
}
}