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 "connection.h"
23 #include "settingshandler.h"
25 #include "restservicemock.h"
27 using namespace communication;
28 using namespace agent;
31 using ::testing::Return;
32 using ::testing::Throw;
33 using ::testing::TypedEq;
34 using ::testing::AtLeast;
40 const std::chrono::milliseconds TEST_KEEP_ALIVE(10);
41 const std::string TEST_SERVER_ADDRESS{"test-server"};
42 const std::string TEST_DEVICE_ID{"device-id"};
43 //const std::string TEST_UPDATES{ R"-([
44 // {"type":"policy","uri":"settings-uri","curi":"settings-curi"}
46 const std::string TEST_SETTINGS_URI{"settings-uri"};
47 const std::string TEST_SETTINGS_CURI{"settings-curi"};
48 const std::string TEST_UPDATES{ "[{\"type\":\"settings\",\"uri\":\"" + TEST_SETTINGS_URI + "\",\"curi\":\"" + TEST_SETTINGS_CURI +"\"}]"};
49 const std::chrono::milliseconds TEST_TIMEOUT{123};
50 const bool TEST_LOCK = true;
51 const std::string TEST_SETTINGS{"{\
52 \"timeout\": " + std::to_string(TEST_TIMEOUT.count()) + ", \
53 \"lock\": " + std::to_string(int(TEST_LOCK)) + " \
55 const std::string TEST_EMPTY_SETTINGS{"{}"};
56 const std::string TEST_MALFORMED_SETTINGS{"{[12}"};
57 const std::string TEST_SETTINGS_THROW{"{\"timeout\": \"asdf\", \"lock\": 0}"};
58 const std::string TEST_SETTINGS_NEG_TIMEOUT{"{\
60 \"lock\": " + std::to_string(int(TEST_LOCK)) + " \
64 class TestSettingsHandler: public ::testing::Test
69 Settings& settings = Settings::instance();
70 settings.setDeviceId(TEST_DEVICE_ID);
71 settings.setKeepAliveTimeout(TEST_KEEP_ALIVE);
72 settings.setServerAddress(TEST_SERVER_ADDRESS);
73 settings.setLock(!TEST_LOCK);
82 * @brief Test for SettingsHandler::accept method correct work
83 * 1. Generate normal update info
84 * 2. Generate settings update object
85 * 3. Check that settings changed to expected values
87 TEST_F(TestSettingsHandler, test_accept)
90 Settings& settings = Settings::instance();
92 ASSERT_NE(TEST_LOCK, settings.isLocked());
94 Connection conn(TEST_DEVICE_ID, TEST_KEEP_ALIVE, &rest, [](){return true;});
95 SettingsHandler handler(conn);
97 EXPECT_CALL(rest, getUpdates(_))
98 .WillRepeatedly(Return(TEST_UPDATES));
99 EXPECT_CALL(rest, doGet(_, Eq(TEST_SETTINGS_URI)))
100 .WillOnce(Return(TEST_SETTINGS));
101 EXPECT_CALL(rest, doPost(_, Eq(TEST_SETTINGS_CURI), TypedEq<const std::string&>("")))
104 std::thread t(&Connection::loop, &conn);
106 std::this_thread::sleep_for(std::chrono::milliseconds(15));
112 ASSERT_EQ(TEST_TIMEOUT, settings.getKeepAliveTimeout());
113 ASSERT_EQ(TEST_LOCK, settings.isLocked());
114 } catch (std::exception& e) {
120 * @brief Test for SettingsHandler::accept method correct work
121 * 1. Generate normal update info
122 * 2. Generate settings update object without properties
123 * 3. Check that settings not changed
125 TEST_F(TestSettingsHandler, test_no_known_settings)
128 Settings& settings = Settings::instance();
129 RestServiceMock rest;
130 ASSERT_NE(TEST_LOCK, settings.isLocked());
132 Connection conn(TEST_DEVICE_ID, TEST_KEEP_ALIVE, &rest, [](){return true;});
133 SettingsHandler handler(conn);
135 EXPECT_CALL(rest, getUpdates(_))
137 .WillRepeatedly(Return(TEST_UPDATES));
138 EXPECT_CALL(rest, doGet(_, Eq(TEST_SETTINGS_URI)))
140 .WillRepeatedly(Return(TEST_EMPTY_SETTINGS));
141 EXPECT_CALL(rest, doPost(_, Eq(TEST_SETTINGS_CURI), TypedEq<const std::string&>("")))
144 std::thread t(&Connection::loop, &conn);
146 std::this_thread::sleep_for(std::chrono::milliseconds(15));
152 ASSERT_EQ(TEST_KEEP_ALIVE, settings.getKeepAliveTimeout());
153 ASSERT_NE(TEST_LOCK, settings.isLocked());
154 } catch (std::exception& e) {
160 * @brief Test for SettingsHandler::accept method correct work
161 * 1. Generate normal update info
162 * 2. Generate malformed settings update object
163 * 3. Check that settings not changed
165 TEST_F(TestSettingsHandler, test_malformed_json)
168 Settings& settings = Settings::instance();
169 RestServiceMock rest;
170 ASSERT_NE(TEST_LOCK, settings.isLocked());
172 Connection conn(TEST_DEVICE_ID, TEST_KEEP_ALIVE, &rest, [](){return true;});
173 SettingsHandler handler(conn);
175 EXPECT_CALL(rest, getUpdates(_))
177 .WillRepeatedly(Return(TEST_UPDATES));
178 EXPECT_CALL(rest, doGet(_, Eq(TEST_SETTINGS_URI)))
180 .WillRepeatedly(Return(TEST_MALFORMED_SETTINGS));
182 std::thread t(&Connection::loop, &conn);
184 std::this_thread::sleep_for(std::chrono::milliseconds(15));
190 ASSERT_EQ(TEST_KEEP_ALIVE, settings.getKeepAliveTimeout());
191 ASSERT_NE(TEST_LOCK, settings.isLocked());
192 } catch (std::exception& e) {
198 * @brief Test for SettingsHandler::accept method correct work
199 * 1. Generate normal update info
200 * 2. Generate settings update object that cause exception
201 * 3. Check that settings not changed
203 TEST_F(TestSettingsHandler, test_accept_throw)
206 Settings& settings = Settings::instance();
207 RestServiceMock rest;
208 ASSERT_NE(TEST_LOCK, settings.isLocked());
210 Connection conn(TEST_DEVICE_ID, TEST_KEEP_ALIVE, &rest, [](){return true;});
211 SettingsHandler handler(conn);
213 EXPECT_CALL(rest, getUpdates(_))
215 .WillRepeatedly(Return(TEST_UPDATES));
216 EXPECT_CALL(rest, doGet(_, Eq(TEST_SETTINGS_URI)))
218 .WillRepeatedly(Return(TEST_SETTINGS_THROW));
220 std::thread t(&Connection::loop, &conn);
222 std::this_thread::sleep_for(std::chrono::milliseconds(15));
228 ASSERT_EQ(TEST_KEEP_ALIVE, settings.getKeepAliveTimeout());
229 ASSERT_NE(TEST_LOCK, settings.isLocked());
230 } catch (std::exception& e) {
236 * @brief Test for SettingsHandler::accept method correct work
237 * 1. Generate normal update info
238 * 2. Generate settings update object with negative timeout property
239 * 3. Check that timeout not changed but lock state changed
241 TEST_F(TestSettingsHandler, test_negative_timeout)
244 Settings& settings = Settings::instance();
245 RestServiceMock rest;
246 ASSERT_NE(TEST_LOCK, settings.isLocked());
248 Connection conn(TEST_DEVICE_ID, TEST_KEEP_ALIVE, &rest, [](){return true;});
249 SettingsHandler handler(conn);
251 EXPECT_CALL(rest, getUpdates(_))
253 .WillRepeatedly(Return(TEST_UPDATES));
254 EXPECT_CALL(rest, doGet(_, Eq(TEST_SETTINGS_URI)))
256 .WillRepeatedly(Return(TEST_SETTINGS_NEG_TIMEOUT));
257 EXPECT_CALL(rest, doPost(_, Eq(TEST_SETTINGS_CURI), TypedEq<const std::string&>("")))
260 std::thread t(&Connection::loop, &conn);
262 std::this_thread::sleep_for(std::chrono::milliseconds(15));
268 ASSERT_EQ(TEST_KEEP_ALIVE, settings.getKeepAliveTimeout());
269 ASSERT_EQ(TEST_LOCK, settings.isLocked());
270 } catch (std::exception& e) {