GH-422 Formalize Cloud Event conversion strategy to consistently handle binary-mode and structured-mode cloud events
Moved CloudEvent related artifacts to ‘cloud events’ package with hopes to eventually donating it to CNCF SDK Created CloudEventUtils identifying necessary constants and utility methods
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* 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 CloudEventJsonMessageConverterTests {
|
||||
@Test
|
||||
public void testFromMessageBinaryPayloadMatchesType() {
|
||||
SmartCompositeMessageConverter messageConverter = this.configure(DummyConfiguration.class);
|
||||
Message<String> message = MessageBuilder.withPayload("Hello Ricky").setHeader("ce_source", "https://spring.io/")
|
||||
.setHeader("ce_id", UUID.randomUUID().toString()).setHeader("ce_type", "org.springframework")
|
||||
.setHeader("ce_specversion", "1.0").setHeader("ce_datacontenttype", "text/plain").build();
|
||||
|
||||
String converted = (String) messageConverter.fromMessage(message, String.class);
|
||||
assertThat(converted).isEqualTo("Hello Ricky");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromMessageBinaryPayloadDoesNotMatchType() {
|
||||
SmartCompositeMessageConverter messageConverter = this.configure(DummyConfiguration.class);
|
||||
Message<byte[]> message = MessageBuilder.withPayload("Hello Ricky".getBytes())
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE,
|
||||
MimeTypeUtils.parseMimeType("application/cloudevents+json;charset=utf-8"))
|
||||
.setHeader("ce_source", "https://spring.io/").setHeader("ce_id", UUID.randomUUID().toString())
|
||||
.setHeader("ce_type", "org.springframework").setHeader("ce_specversion", "1.0")
|
||||
.setHeader("ce_datacontenttype", "text/plain").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);
|
||||
Message<byte[]> message = MessageBuilder.withPayload("Hello Ricky".getBytes())
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE,
|
||||
MimeTypeUtils.parseMimeType("application/cloudevents+json;charset=utf-8"))
|
||||
.setHeader("ce_source", "https://spring.io/").setHeader("ce_id", UUID.randomUUID().toString())
|
||||
.setHeader("ce_type", "org.springframework").setHeader("ce_specversion", "1.0").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);
|
||||
Message<byte[]> message = MessageBuilder.withPayload("Hello Ricky".getBytes())
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE,
|
||||
MimeTypeUtils.parseMimeType("application/cloudevents+json;charset=utf-8"))
|
||||
.setHeader("ce_source", "https://spring.io/").setHeader("ce_id", UUID.randomUUID().toString())
|
||||
.setHeader("ce_type", "org.springframework").setHeader("ce_specversion", "1.0").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);
|
||||
Message<byte[]> message = MessageBuilder.withPayload("{\"name\":\"Ricky\"}".getBytes())
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE,
|
||||
MimeTypeUtils.parseMimeType("application/cloudevents+json;charset=utf-8"))
|
||||
.setHeader("ce_source", "https://spring.io/").setHeader("ce_id", UUID.randomUUID().toString())
|
||||
.setHeader("ce_type", "org.springframework").setHeader("ce_specversion", "1.0").build();
|
||||
Person converted = (Person) messageConverter.fromMessage(message, Person.class);
|
||||
assertThat(converted.getName()).isEqualTo("Ricky");
|
||||
}
|
||||
|
||||
@Test // will fall on default CT which is json
|
||||
public void testFromMessageStructured() {
|
||||
String cloudEventStructured = "{\n" + " \"specversion\" : \"1.0\",\n"
|
||||
+ " \"type\" : \"org.springframework\",\n" + " \"source\" : \"https://spring.io/\",\n"
|
||||
+ " \"id\" : \"A234-1234-1234\",\n" + " \"datacontenttype\" : \"application/json\",\n"
|
||||
+ " \"data\" : {\n" + " \"version\" : \"1.0\",\n"
|
||||
+ " \"releaseName\" : \"Spring Framework\",\n" + " \"releaseDate\" : \"24-03-2004\"\n"
|
||||
+ " }\n" + " }";
|
||||
SmartCompositeMessageConverter messageConverter = this.configure(DummyConfiguration.class);
|
||||
Message<String> message = MessageBuilder.withPayload(cloudEventStructured)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE,
|
||||
MimeTypeUtils.parseMimeType("application/cloudevents+json;charset=utf-8"))
|
||||
.setHeader("ce_datacontenttype", "application/json").build();
|
||||
SpringReleaseEvent springReleaseEvent = (SpringReleaseEvent) messageConverter.fromMessage(message,
|
||||
SpringReleaseEvent.class);
|
||||
assertThat(springReleaseEvent.getReleaseName()).isEqualTo("Spring Framework");
|
||||
assertThat(springReleaseEvent.getVersion()).isEqualTo("1.0");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2020-2020 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.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
||||
/**
|
||||
* An example POJO that represents cloud event data
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class SpringReleaseEvent {
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
|
||||
private Date releaseDate;
|
||||
|
||||
private String releaseName;
|
||||
|
||||
private String version;
|
||||
|
||||
public Date getReleaseDate() {
|
||||
return releaseDate;
|
||||
}
|
||||
|
||||
public void setReleaseDate(Date releaseDate) {
|
||||
this.releaseDate = releaseDate;
|
||||
}
|
||||
|
||||
public String getReleaseName() {
|
||||
return releaseName;
|
||||
}
|
||||
|
||||
public void setReleaseName(String releaseName) {
|
||||
this.releaseName = releaseName;
|
||||
}
|
||||
|
||||
public String getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(String version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "releaseDate:" + new SimpleDateFormat("dd-MM-yyyy").format(releaseDate) + "; releaseName:" + releaseName + "; version:" + version;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user