2 * Samsung Ukraine R&D Center (SRK under a contract between)
3 * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
22 #include "reportcomposer.h"
23 #include "connection.h"
24 #include "eventlistener.h"
25 #include "restservicemock.h"
26 #include <jsoncpp/json/reader.h>
27 #include <jsoncpp/json/writer.h>
29 using namespace communication;
32 using ::testing::Return;
33 using ::testing::ReturnNull;
34 using ::testing::Throw;
35 using ::testing::DoAll;
36 using ::testing::Invoke;
37 using ::testing::WithArg;
38 using ::testing::WithArgs;
39 using ::testing::AtLeast;
45 const std::chrono::milliseconds TEST_KEEP_ALIVE(10);
46 const std::string TEST_SERVER_ADDRESS{"test-server"};
47 const std::string TEST_DEVICE_ID{"device-id"};
49 const std::string TEST_EVENT_TYPE{"report"};
50 const std::string TEST_EVENT_DATA1{"{\"value\": 1}"};
51 const std::string TEST_EVENT_DATA2{"{\"value\": 2}"};
52 const std::string TEST_UPDATES{ R"-([
53 {"type":"policy","uri":"policy-uri"},
54 {"type":"action","uri":"action-uri"},
55 {"type":"unused","uri":"unused-uri"}
57 const std::string TEST_POLICY_URI{"policy-uri"};
58 const std::string TEST_ACTION_URI{"action-uri"};
61 class EventListenerMock: public EventListener
64 EventListenerMock(Connection& conn, const std::string& type): EventListener(conn, type)
68 MOCK_METHOD1(accept, void(Event& event));
71 namespace communication
74 bool operator==(const SessionInfo& si1, const SessionInfo& si2)
76 return si1.duid == si2.duid && si1.authToken == si2.authToken;
81 TEST(TestConnection, test_signal)
84 Connection conn("", TEST_KEEP_ALIVE, &rest, [](){return true;});
86 Json::FastWriter writer;
88 Json::Value data1, data2;
89 ASSERT_TRUE(reader.parse(TEST_EVENT_DATA1, data1));
90 ASSERT_TRUE(reader.parse(TEST_EVENT_DATA2, data2));
91 rc.addEvent(std::make_pair(TEST_EVENT_TYPE, data1));
92 rc.addEvent(std::make_pair(TEST_EVENT_TYPE, data2));
93 SessionInfo checkSessState{"", ""};
95 EXPECT_EQ(rc.str(), writer.write(rc.get()));
97 EXPECT_CALL(rest, registerDevice(Eq(checkSessState), Eq(data1)))
98 .WillOnce(Throw(std::runtime_error("")))
99 .WillOnce(Return(TEST_DEVICE_ID));
100 EXPECT_CALL(rest, getUpdates(_))
101 .WillRepeatedly(Return(""));
102 EXPECT_CALL(rest, sendData(_, ::testing::Matcher<const Json::Value&>(rc.get())))
105 EXPECT_ANY_THROW(conn.registerDevice(Json::Value(data1)));
106 EXPECT_EQ(TEST_DEVICE_ID, conn.registerDevice(Json::Value(data1)));
108 std::thread t(&Connection::loop, &conn);
109 conn.addReportEvent(TEST_EVENT_TYPE, std::move(data1));
110 conn.addReportEvent(TEST_EVENT_TYPE, std::move(data2));
112 std::this_thread::sleep_for(std::chrono::milliseconds(70));
119 void fakeInvoker(Event& e)
124 TEST(TestConnection, test_Listeners)
127 RestServiceMock rest;
128 Connection conn(TEST_DEVICE_ID, TEST_KEEP_ALIVE, &rest, [](){return true;});
130 EventListenerMock el1{conn, "policy"};
131 EventListenerMock el2{conn, "policy"};
132 EventListenerMock el3{conn, "action"};
134 EXPECT_CALL(rest, getUpdates(_))
136 .WillRepeatedly(Return(TEST_UPDATES));
137 EXPECT_CALL(rest, doGet(_, TEST_POLICY_URI))
139 .WillRepeatedly(Return(TEST_EVENT_DATA1));
140 EXPECT_CALL(rest, doGet(_, TEST_ACTION_URI))
142 .WillRepeatedly(Return(TEST_EVENT_DATA2));
143 EXPECT_CALL(el1, accept(_))
145 .WillRepeatedly(WithArgs<0>(Invoke(fakeInvoker)));
146 EXPECT_CALL(el2, accept(_))
148 .WillRepeatedly(WithArgs<0>(Invoke(fakeInvoker)));
149 EXPECT_CALL(el3, accept(_))
151 .WillRepeatedly(WithArgs<0>(Invoke(fakeInvoker)));
153 std::thread t(&Connection::loop, &conn);
155 std::this_thread::sleep_for(std::chrono::milliseconds(15));
160 } catch (std::exception& e) {