Added a method to remove the extensions (#218)
* Added a method to remove the extensions Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Added a method to remove the materialized extension Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Better test Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
This commit is contained in:
parent
75171b9705
commit
8b4e586b64
|
@ -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
|
||||
*
|
||||
|
|
|
@ -91,6 +91,18 @@ public abstract class BaseCloudEventBuilder<SELF extends BaseCloudEventBuilder<S
|
|||
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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue