blob: 26600c2e8a335856cafe4e36cb9155b01b2ac898 [file] [log] [blame]
Krzysztof Grobelny73da6902020-09-24 13:42:04 +02001#include "persistent_json_storage.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01002#include "printers.hpp"
Krzysztof Grobelny73da6902020-09-24 13:42:04 +02003
4#include "gmock/gmock.h"
5#include "gtest/gtest.h"
6
7using namespace testing;
8
9class 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
34const interfaces::JsonStorage::DirectoryPath
35 TestPersistentJsonStorage::directory =
36 interfaces::JsonStorage::DirectoryPath(std::tmpnam(nullptr));
37
38TEST_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
49TEST_F(TestPersistentJsonStorage, emptyListWhenNoReportsCreated)
50{
Wludzik, Jozefe2362792020-10-27 17:23:55 +010051 EXPECT_THAT(sut.list(), SizeIs(0u));
Krzysztof Grobelny73da6902020-09-24 13:42:04 +020052}
53
54TEST_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
Wludzik, Jozefe2362792020-10-27 17:23:55 +010065 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")));
Krzysztof Grobelny73da6902020-09-24 13:42:04 +020071}
72
73TEST_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
Wludzik, Jozefe2362792020-10-27 17:23:55 +010086 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")));
Krzysztof Grobelny73da6902020-09-24 13:42:04 +020090}
91
92TEST_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
104TEST_F(TestPersistentJsonStorage, returnsFalseWhenDeletingNonExistingFile)
105{
106 ASSERT_THAT(sut.remove(fileName), Eq(false));
107}
108
109TEST_F(TestPersistentJsonStorage, returnsNulloptWhenFileDoesntExist)
110{
111 ASSERT_THAT(sut.load(fileName), Eq(std::nullopt));
112}