Sfoglia il codice sorgente

Extend Event class

- make Event class serializable
- extend tests for Event class
Patrick-Christopher Mattulat 3 anni fa
parent
commit
8c1ae8b2b4

+ 16 - 5
include/ls_std/event/Event.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-11-26
- * Changed:         2020-12-20
+ * Changed:         2020-12-23
  *
  * */
 
@@ -11,28 +11,39 @@
 #define LS_STD_EVENT_HPP
 
 #include <ls_std/base/Class.hpp>
+#include <ls_std/serialization/ISerializable.hpp>
+#include <memory>
 #include "EventTypes.hpp"
 
 namespace ls_std {
-  class Event : public ls_std::Class {
+  class Event : public ls_std::Class, public ISerializable {
     public:
 
-      explicit Event(ls_std::event_id  _id);
+      explicit Event(const ls_std::event_id&  _id);
       ~Event() override = default;
 
+      // implementation
+
+      ls_std::byte_field marshal() override;
+      void unmarshal(const ls_std::byte_field& _data) override;
+
+      // additional functionality
+
       void addParameter(const ls_std::event_parameter& _eventParameter);
       void clearParameterList();
       ls_std::event_id getId();
       ls_std::event_parameter_list getParameterList();
       void removeParameter(const ls_std::event_parameter_id& _id);
-      void setId(ls_std::event_id  _id);
+      void setId(const ls_std::event_id&  _id);
+      void setSerializable(std::shared_ptr<ISerializable> _serializable);
 
     private:
 
       ls_std::event_id id {};
       ls_std::event_parameter_list parameterList {};
+      std::shared_ptr<ISerializable> serializable {};
 
-      void _assignId(ls_std::event_id _id);
+      void _assignId(const ls_std::event_id& _id);
       bool _hasParameter(const ls_std::event_id&  _id);
   };
 }

+ 29 - 6
source/ls_std/event/Event.cpp

@@ -3,16 +3,34 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-11-26
- * Changed:         2020-12-20
+ * Changed:         2020-12-23
  *
  * */
 
 #include <ls_std/event/Event.hpp>
 #include <ls_std/exception/IllegalArgumentException.hpp>
 
-ls_std::Event::Event(ls_std::event_id _id) : ls_std::Class("Event")
+ls_std::Event::Event(const ls_std::event_id& _id) : ls_std::Class("Event")
 {
-  this->_assignId(std::move(_id));
+  this->_assignId(_id);
+}
+
+ls_std::byte_field ls_std::Event::marshal()
+{
+  ls_std::byte_field data {};
+
+  if(this->serializable != nullptr) {
+    data = this->serializable->marshal();
+  }
+
+  return data;
+}
+
+void ls_std::Event::unmarshal(const ls_std::byte_field &_data)
+{
+  if(this->serializable != nullptr) {
+    this->serializable->unmarshal(_data);
+  }
 }
 
 void ls_std::Event::addParameter(const ls_std::event_parameter &_eventParameter)
@@ -44,12 +62,12 @@ void ls_std::Event::removeParameter(const ls_std::event_parameter_id &_id)
   }
 }
 
-void ls_std::Event::setId(ls_std::event_id  _id)
+void ls_std::Event::setId(const ls_std::event_id&  _id)
 {
-  this->_assignId(std::move(_id));
+  this->_assignId(_id);
 }
 
-void ls_std::Event::_assignId(ls_std::event_id _id)
+void ls_std::Event::_assignId(const ls_std::event_id& _id)
 {
   if(_id.empty()) {
     throw ls_std::IllegalArgumentException {};
@@ -62,3 +80,8 @@ bool ls_std::Event::_hasParameter(const ls_std::event_id &_id)
 {
   return this->parameterList.find(_id) != this->parameterList.end();
 }
+
+void ls_std::Event::setSerializable(std::shared_ptr<ISerializable> _serializable)
+{
+  this->serializable = std::move(_serializable);
+}

+ 37 - 1
test/cases/event/EventTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-11-26
- * Changed:         2020-12-20
+ * Changed:         2020-12-23
  *
  * */
 
@@ -24,6 +24,42 @@ namespace {
       {}
   };
 
+  // implementation
+
+  TEST_F(EventTest, marshal)
+  {
+    ls_std::Event event {"OPEN_DOOR_EVENT"};
+    event.addParameter(ls_std::event_parameter{"key_available", "true"});
+    event.addParameter(ls_std::event_parameter{"door_id", "16675"});
+
+    std::shared_ptr<ls_std::SerializableJSONEvent> serializable = std::make_shared<ls_std::SerializableJSONEvent>(std::make_shared<ls_std::Event>(event));
+    event.setSerializable(serializable);
+
+    ls_std::byte_field data = event.marshal();
+    ASSERT_FALSE(data.empty());
+    std::string expectedString = R"({"id":"OPEN_DOOR_EVENT","parameterList":{"door_id":["door_id","16675"],"key_available":["key_available","true"]}})";
+    ASSERT_STREQ(expectedString.c_str(), data.c_str());
+  }
+
+  TEST_F(EventTest, unmarshal)
+  {
+    std::shared_ptr<ls_std::Event> event =  std::make_shared<ls_std::Event>("TMP_EVENT");
+    std::shared_ptr<ls_std::SerializableJSONEvent> serializable = std::make_shared<ls_std::SerializableJSONEvent>(event);
+    event->setSerializable(serializable);
+
+    std::string jsonString = R"({"id":"OPEN_DOOR_EVENT","parameterList":{"door_id":["door_id","16675"],"key_available":["key_available","true"]}})";
+    event->unmarshal(jsonString);
+    ASSERT_STREQ("OPEN_DOOR_EVENT", event->getId().c_str());
+    ls_std::event_parameter_list parameterList = event->getParameterList();
+
+    ASSERT_FALSE(parameterList.empty());
+    ASSERT_EQ(2, parameterList.size());
+    ASSERT_STREQ("16675", parameterList.at("door_id").c_str());
+    ASSERT_STREQ("true", parameterList.at("key_available").c_str());
+  }
+
+  // additional functionality
+
   TEST_F(EventTest, getClassName)
   {
     ls_std::Event event {"OPEN_DOOR_EVENT"};