austinfcui | ab1e4dd | 2022-03-14 22:34:47 -0500 | [diff] [blame^] | 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 | |
| 12 | TEST(UtilDataFile, TestFindFiles) |
| 13 | { |
| 14 | TemporaryFile tempFile1{}; |
| 15 | TemporaryFile tempFile2{}; |
| 16 | |
| 17 | string fullPathTempFile1 = tempFile1.getPath(); |
| 18 | string fullPathTempFile2 = tempFile2.getPath(); |
| 19 | EXPECT_NE(fullPathTempFile1, fullPathTempFile2); |
| 20 | |
| 21 | trace::inf("fullPathTempFile1: %s", fullPathTempFile1.c_str()); |
| 22 | trace::inf("fullPathTempFile2: %s", fullPathTempFile2.c_str()); |
| 23 | |
| 24 | // path1 and path2 will be used to test later. |
| 25 | fs::path path1 = fullPathTempFile1; |
| 26 | fs::path path2 = fullPathTempFile2; |
| 27 | |
| 28 | // parent pathes for path1 and path2 are the same. |
| 29 | fs::path dataDir = path1.parent_path(); |
| 30 | auto regexPattern = R"(openpower\-hw\-diags\-.*)"; |
| 31 | vector<fs::path> dataPaths; |
| 32 | |
| 33 | trace::inf("parent path: %s", string(dataDir).c_str()); |
| 34 | // call the function under test. |
| 35 | util::findFiles(dataDir, regexPattern, dataPaths); |
| 36 | |
| 37 | for (auto path : dataPaths) |
| 38 | { |
| 39 | trace::inf("path in dataPath vector: %s", path.c_str()); |
| 40 | } |
| 41 | |
| 42 | EXPECT_EQ(2, dataPaths.size()); |
| 43 | |
| 44 | vector<fs::path>::iterator it; |
| 45 | it = find(dataPaths.begin(), dataPaths.end(), path1); |
| 46 | EXPECT_TRUE(it != dataPaths.end()); |
| 47 | it = find(dataPaths.begin(), dataPaths.end(), path2); |
| 48 | EXPECT_TRUE(it != dataPaths.end()); |
| 49 | } |
| 50 | |
| 51 | TEST(UtilDataFile, TestValidateJson) |
| 52 | { |
| 53 | // The json object that is used as schema for other json objects. |
| 54 | // Copied/modified from file: |
| 55 | // ./analyzer/ras-data/schema/ras-data-schema-v01.json |
| 56 | json schema_obj = R"({ |
| 57 | "$schema": "https://json-schema.org/draft/2020-12/schema", |
| 58 | "title": "RAS Data schema for openpower-hw-diags", |
| 59 | "version": 1, |
| 60 | "type": "object", |
| 61 | "additionalProperties": false, |
| 62 | "required": [ "model_ec" ], |
| 63 | "properties": { |
| 64 | "model_ec": { |
| 65 | "type": "string", |
| 66 | "pattern": "^[0-9A-Fa-f]{8}$" |
| 67 | }, |
| 68 | "version": { |
| 69 | "type": "integer", |
| 70 | "minimum": 1 |
| 71 | } |
| 72 | } |
| 73 | })"_json; |
| 74 | |
| 75 | // The json objects |
| 76 | // Copied/modified from file: |
| 77 | // ./analyzer/ras-data/data/ras-data-p10-20.json |
| 78 | json json_obj = R"({ |
| 79 | "model_ec" : "20da0020", |
| 80 | "version" : 1 |
| 81 | })"_json; |
| 82 | |
| 83 | EXPECT_TRUE(util::validateJson(schema_obj, json_obj)); |
| 84 | |
| 85 | // Test negative scenario. |
| 86 | // The key "version_1" does not exist in the schema. |
| 87 | json json_obj1 = R"({ |
| 88 | "model_ec" : "20da0020", |
| 89 | "version_1" : 1 |
| 90 | })"_json; |
| 91 | |
| 92 | EXPECT_FALSE(util::validateJson(schema_obj, json_obj1)); |
| 93 | } |