Kun Yi | 4dc7648 | 2019-03-05 16:06:19 -0800 | [diff] [blame] | 1 | #include "parse_config.hpp" |
| 2 | |
| 3 | #include <gmock/gmock.h> |
| 4 | #include <gtest/gtest.h> |
| 5 | |
| 6 | using json = nlohmann::json; |
| 7 | using namespace conf; |
| 8 | |
| 9 | TEST(ParseConfigTest, ExceptionWhenMissingRequiredFields) |
| 10 | { |
| 11 | auto j = R"( |
| 12 | { |
| 13 | "blobBaseId": "/test/" |
| 14 | } |
| 15 | )"_json; |
| 16 | |
| 17 | BinaryBlobConfig config; |
| 18 | |
| 19 | EXPECT_THROW(parseFromConfigFile(j, config), std::exception); |
| 20 | } |
| 21 | |
| 22 | TEST(ParseConfigTest, TestSimpleConfig) |
| 23 | { |
| 24 | auto j = R"( |
| 25 | { |
| 26 | "blobBaseId": "/test/", |
| 27 | "sysFilePath": "/sys/fake/path", |
| 28 | "offsetBytes": 32, |
Willy Tu | 7f10780 | 2023-11-06 23:05:25 -0800 | [diff] [blame^] | 29 | "maxSizeBytes": 2, |
| 30 | "aliasBlobBaseId": "/test2/", |
| 31 | "migrateToAlias": true |
Kun Yi | 4dc7648 | 2019-03-05 16:06:19 -0800 | [diff] [blame] | 32 | } |
| 33 | )"_json; |
| 34 | |
| 35 | BinaryBlobConfig config; |
| 36 | |
| 37 | EXPECT_NO_THROW(parseFromConfigFile(j, config)); |
| 38 | EXPECT_EQ(config.blobBaseId, "/test/"); |
| 39 | EXPECT_EQ(config.sysFilePath, "/sys/fake/path"); |
| 40 | EXPECT_EQ(config.offsetBytes, 32); |
| 41 | EXPECT_EQ(config.maxSizeBytes, 2); |
Willy Tu | 7f10780 | 2023-11-06 23:05:25 -0800 | [diff] [blame^] | 42 | EXPECT_EQ(config.aliasBlobBaseId, "/test2/"); |
| 43 | EXPECT_TRUE(config.migrateToAlias); |
Kun Yi | 4dc7648 | 2019-03-05 16:06:19 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | TEST(ParseConfigTest, TestConfigArray) |
| 47 | { |
| 48 | auto j = R"( |
| 49 | [{ |
| 50 | "blobBaseId": "/test/", |
| 51 | "sysFilePath": "/sys/fake/path", |
| 52 | "offsetBytes": 32, |
| 53 | "maxSizeBytes": 32 |
| 54 | }, |
| 55 | { |
| 56 | "blobBaseId": "/test/", |
| 57 | "sysFilePath": "/another/path" |
Willy Tu | 2b6683f | 2022-02-20 01:45:42 -0800 | [diff] [blame] | 58 | }, |
| 59 | { |
| 60 | "blobBaseId": "/test/", |
| 61 | "sysFilePath": "/another/path", |
| 62 | "offsetBytes": 32 |
| 63 | }, |
| 64 | { |
| 65 | "blobBaseId": "/test/", |
| 66 | "sysFilePath": "/another/path", |
| 67 | "maxSizeBytes": 32 |
Kun Yi | 4dc7648 | 2019-03-05 16:06:19 -0800 | [diff] [blame] | 68 | }] |
| 69 | )"_json; |
| 70 | |
| 71 | for (auto& element : j) |
| 72 | { |
| 73 | BinaryBlobConfig config; |
| 74 | |
| 75 | EXPECT_NO_THROW(parseFromConfigFile(element, config)); |
| 76 | EXPECT_EQ(config.blobBaseId, "/test/"); |
Willy Tu | 2b6683f | 2022-02-20 01:45:42 -0800 | [diff] [blame] | 77 | EXPECT_TRUE(config.offsetBytes == std::nullopt || |
| 78 | *config.offsetBytes == 32); |
| 79 | EXPECT_TRUE(config.maxSizeBytes == std::nullopt || |
| 80 | *config.maxSizeBytes == 32); |
Kun Yi | 4dc7648 | 2019-03-05 16:06:19 -0800 | [diff] [blame] | 81 | } |
| 82 | } |