|
1 | 1 | package io.cloudevents.examples.spring; |
2 | 2 |
|
3 | | -import java.net.URI; |
4 | | - |
| 3 | +import org.junit.jupiter.api.BeforeEach; |
5 | 4 | import org.junit.jupiter.api.Test; |
6 | | - |
7 | | -import org.springframework.beans.factory.annotation.Autowired; |
8 | 5 | import org.springframework.boot.test.context.SpringBootTest; |
9 | 6 | import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
10 | | -import org.springframework.boot.test.web.client.TestRestTemplate; |
11 | | -import org.springframework.boot.web.server.LocalServerPort; |
12 | | -import org.springframework.http.HttpHeaders; |
| 7 | +import org.springframework.boot.test.web.server.LocalServerPort; |
13 | 8 | import org.springframework.http.HttpStatus; |
14 | 9 | import org.springframework.http.MediaType; |
15 | | -import org.springframework.http.RequestEntity; |
16 | | -import org.springframework.http.ResponseEntity; |
| 10 | +import org.springframework.test.web.servlet.client.ExchangeResult; |
| 11 | +import org.springframework.test.web.servlet.client.RestTestClient; |
| 12 | + |
| 13 | +import java.util.Map; |
17 | 14 |
|
18 | 15 | import static org.assertj.core.api.Assertions.assertThat; |
19 | 16 |
|
20 | 17 | @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) |
21 | | -public class DemoApplicationTests { |
22 | | - |
23 | | - @Autowired |
24 | | - private TestRestTemplate rest; |
| 18 | +class DemoApplicationTests { |
| 19 | + String NEW_BODY = "{\"data\": {\"value\": \"Dave\" }}"; |
| 20 | + private static final String BODY = "{\"value\":\"Dave\"}"; |
| 21 | + private RestTestClient rest; |
25 | 22 |
|
26 | 23 | @LocalServerPort |
27 | 24 | private int port; |
28 | 25 |
|
| 26 | + @BeforeEach |
| 27 | + void setUp() { |
| 28 | + rest = RestTestClient |
| 29 | + .bindToServer() |
| 30 | + .baseUrl(String.format("http://localhost:%d", port)) |
| 31 | + .build(); |
| 32 | + } |
| 33 | + |
29 | 34 | @Test |
30 | 35 | void echoWithCorrectHeaders() { |
31 | | - |
32 | | - ResponseEntity<String> response = rest |
33 | | - .exchange(RequestEntity.post(URI.create("http://localhost:" + port + "/foos")) // |
34 | | - .header("ce-id", "12345") // |
35 | | - .header("ce-specversion", "1.0") // |
36 | | - .header("ce-type", "io.spring.event") // |
37 | | - .header("ce-source", "https://spring.io/events") // |
38 | | - .contentType(MediaType.APPLICATION_JSON) // |
39 | | - .body("{\"value\":\"Dave\"}"), String.class); |
40 | | - |
41 | | - assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
42 | | - assertThat(response.getBody()).isEqualTo("{\"value\":\"Dave\"}"); |
43 | | - |
44 | | - HttpHeaders headers = response.getHeaders(); |
45 | | - |
46 | | - assertThat(headers).containsKey("ce-id"); |
47 | | - assertThat(headers).containsKey("ce-source"); |
48 | | - assertThat(headers).containsKey("ce-type"); |
49 | | - |
50 | | - assertThat(headers.getFirst("ce-id")).isNotEqualTo("12345"); |
51 | | - assertThat(headers.getFirst("ce-type")).isEqualTo("io.spring.event.Foo"); |
52 | | - assertThat(headers.getFirst("ce-source")).isEqualTo("https://spring.io/foos"); |
53 | | - |
| 36 | + ExchangeResult response = rest.post() |
| 37 | + .uri("/foos") |
| 38 | + .header("ce-id", "12345") |
| 39 | + .header("ce-specversion", "1.0") |
| 40 | + .header("ce-type", "io.spring.event") |
| 41 | + .header("ce-source", "https://spring.io/events") |
| 42 | + .contentType(MediaType.APPLICATION_JSON) |
| 43 | + .body(NEW_BODY) |
| 44 | + .exchange() |
| 45 | + .returnResult(String.class); |
| 46 | + |
| 47 | + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK); |
| 48 | + assertThat(response.getResponseBodyContent()).isEqualTo(BODY.getBytes()); |
| 49 | + |
| 50 | + Map<String, String> headers = response.getResponseHeaders().toSingleValueMap(); |
| 51 | + |
| 52 | + assertThat(headers) |
| 53 | + .containsKey("ce-id") |
| 54 | + .containsKey("ce-source") |
| 55 | + .containsKey("ce-type") |
| 56 | + .doesNotContainEntry("ce-id", "12345") |
| 57 | + .containsEntry("ce-type", "io.spring.event.Foo") |
| 58 | + .containsEntry("ce-source", "https://spring.io/foos"); |
54 | 59 | } |
55 | 60 |
|
56 | 61 | @Test |
57 | 62 | void structuredRequestResponseEvents() { |
58 | | - |
59 | | - ResponseEntity<String> response = rest |
60 | | - .exchange(RequestEntity.post(URI.create("http://localhost:" + port + "/event")) // |
61 | | - .contentType(new MediaType("application", "cloudevents+json")) // |
62 | | - .body("{" // |
63 | | - + "\"id\":\"12345\"," // |
64 | | - + "\"specversion\":\"1.0\"," // |
65 | | - + "\"type\":\"io.spring.event\"," // |
66 | | - + "\"source\":\"https://spring.io/events\"," // |
67 | | - + "\"data\":{\"value\":\"Dave\"}}"), |
68 | | - String.class); |
69 | | - |
70 | | - assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
71 | | - assertThat(response.getBody()).isEqualTo("{\"value\":\"Dave\"}"); |
72 | | - |
73 | | - HttpHeaders headers = response.getHeaders(); |
74 | | - |
75 | | - assertThat(headers).containsKey("ce-id"); |
76 | | - assertThat(headers).containsKey("ce-source"); |
77 | | - assertThat(headers).containsKey("ce-type"); |
78 | | - |
79 | | - assertThat(headers.getFirst("ce-id")).isNotEqualTo("12345"); |
80 | | - assertThat(headers.getFirst("ce-type")).isEqualTo("io.spring.event.Foo"); |
81 | | - assertThat(headers.getFirst("ce-source")).isEqualTo("https://spring.io/foos"); |
82 | | - |
| 63 | + ExchangeResult response = rest.post() |
| 64 | + .uri("/event") |
| 65 | + .contentType(new MediaType("application", "cloudevents+json")) |
| 66 | + .body(String.format( |
| 67 | + """ |
| 68 | + { |
| 69 | + "id": "12345", |
| 70 | + "specversion": "1.0", |
| 71 | + "type": "io.spring.event", |
| 72 | + "source": "https://spring.io/events", |
| 73 | + "data": %s |
| 74 | + } |
| 75 | + """, BODY |
| 76 | + )) |
| 77 | + .exchange() |
| 78 | + .returnResult(String.class); |
| 79 | + |
| 80 | + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK); |
| 81 | + assertThat(response.getResponseBodyContent()).isEqualTo(BODY.getBytes()); |
| 82 | + |
| 83 | + Map<String, String> headers = response.getResponseHeaders().toSingleValueMap(); |
| 84 | + |
| 85 | + assertThat(headers) |
| 86 | + .containsKey("ce-id") |
| 87 | + .containsKey("ce-source") |
| 88 | + .containsKey("ce-type") |
| 89 | + .doesNotContainEntry("ce-id", "12345") |
| 90 | + .containsEntry("ce-type", "io.spring.event.Foo") |
| 91 | + .containsEntry("ce-source", "https://spring.io/foos"); |
83 | 92 | } |
84 | 93 |
|
85 | 94 | @Test |
86 | 95 | void requestResponseEvents() { |
87 | | - |
88 | | - ResponseEntity<String> response = rest |
89 | | - .exchange(RequestEntity.post(URI.create("http://localhost:" + port + "/event")) // |
90 | | - .header("ce-id", "12345") // |
91 | | - .header("ce-specversion", "1.0") // |
92 | | - .header("ce-type", "io.spring.event") // |
93 | | - .header("ce-source", "https://spring.io/events") // |
94 | | - .contentType(MediaType.APPLICATION_JSON) // |
95 | | - .body("{\"value\":\"Dave\"}"), String.class); |
96 | | - |
97 | | - assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
98 | | - assertThat(response.getBody()).isEqualTo("{\"value\":\"Dave\"}"); |
99 | | - |
100 | | - HttpHeaders headers = response.getHeaders(); |
101 | | - |
102 | | - assertThat(headers).containsKey("ce-id"); |
103 | | - assertThat(headers).containsKey("ce-source"); |
104 | | - assertThat(headers).containsKey("ce-type"); |
105 | | - |
106 | | - assertThat(headers.getFirst("ce-id")).isNotEqualTo("12345"); |
107 | | - assertThat(headers.getFirst("ce-type")).isEqualTo("io.spring.event.Foo"); |
108 | | - assertThat(headers.getFirst("ce-source")).isEqualTo("https://spring.io/foos"); |
109 | | - |
| 96 | + ExchangeResult response = rest.post() |
| 97 | + .uri("/event") |
| 98 | + .header("ce-id", "12345") |
| 99 | + .header("ce-specversion", "1.0") |
| 100 | + .header("ce-type", "io.spring.event") |
| 101 | + .header("ce-source", "https://spring.io/events") |
| 102 | + .contentType(MediaType.APPLICATION_JSON) |
| 103 | + .body(BODY) |
| 104 | + .exchange() |
| 105 | + .returnResult(String.class); |
| 106 | + |
| 107 | + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK); |
| 108 | + assertThat(response.getResponseBodyContent()).isEqualTo(BODY.getBytes()); |
| 109 | + |
| 110 | + Map<String, String> headers = response.getResponseHeaders().toSingleValueMap(); |
| 111 | + |
| 112 | + assertThat(headers) |
| 113 | + .containsKey("ce-id") |
| 114 | + .containsKey("ce-source") |
| 115 | + .containsKey("ce-type") |
| 116 | + .doesNotContainEntry("ce-id", "12345") |
| 117 | + .containsEntry("ce-type", "io.spring.event.Foo") |
| 118 | + .containsEntry("ce-source", "https://spring.io/foos"); |
110 | 119 | } |
111 | 120 |
|
112 | 121 | } |
0 commit comments