Repository navigation
Expand file tree
/
Copy pathLogSessionDataFuncTests.cpp
More file actions
150 lines (128 loc) · 5.34 KB
/
Copy pathLogSessionDataFuncTests.cpp
File metadata and controls
150 lines (128 loc) · 5.34 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
#include "common/Common.hpp"
#include <functional>
#include <LogSessionData.hpp>
#include "offline/LogSessionDataProvider.hpp"
#include "utils/FileUtils.hpp"
#include "utils/StringUtils.hpp"
using namespace testing;
using namespace MAT;
const std::string SessionFileArgument = "test";
const char* const SessionFile = "test.ses";
const char* const MemorySessionFile = ":memory:.ses";
class LogSessionDataFuncTests : public ::testing::Test
{
void CleanupLocalSessionFile()
{
for (const auto* sessionFile : {SessionFile, MemorySessionFile})
{
if (MAT::FileExists(sessionFile))
{
MAT::FileDelete(sessionFile);
}
}
}
virtual void SetUp() override
{
CleanupLocalSessionFile();
}
virtual void TearDown() override
{
CleanupLocalSessionFile();
}
};
void ConstructSesFile(const char* sessionFile, const std::string& contents)
{
ASSERT_TRUE(MAT::FileWrite(sessionFile, contents.c_str()));
}
void ConstructSesFile(const char* sessionFile, const std::string& utcTimeMs, const std::string& skuID)
{
std::ostringstream stream;
stream << utcTimeMs << '\n' << skuID << '\n';
ConstructSesFile(sessionFile, stream.str());
}
std::string RemoveWhitespace(std::string& str)
{
std::string trimmedString{ str };
if (!trimmedString.empty() && trimmedString[trimmedString.length() - 1] == '\n')
{
trimmedString.erase(trimmedString.length() - 1);
}
if (!trimmedString.empty() && trimmedString[trimmedString.length() - 1] == '\r')
{
trimmedString.erase(trimmedString.length() - 1);
}
return trimmedString;
}
std::pair<unsigned long long, std::string> ReadPropertiesFromSessionFile(const char* sessonFile)
{
auto contents = MAT::FileGetContents(sessonFile);
std::vector<std::string> splitContents;
StringUtils::SplitString(contents, '\n', splitContents);
auto sessionFirstTime = RemoveWhitespace(splitContents[0]);
auto skuID = RemoveWhitespace(splitContents[1]);
return std::make_pair(std::stoull(sessionFirstTime), skuID);
}
TEST_F(LogSessionDataFuncTests, Constructor_SessionFile_FileCreated)
{
auto logSessionDataProvider = LogSessionDataProvider(SessionFileArgument);
logSessionDataProvider.CreateLogSessionData();
ASSERT_TRUE(MAT::FileExists(SessionFile));
}
TEST_F(LogSessionDataFuncTests, Constructor_InMemoryCache_NoSessionFileCreated)
{
auto logSessionDataProvider = LogSessionDataProvider(":memory:");
logSessionDataProvider.CreateLogSessionData();
const auto* logSessionData = logSessionDataProvider.GetLogSessionData();
ASSERT_NE(logSessionData, nullptr);
EXPECT_GT(logSessionData->getSessionFirstTime(), 0ull);
EXPECT_FALSE(logSessionData->getSessionSDKUid().empty());
const auto sessionSDKUid = logSessionData->getSessionSDKUid();
EXPECT_FALSE(MAT::FileExists(MemorySessionFile));
logSessionDataProvider.ResetLogSessionData();
logSessionData = logSessionDataProvider.GetLogSessionData();
ASSERT_NE(logSessionData, nullptr);
EXPECT_GT(logSessionData->getSessionFirstTime(), 0ull);
EXPECT_FALSE(logSessionData->getSessionSDKUid().empty());
EXPECT_NE(logSessionData->getSessionSDKUid(), sessionSDKUid);
EXPECT_FALSE(MAT::FileExists(MemorySessionFile));
logSessionDataProvider.DeleteLogSessionData();
EXPECT_FALSE(MAT::FileExists(MemorySessionFile));
}
TEST_F(LogSessionDataFuncTests, Constructor_ValidSessionFileExists_MembersSetToExistingFile)
{
const std::string validSessionFirstTime{ "123456" };
const std::string validSkuId{ "abc123" };
ConstructSesFile(SessionFile, validSessionFirstTime, validSkuId);
auto logSessionDataProvider = LogSessionDataProvider(SessionFileArgument);
logSessionDataProvider.CreateLogSessionData();
const auto* logSessionData = logSessionDataProvider.GetLogSessionData();
ASSERT_EQ(logSessionData->getSessionFirstTime(), 123456ull);
ASSERT_EQ(logSessionData->getSessionSDKUid(), validSkuId);
}
TEST_F(LogSessionDataFuncTests, Constructor_InvalidSessionFileExists_MembersRegenerated)
{
const std::string invalidSessionFirstTime{ "not-a-number" };
const std::string validSkuId{ "abc123" };
ConstructSesFile(SessionFile, invalidSessionFirstTime, validSkuId);
auto logSessionDataProvider = LogSessionDataProvider(SessionFileArgument);
logSessionDataProvider.CreateLogSessionData();
const auto* logSessionData = logSessionDataProvider.GetLogSessionData();
ASSERT_NE(logSessionData->getSessionFirstTime(), 123456ull);
ASSERT_NE(logSessionData->getSessionSDKUid(), validSkuId);
}
TEST_F(LogSessionDataFuncTests, Constructor_InvalidSessionFileExists_NewFileWritten)
{
const std::string invalidSessionFirstTime{ "not-a-number" };
const std::string validSkuId{ "abc123" };
ConstructSesFile(SessionFile, invalidSessionFirstTime, validSkuId);
auto logSessionDataProvider = LogSessionDataProvider(SessionFileArgument);
logSessionDataProvider.CreateLogSessionData();
const auto* logSessionData = logSessionDataProvider.GetLogSessionData();
auto properties = ReadPropertiesFromSessionFile(SessionFile);
ASSERT_EQ(logSessionData->getSessionFirstTime(), properties.first);
ASSERT_EQ(logSessionData->getSessionSDKUid(), properties.second);
}