Skip to content

Commit

Permalink
fix: Adding withoutData, withoutDataContentType and withoutDataSchema…
Browse files Browse the repository at this point in the history
… to CloudEventBuilder (#374)

Signed-off-by: Johan Haleby <johan.haleby@gmail.com>
  • Loading branch information
johanhaleby committed Apr 23, 2021
1 parent ff07dd8 commit 30fd676
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,28 @@ public interface CloudEventBuilder extends CloudEventWriter<CloudEvent> {
*/
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
*
Expand Down Expand Up @@ -309,9 +331,9 @@ static CloudEventBuilder from(@Nonnull CloudEvent event) {
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."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ public SELF withData(String dataContentType, URI dataSchema, CloudEventData data
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) {
if (!isValidExtensionName(key)) {
throw CloudEventRWException.newInvalidExtensionName(key);
Expand Down Expand Up @@ -189,6 +207,7 @@ public CloudEvent end() {
protected static IllegalStateException createMissingAttributeException(String attributeName) {
return new IllegalStateException("Attribute '" + attributeName + "' cannot be null");
}

/**
* Validates the extension name as defined in CloudEvents spec.
*
Expand All @@ -197,7 +216,7 @@ protected static IllegalStateException createMissingAttributeException(String at
* @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) {
for(int i = 0; i < name.length(); i++) {
for (int i = 0; i < name.length(); i++) {
if (!isValidChar(name.charAt(i))) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -101,4 +100,43 @@ public void testBinaryExtension() {
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()
);

}
}

0 comments on commit 30fd676

Please sign in to comment.