/* * Copyright 2019 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include using alexaClientSDK::avsCommon::avs::AVSDirective; using alexaClientSDK::avsCommon::avs::AVSMessageHeader; using alexaClientSDK::avsCommon::avs::MessageRequest; using alexaClientSDK::avsCommon::avs::attachment::AttachmentManager; using alexaClientSDK::avsCommon::sdkInterfaces::MessageRequestObserverInterface; using alexaClientSDK::avsCommon::sdkInterfaces::storage::test::StubMiscStorage; using alexaClientSDK::avsCommon::sdkInterfaces::test::MockAVSConnectionManager; using alexaClientSDK::avsCommon::sdkInterfaces::test::MockDirectiveHandlerResult; using alexaClientSDK::avsCommon::sdkInterfaces::test::MockExceptionEncounteredSender; using alexaClientSDK::avsCommon::sdkInterfaces::test::MockMessageSender; using alexaClientSDK::registrationManager::CustomerDataManager; namespace alexaClientSDK { namespace capabilityAgents { namespace system { namespace test { /// Fake message ID used for testing. static const std::string TEST_MESSAGE_ID("c88f970a-3519-4ecb-bdcc-0488aca22b87"); /// Fake request ID used for testing. static const std::string TEST_REQUEST_ID("4b73575e-2e7d-425b-bfa4-c6615e0fbd43"); /// Fake context ID used for testing. static const std::string TEST_CONTEXT_ID("71c967d8-ad58-47b0-924d-b752deb75e4e"); class MockStateReportGenerator : public StateReportGenerator { public: MockStateReportGenerator(std::function reportFunction) : StateReportGenerator({reportFunction}) { } }; class ReportStateHandlerTest : public ::testing::Test { protected: void SetUp() override { m_customerDataManager = std::make_shared(); m_mockExceptionEncounteredSender = std::make_shared(); m_mockAVSConnectionManager = std::make_shared<::testing::NiceMock>(); m_mockMessageSender = std::make_shared(); m_stubMiscStorage = StubMiscStorage::create(); m_attachmentManager = std::make_shared(AttachmentManager::AttachmentType::IN_PROCESS); m_mockDirectiveHandlerResult = std::unique_ptr(new MockDirectiveHandlerResult); } void waitUntilEventSent() { std::unique_lock lock(m_mutex); // This will take 2 seconds only in case it fails (normally it exits right away). ASSERT_TRUE(m_condition.wait_for( lock, std::chrono::seconds(2), [this] { return m_directiveCompleted && m_eventSent; })); } std::shared_ptr m_unit; std::shared_ptr m_customerDataManager; std::shared_ptr m_mockExceptionEncounteredSender; std::shared_ptr m_mockMessageSender; std::shared_ptr<::testing::NiceMock> m_mockAVSConnectionManager; std::shared_ptr m_stubMiscStorage; std::vector generators; std::shared_ptr m_attachmentManager; std::unique_ptr m_mockDirectiveHandlerResult; std::mutex m_mutex; std::condition_variable m_condition; bool m_directiveCompleted = false; bool m_eventSent = false; std::string m_eventJson; std::shared_ptr createDirective() { auto avsMessageHeader = std::make_shared("System", "ReportState", TEST_MESSAGE_ID, TEST_REQUEST_ID); auto directive = AVSDirective::create("", avsMessageHeader, "", m_attachmentManager, TEST_CONTEXT_ID); return std::move(directive); } }; TEST_F(ReportStateHandlerTest, testReportState) { void* leak = new int[23]; leak = nullptr; ASSERT_EQ(nullptr, leak); MockStateReportGenerator mockGenerator([] { return R"({"unitTest":"ON","complaints":"OFF"})"; }); generators.push_back(mockGenerator); m_unit = ReportStateHandler::create( m_customerDataManager, m_mockExceptionEncounteredSender, m_mockAVSConnectionManager, m_mockMessageSender, m_stubMiscStorage, generators); EXPECT_CALL(*m_mockDirectiveHandlerResult, setCompleted()).WillOnce(::testing::InvokeWithoutArgs([this] { std::lock_guard lock(m_mutex); m_directiveCompleted = true; m_condition.notify_all(); })); EXPECT_CALL(*m_mockMessageSender, sendMessage(::testing::_)) .WillOnce(::testing::WithArg<0>(::testing::Invoke([this](std::shared_ptr request) { std::lock_guard lock(m_mutex); m_eventSent = true; m_eventJson = request->getJsonContent(); request->sendCompleted(MessageRequestObserverInterface::Status::SUCCESS); m_condition.notify_all(); }))); m_unit->CapabilityAgent::preHandleDirective(createDirective(), std::move(m_mockDirectiveHandlerResult)); m_unit->CapabilityAgent::handleDirective(TEST_MESSAGE_ID); waitUntilEventSent(); ASSERT_TRUE(m_eventJson.find(R"("unitTest":"ON")") != m_eventJson.npos); } } // namespace test } // namespace system } // namespace capabilityAgents } // namespace alexaClientSDK