Rule management added.
[platform/core/security/suspicious-activity-monitor.git] / utest / test_servicerequest.cpp
1 /**
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.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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
17  */
18 #include <gmock/gmock.h>
19 #include <chrono>
20 #include <jsoncpp/json/value.h>
21
22 #include "connection.h"
23 #include "restservicemock.h"
24 #include "servicerequest.h"
25 #include "settings.h"
26
27 using ::testing::_;
28 using ::testing::Eq;
29 using ::testing::Return;
30 using ::testing::Throw;
31 using ::testing::DoAll;
32 using ::testing::Invoke;
33 using ::testing::WithArgs;
34 using ::testing::TypedEq;
35
36 namespace
37 {
38 const std::chrono::milliseconds TEST_TIMEOUT_INTERVAL{1};
39 const std::string TEST_DEVICE_ID{"device-id"};
40 }
41
42 TEST(TestServiceRequest, test_registerDevice)
43 {
44     try {
45         auto& settings = agent::Settings::instance();
46         settings.setKeepAliveTimeout(TEST_TIMEOUT_INTERVAL);
47         settings.setDeviceId("");
48         settings.setSaveFileName("/dev/null");
49         RestServiceMock rest;
50         communication::Connection conn("", TEST_TIMEOUT_INTERVAL, &rest);
51
52         EXPECT_CALL(rest, registerDevice(_, _))
53                 .WillOnce(Throw(std::runtime_error("")))
54                 .WillOnce(Return(TEST_DEVICE_ID));
55
56
57         agent::ServiceRequest::registerDevice(conn);
58         EXPECT_EQ(TEST_DEVICE_ID, settings.getDeviceId());
59     } catch (std::exception& e) {
60         FAIL() << e.what();
61     }
62 }
63
64 TEST(TestServiceRequest, test_updateRules)
65 {
66     try {
67         auto& settings = agent::Settings::instance();
68         settings.setKeepAliveTimeout(TEST_TIMEOUT_INTERVAL);
69         RestServiceMock rest;
70         communication::Connection conn("", TEST_TIMEOUT_INTERVAL, &rest);
71         Json::Value check;
72         check["type"] = std::string{"request-rules"};
73
74         EXPECT_CALL(rest, sendData(_, TypedEq<const Json::Value&>(check)))
75                 .WillOnce(Throw(std::runtime_error("")))
76                 .WillOnce(Return());
77
78         agent::ServiceRequest::updateRules(conn);
79         EXPECT_EQ(TEST_DEVICE_ID, settings.getDeviceId());
80     } catch (std::exception& e) {
81         FAIL() << e.what();
82     }
83 }