blob: 8e67a2eae608603c1929c666b0c3075ee22d78d9 [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
117class TestPersistentJsonStorageWithSymlink : public TestPersistentJsonStorage
118{
119 public:
120 TestPersistentJsonStorageWithSymlink()
121 {
122 std::ofstream file(dummyReportPath);
123 file << "{}";
124 file.close();
125
126 std::filesystem::create_directories(std::filesystem::path(directory) /
127 "report");
128 std::filesystem::create_symlink(dummyReportPath,
129 std::filesystem::path(directory) /
130 "report/symlink.json");
131 }
132
133 static void SetUpTestSuite()
134 {
135 TestPersistentJsonStorage::SetUpTestSuite();
136 ASSERT_FALSE(std::filesystem::exists(dummyReportPath));
137 }
138
139 void TearDown() override
140 {
141 TestPersistentJsonStorage::TearDown();
142 if (std::filesystem::exists(dummyReportPath))
143 {
144 std::filesystem::remove(dummyReportPath);
145 }
146 }
147
148 static const std::filesystem::path dummyReportPath;
149};
150
151const std::filesystem::path
152 TestPersistentJsonStorageWithSymlink::dummyReportPath =
153 std::filesystem::temp_directory_path() / "report";
154
155TEST_F(TestPersistentJsonStorageWithSymlink, symlinksAreNotListed)
156{
157 ASSERT_THAT(sut.list(), UnorderedElementsAre());
158}
159
160TEST_F(TestPersistentJsonStorageWithSymlink, throwsWhenStoreTargetIsSymlink)
161{
162 ASSERT_THROW(
163 sut.store(FilePath("report/symlink.json"), nlohmann::json("data")),
164 std::runtime_error);
165
166 ASSERT_THAT(sut.list(), UnorderedElementsAre());
167}
168
169TEST_F(TestPersistentJsonStorageWithSymlink, returnsNulloptWhenFileIsSymlink)
170{
171 ASSERT_THAT(sut.load(FilePath("report/symlink.json")), Eq(std::nullopt));
172}
173
174TEST_F(TestPersistentJsonStorageWithSymlink,
175 returnsFalseWhenTryingToDeleteSymlink)
176{
177 EXPECT_THAT(sut.remove(FilePath("report/symlink.json")), Eq(false));
178 EXPECT_TRUE(std::filesystem::exists(std::filesystem::path(directory) /
179 "report/symlink.json"));
180 EXPECT_TRUE(std::filesystem::exists(dummyReportPath));
181}