From 928ebf2f16e384e5238fd22bba1fda03dcd9f157 Mon Sep 17 00:00:00 2001
From: Rainer Kartmann <rainer.kartmann@kit.edu>
Date: Thu, 9 Mar 2023 17:01:45 +0100
Subject: [PATCH] Add helper class for C++ test side

---
 source/RobotAPI/libraries/aron/CMakeLists.txt |   1 +
 .../aron/test/AronConversionTester.cpp        |  38 +++++++
 .../aron/test/AronConversionTester.h          | 107 ++++++++++++++++++
 .../libraries/aron/test/CMakeLists.txt        |  25 ++++
 4 files changed, 171 insertions(+)
 create mode 100644 source/RobotAPI/libraries/aron/test/AronConversionTester.cpp
 create mode 100644 source/RobotAPI/libraries/aron/test/AronConversionTester.h
 create mode 100644 source/RobotAPI/libraries/aron/test/CMakeLists.txt

diff --git a/source/RobotAPI/libraries/aron/CMakeLists.txt b/source/RobotAPI/libraries/aron/CMakeLists.txt
index ce4f7bc99..e1ccc8560 100644
--- a/source/RobotAPI/libraries/aron/CMakeLists.txt
+++ b/source/RobotAPI/libraries/aron/CMakeLists.txt
@@ -2,3 +2,4 @@ add_subdirectory(core)
 add_subdirectory(converter)
 add_subdirectory(codegeneration)
 add_subdirectory(common)
+add_subdirectory(test)
diff --git a/source/RobotAPI/libraries/aron/test/AronConversionTester.cpp b/source/RobotAPI/libraries/aron/test/AronConversionTester.cpp
new file mode 100644
index 000000000..aac543a04
--- /dev/null
+++ b/source/RobotAPI/libraries/aron/test/AronConversionTester.cpp
@@ -0,0 +1,38 @@
+/**
+ * This file is part of ArmarX.
+ *
+ * ArmarX is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * ArmarX is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @package    RobotAPI::ArmarXObjects::aron_cpp_to_python_conv_test
+ * @author     Rainer Kartmann ( rainer dot kartmann at kit dot edu )
+ * @date       2023
+ * @copyright  http://www.gnu.org/licenses/gpl-2.0.txt
+ *             GNU General Public License
+ */
+
+
+#include "AronConversionTester.h"
+
+#include <ArmarXCore/core/exceptions/local/ExpressionException.h>
+
+
+namespace armarx::aron::test
+{
+
+    AronConversionTester::AronConversionTester(dti::AronConversionTestInterfacePrx python) : interface(python)
+    {
+        ARMARX_CHECK(python);
+    }
+
+
+} // namespace armarx::aron::test
diff --git a/source/RobotAPI/libraries/aron/test/AronConversionTester.h b/source/RobotAPI/libraries/aron/test/AronConversionTester.h
new file mode 100644
index 000000000..fe4de8835
--- /dev/null
+++ b/source/RobotAPI/libraries/aron/test/AronConversionTester.h
@@ -0,0 +1,107 @@
+/**
+ * This file is part of ArmarX.
+ *
+ * ArmarX is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * ArmarX is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @package    RobotAPI::ArmarXObjects::aron_cpp_to_python_conv_test
+ * @author     Rainer Kartmann ( rainer dot kartmann at kit dot edu )
+ * @date       2023
+ * @copyright  http://www.gnu.org/licenses/gpl-2.0.txt
+ *             GNU General Public License
+ */
+
+
+#pragma once
+
+#include <ArmarXCore/core/exceptions/local/ExpressionException.h>
+#include <ArmarXCore/core/logging/Logging.h>
+
+#include <RobotAPI/interface/aron/test/AronConversionTestInterface.h>
+
+
+namespace armarx::aron::test
+{
+
+    /**
+     * @brief Helper class for implementing distributed ARON conversion tests
+     * based on the `dti::AronConversionTestInterfacePrx`.
+     *
+     * Example usage:
+     *
+     * @code
+     *
+     * armarx::aron::test::dti::AronConversionTestInterfacePrx pythonComponent = ...;
+     *
+     * auto myAronClassProbeFn = []()
+     * {
+     *     my::arondto::MyAronClass probe;
+     *     probe.data = "42";
+     *     return probe,
+     * };
+     *
+     * armarx::aron::test::AronConversionTester tester(pythonComponent);
+     *
+     * tester.test<my::arondto::MyAronClass>(myAronClassProbeFn, "MyAronClass");
+     *
+     * @endcode
+     *
+     * Note that the `pythonComponent` must point to a corresponding
+     * implementation of the `AronConversionTestInterfacePrx`.
+     */
+    class AronConversionTester
+    {
+    public:
+        template <class AronClassT>
+        using ProbeFn = std::function<AronClassT()>;
+
+
+    public:
+        AronConversionTester(dti::AronConversionTestInterfacePrx interface);
+
+        /**
+         * @brief Test the conversion of a specific ARON class.
+         *
+         * @param probeFn A factory function creating a test instance of the
+         *      ARON class.
+         * @param aronClassName The name of the ARON class. Can be used by the
+         *      other component to decide which class to convert to.
+         */
+        template <class AronClassT>
+        void
+        test(ProbeFn<AronClassT> probeFn, const std::string& aronClassName)
+        {
+            const AronClassT probe = probeFn();
+
+            dto::TestAronConversionRequest req;
+            req.aronClassName = aronClassName;
+            req.probe = probe.toAronDTO();
+
+            dto::TestAronConversionResponse res = interface->testAronConversion(req);
+            if (res.success)
+            {
+                const AronClassT probeOut = AronClassT::FromAron(res.probe);
+                ARMARX_CHECK(probeOut == probe);
+
+                ARMARX_IMPORTANT << "Test successful.";
+            }
+            else
+            {
+                ARMARX_WARNING << "Conversion in Python component failed: \n" << res.errorMessage;
+            }
+        }
+
+    public:
+        dti::AronConversionTestInterfacePrx interface;
+    };
+
+} // namespace armarx::aron::test
diff --git a/source/RobotAPI/libraries/aron/test/CMakeLists.txt b/source/RobotAPI/libraries/aron/test/CMakeLists.txt
new file mode 100644
index 000000000..3a199acf0
--- /dev/null
+++ b/source/RobotAPI/libraries/aron/test/CMakeLists.txt
@@ -0,0 +1,25 @@
+set(LIB_NAME arontest)
+
+armarx_component_set_name("${LIB_NAME}")
+armarx_set_target("Library: ${LIB_NAME}")
+
+
+armarx_add_library(
+    LIBS
+        # ArmarXCore
+        ArmarXCore
+        # RobotAPI
+        RobotAPICore
+        RobotAPIInterfaces
+        aron
+
+    HEADERS
+        AronConversionTester.h
+
+    SOURCES
+        AronConversionTester.cpp
+)
+
+
+add_library(aron::test ALIAS arontest)
+add_library(${PROJECT_NAME}::Aron::test ALIAS arontest)
-- 
GitLab