Browse Source

Added SerializableJSONStateConnection class

- added SerializableJSONStateConnection class to offer
functionality for serializing StateConnection class
- added tests for SerializableJSONStateConnection class
Patrick-Laptop 3 years ago
parent
commit
f303f5d601

+ 5 - 2
CMakeLists.txt

@@ -98,7 +98,9 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/logic/State.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/logic/StateConnection.hpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/logic/StateConnection.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/source/logic/StateMachineTypes.hpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/logic/StateMachineTypes.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/json/logic/SerializableJSONStateConnection.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/json/logic/SerializableJSONStateConnection.cpp)
 
 set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/IntegerTest.cpp
@@ -127,7 +129,8 @@ set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/json/boxing/SerializableJSONBooleanTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/logic/StateTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/logic/StateMachineTest.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/logic/StateConnectionTest.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/logic/StateConnectionTest.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/json/logic/SerializableJSONStateConnectionTest.cpp)
 
 ##########################################################
 # Build

+ 40 - 0
source/serialization/json/logic/SerializableJSONStateConnection.cpp

@@ -0,0 +1,40 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-09-14
+ * Changed:         2020-09-14
+ *
+ * */
+
+#include "SerializableJSONStateConnection.hpp"
+
+ls_std::SerializableJSONStateConnection::SerializableJSONStateConnection(std::shared_ptr<ls_std::StateConnection> _value) :
+Class("SerializableJSONStateConnection"),
+value(std::move(_value))
+{}
+
+ls_std::byte_field ls_std::SerializableJSONStateConnection::marshal()
+{
+  this->_update();
+  return this->jsonObject.dump();
+}
+
+void ls_std::SerializableJSONStateConnection::unmarshal(const ls_std::byte_field &_data)
+{
+  std::string jsonString = std::string(_data);
+  this->jsonObject = nlohmann::json::parse(jsonString);
+
+  this->value->setConnectionId(this->jsonObject["connectionId"]);
+  this->value->setStateId(this->jsonObject["stateId"]);
+  this->value->updatePassCondition(this->jsonObject["condition"]);
+}
+
+void ls_std::SerializableJSONStateConnection::_update()
+{
+  this->jsonObject = {
+      {"condition", this->value->isPassable()},
+      {"connectionId", this->value->getConnectionId()},
+      {"stateId", this->value->getStateId()}
+  };
+}

+ 38 - 0
source/serialization/json/logic/SerializableJSONStateConnection.hpp

@@ -0,0 +1,38 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-09-14
+ * Changed:         2020-09-14
+ *
+ * */
+
+#ifndef LS_STD_SERIALIZABLE_JSON_STATE_CONNECTION_HPP
+#define LS_STD_SERIALIZABLE_JSON_STATE_CONNECTION_HPP
+
+#include "../../../base/Class.hpp"
+#include "../../ISerializable.hpp"
+#include "../../../logic/StateConnection.hpp"
+#include <memory>
+#include <json.hpp>
+
+namespace ls_std {
+  class SerializableJSONStateConnection : public Class, public ISerializable {
+    public:
+
+      SerializableJSONStateConnection(std::shared_ptr<ls_std::StateConnection> _value);
+      ~SerializableJSONStateConnection() = default;
+
+      ls_std::byte_field marshal() override;
+      void unmarshal(const ls_std::byte_field& _data) override;
+
+    private:
+
+      std::shared_ptr<ls_std::StateConnection> value {};
+      nlohmann::json jsonObject {};
+
+      void _update();
+  };
+}
+
+#endif

+ 49 - 0
test/cases/serialization/json/logic/SerializableJSONStateConnectionTest.cpp

@@ -0,0 +1,49 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-09-14
+ * Changed:         2020-09-14
+ *
+ * */
+
+#include <gtest/gtest.h>
+#include "../../../../../source/boxing/String.hpp"
+#include "../../../../../source/logic/StateConnection.hpp"
+#include "../../../../../source/serialization/json/logic/SerializableJSONStateConnection.hpp"
+
+namespace {
+  class SerializableJSONStateConnectionTest : public ::testing::Test {
+    protected:
+
+      SerializableJSONStateConnectionTest() = default;
+      ~SerializableJSONStateConnectionTest() override = default;
+
+      void SetUp() override {}
+      void TearDown() override {}
+  };
+
+  TEST_F(SerializableJSONStateConnectionTest, marshal)
+  {
+    ls_std::StateConnection x {"AB", "B"};
+    ls_std::SerializableJSONStateConnection serializable {std::make_shared<ls_std::StateConnection>(x)};
+    ls_std::String jsonString {serializable.marshal()};
+
+    ASSERT_STREQ(R"({"condition":false,"connectionId":"AB","stateId":"B"})", jsonString.toString().c_str());
+  }
+
+  TEST_F(SerializableJSONStateConnectionTest, unmarshal)
+  {
+    std::shared_ptr<ls_std::StateConnection> x = std::make_shared<ls_std::StateConnection>("AB", "B");
+    ASSERT_STREQ("AB", x->getConnectionId().c_str());
+    ASSERT_STREQ("B", x->getStateId().c_str());
+    ASSERT_FALSE(x->isPassable());
+
+    ls_std::SerializableJSONStateConnection serializable {x};
+    serializable.unmarshal(R"({"condition":true,"connectionId":"BC","stateId":"C"})");
+
+    ASSERT_STREQ("BC", x->getConnectionId().c_str());
+    ASSERT_STREQ("C", x->getStateId().c_str());
+    ASSERT_TRUE(x->isPassable());
+  }
+}