Skip to content

Commit 30fd676

Browse files
authored
fix: Adding withoutData, withoutDataContentType and withoutDataSchema to CloudEventBuilder (#374)
Signed-off-by: Johan Haleby <johan.haleby@gmail.com>
1 parent ff07dd8 commit 30fd676

File tree

3 files changed

+89
-10
lines changed

3 files changed

+89
-10
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,28 @@ public interface CloudEventBuilder extends CloudEventWriter<CloudEvent> {
141141
*/
142142
CloudEventBuilder withData(String dataContentType, URI dataSchema, CloudEventData data);
143143

144+
/**
145+
* Remove the {@code datacontenttype}, {@code dataschema} and {@code data} from the event
146+
*
147+
* @return self
148+
*/
149+
CloudEventBuilder withoutData();
150+
151+
/**
152+
* Remove the {@code dataschema} from the event
153+
*
154+
* @return self
155+
*/
156+
CloudEventBuilder withoutDataSchema();
157+
158+
159+
/**
160+
* Remove the {@code datacontenttype} from the event
161+
*
162+
* @return self
163+
*/
164+
CloudEventBuilder withoutDataContentType();
165+
144166
/**
145167
* Set an extension with provided key and string value
146168
*
@@ -309,9 +331,9 @@ static CloudEventBuilder from(@Nonnull CloudEvent event) {
309331
static CloudEventBuilder fromContext(@Nonnull CloudEventContext context) {
310332
switch (context.getSpecVersion()) {
311333
case V1:
312-
return new io.cloudevents.core.v1.CloudEventBuilder(context);
334+
return new io.cloudevents.core.v1.CloudEventBuilder(context);
313335
case V03:
314-
return new io.cloudevents.core.v03.CloudEventBuilder(context);
336+
return new io.cloudevents.core.v03.CloudEventBuilder(context);
315337
}
316338
throw new IllegalStateException(
317339
"The provided spec version doesn't exist. Please make sure your io.cloudevents deps versions are aligned."

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,24 @@ public SELF withData(String dataContentType, URI dataSchema, CloudEventData data
9898
return this.self;
9999
}
100100

101+
@Override
102+
public CloudEventBuilder withoutData() {
103+
this.data = null;
104+
return this.self;
105+
}
106+
107+
@Override
108+
public CloudEventBuilder withoutDataSchema() {
109+
withDataSchema(null);
110+
return this.self;
111+
}
112+
113+
@Override
114+
public CloudEventBuilder withoutDataContentType() {
115+
withDataContentType(null);
116+
return this.self;
117+
}
118+
101119
public SELF withExtension(@Nonnull String key, @Nonnull String value) {
102120
if (!isValidExtensionName(key)) {
103121
throw CloudEventRWException.newInvalidExtensionName(key);
@@ -189,6 +207,7 @@ public CloudEvent end() {
189207
protected static IllegalStateException createMissingAttributeException(String attributeName) {
190208
return new IllegalStateException("Attribute '" + attributeName + "' cannot be null");
191209
}
210+
192211
/**
193212
* Validates the extension name as defined in CloudEvents spec.
194213
*
@@ -197,7 +216,7 @@ protected static IllegalStateException createMissingAttributeException(String at
197216
* @see <a href="https://github.com/cloudevents/spec/blob/master/spec.md#attribute-naming-convention">attribute-naming-convention</a>
198217
*/
199218
private static boolean isValidExtensionName(String name) {
200-
for(int i = 0; i < name.length(); i++) {
219+
for (int i = 0; i < name.length(); i++) {
201220
if (!isValidChar(name.charAt(i))) {
202221
return false;
203222
}

core/src/test/java/io/cloudevents/core/impl/BaseCloudEventBuilderTest.java

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package io.cloudevents.core.impl;
22

3-
import static org.assertj.core.api.Assertions.assertThat;
4-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
5-
import static org.junit.jupiter.api.Assertions.assertEquals;
6-
import static org.junit.jupiter.api.Assertions.assertNotNull;
7-
import static org.junit.jupiter.api.Assertions.assertThrows;
8-
import static org.junit.jupiter.api.Assertions.assertTrue;
9-
103
import io.cloudevents.CloudEvent;
114
import io.cloudevents.core.builder.CloudEventBuilder;
125
import io.cloudevents.core.extensions.DistributedTracingExtension;
136
import io.cloudevents.core.test.Data;
147
import org.junit.jupiter.api.Test;
158

9+
import java.util.Objects;
10+
11+
import static io.cloudevents.core.test.Data.*;
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
import static org.junit.jupiter.api.Assertions.*;
14+
1615
public class BaseCloudEventBuilderTest {
1716

1817
@Test
@@ -101,4 +100,43 @@ public void testBinaryExtension() {
101100
assertEquals(Data.BINARY_VALUE, given.getExtension(EXT_NAME));
102101

103102
}
103+
104+
@Test
105+
public void withoutDataRemovesDataAttributeFromCopiedCloudEvent() {
106+
CloudEvent original = CloudEventBuilder.v1(Data.V1_WITH_JSON_DATA_WITH_EXT).build();
107+
CloudEvent copy = CloudEventBuilder.v1(original).withoutData().build();
108+
109+
assertAll(
110+
() -> assertThat(copy.getData()).isNull(),
111+
() -> assertThat(copy.getDataContentType()).isEqualTo(DATACONTENTTYPE_JSON),
112+
() -> assertThat(copy.getDataSchema()).isEqualTo(DATASCHEMA)
113+
);
114+
115+
}
116+
117+
@Test
118+
public void withoutDataContentTypeRemovesDataContentTypeAttributeFromCopiedCloudEvent() {
119+
CloudEvent original = CloudEventBuilder.v1(Data.V1_WITH_JSON_DATA_WITH_EXT).build();
120+
CloudEvent copy = CloudEventBuilder.v1(original).withoutDataContentType().build();
121+
122+
assertAll(
123+
() -> assertThat(Objects.requireNonNull(copy.getData()).toBytes()).isEqualTo(DATA_JSON_SERIALIZED),
124+
() -> assertThat(copy.getDataContentType()).isNull(),
125+
() -> assertThat(copy.getDataSchema()).isEqualTo(DATASCHEMA)
126+
);
127+
128+
}
129+
130+
@Test
131+
public void withoutDataSchemaRemovesDataSchemaAttributeFromCopiedCloudEvent() {
132+
CloudEvent original = CloudEventBuilder.v1(Data.V1_WITH_JSON_DATA_WITH_EXT).build();
133+
CloudEvent copy = CloudEventBuilder.v1(original).withoutDataSchema().build();
134+
135+
assertAll(
136+
() -> assertThat(Objects.requireNonNull(copy.getData()).toBytes()).isEqualTo(DATA_JSON_SERIALIZED),
137+
() -> assertThat(copy.getDataContentType()).isEqualTo(DATACONTENTTYPE_JSON),
138+
() -> assertThat(copy.getDataSchema()).isNull()
139+
);
140+
141+
}
104142
}

0 commit comments

Comments
 (0)