This is an automated email from the ASF dual-hosted git repository. lburgazzoli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git
commit c433628530ac2ec44f513b27f099c7a5a745f4c3 Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Fri May 15 15:37:24 2020 +0200 Support CloudEvents v1.0.0 #179 --- .../camel/component/knative/spi/CloudEvents.java | 22 +++++++++++-- .../component/knative/http/KnativeHttpTest.java | 38 +++++++++++++++++----- .../apache/camel/component/knative/knative.json | 2 +- .../component/knative/KnativeConfiguration.java | 4 +-- .../component/knative/ce/CloudEventProcessors.java | 33 +++++++++++++++++-- 5 files changed, 81 insertions(+), 18 deletions(-) diff --git a/camel-knative/camel-knative-api/src/main/java/org/apache/camel/component/knative/spi/CloudEvents.java b/camel-knative/camel-knative-api/src/main/java/org/apache/camel/component/knative/spi/CloudEvents.java index 6118ef0..961d18e 100644 --- a/camel-knative/camel-knative-api/src/main/java/org/apache/camel/component/knative/spi/CloudEvents.java +++ b/camel-knative/camel-knative-api/src/main/java/org/apache/camel/component/knative/spi/CloudEvents.java @@ -24,7 +24,7 @@ public enum CloudEvents implements CloudEvent { // // V0.1 - https://github.com/cloudevents/spec/blob/v0.1/spec.md // - V01(new CloudEventImpl( + v0_1(new CloudEventImpl( "0.1", Arrays.asList( Attribute.simple(CloudEvent.CAMEL_CLOUD_EVENT_TYPE, "CE-EventType", "eventType"), @@ -41,7 +41,7 @@ public enum CloudEvents implements CloudEvent { // // V0.2 - https://github.com/cloudevents/spec/blob/v0.2/spec.md // - V02(new CloudEventImpl( + v0_2(new CloudEventImpl( "0.2", Arrays.asList( Attribute.simple(CloudEvent.CAMEL_CLOUD_EVENT_TYPE, "ce-type", "type"), @@ -56,7 +56,7 @@ public enum CloudEvents implements CloudEvent { // // V0.3 - https://github.com/cloudevents/spec/blob/v0.3/spec.md // - V03(new CloudEventImpl( + v0_3(new CloudEventImpl( "0.3", Arrays.asList( Attribute.simple(CloudEvent.CAMEL_CLOUD_EVENT_ID, "ce-id", "id"), @@ -69,6 +69,22 @@ public enum CloudEvents implements CloudEvent { Attribute.simple(CloudEvent.CAMEL_CLOUD_EVENT_SUBJECT, "ce-subject", "subject"), Attribute.simple(CloudEvent.CAMEL_CLOUD_EVENT_TIME, "ce-time", "time") ) + )), + // + // V1.0 - https://github.com/cloudevents/spec/blob/v1.0/spec.md + // + v1_0(new CloudEventImpl( + "1.0", + Arrays.asList( + Attribute.simple(CloudEvent.CAMEL_CLOUD_EVENT_ID, "ce-id", "id"), + Attribute.simple(CloudEvent.CAMEL_CLOUD_EVENT_SOURCE, "ce-source", "source"), + Attribute.simple(CloudEvent.CAMEL_CLOUD_EVENT_VERSION, "ce-specversion", "specversion"), + Attribute.simple(CloudEvent.CAMEL_CLOUD_EVENT_TYPE, "ce-type", "type"), + Attribute.simple(CloudEvent.CAMEL_CLOUD_EVENT_DATA_CONTENT_TYPE, "ce-datacontenttype", "datacontenttype"), + Attribute.simple(CloudEvent.CAMEL_CLOUD_EVENT_SCHEMA_URL, "ce-dataschema", "dataschema"), + Attribute.simple(CloudEvent.CAMEL_CLOUD_EVENT_SUBJECT, "ce-subject", "subject"), + Attribute.simple(CloudEvent.CAMEL_CLOUD_EVENT_TIME, "ce-time", "time") + ) )); private final CloudEvent instance; diff --git a/camel-knative/camel-knative-http/src/test/java/org/apache/camel/component/knative/http/KnativeHttpTest.java b/camel-knative/camel-knative-http/src/test/java/org/apache/camel/component/knative/http/KnativeHttpTest.java index e0d6612..a4a55a6 100644 --- a/camel-knative/camel-knative-http/src/test/java/org/apache/camel/component/knative/http/KnativeHttpTest.java +++ b/camel-knative/camel-knative-http/src/test/java/org/apache/camel/component/knative/http/KnativeHttpTest.java @@ -235,6 +235,7 @@ public class KnativeHttpTest { @ParameterizedTest @EnumSource(CloudEvents.class) void testConsumeStructuredContent(CloudEvent ce) throws Exception { + configureKnativeComponent( context, ce, @@ -263,7 +264,7 @@ public class KnativeHttpTest { mock.expectedBodiesReceived("test"); mock.expectedMessageCount(1); - if (Objects.equals(CloudEvents.V01.version(), ce.version())) { + if (Objects.equals(CloudEvents.v0_1.version(), ce.version())) { given() .contentType(Knative.MIME_STRUCTURED_CONTENT_MODE) .body( @@ -278,11 +279,11 @@ public class KnativeHttpTest { ), ObjectMapperType.JACKSON_2 ) - .when() + .when() .post() - .then() + .then() .statusCode(200); - } else if (Objects.equals(CloudEvents.V02.version(), ce.version())) { + } else if (Objects.equals(CloudEvents.v0_2.version(), ce.version())) { given() .contentType(Knative.MIME_STRUCTURED_CONTENT_MODE) .body( @@ -297,11 +298,11 @@ public class KnativeHttpTest { ), ObjectMapperType.JACKSON_2 ) - .when() + .when() .post() - .then() + .then() .statusCode(200); - } else if (Objects.equals(CloudEvents.V03.version(), ce.version())) { + } else if (Objects.equals(CloudEvents.v0_3.version(), ce.version())) { given() .contentType(Knative.MIME_STRUCTURED_CONTENT_MODE) .body( @@ -316,9 +317,28 @@ public class KnativeHttpTest { ), ObjectMapperType.JACKSON_2 ) - .when() + .when() .post() - .then() + .then() + .statusCode(200); + } else if (Objects.equals(CloudEvents.v1_0.version(), ce.version())) { + given() + .contentType(Knative.MIME_STRUCTURED_CONTENT_MODE) + .body( + mapOf( + "specversion", ce.version(), + "type", "org.apache.camel.event", + "id", "myEventID", + "time", DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()), + "source", "/somewhere", + "datacontenttype", "text/plain", + "data", "test" + ), + ObjectMapperType.JACKSON_2 + ) + .when() + .post() + .then() .statusCode(200); } else { throw new IllegalArgumentException("Unknown CloudEvent spec: " + ce.version()); diff --git a/camel-knative/camel-knative/src/generated/resources/org/apache/camel/component/knative/knative.json b/camel-knative/camel-knative/src/generated/resources/org/apache/camel/component/knative/knative.json index 9fc04b0..ea549c7 100644 --- a/camel-knative/camel-knative/src/generated/resources/org/apache/camel/component/knative/knative.json +++ b/camel-knative/camel-knative/src/generated/resources/org/apache/camel/component/knative/knative.json @@ -30,7 +30,7 @@ "type": { "kind": "path", "displayName": "Type", "group": "common", "label": "", "required": false, "type": "object", "javaType": "org.apache.camel.component.knative.spi.Knative.Type", "enum": [ "endpoint", "channel", "event" ], "deprecated": false, "secret": false, "description": "The Knative type" }, "name": { "kind": "path", "displayName": "Name", "group": "common", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "secret": false, "description": "The Knative name" }, "ceOverride": { "kind": "parameter", "displayName": "Ce Override", "group": "common", "label": "", "required": false, "type": "object", "javaType": "java.util.Map<java.lang.String, java.lang.Object>", "prefix": "ce.override.", "deprecated": false, "secret": false, "configurationClass": "org.apache.camel.component.knative.KnativeConfiguration", "configurationField": "configuration", "description": "CloudEvent headers to override" }, - "cloudEventsSpecVersion": { "kind": "parameter", "displayName": "Cloud Events Spec Version", "group": "common", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "enum": [ "0.1", "0.2", "0.3" ], "deprecated": false, "secret": false, "defaultValue": "0.3", "configurationClass": "org.apache.camel.component.knative.KnativeConfiguration", "configurationField": "configuration", "description": "Set the version of the cloudevents spec." }, + "cloudEventsSpecVersion": { "kind": "parameter", "displayName": "Cloud Events Spec Version", "group": "common", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "enum": [ "0.1", "0.2", "0.3", "1.0" ], "deprecated": false, "secret": false, "defaultValue": "0.1", "configurationClass": "org.apache.camel.component.knative.KnativeConfiguration", "configurationField": "configuration", "description": "Set the version of the cloudevents spec." }, "cloudEventsType": { "kind": "parameter", "displayName": "Cloud Events Type", "group": "common", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "secret": false, "defaultValue": "org.apache.camel.event", "configurationClass": "org.apache.camel.component.knative.KnativeConfiguration", "configurationField": "configuration", "description": "Set the event-type information of the produced events." }, "environment": { "kind": "parameter", "displayName": "Environment", "group": "common", "label": "", "required": false, "type": "object", "javaType": "org.apache.camel.component.knative.spi.KnativeEnvironment", "deprecated": false, "secret": false, "configurationClass": "org.apache.camel.component.knative.KnativeConfiguration", "configurationField": "configuration", "description": "The environment" }, "filters": { "kind": "parameter", "displayName": "Filters", "group": "common", "label": "", "required": false, "type": "object", "javaType": "java.util.Map<java.lang.String, java.lang.Object>", "prefix": "filter.", "deprecated": false, "secret": false, "configurationClass": "org.apache.camel.component.knative.KnativeConfiguration", "configurationField": "configuration", "description": "Set the filters." }, diff --git a/camel-knative/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeConfiguration.java b/camel-knative/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeConfiguration.java index 825275e..557d65d 100644 --- a/camel-knative/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeConfiguration.java +++ b/camel-knative/camel-knative/src/main/java/org/apache/camel/component/knative/KnativeConfiguration.java @@ -31,8 +31,8 @@ public class KnativeConfiguration implements Cloneable { private KnativeEnvironment environment; @UriParam private String serviceName; - @UriParam(defaultValue = "0.3", enums = "0.1,0.2,0.3") - private String cloudEventsSpecVersion = CloudEvents.V03.version(); + @UriParam(defaultValue = "0.1", enums = "0.1,0.2,0.3,1.0") + private String cloudEventsSpecVersion = CloudEvents.v1_0.version(); @UriParam(defaultValue = "org.apache.camel.event") private String cloudEventsType = "org.apache.camel.event"; @UriParam(prefix = "transport.") diff --git a/camel-knative/camel-knative/src/main/java/org/apache/camel/component/knative/ce/CloudEventProcessors.java b/camel-knative/camel-knative/src/main/java/org/apache/camel/component/knative/ce/CloudEventProcessors.java index 04ea3df..96e891d 100644 --- a/camel-knative/camel-knative/src/main/java/org/apache/camel/component/knative/ce/CloudEventProcessors.java +++ b/camel-knative/camel-knative/src/main/java/org/apache/camel/component/knative/ce/CloudEventProcessors.java @@ -30,7 +30,7 @@ import org.apache.camel.component.knative.spi.KnativeEnvironment; import static org.apache.camel.util.ObjectHelper.ifNotEmpty; public enum CloudEventProcessors implements CloudEventProcessor { - V01(new AbstractCloudEventProcessor(CloudEvents.V01) { + v0_1(new AbstractCloudEventProcessor(CloudEvents.v0_1) { @SuppressWarnings("unchecked") @Override protected void decodeStructuredContent(Exchange exchange, Map<String, Object> content) { @@ -60,7 +60,7 @@ public enum CloudEventProcessors implements CloudEventProcessor { } } }), - V02(new AbstractCloudEventProcessor(CloudEvents.V02) { + v0_2(new AbstractCloudEventProcessor(CloudEvents.v0_2) { @Override protected void decodeStructuredContent(Exchange exchange, Map<String, Object> content) { final CloudEvent ce = cloudEvent(); @@ -88,7 +88,7 @@ public enum CloudEventProcessors implements CloudEventProcessor { } }), - V03(new AbstractCloudEventProcessor(CloudEvents.V03) { + v0_3(new AbstractCloudEventProcessor(CloudEvents.v0_3) { @Override protected void decodeStructuredContent(Exchange exchange, Map<String, Object> content) { final CloudEvent ce = cloudEvent(); @@ -117,6 +117,33 @@ public enum CloudEventProcessors implements CloudEventProcessor { message.setHeader(key.toLowerCase(), val); }); } + }), + v1_0(new AbstractCloudEventProcessor(CloudEvents.v1_0) { + @Override + protected void decodeStructuredContent(Exchange exchange, Map<String, Object> content) { + final CloudEvent ce = cloudEvent(); + final Message message = exchange.getIn(); + + // body + ifNotEmpty(content.remove("data"), message::setBody); + + ifNotEmpty(content.remove(ce.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_DATA_CONTENT_TYPE).json()), val -> { + message.setHeader(Exchange.CONTENT_TYPE, val); + }); + + for (CloudEvent.Attribute attribute: ce.attributes()) { + ifNotEmpty(content.remove(attribute.json()), val -> { + message.setHeader(attribute.id(), val); + }); + } + + // + // Map every remaining field as it is (extensions). + // + content.forEach((key, val) -> { + message.setHeader(key.toLowerCase(), val); + }); + } }); private final CloudEventProcessor instance;