blob: d5fec71fabc86d29172e6d68c4d737c65a54bdee [file] [log] [blame]
Matt Spinlera3c33e72018-05-23 13:02:51 -05001/**
2 * Copyright © 2018 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Matt Spinlera3c33e72018-05-23 13:02:51 -050016#include "callout.hpp"
17#include "dbus.hpp"
18
Matt Spinler66e07072018-09-12 10:36:14 -050019#include <experimental/filesystem>
20#include <fstream>
21
22#include <gtest/gtest.h>
23
Matt Spinlera3c33e72018-05-23 13:02:51 -050024using namespace ibm::logging;
25
26class CalloutTest : public ::testing::Test
27{
28 protected:
29 virtual void SetUp()
30 {
31 char dir[] = {"./calloutsXXXXXX"};
32
33 persistDir = mkdtemp(dir);
34 }
35
36 virtual void TearDown()
37 {
38 fs::remove_all(persistDir);
39 }
40
41 fs::path persistDir;
42};
43
44TEST_F(CalloutTest, TestPersist)
45{
46 using namespace std::literals::string_literals;
47
48 auto bus = sdbusplus::bus::new_default();
49 std::string objectPath{"/callout/path/0"};
50 std::string calloutPath{"/some/inventory/object"};
51 size_t id = 0;
52 uint64_t ts = 5;
53
54 DbusPropertyMap assetProps{{"BuildDate"s, Value{"Date42"s}},
55 {"Manufacturer"s, Value{"Mfg42"s}},
56 {"Model"s, Value{"Model42"s}},
57 {"PartNumber"s, Value{"PN42"s}},
58 {"SerialNumber"s, Value{"SN42"s}}};
59 {
60 auto callout = std::make_unique<Callout>(bus, objectPath, calloutPath,
61 id, ts, assetProps);
62 callout->serialize(persistDir);
63
64 ASSERT_EQ(fs::exists(persistDir / std::to_string(id)), true);
65 }
66
67 // Test object restoration
68 {
69 auto callout = std::make_unique<Callout>(bus, objectPath, id, ts);
70
71 ASSERT_EQ(callout->deserialize(persistDir), true);
72
73 ASSERT_EQ(callout->id(), id);
74 ASSERT_EQ(callout->ts(), ts);
75 ASSERT_EQ(callout->path(), calloutPath);
76 ASSERT_EQ(callout->buildDate(),
William A. Kennington IIIf5866e72018-11-12 15:46:11 -080077 sdbusplus::message::variant_ns::get<std::string>(
78 assetProps["BuildDate"]));
Matt Spinlera3c33e72018-05-23 13:02:51 -050079 ASSERT_EQ(callout->manufacturer(),
William A. Kennington IIIf5866e72018-11-12 15:46:11 -080080 sdbusplus::message::variant_ns::get<std::string>(
81 assetProps["Manufacturer"]));
82 ASSERT_EQ(callout->model(),
83 sdbusplus::message::variant_ns::get<std::string>(
84 assetProps["Model"]));
Matt Spinlera3c33e72018-05-23 13:02:51 -050085 ASSERT_EQ(callout->partNumber(),
William A. Kennington IIIf5866e72018-11-12 15:46:11 -080086 sdbusplus::message::variant_ns::get<std::string>(
87 assetProps["PartNumber"]));
Matt Spinlera3c33e72018-05-23 13:02:51 -050088 ASSERT_EQ(callout->serialNumber(),
William A. Kennington IIIf5866e72018-11-12 15:46:11 -080089 sdbusplus::message::variant_ns::get<std::string>(
90 assetProps["SerialNumber"]));
Matt Spinlera3c33e72018-05-23 13:02:51 -050091 }
92
93 // Test a serialization failure due to a bad timestamp
94 {
95 auto callout = std::make_unique<Callout>(bus, objectPath, id, ts + 1);
96
97 ASSERT_EQ(callout->deserialize(persistDir), false);
98 ASSERT_EQ(fs::exists(persistDir / std::to_string(id)), false);
99 }
100}