diff --git a/src/main/java/org/eclipse/uprotocol/utransport/datamodel/UAttributes.java b/src/main/java/org/eclipse/uprotocol/utransport/datamodel/UAttributes.java index 5a3cf87f..129b6930 100644 --- a/src/main/java/org/eclipse/uprotocol/utransport/datamodel/UAttributes.java +++ b/src/main/java/org/eclipse/uprotocol/utransport/datamodel/UAttributes.java @@ -148,7 +148,7 @@ public Optional serializationHint() { * @return Returns an Optional destination URI attribute. */ public Optional sink() { - return sink == null || sink.isEmpty() ? Optional.empty() : Optional.of(sink); + return sink == null ? Optional.empty() : Optional.of(sink); } /** @@ -269,7 +269,7 @@ public UAttributesBuilder withSink(UUri sink) { * @param plevel the permission level of the message. * @return Returns the UAttributesBuilder with the configured plevel. */ - public UAttributesBuilder withPlevel(Integer plevel) { + public UAttributesBuilder withPermissionLevel(Integer plevel) { this.plevel = plevel; return this; } @@ -279,7 +279,7 @@ public UAttributesBuilder withPlevel(Integer plevel) { * @param commstatus the communication status of the message. * @return Returns the UAttributesBuilder with the configured commstatus. */ - public UAttributesBuilder withCommstatus(Integer commstatus) { + public UAttributesBuilder withCommStatus(Integer commstatus) { this.commstatus = commstatus; return this; } @@ -289,7 +289,7 @@ public UAttributesBuilder withCommstatus(Integer commstatus) { * @param reqid the request ID. * @return Returns the UAttributesBuilder with the configured reqid. */ - public UAttributesBuilder withReqid(UUID reqid) { + public UAttributesBuilder withReqId(UUID reqid) { this.reqid = reqid; return this; } diff --git a/src/main/java/org/eclipse/uprotocol/utransport/validate/UAttributesValidator.java b/src/main/java/org/eclipse/uprotocol/utransport/validate/UAttributesValidator.java index 34926e33..4cb3c9da 100644 --- a/src/main/java/org/eclipse/uprotocol/utransport/validate/UAttributesValidator.java +++ b/src/main/java/org/eclipse/uprotocol/utransport/validate/UAttributesValidator.java @@ -9,10 +9,10 @@ import org.eclipse.uprotocol.uri.validator.UriValidator; import org.eclipse.uprotocol.utransport.datamodel.UAttributes; import org.eclipse.uprotocol.utransport.datamodel.UMessageType; +import org.eclipse.uprotocol.utransport.datamodel.USerializationHint; import org.eclipse.uprotocol.utransport.datamodel.UStatus; import org.eclipse.uprotocol.uuid.factory.UUIDUtils; - -import com.google.rpc.Code; +import org.eclipse.uprotocol.utransport.datamodel.UStatus.Code; /** * Abstract class for validating UAttributes. @@ -67,32 +67,35 @@ public UStatus validate(UAttributes attributes) { validateCommStatus(attributes), validateTtl(attributes), validatePermissionLevel(attributes), - validateCorrelationId(attributes)) + validateReqId(attributes)) .filter(UStatus::isFailed) .map(UStatus::msg) .collect(Collectors.joining(",")); return errorMessage.isBlank() ? UStatus.ok() : - UStatus.failed("Invalid argument", Code.INVALID_ARGUMENT_VALUE); + UStatus.failed(errorMessage, Code.INVALID_ARGUMENT); } public UStatus validatePriority(UAttributes attributes) { - return attributes.priority() != null ? UStatus.ok() : UStatus.failed(); + return attributes.priority() != null ? UStatus.ok() : + UStatus.failed("Invalid Priority", Code.INVALID_ARGUMENT.value()); } public UStatus validateTtl(UAttributes attributes) { Optional ttl = attributes.ttl(); if (ttl.isPresent()) { - return ttl.get() > 0 ? UStatus.ok() : UStatus.failed(); + return ttl.get() > 0 ? UStatus.ok() : + UStatus.failed("Invalid TTL", Code.INVALID_ARGUMENT.value()); } return UStatus.ok(); } public UStatus validateId(UAttributes attributes) { try { - return UUIDUtils.isUuid(attributes.id()) ? UStatus.ok() : UStatus.failed(); + return UUIDUtils.isUuid(attributes.id()) ? UStatus.ok() : + UStatus.failed("Invalid UUID", Code.INVALID_ARGUMENT.value()); } catch (Exception e) { - return UStatus.failed(); + return UStatus.failed("Invalid UUID", Code.INVALID_ARGUMENT.value()); } } @@ -102,7 +105,8 @@ public UStatus validateId(UAttributes attributes) { * @return Returns a UStatus indicating if the Uri is valid or not. */ public UStatus validateSink(UAttributes attributes) { - if (!attributes.sink().isEmpty()){ + Optional sink = attributes.sink(); + if (sink.isPresent()) { return UriValidator.validate(attributes.sink().get()); } return UStatus.ok(); @@ -114,8 +118,12 @@ public UStatus validateSink(UAttributes attributes) { * @return Returns a UStatus indicating if the commStatus is valid or not. */ public UStatus validateCommStatus(UAttributes attributes) { - if (!attributes.commstatus().isEmpty()){ - return Code.forNumber(attributes.commstatus().get()) != null ? UStatus.ok() : UStatus.failed(); + Optional commStatus = attributes.commstatus(); + + if (commStatus.isPresent()) { + Optional code = Code.from(commStatus.get()); + return code.isPresent() ? UStatus.ok() : + UStatus.failed("Invalid Communication Status Code", Code.INVALID_ARGUMENT.value()); } return UStatus.ok(); } @@ -128,8 +136,8 @@ public UStatus validateCommStatus(UAttributes attributes) { public UStatus validatePermissionLevel(UAttributes attributes) { final Optional plevel = attributes.plevel(); if (plevel.isPresent()) { - - return plevel.get() > 0 ? UStatus.ok() : UStatus.failed(); + return plevel.get() >= 0 ? UStatus.ok() : + UStatus.failed("Invalid Permission Level", Code.INVALID_ARGUMENT.value()); } return UStatus.ok(); } @@ -139,11 +147,12 @@ public UStatus validatePermissionLevel(UAttributes attributes) { * @param attributes UAttributes to validate * @return Returns a UStatus indicating if the correlationId is valid or not. */ - public UStatus validateCorrelationId(UAttributes attributes) { + public UStatus validateReqId(UAttributes attributes) { final Optional correlationId = attributes.reqid(); if (correlationId.isPresent()) { - return UUIDUtils.isUuid(correlationId.get()) ? UStatus.ok() : UStatus.failed(); + return UUIDUtils.isUuid(correlationId.get()) ? UStatus.ok() : + UStatus.failed("Invalid UUID", Code.INVALID_ARGUMENT.value()); } return UStatus.ok(); } @@ -170,7 +179,7 @@ private static class Publish extends UAttributesValidator { */ @Override public UStatus validateType(UAttributes attributes) { - return attributes.type() == UMessageType.PUBLISH ? UStatus.ok() : UStatus.failed(); + return attributes.type() == UMessageType.PUBLISH ? UStatus.ok() : UStatus.failed("Wrong Attribute Type", Code.INVALID_ARGUMENT.value()); } } @@ -194,7 +203,7 @@ public UStatus validateSink(UAttributes attributes) { final Optional sink = attributes.sink(); if (!sink.isPresent()) { - return UStatus.failed("Missing Sink", Code.INVALID_ARGUMENT_VALUE); + return UStatus.failed("Missing Sink", Code.INVALID_ARGUMENT.value()); } return UriValidator.validateRpcResponse(sink.get()); } @@ -208,7 +217,7 @@ public UStatus validateSink(UAttributes attributes) { public UStatus validateTtl(UAttributes attributes) { final Optional ttl = attributes.ttl(); if (!ttl.isPresent()) { - return UStatus.failed("Missing TTL", Code.INVALID_ARGUMENT_VALUE); + return UStatus.failed("Missing TTL", Code.INVALID_ARGUMENT.value()); } else { return ttl.get() > 0 ? UStatus.ok() : UStatus.failed(); } @@ -223,7 +232,8 @@ private static class Response extends UAttributesValidator { @Override public UStatus validateType(UAttributes attributes) { - return attributes.type() == UMessageType.RESPONSE ? UStatus.ok() : UStatus.failed(); + return attributes.type() == UMessageType.RESPONSE ? UStatus.ok() : + UStatus.failed("Invalid Type", Code.INVALID_ARGUMENT.value()); } /** @@ -236,7 +246,7 @@ public UStatus validateSink(UAttributes attributes) { final Optional sink = attributes.sink(); if (!sink.isPresent()) { - return UStatus.failed("Missing Sink", Code.INVALID_ARGUMENT_VALUE); + return UStatus.failed("Missing Sink", Code.INVALID_ARGUMENT.value()); } return UriValidator.validateRpcMethod(sink.get()); } @@ -247,13 +257,14 @@ public UStatus validateSink(UAttributes attributes) { * @return Returns a UStatus indicating if the correlationId is valid or not. */ @Override - public UStatus validateCorrelationId(UAttributes attributes) { + public UStatus validateReqId(UAttributes attributes) { final Optional correlationId = attributes.reqid(); if (!correlationId.isPresent()) { - return UStatus.failed("Missing correlationId", Code.INVALID_ARGUMENT_VALUE); + return UStatus.failed("Missing correlationId", Code.INVALID_ARGUMENT.value()); } - return UUIDUtils.isUuid(correlationId.get()) ? UStatus.ok() : UStatus.failed(); + return UUIDUtils.isUuid(correlationId.get()) ? UStatus.ok() : + UStatus.failed("Invalid UUID", Code.INVALID_ARGUMENT.value()); } } diff --git a/src/test/java/org/eclipse/uprotocol/utransport/validator/UAttributesValidatorTest.java b/src/test/java/org/eclipse/uprotocol/utransport/validator/UAttributesValidatorTest.java index 09e19627..b77a750d 100644 --- a/src/test/java/org/eclipse/uprotocol/utransport/validator/UAttributesValidatorTest.java +++ b/src/test/java/org/eclipse/uprotocol/utransport/validator/UAttributesValidatorTest.java @@ -22,14 +22,22 @@ package org.eclipse.uprotocol.utransport.validator; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import java.util.UUID; + +import org.eclipse.uprotocol.uri.datamodel.UUri; import org.eclipse.uprotocol.uri.factory.UriFactory; +import org.eclipse.uprotocol.uri.validator.UriValidator; import org.eclipse.uprotocol.utransport.datamodel.UAttributes; import org.eclipse.uprotocol.utransport.datamodel.UMessageType; import org.eclipse.uprotocol.utransport.datamodel.UPriority; import org.eclipse.uprotocol.utransport.datamodel.USerializationHint; +import org.eclipse.uprotocol.utransport.datamodel.UStatus; import org.eclipse.uprotocol.utransport.datamodel.UAttributes.UAttributesBuilder; +import org.eclipse.uprotocol.utransport.datamodel.UStatus.Code; import org.eclipse.uprotocol.utransport.validate.UAttributesValidator; import org.eclipse.uprotocol.uuid.factory.UUIDFactory; import org.junit.jupiter.api.DisplayName; @@ -69,5 +77,298 @@ public void test_fetching_validator_for_valid_types() { .build()); assert(invalid instanceof UAttributesValidator); } + + @Test + @DisplayName("test validating invalid types") + public void test_validator_invalid_types() { + final UAttributes attributes = new UAttributesBuilder() + .withId(UUIDFactory.Factories.UPROTOCOL.factory().create()) + .withPriority(UPriority.LOW) + .build(); + + final UAttributesValidator validator = UAttributesValidator.getValidator(attributes); + assert(validator instanceof UAttributesValidator); + final UStatus status = validator.validate(attributes); + assertTrue(status.isFailed()); + assertEquals(status.getCode(), Code.INVALID_ARGUMENT.value()); + assertEquals(status.msg(), "Wrong Attribute Type"); + } + + @Test + @DisplayName("test validating_valid_publish_messagetypes") + public void test_validating_valid_publish_messagetypes() { + final UAttributes attributes = new UAttributesBuilder() + .withId(UUIDFactory.Factories.UPROTOCOL.factory().create()) + .withPriority(UPriority.LOW) + .withType(UMessageType.PUBLISH) + .build(); + + final UAttributesValidator validator = UAttributesValidator.getValidator(attributes); + assert(validator instanceof UAttributesValidator); + final UStatus status = validator.validate(attributes); + assertTrue(status.isSuccess()); + assertEquals(status.msg(), "ok"); + } + + @Test + @DisplayName("test validating invalid priority attribute") + public void test_validating_invalid_priority_attribute() { + final UAttributes attributes = new UAttributesBuilder() + .withPriority(null) + .build(); + + final UAttributesValidator validator = UAttributesValidator.Validators.PUBLISH.validator(); + + final UStatus status = validator.validatePriority(attributes); + assertTrue(status.isFailed()); + assertEquals(status.getCode(), Code.INVALID_ARGUMENT.value()); + assertEquals(status.msg(), "Invalid Priority"); + } + + @Test + @DisplayName("test validating valid priority attribute") + public void test_validating_valid_priority_attribute() { + final UAttributes attributes = new UAttributesBuilder() + .withPriority(UPriority.LOW) + .build(); + + final UAttributesValidator validator = UAttributesValidator.Validators.PUBLISH.validator(); + final UStatus status = validator.validatePriority(attributes); + assertEquals(status, UStatus.ok()); + } + + @Test + @DisplayName("test validating invalid ttl attribute") + public void test_validating_invalid_ttl_attribute() { + final UAttributes attributes = new UAttributesBuilder() + .withTtl(-1) + .build(); + + final UAttributesValidator validator = UAttributesValidator.Validators.PUBLISH.validator(); + final UStatus status = validator.validateTtl(attributes); + assertTrue(status.isFailed()); + assertEquals(status.getCode(), Code.INVALID_ARGUMENT.value()); + assertEquals(status.msg(), "Invalid TTL"); + } + + @Test + @DisplayName("test validating valid ttl attribute") + public void test_validating_valid_ttl_attribute() { + final UAttributes attributes = new UAttributesBuilder() + .withTtl(100) + .build(); + + final UAttributesValidator validator = UAttributesValidator.Validators.PUBLISH.validator(); + final UStatus status = validator.validateTtl(attributes); + assertEquals(status, UStatus.ok()); + } + + @Test + @DisplayName("test validating invalid id attribute") + public void test_validating_invalid_id_attribute() { + final UAttributes attributes = new UAttributesBuilder() + .withId(null) + .build(); + final UAttributes attributes1 = new UAttributesBuilder() + .withId(UUID.randomUUID()) + .build(); + + final UAttributesValidator validator = UAttributesValidator.Validators.PUBLISH.validator(); + UStatus status = validator.validateId(attributes); + assertTrue(status.isFailed()); + assertEquals(status.getCode(), Code.INVALID_ARGUMENT.value()); + assertEquals(status.msg(), "Invalid UUID"); + + status = validator.validateId(attributes1); + assertTrue(status.isFailed()); + assertEquals(status.getCode(), Code.INVALID_ARGUMENT.value()); + assertEquals(status.msg(), "Invalid UUID"); + } + + @Test + @DisplayName("test validating valid id attribute") + public void test_validating_valid_id_attribute() { + final UAttributes attributes = new UAttributesBuilder() + .withId(UUIDFactory.Factories.UPROTOCOL.factory().create()) + .build(); + + final UAttributesValidator validator = UAttributesValidator.Validators.PUBLISH.validator(); + final UStatus status = validator.validateId(attributes); + assertEquals(status, UStatus.ok()); + } + + @Test + @DisplayName("test validating invalid sink attribute") + public void test_validating_invalid_sink_attribute() { + final UUri uri = UriFactory.parseFromUri("//"); + final UAttributes attributes = new UAttributesBuilder() + .withSink(uri) + .build(); + + final UAttributesValidator validator = UAttributesValidator.Validators.PUBLISH.validator(); + final UStatus status = validator.validateSink(attributes); + + assertTrue(status.isFailed()); + assertEquals(status.getCode(), Code.INVALID_ARGUMENT.value()); + assertEquals(status.msg(), "Uri is configured to be remote and is missing uAuthority device name."); + } + + @Test + @DisplayName("test validating valid sink attribute") + public void test_validating_valid_sink_attribute() { + final UUri uri = UriFactory.parseFromUri("/haartley/1"); + final UAttributes attributes = new UAttributesBuilder() + .withSink(uri) + .build(); + + final UAttributesValidator validator = UAttributesValidator.Validators.PUBLISH.validator(); + final UStatus status = validator.validateSink(attributes); + assertEquals(status, UStatus.ok()); + } + + @Test + @DisplayName("test validating invalid ReqId attribute") + public void test_validating_invalid_ReqId_attribute() { + final UAttributes attributes = new UAttributesBuilder() + .withReqId(UUID.randomUUID()) + .build(); + + final UAttributesValidator validator = UAttributesValidator.Validators.PUBLISH.validator(); + final UStatus status = validator.validateReqId(attributes); + assertTrue(status.isFailed()); + assertEquals(status.getCode(), Code.INVALID_ARGUMENT.value()); + assertEquals(status.msg(), "Invalid UUID"); + } + + @Test + @DisplayName("test validating valid ReqId attribute") + public void test_validating_valid_ReqId_attribute() { + final UAttributes attributes = new UAttributesBuilder() + .withReqId(UUIDFactory.Factories.UPROTOCOL.factory().create()) + .build(); + + final UAttributesValidator validator = UAttributesValidator.Validators.PUBLISH.validator(); + final UStatus status = validator.validateReqId(attributes); + assertEquals(status, UStatus.ok()); + } + + + @Test + @DisplayName("test validating invalid PermissionLevel attribute") + public void test_validating_invalid_PermissionLevel_attribute() { + final UAttributes attributes = new UAttributesBuilder() + .withPermissionLevel(-1) + .build(); + + final UAttributesValidator validator = UAttributesValidator.Validators.PUBLISH.validator(); + final UStatus status = validator.validatePermissionLevel(attributes); + assertTrue(status.isFailed()); + assertEquals(status.getCode(), Code.INVALID_ARGUMENT.value()); + assertEquals(status.msg(), "Invalid Permission Level"); + } + + @Test + @DisplayName("test validating valid PermissionLevel attribute") + public void test_validating_valid_PermissionLevel_attribute() { + final UAttributes attributes = new UAttributesBuilder() + .withPermissionLevel(0) + .build(); + + final UAttributesValidator validator = UAttributesValidator.Validators.PUBLISH.validator(); + final UStatus status = validator.validatePermissionLevel(attributes); + assertEquals(status, UStatus.ok()); + } + + @Test + @DisplayName("test validating invalid commstatus attribute") + public void test_validating_invalid_commstatus_attribute() { + final UAttributes attributes = new UAttributesBuilder() + .withCommStatus(100) + .build(); + + final UAttributesValidator validator = UAttributesValidator.Validators.PUBLISH.validator(); + final UStatus status = validator.validateCommStatus(attributes); + assertTrue(status.isFailed()); + assertEquals(status.getCode(), Code.INVALID_ARGUMENT.value()); + assertEquals(status.msg(), "Invalid Communication Status Code"); + } + + @Test + @DisplayName("test validating valid commstatus attribute") + public void test_validating_valid_commstatus_attribute() { + final UAttributes attributes = new UAttributesBuilder() + .withCommStatus(Code.ABORTED.value()) + .build(); + + final UAttributesValidator validator = UAttributesValidator.Validators.PUBLISH.validator(); + final UStatus status = validator.validateCommStatus(attributes); + assertEquals(status, UStatus.ok()); + } + + + @Test + @DisplayName("test validating request message types") + public void test_validating_request_message_types() { + final UUri sink = UriFactory.parseFromUri("/hartley/1/rpc.response"); + final UAttributes attributes = new UAttributesBuilder() + .withId(UUIDFactory.Factories.UPROTOCOL.factory().create()) + .withPriority(UPriority.NETWORK_CONTROL) + .withType(UMessageType.REQUEST) + .withSink(sink) + .withTtl(100) + .build(); + + final UAttributesValidator validator = UAttributesValidator.getValidator(attributes); + assert(validator instanceof UAttributesValidator); + final UStatus status = validator.validate(attributes); + assertTrue(status.isSuccess()); + assertEquals(status.msg(), "ok"); + } + + @Test + @DisplayName("test validating request validator using wrong messagetype") + public void test_validating_request_validator_with_wrong_messagetype() { + final UAttributes attributes = new UAttributesBuilder() + .withType(UMessageType.PUBLISH) + .build(); + + final UAttributesValidator validator = UAttributesValidator.Validators.REQUEST.validator(); + assert(validator instanceof UAttributesValidator); + final UStatus status = validator.validate(attributes); + assertTrue(status.isFailed()); + assertEquals(status.getCode(), Code.INVALID_ARGUMENT.value()); + assertEquals(status.msg(), "failed,Invalid UUID,Missing Sink,Invalid Priority,Missing TTL"); + } + + @Test + @DisplayName("test validating publish validator with wrong messagetype") + public void test_validating_publish_validator_with_wrong_messagetype() { + final UAttributes attributes = new UAttributesBuilder() + .withType(UMessageType.REQUEST) + .build(); + + final UAttributesValidator validator = UAttributesValidator.Validators.PUBLISH.validator(); + assert(validator instanceof UAttributesValidator); + final UStatus status = validator.validate(attributes); + assertTrue(status.isFailed()); + assertEquals(status.getCode(), Code.INVALID_ARGUMENT.value()); + assertEquals(status.msg(), "Wrong Attribute Type,Invalid UUID,Invalid Priority"); + } + + @Test + @DisplayName("test validating response validator with wrong messagetype") + public void test_validating_response_validator_with_wrong_messagetype() { + final UAttributes attributes = new UAttributesBuilder() + .withType(UMessageType.PUBLISH) + .build(); + + final UAttributesValidator validator = UAttributesValidator.Validators.RESPONSE.validator(); + assert(validator instanceof UAttributesValidator); + final UStatus status = validator.validate(attributes); + assertTrue(status.isFailed()); + assertEquals(status.getCode(), Code.INVALID_ARGUMENT.value()); + assertEquals(status.msg(), "Invalid Type,Invalid UUID,Missing Sink,Invalid Priority,Missing correlationId"); + } + }