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