1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 #include "avsession_errors.h"
18 #include "avsession_log.h"
19 #include "json_utils.h"
20 
21 using namespace testing::ext;
22 using namespace OHOS::AVSession;
23 
24 class JsonUtilsTest : public testing::Test {
25 public:
26     static void SetUpTestCase(void);
27     static void TearDownTestCase(void);
28     void SetUp();
29     void TearDown();
30 };
31 
SetUpTestCase()32 void JsonUtilsTest::SetUpTestCase()
33 {}
34 
TearDownTestCase()35 void JsonUtilsTest::TearDownTestCase()
36 {}
37 
SetUp()38 void JsonUtilsTest::SetUp()
39 {}
40 
TearDown()41 void JsonUtilsTest::TearDown()
42 {}
43 
44 /**
45 * @tc.name: IsInt32_001
46 * @tc.desc: test IsInt32
47 * @tc.type: FUNC
48 * @tc.require: #I62OZV
49 */
HWTEST(JsonUtilsTest, IsInt32_001, TestSize.Level1)50 static HWTEST(JsonUtilsTest, IsInt32_001, TestSize.Level1)
51 {
52     SLOGI("IsInt32_001 begin!");
53     json jsonTest = { {"test", 1} };
54     bool ret = JsonUtils::IsInt32(jsonTest, "test");
55     EXPECT_EQ(ret, true);
56     SLOGI("IsInt32_001 end!");
57 }
58 
59 /**
60 * @tc.name: IsInt32_002
61 * @tc.desc: test IsInt32
62 * @tc.type: FUNC
63 * @tc.require: #I62OZV
64 */
HWTEST(JsonUtilsTest, IsInt32_002, TestSize.Level1)65 static HWTEST(JsonUtilsTest, IsInt32_002, TestSize.Level1)
66 {
67     SLOGI("IsInt32_002 begin!");
68     json jsonTest = { {"test", "1"} };
69     bool ret = JsonUtils::IsInt32(jsonTest, "test");
70     EXPECT_EQ(ret, false);
71     SLOGI("IsInt32_002 end!");
72 }
73 
74 /**
75 * @tc.name: IsString_001
76 * @tc.desc: test IsString
77 * @tc.type: FUNC
78 * @tc.require: #I62OZV
79 */
HWTEST(JsonUtilsTest, IsString_001, TestSize.Level1)80 static HWTEST(JsonUtilsTest, IsString_001, TestSize.Level1)
81 {
82     SLOGI("IsString_001 begin!");
83     json jsonTest = { {"test", "123"} };
84     bool ret = JsonUtils::IsString(jsonTest, "test");
85     EXPECT_EQ(ret, true);
86     SLOGI("IsString_001 end!");
87 }
88 
89 /**
90 * @tc.name: IsString_002
91 * @tc.desc: test IsString
92 * @tc.type: FUNC
93 * @tc.require: #I62OZV
94 */
HWTEST(JsonUtilsTest, IsString_002, TestSize.Level1)95 static HWTEST(JsonUtilsTest, IsString_002, TestSize.Level1)
96 {
97     SLOGI("IsString_002 begin!");
98     json jsonTest = { {"test", 1} };
99     bool ret = JsonUtils::IsString(jsonTest, "test");
100     EXPECT_EQ(ret, false);
101     SLOGI("IsString_002 end!");
102 }
103 
104 /**
105 * @tc.name: IsBool_001
106 * @tc.desc: test IsBool
107 * @tc.type: FUNC
108 * @tc.require: #I62OZV
109 */
HWTEST(JsonUtilsTest, IsBool_001, TestSize.Level1)110 static HWTEST(JsonUtilsTest, IsBool_001, TestSize.Level1)
111 {
112     SLOGI("IsBool_001 begin!");
113     json jsonTest = { {"test", true} };
114     bool ret = JsonUtils::IsBool(jsonTest, "test");
115     EXPECT_EQ(ret, true);
116     SLOGI("IsBool_001 end!");
117 }
118 
119 /**
120 * @tc.name: IsBool_002
121 * @tc.desc: test IsBool
122 * @tc.type: FUNC
123 * @tc.require: #I62OZV
124 */
HWTEST(JsonUtilsTest, IsBool_002, TestSize.Level1)125 static HWTEST(JsonUtilsTest, IsBool_002, TestSize.Level1)
126 {
127     SLOGI("IsBool_002 begin!");
128     json jsonTest = { {"test", "123"} };
129     bool ret = JsonUtils::IsBool(jsonTest, "test");
130     EXPECT_EQ(ret, false);
131     SLOGI("IsBool_002 end!");
132 }
133 
134 /**
135 * @tc.name: SetSessionBasicInfo001
136 * @tc.desc: test SetSessionBasicInfo
137 * @tc.type: FUNC
138 * @tc.require: #I62OZV
139 */
HWTEST(JsonUtilsTest, SetSessionBasicInfo001, TestSize.Level1)140 static HWTEST(JsonUtilsTest, SetSessionBasicInfo001, TestSize.Level1)
141 {
142     SLOGI("SetSessionBasicInfo001 begin!");
143     std::string jsonStr = R"(
144         {"test":1}
145     )";
146     AVSessionBasicInfo basicInfo;
147     int32_t ret = JsonUtils::SetSessionBasicInfo(jsonStr, basicInfo);
148     EXPECT_EQ(ret, AVSESSION_SUCCESS);
149     SLOGI("SetSessionBasicInfo001 end!");
150 }
151 
152 /**
153 * @tc.name: SetSessionDescriptors001
154 * @tc.desc: test SetSessionDescriptors
155 * @tc.type: FUNC
156 * @tc.require: #I62OZV
157 */
HWTEST(JsonUtilsTest, SetSessionDescriptors001, TestSize.Level1)158 static HWTEST(JsonUtilsTest, SetSessionDescriptors001, TestSize.Level1)
159 {
160     SLOGI("SetSessionDescriptors001 begin!");
161     std::string jsonStr = "";
162     std::vector<AVSessionDescriptor> descriptors;
163     int32_t ret = JsonUtils::SetSessionDescriptors(jsonStr, descriptors);
164     EXPECT_EQ(ret, AVSESSION_SUCCESS);
165     SLOGI("SetSessionDescriptors001 end!");
166 }
167 
168 /**
169 * @tc.name: SetSessionDescriptors002
170 * @tc.desc: test SetSessionDescriptors
171 * @tc.type: FUNC
172 * @tc.require: #I62OZV
173 */
HWTEST(JsonUtilsTest, SetSessionDescriptors002, TestSize.Level1)174 static HWTEST(JsonUtilsTest, SetSessionDescriptors002, TestSize.Level1)
175 {
176     SLOGI("SetSessionDescriptors002 begin!");
177     std::string jsonStr = R"(
178         {"test":1}
179     )";
180     std::vector<AVSessionDescriptor> descriptors;
181     int32_t ret = JsonUtils::SetSessionDescriptors(jsonStr, descriptors);
182     EXPECT_EQ(ret, AVSESSION_SUCCESS);
183     SLOGI("SetSessionDescriptors002 end!");
184 }
185 
186 /**
187 * @tc.name: SetSessionDescriptors003
188 * @tc.desc: test SetSessionDescriptors
189 * @tc.type: FUNC
190 * @tc.require: #I62OZV
191 */
HWTEST(JsonUtilsTest, SetSessionDescriptors003, TestSize.Level1)192 static HWTEST(JsonUtilsTest, SetSessionDescriptors003, TestSize.Level1)
193 {
194     SLOGI("SetSessionDescriptors003 begin!");
195     std::string jsonStr = R"(
196         {(])}
197     )";
198     std::vector<AVSessionDescriptor> descriptors;
199     int32_t ret = JsonUtils::SetSessionDescriptors(jsonStr, descriptors);
200     EXPECT_EQ(ret, AVSESSION_ERROR);
201     SLOGI("SetSessionDescriptors003 end!");
202 }
203 
204 /**
205 * @tc.name: SetSessionDescriptor001
206 * @tc.desc: test SetSessionDescriptor
207 * @tc.type: FUNC
208 * @tc.require: #I62OZV
209 */
HWTEST(JsonUtilsTest, SetSessionDescriptor001, TestSize.Level1)210 static HWTEST(JsonUtilsTest, SetSessionDescriptor001, TestSize.Level1)
211 {
212     SLOGI("SetSessionDescriptor001 begin!");
213     std::string jsonStr = "";
214     AVSessionDescriptor descriptor;
215     int32_t ret = JsonUtils::SetSessionDescriptor(jsonStr, descriptor);
216     EXPECT_EQ(ret, AVSESSION_SUCCESS);
217     SLOGI("SetSessionDescriptor001 end!");
218 }
219 
220 /**
221 * @tc.name: SetSessionDescriptor002
222 * @tc.desc: test SetSessionDescriptor
223 * @tc.type: FUNC
224 * @tc.require: #I62OZV
225 */
HWTEST(JsonUtilsTest, SetSessionDescriptor002, TestSize.Level1)226 static HWTEST(JsonUtilsTest, SetSessionDescriptor002, TestSize.Level1)
227 {
228     SLOGI("SetSessionDescriptor002 begin!");
229     std::string jsonStr = R"(
230         {(])}
231     )";
232     AVSessionDescriptor descriptor;
233     int32_t ret = JsonUtils::SetSessionDescriptor(jsonStr, descriptor);
234     EXPECT_EQ(ret, AVSESSION_ERROR);
235     SLOGI("SetSessionDescriptor002 end!");
236 }