diff --git a/spring-cloud-function-samples/function-sample-cloudevent/src/main/java/io/spring/cloudevent/CloudeventDemoApplication.java b/spring-cloud-function-samples/function-sample-cloudevent/src/main/java/io/spring/cloudevent/CloudeventDemoApplication.java index 66979034f..291c19001 100644 --- a/spring-cloud-function-samples/function-sample-cloudevent/src/main/java/io/spring/cloudevent/CloudeventDemoApplication.java +++ b/spring-cloud-function-samples/function-sample-cloudevent/src/main/java/io/spring/cloudevent/CloudeventDemoApplication.java @@ -16,6 +16,8 @@ package io.spring.cloudevent; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.Collections; import java.util.Map; import java.util.UUID; @@ -102,19 +104,9 @@ public Function, Message> consum }; } - @Bean - public Function, SpringReleaseEvent> consumeAndProduceCloudEventPojo() { - return ceMessage -> { - SpringReleaseEvent data = ceMessage.getPayload(); - data.setVersion("2.0"); - data.setReleaseDateAsString("01-10-2006"); - - return data; - }; - } @Bean - public Function, Map> consumeAndProduceCloudEventAsPojoToPojo() { + public Function, Map> consumeAndProduceCloudEventAsMapToMap() { return ceMessage -> { ceMessage.put("version", "10.0"); ceMessage.put("releaseDate", "01-10-2050"); @@ -123,22 +115,10 @@ public Function, Map> consumeAndProduceCloud } @Bean - public CloudEventAttributesProvider cloudEventAttributesProvider() { - return new CustomCloudEventAtttributesProvider(); - } - - public static class CustomCloudEventAtttributesProvider extends DefaultCloudEventAttributesProvider { - - @Override - public Map generateDefaultCloudEventHeaders(Message inputMessage, Object result) { - if (inputMessage.getHeaders().containsKey(CloudEventMessageUtils.CE_ID)) { // input is a cloud event - String applicationName = "http://spring.io/fooBar"; - return this.get(inputMessage.getHeaders()) - .setId(UUID.randomUUID().toString()) - .setType(result.getClass().getName()) - .setSource(applicationName); - } - return Collections.emptyMap(); - } + public Function consumeAndProduceCloudEventAsPojoToPojo() { + return event -> { + event.setVersion("2.0"); + return event; + }; } } diff --git a/spring-cloud-function-samples/function-sample-cloudevent/src/test/java/io/spring/cloudevent/CloudeventDemoApplicationFunctionTests.java b/spring-cloud-function-samples/function-sample-cloudevent/src/test/java/io/spring/cloudevent/CloudeventDemoApplicationFunctionTests.java index 2e46b5ef9..a3fef454c 100644 --- a/spring-cloud-function-samples/function-sample-cloudevent/src/test/java/io/spring/cloudevent/CloudeventDemoApplicationFunctionTests.java +++ b/spring-cloud-function-samples/function-sample-cloudevent/src/test/java/io/spring/cloudevent/CloudeventDemoApplicationFunctionTests.java @@ -99,7 +99,7 @@ public void demoPureFunctionProduceConsumeCloudEventAsPojo() { * is (see `asPOJOMessage` and `asPOJO` specifically). Type conversion will happen * inside spring-cloud-function. */ - Function, Message> asPojoMessage = catalog.lookup("consumeAndProduceCloudEventPojo"); + Function, Message> asPojoMessage = catalog.lookup("consumeAndProduceCloudEventAsPojoToPojo"); System.out.println(asPojoMessage.apply(binaryCloudEventMessage)); } } diff --git a/spring-cloud-function-samples/function-sample-cloudevent/src/test/java/io/spring/cloudevent/CloudeventDemoApplicationRESTTests.java b/spring-cloud-function-samples/function-sample-cloudevent/src/test/java/io/spring/cloudevent/CloudeventDemoApplicationRESTTests.java index 31488a1c6..1a9f3b195 100644 --- a/spring-cloud-function-samples/function-sample-cloudevent/src/test/java/io/spring/cloudevent/CloudeventDemoApplicationRESTTests.java +++ b/spring-cloud-function-samples/function-sample-cloudevent/src/test/java/io/spring/cloudevent/CloudeventDemoApplicationRESTTests.java @@ -20,6 +20,9 @@ import java.net.URI; import java.text.SimpleDateFormat; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; import java.util.UUID; import org.junit.jupiter.api.BeforeEach; @@ -27,7 +30,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.cloud.function.cloudevent.CloudEventAttributesProvider; import org.springframework.cloud.function.cloudevent.CloudEventJsonMessageConverter; +import org.springframework.cloud.function.cloudevent.CloudEventMessageUtils; +import org.springframework.cloud.function.cloudevent.DefaultCloudEventAttributesProvider; import org.springframework.cloud.function.json.JsonMapper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -42,6 +48,7 @@ import org.springframework.messaging.converter.AbstractMessageConverter; import org.springframework.messaging.converter.MessageConverter; import org.springframework.util.MimeType; +import org.springframework.util.MimeTypeUtils; import org.springframework.util.SocketUtils; /** @@ -200,6 +207,53 @@ public void testAsStracturalFormatToString() throws Exception { assertThat(response.getBody()).isEqualTo("{\"version\":\"1.0\",\"releaseName\":\"Spring Framework\",\"releaseDate\":\"24-03-2004\"}"); } + @Test + public void testAsBinaryMapToMap() throws Exception { + SpringApplication.run(new Class[] {CloudeventDemoApplication.class}, new String[] {}); + + HttpHeaders headers = this.buildHeaders(MediaType.APPLICATION_JSON); + String payload = "{\"releaseDate\":\"24-03-2004\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}"; + + RequestEntity re = new RequestEntity<>(payload, headers, HttpMethod.POST, this.constructURI("/consumeAndProduceCloudEventAsMapToMap")); + ResponseEntity response = testRestTemplate.exchange(re, String.class); + + assertThat(response.getBody()).isEqualTo("{\"releaseDate\":\"01-10-2050\",\"releaseName\":\"Spring Framework\",\"version\":\"10.0\"}"); + assertThat(response.getHeaders().get(CloudEventMessageUtils.CE_SOURCE)) + .isEqualTo(Collections.singletonList("http://spring.io/application-application")); + assertThat(response.getHeaders().get(CloudEventMessageUtils.CE_TYPE)) + .isEqualTo(Collections.singletonList(LinkedHashMap.class.getName())); + } + + @Test + public void testAsBinaryPojoToPojo() throws Exception { + SpringApplication.run(new Class[] {CloudeventDemoApplication.class}, new String[] {}); + + HttpHeaders headers = this.buildHeaders(MediaType.APPLICATION_JSON); + String payload = "{\"releaseDate\":\"01-10-2006\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}"; + + RequestEntity re = new RequestEntity<>(payload, headers, HttpMethod.POST, this.constructURI("/consumeAndProduceCloudEventAsPojoToPojo")); + ResponseEntity response = testRestTemplate.exchange(re, String.class); + + assertThat(response.getBody()).isEqualTo("{\"releaseDate\":\"01-10-2006\",\"releaseName\":\"Spring Framework\",\"version\":\"2.0\"}"); + assertThat(response.getHeaders().get(CloudEventMessageUtils.CE_SOURCE)) + .isEqualTo(Collections.singletonList("http://spring.io/application-application")); + assertThat(response.getHeaders().get(CloudEventMessageUtils.CE_TYPE)) + .isEqualTo(Collections.singletonList(SpringReleaseEvent.class.getName())); + } + + private URI constructURI(String path) throws Exception { + return new URI("http://localhost:" + System.getProperty("server.port") + path); + } + + private HttpHeaders buildHeaders(MediaType contentType) { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(contentType); + headers.set(CloudEventMessageUtils.CE_ID, UUID.randomUUID().toString()); + headers.set(CloudEventMessageUtils.CE_SOURCE, "https://spring.io/"); + headers.set(CloudEventMessageUtils.CE_SPECVERSION, "1.0"); + headers.set(CloudEventMessageUtils.CE_TYPE, "org.springframework"); + return headers; + } @Configuration public static class FooBarConverterConfiguration { @@ -270,18 +324,4 @@ protected Object convertToInternal(Object payload, @Nullable MessageHeaders head } } - private URI constructURI(String path) throws Exception { - return new URI("http://localhost:" + System.getProperty("server.port") + path); - } - - private HttpHeaders buildHeaders(MediaType contentType) { - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(contentType); - headers.set("id", UUID.randomUUID().toString()); - headers.set("source", "https://spring.io/"); - headers.set("specversion", "1.0"); - headers.set("type", "org.springframework"); - return headers; - } - }