-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
March 2024 Release of the APL 2024.1 compliant APL Client Library
For more details on this release refer to CHANGELOG.md To learn about APL see: https://developer.amazon.com/docs/alexa-presentation-language/understand-apl.html
- Loading branch information
1 parent
cb20599
commit 8985868
Showing
18 changed files
with
510 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
#ifndef APL_CLIENT_LIBRARY_APL_SESSION_H_ | ||
#define APL_CLIENT_LIBRARY_APL_SESSION_H_ | ||
|
||
#pragma GCC diagnostic push | ||
#pragma GCC diagnostic ignored "-Wreorder" | ||
#pragma push_macro("DEBUG") | ||
#undef DEBUG | ||
#include <apl/utils/session.h> | ||
#pragma pop_macro("DEBUG") | ||
#pragma GCC diagnostic pop | ||
|
||
namespace APLClient { | ||
|
||
class AplSession : public apl::Session { | ||
public: | ||
AplSession(bool isLogCommandEnabled); | ||
void write(const char *filename, const char *func, const char *value) override; | ||
void write(apl::LogCommandMessage&& message) override; | ||
|
||
private: | ||
const apl::SessionPtr m_defaultSession = apl::makeDefaultSession(); | ||
const bool m_isLogCommandEnabled; | ||
std::string getLogInfoFrom(const apl::LogCommandMessage& message); | ||
}; | ||
|
||
} // namespace APLClient | ||
|
||
#endif // APL_CLIENT_LIBRARY_APL_SESSION_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
#include <rapidjson/document.h> | ||
#include <rapidjson/writer.h> | ||
#include <rapidjson/stringbuffer.h> | ||
|
||
#include "APLClient/AplSession.h" | ||
|
||
namespace APLClient { | ||
|
||
AplSession::AplSession(bool isLogCommandEnabled) | ||
: apl::Session(), | ||
m_isLogCommandEnabled(isLogCommandEnabled) { | ||
} | ||
|
||
void AplSession::write(const char *filename, const char *func, const char *value) { | ||
m_defaultSession->write(filename, func, value); | ||
} | ||
|
||
void AplSession::write(apl::LogCommandMessage&& message) { | ||
if (m_isLogCommandEnabled) { | ||
std::string info = getLogInfoFrom(message); | ||
|
||
apl::LoggerFactory::instance().getLogger(message.level, "Log", "Command") | ||
.session(*this) | ||
.log("%s %s", message.text.c_str(), info.c_str()); | ||
} | ||
} | ||
|
||
std::string AplSession::getLogInfoFrom(const apl::LogCommandMessage& message) { | ||
rapidjson::StringBuffer buffer; | ||
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); | ||
rapidjson::Document serializer; | ||
|
||
writer.StartObject(); | ||
|
||
// Add arguments array if it's not empty | ||
if (!message.arguments.empty()) { | ||
writer.Key("arguments"); | ||
rapidjson::Value serializedArguments = message.arguments.serialize(serializer.GetAllocator()); | ||
serializedArguments.Accept(writer); // Serialize into the writer directly | ||
} | ||
|
||
// Add origin object if it's not empty | ||
if (!message.origin.empty()) { | ||
writer.Key("origin"); | ||
rapidjson::Value serializedOrigin = message.origin.serialize(serializer.GetAllocator()); | ||
serializedOrigin.Accept(writer); | ||
} | ||
|
||
writer.EndObject(); | ||
|
||
return buffer.GetString(); | ||
} | ||
|
||
} // namespace APLClient |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
#include "APLClient/AplSession.h" | ||
|
||
#include <gtest/gtest.h> | ||
#include <fstream> | ||
|
||
namespace APLClient { | ||
namespace test { | ||
|
||
using namespace ::testing; | ||
|
||
static const std::string TEXT = "test text"; | ||
static const std::string ARGUMENTS = "test arguments"; | ||
static const std::string ORIGIN = "test origin"; | ||
|
||
class AplSessionTest : public ::testing::Test { | ||
public: | ||
void SetUp() override; | ||
std::string CaptureLog(apl::LogCommandMessage&& message); | ||
bool FindText(std::string text, std::string logContents); | ||
|
||
protected: | ||
std::shared_ptr<AplSession> m_aplSession; | ||
}; | ||
|
||
void AplSessionTest::SetUp() { | ||
m_aplSession = std::make_shared<AplSession>(true); | ||
} | ||
|
||
std::string AplSessionTest::CaptureLog(apl::LogCommandMessage&& message) { | ||
// Redirect stdout to a file | ||
std::string logFile = "CapturedLogCommand.txt"; | ||
freopen(logFile.c_str(),"w", stdout); | ||
|
||
// Write the log message | ||
m_aplSession->write(std::move(message)); | ||
|
||
// Redirect stdout back to terminal | ||
freopen("/dev/tty","w", stdout); | ||
|
||
// Read log file | ||
std::ifstream in(logFile); | ||
std::string logContents((std::istreambuf_iterator<char>(in)), | ||
std::istreambuf_iterator<char>()); | ||
|
||
// Close and delete the temp log file | ||
in.close(); | ||
std::remove(logFile.c_str()); | ||
|
||
return logContents; | ||
} | ||
|
||
bool AplSessionTest::FindText(std::string text, std::string logContents) { | ||
return logContents.find(text) != std::string::npos; | ||
} | ||
|
||
TEST_F(AplSessionTest, WriteLogCommandMessage) { | ||
apl::LogCommandMessage message { | ||
TEXT, | ||
apl::LogLevel::kInfo, | ||
apl::Object(ARGUMENTS), | ||
apl::Object(ORIGIN) | ||
}; | ||
|
||
std::string logContents = CaptureLog(std::move(message)); | ||
|
||
EXPECT_TRUE(FindText(TEXT, logContents)); | ||
EXPECT_TRUE(FindText(ARGUMENTS, logContents)); | ||
EXPECT_TRUE(FindText(ARGUMENTS, logContents)); | ||
} | ||
|
||
TEST_F(AplSessionTest, WriteLogCommandMessageWithoutArguments) { | ||
apl::LogCommandMessage message { | ||
TEXT, | ||
apl::LogLevel::kInfo, | ||
NULL, | ||
apl::Object(ORIGIN) | ||
}; | ||
|
||
std::string logContents = CaptureLog(std::move(message)); | ||
|
||
EXPECT_TRUE(FindText(TEXT, logContents)); | ||
EXPECT_FALSE(FindText(ARGUMENTS, logContents)); | ||
EXPECT_TRUE(FindText(ORIGIN, logContents)); | ||
} | ||
|
||
TEST_F(AplSessionTest, WriteLogCommandMessageWithoutOrigin) { | ||
apl::LogCommandMessage message { | ||
TEXT, | ||
apl::LogLevel::kInfo, | ||
apl::Object(ARGUMENTS), | ||
NULL | ||
}; | ||
|
||
std::string logContents = CaptureLog(std::move(message)); | ||
|
||
EXPECT_TRUE(FindText(TEXT, logContents)); | ||
EXPECT_TRUE(FindText(ARGUMENTS, logContents)); | ||
EXPECT_FALSE(FindText(ORIGIN, logContents)); | ||
} | ||
|
||
TEST_F(AplSessionTest, WriteLogCommandMessageWhenDisabled) { | ||
m_aplSession = std::make_shared<AplSession>(false); | ||
|
||
apl::LogCommandMessage message { | ||
TEXT, | ||
apl::LogLevel::kInfo, | ||
apl::Object(ARGUMENTS), | ||
apl::Object(ORIGIN) | ||
}; | ||
|
||
std::string logContents = CaptureLog(std::move(message)); | ||
|
||
EXPECT_FALSE(FindText(TEXT, logContents)); | ||
EXPECT_FALSE(FindText(ARGUMENTS, logContents)); | ||
EXPECT_FALSE(FindText(ORIGIN, logContents)); | ||
} | ||
|
||
TEST_F(AplSessionTest, WriteLogWithFilenameFuncAndValue) { | ||
std::string logFile = "CapturedLogCommand.txt"; | ||
freopen(logFile.c_str(),"w", stdout); | ||
|
||
m_aplSession->write("filename", "function", "test value"); | ||
|
||
freopen("/dev/tty","w", stdout); | ||
|
||
std::ifstream in(logFile); | ||
std::string logContents((std::istreambuf_iterator<char>(in)), | ||
std::istreambuf_iterator<char>()); | ||
|
||
in.close(); | ||
std::remove(logFile.c_str()); | ||
|
||
EXPECT_TRUE(FindText("test value", logContents)); | ||
} | ||
|
||
} // namespace test | ||
} // namespace APLClient |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.