-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathLogSessionDataFuncTests.cpp
120 lines (100 loc) · 3.98 KB
/
LogSessionDataFuncTests.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// 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";
class LogSessionDataFuncTests : public ::testing::Test
{
void CleanupLocalSessionFile()
{
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);
}
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_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);
}