From c1fb3d13fffe78a5eb17f0dc1bf313d53e7b89d3 Mon Sep 17 00:00:00 2001 From: Keaton Sentak Date: Tue, 2 Jul 2024 21:15:03 -0400 Subject: [PATCH] feat: Add discovery cpp testing files --- src/sdks/discovery/package.json | 3 + .../src/cpp/sdk/cpptest/CMakeLists.txt | 75 +++++++++++++ .../src/cpp/sdk/cpptest/DiscoverySDKTest.cpp | 102 ++++++++++++++++++ .../src/cpp/sdk/cpptest/DiscoverySDKTest.h | 41 +++++++ .../discovery/src/cpp/sdk/cpptest/Main.cpp | 89 +++++++++++++++ .../discovery/src/cpp/sdk/cpptest/build.sh | 41 +++++++ 6 files changed, 351 insertions(+) create mode 100644 src/sdks/discovery/src/cpp/sdk/cpptest/CMakeLists.txt create mode 100644 src/sdks/discovery/src/cpp/sdk/cpptest/DiscoverySDKTest.cpp create mode 100644 src/sdks/discovery/src/cpp/sdk/cpptest/DiscoverySDKTest.h create mode 100644 src/sdks/discovery/src/cpp/sdk/cpptest/Main.cpp create mode 100644 src/sdks/discovery/src/cpp/sdk/cpptest/build.sh diff --git a/src/sdks/discovery/package.json b/src/sdks/discovery/package.json index 0e4ec34e9..f2384861c 100644 --- a/src/sdks/discovery/package.json +++ b/src/sdks/discovery/package.json @@ -12,6 +12,9 @@ "validate": "npx firebolt-openrpc validate --input ./dist/firebolt-discovery-open-rpc.json", "sdk": "npx firebolt-openrpc sdk --input ./dist/firebolt-discovery-open-rpc.json --template ./src/js --output ./build/javascript/src", "native": "npx firebolt-openrpc sdk --input ./dist/firebolt-discovery-open-rpc.json --template ./src/js --output ./build/c/src --language ../../../node_modules/@firebolt-js/openrpc/languages/c", + "cpp": "npm run cpp:compile && npm run cpp:install", + "cpp:compile": "npx firebolt-openrpc sdk --input ./dist/firebolt-discovery-open-rpc.json --template ./src/cpp --output ./build/cpp/src --static-module Platform --language ../../../node_modules/@firebolt-js/openrpc/languages/cpp", + "cpp:install": "./build/cpp/src/scripts/install.sh -i ./build/cpp/src -s ./build/cpp/src/ -m discovery", "compile": "cd ../../.. && npm run compile", "slice": "npx firebolt-openrpc slice -i ../../../dist/firebolt-open-rpc.json --sdk ./sdk.config.json -o ./dist/firebolt-discovery-open-rpc.json", "docs": "npx firebolt-openrpc docs --input ./dist/firebolt-discovery-open-rpc.json --output build/docs/markdown --as-path", diff --git a/src/sdks/discovery/src/cpp/sdk/cpptest/CMakeLists.txt b/src/sdks/discovery/src/cpp/sdk/cpptest/CMakeLists.txt new file mode 100644 index 000000000..e2fc88793 --- /dev/null +++ b/src/sdks/discovery/src/cpp/sdk/cpptest/CMakeLists.txt @@ -0,0 +1,75 @@ +# Copyright 2023 Comcast Cable Communications Management, LLC +# +# 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-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.3) + +project(FireboltDiscoverySDKTests) + +if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) + set(CMAKE_INSTALL_PREFIX "${SYSROOT_PATH}/usr" CACHE INTERNAL "" FORCE) + set(CMAKE_PREFIX_PATH ${SYSROOT_PATH}/usr/lib/cmake CACHE INTERNAL "" FORCE) +endif() + +list(APPEND CMAKE_MODULE_PATH + "${SYSROOT_PATH}/usr/lib/cmake" + "${SYSROOT_PATH}/tools/cmake") +message("FIREBOLT_PATH inside cmake " ${FIREBOLT_PATH}) +if (FIREBOLT_PATH) + set(CMAKE_FIREBOLT_PATH + "${FIREBOLT_PATH}/usr/lib/cmake/Firebolt" + "${FIREBOLT_PATH}/usr/lib/cmake/FireboltSDK") + list(APPEND CMAKE_PREFIX_PATH ${CMAKE_FIREBOLT_PATH}) + list(APPEND CMAKE_MODULE_PATH ${CMAKE_FIREBOLT_PATH}) +else () + set(FIREBOLT_PATH "${SYSROOT_PATH}" CACHE INTERNAL "" FORCE) +endif () + +find_package(WPEFramework CONFIG REQUIRED) +find_package(${NAMESPACE}Core CONFIG REQUIRED) +find_package(Firebolt CONFIG REQUIRED) +find_package(${FIREBOLT_NAMESPACE}SDK CONFIG REQUIRED) + +set(TESTAPP TestFireboltDiscovery) + +message("Setup ${TESTAPP}") + +add_executable(${TESTAPP} DiscoverySDKTest.cpp Main.cpp) + +target_link_libraries(${TESTAPP} + PRIVATE + ${NAMESPACE}Core::${NAMESPACE}Core + ${FIREBOLT_NAMESPACE}SDK::${FIREBOLT_NAMESPACE}SDK +) + +target_include_directories(${TESTAPP} + PRIVATE + $ + $ + $ +) + +set_target_properties(${TESTAPP} PROPERTIES + CXX_STANDARD 17 + CXX_STANDARD_REQUIRED YES +) + +add_custom_command( + TARGET ${TESTAPP} + POST_BUILD + COMMENT "=================== Installing TestApp ======================" + COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/${FIREBOLT_NAMESPACE}/usr/bin + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/${TESTAPP} ${CMAKE_BINARY_DIR}/${FIREBOLT_NAMESPACE}/usr/bin +) diff --git a/src/sdks/discovery/src/cpp/sdk/cpptest/DiscoverySDKTest.cpp b/src/sdks/discovery/src/cpp/sdk/cpptest/DiscoverySDKTest.cpp new file mode 100644 index 000000000..9a0dad7bf --- /dev/null +++ b/src/sdks/discovery/src/cpp/sdk/cpptest/DiscoverySDKTest.cpp @@ -0,0 +1,102 @@ +/* + * Copyright 2023 Comcast Cable Communications Management, LLC + * + * 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-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include "DiscoverySDKTest.h" + +using namespace std; +bool DiscoverySDKTest::_connected; + +void DiscoverySDKTest::ConnectionChanged(const bool connected, const Firebolt::Error error) +{ + cout << "Change in connection: connected: " << connected << " error: " << static_cast(error) << endl; + _connected = connected; +} + +void DiscoverySDKTest::CreateFireboltInstance(const std::string& url) +{ + const std::string config = "{\ + \"waitTime\": 100000,\ + \"logLevel\": \"Info\",\ + \"workerPool\":{\ + \"queueSize\": 8,\ + \"threadCount\": 3\ + },\ + \"wsUrl\": " + url + "}"; + + _connected = false; + Firebolt::IFireboltAccessor::Instance().Initialize(config); + Firebolt::IFireboltAccessor::Instance().Connect(ConnectionChanged); +} + +void DiscoverySDKTest::DestroyFireboltInstance() +{ + Firebolt::IFireboltAccessor::Instance().Disconnect(); + Firebolt::IFireboltAccessor::Instance().Deinitialize(); + Firebolt::IFireboltAccessor::Instance().Dispose(); +} + +bool DiscoverySDKTest::WaitOnConnectionReady() +{ + uint32_t waiting = 10000; + static constexpr uint32_t SLEEPSLOT_TIME = 100; + + // Right, a wait till connection is closed is requested.. + while ((waiting > 0) && (_connected == false)) { + + uint32_t sleepSlot = (waiting > SLEEPSLOT_TIME ? SLEEPSLOT_TIME : waiting); + // Right, lets sleep in slices of 100 ms + usleep(sleepSlot); + waiting -= sleepSlot; + } + return _connected; +} + +template +using EnumMap = std::unordered_map; +template +inline const string& ConvertFromEnum(EnumMap enumMap, T type) +{ + return enumMap[type]; +} +template +inline const T ConvertToEnum(EnumMap enumMap, const string& str) +{ + T value; + for (auto element: enumMap) { + if (element.second == str) { + value = element.first; + break; + } + } + return value; +} + +void DiscoverySDKTest::SampleTest() +{ + Firebolt::Error error = Firebolt::Error::None; + + if (error === Firebolt::Error::None) { + cout << "Sample Test Passed!" << endl; + } else { + std::string errorMessage = "Error: " + std::to_string(static_cast(error)); + throw std::runtime_error("SampleTest failed. " + errorMessage); + } +} diff --git a/src/sdks/discovery/src/cpp/sdk/cpptest/DiscoverySDKTest.h b/src/sdks/discovery/src/cpp/sdk/cpptest/DiscoverySDKTest.h new file mode 100644 index 000000000..2fcd2bcc0 --- /dev/null +++ b/src/sdks/discovery/src/cpp/sdk/cpptest/DiscoverySDKTest.h @@ -0,0 +1,41 @@ +/* + * Copyright 2023 Comcast Cable Communications Management, LLC + * + * 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-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include "firebolt.h" + +class DiscoverySDKTest { + +public: + DiscoverySDKTest() = default; + virtual ~DiscoverySDKTest() = default; + + static void CreateFireboltInstance(const std::string& url); + static void DestroyFireboltInstance(); + static void TestDiscoveryStaticSDK(); + + static void SampleTest(); + + static bool WaitOnConnectionReady(); + +private: + static void ConnectionChanged(const bool, const Firebolt::Error); + static bool _connected; +}; \ No newline at end of file diff --git a/src/sdks/discovery/src/cpp/sdk/cpptest/Main.cpp b/src/sdks/discovery/src/cpp/sdk/cpptest/Main.cpp new file mode 100644 index 000000000..8e56bae39 --- /dev/null +++ b/src/sdks/discovery/src/cpp/sdk/cpptest/Main.cpp @@ -0,0 +1,89 @@ +/* + * Copyright 2023 Comcast Cable Communications Management, LLC + * + * 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-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include "DiscoverySDKTest.h" + +using namespace std; + +const char* options = ":hu:"; + +void RunAllTests() { + bool allTestsPassed = true; + vector errorMessages; + + auto runTest = [&allTestsPassed, &errorMessages](auto testFunction, const string& testName) { + try { + testFunction(); + } catch (const exception& e) { + errorMessages.push_back("Test " + testName + " failed: " + e.what()); + allTestsPassed = false; + } + }; + + // Ensure the connection is ready before running tests + if (DiscoverySDKTest::WaitOnConnectionReady()) { + // Add tests here + + // runTest(DiscoverySDKTest::TestName, "TestName"); + + if (allTestsPassed) { + cout << "============================" << endl; + cout << "ALL DISCOVERY SDK TESTS SUCCEEDED!" << endl; + cout << "============================" << endl; + } else { + cout << "============================" << endl; + cout << "SOME TESTS FAILED:" << endl; + for (const auto& errorMessage : errorMessages) { + cout << errorMessage << endl; + } + cout << "============================" << endl; + exit(1); + } + } else { + cout << "Discovery Test not able to connect with server..." << endl; + exit(1); + } +} + +int main(int argc, char* argv[]) { + int c; + string url = "ws://127.0.0.1:9998"; + while ((c = getopt(argc, argv, options)) != -1) { + switch (c) { + case 'u': + url = optarg; + break; + case 'h': + printf("./TestFireboltDiscovery -u ws://ip:port\n"); + exit(1); + } + } + + printf("Firebolt Discovery SDK Test\n"); + + DiscoverySDKTest::CreateFireboltInstance(url); + RunAllTests(); + DiscoverySDKTest::DestroyFireboltInstance(); + + return 0; +} \ No newline at end of file diff --git a/src/sdks/discovery/src/cpp/sdk/cpptest/build.sh b/src/sdks/discovery/src/cpp/sdk/cpptest/build.sh new file mode 100644 index 000000000..b404a78f3 --- /dev/null +++ b/src/sdks/discovery/src/cpp/sdk/cpptest/build.sh @@ -0,0 +1,41 @@ +#!/bin/bash +usage() +{ + echo "options:" + echo " -t test path" + echo " -s sysroot path" + echo " -f firebolt path" + echo " -c clear build" + echo " -h : help" + echo + echo "usage: " + echo " ./build.sh -t testpath -c -f fireboltpath -s sysrootpath" +} + +TestPath="." +FireboltPath=${FIREBOLT_PATH} +SysrootPath=${SYSROOT_PATH} +ClearBuild="N" +while getopts t:s:f:ch flag +do + case "${flag}" in + t) TestPath="${OPTARG}";; + s) SysrootPath="${OPTARG}";; + f) FireboltPath="${OPTARG}";; + c) ClearBuild="Y";; + h) usage && exit 1;; + esac +done + +if [ "${ClearBuild}" == "Y" ]; +then + rm -rf ${TestPath}/build +fi + +echo "TestPath" +echo "${TestPath}" +echo "FireboltPath" +echo ${FireboltPath} +cmake -B${TestPath}/build -S${TestPath} -DSYSROOT_PATH=${SysrootPath} -DFIREBOLT_PATH=${FireboltPath} +# -DPOLYMORPHICS_METHODS=ON // Enable this to test +cmake --build ${TestPath}/build