Skip to content

Commit

Permalink
Workflow test
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewd0123 committed Apr 23, 2024
1 parent b9e7f25 commit f1b046f
Show file tree
Hide file tree
Showing 7 changed files with 348 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tck-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: TCK Tests

on:
push:
branches: [ "main" ]
branches: [ "main", "uattributes_val_temp" ]
pull_request:
branches: [ "main" ]
branches: [ "main", "uattributes_val_temp" ]

permissions:
contents: read
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ public class Constant {
public static final String VALIDATE_UUID = "uuid_validate";
public static final String SERIALIZE_UUID = "uuid_serialize";
public static final String DESERIALIZE_UUID = "uuid_deserialize";
public static final String VALIDATE_UATTRIBUTES = "uattributes_validate";

}
152 changes: 152 additions & 0 deletions test_agent/java/src/main/java/org/eclipse/uprotocol/TestAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.protobuf.StringValue;
import org.eclipse.uprotocol.transport.UListener;
import org.eclipse.uprotocol.transport.builder.UAttributesBuilder;
import org.eclipse.uprotocol.transport.validate.UAttributesValidator;
import org.eclipse.uprotocol.uri.serializer.LongUriSerializer;
import org.eclipse.uprotocol.uri.validator.UriValidator;
import org.eclipse.uprotocol.validation.ValidationResult;
Expand Down Expand Up @@ -70,6 +71,7 @@ public class TestAgent {
actionHandlers.put(Constant.DESERIALIZE_URI, TestAgent::handleLongDeserializeUriCommand);
actionHandlers.put(Constant.VALIDATE_URI, TestAgent::handleValidateUriCommand);
actionHandlers.put(Constant.VALIDATE_UUID, TestAgent::handleValidateUuidCommand);
actionHandlers.put(Constant.VALIDATE_UATTRIBUTES, TestAgent::handleUAttributesValidateCommand);
actionHandlers.put(Constant.SERIALIZE_UUID, TestAgent::handleLongSerializeUuidCommand);
actionHandlers.put(Constant.DESERIALIZE_UUID, TestAgent::handleLongDeserializeUuidCommand);

Expand Down Expand Up @@ -293,6 +295,156 @@ public static Object handleValidateUuidCommand(Map<String, Object> jsonData) {
return null;
}

public static Object handleUAttributesValidateCommand(Map<String, Object> jsonData) {
Map<String, Object> data = (Map<String, Object>) jsonData.get("data");
String valMethod = (String) data.getOrDefault("validation_method", "default");
String valType = (String) data.getOrDefault("validation_type", "default");

UAttributes attributes = null;
if (data.get("attributes") != null) {
attributes = (UAttributes) ProtoConverter.dictToProto((Map<String, Object>)data.get("attributes"), UAttributes.newBuilder());
if ("default".equals(attributes.getSink().getAuthority().getName())) {
attributes = attributes.toBuilder().setSink(UUri.getDefaultInstance()).build();
}
} else {
attributes = UAttributes.newBuilder().build();
}

if ("uprotocol".equals(data.get("id"))) {
attributes = attributes.toBuilder().setId(UuidFactory.Factories.UPROTOCOL.factory().create()).build();
} else if ("uuid".equals(data.get("id"))) {
attributes = attributes.toBuilder().setId(UuidFactory.Factories.UUIDV6.factory().create()).build();
}

if ("uprotocol".equals(data.get("reqid"))) {
attributes = attributes.toBuilder().setReqid(UuidFactory.Factories.UPROTOCOL.factory().create()).build();
} else if ("uuid".equals(data.get("reqid"))) {
attributes = attributes.toBuilder().setReqid(UuidFactory.Factories.UUIDV6.factory().create()).build();
}

String str_result = null;
Boolean bool_result = null;
ValidationResult val_result = null;

UAttributesValidator pub_val = UAttributesValidator.Validators.PUBLISH.validator();
UAttributesValidator req_val = UAttributesValidator.Validators.REQUEST.validator();
UAttributesValidator res_val = UAttributesValidator.Validators.RESPONSE.validator();
UAttributesValidator not_val = UAttributesValidator.Validators.NOTIFICATION.validator();

if (attributes != null && attributes.getTtl() == 1) {
try {
Thread.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

if (valType.equals("get_validator")){
str_result = UAttributesValidator.getValidator(attributes).toString();
}

switch (valMethod) {
case "publish_validator":
switch (valType) {
case "is_expired":
bool_result = pub_val.isExpired(attributes);
break;
case "validate_ttl":
val_result = pub_val.validateTtl(attributes);
break;
case "validate_sink":
val_result = pub_val.validateSink(attributes);
break;
case "validate_req_id":
val_result = pub_val.validateReqId(attributes);
break;
case "validate_permission_level":
val_result = pub_val.validatePermissionLevel(attributes);
break;
default:
val_result = pub_val.validate(attributes);
}
break;
case "request_validator":
switch (valType) {
case "is_expired":
bool_result = req_val.isExpired(attributes);
break;
case "validate_ttl":
val_result = req_val.validateTtl(attributes);
break;
case "validate_sink":
val_result = req_val.validateSink(attributes);
break;
case "validate_req_id":
val_result = req_val.validateReqId(attributes);
break;
default:
val_result = req_val.validate(attributes);
}
break;
case "response_validator":
switch (valType) {
case "is_expired":
bool_result = res_val.isExpired(attributes);
break;
case "validate_ttl":
val_result = res_val.validateTtl(attributes);
break;
case "validate_sink":
val_result = res_val.validateSink(attributes);
break;
case "validate_req_id":
val_result = res_val.validateReqId(attributes);
break;
default:
val_result = res_val.validate(attributes);
}
break;
case "notification_validator":
switch (valType) {
case "is_expired":
bool_result = not_val.isExpired(attributes);
break;
case "validate_ttl":
val_result = not_val.validateTtl(attributes);
break;
case "validate_sink":
val_result = not_val.validateSink(attributes);
break;
case "validate_req_id":
val_result = not_val.validateReqId(attributes);
break;
case "validate_type":
val_result = not_val.validateType(attributes);
break;
default:
val_result = not_val.validate(attributes);
}
break;
default:
break;
}

String result = "";
String message = "";

if (bool_result != null){
result = bool_result ? "True" : "False";
message = "";
} else if (val_result != null) {
result = val_result.isSuccess() ? "True" : "False";
message = val_result.getMessage();
} else if (str_result != null) {
result = "";
message = str_result;
}

String testID = (String) jsonData.get("test_id");
sendToTestManager(Map.of("result", result, "message", message), Constant.VALIDATE_UATTRIBUTES, testID);
return null;
}

private static Object handleLongSerializeUuidCommand(Map<String, Object> jsonData) {
Map<String, Object> data = (Map<String, Object>) jsonData.get("data");
UUID uuid = (UUID) ProtoConverter.dictToProto(data, UUID.newBuilder());
Expand Down
1 change: 1 addition & 0 deletions test_agent/python/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@
DESERIALIZE_URI = "uri_deserialize"
VALIDATE_URI = "uri_validate"
VALIDATE_UUID = "uuid_validate"
VALIDATE_UATTRIBUTES = "uattributes_validate"
SERIALIZE_UUID = "uuid_serialize"
DESERIALIZE_UUID = "uuid_deserialize"
102 changes: 99 additions & 3 deletions test_agent/python/testagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from threading import Thread
from typing import Any, Dict, List, Union
from datetime import datetime, timezone
import time

from google.protobuf import any_pb2
from google.protobuf.message import Message
Expand All @@ -40,21 +41,29 @@
UPriority,
UMessageType,
CallOptions,
UAttributes,
)
from uprotocol.proto.umessage_pb2 import UMessage
from uprotocol.proto.upayload_pb2 import UPayload, UPayloadFormat
from uprotocol.proto.ustatus_pb2 import UStatus
from uprotocol.proto.uri_pb2 import UUri
from uprotocol.proto.uuid_pb2 import UUID
from uprotocol.transport.validate import uattributesvalidator
from uprotocol.transport.validate.uattributesvalidator import (
UAttributesValidator,
)
from uprotocol.transport.builder.uattributesbuilder import UAttributesBuilder
from uprotocol.transport.ulistener import UListener
from uprotocol.uri.serializer.longuriserializer import LongUriSerializer
from uprotocol.uuid.serializer.longuuidserializer import LongUuidSerializer
from uprotocol.uuid.factory.uuidfactory import Factories
from uprotocol.uri.validator.urivalidator import UriValidator
from uprotocol.uuid.validate.uuidvalidator import UuidValidator, Validators
from uprotocol.uuid.validate import uuidvalidator
from uprotocol.uuid.validate.uuidvalidator import UuidValidator
from uprotocol.uuid.factory.uuidutils import UUIDUtils
from uprotocol.proto.ustatus_pb2 import UCode
from uprotocol.validation.validationresult import ValidationResult
from uprotocol.uri.validator.urivalidator import UriValidator

import constants as CONSTANTS

Expand Down Expand Up @@ -248,6 +257,90 @@ def handle_long_serialize_uuid(json_msg: Dict[str, Any]):
)


def handle_uattributes_validate_command(json_msg: Dict[str, Any]):
data = json_msg["data"]
val_method = data.get("validation_method")
val_type = data.get("validation_type")
if data.get("attributes"):
attributes = dict_to_proto(data["attributes"], UAttributes())
if attributes.sink.authority.name == "default":
attributes.sink.CopyFrom(UUri())
else:
attributes = UAttributes()

if data.get("id") == "uprotocol":
attributes.id.CopyFrom(Factories.UPROTOCOL.create())
elif data.get("id") == "uuid":
attributes.id.CopyFrom(Factories.UUIDV6.create())

if data.get("reqid") == "uprotocol":
attributes.reqid.CopyFrom(Factories.UPROTOCOL.create())
elif data.get("reqid") == "uuid":
attributes.reqid.CopyFrom(Factories.UUIDV6.create())

validator_type = {
"get_validator": UAttributesValidator.get_validator,
}.get(val_type)

pub_val = uattributesvalidator.Validators.PUBLISH.validator()
req_val = uattributesvalidator.Validators.REQUEST.validator()
res_val = uattributesvalidator.Validators.RESPONSE.validator()
not_val = uattributesvalidator.Validators.NOTIFICATION.validator()

validator_method = {
"publish_validator": {
"is_expired": pub_val.is_expired,
"validate_ttl": pub_val.validate_ttl,
"validate_sink": pub_val.validate_sink,
"validate_req_id": pub_val.validate_req_id,
"validate_permission_level": pub_val.validate_permission_level
}.get(val_type, pub_val.validate),
"request_validator": {
"is_expired": req_val.is_expired,
"validate_ttl": req_val.validate_ttl,
"validate_sink": req_val.validate_sink,
"validate_req_id": req_val.validate_req_id,
}.get(val_type, req_val.validate),
"response_validator": {
"is_expired": res_val.is_expired,
"validate_ttl": res_val.validate_ttl,
"validate_sink": res_val.validate_sink,
"validate_req_id": res_val.validate_req_id,
}.get(val_type, res_val.validate),
"notification_validator": {
"is_expired": not_val.is_expired,
"validate_ttl": not_val.validate_ttl,
"validate_sink": not_val.validate_sink,
"validate_req_id": not_val.validate_req_id,
"validate_type": not_val.validate_type
}.get(val_type, not_val.validate),
}.get(val_method)

if attributes.ttl == 1:
time.sleep(0.8)

if val_type == "get_validator":
status = validator_type(attributes)
if validator_method is not None:
status = validator_method(attributes)

if isinstance(status, ValidationResult):
result = str(status.is_success())
message = status.get_message()
elif isinstance(status, bool):
result = str(status)
message = ""
else:
result = ""
message = str(status)

send_to_test_manager(
{"result": result, "message": message},
CONSTANTS.VALIDATE_UATTRIBUTES,
received_test_id=json_msg["test_id"],
)


def handle_uri_validate_command(json_msg):
val_type = json_msg["data"]["type"]
uri = LongUriSerializer().deserialize(json_msg["data"].get("uri"))
Expand Down Expand Up @@ -297,8 +390,10 @@ def handle_uuid_validate_command(json_msg):

status = {
"get_validator": UuidValidator.get_validator(uuid).validate(uuid),
"uprotocol": Validators.UPROTOCOL.validator().validate(uuid),
"uuidv6": Validators.UUIDV6.validator().validate(uuid),
"uprotocol": uuidvalidator.Validators.UPROTOCOL.validator().validate(
uuid
),
"uuidv6": uuidvalidator.Validators.UUIDV6.validator().validate(uuid),
"get_validator_is_uuidv6": UUIDUtils.is_uuidv6(uuid),
}.get(validator_type)

Expand Down Expand Up @@ -327,6 +422,7 @@ def handle_uuid_validate_command(json_msg):
CONSTANTS.DESERIALIZE_UUID: handle_long_deserialize_uuid,
CONSTANTS.VALIDATE_URI: handle_uri_validate_command,
CONSTANTS.VALIDATE_UUID: handle_uuid_validate_command,
CONSTANTS.VALIDATE_UATTRIBUTES: handle_uattributes_validate_command,
}


Expand Down
2 changes: 2 additions & 0 deletions test_manager/features/steps/tck_step_implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def serialized_uuid_received(context, expected_uuid: str):

@then('receives validation result as "{expected_result}"')
def receive_validation_result(context, expected_result):
if expected_result == "none":
return
try:
expected_result = expected_result.strip()
actual_val_res = context.response_data["result"]
Expand Down
Loading

0 comments on commit f1b046f

Please sign in to comment.