2017-06-23 23:26:34 +00:00
|
|
|
/*
|
2017-02-10 23:39:10 +00:00
|
|
|
* MessageRouterTest.h
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#ifndef ALEXA_CLIENT_SDK_ACL_TEST_TRANSPORT_ABSTRACT_MESSAGE_ROUTER_TEST_H_
|
|
|
|
#define ALEXA_CLIENT_SDK_ACL_TEST_TRANSPORT_ABSTRACT_MESSAGE_ROUTER_TEST_H_
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
2017-03-10 00:01:46 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2017-02-10 23:39:10 +00:00
|
|
|
|
2017-06-23 23:26:34 +00:00
|
|
|
#include "AVSCommon/Utils/Threading/Executor.h"
|
|
|
|
#include "AVSCommon/Utils/Memory/Memory.h"
|
2017-02-10 23:39:10 +00:00
|
|
|
|
|
|
|
#include "MockMessageRouterObserver.h"
|
|
|
|
#include "MockAuthDelegate.h"
|
|
|
|
#include "MockTransport.h"
|
|
|
|
#include "ACL/Transport/MessageRouter.h"
|
2017-03-10 00:01:46 +00:00
|
|
|
#include "ACL/Transport/MessageConsumerInterface.h"
|
2017-02-10 23:39:10 +00:00
|
|
|
|
|
|
|
namespace alexaClientSDK {
|
|
|
|
namespace acl {
|
2017-06-09 23:23:31 +00:00
|
|
|
namespace test {
|
2017-02-10 23:39:10 +00:00
|
|
|
|
|
|
|
using namespace transport;
|
2017-06-09 23:23:31 +00:00
|
|
|
using namespace transport::test;
|
2017-05-26 23:06:14 +00:00
|
|
|
using namespace avsCommon::avs::attachment;
|
2017-06-23 23:26:34 +00:00
|
|
|
using namespace avsCommon::utils::threading;
|
|
|
|
using namespace avsCommon::utils::memory;
|
2017-02-10 23:39:10 +00:00
|
|
|
|
|
|
|
using namespace ::testing;
|
|
|
|
|
|
|
|
class TestableMessageRouter : public MessageRouter {
|
|
|
|
public:
|
|
|
|
TestableMessageRouter(
|
|
|
|
std::shared_ptr<AuthDelegateInterface> authDelegate,
|
2017-05-26 23:06:14 +00:00
|
|
|
std::shared_ptr<AttachmentManager> attachmentManager,
|
2017-02-10 23:39:10 +00:00
|
|
|
std::shared_ptr<MockTransport> transport,
|
|
|
|
const std::string& avsEndpoint,
|
2017-06-23 23:26:34 +00:00
|
|
|
std::shared_ptr<avsCommon::utils::threading::Executor> executor)
|
|
|
|
: MessageRouter(authDelegate, attachmentManager, avsEndpoint, executor),
|
2017-02-10 23:39:10 +00:00
|
|
|
m_mockTransport{transport} {
|
|
|
|
}
|
|
|
|
|
|
|
|
void setMockTransport(std::shared_ptr<MockTransport> transport) {
|
|
|
|
m_mockTransport = transport;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<TransportInterface> createTransport(
|
|
|
|
std::shared_ptr<AuthDelegateInterface> authDelegate,
|
2017-05-26 23:06:14 +00:00
|
|
|
std::shared_ptr<AttachmentManager> attachmentManager,
|
2017-02-10 23:39:10 +00:00
|
|
|
const std::string& avsEndpoint,
|
2017-03-10 00:01:46 +00:00
|
|
|
MessageConsumerInterface* messageConsumerInterface,
|
2017-02-10 23:39:10 +00:00
|
|
|
TransportObserverInterface* transportObserverInterface) override {
|
|
|
|
return m_mockTransport;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<MockTransport> m_mockTransport;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MessageRouterTest : public ::testing::Test {
|
|
|
|
public:
|
|
|
|
const std::string AVS_ENDPOINT = "AVS_ENDPOINT";
|
|
|
|
|
|
|
|
MessageRouterTest()
|
|
|
|
:
|
|
|
|
m_mockMessageRouterObserver{std::make_shared<MockMessageRouterObserver>()},
|
|
|
|
m_mockAuthDelegate{std::make_shared<MockAuthDelegate>()},
|
2017-05-26 23:06:14 +00:00
|
|
|
m_attachmentManager{std::make_shared<AttachmentManager>(AttachmentManager::AttachmentType::IN_PROCESS)},
|
2017-02-10 23:39:10 +00:00
|
|
|
m_mockTransport{std::make_shared<NiceMock<MockTransport>>()},
|
2017-06-23 23:26:34 +00:00
|
|
|
m_executor{std::make_shared<Executor>()},
|
2017-02-10 23:39:10 +00:00
|
|
|
m_router{m_mockAuthDelegate,
|
2017-05-26 23:06:14 +00:00
|
|
|
m_attachmentManager,
|
2017-02-10 23:39:10 +00:00
|
|
|
m_mockTransport,
|
|
|
|
AVS_ENDPOINT,
|
2017-06-23 23:26:34 +00:00
|
|
|
m_executor} {
|
2017-02-10 23:39:10 +00:00
|
|
|
m_router.setObserver(m_mockMessageRouterObserver);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown() {
|
|
|
|
// Wait on both executors to ensure everything is finished
|
2017-06-23 23:26:34 +00:00
|
|
|
waitOnExecutor(m_executor, SHORT_TIMEOUT_MS);
|
2017-02-10 23:39:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 05:02:48 +00:00
|
|
|
std::shared_ptr<avsCommon::avs::MessageRequest> createMessageRequest() {
|
|
|
|
return std::make_shared<avsCommon::avs::MessageRequest>(MESSAGE, nullptr);
|
2017-02-10 23:39:10 +00:00
|
|
|
}
|
|
|
|
void waitOnExecutor(std::shared_ptr<Executor> executor, std::chrono::milliseconds millisecondsToWait) {
|
2017-06-23 23:26:34 +00:00
|
|
|
auto future = executor->submit([]() {;});
|
2017-02-10 23:39:10 +00:00
|
|
|
auto status = future.wait_for(millisecondsToWait);
|
|
|
|
|
|
|
|
ASSERT_EQ(std::future_status::ready, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setupStateToPending() {
|
|
|
|
initializeMockTransport(m_mockTransport.get());
|
|
|
|
m_router.enable();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setupStateToConnected() {
|
|
|
|
setupStateToPending();
|
|
|
|
m_router.onConnected();
|
|
|
|
connectMockTransport(m_mockTransport.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the length of MESSAGE always matches MESSAGE_LENGTH - 1 (one for each char and a null terminator)
|
|
|
|
static const std::string MESSAGE;
|
|
|
|
static const int MESSAGE_LENGTH;
|
|
|
|
static const std::chrono::milliseconds SHORT_TIMEOUT_MS;
|
|
|
|
|
|
|
|
std::shared_ptr<MockMessageRouterObserver> m_mockMessageRouterObserver;
|
|
|
|
std::shared_ptr<MockAuthDelegate> m_mockAuthDelegate;
|
2017-05-26 23:06:14 +00:00
|
|
|
std::shared_ptr<AttachmentManager> m_attachmentManager;
|
2017-02-10 23:39:10 +00:00
|
|
|
std::shared_ptr<NiceMock<MockTransport>> m_mockTransport;
|
2017-06-23 23:26:34 +00:00
|
|
|
std::shared_ptr<avsCommon::utils::threading::Executor> m_executor;
|
2017-02-10 23:39:10 +00:00
|
|
|
TestableMessageRouter m_router;
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::string MessageRouterTest::MESSAGE = "123456789";
|
|
|
|
const int MessageRouterTest::MESSAGE_LENGTH = 10;
|
|
|
|
const std::chrono::milliseconds MessageRouterTest::SHORT_TIMEOUT_MS = std::chrono::milliseconds(1000);
|
|
|
|
|
2017-06-09 23:23:31 +00:00
|
|
|
} // namespace test
|
2017-02-10 23:39:10 +00:00
|
|
|
} // namespace acl
|
|
|
|
} // namespace alexaClientSDK
|
|
|
|
|
2017-03-10 00:01:46 +00:00
|
|
|
#endif // ALEXA_CLIENT_SDK_ACL_TEST_TRANSPORT_ABSTRACT_MESSAGE_ROUTER_TEST_H_
|