blob: c1e98b6c0a45f54643732bf2b53e2b3f1650fc39 [file] [log] [blame]
SunnySrivastava1984a0d460e2020-06-03 07:49:26 -05001#include "editor_impl.hpp"
2#include "parser.hpp"
3
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(
72 itemEEPROM["extraInterfaces"][LOCATION_CODE_INF]["LocationCode"]
73 .get_ref<const nlohmann::json::string_t&>(),
74 itemEEPROM["inventoryPath"]
75 .get_ref<const nlohmann::json::string_t&>());
76 }
77 }
78}
79
80TEST_F(vpdManagerEditorTest, InvalidFile)
81{
82 Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y',
83 'D', 'A', 'T', 'A', 'O', 'K'};
84
85 Binary emptyVpdFile;
86 try
87 {
88 // Invalid kwd name
89 EditorImpl edit("VINI", "SN", std::move(emptyVpdFile));
90 edit.updateKeyword(dataToUodate);
91 }
92 catch (const std::exception& e)
93 {
94 EXPECT_EQ(std::string(e.what()), std::string("Invalid File"));
95 }
96}
97
98TEST_F(vpdManagerEditorTest, InvalidHeader)
99{
100 Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y',
101 'D', 'A', 'T', 'A', 'O', 'K'};
102
103 readFile("vpd-manager-test/invalidHeaderFile.dat");
104 try
105 {
106 // the path is dummy
107 EditorImpl edit("VINI", "SN", std::move(vpd));
108 edit.updateKeyword(dataToUodate);
109 }
110 catch (const std::exception& e)
111 {
112 EXPECT_EQ(std::string(e.what()), std::string("VHDR record not found"));
113 }
114}
115
116TEST_F(vpdManagerEditorTest, InvalidRecordName)
117{
118 Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y',
119 'D', 'A', 'T', 'A', 'O', 'K'};
120
121 readFile("vpd-manager-test/vpdFile.dat");
122
123 try
124 {
125 // Invalid record name "VIN", path is dummy
126 EditorImpl edit("VIN", "SN", std::move(vpd));
127 edit.updateKeyword(dataToUodate);
128 }
129 catch (const std::exception& e)
130 {
131 EXPECT_EQ(std::string(e.what()), std::string("Record not found"));
132 }
133}
134
135TEST_F(vpdManagerEditorTest, InvalidKWdName)
136{
137 Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y',
138 'D', 'A', 'T', 'A', 'O', 'K'};
139
140 readFile("vpd-manager-test/vpdFile.dat");
141
142 try
143 {
144 // All valid data
145 EditorImpl edit("VINI", "Sn", std::move(vpd));
146 edit.updateKeyword(dataToUodate);
147 }
148 catch (std::runtime_error& e)
149 {
150 EXPECT_EQ(std::string(e.what()), std::string("Keyword not found"));
151 }
152}
153
154TEST_F(vpdManagerEditorTest, UpdateKwd_Success)
155{
156 Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y',
157 'D', 'A', 'T', 'A', 'O', 'K'};
158
159 readFile("vpd-manager-test/vpdFile.dat");
160
161 try
162 {
163 // All valid data
164 EditorImpl edit("VINI", "SN", std::move(vpd));
165 edit.updateKeyword(dataToUodate);
166 }
167 catch (std::runtime_error& e)
168 {
169 EXPECT_EQ(std::string(e.what()),
170 std::string("Data updated successfully"));
171 }
172}
173
174int main(int argc, char** argv)
175{
176 ::testing::InitGoogleTest(&argc, argv);
177
178 return RUN_ALL_TESTS();
179}