Add Callout serialization unit tests

Tested: Testcases pass

Change-Id: I4a6c523847055607f2211d9bc8c01a2d2011675c
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/test/Makefile.am b/test/Makefile.am
index a699696..c1cce92 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -2,16 +2,45 @@
 
 TESTS = $(check_PROGRAMS)
 
-check_PROGRAMS = test_policy
-test_policy_CPPFLAGS = -Igtest $(GTEST_CPPFLAGS) $(AM_CPPFLAGS)
+check_PROGRAMS = test_policy test_callout
 
-test_policy_CXXFLAGS = $(PTHREAD_CFLAGS) $(SDBUSPLUS_CFLAGS)
-test_policy_LDFLAGS = -lgtest_main -lgtest \
-	$(PTHREAD_LIBS) $(OESDK_TESTCASE_FLAGS)
+test_cppflags = \
+	-Igtest \
+	$(GTEST_CPPFLAGS) \
+	$(AM_CPPFLAGS)
 
+test_cxxflags = \
+	$(PTHREAD_CFLAGS) \
+	$(PHOSPHOR_DBUS_INTERFACES_CFLAGS) \
+	$(IBM_DBUS_INTERFACES_CFLAGS) \
+	$(SDBUSPLUS_CFLAGS) \
+	$(PHOSPHOR_LOGGING_CFLAGS)
+
+test_ldflags = \
+	-lgtest_main -lgtest \
+	-lstdc++fs \
+	$(PTHREAD_LIBS) \
+	$(OESDK_TESTCASE_FLAGS) \
+	$(PHOSPHOR_DBUS_INTERFACES_LIBS) \
+	$(IBM_DBUS_INTERFACES_LIBS) \
+	$(SDBUSPLUS_LIBS)
+
+test_policy_CPPFLAGS = $(test_cppflags)
+test_policy_CXXFLAGS = $(test_cxxflags)
+test_policy_LDFLAGS = $(test_ldflags)
 test_policy_SOURCES = test_policy.cpp
 test_policy_LDADD = \
 	$(top_builddir)/policy_table.o \
-	$(top_builddir)/policy_find.o \
-	-lstdc++fs \
-	$(SDBUSPLUS_LIBS)
+	$(top_builddir)/policy_find.o
+
+test_callout_CPPFLAGS = $(test_cppflags)
+test_callout_CXXFLAGS = $(test_cxxflags)
+test_callout_LDFLAGS = $(test_ldflags)
+test_callout_SOURCES = test_callout.cpp
+
+#TODO: remove ObjectPath when it merges in phosphor-dbus-interfaces
+test_callout_LDADD = \
+	$(top_builddir)/xyz/openbmc_project/Common/ObjectPath/server.o \
+	$(top_builddir)/callout.o
+
+
diff --git a/test/test_callout.cpp b/test/test_callout.cpp
new file mode 100644
index 0000000..8d656cc
--- /dev/null
+++ b/test/test_callout.cpp
@@ -0,0 +1,92 @@
+/**
+ * Copyright © 2018 IBM Corporation
+ *
+ * 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.
+ */
+#include <fstream>
+#include <gtest/gtest.h>
+#include <experimental/filesystem>
+#include "callout.hpp"
+#include "dbus.hpp"
+
+using namespace ibm::logging;
+
+class CalloutTest : public ::testing::Test
+{
+  protected:
+    virtual void SetUp()
+    {
+        char dir[] = {"./calloutsXXXXXX"};
+
+        persistDir = mkdtemp(dir);
+    }
+
+    virtual void TearDown()
+    {
+        fs::remove_all(persistDir);
+    }
+
+    fs::path persistDir;
+};
+
+TEST_F(CalloutTest, TestPersist)
+{
+    using namespace std::literals::string_literals;
+
+    auto bus = sdbusplus::bus::new_default();
+    std::string objectPath{"/callout/path/0"};
+    std::string calloutPath{"/some/inventory/object"};
+    size_t id = 0;
+    uint64_t ts = 5;
+
+    DbusPropertyMap assetProps{{"BuildDate"s, Value{"Date42"s}},
+                               {"Manufacturer"s, Value{"Mfg42"s}},
+                               {"Model"s, Value{"Model42"s}},
+                               {"PartNumber"s, Value{"PN42"s}},
+                               {"SerialNumber"s, Value{"SN42"s}}};
+    {
+        auto callout = std::make_unique<Callout>(bus, objectPath, calloutPath,
+                                                 id, ts, assetProps);
+        callout->serialize(persistDir);
+
+        ASSERT_EQ(fs::exists(persistDir / std::to_string(id)), true);
+    }
+
+    // Test object restoration
+    {
+        auto callout = std::make_unique<Callout>(bus, objectPath, id, ts);
+
+        ASSERT_EQ(callout->deserialize(persistDir), true);
+
+        ASSERT_EQ(callout->id(), id);
+        ASSERT_EQ(callout->ts(), ts);
+        ASSERT_EQ(callout->path(), calloutPath);
+        ASSERT_EQ(callout->buildDate(),
+                  assetProps["BuildDate"].get<std::string>());
+        ASSERT_EQ(callout->manufacturer(),
+                  assetProps["Manufacturer"].get<std::string>());
+        ASSERT_EQ(callout->model(), assetProps["Model"].get<std::string>());
+        ASSERT_EQ(callout->partNumber(),
+                  assetProps["PartNumber"].get<std::string>());
+        ASSERT_EQ(callout->serialNumber(),
+                  assetProps["SerialNumber"].get<std::string>());
+    }
+
+    // Test a serialization failure due to a bad timestamp
+    {
+        auto callout = std::make_unique<Callout>(bus, objectPath, id, ts + 1);
+
+        ASSERT_EQ(callout->deserialize(persistDir), false);
+        ASSERT_EQ(fs::exists(persistDir / std::to_string(id)), false);
+    }
+}