Skip to content

Commit

Permalink
feat: Customizing Cloudevents validation
Browse files Browse the repository at this point in the history
Signed-off-by: vbhat6 <[email protected]>
  • Loading branch information
vbhat6 committed Feb 7, 2024
1 parent 982d4ae commit 1beaf00
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,15 @@
*/
public class CloudEventValidatorProvider {

private static CloudEventValidatorProvider cloudEventValidatorProvider;
private static final CloudEventValidatorProvider cloudEventValidatorProvider = new CloudEventValidatorProvider();

private ServiceLoader<CloudEventValidator> loader;
private final ServiceLoader<CloudEventValidator> loader;

private CloudEventValidatorProvider(){
loader = ServiceLoader.load(CloudEventValidator.class);
}

public static synchronized CloudEventValidatorProvider getInstance(){
if(cloudEventValidatorProvider == null){
cloudEventValidatorProvider = new CloudEventValidatorProvider();
}
public static CloudEventValidatorProvider getInstance() {
return cloudEventValidatorProvider;
}

Expand All @@ -48,23 +45,8 @@ public static synchronized CloudEventValidatorProvider getInstance(){
* @param cloudEvent
*/
public void validate(CloudEvent cloudEvent){
try{
//
Iterator<CloudEventValidator> validatorIterator = loader.iterator();
while (validatorIterator.hasNext()){
CloudEventValidator validator = validatorIterator.next();
validator.validate(cloudEvent);

}
} catch (ServiceConfigurationError serviceError) {

serviceError.printStackTrace();
for (final CloudEventValidator validator : loader) {
validator.validate(cloudEvent);
}

}





}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
*/
package io.cloudevents.core.v03;

import io.cloudevents.CloudEvent;
import io.cloudevents.SpecVersion;
import io.cloudevents.core.CloudEventUtils;
import io.cloudevents.core.impl.BaseCloudEventBuilder;
import io.cloudevents.core.provider.CloudEventValidatorProvider;
import io.cloudevents.core.v1.CloudEventV1;
import io.cloudevents.rw.CloudEventContextReader;
import io.cloudevents.rw.CloudEventContextWriter;
import io.cloudevents.rw.CloudEventRWException;
Expand Down Expand Up @@ -122,7 +125,11 @@ public CloudEventV03 build() {
throw createMissingAttributeException("type");
}

return new CloudEventV03(id, source, type, time, schemaurl, datacontenttype, subject, this.data, this.extensions);
CloudEventV03 cloudEvent = new CloudEventV03(id, source, type, time, schemaurl, datacontenttype, subject, this.data, this.extensions);
final CloudEventValidatorProvider validator = CloudEventValidatorProvider.getInstance();
validator.validate(cloudEvent);

return cloudEvent;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public CloudEvent build() {

CloudEvent cloudEvent = new CloudEventV1(id, source, type, datacontenttype, dataschema, subject, time, this.data, this.extensions);

CloudEventValidatorProvider validator = CloudEventValidatorProvider.getInstance();
final CloudEventValidatorProvider validator = CloudEventValidatorProvider.getInstance();
validator.validate(cloudEvent);

return cloudEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,16 @@ void testMissingType() {
).hasMessageContaining("Attribute 'type' cannot be null");
}

@Test
void testValidatorProvider(){
assertThatCode(() -> CloudEventBuilder
.v1()
.withId("000")
.withSource(URI.create("http://localhost"))
.withType(TYPE)
.withExtension("namespace", "order")
.build()
).hasMessageContaining("Expecting sales in namespace extension");
}

}

0 comments on commit 1beaf00

Please sign in to comment.