Restructure Cloud Events support to optionally support Cloud Events SDK
This commit is contained in:
@@ -16,7 +16,9 @@
|
||||
|
||||
package org.springframework.cloud.function.cloudevent;
|
||||
|
||||
import java.net.URI;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -29,7 +31,6 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -42,12 +43,50 @@ public class CloudEventFunctionTests {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testBinaryPojoToPojoDefaultOutputAttributeProvider() {
|
||||
public void testBinaryPojoToPojoDefaultOutputHeaderProvider() {
|
||||
Function<Object, Object> function = this.lookup("echo", TestConfiguration.class);
|
||||
|
||||
Message<String> inputMessage = MessageBuilder.withPayload("{\"name\":\"Ricky\"}")
|
||||
.copyHeaders(CloudEventMessageUtils.get("https://spring.io/", "org.springframework")).build();
|
||||
assertThat(CloudEventMessageUtils.isBinary(inputMessage.getHeaders())).isTrue();
|
||||
String id = UUID.randomUUID().toString();
|
||||
|
||||
Message<String> inputMessage = CloudEventMessageBuilder
|
||||
.withData("{\"name\":\"Ricky\"}")
|
||||
.setId(id)
|
||||
.setSource("https://spring.io/")
|
||||
.setType("org.springframework")
|
||||
.build();
|
||||
|
||||
assertThat(inputMessage.getHeaders().getId()).isEqualTo(UUID.fromString(id));
|
||||
assertThat(CloudEventMessageUtils.isBinary(inputMessage)).isTrue();
|
||||
|
||||
Message<Person> resultMessage = (Message<Person>) function.apply(inputMessage);
|
||||
|
||||
|
||||
/*
|
||||
* Validates that although user only deals with POJO, the framework recognizes
|
||||
* both on input and output that it is dealing with Cloud Event and generates
|
||||
* appropriate headers/attributes
|
||||
*/
|
||||
assertThat(CloudEventMessageUtils.isBinary(resultMessage)).isTrue();
|
||||
assertThat(CloudEventMessageUtils.getType(resultMessage)).isEqualTo(Person.class.getName());
|
||||
assertThat(CloudEventMessageUtils.getSource(resultMessage)).isEqualTo(URI.create("http://spring.io/application-application"));
|
||||
}
|
||||
|
||||
// this kind of emulates that message came from Kafka
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testBinaryPojoToPojoDefaultOutputHeaderProviderWithPrefix() {
|
||||
Function<Object, Object> function = this.lookup("echo", TestConfiguration.class);
|
||||
|
||||
String id = UUID.randomUUID().toString();
|
||||
|
||||
Message<String> inputMessage = CloudEventMessageBuilder
|
||||
.withData("{\"name\":\"Ricky\"}")
|
||||
.setHeader("ce_id", id)
|
||||
.setHeader("ce_source", "https://spring.io/")
|
||||
.setHeader("ce_type", "org.springframework")
|
||||
.build();
|
||||
|
||||
// assertThat(CloudEventMessageUtils.isBinary(inputMessage)).isTrue();
|
||||
|
||||
Message<Person> resultMessage = (Message<Person>) function.apply(inputMessage);
|
||||
|
||||
@@ -56,10 +95,9 @@ public class CloudEventFunctionTests {
|
||||
* both on input and output that it is dealing with Cloud Event and generates
|
||||
* appropriate headers/attributes
|
||||
*/
|
||||
CloudEventAttributes attributes = new CloudEventAttributes(resultMessage.getHeaders());
|
||||
assertThat(attributes.isValidCloudEvent()).isTrue();
|
||||
assertThat((String) attributes.getType()).isEqualTo(Person.class.getName());
|
||||
assertThat((String) attributes.getSource()).isEqualTo("http://spring.io/application-application");
|
||||
assertThat(CloudEventMessageUtils.isBinary(resultMessage)).isTrue();
|
||||
assertThat(CloudEventMessageUtils.getType(resultMessage)).isEqualTo(Person.class.getName());
|
||||
assertThat(CloudEventMessageUtils.getSource(resultMessage)).isEqualTo(URI.create("http://spring.io/application-application"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -79,24 +117,25 @@ public class CloudEventFunctionTests {
|
||||
"}";
|
||||
Function<Object, Object> function = this.lookup("springRelease", TestConfiguration.class);
|
||||
|
||||
Message<String> inputMessage = MessageBuilder.withPayload(payload)
|
||||
Message<String> inputMessage = CloudEventMessageBuilder
|
||||
.withData(payload)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, CloudEventMessageUtils.APPLICATION_CLOUDEVENTS_VALUE + "+json")
|
||||
.build();
|
||||
assertThat(CloudEventMessageUtils.isBinary(inputMessage.getHeaders())).isFalse();
|
||||
|
||||
assertThat(CloudEventMessageUtils.isBinary(inputMessage)).isFalse();
|
||||
|
||||
Message<SpringReleaseEvent> resultMessage = (Message<SpringReleaseEvent>) function.apply(inputMessage);
|
||||
assertThat(resultMessage.getPayload().getReleaseDate())
|
||||
.isEqualTo(new SimpleDateFormat("dd-MM-yyyy").parse("01-10-2006"));
|
||||
assertThat(resultMessage.getPayload().getVersion()).isEqualTo("2.0");
|
||||
/*
|
||||
* Validates that although user only deals with POJO, the framework recognizes
|
||||
* both on input and output that it is dealing with Cloud Event and generates
|
||||
* appropriate headers/attributes
|
||||
*/
|
||||
CloudEventAttributes attributes = new CloudEventAttributes(resultMessage.getHeaders());
|
||||
assertThat(attributes.isValidCloudEvent()).isTrue();
|
||||
assertThat((String) attributes.getType()).isEqualTo(SpringReleaseEvent.class.getName());
|
||||
assertThat((String) attributes.getSource()).isEqualTo("http://spring.io/application-application");
|
||||
// /*
|
||||
// * Validates that although user only deals with POJO, the framework recognizes
|
||||
// * both on input and output that it is dealing with Cloud Event and generates
|
||||
// * appropriate headers/attributes
|
||||
// */
|
||||
assertThat(CloudEventMessageUtils.isBinary(resultMessage)).isTrue();
|
||||
assertThat(CloudEventMessageUtils.getType(resultMessage)).isEqualTo(SpringReleaseEvent.class.getName());
|
||||
assertThat(CloudEventMessageUtils.getSource(resultMessage)).isEqualTo(URI.create("http://spring.io/application-application"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -115,10 +154,11 @@ public class CloudEventFunctionTests {
|
||||
"}";
|
||||
Function<Object, Object> function = this.lookup("springRelease", TestConfiguration.class);
|
||||
|
||||
Message<String> inputMessage = MessageBuilder.withPayload(payload)
|
||||
Message<String> inputMessage = CloudEventMessageBuilder
|
||||
.withData(payload)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, CloudEventMessageUtils.APPLICATION_CLOUDEVENTS_VALUE + "+json")
|
||||
.build();
|
||||
assertThat(CloudEventMessageUtils.isBinary(inputMessage.getHeaders())).isFalse();
|
||||
assertThat(CloudEventMessageUtils.isBinary(inputMessage)).isFalse();
|
||||
|
||||
Message<SpringReleaseEvent> resultMessage = (Message<SpringReleaseEvent>) function.apply(inputMessage);
|
||||
assertThat(resultMessage.getPayload().getReleaseDate())
|
||||
@@ -129,10 +169,9 @@ public class CloudEventFunctionTests {
|
||||
* both on input and output that it is dealing with Cloud Event and generates
|
||||
* appropriate headers/attributes
|
||||
*/
|
||||
CloudEventAttributes attributes = new CloudEventAttributes(resultMessage.getHeaders());
|
||||
assertThat(attributes.isValidCloudEvent()).isTrue();
|
||||
assertThat((String) attributes.getType()).isEqualTo(SpringReleaseEvent.class.getName());
|
||||
assertThat((String) attributes.getSource()).isEqualTo("http://spring.io/application-application");
|
||||
assertThat(CloudEventMessageUtils.isBinary(resultMessage)).isTrue();
|
||||
assertThat(CloudEventMessageUtils.getType(resultMessage)).isEqualTo(SpringReleaseEvent.class.getName());
|
||||
assertThat(CloudEventMessageUtils.getSource(resultMessage)).isEqualTo(URI.create("http://spring.io/application-application"));
|
||||
}
|
||||
|
||||
private Function<Object, Object> lookup(String functionDefinition, Class<?>... configClass) {
|
||||
|
||||
@@ -1,142 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.function.cloudevent;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.function.context.FunctionCatalog;
|
||||
import org.springframework.cloud.function.context.catalog.BeanFactoryAwareFunctionRegistry;
|
||||
import org.springframework.cloud.function.context.config.SmartCompositeMessageConverter;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class CloudEventTypeConversionTests {
|
||||
@Test
|
||||
public void testFromMessageBinaryPayloadMatchesType() {
|
||||
SmartCompositeMessageConverter messageConverter = this.configure(DummyConfiguration.class);
|
||||
CloudEventAttributes ceAttributes = CloudEventMessageUtils
|
||||
.get(UUID.randomUUID().toString(), "1.0", "https://spring.io/", "org.springframework");
|
||||
ceAttributes.setDataContentType("text/plain");
|
||||
Message<String> message = MessageBuilder.withPayload("Hello Ricky").copyHeaders(ceAttributes).build();
|
||||
|
||||
String converted = (String) messageConverter.fromMessage(message, String.class);
|
||||
assertThat(converted).isEqualTo("Hello Ricky");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromMessageBinaryPayloadDoesNotMatchType() {
|
||||
SmartCompositeMessageConverter messageConverter = this.configure(DummyConfiguration.class);
|
||||
CloudEventAttributes ceAttributes = CloudEventMessageUtils
|
||||
.get(UUID.randomUUID().toString(), "1.0", "https://spring.io/", "org.springframework");
|
||||
Message<byte[]> message = MessageBuilder.withPayload("Hello Ricky".getBytes())
|
||||
.copyHeaders(ceAttributes)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE,
|
||||
MimeTypeUtils.parseMimeType("application/cloudevents+json;charset=utf-8"))
|
||||
.build();
|
||||
String converted = (String) messageConverter.fromMessage(message, String.class);
|
||||
assertThat(converted).isEqualTo("Hello Ricky");
|
||||
}
|
||||
|
||||
@Test // JsonMessageConverter does some special things between byte[] and String so this works
|
||||
public void testFromMessageBinaryPayloadNoDataContentTypeToString() {
|
||||
SmartCompositeMessageConverter messageConverter = this.configure(DummyConfiguration.class);
|
||||
CloudEventAttributes ceAttributes = CloudEventMessageUtils
|
||||
.get(UUID.randomUUID().toString(), "1.0", "https://spring.io/", "org.springframework");
|
||||
Message<byte[]> message = MessageBuilder.withPayload("Hello Ricky".getBytes())
|
||||
.copyHeaders(ceAttributes)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE,
|
||||
MimeTypeUtils.parseMimeType("application/cloudevents+json;charset=utf-8"))
|
||||
.build();
|
||||
String converted = (String) messageConverter.fromMessage(message, String.class);
|
||||
assertThat(converted).isEqualTo("Hello Ricky");
|
||||
}
|
||||
|
||||
@Test // Unlike the previous test the type here is POJO so no special treatement
|
||||
public void testFromMessageBinaryPayloadNoDataContentTypeToPOJO() {
|
||||
SmartCompositeMessageConverter messageConverter = this.configure(DummyConfiguration.class);
|
||||
CloudEventAttributes ceAttributes = CloudEventMessageUtils.get("https://spring.io/", "org.springframework");
|
||||
Message<byte[]> message = MessageBuilder.withPayload("Hello Ricky".getBytes())
|
||||
.copyHeaders(ceAttributes)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE,
|
||||
MimeTypeUtils.parseMimeType("application/cloudevents+json;charset=utf-8"))
|
||||
.build();
|
||||
String converted = (String) messageConverter.fromMessage(message, Person.class);
|
||||
assertThat(converted).isNull();
|
||||
}
|
||||
|
||||
@Test // will fall on default CT which is json
|
||||
public void testFromMessageBinaryPayloadNoDataContentTypeToPOJOThatWorks() {
|
||||
SmartCompositeMessageConverter messageConverter = this.configure(DummyConfiguration.class);
|
||||
CloudEventAttributes ceAttributes = CloudEventMessageUtils.get("https://spring.io/", "org.springframework");
|
||||
Message<byte[]> message = MessageBuilder.withPayload("{\"name\":\"Ricky\"}".getBytes())
|
||||
.copyHeaders(ceAttributes)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE,
|
||||
MimeTypeUtils.parseMimeType("application/cloudevents+json;charset=utf-8"))
|
||||
.build();
|
||||
Person converted = (Person) messageConverter.fromMessage(message, Person.class);
|
||||
assertThat(converted.getName()).isEqualTo("Ricky");
|
||||
}
|
||||
|
||||
private SmartCompositeMessageConverter configure(Class<?>... configClass) {
|
||||
ApplicationContext context = new SpringApplicationBuilder(configClass).run(
|
||||
"--logging.level.org.springframework.cloud.function=DEBUG", "--spring.main.lazy-initialization=true");
|
||||
FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
|
||||
Field f = ReflectionUtils.findField(BeanFactoryAwareFunctionRegistry.class, "messageConverter");
|
||||
f.setAccessible(true);
|
||||
try {
|
||||
SmartCompositeMessageConverter messageConverter = (SmartCompositeMessageConverter) f.get(catalog);
|
||||
return messageConverter;
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@Configuration
|
||||
public static class DummyConfiguration {
|
||||
}
|
||||
|
||||
public static class Person {
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user