fix: Adding withoutData, withoutDataContentType and withoutDataSchema to CloudEventBuilder (#374)

Signed-off-by: Johan Haleby <johan.haleby@gmail.com>
This commit is contained in:
Johan Haleby 2021-04-23 13:59:39 +02:00 committed by GitHub
parent ff07dd8315
commit 30fd6769eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 10 deletions

View File

@ -141,6 +141,28 @@ public interface CloudEventBuilder extends CloudEventWriter<CloudEvent> {
*/ */
CloudEventBuilder withData(String dataContentType, URI dataSchema, CloudEventData data); 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 * Set an extension with provided key and string value
* *
@ -309,9 +331,9 @@ public interface CloudEventBuilder extends CloudEventWriter<CloudEvent> {
static CloudEventBuilder fromContext(@Nonnull CloudEventContext context) { static CloudEventBuilder fromContext(@Nonnull CloudEventContext context) {
switch (context.getSpecVersion()) { switch (context.getSpecVersion()) {
case V1: case V1:
return new io.cloudevents.core.v1.CloudEventBuilder(context); return new io.cloudevents.core.v1.CloudEventBuilder(context);
case V03: case V03:
return new io.cloudevents.core.v03.CloudEventBuilder(context); return new io.cloudevents.core.v03.CloudEventBuilder(context);
} }
throw new IllegalStateException( throw new IllegalStateException(
"The provided spec version doesn't exist. Please make sure your io.cloudevents deps versions are aligned." "The provided spec version doesn't exist. Please make sure your io.cloudevents deps versions are aligned."

View File

@ -98,6 +98,24 @@ public abstract class BaseCloudEventBuilder<SELF extends BaseCloudEventBuilder<S
return this.self; return this.self;
} }
@Override
public CloudEventBuilder withoutData() {
this.data = null;
return this.self;
}
@Override
public CloudEventBuilder withoutDataSchema() {
withDataSchema(null);
return this.self;
}
@Override
public CloudEventBuilder withoutDataContentType() {
withDataContentType(null);
return this.self;
}
public SELF withExtension(@Nonnull String key, @Nonnull String value) { public SELF withExtension(@Nonnull String key, @Nonnull String value) {
if (!isValidExtensionName(key)) { if (!isValidExtensionName(key)) {
throw CloudEventRWException.newInvalidExtensionName(key); throw CloudEventRWException.newInvalidExtensionName(key);
@ -189,6 +207,7 @@ public abstract class BaseCloudEventBuilder<SELF extends BaseCloudEventBuilder<S
protected static IllegalStateException createMissingAttributeException(String attributeName) { protected static IllegalStateException createMissingAttributeException(String attributeName) {
return new IllegalStateException("Attribute '" + attributeName + "' cannot be null"); return new IllegalStateException("Attribute '" + attributeName + "' cannot be null");
} }
/** /**
* Validates the extension name as defined in CloudEvents spec. * Validates the extension name as defined in CloudEvents spec.
* *
@ -197,7 +216,7 @@ public abstract class BaseCloudEventBuilder<SELF extends BaseCloudEventBuilder<S
* @see <a href="https://github.com/cloudevents/spec/blob/master/spec.md#attribute-naming-convention">attribute-naming-convention</a> * @see <a href="https://github.com/cloudevents/spec/blob/master/spec.md#attribute-naming-convention">attribute-naming-convention</a>
*/ */
private static boolean isValidExtensionName(String name) { 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))) { if (!isValidChar(name.charAt(i))) {
return false; return false;
} }

View File

@ -1,18 +1,17 @@
package io.cloudevents.core.impl; 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.CloudEvent;
import io.cloudevents.core.builder.CloudEventBuilder; import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.core.extensions.DistributedTracingExtension; import io.cloudevents.core.extensions.DistributedTracingExtension;
import io.cloudevents.core.test.Data; import io.cloudevents.core.test.Data;
import org.junit.jupiter.api.Test; 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 { public class BaseCloudEventBuilderTest {
@Test @Test
@ -101,4 +100,43 @@ public class BaseCloudEventBuilderTest {
assertEquals(Data.BINARY_VALUE, given.getExtension(EXT_NAME)); 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()
);
}
} }