From c07722dab3e4acb65c25600e9b08c010c19352af Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Wed, 2 Dec 2020 15:17:17 +0100 Subject: [PATCH] Tidy up logic around canonicalization of attributes Added initial test to validate builder and utils --- .../cloudevent/CloudEventMessageUtils.java | 14 ++++++++++---- .../CloudEventMessageUtilsAndBuilderTests.java | 15 +++++++++++++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/cloudevent/CloudEventMessageUtils.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/cloudevent/CloudEventMessageUtils.java index 3802ad378..2cf107cf9 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/cloudevent/CloudEventMessageUtils.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/cloudevent/CloudEventMessageUtils.java @@ -57,8 +57,7 @@ public final class CloudEventMessageUtils { private CloudEventMessageUtils() { } - //===== - + //=========== INTERNAL USE ONLY == static String _DATA = "data"; static String _ID = "id"; @@ -78,6 +77,7 @@ public final class CloudEventMessageUtils { static String _SUBJECT = "subject"; static String _TIME = "time"; + // ================================ /** * String value of 'application/cloudevents' mime type. @@ -288,9 +288,15 @@ public final class CloudEventMessageUtils { } /** - * Will canonicalize Cloud Event attributes (headers) by removing well known prefixes. - * So, for example 'ce_source' will become 'source'. + * Will canonicalize Cloud Event attributes (headers) by ensuring canonical + * prefix for all attributes and extensions regardless of where they came from. + * The canonical prefix is 'ce-'. + * + * So, for example 'ce_source' will become 'ce-source'. * @param headers message headers + * @param structured boolean signifying that headers map represents structured Cloud Event + * at which point attributes without any prefix will still be treated as + * Cloud Event attributes. */ private static void canonicalizeHeaders(Map headers, boolean structured) { String[] keys = headers.keySet().toArray(new String[] {}); diff --git a/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/cloudevent/CloudEventMessageUtilsAndBuilderTests.java b/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/cloudevent/CloudEventMessageUtilsAndBuilderTests.java index 2bd80be88..f76b65f77 100644 --- a/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/cloudevent/CloudEventMessageUtilsAndBuilderTests.java +++ b/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/cloudevent/CloudEventMessageUtilsAndBuilderTests.java @@ -16,15 +16,16 @@ package org.springframework.cloud.function.cloudevent; -import static org.assertj.core.api.Assertions.assertThat; - import java.net.URI; import java.util.Map; import org.junit.jupiter.api.Test; + import org.springframework.messaging.Message; import org.springframework.messaging.support.MessageBuilder; +import static org.assertj.core.api.Assertions.assertThat; + /** * @author Oleg Zhurakousky */ @@ -52,5 +53,15 @@ public class CloudEventMessageUtilsAndBuilderTests { assertThat(CloudEventMessageUtils.getType(kafkaMessage)).isEqualTo("blah"); assertThat(kafkaMessage.getHeaders().get("ce_specversion")).isNotNull(); assertThat(CloudEventMessageUtils.getSpecVersion(kafkaMessage)).isEqualTo("1.0"); + + httpMessage = CloudEventMessageBuilder.fromMessage(kafkaMessage).build(CloudEventMessageUtils.DEFAULT_ATTR_PREFIX); + attributes = CloudEventMessageUtils.getAttributes(httpMessage); + assertThat(attributes.size()).isEqualTo(3); + assertThat(httpMessage.getHeaders().get("ce-source")).isNotNull(); + assertThat(CloudEventMessageUtils.getSource(httpMessage)).isEqualTo(URI.create("https://foo.bar")); + assertThat(httpMessage.getHeaders().get("ce-type")).isNotNull(); + assertThat(CloudEventMessageUtils.getType(httpMessage)).isEqualTo("blah"); + assertThat(httpMessage.getHeaders().get("ce-specversion")).isNotNull(); + assertThat(CloudEventMessageUtils.getSpecVersion(httpMessage)).isEqualTo("1.0"); } }