blob: 7ddc5b6b665fcc614a49e195fb3d2f9b2600a305 [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
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 =
Wludzik, Jozef596a9942021-01-27 12:28:59 +010036 interfaces::JsonStorage::DirectoryPath(
37 std::filesystem::temp_directory_path() / "telemetry-tests");
Krzysztof Grobelny73da6902020-09-24 13:42:04 +020038
39TEST_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
50TEST_F(TestPersistentJsonStorage, emptyListWhenNoReportsCreated)
51{
Wludzik, Jozefe2362792020-10-27 17:23:55 +010052 EXPECT_THAT(sut.list(), SizeIs(0u));
Krzysztof Grobelny73da6902020-09-24 13:42:04 +020053}
54
55TEST_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
Wludzik, Jozefe2362792020-10-27 17:23:55 +010066 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")));
Krzysztof Grobelny73da6902020-09-24 13:42:04 +020072}
73
74TEST_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
Wludzik, Jozefe2362792020-10-27 17:23:55 +010087 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")));
Krzysztof Grobelny73da6902020-09-24 13:42:04 +020091}
92
93TEST_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
105TEST_F(TestPersistentJsonStorage, returnsFalseWhenDeletingNonExistingFile)
106{
107 ASSERT_THAT(sut.remove(fileName), Eq(false));
108}
109
110TEST_F(TestPersistentJsonStorage, returnsNulloptWhenFileDoesntExist)
111{
112 ASSERT_THAT(sut.load(fileName), Eq(std::nullopt));
113}