diff --git a/core/src/main/java/io/cloudevents/core/impl/BaseCloudEventBuilder.java b/core/src/main/java/io/cloudevents/core/impl/BaseCloudEventBuilder.java index dbc6a5875..aa941f73b 100644 --- a/core/src/main/java/io/cloudevents/core/impl/BaseCloudEventBuilder.java +++ b/core/src/main/java/io/cloudevents/core/impl/BaseCloudEventBuilder.java @@ -219,6 +219,10 @@ protected static IllegalStateException createMissingAttributeException(String at return new IllegalStateException("Attribute '" + attributeName + "' cannot be null"); } + protected static IllegalStateException createEmptyAttributeException(String attributeName) { + return new IllegalStateException("Attribute '" + attributeName + "' cannot be empty"); + } + /** * Validates the extension name as defined in CloudEvents spec. * diff --git a/core/src/main/java/io/cloudevents/core/v03/CloudEventBuilder.java b/core/src/main/java/io/cloudevents/core/v03/CloudEventBuilder.java index 152498cc3..d747a1629 100644 --- a/core/src/main/java/io/cloudevents/core/v03/CloudEventBuilder.java +++ b/core/src/main/java/io/cloudevents/core/v03/CloudEventBuilder.java @@ -124,6 +124,9 @@ public CloudEventV03 build() { if (type == null) { throw createMissingAttributeException("type"); } + if (subject != null && subject.isEmpty()) { + throw createEmptyAttributeException(("subject")); + } CloudEventV03 cloudEvent = new CloudEventV03(id, source, type, time, schemaurl, datacontenttype, subject, this.data, this.extensions); final CloudEventValidatorProvider validator = CloudEventValidatorProvider.getInstance(); diff --git a/core/src/main/java/io/cloudevents/core/v1/CloudEventBuilder.java b/core/src/main/java/io/cloudevents/core/v1/CloudEventBuilder.java index 060fa65df..ce26cd875 100644 --- a/core/src/main/java/io/cloudevents/core/v1/CloudEventBuilder.java +++ b/core/src/main/java/io/cloudevents/core/v1/CloudEventBuilder.java @@ -120,6 +120,9 @@ public CloudEvent build() { if (type == null) { throw createMissingAttributeException(TYPE); } + if (subject != null && subject.isEmpty()) { + throw createEmptyAttributeException(("subject")); + } CloudEvent cloudEvent = new CloudEventV1(id, source, type, datacontenttype, dataschema, subject, time, this.data, this.extensions); diff --git a/core/src/test/java/io/cloudevents/core/v03/CloudEventBuilderTest.java b/core/src/test/java/io/cloudevents/core/v03/CloudEventBuilderTest.java index 041e82d36..9feafcd69 100644 --- a/core/src/test/java/io/cloudevents/core/v03/CloudEventBuilderTest.java +++ b/core/src/test/java/io/cloudevents/core/v03/CloudEventBuilderTest.java @@ -158,4 +158,27 @@ void testMissingType() { ).hasMessageContaining("Attribute 'type' cannot be null"); } + @Test + void testMissingSubject() { + CloudEvent actual = CloudEventBuilder + .v03() + .withId("000") + .withSource(URI.create("http://localhost")) + .withType(TYPE) + .build(); + + assertThat(actual).isNotNull(); + } + + @Test + void testEmptySubject() { + assertThatCode(() -> CloudEventBuilder + .v03() + .withId("000") + .withSource(URI.create("http://localhost")) + .withType(TYPE) + .withSubject("") + .build() + ).hasMessageContaining("Attribute 'subject' cannot be empty"); + } } diff --git a/core/src/test/java/io/cloudevents/core/v1/CloudEventBuilderTest.java b/core/src/test/java/io/cloudevents/core/v1/CloudEventBuilderTest.java index 1f7c9fcc9..07eb52015 100644 --- a/core/src/test/java/io/cloudevents/core/v1/CloudEventBuilderTest.java +++ b/core/src/test/java/io/cloudevents/core/v1/CloudEventBuilderTest.java @@ -154,4 +154,27 @@ void testValidatorProvider(){ ).hasMessageContaining("Expecting sales in namespace extension"); } + @Test + void testMissingSubject() { + CloudEvent actual = CloudEventBuilder + .v1() + .withId("000") + .withSource(URI.create("http://localhost")) + .withType(TYPE) + .build(); + + assertThat(actual).isNotNull(); + } + + @Test + void testEmptySubject() { + assertThatCode(() -> CloudEventBuilder + .v1() + .withId("000") + .withSource(URI.create("http://localhost")) + .withType(TYPE) + .withSubject("") + .build() + ).hasMessageContaining("Attribute 'subject' cannot be empty"); + } }