|
9 | 9 | import static org.springframework.cloud.function.cloudevent.CloudEventMessageUtils.SUBJECT; |
10 | 10 | import static org.springframework.cloud.function.cloudevent.CloudEventMessageUtils.TYPE; |
11 | 11 |
|
12 | | -import java.net.URI; |
13 | 12 | import java.util.UUID; |
14 | 13 | import org.junit.jupiter.api.Test; |
15 | | -import org.springframework.beans.factory.annotation.Autowired; |
16 | 14 | import org.springframework.boot.test.context.SpringBootTest; |
17 | 15 | import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
18 | | -import org.springframework.boot.test.web.client.TestRestTemplate; |
19 | | -import org.springframework.http.HttpHeaders; |
| 16 | +import org.springframework.boot.test.web.server.LocalServerPort; |
20 | 17 | import org.springframework.http.MediaType; |
21 | | -import org.springframework.http.RequestEntity; |
22 | | -import org.springframework.http.ResponseEntity; |
| 18 | +import org.springframework.test.web.reactive.server.WebTestClient; |
23 | 19 |
|
24 | 20 | @SpringBootTest(classes = SpringCloudEventsApplication.class, |
25 | 21 | webEnvironment = WebEnvironment.RANDOM_PORT) |
26 | 22 | public class SpringCloudEventsApplicationTests { |
27 | 23 |
|
28 | | - @Autowired |
29 | | - private TestRestTemplate rest; |
| 24 | + @LocalServerPort |
| 25 | + private int port; |
| 26 | + |
| 27 | + private WebTestClient getWebTestClient() { |
| 28 | + return WebTestClient.bindToServer() |
| 29 | + .baseUrl("http://localhost:" + port) |
| 30 | + .build(); |
| 31 | + } |
30 | 32 |
|
31 | 33 | @Test |
32 | 34 | public void testEchoInput() throws Exception { |
33 | 35 |
|
34 | 36 | String input ="hello"; |
35 | 37 |
|
36 | | - HttpHeaders ceHeaders = new HttpHeaders(); |
37 | | - ceHeaders.add(SPECVERSION, "1.0"); |
38 | | - ceHeaders.add(ID, UUID.randomUUID() |
39 | | - .toString()); |
40 | | - ceHeaders.add(TYPE, "echo"); |
41 | | - ceHeaders.add(SOURCE, "http://localhost:8080/echo"); |
42 | | - ceHeaders.add(SUBJECT, "Echo content"); |
43 | | - |
44 | | - ResponseEntity<String> response = this.rest.exchange( |
45 | | - RequestEntity.post(new URI("/echo")) |
46 | | - .contentType(MediaType.APPLICATION_JSON) |
47 | | - .headers(ceHeaders) |
48 | | - .body(input), |
49 | | - String.class); |
50 | | - |
51 | | - assertThat(response.getStatusCode() |
52 | | - .value(), equalTo(200)); |
53 | | - String body = response.getBody(); |
54 | | - assertThat(body, notNullValue()); |
55 | | - assertThat(body, equalTo(input)); |
| 38 | + getWebTestClient().post() |
| 39 | + .uri("/echo") |
| 40 | + .contentType(MediaType.APPLICATION_JSON) |
| 41 | + .header(SPECVERSION, "1.0") |
| 42 | + .header(ID, UUID.randomUUID().toString()) |
| 43 | + .header(TYPE, "echo") |
| 44 | + .header(SOURCE, "http://localhost:8080/echo") |
| 45 | + .header(SUBJECT, "Echo content") |
| 46 | + .bodyValue(input) |
| 47 | + .exchange() |
| 48 | + .expectStatus().isOk() |
| 49 | + .expectBody(String.class) |
| 50 | + .value(body -> { |
| 51 | + assertThat(body, notNullValue()); |
| 52 | + assertThat(body, equalTo(input)); |
| 53 | + }); |
56 | 54 | } |
57 | 55 |
|
58 | 56 | @Test |
59 | 57 | public void testEchoRoutingBasedOnType() throws Exception { |
60 | 58 |
|
61 | 59 | String input ="hello"; |
62 | 60 |
|
63 | | - HttpHeaders ceHeaders = new HttpHeaders(); |
64 | | - ceHeaders.add(SPECVERSION, "1.0"); |
65 | | - ceHeaders.add(ID, UUID.randomUUID() |
66 | | - .toString()); |
67 | | - ceHeaders.add(TYPE, "echo"); |
68 | | - ceHeaders.add(SOURCE, "http://localhost:8080/echo"); |
69 | | - ceHeaders.add(SUBJECT, "Echo content"); |
70 | | - |
71 | | - ResponseEntity<String> response = this.rest.exchange( |
72 | | - RequestEntity.post(new URI("/")) |
| 61 | + getWebTestClient().post() |
| 62 | + .uri("/") |
73 | 63 | .contentType(MediaType.APPLICATION_JSON) |
74 | | - .headers(ceHeaders) |
75 | | - .body(input), |
76 | | - String.class); |
77 | | - |
78 | | - assertThat(response.getStatusCode() |
79 | | - .value(), equalTo(200)); |
80 | | - String body = response.getBody(); |
81 | | - assertThat(body, notNullValue()); |
82 | | - assertThat(body, equalTo(input)); |
| 64 | + .header(SPECVERSION, "1.0") |
| 65 | + .header(ID, UUID.randomUUID().toString()) |
| 66 | + .header(TYPE, "echo") |
| 67 | + .header(SOURCE, "http://localhost:8080/echo") |
| 68 | + .header(SUBJECT, "Echo content") |
| 69 | + .bodyValue(input) |
| 70 | + .exchange() |
| 71 | + .expectStatus().isOk() |
| 72 | + .expectBody(String.class) |
| 73 | + .value(body -> { |
| 74 | + assertThat(body, notNullValue()); |
| 75 | + assertThat(body, equalTo(input)); |
| 76 | + }); |
83 | 77 | } |
84 | 78 | } |
0 commit comments