1/*
2 * Copyright (c) 2021-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
18#define private public
19#define protected public
20#include "operation.h"
21#include "operation_builder.h"
22#undef private
23#undef protected
24
25using namespace testing::ext;
26using namespace OHOS::AAFwk;
27using OHOS::Parcel;
28using Uri = OHOS::Uri;
29class OperationBaseTest : public testing::Test {
30public:
31    OperationBaseTest()
32    {}
33    ~OperationBaseTest()
34    {}
35    static void SetUpTestCase(void);
36    static void TearDownTestCase(void);
37    void SetUp();
38    void TearDown();
39
40    std::shared_ptr<OperationBuilder> operationbuilder_ = nullptr;
41};
42
43void OperationBaseTest::SetUpTestCase(void)
44{}
45
46void OperationBaseTest::TearDownTestCase(void)
47{}
48
49void OperationBaseTest::SetUp(void)
50{
51    operationbuilder_ = std::make_shared<OperationBuilder>();
52}
53
54void OperationBaseTest::TearDown(void)
55{}
56
57/**
58 * @tc.number: AaFwk_Operation_GetAbilityName_0100
59 * @tc.name: WithAbilityName/GetAbilityName.
60 * @tc.desc: Verify the function when the input string contains special characters.
61 */
62HWTEST_F(OperationBaseTest, AaFwk_Operation_GetAbilityName_0100, Function | MediumTest | Level1)
63{
64    std::string value = "enter";
65    GTEST_LOG_(INFO) << "AaFwk_Operation_GetAbilityName_0100 start";
66
67    operationbuilder_->WithAbilityName(value);
68    std::shared_ptr<Operation> operation = operationbuilder_->Build();
69    EXPECT_STREQ(value.c_str(), operation->GetAbilityName().c_str());
70
71    GTEST_LOG_(INFO) << "AaFwk_Operation_GetAbilityName_0100 end";
72}
73
74/**
75 * @tc.number: AaFwk_Operation_GetAbilityName_0200
76 * @tc.name: WithAbilityName/GetAbilityName.
77 * @tc.desc: Verify the function when the input string is empty.
78 */
79HWTEST_F(OperationBaseTest, AaFwk_Operation_GetAbilityName_0200, Function | MediumTest | Level3)
80{
81    std::string value = "";
82    operationbuilder_->WithAbilityName(value);
83    std::shared_ptr<Operation> operation = operationbuilder_->Build();
84
85    EXPECT_STREQ(value.c_str(), operation->GetAbilityName().c_str());
86}
87
88/**
89 * @tc.number:  AaFwk_Operation_GetBundleName_0100
90 * @tc.name: WithBundleName/GetBundleName
91 * @tc.desc: Verify the function when the input string contains special characters.
92 */
93HWTEST_F(OperationBaseTest, AaFwk_Operation_GetBundleName_0100, Function | MediumTest | Level1)
94{
95    std::string value = "value";
96    operationbuilder_->WithBundleName(value);
97    std::shared_ptr<Operation> operation = operationbuilder_->Build();
98    EXPECT_STREQ(value.c_str(), operation->GetBundleName().c_str());
99}
100
101/**
102 * @tc.number: AaFwk_Operation_GetBundleName_0200
103 * @tc.name: WithBundleName/GetBundleName
104 * @tc.desc: Verify the function when the input string is empty.
105 */
106HWTEST_F(OperationBaseTest, AaFwk_Operation_GetBundleName_0200, Function | MediumTest | Level3)
107{
108    std::string value = "";
109    operationbuilder_->WithBundleName(value);
110    std::shared_ptr<Operation> operation = operationbuilder_->Build();
111    EXPECT_STREQ(value.c_str(), operation->GetBundleName().c_str());
112}
113
114/**
115 * @tc.number: AaFwk_Operation_GetDeviceId_0100
116 * @tc.name: WithDeviceId/GetDeviceId
117 * @tc.desc: Verify the function when the input string contains special characters.
118 */
119HWTEST_F(OperationBaseTest, AaFwk_Operation_GetDeviceId_0100, Function | MediumTest | Level1)
120{
121    std::string value = "value";
122    operationbuilder_->WithDeviceId(value);
123    std::shared_ptr<Operation> operation = operationbuilder_->Build();
124    EXPECT_STREQ(value.c_str(), operation->GetDeviceId().c_str());
125}
126
127/**
128 * @tc.number: AaFwk_Operation_GetDeviceId_0200
129 * @tc.name: WithDeviceId/GetDeviceId
130 * @tc.desc: Verify the function when the input string is empty.
131 */
132HWTEST_F(OperationBaseTest, AaFwk_Operation_GetDeviceId_0200, Function | MediumTest | Level3)
133{
134    std::string value = "";
135    operationbuilder_->WithDeviceId(value);
136    std::shared_ptr<Operation> operation = operationbuilder_->Build();
137    EXPECT_STREQ(value.c_str(), operation->GetDeviceId().c_str());
138}
139
140/**
141 * @tc.number: AaFwk_Operation_GetAction_0100
142 * @tc.name: WithAction/GetAction
143 * @tc.desc: Verify the function when the input string contains special characters.
144 */
145HWTEST_F(OperationBaseTest, AaFwk_Operation_GetAction_0100, Function | MediumTest | Level1)
146{
147    std::string value = "value";
148    operationbuilder_->WithAction(value);
149    std::shared_ptr<Operation> operation = operationbuilder_->Build();
150    EXPECT_STREQ(value.c_str(), operation->GetAction().c_str());
151}
152
153/**
154 * @tc.number: AaFwk_Operation_GetAction_0200
155 * @tc.name: WithAction/GetAction
156 * @tc.desc: Verify the function when the input string is empty.
157 */
158HWTEST_F(OperationBaseTest, AaFwk_Operation_GetAction_0200, Function | MediumTest | Level3)
159{
160    std::string value = "";
161    operationbuilder_->WithAction(value);
162    std::shared_ptr<Operation> operation = operationbuilder_->Build();
163    EXPECT_STREQ(value.c_str(), operation->GetAction().c_str());
164}
165
166/**
167 * @tc.number: AaFwk_Operation_GetEntities_0100
168 * @tc.name: WithEntities/GetEntities
169 * @tc.desc: Verify the function when the input string contains special characters.
170 */
171HWTEST_F(OperationBaseTest, AaFwk_Operation_GetEntities_0100, Function | MediumTest | Level1)
172{
173    std::vector<std::string> value;
174    value.push_back("string1");
175    operationbuilder_->WithEntities(value);
176    std::shared_ptr<Operation> operation = operationbuilder_->Build();
177
178    std::vector<std::string> revValue = operation->GetEntities();
179
180    if (value.size() > 0 && revValue.size() > 0) {
181        EXPECT_STREQ(value.at(0).c_str(), operation->GetEntities().at(0).c_str());
182    } else {
183        EXPECT_EQ(true, revValue.size() > 0);
184    }
185}
186
187/**
188 * @tc.number: AaFwk_Operation_GetEntities_0200
189 * @tc.name: WithEntities/GetEntities
190 * @tc.desc: Verify the function when the input string is empty.
191 */
192HWTEST_F(OperationBaseTest, AaFwk_Operation_GetEntities_0200, Function | MediumTest | Level3)
193{
194    std::vector<std::string> value;
195    operationbuilder_->WithEntities(value);
196    std::shared_ptr<Operation> operation = operationbuilder_->Build();
197    EXPECT_EQ(true, operation->GetEntities().size() == 0);
198}
199
200/**
201 * @tc.number: AaFwk_Operation_GetFlags_0100
202 * @tc.name: WithFlags/GetFlags
203 * @tc.desc: Verify the function when the input string contains special characters.
204 */
205HWTEST_F(OperationBaseTest, AaFwk_Operation_GetFlags_0100, Function | MediumTest | Level1)
206{
207    unsigned int value = 1;
208    operationbuilder_->WithFlags(value);
209    std::shared_ptr<Operation> operation = operationbuilder_->Build();
210    EXPECT_EQ(value, operation->GetFlags());
211}
212
213/**
214 * @tc.number: AaFwk_Operation_GetFlags_0200
215 * @tc.name: WithFlags/GetFlags
216 * @tc.desc: Verify the function when the input string is empty.
217 */
218HWTEST_F(OperationBaseTest, AaFwk_Operation_GetFlags_0200, Function | MediumTest | Level3)
219{
220    unsigned int value = 0;
221    operationbuilder_->WithFlags(value);
222    std::shared_ptr<Operation> operation = operationbuilder_->Build();
223    EXPECT_EQ(value, operation->GetFlags());
224}
225
226/**
227 * @tc.number: AaFwk_Operation_GetUri_0100
228 * @tc.name: WithUri/GetUri
229 * @tc.desc: Verify the function when the input string contains special characters.
230 */
231HWTEST_F(OperationBaseTest, AaFwk_Operation_GetUri_0100, Function | MediumTest | Level1)
232{
233    std::string value = "scheme://authority/path1/path2/path3?id = 1&name = mingming&old#fragment";
234    OHOS::Uri uri(value);
235    operationbuilder_->WithUri(uri);
236    std::shared_ptr<Operation> operation = operationbuilder_->Build();
237
238    EXPECT_EQ(uri, operation->GetUri());
239}
240
241/**
242 * @tc.number: AaFwk_Operation_GetUri_0200
243 * @tc.name: WithUri/GetUri
244 * @tc.desc: Verify the function when the input string is empty.
245 */
246HWTEST_F(OperationBaseTest, AaFwk_Operation_GetUri_0200, Function | MediumTest | Level3)
247{
248    std::string value = "";
249    OHOS::Uri uri(value);
250    operationbuilder_->WithUri(uri);
251    std::shared_ptr<Operation> operation = operationbuilder_->Build();
252    EXPECT_EQ(uri, operation->GetUri());
253}
254
255/**
256 * @tc.number: AaFwk_Operation_build_0100
257 * @tc.name: build
258 * @tc.desc: Verify that the parameters are correct.
259 */
260HWTEST_F(OperationBaseTest, AaFwk_Operation_build_0100, Function | MediumTest | Level1)
261{
262    std::string value = "value";
263    OHOS::Uri uri(value);
264    std::vector<std::string> columns;
265    columns.push_back("string1");
266    operationbuilder_->WithUri(uri);
267    operationbuilder_->WithAction(value);
268    operationbuilder_->WithEntities(columns);
269    operationbuilder_->WithDeviceId(value);
270    operationbuilder_->WithBundleName(value);
271    operationbuilder_->WithAbilityName(value);
272
273    std::shared_ptr<Operation> operation = operationbuilder_->Build();
274
275    EXPECT_EQ(uri, operation->GetUri());
276    EXPECT_STREQ(value.c_str(), operation->GetAction().c_str());
277
278    std::vector<std::string> revValue = operation->GetEntities();
279
280    if (columns.size() > 0 && revValue.size() > 0) {
281        EXPECT_STREQ(columns.at(0).c_str(), operation->GetEntities().at(0).c_str());
282    } else {
283        EXPECT_EQ(true, revValue.size() > 0);
284    }
285    EXPECT_STREQ(value.c_str(), operation->GetDeviceId().c_str());
286    EXPECT_STREQ(value.c_str(), operation->GetBundleName().c_str());
287    EXPECT_STREQ(value.c_str(), operation->GetAbilityName().c_str());
288}
289
290/**
291 * @tc.number: AaFwk_Operation_Marshalling_0100
292 * @tc.name: Marshalling/Unmarshalling
293 * @tc.desc: Validation serialization.
294 */
295HWTEST_F(OperationBaseTest, AaFwk_Operation_Marshalling_0100, Function | MediumTest | Level1)
296{
297    std::string value = "value";
298    OHOS::Uri uri(value);
299    std::vector<std::string> columns;
300    columns.push_back("string1");
301    operationbuilder_->WithUri(uri);
302    operationbuilder_->WithAction(value);
303    operationbuilder_->WithEntities(columns);
304    operationbuilder_->WithDeviceId(value);
305    operationbuilder_->WithBundleName(value);
306    operationbuilder_->WithAbilityName(value);
307
308    std::shared_ptr<Operation> operation = operationbuilder_->Build();
309    Parcel in;
310    operation->Marshalling(in);
311
312    Operation *pOperation = operation->Unmarshalling(in);
313    if (pOperation != nullptr) {
314        EXPECT_EQ(true, *pOperation == *(operation.get()));
315    } else {
316        EXPECT_EQ(true, pOperation != nullptr);
317    }
318}
319
320/**
321 * @tc.number: AaFwk_Operation_Operator_0100
322 * @tc.name: Operator
323 * @tc.desc: Verify string overload.
324 */
325HWTEST_F(OperationBaseTest, AaFwk_Operation_Operator_0100, Function | MediumTest | Level1)
326{
327    Operation operation_;
328    std::string value = "value";
329    OHOS::Uri uri(value);
330    std::vector<std::string> columns;
331    columns.push_back("string1");
332    operationbuilder_->WithUri(uri);
333    operationbuilder_->WithAction(value);
334    operationbuilder_->WithEntities(columns);
335    operationbuilder_->WithDeviceId(value);
336    operationbuilder_->WithBundleName(value);
337    operationbuilder_->WithAbilityName(value);
338
339    std::shared_ptr<Operation> operation = operationbuilder_->Build();
340    operation_ = *(operation.get());
341
342    EXPECT_EQ(true, operation_ == *(operation.get()));
343}
344
345/**
346 * @tc.number: AaFwk_Operation_DumpInfo_0100
347 * @tc.name: DumpInfo
348 * @tc.desc: Verify string overload.
349 */
350HWTEST_F(OperationBaseTest, AaFwk_Operation_DumpInfo_0100, Function | MediumTest | Level1)
351{
352    GTEST_LOG_(INFO) << "AaFwk_Operation_DumpInfo_0100 start";
353    std::shared_ptr<Operation> operation = operationbuilder_->Build();
354    int level = 10;
355    operation->entities_.push_back("a");
356    operation->entities_.push_back("b");
357    operation->DumpInfo(level);
358    EXPECT_EQ(true, operation->GetEntities().size() == 2);
359    GTEST_LOG_(INFO) << "AaFwk_Operation_DumpInfo_0100 end";
360}
361