Skip to content

Commit

Permalink
Bug fix (#48)
Browse files Browse the repository at this point in the history
* Bug fix

* Change maven release rule

* Revert "Change maven release rule"

This reverts commit 52a05e8.

* Use validation result in CloudEventValidator.Validate()

Replace the usage of the UStatus in the CloudEventValidator.Validate() method with the ValidationResult.
  • Loading branch information
neelam-kushwah authored Nov 23, 2023
1 parent 494455a commit 4be8650
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.cloudevents.CloudEvent;
import io.cloudevents.core.builder.CloudEventBuilder;
import org.eclipse.uprotocol.uuid.factory.UuidFactory;
import org.eclipse.uprotocol.uuid.serializer.LongUuidSerializer;
import org.eclipse.uprotocol.v1.UMessageType;
import org.eclipse.uprotocol.v1.UUID;
import org.eclipse.uprotocol.v1.UUri;
Expand Down Expand Up @@ -154,7 +155,7 @@ static CloudEvent notification(String source, String sink, Any protoPayload, UCl
*/
static String generateCloudEventId() {
UUID uuid = UuidFactory.Factories.UPROTOCOL.factory().create();
return uuid.toString();
return LongUuidSerializer.instance().serialize(uuid);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ public CloudEventValidator validator() {
* @param cloudEvent The CloudEvent to validate.
* @return Returns a google.rpc.Status with success or a google.rpc.Status with failure containing all the errors that were found.
*/
public UStatus validate(CloudEvent cloudEvent) {
public ValidationResult validate(CloudEvent cloudEvent) {
final String errorMessage = Stream.of(validateVersion(cloudEvent), validateId(cloudEvent),
validateSource(cloudEvent), validateType(cloudEvent), validateSink(cloudEvent))
.filter(ValidationResult::isFailure)
.map(ValidationResult::getMessage)
.collect(Collectors.joining(","));
return errorMessage.isBlank() ? ValidationResult.success().toStatus() :
UStatus.newBuilder().setCode(UCode.INVALID_ARGUMENT).setMessage(errorMessage).build();
return errorMessage.isBlank() ? ValidationResult.success() :
ValidationResult.failure(errorMessage);
}

public static ValidationResult validateVersion(CloudEvent cloudEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ void test_publish_type_cloudevent_is_valid_when_everything_is_valid_local() {
.withSource(URI.create("/body.access/1/door.front_left#Door")).withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH));
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.PUBLISH.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(ValidationResult.STATUS_SUCCESS, status);
final ValidationResult result = validator.validate(cloudEvent);
assertEquals(ValidationResult.success(), result);
}

@Test
Expand All @@ -243,8 +243,8 @@ void test_publish_type_cloudevent_is_valid_when_everything_is_valid_remote() {
.withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH));
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.PUBLISH.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(ValidationResult.STATUS_SUCCESS, status);
final ValidationResult result = validator.validate(cloudEvent);
assertEquals(ValidationResult.success(), result);
}

@Test
Expand All @@ -258,8 +258,8 @@ void test_publish_type_cloudevent_is_valid_when_everything_is_valid_remote_with_
.withExtension("sink", "//bo.cloud/petapp").withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH));
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.PUBLISH.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(ValidationResult.STATUS_SUCCESS, status);
final ValidationResult result = validator.validate(cloudEvent);
assertEquals(ValidationResult.success(), result);
}

@Test
Expand All @@ -273,8 +273,7 @@ void test_publish_type_cloudevent_is_not_valid_when_remote_with_invalid_sink() {
.withExtension("sink", "//bo.cloud").withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH));
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.PUBLISH.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(UCode.INVALID_ARGUMENT, status.getCode());
final ValidationResult status = validator.validate(cloudEvent);
assertEquals("Invalid CloudEvent sink [//bo.cloud]. Uri is missing uSoftware Entity name.",
status.getMessage());
}
Expand All @@ -289,9 +288,8 @@ void test_publish_type_cloudevent_is_not_valid_when_source_is_empty() {
.withSource(URI.create("/")).withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH));
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.PUBLISH.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(UCode.INVALID_ARGUMENT, status.getCode());
assertEquals("Invalid Publish type CloudEvent source [/]. Uri is empty.", status.getMessage());
final ValidationResult result = validator.validate(cloudEvent);
assertEquals("Invalid Publish type CloudEvent source [/]. Uri is empty.", result.getMessage());
}

