Make CloudEventAttributesProvider a FunctionalInterface
Related to #422 and #606
This commit is contained in:
@@ -16,11 +16,7 @@
|
||||
|
||||
package io.spring.cloudevent;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
@@ -28,8 +24,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventAttributes;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventAttributesProvider;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventMessageUtils;
|
||||
import org.springframework.cloud.function.cloudevent.DefaultCloudEventAttributesProvider;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
@@ -96,7 +90,7 @@ public class CloudeventDemoApplication {
|
||||
data.setVersion("2.0");
|
||||
data.setReleaseDateAsString("01-10-2006");
|
||||
|
||||
CloudEventAttributes ceAttributes = ceAttrProvider.get(ceMessage.getHeaders())
|
||||
CloudEventAttributes ceAttributes = CloudEventMessageUtils.get(ceMessage.getHeaders())
|
||||
.setSource("https://interface21.com/")
|
||||
.setType("com.interface21");
|
||||
|
||||
|
||||
@@ -20,8 +20,7 @@ import java.util.function.Function;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventAttributesProvider;
|
||||
import org.springframework.cloud.function.cloudevent.DefaultCloudEventAttributesProvider;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventMessageUtils;
|
||||
import org.springframework.cloud.function.context.FunctionCatalog;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.messaging.Message;
|
||||
@@ -39,10 +38,9 @@ public class CloudeventDemoApplicationFunctionTests {
|
||||
|
||||
try(ConfigurableApplicationContext context = SpringApplication.run(CloudeventDemoApplication.class)) {
|
||||
FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
|
||||
CloudEventAttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
Message<String> binaryCloudEventMessage = MessageBuilder
|
||||
.withPayload("{\"releaseDate\":\"24-03-2004\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}")
|
||||
.copyHeaders(ceAttrProvider.get("spring.io/spring-event", "com.example.springevent"))
|
||||
.copyHeaders(CloudEventMessageUtils.get("spring.io/spring-event", "com.example.springevent"))
|
||||
.build();
|
||||
|
||||
/*
|
||||
@@ -68,10 +66,9 @@ public class CloudeventDemoApplicationFunctionTests {
|
||||
public void demoPureFunctionProduceConsumeCloudEvent() {
|
||||
try(ConfigurableApplicationContext context = SpringApplication.run(CloudeventDemoApplication.class)) {
|
||||
FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
|
||||
CloudEventAttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
Message<String> binaryCloudEventMessage = MessageBuilder
|
||||
.withPayload("{\"releaseDate\":\"24-03-2004\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}")
|
||||
.copyHeaders(ceAttrProvider.get("spring.io/spring-event", "com.example.springevent"))
|
||||
.copyHeaders(CloudEventMessageUtils.get("spring.io/spring-event", "com.example.springevent"))
|
||||
.build();
|
||||
|
||||
/*
|
||||
@@ -88,10 +85,9 @@ public class CloudeventDemoApplicationFunctionTests {
|
||||
public void demoPureFunctionProduceConsumeCloudEventAsPojo() {
|
||||
try(ConfigurableApplicationContext context = SpringApplication.run(CloudeventDemoApplication.class)) {
|
||||
FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
|
||||
CloudEventAttributesProvider ceAttrProvider = new DefaultCloudEventAttributesProvider();
|
||||
Message<String> binaryCloudEventMessage = MessageBuilder
|
||||
.withPayload("{\"releaseDate\":\"24-03-2004\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}")
|
||||
.copyHeaders(ceAttrProvider.get("spring.io/spring-event", "com.example.springevent"))
|
||||
.copyHeaders(CloudEventMessageUtils.get("spring.io/spring-event", "com.example.springevent"))
|
||||
.build();
|
||||
|
||||
/*
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.cloud.function.cloudevent.CloudEventJsonMessageConver
|
||||
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.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
@@ -241,6 +242,51 @@ public class CloudeventDemoApplicationRESTTests {
|
||||
.isEqualTo(Collections.singletonList(SpringReleaseEvent.class.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAsStructuralPojoToPojo() throws Exception {
|
||||
ApplicationContext context = SpringApplication.run(CloudeventDemoApplication.class);
|
||||
JsonMapper mapper = context.getBean(JsonMapper.class);
|
||||
|
||||
String payload = "{\n" +
|
||||
" \"specversion\" : \"1.0\",\n" +
|
||||
" \"type\" : \"org.springframework\",\n" +
|
||||
" \"source\" : \"https://spring.io/\",\n" +
|
||||
" \"id\" : \"A234-1234-1234\",\n" +
|
||||
// " \"ce-datacontenttype\" : \"application/json\",\n" +
|
||||
" \"data\" : {\n" +
|
||||
" \"version\" : \"1.0\",\n" +
|
||||
" \"releaseName\" : \"Spring Framework\",\n" +
|
||||
" \"releaseDate\" : \"24-03-2004\"\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
|
||||
System.out.println(payload);
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.valueOf("application/cloudevents+json;charset=utf-8"));
|
||||
|
||||
RequestEntity<String> re = new RequestEntity<>(payload, headers, HttpMethod.POST, this.constructURI("/consumeAndProduceCloudEventAsPojoToPojo"));
|
||||
ResponseEntity<String> response = testRestTemplate.exchange(re, String.class);
|
||||
|
||||
SpringReleaseEvent springReleaseEvent = mapper.fromJson(response.getBody(), SpringReleaseEvent.class);
|
||||
|
||||
assertThat(springReleaseEvent.getReleaseName()).isEqualTo("Spring Framework");
|
||||
assertThat(springReleaseEvent.getVersion()).isEqualTo("2.0");
|
||||
|
||||
re = new RequestEntity<>(payload, headers, HttpMethod.POST, this.constructURI("/consumeAndProduceCloudEventAsMapToMap"));
|
||||
response = testRestTemplate.exchange(re, String.class);
|
||||
|
||||
springReleaseEvent = mapper.fromJson(response.getBody(), SpringReleaseEvent.class);
|
||||
|
||||
assertThat(springReleaseEvent.getReleaseName()).isEqualTo("Spring Framework");
|
||||
assertThat(springReleaseEvent.getVersion()).isEqualTo("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(SpringReleaseEvent.class.getName()));
|
||||
}
|
||||
|
||||
private URI constructURI(String path) throws Exception {
|
||||
return new URI("http://localhost:" + System.getProperty("server.port") + path);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user