1 /*
2  * Copyright (c) 2024 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 <sys/stat.h>
18 #include <sys/types.h>
19 #include <string>
20 
21 #include "switch_adapter.h"
22 #include "distributed_device_profile_errors.h"
23 #include "distributed_device_profile_log.h"
24 #include "kv_data_change_listener.h"
25 #include "types.h"
26 
27 namespace OHOS {
28 namespace DistributedDeviceProfile {
29 using namespace testing::ext;
30 using namespace std;
31 namespace {
32     const std::string TAG = "SwitchAdapterTest";
33 }
34 class SwitchAdapterTest : public testing::Test {
35 public:
36     static void SetUpTestCase();
37     static void TearDownTestCase();
38     void SetUp();
39     void TearDown();
40 };
41 
SetUpTestCase()42 void SwitchAdapterTest::SetUpTestCase()
43 {
44     OHOS::DistributedDeviceProfile::
45         SwitchAdapter::GetInstance().Init();
46 }
47 
TearDownTestCase()48 void SwitchAdapterTest::TearDownTestCase()
49 {
50     OHOS::DistributedDeviceProfile::
51         SwitchAdapter::GetInstance().Uninit();
52 }
53 
SetUp()54 void SwitchAdapterTest::SetUp()
55 {
56 }
57 
TearDown()58 void SwitchAdapterTest::TearDown()
59 {
60 }
61 
62 /*
63  * @tc.name: PutSwitch_001
64  * @tc.desc: Normal testCase of SwitchAdapterTest
65  * @tc.type: FUNC
66  */
67 HWTEST_F(SwitchAdapterTest, PutSwitch_001, TestSize.Level1)
68 {
69     const std::string appId;
70     uint32_t value = 1;
71     uint16_t length = 1;
72     int32_t ret = OHOS::DistributedDeviceProfile::
73         SwitchAdapter::GetInstance().PutSwitch(appId, value, length);
74     EXPECT_EQ(ret, DP_INVALID_PARAMS);
75 }
76 
77 /*
78  * @tc.name: PutSwitch_002
79  * @tc.desc: Normal testCase of SwitchAdapterTest
80  * @tc.type: FUNC
81  */
82 HWTEST_F(SwitchAdapterTest, PutSwitch_002, TestSize.Level1)
83 {
84     const std::string appId = "6666";
85     uint32_t value = 1;
86     uint16_t length = 1;
87     int32_t ret = OHOS::DistributedDeviceProfile::
88         SwitchAdapter::GetInstance().PutSwitch(appId, value, length);
89     EXPECT_EQ(ret, DP_PUT_KV_DB_FAIL);
90 }
91 
92 /*
93  * @tc.name: GetSwitch_001
94  * @tc.desc: Normal testCase of SwitchAdapterTest
95  * @tc.type: FUNC
96  */
97 HWTEST_F(SwitchAdapterTest, GetSwitch_001, TestSize.Level1)
98 {
99     const std::string appId = "";
100     std::string networkId = "networkId";
101     uint32_t value = 1;
102     uint32_t switchLength = 0;
103     int32_t ret = OHOS::DistributedDeviceProfile::
104         SwitchAdapter::GetInstance().GetSwitch(appId, networkId, value, switchLength);
105     EXPECT_EQ(ret, DP_INVALID_PARAMS);
106 }
107 
108 /*
109  * @tc.name: GetSwitch_002
110  * @tc.desc: Normal testCase of SwitchAdapterTest
111  * @tc.type: FUNC
112  */
113 HWTEST_F(SwitchAdapterTest, GetSwitch_002, TestSize.Level1)
114 {
115     const std::string appId = "appid";
116     std::string networkId = "";
117     uint32_t value = 1;
118     uint32_t switchLength = 0;
119     int32_t ret = OHOS::DistributedDeviceProfile::
120         SwitchAdapter::GetInstance().GetSwitch(appId, networkId, value, switchLength);
121     EXPECT_EQ(ret, DP_INVALID_PARAMS);
122 }
123 
124 /*
125  * @tc.name: GetSwitch_003
126  * @tc.desc: Normal testCase of SwitchAdapterTest
127  * @tc.type: FUNC
128  */
129 HWTEST_F(SwitchAdapterTest, GetSwitch_003, TestSize.Level1)
130 {
131     const std::string appId = "appid";
132     std::string networkId = "networkId";
133     uint32_t value = 1;
134     uint32_t switchLength = 0;
135     int32_t ret = OHOS::DistributedDeviceProfile::
136         SwitchAdapter::GetInstance().GetSwitch(appId, networkId, value, switchLength);
137     EXPECT_EQ(ret, DP_PUT_KV_DB_FAIL);
138 }
139 
140 /*
141  * @tc.name: SubscribeSwitchData_001
142  * @tc.desc: Normal testCase of SwitchAdapterTest
143  * @tc.type: FUNC
144  */
145 HWTEST_F(SwitchAdapterTest, SubscribeSwitchData_001, TestSize.Level1)
146 {
147     const std::string appId = "";
148     int32_t ret = OHOS::DistributedDeviceProfile::
149         SwitchAdapter::GetInstance().SubscribeSwitchData(appId);
150     EXPECT_EQ(ret, DP_INVALID_PARAMS);
151 }
152 
153 /*
154  * @tc.name: SubscribeSwitchData_002
155  * @tc.desc: Normal testCase of SwitchAdapterTest
156  * @tc.type: FUNC
157  */
158 HWTEST_F(SwitchAdapterTest, SubscribeSwitchData_002, TestSize.Level1)
159 {
160     const std::string appId = "appid";
161     int32_t ret = OHOS::DistributedDeviceProfile::
162         SwitchAdapter::GetInstance().SubscribeSwitchData(appId);
163     EXPECT_EQ(ret, DP_SUBSCRIBE_FAILED);
164 }
165 
166 /*
167  * @tc.name: UnsubscribeSwitchData_001
168  * @tc.desc: Normal testCase of SwitchAdapterTest
169  * @tc.type: FUNC
170  */
171 HWTEST_F(SwitchAdapterTest, UnsubscribeSwitchData_001, TestSize.Level1)
172 {
173     const std::string appId = "";
174     int32_t ret = OHOS::DistributedDeviceProfile::
175         SwitchAdapter::GetInstance().SubscribeSwitchData(appId);
176     EXPECT_EQ(ret, DP_INVALID_PARAMS);
177 }
178 
179 /*
180  * @tc.name: UnsubscribeSwitchData_002
181  * @tc.desc: Normal testCase of SwitchAdapterTest
182  * @tc.type: FUNC
183  */
184 HWTEST_F(SwitchAdapterTest, UnsubscribeSwitchData_002, TestSize.Level1)
185 {
186     const std::string appId = "appid";
187     int32_t ret = OHOS::DistributedDeviceProfile::
188         SwitchAdapter::GetInstance().SubscribeSwitchData(appId);
189     EXPECT_EQ(ret, DP_SUBSCRIBE_FAILED);
190 }
191 } // namespace DistributedDeviceProfile
192 } // namespace OHOS