blob: 7a88c59997fa23f3e590d982ec3cf830b32b3984 [file] [log] [blame]
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -05001#include "editor_impl.hpp"
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05002#include "ipz_parser.hpp"
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -05003
4#include <algorithm>
5#include <nlohmann/json.hpp>
6#include <vector>
7
8#include <gtest/gtest.h>
9
10using namespace openpower::vpd;
11using namespace openpower::vpd::manager::editor;
12using namespace openpower::vpd::inventory;
13using namespace openpower::vpd::constants;
14
15class vpdManagerEditorTest : public ::testing::Test
16{
17 protected:
18 Binary vpd;
19
20 nlohmann::json jsonFile;
21
22 // map to hold the mapping of location code and inventory path
23 inventory::LocationCodeMap fruLocationCode;
24
25 public:
26 // constructor
27 vpdManagerEditorTest()
28 {
29 processJson();
30 }
31
32 void processJson();
33 void readFile(std::string pathToFile);
34};
35
36void vpdManagerEditorTest::readFile(std::string pathToFile)
37{
38 // read the json file and parse it
39 std::ifstream vpdFile(pathToFile, std::ios::binary);
40
41 if (!vpdFile)
42 {
43 throw std::runtime_error("json file not found");
44 }
45
46 vpd.assign((std::istreambuf_iterator<char>(vpdFile)),
47 std::istreambuf_iterator<char>());
48}
49
50void vpdManagerEditorTest::processJson()
51{
52 // read the json file and parse it
53 std::ifstream json("vpd-manager-test/vpd_editor_test.json",
54 std::ios::binary);
55
56 if (!json)
57 {
58 throw std::runtime_error("json file not found");
59 }
60
61 jsonFile = nlohmann::json::parse(json);
62
63 const nlohmann::json& groupFRUS =
64 jsonFile["frus"].get_ref<const nlohmann::json::object_t&>();
65 for (const auto& itemFRUS : groupFRUS.items())
66 {
67 const std::vector<nlohmann::json>& groupEEPROM =
68 itemFRUS.value().get_ref<const nlohmann::json::array_t&>();
69 for (const auto& itemEEPROM : groupEEPROM)
70 {
71 fruLocationCode.emplace(
Alpana Kumari414d5ae2021-03-04 21:06:35 +000072 itemEEPROM["extraInterfaces"][IBM_LOCATION_CODE_INF]
73 ["LocationCode"]
74 .get_ref<const nlohmann::json::string_t&>(),
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -050075 itemEEPROM["inventoryPath"]
76 .get_ref<const nlohmann::json::string_t&>());
77 }
78 }
79}
80
81TEST_F(vpdManagerEditorTest, InvalidFile)
82{
83 Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y',
84 'D', 'A', 'T', 'A', 'O', 'K'};
85
86 Binary emptyVpdFile;
87 try
88 {
89 // Invalid kwd name
90 EditorImpl edit("VINI", "SN", std::move(emptyVpdFile));
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053091 edit.updateKeyword(dataToUodate, true);
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -050092 }
93 catch (const std::exception& e)
94 {
95 EXPECT_EQ(std::string(e.what()), std::string("Invalid File"));
96 }
97}
98
99TEST_F(vpdManagerEditorTest, InvalidHeader)
100{
101 Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y',
102 'D', 'A', 'T', 'A', 'O', 'K'};
103
104 readFile("vpd-manager-test/invalidHeaderFile.dat");
105 try
106 {
107 // the path is dummy
108 EditorImpl edit("VINI", "SN", std::move(vpd));
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530109 edit.updateKeyword(dataToUodate, true);
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500110 }
111 catch (const std::exception& e)
112 {
113 EXPECT_EQ(std::string(e.what()), std::string("VHDR record not found"));
114 }
115}
116
117TEST_F(vpdManagerEditorTest, InvalidRecordName)
118{
119 Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y',
120 'D', 'A', 'T', 'A', 'O', 'K'};
121
122 readFile("vpd-manager-test/vpdFile.dat");
123
124 try
125 {
126 // Invalid record name "VIN", path is dummy
127 EditorImpl edit("VIN", "SN", std::move(vpd));
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530128 edit.updateKeyword(dataToUodate, true);
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500129 }
130 catch (const std::exception& e)
131 {
132 EXPECT_EQ(std::string(e.what()), std::string("Record not found"));
133 }
134}
135
136TEST_F(vpdManagerEditorTest, InvalidKWdName)
137{
138 Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y',
139 'D', 'A', 'T', 'A', 'O', 'K'};
140
141 readFile("vpd-manager-test/vpdFile.dat");
142
143 try
144 {
145 // All valid data
146 EditorImpl edit("VINI", "Sn", std::move(vpd));
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530147 edit.updateKeyword(dataToUodate, true);
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500148 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500149 catch (const std::runtime_error& e)
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500150 {
151 EXPECT_EQ(std::string(e.what()), std::string("Keyword not found"));
152 }
153}
154
155TEST_F(vpdManagerEditorTest, UpdateKwd_Success)
156{
157 Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y',
158 'D', 'A', 'T', 'A', 'O', 'K'};
159
160 readFile("vpd-manager-test/vpdFile.dat");
161
162 try
163 {
164 // All valid data
165 EditorImpl edit("VINI", "SN", std::move(vpd));
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530166 edit.updateKeyword(dataToUodate, true);
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500167 }
Patrick Williams8e15b932021-10-06 13:04:22 -0500168 catch (const std::runtime_error& e)
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -0500169 {
170 EXPECT_EQ(std::string(e.what()),
171 std::string("Data updated successfully"));
172 }
173}
174
175int main(int argc, char** argv)
176{
177 ::testing::InitGoogleTest(&argc, argv);
178
179 return RUN_ALL_TESTS();
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530180}