/* * DirectiveProcessorTest.cpp * * Copyright 2017 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. */ // @file DirectiveProcessorTest.cpp #include #include #include #include #include #include #include #include "ADSL/DirectiveProcessor.h" #include "MockAttachmentManager.h" #include "MockDirectiveHandler.h" #include "MockDirectiveHandlerResult.h" using namespace ::testing; namespace alexaClientSDK { namespace adsl { namespace test { using namespace avsCommon; /// Generic messageId used for tests. static const std::string MESSAGE_ID_0_0("Message_0_0"); /// Generic MessageId used for test. static const std::string MESSAGE_ID_0_1("Message_0_1"); /// Generic MessageId used for tests. static const std::string MESSAGE_ID_1_0("Message_1_0"); /// Generic DialogRequestId used for tests. static const std::string DIALOG_REQUEST_ID_0("DialogRequestId_0"); /// Generic DialogRequestId used for tests. static const std::string DIALOG_REQUEST_ID_1("DialogRequestId_1"); /// An unparsed directive for test. static const std::string UNPARSED_DIRECTIVE("unparsedDirectiveForTest"); /// A paylaod for test; static const std::string PAYLOAD_TEST("payloadForTest"); /// A generic namespace string for tests. static const std::string NAMESPACE_0("namespace_0"); /// A generic namespace string for tests. static const std::string NAMESPACE_1("namespace_1"); /// A generic name string for tests. static const std::string NAME_0("name_0"); /// A generic name string for tests. static const std::string NAME_1("name_1"); /// Namespace and name combination for tests. #define NAMESPACE_AND_NAME_0_0 NAMESPACE_0, NAME_0 /// Namespace and name combination (changing name this time) for tests. #define NAMESPACE_AND_NAME_0_1 NAMESPACE_0, NAME_1 /// Namespace and name combination (changing namespace this time) for tests. #define NAMESPACE_AND_NAME_1_0 NAMESPACE_1, NAME_0 /** * DirectiveRouterTest */ class DirectiveProcessorTest : public ::testing::Test { public: void SetUp() override; /// A DirectiveRouter instance to test with. std::shared_ptr m_router; /// A DirectiveProcessor instance to test with. std::shared_ptr m_processor; /// Mock AttachmentManager with which to create directives. std::shared_ptr m_attachmentManager; /// Generic mock @c DirectiveHandler for tests. std::shared_ptr m_handler0; /// Generic mock @c DirectiveHandler for tests. std::shared_ptr m_handler1; /// Generic mock @c DirectiveHandler for tests. std::shared_ptr m_handler2; /// Generic @c AVSDirective for tests. std::shared_ptr m_directive_0_0; /// Generic @c AVSDirective for tests. std::shared_ptr m_directive_0_1; /// Generic @c AVSDirective for tests. std::shared_ptr m_directive_1_0; }; void DirectiveProcessorTest::SetUp() { m_router = std::make_shared(); m_processor = std::make_shared(m_router.get()); m_attachmentManager = std::make_shared>(); m_handler0 = MockDirectiveHandler::create(); m_handler1 = MockDirectiveHandler::create(); m_handler2 = MockDirectiveHandler::create(); auto avsMessageHeader_0_0 = std::make_shared( NAMESPACE_AND_NAME_0_0, MESSAGE_ID_0_0, DIALOG_REQUEST_ID_0); m_directive_0_0 = AVSDirective::create( UNPARSED_DIRECTIVE, avsMessageHeader_0_0, PAYLOAD_TEST, m_attachmentManager); auto avsMessageHeader_0_1 = std::make_shared( NAMESPACE_AND_NAME_0_1, MESSAGE_ID_0_1, DIALOG_REQUEST_ID_0); m_directive_0_1 = AVSDirective::create( UNPARSED_DIRECTIVE, avsMessageHeader_0_1, PAYLOAD_TEST, m_attachmentManager); auto avsMessageHeader_1_0 = std::make_shared( NAMESPACE_AND_NAME_1_0, MESSAGE_ID_1_0, DIALOG_REQUEST_ID_1); m_directive_1_0 = AVSDirective::create( UNPARSED_DIRECTIVE, avsMessageHeader_1_0, PAYLOAD_TEST, m_attachmentManager); } /** * Send a nullptr @c AVSDirective. Expect that it is ignored and a failure status (false) is returned. */ TEST_F(DirectiveProcessorTest, testNullptrDirective) { ASSERT_FALSE(m_processor->onDirective(nullptr)); } /** * Register a @c DirectiveHandler and set the @c dialogRequestId. Send a @c AVSDirective that the handler * registered for, but with a @c dialogRequestId that does not match. Expect that @c onDirective() * returns true (because the handler was registered) but that none of the handler methods are called * (because directives with the wrong @c dialogRequestID are dropped). */ TEST_F(DirectiveProcessorTest, testWrongDialogRequestId) { ASSERT_TRUE(m_router->addDirectiveHandlers({ {{NAMESPACE_AND_NAME_0_0}, {m_handler0, BlockingPolicy::NON_BLOCKING}} })); EXPECT_CALL(*(m_handler0.get()), handleDirectiveImmediately(_)).Times(0); EXPECT_CALL(*(m_handler0.get()), preHandleDirective(m_directive_0_0, _)).Times(0); EXPECT_CALL(*(m_handler0.get()), handleDirective(MESSAGE_ID_0_0)).Times(0); EXPECT_CALL(*(m_handler0.get()), cancelDirective(_)).Times(0); m_processor->setDialogRequestId(DIALOG_REQUEST_ID_1); ASSERT_TRUE(m_processor->onDirective(m_directive_0_0)); } /** * Register a @c NON_BLOCKING @c DirectiveHandler. Send an @c AVSDirective that matches the registered handler. * Expect that @c preHandleDirective() and @c handleDirective() are called. */ TEST_F(DirectiveProcessorTest, testSendNonBlocking) { ASSERT_TRUE(m_router->addDirectiveHandlers({ {{NAMESPACE_AND_NAME_0_0}, {m_handler0, BlockingPolicy::NON_BLOCKING}} })); EXPECT_CALL(*(m_handler0.get()), handleDirectiveImmediately(_)).Times(0); EXPECT_CALL(*(m_handler0.get()), preHandleDirective(m_directive_0_0, _)).Times(1); EXPECT_CALL(*(m_handler0.get()), handleDirective(MESSAGE_ID_0_0)).Times(1); EXPECT_CALL(*(m_handler0.get()), cancelDirective(_)).Times(0); m_processor->setDialogRequestId(DIALOG_REQUEST_ID_0); ASSERT_TRUE(m_processor->onDirective(m_directive_0_0)); ASSERT_TRUE(m_handler0->waitUntilCompleted()); } /** * Test sending a blocking and then a non-blocking directive. Expect that @c preHandleDirective() and * @c handleDirective() is called for each. */ TEST_F(DirectiveProcessorTest, testSendBlockingThenNonBlocking) { ASSERT_TRUE(m_router->addDirectiveHandlers({ {{NAMESPACE_AND_NAME_0_0}, {m_handler0, BlockingPolicy::BLOCKING}}, {{NAMESPACE_AND_NAME_0_1}, {m_handler1, BlockingPolicy::NON_BLOCKING}} })); EXPECT_CALL(*(m_handler0.get()), handleDirectiveImmediately(_)).Times(0); EXPECT_CALL(*(m_handler0.get()), preHandleDirective(m_directive_0_0, _)).Times(1); EXPECT_CALL(*(m_handler0.get()), handleDirective(MESSAGE_ID_0_0)).Times(1); EXPECT_CALL(*(m_handler0.get()), cancelDirective(_)).Times(0); EXPECT_CALL(*(m_handler1.get()), handleDirectiveImmediately(_)).Times(0); EXPECT_CALL(*(m_handler1.get()), preHandleDirective(m_directive_0_1, _)).Times(1); EXPECT_CALL(*(m_handler1.get()), handleDirective(MESSAGE_ID_0_1)).Times(1); EXPECT_CALL(*(m_handler1.get()), cancelDirective(_)).Times(0); m_processor->setDialogRequestId(DIALOG_REQUEST_ID_0); ASSERT_TRUE(m_processor->onDirective(m_directive_0_0)); ASSERT_TRUE(m_processor->onDirective(m_directive_0_1)); ASSERT_TRUE(m_handler1->waitUntilCompleted()); } /** * Register a handler for two @c AVSDirectives. Send an @c AVSDirective for which no handler is registered. * Then send an @c AVSDirective with the same @c dialogRequestId. Expect that the first @c AVSDirective will * be dropped (and @c onDirective() will return false) because there is no handler. Expect the second directive * to be handled, including @c preHandleDirective() and @c handleDirective() calls. After @c handleDirective() * has been called, set the @c dialogRequestId and send an @c AVSDirective for which the other handler was * registered. Expect that the last directive is handled as well. */ TEST_F(DirectiveProcessorTest, testOnUnregisteredDirective) { ASSERT_TRUE(m_router->addDirectiveHandlers({ {{NAMESPACE_AND_NAME_0_1}, {m_handler1, BlockingPolicy::NON_BLOCKING}}, {{NAMESPACE_AND_NAME_1_0}, {m_handler2, BlockingPolicy::NON_BLOCKING}} })); EXPECT_CALL(*(m_handler1.get()), handleDirectiveImmediately(_)).Times(0); EXPECT_CALL(*(m_handler1.get()), preHandleDirective(m_directive_0_1, _)).Times(1); EXPECT_CALL(*(m_handler1.get()), handleDirective(MESSAGE_ID_0_1)).Times(1); EXPECT_CALL(*(m_handler1.get()), cancelDirective(_)).Times(0); EXPECT_CALL(*(m_handler2.get()), handleDirectiveImmediately(_)).Times(0); EXPECT_CALL(*(m_handler2.get()), preHandleDirective(m_directive_1_0, _)).Times(1); EXPECT_CALL(*(m_handler2.get()), handleDirective(MESSAGE_ID_1_0)).Times(1); EXPECT_CALL(*(m_handler2.get()), cancelDirective(_)).Times(0); m_processor->setDialogRequestId(DIALOG_REQUEST_ID_0); ASSERT_FALSE(m_processor->onDirective(m_directive_0_0)); ASSERT_TRUE(m_processor->onDirective(m_directive_0_1)); ASSERT_TRUE(m_handler1->waitUntilCompleted()); m_processor->setDialogRequestId(DIALOG_REQUEST_ID_1); ASSERT_TRUE(m_processor->onDirective(m_directive_1_0)); ASSERT_TRUE(m_handler2->waitUntilCompleted()); } /** * Register a long running and @c BLOCKING @c DirectiveHandler for one @c AVSDirective and a short * running @c DirectiveHandler for two more @c AVSDirectives. Send the long running @c AVSDirective * followed by a short running one. Wait until the long running @c AVSDirective has entered * handleDirective() and the short running @c AVSDirective has entered preHandleDirective(). * then change the @c dialogRequestId() and send a final @c AVSDirective with the new * @c dialogRequestId(). Expect the first two @c AVSDirectives to be cancelled and expect the * final @c AVSDirective to be processed normally. */ TEST_F(DirectiveProcessorTest, testSetDialogRequestIdCancelsOutstandingDirectives) { auto longRunningHandler = MockDirectiveHandler::create(MockDirectiveHandler::DEFAULT_DONE_TIMEOUT_MS); ASSERT_TRUE(m_router->addDirectiveHandlers({ {{NAMESPACE_AND_NAME_0_0}, {longRunningHandler, BlockingPolicy::BLOCKING}}, {{NAMESPACE_AND_NAME_0_1}, {m_handler1, BlockingPolicy::NON_BLOCKING}}, {{NAMESPACE_AND_NAME_1_0}, {m_handler2, BlockingPolicy::NON_BLOCKING}} })); EXPECT_CALL(*(longRunningHandler.get()), handleDirectiveImmediately(_)).Times(0); EXPECT_CALL(*(longRunningHandler.get()), preHandleDirective(_, _)).Times(1); EXPECT_CALL(*(longRunningHandler.get()), handleDirective(_)).Times(1); EXPECT_CALL(*(longRunningHandler.get()), cancelDirective(_)).Times(1); EXPECT_CALL(*(m_handler1.get()), handleDirectiveImmediately(_)).Times(0); EXPECT_CALL(*(m_handler1.get()), preHandleDirective(m_directive_0_1, _)).Times(1); EXPECT_CALL(*(m_handler1.get()), handleDirective(MESSAGE_ID_0_1)).Times(0); EXPECT_CALL(*(m_handler1.get()), cancelDirective(_)).Times(1); EXPECT_CALL(*(m_handler2.get()), handleDirectiveImmediately(_)).Times(0); EXPECT_CALL(*(m_handler2.get()), preHandleDirective(m_directive_1_0, _)).Times(1); EXPECT_CALL(*(m_handler2.get()), handleDirective(MESSAGE_ID_1_0)).Times(1); EXPECT_CALL(*(m_handler2.get()), cancelDirective(_)).Times(0); m_processor->setDialogRequestId(DIALOG_REQUEST_ID_0); ASSERT_TRUE(m_processor->onDirective(m_directive_0_0)); ASSERT_TRUE(m_processor->onDirective(m_directive_0_1)); ASSERT_TRUE(longRunningHandler->waitUntilHandling()); ASSERT_TRUE(m_handler1->waitUntilPreHandling()); m_processor->setDialogRequestId(DIALOG_REQUEST_ID_1); ASSERT_TRUE(m_processor->onDirective(m_directive_1_0)); ASSERT_TRUE(m_handler2->waitUntilCompleted()); } } // namespace test } // namespace adsl } // namespace alexaClientSDK