|
20 | 20 |
|
21 | 21 | import java.net.URI;
|
22 | 22 | import java.text.SimpleDateFormat;
|
| 23 | +import java.util.Collections; |
| 24 | +import java.util.LinkedHashMap; |
| 25 | +import java.util.Map; |
23 | 26 | import java.util.UUID;
|
24 | 27 |
|
25 | 28 | import org.junit.jupiter.api.BeforeEach;
|
26 | 29 | import org.junit.jupiter.api.Test;
|
27 | 30 |
|
28 | 31 | import org.springframework.boot.SpringApplication;
|
29 | 32 | import org.springframework.boot.test.web.client.TestRestTemplate;
|
| 33 | +import org.springframework.cloud.function.cloudevent.CloudEventAttributesProvider; |
30 | 34 | import org.springframework.cloud.function.cloudevent.CloudEventJsonMessageConverter;
|
| 35 | +import org.springframework.cloud.function.cloudevent.CloudEventMessageUtils; |
| 36 | +import org.springframework.cloud.function.cloudevent.DefaultCloudEventAttributesProvider; |
31 | 37 | import org.springframework.cloud.function.json.JsonMapper;
|
32 | 38 | import org.springframework.context.annotation.Bean;
|
33 | 39 | import org.springframework.context.annotation.Configuration;
|
|
42 | 48 | import org.springframework.messaging.converter.AbstractMessageConverter;
|
43 | 49 | import org.springframework.messaging.converter.MessageConverter;
|
44 | 50 | import org.springframework.util.MimeType;
|
| 51 | +import org.springframework.util.MimeTypeUtils; |
45 | 52 | import org.springframework.util.SocketUtils;
|
46 | 53 |
|
47 | 54 | /**
|
@@ -200,6 +207,53 @@ public void testAsStracturalFormatToString() throws Exception {
|
200 | 207 | assertThat(response.getBody()).isEqualTo("{\"version\":\"1.0\",\"releaseName\":\"Spring Framework\",\"releaseDate\":\"24-03-2004\"}");
|
201 | 208 | }
|
202 | 209 |
|
| 210 | + @Test |
| 211 | + public void testAsBinaryMapToMap() throws Exception { |
| 212 | + SpringApplication.run(new Class[] {CloudeventDemoApplication.class}, new String[] {}); |
| 213 | + |
| 214 | + HttpHeaders headers = this.buildHeaders(MediaType.APPLICATION_JSON); |
| 215 | + String payload = "{\"releaseDate\":\"24-03-2004\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}"; |
| 216 | + |
| 217 | + RequestEntity<String> re = new RequestEntity<>(payload, headers, HttpMethod.POST, this.constructURI("/consumeAndProduceCloudEventAsMapToMap")); |
| 218 | + ResponseEntity<String> response = testRestTemplate.exchange(re, String.class); |
| 219 | + |
| 220 | + assertThat(response.getBody()).isEqualTo("{\"releaseDate\":\"01-10-2050\",\"releaseName\":\"Spring Framework\",\"version\":\"10.0\"}"); |
| 221 | + assertThat(response.getHeaders().get(CloudEventMessageUtils.CE_SOURCE)) |
| 222 | + .isEqualTo(Collections.singletonList("http://spring.io/application-application")); |
| 223 | + assertThat(response.getHeaders().get(CloudEventMessageUtils.CE_TYPE)) |
| 224 | + .isEqualTo(Collections.singletonList(LinkedHashMap.class.getName())); |
| 225 | + } |
| 226 | + |
| 227 | + @Test |
| 228 | + public void testAsBinaryPojoToPojo() throws Exception { |
| 229 | + SpringApplication.run(new Class[] {CloudeventDemoApplication.class}, new String[] {}); |
| 230 | + |
| 231 | + HttpHeaders headers = this.buildHeaders(MediaType.APPLICATION_JSON); |
| 232 | + String payload = "{\"releaseDate\":\"01-10-2006\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}"; |
| 233 | + |
| 234 | + RequestEntity<String> re = new RequestEntity<>(payload, headers, HttpMethod.POST, this.constructURI("/consumeAndProduceCloudEventAsPojoToPojo")); |
| 235 | + ResponseEntity<String> response = testRestTemplate.exchange(re, String.class); |
| 236 | + |
| 237 | + assertThat(response.getBody()).isEqualTo("{\"releaseDate\":\"01-10-2006\",\"releaseName\":\"Spring Framework\",\"version\":\"2.0\"}"); |
| 238 | + assertThat(response.getHeaders().get(CloudEventMessageUtils.CE_SOURCE)) |
| 239 | + .isEqualTo(Collections.singletonList("http://spring.io/application-application")); |
| 240 | + assertThat(response.getHeaders().get(CloudEventMessageUtils.CE_TYPE)) |
| 241 | + .isEqualTo(Collections.singletonList(SpringReleaseEvent.class.getName())); |
| 242 | + } |
| 243 | + |
| 244 | + private URI constructURI(String path) throws Exception { |
| 245 | + return new URI("http://localhost:" + System.getProperty("server.port") + path); |
| 246 | + } |
| 247 | + |
| 248 | + private HttpHeaders buildHeaders(MediaType contentType) { |
| 249 | + HttpHeaders headers = new HttpHeaders(); |
| 250 | + headers.setContentType(contentType); |
| 251 | + headers.set(CloudEventMessageUtils.CE_ID, UUID.randomUUID().toString()); |
| 252 | + headers.set(CloudEventMessageUtils.CE_SOURCE, "https://spring.io/"); |
| 253 | + headers.set(CloudEventMessageUtils.CE_SPECVERSION, "1.0"); |
| 254 | + headers.set(CloudEventMessageUtils.CE_TYPE, "org.springframework"); |
| 255 | + return headers; |
| 256 | + } |
203 | 257 |
|
204 | 258 | @Configuration
|
205 | 259 | public static class FooBarConverterConfiguration {
|
@@ -270,18 +324,4 @@ protected Object convertToInternal(Object payload, @Nullable MessageHeaders head
|
270 | 324 | }
|
271 | 325 | }
|
272 | 326 |
|
273 |
| - private URI constructURI(String path) throws Exception { |
274 |
| - return new URI("http://localhost:" + System.getProperty("server.port") + path); |
275 |
| - } |
276 |
| - |
277 |
| - private HttpHeaders buildHeaders(MediaType contentType) { |
278 |
| - HttpHeaders headers = new HttpHeaders(); |
279 |
| - headers.setContentType(contentType); |
280 |
| - headers.set("id", UUID.randomUUID().toString()); |
281 |
| - headers.set("source", "https://spring.io/"); |
282 |
| - headers.set("specversion", "1.0"); |
283 |
| - headers.set("type", "org.springframework"); |
284 |
| - return headers; |
285 |
| - } |
286 |
| - |
287 | 327 | }
|
0 commit comments