Add SDL developer and user guides
[ric-plt/sdl.git] / tst / databaseinfo_test.cpp
1 /*
2    Copyright (c) 2018-2019 Nokia.
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 /*
18  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19  * platform project (RICP).
20 */
21
22 #include <arpa/inet.h>
23 #include <type_traits>
24 #include <gtest/gtest.h>
25 #include <gmock/gmock.h>
26 #include "private/redis/databaseinfo.hpp"
27
28 using namespace shareddatalayer;
29 using namespace shareddatalayer::redis;
30 using namespace testing;
31
32 namespace
33 {
34     static const std::string defaultAddress = "address";
35     static const uint16_t defaultPort = 3333;
36
37     DatabaseInfo getDatabaseInfo(DatabaseInfo::Type type = DatabaseInfo::Type::SINGLE,
38                                  std::string address = defaultAddress,
39                                  uint16_t port = defaultPort)
40     {
41         DatabaseInfo databaseInfo;
42         databaseInfo.type = type;
43         databaseInfo.hosts.push_back({address, htons(port)});
44         databaseInfo.ns = "namespace";
45         return databaseInfo;
46     }
47
48     DatabaseInfo& addHost(DatabaseInfo& databaseInfo,
49                           std::string address,
50                           uint16_t port)
51     {
52         databaseInfo.hosts.push_back({address, htons(port)});
53         return databaseInfo;
54     }
55
56     class DatabaseInfoTest: public testing::Test
57     {
58     public:
59         DatabaseInfoTest()
60         {
61         }
62
63         virtual ~DatabaseInfoTest()
64         {
65         }
66     };
67 }
68
69 TEST_F(DatabaseInfoTest, EqualAndUnequalOperatorsIdenticalInfos)
70 {
71     InSequence dummy;
72     DatabaseInfo databaseInfo1;
73     DatabaseInfo databaseInfo2;
74
75     databaseInfo1 = getDatabaseInfo();
76     databaseInfo2 = getDatabaseInfo();
77
78     EXPECT_TRUE(databaseInfo1 == databaseInfo2);
79     EXPECT_FALSE(databaseInfo1 != databaseInfo2);
80 }
81
82 TEST_F(DatabaseInfoTest, EqualOperatorDifferentAmountOfHosts)
83 {
84     InSequence dummy;
85     DatabaseInfo databaseInfo1;
86     DatabaseInfo databaseInfo2;
87
88     databaseInfo1 = getDatabaseInfo();
89     databaseInfo2 = getDatabaseInfo();
90     databaseInfo2 = addHost(databaseInfo2,
91                             "address2",
92                             123);
93
94     EXPECT_FALSE(databaseInfo1 == databaseInfo2);
95 }
96
97 TEST_F(DatabaseInfoTest, EqualOperatorHostsInDifferentOrder)
98 {
99     InSequence dummy;
100     DatabaseInfo databaseInfo1;
101     DatabaseInfo databaseInfo2;
102
103     databaseInfo1 = getDatabaseInfo(DatabaseInfo::Type::SINGLE,
104                                     "address2",
105                                     123);
106     databaseInfo1 = addHost(databaseInfo1,
107                             defaultAddress,
108                             defaultPort);
109     databaseInfo2 = getDatabaseInfo();
110     databaseInfo2 = addHost(databaseInfo2,
111                             "address2",
112                             123);
113
114     EXPECT_TRUE(databaseInfo1 == databaseInfo2);
115 }
116
117 TEST_F(DatabaseInfoTest, EqualOperatorDiffrentAddressInHosts)
118 {
119     InSequence dummy;
120     DatabaseInfo databaseInfo1;
121     DatabaseInfo databaseInfo2;
122
123     databaseInfo1 = getDatabaseInfo(DatabaseInfo::Type::SINGLE,
124                                     "address1",
125                                     123);
126     databaseInfo2 = getDatabaseInfo(DatabaseInfo::Type::SINGLE,
127                                     "address2",
128                                     123);
129
130     EXPECT_FALSE(databaseInfo1 == databaseInfo2);
131 }
132
133  TEST_F(DatabaseInfoTest, EqualOperatorDiffrentPortInHosts)
134 {
135     InSequence dummy;
136     DatabaseInfo databaseInfo1;
137     DatabaseInfo databaseInfo2;
138
139     databaseInfo1 = getDatabaseInfo(DatabaseInfo::Type::SINGLE,
140                                     "address1",
141                                     123);
142     databaseInfo2 = getDatabaseInfo(DatabaseInfo::Type::SINGLE,
143                                     "address1",
144                                     1234);
145
146     EXPECT_FALSE(databaseInfo1 == databaseInfo2);
147 }