From 30fd6769ebfbed6e8075fbca1a0c11fb184b3db9 Mon Sep 17 00:00:00 2001 From: Johan Haleby Date: Fri, 23 Apr 2021 13:59:39 +0200 Subject: [PATCH] fix: Adding withoutData, withoutDataContentType and withoutDataSchema to CloudEventBuilder (#374) Signed-off-by: Johan Haleby --- .../core/builder/CloudEventBuilder.java | 26 +++++++++- .../core/impl/BaseCloudEventBuilder.java | 21 +++++++- .../core/impl/BaseCloudEventBuilderTest.java | 52 ++++++++++++++++--- 3 files changed, 89 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/io/cloudevents/core/builder/CloudEventBuilder.java b/core/src/main/java/io/cloudevents/core/builder/CloudEventBuilder.java index f8cbfb50..918fb4c6 100644 --- a/core/src/main/java/io/cloudevents/core/builder/CloudEventBuilder.java +++ b/core/src/main/java/io/cloudevents/core/builder/CloudEventBuilder.java @@ -141,6 +141,28 @@ public interface CloudEventBuilder extends CloudEventWriter { */ CloudEventBuilder withData(String dataContentType, URI dataSchema, CloudEventData data); + /** + * Remove the {@code datacontenttype}, {@code dataschema} and {@code data} from the event + * + * @return self + */ + CloudEventBuilder withoutData(); + + /** + * Remove the {@code dataschema} from the event + * + * @return self + */ + CloudEventBuilder withoutDataSchema(); + + + /** + * Remove the {@code datacontenttype} from the event + * + * @return self + */ + CloudEventBuilder withoutDataContentType(); + /** * Set an extension with provided key and string value * @@ -309,9 +331,9 @@ public interface CloudEventBuilder extends CloudEventWriter { static CloudEventBuilder fromContext(@Nonnull CloudEventContext context) { switch (context.getSpecVersion()) { case V1: - return new io.cloudevents.core.v1.CloudEventBuilder(context); + return new io.cloudevents.core.v1.CloudEventBuilder(context); case V03: - return new io.cloudevents.core.v03.CloudEventBuilder(context); + return new io.cloudevents.core.v03.CloudEventBuilder(context); } throw new IllegalStateException( "The provided spec version doesn't exist. Please make sure your io.cloudevents deps versions are aligned." diff --git a/core/src/main/java/io/cloudevents/core/impl/BaseCloudEventBuilder.java b/core/src/main/java/io/cloudevents/core/impl/BaseCloudEventBuilder.java index 6e85e78c..3268e70a 100644 --- a/core/src/main/java/io/cloudevents/core/impl/BaseCloudEventBuilder.java +++ b/core/src/main/java/io/cloudevents/core/impl/BaseCloudEventBuilder.java @@ -98,6 +98,24 @@ public abstract class BaseCloudEventBuilderattribute-naming-convention */ private static boolean isValidExtensionName(String name) { - for(int i = 0; i < name.length(); i++) { + for (int i = 0; i < name.length(); i++) { if (!isValidChar(name.charAt(i))) { return false; } diff --git a/core/src/test/java/io/cloudevents/core/impl/BaseCloudEventBuilderTest.java b/core/src/test/java/io/cloudevents/core/impl/BaseCloudEventBuilderTest.java index 7f8bc2fa..3fab09f2 100644 --- a/core/src/test/java/io/cloudevents/core/impl/BaseCloudEventBuilderTest.java +++ b/core/src/test/java/io/cloudevents/core/impl/BaseCloudEventBuilderTest.java @@ -1,18 +1,17 @@ package io.cloudevents.core.impl; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - import io.cloudevents.CloudEvent; import io.cloudevents.core.builder.CloudEventBuilder; import io.cloudevents.core.extensions.DistributedTracingExtension; import io.cloudevents.core.test.Data; import org.junit.jupiter.api.Test; +import java.util.Objects; + +import static io.cloudevents.core.test.Data.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + public class BaseCloudEventBuilderTest { @Test @@ -101,4 +100,43 @@ public class BaseCloudEventBuilderTest { assertEquals(Data.BINARY_VALUE, given.getExtension(EXT_NAME)); } + + @Test + public void withoutDataRemovesDataAttributeFromCopiedCloudEvent() { + CloudEvent original = CloudEventBuilder.v1(Data.V1_WITH_JSON_DATA_WITH_EXT).build(); + CloudEvent copy = CloudEventBuilder.v1(original).withoutData().build(); + + assertAll( + () -> assertThat(copy.getData()).isNull(), + () -> assertThat(copy.getDataContentType()).isEqualTo(DATACONTENTTYPE_JSON), + () -> assertThat(copy.getDataSchema()).isEqualTo(DATASCHEMA) + ); + + } + + @Test + public void withoutDataContentTypeRemovesDataContentTypeAttributeFromCopiedCloudEvent() { + CloudEvent original = CloudEventBuilder.v1(Data.V1_WITH_JSON_DATA_WITH_EXT).build(); + CloudEvent copy = CloudEventBuilder.v1(original).withoutDataContentType().build(); + + assertAll( + () -> assertThat(Objects.requireNonNull(copy.getData()).toBytes()).isEqualTo(DATA_JSON_SERIALIZED), + () -> assertThat(copy.getDataContentType()).isNull(), + () -> assertThat(copy.getDataSchema()).isEqualTo(DATASCHEMA) + ); + + } + + @Test + public void withoutDataSchemaRemovesDataSchemaAttributeFromCopiedCloudEvent() { + CloudEvent original = CloudEventBuilder.v1(Data.V1_WITH_JSON_DATA_WITH_EXT).build(); + CloudEvent copy = CloudEventBuilder.v1(original).withoutDataSchema().build(); + + assertAll( + () -> assertThat(Objects.requireNonNull(copy.getData()).toBytes()).isEqualTo(DATA_JSON_SERIALIZED), + () -> assertThat(copy.getDataContentType()).isEqualTo(DATACONTENTTYPE_JSON), + () -> assertThat(copy.getDataSchema()).isNull() + ); + + } }