1 #include <util/data_file.hpp>
2 #include <util/temporary_file.hpp>
3 #include <util/trace.hpp>
4
5 #include "gtest/gtest.h"
6
7 using namespace std;
8 using namespace util;
9
10 using json = nlohmann::json;
11
TEST(UtilDataFile,TestFindFiles)12 TEST(UtilDataFile, TestFindFiles)
13 {
14 // The parent folder is determined by the file system.
15 // On Linux it is /tmp.
16 fs::path dataDir = fs::temp_directory_path();
17 // Regex pattern of the temp files used by TemporaryFile class.
18 auto regexPattern = R"(openpower\-hw\-diags\-.*)";
19 // The vector to store temp files.
20 vector<fs::path> dataPaths;
21
22 // Create two new temp file objects.
23 TemporaryFile tempFile1{};
24 TemporaryFile tempFile2{};
25
26 // Get the string of paths of temp files.
27 string fullPathTempFile1 = tempFile1.getPath();
28 string fullPathTempFile2 = tempFile2.getPath();
29 EXPECT_NE(fullPathTempFile1, fullPathTempFile2);
30
31 trace::inf("fullPathTempFile1: %s", fullPathTempFile1.c_str());
32 trace::inf("fullPathTempFile2: %s", fullPathTempFile2.c_str());
33
34 // Path objects of two temp files
35 // path1 and path2 will be used to test later.
36 fs::path path1 = fullPathTempFile1;
37 fs::path path2 = fullPathTempFile2;
38
39 // After creating new temp files, call the function under test.
40 util::findFiles(dataDir, regexPattern, dataPaths);
41
42 vector<fs::path>::iterator it;
43 // Verify that the newly created temp files were in vector dataPaths
44 it = find(dataPaths.begin(), dataPaths.end(), path1);
45 EXPECT_TRUE(it != dataPaths.end());
46 it = find(dataPaths.begin(), dataPaths.end(), path2);
47 EXPECT_TRUE(it != dataPaths.end());
48
49 // Remove the newly created temp files.
50 tempFile1.remove();
51 tempFile2.remove();
52 }
53
TEST(UtilDataFile,TestValidateJson)54 TEST(UtilDataFile, TestValidateJson)
55 {
56 // The json object that is used as schema for other json objects.
57 // Copied/modified from file:
58 // ./analyzer/ras-data/schema/ras-data-schema-v01.json
59 json schema_obj = R"({
60 "$schema": "https://json-schema.org/draft/2020-12/schema",
61 "title": "RAS Data schema for openpower-hw-diags",
62 "version": 1,
63 "type": "object",
64 "additionalProperties": false,
65 "required": [ "model_ec" ],
66 "properties": {
67 "model_ec": {
68 "type": "string",
69 "pattern": "^[0-9A-Fa-f]{8}$"
70 },
71 "version": {
72 "type": "integer",
73 "minimum": 1
74 }
75 }
76 })"_json;
77
78 // The json objects
79 // Copied/modified from file:
80 // ./analyzer/ras-data/data/ras-data-p10-20.json
81 json json_obj = R"({
82 "model_ec" : "20da0020",
83 "version" : 1
84 })"_json;
85
86 EXPECT_TRUE(util::validateJson(schema_obj, json_obj));
87
88 // Test negative scenario.
89 // The key "version_1" does not exist in the schema.
90 json json_obj1 = R"({
91 "model_ec" : "20da0020",
92 "version_1" : 1
93 })"_json;
94
95 EXPECT_FALSE(util::validateJson(schema_obj, json_obj1));
96 }
97