Release version 0.5.13
[platform/core/appfw/librua.git] / tests / unittest / rua_stat_unit_test.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <gtest/gtest.h>
18
19 #include <rua.h>
20 #include <rua_stat.h>
21 #include <rua_stat_internal.h>
22
23 #include <string>
24 #include <memory>
25
26 #include "sqlite3_mock.h"
27 #include "test_fixture.h"
28
29 using ::testing::_;
30 using ::testing::DoAll;
31 using ::testing::SetArgPointee;
32 using ::testing::Return;
33
34 static int __rua_stat_tag_iter(const char* stat_tag, void* user_data) {
35   return 0;
36 }
37
38 class StatMocks : public ::testing::NiceMock<SqlMock> {};
39
40 class RuaStatTest : public TestFixture {
41  public:
42   RuaStatTest() : TestFixture(std::make_unique<StatMocks>()) {}
43   virtual ~RuaStatTest() {}
44
45   virtual void SetUp() {
46   }
47
48   virtual void TearDown() {
49   }
50
51 };
52
53 TEST_F(RuaStatTest, rua_stat_update_for_uid_N) {
54   int ret = rua_stat_update_for_uid(const_cast<char*>("caller"),
55       const_cast<char*>("tag"), 5001);
56   EXPECT_NE(ret, 0);
57 }
58
59 TEST_F(RuaStatTest, rua_stat_get_stat_tags) {
60   char** table = (char** )calloc(10, sizeof(char*));
61   ASSERT_TRUE(table != nullptr);
62   sqlite3* db = (sqlite3*) table;
63
64   EXPECT_CALL(GetMock<SqlMock>(),
65       sqlite3_open_v2(_, _, _, _)).
66           WillRepeatedly(DoAll(SetArgPointee<1>(db), (Return(0))));
67   EXPECT_CALL(GetMock<SqlMock>(),
68       sqlite3_close_v2(_)).WillRepeatedly(Return(0));
69   EXPECT_CALL(GetMock<SqlMock>(),
70       sqlite3_busy_handler(_, _, _)).WillRepeatedly(Return(0));
71   EXPECT_CALL(GetMock<SqlMock>(),
72       sqlite3_bind_text(_, _, _, _, _)).WillRepeatedly(Return(0));
73   EXPECT_CALL(GetMock<SqlMock>(),
74       sqlite3_step(_)).WillOnce(Return(SQLITE_DONE));
75   EXPECT_CALL(GetMock<SqlMock>(),
76       sqlite3_finalize(_)).WillOnce(Return(0));
77
78   int ret = rua_stat_get_stat_tags(const_cast<char*>("caller"),
79       __rua_stat_tag_iter, nullptr);
80   EXPECT_EQ(ret, 0);
81
82   free(table);
83 }
84
85 TEST_F(RuaStatTest, rua_stat_db_update) {
86   char** table = (char** )calloc(10, sizeof(char*));
87   ASSERT_TRUE(table != nullptr);
88   sqlite3* db = (sqlite3*) table;
89
90   EXPECT_CALL(GetMock<SqlMock>(),
91       sqlite3_open_v2(_, _, _, _)).
92           WillRepeatedly(DoAll(SetArgPointee<1>(db), (Return(0))));
93   EXPECT_CALL(GetMock<SqlMock>(),
94       sqlite3_close_v2(_)).WillRepeatedly(Return(0));
95   EXPECT_CALL(GetMock<SqlMock>(),
96       sqlite3_busy_handler(_, _, _)).WillRepeatedly(Return(0));
97   EXPECT_CALL(GetMock<SqlMock>(),
98       sqlite3_exec(_, _, _, _, _)).WillRepeatedly(Return(SQLITE_OK));
99   EXPECT_CALL(GetMock<SqlMock>(),
100       sqlite3_bind_int(_, _, _)).WillRepeatedly(Return(0));
101   EXPECT_CALL(GetMock<SqlMock>(),
102       sqlite3_bind_double(_, _, _)).WillOnce(Return(0));
103   EXPECT_CALL(GetMock<SqlMock>(),
104       sqlite3_bind_text(_, _, _, _, _)).WillRepeatedly(Return(0));
105   EXPECT_CALL(GetMock<SqlMock>(),
106       sqlite3_step(_)).WillRepeatedly(Return(SQLITE_DONE));
107   EXPECT_CALL(GetMock<SqlMock>(),
108       sqlite3_finalize(_)).WillRepeatedly(Return(0));
109
110   int ret = rua_stat_db_update(const_cast<char*>("caller"),
111       const_cast<char*>("rua_stat_tag"));
112   EXPECT_EQ(ret, SQLITE_DONE);
113
114   free(table);
115 }