1 #include "persistent_json_storage.hpp"
2 #include "printers.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(std::tmpnam(nullptr));
37 
38 TEST_F(TestPersistentJsonStorage, storesJsonData)
39 {
40     nlohmann::json data = nlohmann::json::object();
41     data["name"] = "kevin";
42     data["lastname"] = "mc calister";
43 
44     sut.store(fileName, data);
45 
46     ASSERT_THAT(sut.load(fileName), Eq(data));
47 }
48 
49 TEST_F(TestPersistentJsonStorage, emptyListWhenNoReportsCreated)
50 {
51     EXPECT_THAT(sut.list(), SizeIs(0u));
52 }
53 
54 TEST_F(TestPersistentJsonStorage, listSavedReports)
55 {
56     sut.store(FilePath("report/domain-1/name-1/conf-1.json"),
57               nlohmann::json("data-1a"));
58     sut.store(FilePath("report/domain-1/name-2/conf-1.json"),
59               nlohmann::json("data-2a"));
60     sut.store(FilePath("report/domain-1/name-2/conf-2.json"),
61               nlohmann::json("data-2b"));
62     sut.store(FilePath("report/domain-2/name-1/conf-1.json"),
63               nlohmann::json("data-3a"));
64 
65     EXPECT_THAT(
66         sut.list(),
67         UnorderedElementsAre(FilePath("report/domain-1/name-1/conf-1.json"),
68                              FilePath("report/domain-1/name-2/conf-1.json"),
69                              FilePath("report/domain-1/name-2/conf-2.json"),
70                              FilePath("report/domain-2/name-1/conf-1.json")));
71 }
72 
73 TEST_F(TestPersistentJsonStorage, listSavedReportsWithoutRemovedOnes)
74 {
75     sut.store(FilePath("report/domain-1/name-1/conf-1.json"),
76               nlohmann::json("data-1a"));
77     sut.store(FilePath("report/domain-1/name-2/conf-1.json"),
78               nlohmann::json("data-2a"));
79     sut.store(FilePath("report/domain-1/name-2/conf-2.json"),
80               nlohmann::json("data-2b"));
81     sut.store(FilePath("report/domain-2/name-1/conf-1.json"),
82               nlohmann::json("data-3a"));
83     sut.remove(FilePath("report/domain-1/name-1/conf-1.json"));
84     sut.remove(FilePath("report/domain-1/name-2/conf-2.json"));
85 
86     EXPECT_THAT(
87         sut.list(),
88         UnorderedElementsAre(FilePath("report/domain-1/name-2/conf-1.json"),
89                              FilePath("report/domain-2/name-1/conf-1.json")));
90 }
91 
92 TEST_F(TestPersistentJsonStorage, removesStoredJson)
93 {
94     nlohmann::json data = nlohmann::json::object();
95     data["name"] = "kevin";
96     data["lastname"] = "mc calister";
97 
98     sut.store(fileName, data);
99 
100     ASSERT_THAT(sut.remove(fileName), Eq(true));
101     ASSERT_THAT(sut.load(fileName), Eq(std::nullopt));
102 }
103 
104 TEST_F(TestPersistentJsonStorage, returnsFalseWhenDeletingNonExistingFile)
105 {
106     ASSERT_THAT(sut.remove(fileName), Eq(false));
107 }
108 
109 TEST_F(TestPersistentJsonStorage, returnsNulloptWhenFileDoesntExist)
110 {
111     ASSERT_THAT(sut.load(fileName), Eq(std::nullopt));
112 }
113