blob: e9ca8ccb77b7e4e1abb10377d06b201d337bdb2d [file] [log] [blame]
Tom Joseph250c4752020-04-15 10:32:45 +05301#include "dbus_to_host_effecters.hpp"
2#include "mocked_utils.hpp"
3#include "utils.hpp"
4
5#include <nlohmann/json.hpp>
6
7#include <iostream>
8
9#include <gtest/gtest.h>
10
11using namespace pldm::host_effecters;
12using namespace pldm::utils;
13using namespace pldm::dbus_api;
14
15class MockHostEffecterParser : public HostEffecterParser
16{
17 public:
18 MockHostEffecterParser(int fd, const pldm_pdr* repo,
19 DBusHandler* const dbusHandler,
20 const std::string& jsonPath) :
21 HostEffecterParser(nullptr, fd, repo, dbusHandler, jsonPath)
22 {}
23
24 MOCK_METHOD(void, setHostStateEffecter,
25 (size_t, std::vector<set_effecter_state_field>&, uint16_t),
26 (const override));
27
28 MOCK_METHOD(void, createHostEffecterMatch,
29 (const std::string&, const std::string&, size_t, size_t,
30 uint16_t),
31 (const override));
32
33 const std::vector<EffecterInfo>& gethostEffecterInfo()
34 {
35 return hostEffecterInfo;
36 }
37};
38
39TEST(HostEffecterParser, parseEffecterJsonGoodPath)
40{
41 MockdBusHandler dbusHandler;
42 int sockfd{};
43 MockHostEffecterParser hostEffecterParserGood(sockfd, nullptr, &dbusHandler,
44 "./host_effecter_jsons/good");
45 auto hostEffecterInfo = hostEffecterParserGood.gethostEffecterInfo();
46 ASSERT_EQ(hostEffecterInfo.size(), 1);
47 ASSERT_EQ(hostEffecterInfo[0].entityInstance, 0);
48 ASSERT_EQ(hostEffecterInfo[0].entityType, 33);
49 ASSERT_EQ(hostEffecterInfo[0].dbusInfo.size(), 1);
50 DBusEffecterMapping dbusInfo{
51 {"/xyz/openbmc_project/control/host0/boot",
52 "xyz.openbmc_project.Control.Boot.Mode", "BootMode", "string"},
53 {"xyz.openbmc_project.Control.Boot.Mode.Modes.Regular"},
54 {196, {2}}};
55 auto& temp = hostEffecterInfo[0].dbusInfo[0];
56 ASSERT_EQ(temp.dbusMap.objectPath == dbusInfo.dbusMap.objectPath, true);
57 ASSERT_EQ(temp.dbusMap.interface == dbusInfo.dbusMap.interface, true);
58 ASSERT_EQ(temp.dbusMap.propertyName == dbusInfo.dbusMap.propertyName, true);
59 ASSERT_EQ(temp.dbusMap.propertyType == dbusInfo.dbusMap.propertyType, true);
60}
61
62TEST(HostEffecterParser, parseEffecterJsonBadPath)
63{
64 MockdBusHandler dbusHandler;
65 int sockfd{};
66 MockHostEffecterParser hostEffecterParser(sockfd, nullptr, &dbusHandler,
67 "./host_effecter_jsons/no_json");
68 ASSERT_THROW(
69 hostEffecterParser.parseEffecterJson("./host_effecter_jsons/no_json"),
70 std::exception);
71 ASSERT_THROW(
72 hostEffecterParser.parseEffecterJson("./host_effecter_jsons/malformed"),
73 std::exception);
74}
75
76TEST(HostEffecterParser, findNewStateValue)
77{
78 MockdBusHandler dbusHandler;
79 int sockfd{};
80 MockHostEffecterParser hostEffecterParser(sockfd, nullptr, &dbusHandler,
81 "./host_effecter_jsons/good");
82
83 PropertyValue val1{std::in_place_type<std::string>,
84 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular"};
85 PropertyValue val2{std::in_place_type<std::string>,
86 "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup"};
87 auto newState = hostEffecterParser.findNewStateValue(0, 0, val1);
88 ASSERT_EQ(newState, 2);
89
90 ASSERT_THROW(hostEffecterParser.findNewStateValue(0, 0, val2),
91 std::exception);
92}