|
33 | 33 | import org.springframework.http.ResponseEntity; |
34 | 34 | import org.springframework.http.client.BufferingClientHttpRequestFactory; |
35 | 35 | import org.springframework.http.client.SimpleClientHttpRequestFactory; |
| 36 | +import org.springframework.web.client.RestClient; |
36 | 37 | import org.springframework.web.client.RestTemplate; |
37 | 38 |
|
38 | 39 | import java.util.Collection; |
39 | 40 | import java.util.Collections; |
40 | 41 | import java.util.Objects; |
| 42 | +import java.util.concurrent.atomic.AtomicReference; |
41 | 43 | import java.util.stream.Stream; |
42 | 44 |
|
43 | 45 | import static io.qameta.allure.test.RunUtils.runWithinTestContext; |
|
49 | 51 | @SuppressWarnings("unchecked") |
50 | 52 | public class AllureRestTemplateTest { |
51 | 53 |
|
52 | | - static Stream<String> attachmentNameProvider() { |
53 | | - return Stream.of("Request", "Response"); |
| 54 | + static Stream<SpringClientType> clientTypeProvider() { |
| 55 | + return Stream.of(SpringClientType.values()); |
54 | 56 | } |
55 | 57 |
|
56 | 58 | @ParameterizedTest |
57 | | - @MethodSource(value = "attachmentNameProvider") |
58 | | - void shouldCreateAttachment(final String attachmentName) { |
59 | | - final AllureResults results = execute(); |
| 59 | + @MethodSource("clientTypeProvider") |
| 60 | + void shouldCreateAttachment(final SpringClientType clientType) { |
| 61 | + final AllureResults results = execute(clientType).getAllureResults(); |
60 | 62 | assertThat(results.getTestResults()) |
61 | 63 | .flatExtracting(TestResult::getAttachments) |
62 | 64 | .flatExtracting(Attachment::getName) |
63 | | - .contains(attachmentName); |
| 65 | + .contains("Request", "Response"); |
64 | 66 | } |
65 | 67 |
|
66 | 68 | @ParameterizedTest |
67 | | - @MethodSource(value = "attachmentNameProvider") |
68 | | - void shouldCatchAttachmentBody(final String attachmentName) { |
69 | | - final AllureResults results = execute(); |
| 69 | + @MethodSource("clientTypeProvider") |
| 70 | + void shouldCatchAttachmentBody(final SpringClientType clientType) { |
| 71 | + final AllureResults results = execute(clientType).getAllureResults(); |
70 | 72 |
|
71 | | - final Attachment found = results.getTestResults().stream() |
72 | | - .map(TestResult::getAttachments) |
73 | | - .flatMap(Collection::stream) |
74 | | - .filter(attachment -> Objects.equals(attachmentName, attachment.getName())) |
75 | | - .findAny() |
76 | | - .orElseThrow(() -> new RuntimeException("attachment not found")); |
77 | | - |
78 | | - assertThat(results.getAttachments()) |
79 | | - .containsKeys(found.getSource()); |
| 73 | + Stream.of("Request", "Response") |
| 74 | + .map(attachmentName -> findAttachment(results, attachmentName)) |
| 75 | + .forEach(found -> assertThat(results.getAttachments()) |
| 76 | + .containsKeys(found.getSource())); |
80 | 77 | } |
81 | 78 |
|
82 | | - protected final AllureResults execute() { |
83 | | - final RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())); |
84 | | - restTemplate.setInterceptors(Collections.singletonList(new AllureRestTemplate())); |
| 79 | + @ParameterizedTest |
| 80 | + @MethodSource("clientTypeProvider") |
| 81 | + void shouldAllowResponseBodyConsumptionAfterInterception(final SpringClientType clientType) { |
| 82 | + final ExecutionResult executionResult = execute(clientType); |
| 83 | + assertThat(executionResult.getResponse().getBody()).isEqualTo("some body"); |
| 84 | + } |
85 | 85 |
|
| 86 | + protected final ExecutionResult execute(final SpringClientType clientType) { |
86 | 87 | final WireMockServer server = new WireMockServer(WireMockConfiguration.options().dynamicPort()); |
| 88 | + final AtomicReference<ResponseEntity<String>> response = new AtomicReference<>(); |
87 | 89 |
|
88 | | - return runWithinTestContext(() -> { |
| 90 | + final AllureResults results = runWithinTestContext(() -> { |
89 | 91 | server.start(); |
90 | 92 | WireMock.configureFor(server.port()); |
91 | 93 | WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/hello")).willReturn(WireMock.aResponse().withBody("some body"))); |
92 | 94 | try { |
93 | | - HttpHeaders headers = new HttpHeaders(); |
94 | | - headers.setContentType(MediaType.APPLICATION_JSON); |
95 | | - HttpEntity<JsonNode> entity = new HttpEntity<>(headers); |
96 | | - ResponseEntity<String> result = restTemplate.exchange(server.url("/hello"), HttpMethod.GET, entity, String.class); |
| 95 | + final ResponseEntity<String> result = clientType.execute(server.url("/hello")); |
| 96 | + response.set(result); |
97 | 97 | Assertions.assertEquals(result.getStatusCode(), HttpStatus.OK); |
| 98 | + Assertions.assertEquals(result.getBody(), "some body"); |
98 | 99 | } finally { |
99 | 100 | server.stop(); |
100 | 101 | } |
101 | 102 | }); |
| 103 | + |
| 104 | + return new ExecutionResult(results, response.get()); |
| 105 | + } |
| 106 | + |
| 107 | + private static Attachment findAttachment(final AllureResults results, final String attachmentName) { |
| 108 | + return results.getTestResults().stream() |
| 109 | + .map(TestResult::getAttachments) |
| 110 | + .flatMap(Collection::stream) |
| 111 | + .filter(attachment -> Objects.equals(attachmentName, attachment.getName())) |
| 112 | + .findAny() |
| 113 | + .orElseThrow(() -> new RuntimeException("attachment not found")); |
| 114 | + } |
| 115 | + |
| 116 | + private static BufferingClientHttpRequestFactory createBufferingRequestFactory() { |
| 117 | + return new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()); |
| 118 | + } |
| 119 | + |
| 120 | + private enum SpringClientType { |
| 121 | + REST_TEMPLATE { |
| 122 | + @Override |
| 123 | + ResponseEntity<String> execute(final String url) { |
| 124 | + final RestTemplate restTemplate = new RestTemplate(createBufferingRequestFactory()); |
| 125 | + restTemplate.setInterceptors(Collections.singletonList(new AllureRestTemplate())); |
| 126 | + |
| 127 | + final HttpHeaders headers = new HttpHeaders(); |
| 128 | + headers.setContentType(MediaType.APPLICATION_JSON); |
| 129 | + final HttpEntity<JsonNode> entity = new HttpEntity<>(headers); |
| 130 | + return restTemplate.exchange(url, HttpMethod.GET, entity, String.class); |
| 131 | + } |
| 132 | + }, |
| 133 | + REST_CLIENT { |
| 134 | + @Override |
| 135 | + ResponseEntity<String> execute(final String url) { |
| 136 | + final RestClient restClient = RestClient.builder() |
| 137 | + .requestFactory(createBufferingRequestFactory()) |
| 138 | + .requestInterceptor(new AllureRestTemplate()) |
| 139 | + .build(); |
| 140 | + return restClient.get() |
| 141 | + .uri(url) |
| 142 | + .accept(MediaType.APPLICATION_JSON) |
| 143 | + .retrieve() |
| 144 | + .toEntity(String.class); |
| 145 | + } |
| 146 | + }; |
| 147 | + |
| 148 | + abstract ResponseEntity<String> execute(String url); |
| 149 | + } |
| 150 | + |
| 151 | + private static final class ExecutionResult { |
| 152 | + |
| 153 | + private final AllureResults allureResults; |
| 154 | + private final ResponseEntity<String> response; |
| 155 | + |
| 156 | + private ExecutionResult(final AllureResults allureResults, final ResponseEntity<String> response) { |
| 157 | + this.allureResults = allureResults; |
| 158 | + this.response = response; |
| 159 | + } |
| 160 | + |
| 161 | + AllureResults getAllureResults() { |
| 162 | + return allureResults; |
| 163 | + } |
| 164 | + |
| 165 | + ResponseEntity<String> getResponse() { |
| 166 | + return response; |
| 167 | + } |
102 | 168 | } |
103 | 169 | } |
0 commit comments