-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathDiskLocalStorageTests.cpp
106 lines (85 loc) · 3.74 KB
/
DiskLocalStorageTests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#if 0
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
#include "common/Common.hpp"
//#include "controlplane/DiskLocalStorage.hpp"
#include "common/MockITenantDataSerializer.hpp"
using namespace testing;
using namespace MAT;
//using namespace MAT::ControlPlane;
// Class to allow unit tests to test DiskLocalStorage without needing to use the file system
class TestableDiskLocalStorage : public DiskLocalStorage
{
std::istream* m_streamToReturn;
std::istream * OpenStream(const std::string& pathToFile) const override
{
UNREFERENCED_PARAMETER(pathToFile);
return m_streamToReturn;
}
public:
TestableDiskLocalStorage(const std::string& pathToCache, std::unique_ptr<ITenantDataSerializer> &serializer,
std::istream* streamToReturn)
: DiskLocalStorage(pathToCache, serializer), m_streamToReturn(streamToReturn)
{
}
};
TEST(DiskLocalStorageTests, Ctor_Succeeds)
{
MockITenantDataSerializer* serializerMock = new StrictMock<MockITenantDataSerializer>();
std::unique_ptr<ITenantDataSerializer> serializer(serializerMock);
DiskLocalStorage reader("", serializer);
}
TEST(DiskLocalStorageTests, ReadTenantData_OpenStreamFails_ReturnsNullptr)
{
LPCSTR path = "My Path";
GUID_t ariaTenantId;
MockITenantDataSerializer* serializerMock = new StrictMock<MockITenantDataSerializer>();
std::unique_ptr<ITenantDataSerializer> serializer(serializerMock);
TestableDiskLocalStorage reader(path, serializer, nullptr);
ASSERT_EQ(nullptr, reader.ReadTenantData(ariaTenantId));
}
TEST(DiskLocalStorageTests, ReadTenantData_OpenStreamReturnsEmptyStream_ReturnsNullptr)
{
LPCSTR path = "My Path";
GUID_t ariaTenantId;
MockITenantDataSerializer* serializerMock = new StrictMock<MockITenantDataSerializer>();
std::unique_ptr<ITenantDataSerializer> serializer(serializerMock);
std::string expectedInput = "";
std::istream * testStream = new std::istringstream(expectedInput);
TestableDiskLocalStorage reader(path, serializer, testStream);
ASSERT_EQ(nullptr, reader.ReadTenantData(ariaTenantId));
}
TEST(DiskLocalStorageTests, ReadTenantData_OpenStreamReturnsNonEmptyStream_SerializerReturnsNullPtr_ReturnsNullptr)
{
LPCSTR path = "My Path";
GUID_t ariaTenantId;
MockITenantDataSerializer* serializerMock = new StrictMock<MockITenantDataSerializer>();
std::unique_ptr<ITenantDataSerializer> serializer(serializerMock);
std::string expectedInput = "This should return nullptr";
std::istream * testStream = new std::istringstream(expectedInput);
std::string actualInput;
EXPECT_CALL(*serializerMock, DeserializeTenantData(_))
.WillOnce(DoAll(SaveArg<0>(&actualInput),Return(nullptr)));
TestableDiskLocalStorage reader(path, serializer, testStream);
ASSERT_EQ(nullptr, reader.ReadTenantData(ariaTenantId));
ASSERT_EQ(expectedInput, actualInput);
}
TEST(DiskLocalStorageTests, ReadTenantData_OpenStreamReturnsNonEmptyStream_SerializerReturnsTenantDataPtr_ReturnsTenantDataPtr)
{
LPCSTR path = "My Path";
GUID_t ariaTenantId;
MockITenantDataSerializer* serializerMock = new StrictMock<MockITenantDataSerializer>();
std::unique_ptr<ITenantDataSerializer> serializer(serializerMock);
std::string expectedInput = "This should return TenantDataPtr";
std::istream * testStream = new std::istringstream(expectedInput);
std::string actualInput;
TenantData tenantData;
EXPECT_CALL(*serializerMock, DeserializeTenantData(_))
.WillOnce(DoAll(SaveArg<0>(&actualInput), Return(&tenantData)));
TestableDiskLocalStorage reader(path, serializer, testStream);
ASSERT_EQ(&tenantData, reader.ReadTenantData(ariaTenantId));
ASSERT_EQ(expectedInput, actualInput);
}
#endif