AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
MessageRouterTest.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef ALEXA_CLIENT_SDK_ACL_TEST_TRANSPORT_MESSAGEROUTERTEST_H_
17 #define ALEXA_CLIENT_SDK_ACL_TEST_TRANSPORT_MESSAGEROUTERTEST_H_
18 
19 #include <gtest/gtest.h>
20 #include <memory>
21 #include <sstream>
22 #include <string>
23 
27 
29 #include "MockAuthDelegate.h"
30 #include "MockTransport.h"
33 
35 
36 namespace alexaClientSDK {
37 namespace acl {
38 namespace test {
39 
40 using namespace transport;
41 using namespace transport::test;
42 using namespace avsCommon::avs::attachment;
43 using namespace avsCommon::utils::threading;
44 using namespace avsCommon::utils::memory;
46 using namespace avsCommon::utils;
47 using namespace avsCommon::utils::observer::test;
48 
49 using namespace ::testing;
50 
52 public:
54  std::shared_ptr<avsCommon::sdkInterfaces::AuthDelegateInterface> authDelegate,
55  std::shared_ptr<AttachmentManagerInterface> attachmentManager,
56  std::shared_ptr<TransportFactoryInterface> factory,
57  const std::string& avsGateway) :
59  authDelegate,
60  attachmentManager,
61  factory,
62  avsGateway,
63  avsCommon::sdkInterfaces::ENGINE_TYPE_ALEXA_VOICE_SERVICES,
64  SHORT_SERVER_SIDE_DISCONNECT_GRACE_PERIOD) {
65  }
66 
73  bool isExecutorReady(std::chrono::milliseconds millisecondsToWait) {
74  auto future = m_executor.submit([]() { ; });
75  auto status = future.wait_for(millisecondsToWait);
76  return status == std::future_status::ready;
77  }
78 
80  return !m_executor.isShutdown();
81  }
82 
84  static const std::chrono::milliseconds SHORT_SERVER_SIDE_DISCONNECT_GRACE_PERIOD;
85 };
86 
88 public:
89  explicit MockTransportFactory(std::shared_ptr<MockTransport> transport) : m_mockTransport{transport} {
90  }
91 
92  void setMockTransport(std::shared_ptr<MockTransport> transport) {
93  m_mockTransport = transport;
94  }
95 
96 private:
97  std::shared_ptr<TransportInterface> createTransport(
98  std::shared_ptr<avsCommon::sdkInterfaces::AuthDelegateInterface> authDelegate,
99  std::shared_ptr<AttachmentManagerInterface> attachmentManager,
100  const std::string& avsGateway,
101  std::shared_ptr<MessageConsumerInterface> messageConsumerInterface,
102  std::shared_ptr<TransportObserverInterface> transportObserverInterface,
103  std::shared_ptr<SynchronizedMessageRequestQueue> sharedMessageRequestQueue) override {
104  return m_mockTransport;
105  }
106 
107  std::shared_ptr<MockTransport> m_mockTransport;
108 };
109 
111 public:
112  const std::string AVS_ENDPOINT = "AVS_ENDPOINT";
113 
115  m_mockMessageRouterObserver{std::make_shared<MockMessageRouterObserver>()},
116  m_mockAuthDelegate{std::make_shared<MockAuthDelegate>()},
117  m_attachmentManager{std::make_shared<AttachmentManager>(AttachmentManager::AttachmentType::IN_PROCESS)},
118  m_mockTransport{std::make_shared<NiceMock<MockTransport>>()},
119  m_transportFactory{std::make_shared<MockTransportFactory>(m_mockTransport)},
120  m_router{std::make_shared<TestableMessageRouter>(
121  m_mockAuthDelegate,
122  m_attachmentManager,
123  m_transportFactory,
124  AVS_ENDPOINT)} {
125  m_router->setObserver(m_mockMessageRouterObserver);
126  }
127 
128  void TearDown() {
129  if (m_router->isExecutorActive()) {
130  // Wait on MessageRouter to ensure everything is finished
131  waitOnMessageRouter(SHORT_TIMEOUT_MS);
132  }
133  }
134 
135  std::shared_ptr<avsCommon::avs::MessageRequest> createMessageRequest() {
136  return std::make_shared<avsCommon::avs::MessageRequest>(MESSAGE);
137  }
138 
139  std::shared_ptr<TestMessageRequestObserver> createObserver() {
140  return std::make_shared<TestMessageRequestObserver>();
141  }
142 
143  void waitOnMessageRouter(std::chrono::milliseconds millisecondsToWait) {
144  auto status = m_router->isExecutorReady(millisecondsToWait);
145  ASSERT_EQ(true, status);
146  }
147 
149  initializeMockTransport(m_mockTransport.get());
150  m_router->enable();
151  }
152 
154  setupStateToPending();
155  m_router->onConnected(m_mockTransport);
156  connectMockTransport(m_mockTransport.get());
157  }
158 
159  // Ensure the length of MESSAGE always matches MESSAGE_LENGTH - 1 (one for each char and a null terminator)
160  static const std::string MESSAGE;
161  static const int MESSAGE_LENGTH;
162  static const std::chrono::milliseconds SHORT_TIMEOUT_MS;
163  static const std::string CONTEXT_ID;
164 
165  std::shared_ptr<MockMessageRouterObserver> m_mockMessageRouterObserver;
166  std::shared_ptr<MockAuthDelegate> m_mockAuthDelegate;
167  std::shared_ptr<AttachmentManagerInterface> m_attachmentManager;
168  std::shared_ptr<NiceMock<MockTransport>> m_mockTransport;
169  std::shared_ptr<MockTransportFactory> m_transportFactory;
170  std::shared_ptr<TestableMessageRouter> m_router;
171  // TestableMessageRouter m_router;
172 };
173 
174 const std::chrono::milliseconds TestableMessageRouter::SHORT_SERVER_SIDE_DISCONNECT_GRACE_PERIOD(2000);
175 const std::string MessageRouterTest::MESSAGE = "123456789";
176 const int MessageRouterTest::MESSAGE_LENGTH = 10;
177 const std::chrono::milliseconds MessageRouterTest::SHORT_TIMEOUT_MS = std::chrono::milliseconds(1000);
178 const std::string MessageRouterTest::CONTEXT_ID = "contextIdString";
179 
180 } // namespace test
181 } // namespace acl
182 } // namespace alexaClientSDK
183 
184 #endif // ALEXA_CLIENT_SDK_ACL_TEST_TRANSPORT_MESSAGEROUTERTEST_H_
Definition: gmock-actions.h:53
std::shared_ptr< TestMessageRequestObserver > createObserver()
Definition: MessageRouterTest.h:139
std::shared_ptr< MockMessageRouterObserver > m_mockMessageRouterObserver
Definition: MessageRouterTest.h:165
std::shared_ptr< TestableMessageRouter > m_router
Definition: MessageRouterTest.h:170
std::shared_ptr< avsCommon::avs::MessageRequest > createMessageRequest()
Definition: MessageRouterTest.h:135
Definition: AmdMetricWrapperTest.cpp:24
::std::string string
Definition: gtest-port.h:1097
std::shared_ptr< MockAuthDelegate > m_mockAuthDelegate
Definition: MessageRouterTest.h:166
static const int MESSAGE_LENGTH
Definition: MessageRouterTest.h:161
Definition: AlexaEventProcessedObserverInterface.h:23
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
std::shared_ptr< NiceMock< MockTransport > > m_mockTransport
Definition: MessageRouterTest.h:168
bool isExecutorReady(std::chrono::milliseconds millisecondsToWait)
Definition: MessageRouterTest.h:73
void TearDown()
Definition: MessageRouterTest.h:128
void setMockTransport(std::shared_ptr< MockTransport > transport)
Definition: MessageRouterTest.h:92
void setupStateToConnected()
Definition: MessageRouterTest.h:153
static const std::chrono::milliseconds SHORT_TIMEOUT_MS
Definition: MessageRouterTest.h:162
static const std::string MESSAGE
Definition: MessageRouterTest.h:160
void connectMockTransport(MockTransport *transport)
Definition: MockTransport.h:68
void setupStateToPending()
Definition: MessageRouterTest.h:148
Definition: TransportFactoryInterface.h:36
Definition: MessageRouter.h:47
void waitOnMessageRouter(std::chrono::milliseconds millisecondsToWait)
Definition: MessageRouterTest.h:143
Definition: MessageRouterTest.h:51
void initializeMockTransport(MockTransport *transport)
Definition: MockTransport.h:60
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
Definition: gtest.h:371
MockTransportFactory(std::shared_ptr< MockTransport > transport)
Definition: MessageRouterTest.h:89
Definition: MessageRouterTest.h:87
Definition: MessageRouterTest.h:110
std::shared_ptr< MockTransportFactory > m_transportFactory
Definition: MessageRouterTest.h:169
static const std::string CONTEXT_ID
Definition: MessageRouterTest.h:163
static const std::chrono::milliseconds SHORT_TIMEOUT_MS
Timeout to sleep before asking for provideState().
Definition: AudioActivityTrackerTest.cpp:80
bool isExecutorActive()
Definition: MessageRouterTest.h:79
std::shared_ptr< AttachmentManagerInterface > m_attachmentManager
Definition: MessageRouterTest.h:167
TestableMessageRouter(std::shared_ptr< avsCommon::sdkInterfaces::AuthDelegateInterface > authDelegate, std::shared_ptr< AttachmentManagerInterface > attachmentManager, std::shared_ptr< TransportFactoryInterface > factory, const std::string &avsGateway)
Definition: MessageRouterTest.h:53
MessageRouterTest()
Definition: MessageRouterTest.h:114
static const std::chrono::milliseconds SHORT_SERVER_SIDE_DISCONNECT_GRACE_PERIOD
Short amount of time to allow for an automatic reconnect before notifying of a server side disconnect...
Definition: MessageRouterTest.h:84
static const int ENGINE_TYPE_ALEXA_VOICE_SERVICES
Definition: ConnectionStatusObserverInterface.h:26

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0