blob: 46e43ae82c7cb80ec5361f9cd8c339d502366cef [file] [log] [blame]
austinfcuiab1e4dd2022-03-14 22:34:47 -05001#include <util/data_file.hpp>
2#include <util/temporary_file.hpp>
3#include <util/trace.hpp>
4
5#include "gtest/gtest.h"
6
7using namespace std;
8using namespace util;
9
10using json = nlohmann::json;
11
12TEST(UtilDataFile, TestFindFiles)
13{
austinfcuidd779992022-03-25 21:13:18 -050014 // 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.
austinfcuiab1e4dd2022-03-14 22:34:47 -050023 TemporaryFile tempFile1{};
24 TemporaryFile tempFile2{};
25
austinfcuidd779992022-03-25 21:13:18 -050026 // Get the string of paths of temp files.
austinfcuiab1e4dd2022-03-14 22:34:47 -050027 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
austinfcuidd779992022-03-25 21:13:18 -050034 // Path objects of two temp files
austinfcuiab1e4dd2022-03-14 22:34:47 -050035 // path1 and path2 will be used to test later.
36 fs::path path1 = fullPathTempFile1;
37 fs::path path2 = fullPathTempFile2;
38
austinfcuidd779992022-03-25 21:13:18 -050039 // After creating new temp files, call the function under test.
austinfcuiab1e4dd2022-03-14 22:34:47 -050040 util::findFiles(dataDir, regexPattern, dataPaths);
41
austinfcuiab1e4dd2022-03-14 22:34:47 -050042 vector<fs::path>::iterator it;
austinfcuidd779992022-03-25 21:13:18 -050043 // Verify that the newly created temp files were in vector dataPaths
austinfcuiab1e4dd2022-03-14 22:34:47 -050044 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());
austinfcuidd779992022-03-25 21:13:18 -050048
49 // Remove the newly created temp files.
50 tempFile1.remove();
51 tempFile2.remove();
austinfcuiab1e4dd2022-03-14 22:34:47 -050052}
53
54TEST(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}