-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathAllureTestCommonsUtils.java
More file actions
214 lines (194 loc) · 7.91 KB
/
Copy pathAllureTestCommonsUtils.java
File metadata and controls
214 lines (194 loc) · 7.91 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
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
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright 2016-2026 Qameta Software Inc
*
* 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.test;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import io.qameta.allure.Allure;
import io.qameta.allure.AllureConstants;
import io.qameta.allure.AttachmentOptions;
import io.qameta.allure.model.Parameter;
import io.qameta.allure.model.Stage;
import io.qameta.allure.model.Status;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Stream;
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_DEFAULT;
import static com.fasterxml.jackson.databind.MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME;
import static io.qameta.allure.util.ResultsUtils.md5;
/**
* Provides utility methods for Allure Java test support support.
*
* <p>The methods are stateless helpers intended for integrations, tests, and extension code that need the same conventions as the built-in Allure adapters.</p>
*/
public final class AllureTestCommonsUtils {
private static final String DOT = ".";
private static final String JSON_EXTENSION = "json";
private static final String JSON_TYPE = "application/json";
private static final String TEXT_EXTENSION = "txt";
private static final String TEXT_TYPE = "text/plain";
private static final ObjectWriter WRITER = JsonMapper
.builder()
.configure(USE_WRAPPER_NAME_AS_PROPERTY_NAME, true)
.serializationInclusion(NON_DEFAULT)
.build()
.registerModule(
new SimpleModule()
.addSerializer(Status.class, new StatusSerializer())
.addSerializer(Stage.class, new StageSerializer())
.addSerializer(Parameter.Mode.class, new ParameterModeSerializer())
)
.writerWithDefaultPrettyPrinter();
private AllureTestCommonsUtils() {
throw new IllegalStateException("do not instance");
}
/**
* Attach {@link AllureResults} to the report.
*/
public static void attach(final AllureResults allureResults) {
allureResults.getTestResults().forEach(testResult -> {
try {
Allure.attachment(
testResult.getUuid() + AllureConstants.TEST_RESULT_FILE_SUFFIX,
JSON_TYPE,
WRITER.writeValueAsString(testResult)
);
} catch (JsonProcessingException e) {
throw new UncheckedIOException(e);
}
});
allureResults.getTestResultContainers().forEach(container -> {
try {
Allure.attachment(
container.getUuid() + AllureConstants.TEST_RESULT_CONTAINER_FILE_SUFFIX,
JSON_TYPE,
WRITER.writeValueAsString(container)
);
} catch (JsonProcessingException e) {
throw new UncheckedIOException(e);
}
});
allureResults.getAttachments().forEach(
(fileName, body) -> Allure
.attachment(
fileName,
type(fileName),
new ByteArrayInputStream(body),
attachmentOptions(fileName)
)
);
}
/**
* Calculates the compatibility history id expected in adapter tests.
*
* @param testCaseId the test case id
* @param parameters the final parameters
* @return the expected history id
*/
public static String expectedHistoryId(final String testCaseId, final List<Parameter> parameters) {
final StringBuilder source = new StringBuilder(testCaseId);
final Stream<Parameter> parameterStream = Objects.isNull(parameters) ? Stream.empty() : parameters.stream();
parameterStream
.filter(Objects::nonNull)
.filter(parameter -> !Boolean.TRUE.equals(parameter.getExcluded()))
.sorted(
Comparator.comparing((Parameter parameter) -> Objects.toString(parameter.getName(), ""))
.thenComparing(parameter -> Objects.toString(parameter.getValue(), ""))
)
.forEachOrdered(
parameter -> source
.append(Objects.toString(parameter.getName(), ""))
.append(Objects.toString(parameter.getValue(), ""))
);
return md5(source.toString());
}
private static AttachmentOptions attachmentOptions(final String fileName) {
if (fileName.endsWith(DOT + JSON_EXTENSION) || fileName.endsWith(DOT + TEXT_EXTENSION)) {
return AttachmentOptions.empty();
}
return AttachmentOptions.withFileExtension(extension(fileName));
}
private static String type(final String fileName) {
if (fileName.endsWith(DOT + JSON_EXTENSION)) {
return JSON_TYPE;
}
if (fileName.endsWith(DOT + TEXT_EXTENSION)) {
return TEXT_TYPE;
}
return null;
}
private static String extension(final String fileName) {
final int index = fileName.lastIndexOf('.');
if (index < 0 || index == fileName.length() - 1) {
return null;
}
return fileName.substring(index + 1);
}
/**
* Parameter mode serializer.
*/
private static class ParameterModeSerializer extends StdSerializer<Parameter.Mode> {
protected ParameterModeSerializer() {
super(Parameter.Mode.class);
}
@Override
public void serialize(final Parameter.Mode value,
final JsonGenerator gen,
final SerializerProvider provider)
throws IOException {
gen.writeString(value.name().toLowerCase(Locale.ENGLISH));
}
}
/**
* Stage serializer.
*/
private static class StageSerializer extends StdSerializer<Stage> {
protected StageSerializer() {
super(Stage.class);
}
@Override
public void serialize(final Stage value,
final JsonGenerator gen,
final SerializerProvider provider)
throws IOException {
gen.writeString(value.name().toLowerCase(Locale.ENGLISH));
}
}
/**
* Status serializer.
*/
private static class StatusSerializer extends StdSerializer<Status> {
protected StatusSerializer() {
super(Status.class);
}
@Override
public void serialize(final Status value,
final JsonGenerator gen,
final SerializerProvider provider)
throws IOException {
gen.writeString(value.name().toLowerCase(Locale.ENGLISH));
}
}
}