Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,27 @@ public interface CloudEventBuilder extends CloudEventWriter<CloudEvent> {
/**
* Add to the builder all the extension key/values of the provided extension
*
* @param extension materialized extension to set in the event
* @param extension materialized extension to set in the builder
* @return self
*/
CloudEventBuilder withExtension(@Nonnull Extension extension);

/**
* Remove from the the builder the provided extension key, if any
*
* @param key key of the extension attribute
* @return self
*/
CloudEventBuilder withoutExtension(@Nonnull String key);

/**
* Remove from the the builder the provided extension, if any
*
* @param extension materialized extension to remove from the builder
* @return self
*/
CloudEventBuilder withoutExtension(@Nonnull Extension extension);

/**
* Build the event
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ public SELF withExtension(@Nonnull String key, boolean value) {
return self;
}

@Override
public SELF withoutExtension(@Nonnull String key) {
this.extensions.remove(key);
return self;
}

@Override
public SELF withoutExtension(@Nonnull Extension extension) {
extension.getKeys().forEach(this::withoutExtension);
return self;
}

public SELF withExtension(@Nonnull Extension extension) {
for (String key : extension.getKeys()) {
Object value = extension.getValue(key);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.cloudevents.core.impl;

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 static org.assertj.core.api.Assertions.assertThat;

public class BaseCloudEventBuilderTest {

@Test
public void copyAndRemoveExtension() {
assertThat(Data.V1_WITH_JSON_DATA_WITH_EXT.getExtensionNames())
.contains("astring");

CloudEvent event = CloudEventBuilder.v1(Data.V1_WITH_JSON_DATA_WITH_EXT)
.withoutExtension("astring")
.build();

assertThat(event.getExtensionNames())
.doesNotContain("astring");
}

@Test
public void copyAndRemoveMaterializedExtension() {
DistributedTracingExtension ext = new DistributedTracingExtension();
ext.setTraceparent("aaa"); // Set only traceparent

CloudEvent given = CloudEventBuilder.v1(Data.V1_WITH_JSON_DATA_WITH_EXT)
.withExtension(ext)
.build();
assertThat(given.getExtensionNames())
.contains("traceparent")
.doesNotContain("tracestate");

CloudEvent have = CloudEventBuilder.v1(given)
.withoutExtension(ext)
.build();

assertThat(have.getExtensionNames())
.doesNotContain("traceparent", "tracestate");
assertThat(Data.V1_WITH_JSON_DATA_WITH_EXT)
.isEqualTo(have);
}

}