-
Notifications
You must be signed in to change notification settings - Fork 6
/
QaseEventListener.java
203 lines (185 loc) · 8.59 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
195
196
197
198
199
200
201
202
203
package io.qase.cucumber7;
import io.cucumber.gherkin.GherkinParser;
import io.cucumber.messages.types.*;
import io.cucumber.plugin.ConcurrentEventListener;
import io.cucumber.plugin.event.TestCase;
import io.cucumber.plugin.event.TestCaseFinished;
import io.cucumber.plugin.event.TestCaseStarted;
import io.cucumber.plugin.event.TestRunFinished;
import io.cucumber.plugin.event.TestStepFinished;
import io.cucumber.plugin.event.TestStepStarted;
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.cucumber7.guice.module.Cucumber7Module;
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.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Stream;
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 7-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::testStepStarted);
}
}
private void parseExamples(URI uri, Envelope envelope) {
envelope.getGherkinDocument()
.flatMap(GherkinDocument::getFeature)
.ifPresent(feature -> {
for (int i = 0; i < feature.getChildren().size(); i++) {
Scenario scenario = feature.getChildren().get(i).getScenario().get();
for (Examples exampleItem : scenario.getExamples()) {
List<String> headers = new ArrayList<>();
exampleItem.getTableHeader()
.ifPresent(headerCell ->
headerCell.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, tableRow.getLocation().getLine()), example);
}
}
}
});
}
private void testStepStarted(TestStepStarted testStepStarted) {
if (testStepStarted.getTestStep() instanceof PickleStepTestStep) {
StepStorage.startStep();
}
}
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) {
if (EXAMPLES.get(getHash(event.getTestCase().getUri(), (long) event.getTestCase().getLocation().getLine())) == null) {
TestCase testCase = event.getTestCase();
URI uri = testCase.getUri();
GherkinParser gherkinParser = GherkinParser.builder().build();
try {
Path path = Paths.get(this.getClass().getClassLoader()
.getResource(uri.toString().replace("classpath:", "")).toURI());
Stream<Envelope> envelopes = gherkinParser.parse(path);
envelopes.forEach(e -> parseExamples(uri, e));
} catch (IOException | URISyntaxException e) {
log.error(e.getMessage());
}
}
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.getLocation().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 Cucumber7Module.getInjector().getInstance(QaseTestCaseListener.class);
}
}