blob: 10b55fb92c3f34863ccdecf45832d285b760e7cd [file] [log] [blame]
/**
* 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 "callout.hpp"
#include "dbus.hpp"
#include <experimental/filesystem>
#include <fstream>
#include <gtest/gtest.h>
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(),
std::get<std::string>(assetProps["BuildDate"]));
ASSERT_EQ(callout->manufacturer(),
std::get<std::string>(assetProps["Manufacturer"]));
ASSERT_EQ(callout->model(), std::get<std::string>(assetProps["Model"]));
ASSERT_EQ(callout->partNumber(),
std::get<std::string>(assetProps["PartNumber"]));
ASSERT_EQ(callout->serialNumber(),
std::get<std::string>(assetProps["SerialNumber"]));
}
// 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);
}
}