Skip to content

Commit 8b4e586

Browse files
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>
1 parent 75171b9 commit 8b4e586

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

core/src/main/java/io/cloudevents/core/builder/CloudEventBuilder.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,27 @@ public interface CloudEventBuilder extends CloudEventWriter<CloudEvent> {
146146
/**
147147
* Add to the builder all the extension key/values of the provided extension
148148
*
149-
* @param extension materialized extension to set in the event
149+
* @param extension materialized extension to set in the builder
150150
* @return self
151151
*/
152152
CloudEventBuilder withExtension(@Nonnull Extension extension);
153153

154+
/**
155+
* Remove from the the builder the provided extension key, if any
156+
*
157+
* @param key key of the extension attribute
158+
* @return self
159+
*/
160+
CloudEventBuilder withoutExtension(@Nonnull String key);
161+
162+
/**
163+
* Remove from the the builder the provided extension, if any
164+
*
165+
* @param extension materialized extension to remove from the builder
166+
* @return self
167+
*/
168+
CloudEventBuilder withoutExtension(@Nonnull Extension extension);
169+
154170
/**
155171
* Build the event
156172
*

core/src/main/java/io/cloudevents/core/impl/BaseCloudEventBuilder.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ public SELF withExtension(@Nonnull String key, boolean value) {
9191
return self;
9292
}
9393

94+
@Override
95+
public SELF withoutExtension(@Nonnull String key) {
96+
this.extensions.remove(key);
97+
return self;
98+
}
99+
100+
@Override
101+
public SELF withoutExtension(@Nonnull Extension extension) {
102+
extension.getKeys().forEach(this::withoutExtension);
103+
return self;
104+
}
105+
94106
public SELF withExtension(@Nonnull Extension extension) {
95107
for (String key : extension.getKeys()) {
96108
Object value = extension.getValue(key);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.cloudevents.core.impl;
2+
3+
import io.cloudevents.CloudEvent;
4+
import io.cloudevents.core.builder.CloudEventBuilder;
5+
import io.cloudevents.core.extensions.DistributedTracingExtension;
6+
import io.cloudevents.core.test.Data;
7+
import org.junit.jupiter.api.Test;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
public class BaseCloudEventBuilderTest {
12+
13+
@Test
14+
public void copyAndRemoveExtension() {
15+
assertThat(Data.V1_WITH_JSON_DATA_WITH_EXT.getExtensionNames())
16+
.contains("astring");
17+
18+
CloudEvent event = CloudEventBuilder.v1(Data.V1_WITH_JSON_DATA_WITH_EXT)
19+
.withoutExtension("astring")
20+
.build();
21+
22+
assertThat(event.getExtensionNames())
23+
.doesNotContain("astring");
24+
}
25+
26+
@Test
27+
public void copyAndRemoveMaterializedExtension() {
28+
DistributedTracingExtension ext = new DistributedTracingExtension();
29+
ext.setTraceparent("aaa"); // Set only traceparent
30+
31+
CloudEvent given = CloudEventBuilder.v1(Data.V1_WITH_JSON_DATA_WITH_EXT)
32+
.withExtension(ext)
33+
.build();
34+
assertThat(given.getExtensionNames())
35+
.contains("traceparent")
36+
.doesNotContain("tracestate");
37+
38+
CloudEvent have = CloudEventBuilder.v1(given)
39+
.withoutExtension(ext)
40+
.build();
41+
42+
assertThat(have.getExtensionNames())
43+
.doesNotContain("traceparent", "tracestate");
44+
assertThat(Data.V1_WITH_JSON_DATA_WITH_EXT)
45+
.isEqualTo(have);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)