@Test
Expand All @@ -301,12 +299,11 @@ void test_publish_type_cloudevent_is_not_valid_when_source_is_missing_authority(
.withSource(URI.create("/body.access")).withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH));
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.PUBLISH.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(UCode.INVALID_ARGUMENT, status.getCode());
final ValidationResult result = validator.validate(cloudEvent);
assertEquals(
"Invalid CloudEvent Id [testme]. CloudEvent Id must be of type UUIDv8.," + "Invalid Publish type " +
"CloudEvent source [/body.access]. UriPart is missing uResource name.",
status.getMessage());
result.getMessage());
}

@Test
Expand All @@ -316,12 +313,11 @@ void test_publish_type_cloudevent_is_not_valid_when_source_is_missing_message_in
.withSource(URI.create("/body.access/1/door.front_left")).withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH));
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.PUBLISH.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(UCode.INVALID_ARGUMENT, status.getCode());
final ValidationResult result = validator.validate(cloudEvent);
assertEquals(
"Invalid CloudEvent Id [testme]. CloudEvent Id must be of type UUIDv8.," + "Invalid Publish type " +
"CloudEvent source [/body.access/1/door.front_left]. UriPart is missing Message information.",
status.getMessage());
result.getMessage());
}

@Test
Expand All @@ -335,8 +331,8 @@ void test_notification_type_cloudevent_is_valid_when_everything_is_valid() {
.withExtension("sink", "//bo.cloud/petapp");
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.NOTIFICATION.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(ValidationResult.STATUS_SUCCESS, status);
final ValidationResult result = validator.validate(cloudEvent);
assertEquals(ValidationResult.success(), result);
}

@Test
Expand All @@ -349,9 +345,8 @@ void test_notification_type_cloudevent_is_not_valid_missing_sink() {
.withSource(URI.create("/body.access/1/door.front_left#Door")).withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH));
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.NOTIFICATION.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(UCode.INVALID_ARGUMENT, status.getCode());
assertEquals("Invalid CloudEvent sink. Notification CloudEvent sink must be an uri.", status.getMessage());
final ValidationResult result = validator.validate(cloudEvent);
assertEquals("Invalid CloudEvent sink. Notification CloudEvent sink must be an uri.", result.getMessage());
}

@Test
Expand All @@ -365,10 +360,9 @@ void test_notification_type_cloudevent_is_not_valid_invalid_sink() {
.withExtension("sink", "//bo.cloud");
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.NOTIFICATION.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(UCode.INVALID_ARGUMENT, status.getCode());
final ValidationResult result = validator.validate(cloudEvent);
assertEquals("Invalid Notification type CloudEvent sink [//bo.cloud]. Uri is missing uSoftware Entity name.",
status.getMessage());
result.getMessage());
}


Expand All @@ -383,8 +377,8 @@ void test_request_type_cloudevent_is_valid_when_everything_is_valid() {
.withExtension("sink", "//VCU.myvin/body.access/1/rpc.UpdateDoor");
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.REQUEST.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(ValidationResult.STATUS_SUCCESS, status);
final ValidationResult result = validator.validate(cloudEvent);
assertEquals(ValidationResult.success(), result);
}

@Test
Expand All @@ -399,12 +393,11 @@ void test_request_type_cloudevent_is_not_valid_invalid_source() {
.withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_REQUEST));
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.REQUEST.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(UCode.INVALID_ARGUMENT, status.getCode());
final ValidationResult result = validator.validate(cloudEvent);
assertEquals(
"Invalid RPC Request CloudEvent source [//bo.cloud/petapp//dog]. " + "Invalid RPC uri application " +
"response topic. UriPart is missing rpc.response.",
status.getMessage());
result.getMessage());
}

@Test
Expand All @@ -417,11 +410,10 @@ void test_request_type_cloudevent_is_not_valid_missing_sink() {
.withSource(URI.create("//bo.cloud/petapp//rpc.response")).withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_REQUEST));
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.REQUEST.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(UCode.INVALID_ARGUMENT, status.getCode());
final ValidationResult result = validator.validate(cloudEvent);
assertEquals(
"Invalid RPC Request CloudEvent sink. Request CloudEvent sink must be uri for the method to be called.",
status.getMessage());
result.getMessage());
}

@Test
Expand All @@ -435,12 +427,11 @@ void test_request_type_cloudevent_is_not_valid_invalid_sink_not_rpc_command() {
.withExtension("sink", "//VCU.myvin/body.access/1/UpdateDoor");
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.REQUEST.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(UCode.INVALID_ARGUMENT, status.getCode());
final ValidationResult result = validator.validate(cloudEvent);
assertEquals(
"Invalid RPC Request CloudEvent sink [//VCU.myvin/body.access/1/UpdateDoor]. " + "Invalid RPC method " +
"uri. UriPart should be the method to be called, or method from response.",
status.getMessage());
result.getMessage());
}

