Skip to content

Commit

Permalink
Added more Test coverage
Browse files Browse the repository at this point in the history
Also fixed some issues in UAttributes and the validator

#17
  • Loading branch information
czfdcn committed Aug 25, 2023
1 parent 4bf89a8 commit ed8e57e
Show file tree
Hide file tree
Showing 3 changed files with 339 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public Optional<USerializationHint> serializationHint() {
* @return Returns an Optional destination URI attribute.
*/
public Optional<UUri> sink() {
return sink == null || sink.isEmpty() ? Optional.empty() : Optional.of(sink);
return sink == null ? Optional.empty() : Optional.of(sink);
}

/**
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Integer> 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());
}
}

Expand All @@ -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<UUri> sink = attributes.sink();
if (sink.isPresent()) {
return UriValidator.validate(attributes.sink().get());
}
return UStatus.ok();
Expand All @@ -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<Integer> commStatus = attributes.commstatus();

if (commStatus.isPresent()) {
Optional<Code> code = Code.from(commStatus.get());
return code.isPresent() ? UStatus.ok() :
UStatus.failed("Invalid Communication Status Code", Code.INVALID_ARGUMENT.value());
}
return UStatus.ok();
}
Expand All @@ -128,8 +136,8 @@ public UStatus validateCommStatus(UAttributes attributes) {
public UStatus validatePermissionLevel(UAttributes attributes) {
final Optional<Integer> 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();
}
Expand All @@ -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<UUID> 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();
}
Expand All @@ -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());
}
}

Expand All @@ -194,7 +203,7 @@ public UStatus validateSink(UAttributes attributes) {
final Optional<UUri> 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());
}
Expand All @@ -208,7 +217,7 @@ public UStatus validateSink(UAttributes attributes) {
public UStatus validateTtl(UAttributes attributes) {
final Optional<Integer> 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();
}
Expand All @@ -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());
}

/**
Expand All @@ -236,7 +246,7 @@ public UStatus validateSink(UAttributes attributes) {
final Optional<UUri> 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());
}
Expand All @@ -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<UUID> 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());
}
}

Expand Down
Loading

0 comments on commit ed8e57e

Please sign in to comment.