blob: cec00195cec1e3a3d536262e3dd3a36680f40fb7 [file] [log] [blame]
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +00001#include "helpers.hpp"
Krzysztof Grobelny73da6902020-09-24 13:42:04 +02002#include "persistent_json_storage.hpp"
3
Krzysztof Grobelnya06626d2022-11-24 15:22:40 +00004#include <fstream>
5
Krzysztof Grobelny73da6902020-09-24 13:42:04 +02006#include "gmock/gmock.h"
7#include "gtest/gtest.h"
8
9using namespace testing;
10
11class TestPersistentJsonStorage : public Test
12{
13 public:
14 using FilePath = interfaces::JsonStorage::FilePath;
15 using DirectoryPath = interfaces::JsonStorage::DirectoryPath;
16
17 static void SetUpTestSuite()
18 {
19 ASSERT_FALSE(std::filesystem::exists(directory));
20 }
21
22 void TearDown() override
23 {
24 if (std::filesystem::exists(directory))
25 {
26 std::filesystem::remove_all(directory);
27 }
28 }
29
30 const FilePath fileName = FilePath("report/1/file.txt");
31
32 static const DirectoryPath directory;
33 PersistentJsonStorage sut{directory};
34};
35
36const interfaces::JsonStorage::DirectoryPath
37 TestPersistentJsonStorage::directory =
Wludzik, Jozef596a9942021-01-27 12:28:59 +010038 interfaces::JsonStorage::DirectoryPath(
39 std::filesystem::temp_directory_path() / "telemetry-tests");
Krzysztof Grobelny73da6902020-09-24 13:42:04 +020040
41TEST_F(TestPersistentJsonStorage, storesJsonData)
42{
43 nlohmann::json data = nlohmann::json::object();
44 data["name"] = "kevin";
45 data["lastname"] = "mc calister";
46
47 sut.store(fileName, data);
48
49 ASSERT_THAT(sut.load(fileName), Eq(data));
50}
51
52TEST_F(TestPersistentJsonStorage, emptyListWhenNoReportsCreated)
53{
Wludzik, Jozefe2362792020-10-27 17:23:55 +010054 EXPECT_THAT(sut.list(), SizeIs(0u));
Krzysztof Grobelny73da6902020-09-24 13:42:04 +020055}
56
57TEST_F(TestPersistentJsonStorage, listSavedReports)
58{
59 sut.store(FilePath("report/domain-1/name-1/conf-1.json"),
60 nlohmann::json("data-1a"));
61 sut.store(FilePath("report/domain-1/name-2/conf-1.json"),
62 nlohmann::json("data-2a"));
63 sut.store(FilePath("report/domain-1/name-2/conf-2.json"),
64 nlohmann::json("data-2b"));
65 sut.store(FilePath("report/domain-2/name-1/conf-1.json"),
66 nlohmann::json("data-3a"));
67
Wludzik, Jozefe2362792020-10-27 17:23:55 +010068 EXPECT_THAT(
69 sut.list(),
70 UnorderedElementsAre(FilePath("report/domain-1/name-1/conf-1.json"),
71 FilePath("report/domain-1/name-2/conf-1.json"),
72 FilePath("report/domain-1/name-2/conf-2.json"),
73 FilePath("report/domain-2/name-1/conf-1.json")));
Krzysztof Grobelny73da6902020-09-24 13:42:04 +020074}
75
76TEST_F(TestPersistentJsonStorage, listSavedReportsWithoutRemovedOnes)
77{
78 sut.store(FilePath("report/domain-1/name-1/conf-1.json"),
79 nlohmann::json("data-1a"));
80 sut.store(FilePath("report/domain-1/name-2/conf-1.json"),
81 nlohmann::json("data-2a"));
82 sut.store(FilePath("report/domain-1/name-2/conf-2.json"),
83 nlohmann::json("data-2b"));
84 sut.store(FilePath("report/domain-2/name-1/conf-1.json"),
85 nlohmann::json("data-3a"));
86 sut.remove(FilePath("report/domain-1/name-1/conf-1.json"));
87 sut.remove(FilePath("report/domain-1/name-2/conf-2.json"));
88
Wludzik, Jozefe2362792020-10-27 17:23:55 +010089 EXPECT_THAT(
90 sut.list(),
91 UnorderedElementsAre(FilePath("report/domain-1/name-2/conf-1.json"),
92 FilePath("report/domain-2/name-1/conf-1.json")));
Krzysztof Grobelny73da6902020-09-24 13:42:04 +020093}
94
95TEST_F(TestPersistentJsonStorage, removesStoredJson)
96{
97 nlohmann::json data = nlohmann::json::object();
98 data["name"] = "kevin";
99 data["lastname"] = "mc calister";
100
101 sut.store(fileName, data);
102
103 ASSERT_THAT(sut.remove(fileName), Eq(true));
104 ASSERT_THAT(sut.load(fileName), Eq(std::nullopt));
105}
106
107TEST_F(TestPersistentJsonStorage, returnsFalseWhenDeletingNonExistingFile)
108{
109 ASSERT_THAT(sut.remove(fileName), Eq(false));
110}
111
112TEST_F(TestPersistentJsonStorage, returnsNulloptWhenFileDoesntExist)
113{
114 ASSERT_THAT(sut.load(fileName), Eq(std::nullopt));
115}
Krzysztof Grobelnya06626d2022-11-24 15:22:40 +0000116
Szymon Dompke4d1c2ce2023-01-23 17:28:14 +0100117struct TestFileSymlink
118{
119 static interfaces::JsonStorage::FilePath
120 setupSymlinks(const std::filesystem::path& originalFile,
121 const interfaces::JsonStorage::DirectoryPath& directory)
122 {
123 auto linkPath = std::filesystem::path(directory) /
124 "report/symlink.json";
125 std::filesystem::create_directories(std::filesystem::path(directory) /
126 "report");
127 std::filesystem::create_symlink(originalFile, linkPath);
128 return interfaces::JsonStorage::FilePath(linkPath);
129 }
130};
131
132struct TestDirectorySymlink
133{
134 static interfaces::JsonStorage::FilePath
135 setupSymlinks(const std::filesystem::path& originalFile,
136 const interfaces::JsonStorage::DirectoryPath& directory)
137 {
138 auto linkPath = std::filesystem::path(directory) / "reportLink";
139 std::filesystem::create_directories(std::filesystem::path(directory) /
140 "report");
141 std::filesystem::create_directory_symlink(originalFile.parent_path(),
142 linkPath);
143 return interfaces::JsonStorage::FilePath(linkPath /
144 originalFile.filename());
145 }
146};
147
148template <typename T>
Krzysztof Grobelnya06626d2022-11-24 15:22:40 +0000149class TestPersistentJsonStorageWithSymlink : public TestPersistentJsonStorage
150{
151 public:
152 TestPersistentJsonStorageWithSymlink()
153 {
154 std::ofstream file(dummyReportPath);
155 file << "{}";
156 file.close();
157
Szymon Dompke4d1c2ce2023-01-23 17:28:14 +0100158 linkPath = T::setupSymlinks(dummyReportPath, directory);
Krzysztof Grobelnya06626d2022-11-24 15:22:40 +0000159 }
160
161 static void SetUpTestSuite()
162 {
163 TestPersistentJsonStorage::SetUpTestSuite();
164 ASSERT_FALSE(std::filesystem::exists(dummyReportPath));
165 }
166
167 void TearDown() override
168 {
169 TestPersistentJsonStorage::TearDown();
170 if (std::filesystem::exists(dummyReportPath))
171 {
172 std::filesystem::remove(dummyReportPath);
173 }
174 }
175
176 static const std::filesystem::path dummyReportPath;
Szymon Dompke4d1c2ce2023-01-23 17:28:14 +0100177 interfaces::JsonStorage::FilePath linkPath;
Krzysztof Grobelnya06626d2022-11-24 15:22:40 +0000178};
179
Szymon Dompke4d1c2ce2023-01-23 17:28:14 +0100180template <typename T>
Krzysztof Grobelnya06626d2022-11-24 15:22:40 +0000181const std::filesystem::path
Szymon Dompke4d1c2ce2023-01-23 17:28:14 +0100182 TestPersistentJsonStorageWithSymlink<T>::dummyReportPath =
183 std::filesystem::temp_directory_path() / "report.json";
Krzysztof Grobelnya06626d2022-11-24 15:22:40 +0000184
Szymon Dompke4d1c2ce2023-01-23 17:28:14 +0100185using SymlinkTypes = Types<TestFileSymlink, TestDirectorySymlink>;
186TYPED_TEST_SUITE(TestPersistentJsonStorageWithSymlink, SymlinkTypes);
187
188TYPED_TEST(TestPersistentJsonStorageWithSymlink, symlinksAreNotListed)
Krzysztof Grobelnya06626d2022-11-24 15:22:40 +0000189{
Szymon Dompke4d1c2ce2023-01-23 17:28:14 +0100190 ASSERT_THAT(TestPersistentJsonStorage::sut.list(), UnorderedElementsAre());
Krzysztof Grobelnya06626d2022-11-24 15:22:40 +0000191}
192
Szymon Dompke4d1c2ce2023-01-23 17:28:14 +0100193TYPED_TEST(TestPersistentJsonStorageWithSymlink, throwsWhenStoreTargetIsSymlink)
Krzysztof Grobelnya06626d2022-11-24 15:22:40 +0000194{
Szymon Dompke4d1c2ce2023-01-23 17:28:14 +0100195 ASSERT_THROW(TestPersistentJsonStorage::sut.store(TestFixture::linkPath,
196 nlohmann::json("data")),
197 std::runtime_error);
Krzysztof Grobelnya06626d2022-11-24 15:22:40 +0000198
Szymon Dompke4d1c2ce2023-01-23 17:28:14 +0100199 ASSERT_THAT(TestPersistentJsonStorage::sut.list(), UnorderedElementsAre());
Krzysztof Grobelnya06626d2022-11-24 15:22:40 +0000200}
201
Szymon Dompke4d1c2ce2023-01-23 17:28:14 +0100202TYPED_TEST(TestPersistentJsonStorageWithSymlink,
203 returnsNulloptWhenFileIsSymlink)
Krzysztof Grobelnya06626d2022-11-24 15:22:40 +0000204{
Szymon Dompke4d1c2ce2023-01-23 17:28:14 +0100205 ASSERT_THAT(TestPersistentJsonStorage::sut.load(TestFixture::linkPath),
206 Eq(std::nullopt));
Krzysztof Grobelnya06626d2022-11-24 15:22:40 +0000207}
208
Szymon Dompke4d1c2ce2023-01-23 17:28:14 +0100209TYPED_TEST(TestPersistentJsonStorageWithSymlink,
210 returnsFalseWhenTryingToDeleteSymlink)
Krzysztof Grobelnya06626d2022-11-24 15:22:40 +0000211{
Szymon Dompke4d1c2ce2023-01-23 17:28:14 +0100212 EXPECT_THAT(TestPersistentJsonStorage::sut.remove(TestFixture::linkPath),
213 Eq(false));
214 EXPECT_TRUE(std::filesystem::exists(TestFixture::linkPath));
215 EXPECT_TRUE(std::filesystem::exists(TestFixture::dummyReportPath));
Krzysztof Grobelnya06626d2022-11-24 15:22:40 +0000216}