Krzysztof Grobelny | 73da690 | 2020-09-24 13:42:04 +0200 | [diff] [blame] | 1 | #include "persistent_json_storage.hpp" |
| 2 | |
| 3 | #include "gmock/gmock.h" |
| 4 | #include "gtest/gtest.h" |
| 5 | |
| 6 | using namespace testing; |
| 7 | |
| 8 | class TestPersistentJsonStorage : public Test |
| 9 | { |
| 10 | public: |
| 11 | using FilePath = interfaces::JsonStorage::FilePath; |
| 12 | using DirectoryPath = interfaces::JsonStorage::DirectoryPath; |
| 13 | |
| 14 | static void SetUpTestSuite() |
| 15 | { |
| 16 | ASSERT_FALSE(std::filesystem::exists(directory)); |
| 17 | } |
| 18 | |
| 19 | void TearDown() override |
| 20 | { |
| 21 | if (std::filesystem::exists(directory)) |
| 22 | { |
| 23 | std::filesystem::remove_all(directory); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | const FilePath fileName = FilePath("report/1/file.txt"); |
| 28 | |
| 29 | static const DirectoryPath directory; |
| 30 | PersistentJsonStorage sut{directory}; |
| 31 | }; |
| 32 | |
| 33 | const interfaces::JsonStorage::DirectoryPath |
| 34 | TestPersistentJsonStorage::directory = |
| 35 | interfaces::JsonStorage::DirectoryPath(std::tmpnam(nullptr)); |
| 36 | |
| 37 | TEST_F(TestPersistentJsonStorage, storesJsonData) |
| 38 | { |
| 39 | nlohmann::json data = nlohmann::json::object(); |
| 40 | data["name"] = "kevin"; |
| 41 | data["lastname"] = "mc calister"; |
| 42 | |
| 43 | sut.store(fileName, data); |
| 44 | |
| 45 | ASSERT_THAT(sut.load(fileName), Eq(data)); |
| 46 | } |
| 47 | |
| 48 | TEST_F(TestPersistentJsonStorage, emptyListWhenNoReportsCreated) |
| 49 | { |
| 50 | EXPECT_THAT(sut.list(DirectoryPath("report")), SizeIs(0u)); |
| 51 | } |
| 52 | |
| 53 | TEST_F(TestPersistentJsonStorage, listSavedReports) |
| 54 | { |
| 55 | sut.store(FilePath("report/domain-1/name-1/conf-1.json"), |
| 56 | nlohmann::json("data-1a")); |
| 57 | sut.store(FilePath("report/domain-1/name-2/conf-1.json"), |
| 58 | nlohmann::json("data-2a")); |
| 59 | sut.store(FilePath("report/domain-1/name-2/conf-2.json"), |
| 60 | nlohmann::json("data-2b")); |
| 61 | sut.store(FilePath("report/domain-2/name-1/conf-1.json"), |
| 62 | nlohmann::json("data-3a")); |
| 63 | |
| 64 | EXPECT_THAT(sut.list(DirectoryPath("report")), |
| 65 | UnorderedElementsAre(FilePath("report/domain-1/name-1"), |
| 66 | FilePath("report/domain-1/name-2"), |
| 67 | FilePath("report/domain-2/name-1"))); |
| 68 | } |
| 69 | |
| 70 | TEST_F(TestPersistentJsonStorage, listSavedReportsWithoutRemovedOnes) |
| 71 | { |
| 72 | sut.store(FilePath("report/domain-1/name-1/conf-1.json"), |
| 73 | nlohmann::json("data-1a")); |
| 74 | sut.store(FilePath("report/domain-1/name-2/conf-1.json"), |
| 75 | nlohmann::json("data-2a")); |
| 76 | sut.store(FilePath("report/domain-1/name-2/conf-2.json"), |
| 77 | nlohmann::json("data-2b")); |
| 78 | sut.store(FilePath("report/domain-2/name-1/conf-1.json"), |
| 79 | nlohmann::json("data-3a")); |
| 80 | sut.remove(FilePath("report/domain-1/name-1/conf-1.json")); |
| 81 | sut.remove(FilePath("report/domain-1/name-2/conf-2.json")); |
| 82 | |
| 83 | EXPECT_THAT(sut.list(DirectoryPath("report")), |
| 84 | UnorderedElementsAre(FilePath("report/domain-1/name-2"), |
| 85 | FilePath("report/domain-2/name-1"))); |
| 86 | } |
| 87 | |
| 88 | TEST_F(TestPersistentJsonStorage, removesStoredJson) |
| 89 | { |
| 90 | nlohmann::json data = nlohmann::json::object(); |
| 91 | data["name"] = "kevin"; |
| 92 | data["lastname"] = "mc calister"; |
| 93 | |
| 94 | sut.store(fileName, data); |
| 95 | |
| 96 | ASSERT_THAT(sut.remove(fileName), Eq(true)); |
| 97 | ASSERT_THAT(sut.load(fileName), Eq(std::nullopt)); |
| 98 | } |
| 99 | |
| 100 | TEST_F(TestPersistentJsonStorage, returnsFalseWhenDeletingNonExistingFile) |
| 101 | { |
| 102 | ASSERT_THAT(sut.remove(fileName), Eq(false)); |
| 103 | } |
| 104 | |
| 105 | TEST_F(TestPersistentJsonStorage, returnsNulloptWhenFileDoesntExist) |
| 106 | { |
| 107 | ASSERT_THAT(sut.load(fileName), Eq(std::nullopt)); |
| 108 | } |