Skip to content

Commit

Permalink
Update UriValidator to 1.5.8
Browse files Browse the repository at this point in the history
* Align UriValidator test with v1.5.8
* Update Python and Java TAs to work with UriValidator
* Create skeleton for 1.5.8 version of SocketUTransports in Python and Java (still requires work after this PR)
* Update Test Manager port (was causing issues locally, changing it fixed the issue)
* Remove some deprecated code from Python and Java Test Agents
  • Loading branch information
matthewd0123 committed Jul 8, 2024
1 parent 284e214 commit 9fb3354
Show file tree
Hide file tree
Showing 18 changed files with 481 additions and 702 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/tck-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ jobs:
with:
command: build
args: --manifest-path test_agent/rust/Cargo.toml
- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '11'
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Install dependencies
run: |
cd scripts
python build_up_java_latest.py
- name: Build up_client_socket_java with Maven
working-directory: up_client_socket/java
run: |
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ MANIFEST
**/jar_files/**
*.jar
**/.DS_Store
**/target
**/target
**/summary.json
**/up-java/
**/up-python/
2 changes: 1 addition & 1 deletion screenshots/RegisterListenerv2.drawio.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion screenshots/TestManagerTestAgentv2.drawio.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions scripts/build_up_java_latest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
SPDX-FileCopyrightText: Copyright (c) 2024 Contributors to the Eclipse Foundation
See the NOTICE file(s) distributed with this work for additional
information regarding copyright ownership.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-FileType: SOURCE
SPDX-License-Identifier: Apache-2.0
"""

import os
import subprocess
import sys


def run_command(command):
"""Run a shell command and handle the output."""
try:
result = subprocess.run(
command,
check=True,
shell=True,
text=True, # , stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
print(result.stdout)
except subprocess.CalledProcessError as e:
print("An error occurred while executing:", command)
print(e.stderr)
sys.exit(1)


def main():
# Define the repository and desired branch or commit
repo_url = "https://github.com/eclipse-uprotocol/up-java.git"

# Directory to be removed and cloned
repo_dir = "up-java"

# Clone the repository
run_command(f"git clone {repo_url}")

# Change directory into the repository
os.chdir(repo_dir)

run_command("mvn clean install")


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion test_agent/cpp/include/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Constants {
constexpr static const char* TEST_MANAGER_IP = "127.0.0.5";
constexpr static const int TEST_MANAGER_PORT = 12345;
constexpr static const int TEST_MANAGER_PORT = 33333;
constexpr static const int BYTES_MSG_LENGTH = 32767;
constexpr static const char* SEND_COMMAND = "send";
constexpr static const char* REGISTER_LISTENER_COMMAND = "registerlistener";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public class Constant {
public static final String TEST_MANAGER_IP = "127.0.0.5";
public static final int TEST_MANAGER_PORT = 12345;
public static final int TEST_MANAGER_PORT = 33333;
public static final int BYTES_MSG_LENGTH = 32767;

}
116 changes: 43 additions & 73 deletions test_agent/java/src/main/java/org/eclipse/uprotocol/TestAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@

import com.google.gson.Gson;
import com.google.protobuf.Any;
import com.google.protobuf.ByteString;
import com.google.protobuf.Message;
import com.google.protobuf.StringValue;
import org.eclipse.uprotocol.communication.CallOptions;
import org.eclipse.uprotocol.communication.UPayload;
import org.eclipse.uprotocol.Constants.ActionCommands;
import org.eclipse.uprotocol.Constants.Constant;
import org.eclipse.uprotocol.transport.UListener;
import org.eclipse.uprotocol.transport.builder.UAttributesBuilder;
import org.eclipse.uprotocol.transport.builder.UMessageBuilder;
import org.eclipse.uprotocol.transport.validate.UAttributesValidator;
import org.eclipse.uprotocol.uri.serializer.LongUriSerializer;
import org.eclipse.uprotocol.uri.serializer.UriSerializer;
import org.eclipse.uprotocol.uri.validator.UriValidator;
import org.eclipse.uprotocol.validation.ValidationResult;
import org.eclipse.uprotocol.uuid.serializer.LongUuidSerializer;
import org.eclipse.uprotocol.uri.serializer.MicroUriSerializer;
import org.eclipse.uprotocol.uuid.serializer.UuidSerializer;
import org.eclipse.uprotocol.uuid.factory.UuidFactory;
import org.eclipse.uprotocol.uuid.factory.UuidUtils;
import org.eclipse.uprotocol.uuid.validate.UuidValidator;
Expand Down Expand Up @@ -75,8 +77,6 @@ public class TestAgent {
actionHandlers.put(ActionCommands.SERIALIZE_UUID, TestAgent::handleLongSerializeUuidCommand);
actionHandlers.put(ActionCommands.DESERIALIZE_UUID, TestAgent::handleLongDeserializeUuidCommand);
actionHandlers.put(ActionCommands.VALIDATE_UATTRIBUTES, TestAgent::handleUAttributesValidateCommand);
actionHandlers.put(ActionCommands.MICRO_SERIALIZE_URI, TestAgent::handleMicroSerializeUuriCommand);
actionHandlers.put(ActionCommands.MICRO_DESERIALIZE_URI, TestAgent::handleMicroDeserializeUuriCommand);
}

static {
Expand Down Expand Up @@ -139,7 +139,7 @@ private static void writeDataToTMSocket(JSONObject responseDict, String action)
}
}

private static UStatus handleSendCommand(Map<String, Object> jsonData) {
private static CompletionStage<UStatus> handleSendCommand(Map<String, Object> jsonData) {
UMessage uMessage = (UMessage) ProtoConverter.dictToProto((Map<String, Object>) jsonData.get("data"),
UMessage.newBuilder());
UAttributes uAttributesWithId = uMessage.getAttributes().toBuilder()
Expand All @@ -148,12 +148,12 @@ private static UStatus handleSendCommand(Map<String, Object> jsonData) {
return transport.send(uMessageWithId);
}

private static UStatus handleRegisterListenerCommand(Map<String, Object> jsonData) {
private static CompletionStage<UStatus> handleRegisterListenerCommand(Map<String, Object> jsonData) {
UUri uri = (UUri) ProtoConverter.dictToProto((Map<String, Object>) jsonData.get("data"), UUri.newBuilder());
return transport.registerListener(uri, listener);
}

private static UStatus handleUnregisterListenerCommand(Map<String, Object> jsonData) {
private static CompletionStage<UStatus> handleUnregisterListenerCommand(Map<String, Object> jsonData) {
UUri uri = (UUri) ProtoConverter.dictToProto((Map<String, Object>) jsonData.get("data"), UUri.newBuilder());
return transport.unregisterListener(uri, listener);
}
Expand All @@ -162,10 +162,11 @@ private static Object handleInvokeMethodCommand(Map<String, Object> jsonData) {
Map<String, Object> data = (Map<String, Object>) jsonData.get("data");
// Convert data and payload to protocol buffers
UUri uri = (UUri) ProtoConverter.dictToProto(data, UUri.newBuilder());
UPayload payload = (UPayload) ProtoConverter.dictToProto((Map<String, Object>) data.get("payload"),
UPayload.newBuilder());
CompletionStage<UMessage> responseFuture = transport.invokeMethod(uri, payload,
CallOptions.newBuilder().setTtl(10000).build());
ByteString payloadBytes = (ByteString) data.get("data");
UPayloadFormat format = (UPayloadFormat) data.get("format");
UPayload payload = new UPayload(payloadBytes, format);
CompletionStage<UPayload> responseFuture = transport.invokeMethod(uri, payload,
CallOptions.DEFAULT);
responseFuture.whenComplete((responseMessage, exception) -> {
sendToTestManager(responseMessage, ActionCommands.INVOKE_METHOD_COMMAND, (String) jsonData.get("test_id"));
});
Expand All @@ -175,14 +176,14 @@ private static Object handleInvokeMethodCommand(Map<String, Object> jsonData) {
private static Object handleLongSerializeUriCommand(Map<String, Object> jsonData) {
Map<String, Object> data = (Map<String, Object>) jsonData.get("data");
UUri uri = (UUri) ProtoConverter.dictToProto(data, UUri.newBuilder());
String serializedUuri = LongUriSerializer.instance().serialize(uri);
String serializedUuri = UriSerializer.serialize(uri);
String testID = (String) jsonData.get("test_id");
sendToTestManager(serializedUuri, ActionCommands.SERIALIZE_URI, testID);
return null;
}

private static Object handleLongDeserializeUriCommand(Map<String, Object> jsonData) {
UUri uri = LongUriSerializer.instance().deserialize(jsonData.get("data").toString());
UUri uri = UriSerializer.deserialize(jsonData.get("data").toString());
String testID = (String) jsonData.get("test_id");
sendToTestManager(uri, ActionCommands.DESERIALIZE_URI, testID);
return null;
Expand All @@ -195,42 +196,40 @@ private static Object handleValidateUriCommand(Map<String, Object> jsonData) {

UUri uri = (UUri) ProtoConverter.dictToProto(uriValue, UUri.newBuilder());

Function<UUri, ValidationResult> validatorFunc = null;
Function<UUri, Boolean> validatorFunc = null;
Function<UUri, Boolean> validatorFuncBool = null;

switch (valType) {
case "uri":
validatorFunc = UriValidator::validate;
break;
case "rpc_response":
validatorFunc = UriValidator::validateRpcResponse;
case "is_empty":
validatorFunc = UriValidator::isEmpty;
break;
case "rpc_method":
validatorFunc = UriValidator::validateRpcMethod;
case "is_rpc_method":
validatorFunc = UriValidator::isRpcMethod;
break;
case "is_empty":
validatorFuncBool = UriValidator::isEmpty;
case "is_rpc_response":
validatorFunc = UriValidator::isRpcResponse;
break;
case "is_resolved":
validatorFuncBool = UriValidator::isResolved;
case "is_notification_destination":
validatorFunc = UriValidator::isRpcResponse;
break;
case "is_micro_form":
validatorFuncBool = UriValidator::isMicroForm;
case "is_default_resource_id":
validatorFunc = UriValidator::isDefaultResourceId;
break;
case "is_long_form":
validatorFuncBool = UriValidator::isLongForm;
case "is_topic":
validatorFunc = UriValidator::isTopic;
break;
}

String testID = (String) jsonData.get("test_id");
if (validatorFunc != null) {
ValidationResult status = validatorFunc.apply(uri);
String result = status.isSuccess() ? "True" : "False";
String message = status.getMessage();
sendToTestManager(Map.of("result", result, "message", message), ActionCommands.VALIDATE_URI, testID);
} else if (validatorFuncBool != null) {
Boolean status = validatorFuncBool.apply(uri);
String result = status ? "True" : "False";
Boolean status = validatorFunc.apply(uri);
String result = status.toString().equals("true") ? "True" : "False";
sendToTestManager(Map.of("result", result, "message", ""), ActionCommands.VALIDATE_URI, testID);
} else if (valType.equals("matches")) {
String uriToMatch = (String) data.get("uuri_2");
UUri convertedMatch = UriSerializer.deserialize(uriToMatch);
Boolean status = UriValidator.matches(uri, convertedMatch);
String result = status.toString().equals("true") ? "True" : "False";
sendToTestManager(Map.of("result", result, "message", ""), ActionCommands.VALIDATE_URI, testID);
}

Expand All @@ -246,7 +245,7 @@ public static Object handleUAttributesValidateCommand(Map<String, Object> jsonDa
if (data.get("attributes") != null) {
attributes = (UAttributes) ProtoConverter.dictToProto((Map<String, Object>) data.get("attributes"),
UAttributes.newBuilder());
if ("default".equals(attributes.getSink().getAuthority().getName())) {
if ("default".equals(attributes.getSink().getAuthorityName())) {
attributes = attributes.toBuilder().setSink(UUri.getDefaultInstance()).build();
}
} else {
Expand Down Expand Up @@ -458,58 +457,29 @@ public static Object handleValidateUuidCommand(Map<String, Object> jsonData) {
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());
String serializedUUid = LongUuidSerializer.instance().serialize(uuid);
String serializedUUid = UuidSerializer.serialize(uuid);
String testID = (String) jsonData.get("test_id");
sendToTestManager(serializedUUid, ActionCommands.SERIALIZE_UUID, testID);
return null;
}

private static Object handleLongDeserializeUuidCommand(Map<String, Object> jsonData) {
UUID uuid = LongUuidSerializer.instance().deserialize(jsonData.get("data").toString());
UUID uuid = UuidSerializer.deserialize(jsonData.get("data").toString());
String testID = (String) jsonData.get("test_id");
sendToTestManager(uuid, ActionCommands.DESERIALIZE_UUID, testID);
return null;
}

private static Object handleMicroSerializeUuriCommand(Map<String, Object> jsonData) {
Map<String, Object> data = (Map<String, Object>) jsonData.get("data");
UUri uri = (UUri) ProtoConverter.dictToProto(data, UUri.newBuilder());
byte[] serializedUuri = MicroUriSerializer.instance().serialize(uri);
String serializedUuriAsStr = "";
try {
serializedUuriAsStr = new String(serializedUuri, "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
String testID = (String) jsonData.get("test_id");
sendToTestManager(serializedUuriAsStr, ActionCommands.MICRO_SERIALIZE_URI, testID);
return null;
}

private static Object handleMicroDeserializeUuriCommand(Map<String, Object> jsonData) {
String microSerializedUuriAsStr = (String) jsonData.get("data");
byte[] microSerializedUuri = microSerializedUuriAsStr.getBytes(StandardCharsets.ISO_8859_1);
UUri uri = MicroUriSerializer.instance().deserialize(microSerializedUuri);

String testID = (String) jsonData.get("test_id");
sendToTestManager(uri, ActionCommands.MICRO_DESERIALIZE_URI, testID);
return null;
}

private static void handleOnReceive(UMessage uMessage) {
logger.info("Java on_receive called: " + uMessage);
if (uMessage.getAttributes().getType().equals(UMessageType.UMESSAGE_TYPE_REQUEST)) {
UAttributes reqAttributes = uMessage.getAttributes();
UAttributes uAttributes = UAttributesBuilder.response(reqAttributes.getSink(), reqAttributes.getSource(),
UPriority.UPRIORITY_CS4, reqAttributes.getId()).build();

StringValue stringValue = StringValue.newBuilder().setValue("SuccessRPCResponse").build();
Any anyObj = Any.pack(stringValue);
UPayload uPayload = new UPayload(anyObj.toByteString(), UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY);
UMessage resMsg = UMessageBuilder.response(reqAttributes).build(uPayload);

UPayload uPayload = UPayload.newBuilder().setValue(anyObj.toByteString())
.setFormat(UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY).build();

UMessage resMsg = UMessage.newBuilder().setAttributes(uAttributes).setPayload(uPayload).build();
transport.send(resMsg);
} else {
sendToTestManager(uMessage, ActionCommands.RESPONSE_ON_RECEIVE);
Expand Down
2 changes: 1 addition & 1 deletion test_agent/python/constants/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
#
# -------------------------------------------------------------------------

TEST_MANAGER_ADDR = ("127.0.0.5", 12345)
TEST_MANAGER_ADDR = ("127.0.0.5", 33333)
BYTES_MSG_LENGTH: int = 32767
Loading

0 comments on commit 9fb3354

Please sign in to comment.