@Test
Expand All @@ -454,8 +445,8 @@ void test_response_type_cloudevent_is_valid_when_everything_is_valid() {
.withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_RESPONSE)).withExtension("sink", "//bo.cloud/petapp//rpc.response");
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.RESPONSE.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(ValidationResult.STATUS_SUCCESS, status);
final ValidationResult result = validator.validate(cloudEvent);
assertEquals(ValidationResult.success(), result);
}

@Test
Expand All @@ -469,12 +460,11 @@ void test_response_type_cloudevent_is_not_valid_invalid_source() {
.withExtension("sink", "//bo.cloud/petapp//rpc.response").withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_RESPONSE));
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.RESPONSE.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(UCode.INVALID_ARGUMENT, status.getCode());
final ValidationResult result = validator.validate(cloudEvent);
assertEquals(
"Invalid RPC Response CloudEvent source [//VCU.myvin/body.access/1/UpdateDoor]. " + "Invalid RPC " +
"method uri. UriPart should be the method to be called, or method from response.",
status.getMessage());
result.getMessage());
}

@Test
Expand All @@ -488,13 +478,12 @@ void test_response_type_cloudevent_is_not_valid_missing_sink_and_invalid_source(
.withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_RESPONSE));
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.RESPONSE.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(UCode.INVALID_ARGUMENT, status.getCode());
final ValidationResult result = validator.validate(cloudEvent);
assertEquals(
"Invalid RPC Response CloudEvent source [//VCU.myvin/body.access/1/UpdateDoor]. " + "Invalid RPC " +
"method uri. UriPart should be the method to be called, or method from response.," + "Invalid" +
" CloudEvent sink. Response CloudEvent sink must be uri the destination of the response.",
status.getMessage());
result.getMessage());
}

@Test
Expand All @@ -508,13 +497,12 @@ void test_response_type_cloudevent_is_not_valid_invalid_sink() {
.withExtension("sink", "//bo.cloud");
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.RESPONSE.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(UCode.INVALID_ARGUMENT, status.getCode());
final ValidationResult result = validator.validate(cloudEvent);
assertEquals(
"Invalid RPC Response CloudEvent source [//VCU.myvin]. Invalid RPC method uri. Uri is missing " +
"uSoftware Entity name.,Invalid RPC Response CloudEvent sink [//bo.cloud]. Invalid RPC uri " +
"application response topic. Uri is missing uSoftware Entity name.",
status.getMessage());
result.getMessage());
}

@Test
Expand All @@ -528,14 +516,13 @@ void test_response_type_cloudevent_is_not_valid_invalid_source_not_rpc_command()
.withExtension("sink", "//VCU.myvin/body.access/1/UpdateDoor");
CloudEvent cloudEvent = builder.build();
final CloudEventValidator validator = CloudEventValidator.Validators.RESPONSE.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(UCode.INVALID_ARGUMENT, status.getCode());
final ValidationResult result = validator.validate(cloudEvent);
assertEquals(
"Invalid RPC Response CloudEvent source [//bo.cloud/petapp/1/dog]. Invalid RPC method uri. UriPart " +
"should be the method to be called, or method from response.," + "Invalid RPC Response " +
"CloudEvent sink [//VCU.myvin/body.access/1/UpdateDoor]. " + "Invalid RPC uri application " +
"response topic. UriPart is missing rpc.response.",
status.getMessage());
result.getMessage());
}

private CloudEventBuilder buildBaseCloudEventBuilderForTest() {
Expand Down Expand Up @@ -588,8 +575,8 @@ public void test_create_a_v6_cloudevent_and_validate_it_against_sdk() {
protoPayload.getTypeUrl(), attributes).withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)).build();

final CloudEventValidator validator = CloudEventValidator.Validators.PUBLISH.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(UCode.OK, status.getCode());
final ValidationResult result = validator.validate(cloudEvent);
assertTrue(result.isSuccess());
assertFalse(UCloudEvent.isExpired(cloudEvent));
}

Expand All @@ -614,8 +601,8 @@ public void test_create_an_expired_v6_cloudevent() {
protoPayload.getTypeUrl(), attributes).withType(UCloudEvent.getEventType(UMessageType.UMESSAGE_TYPE_PUBLISH)).build();

final CloudEventValidator validator = CloudEventValidator.Validators.PUBLISH.validator();
final UStatus status = validator.validate(cloudEvent);
assertEquals(UCode.OK, status.getCode());
final ValidationResult result = validator.validate(cloudEvent);
assertTrue(result.isSuccess());
assertTrue(UCloudEvent.isExpired(cloudEvent));
}

Expand Down

0 comments on commit 4be8650

Please sign in to comment.