forked from allure-framework/allure-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllureResponseDecoderTests.java
More file actions
108 lines (87 loc) · 3.77 KB
/
AllureResponseDecoderTests.java
File metadata and controls
108 lines (87 loc) · 3.77 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
/*
* Copyright 2016-2024 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.openfeign;
import com.github.tomakehurst.wiremock.WireMockServer;
import feign.Feign;
import feign.RequestLine;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import io.qameta.allure.model.Attachment;
import io.qameta.allure.test.AllureResults;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import static io.qameta.allure.test.RunUtils.runWithinTestContext;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class AllureResponseDecoderTests {
static WireMockServer wireMockServer;
@BeforeAll
static void setUp() {
wireMockServer = new WireMockServer(options().dynamicPort());
wireMockServer.start();
wireMockServer.stubFor(
get(urlEqualTo("/api/v1/json"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("{\"message\":\"Hello World\"}")));
}
@Test
void jsonBodyTest() {
AtomicReference<HelloWorldRecord> helloWorldRecord = new AtomicReference<>();
AllureResults allureResults = runWithinTestContext(() -> {
helloWorldRecord.set(Feign.builder()
.decoder(new AllureResponseDecoder(new GsonDecoder()))
.encoder(new GsonEncoder())
.target(HelloWorldFeignClient.class, wireMockServer.baseUrl())
.getJsonHelloWorld());
});
List<String> attachmentNames = allureResults.getTestResults().stream()
.flatMap(testResult -> testResult.getAttachments().stream())
.map(Attachment::getName).collect(Collectors.toList());
assertAll(
() -> assertEquals(new HelloWorldRecord("Hello World").getMessage(), helloWorldRecord.get().getMessage()),
() -> assertTrue(attachmentNames.contains("Response"), "Cannot find attachment with name \"Response\""),
() -> assertTrue(attachmentNames.contains("Request"), "Cannot find attachment with name \"Request\"")
);
}
interface HelloWorldFeignClient {
@RequestLine("GET /api/v1/json")
HelloWorldRecord getJsonHelloWorld();
}
static class HelloWorldRecord {
private String message;
public HelloWorldRecord() {
}
public HelloWorldRecord(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
}