• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "CloudTest"
17 #include <string>
18 
19 #include "account/account_delegate.h"
20 #include "cloud/cloud_server.h"
21 #include "cloud/sync_event.h"
22 #include "gtest/gtest.h"
23 #include "ipc_skeleton.h"
24 using namespace testing::ext;
25 using namespace OHOS::DistributedData;
26 namespace OHOS::Test {
27 class CloudTest : public testing::Test {
28 public:
SetUpTestCase(void)29     static void SetUpTestCase(void){};
TearDownTestCase(void)30     static void TearDownTestCase(void){};
SetUp()31     void SetUp(){};
TearDown()32     void TearDown(){};
33 
34 protected:
35     static constexpr const char* testCloudBundle = "test_cloud_bundleName";
36     static constexpr const char* testCloudStore = "test_cloud_database_name";
37 };
38 
39 /**
40 * @tc.name: EventInfo
41 * @tc.desc:
42 * @tc.type: FUNC
43 * @tc.require:
44 */
45 HWTEST_F(CloudTest, EventInfo, TestSize.Level1)
46 {
47     const int32_t mode = 1;
48     const int32_t wait = 10;
49     const bool retry = true;
50     std::shared_ptr<GenQuery> query = nullptr;
51     int sign = 0;
__anon7d36bce70102(const GenDetails& details) 52     auto async = [&sign](const GenDetails& details) {
53         ++sign;
54     };
55     SyncEvent::EventInfo eventInfo1(mode, wait, retry, query, async);
56     SyncEvent::EventInfo eventInfo2(std::move(eventInfo1));
57     SyncEvent::EventInfo eventInfo3 = std::move(eventInfo2);
58     CloudEvent::StoreInfo storeInfo{ IPCSkeleton::GetCallingTokenID(), testCloudBundle, testCloudStore, 0 };
59     SyncEvent evt(storeInfo, eventInfo3);
60     EXPECT_EQ(evt.GetMode(), mode);
61     EXPECT_EQ(evt.GetWait(), wait);
62     EXPECT_EQ(evt.AutoRetry(), retry);
63     EXPECT_EQ(evt.GetQuery(), query);
64     evt.GetAsyncDetail()(GenDetails());
65     EXPECT_NE(0, sign);
66 }
67 
68 /**
69 * @tc.name: Serializable_Marshal
70 * @tc.desc:
71 * @tc.type: FUNC
72 * @tc.require:
73 */
74 HWTEST_F(CloudTest, Serializable_Marshal, TestSize.Level1)
75 {
76     SchemaMeta schemaMeta;
77     bool ret = schemaMeta.IsValid();
78     EXPECT_EQ(false, ret);
79 
80     SchemaMeta::Database database;
81     database.name = testCloudStore;
82     database.alias = "database_alias_test";
83 
84     schemaMeta.version = 1;
85     schemaMeta.bundleName = testCloudBundle;
86     schemaMeta.databases.emplace_back(database);
87     ret = schemaMeta.IsValid();
88     EXPECT_EQ(true, ret);
89 
90     Serializable::json node = schemaMeta.Marshall();
91     SchemaMeta schemaMeta2;
92     schemaMeta2.Unmarshal(node);
93 
94     EXPECT_EQ(schemaMeta.version, schemaMeta2.version);
95     EXPECT_EQ(schemaMeta.bundleName, schemaMeta2.bundleName);
96     Database database2 = schemaMeta2.GetDataBase(testCloudStore);
97     EXPECT_EQ(database.alias, database2.alias);
98 }
99 
100 /**
101 * @tc.name: Field_Marshal
102 * @tc.desc:
103 * @tc.type: FUNC
104 * @tc.require:
105 */
106 HWTEST_F(CloudTest, Field_Marshal, TestSize.Level1)
107 {
108     Field field;
109     field.colName = "field_name1_test";
110     field.alias = "field_alias1_test";
111     field.type = 1;
112     field.primary = true;
113     field.nullable = false;
114 
115     Serializable::json node = field.Marshall();
116     Field field2;
117     field2.Unmarshal(node);
118 
119     EXPECT_EQ(field.colName, field2.colName);
120     EXPECT_EQ(field.alias, field2.alias);
121     EXPECT_EQ(field.type, field2.type);
122     EXPECT_EQ(field.primary, field2.primary);
123     EXPECT_EQ(field.nullable, field2.nullable);
124 }
125 
126 /**
127 * @tc.name: Database_Marshal
128 * @tc.desc:
129 * @tc.type: FUNC
130 * @tc.require:
131 */
132 HWTEST_F(CloudTest, Database_Marshal, TestSize.Level1)
133 {
134     SchemaMeta::Table table1;
135     table1.name = "test_cloud_table_name1";
136     table1.alias = "test_cloud_table_alias1";
137     SchemaMeta::Table table2;
138     table2.name = "test_cloud_table_name2";
139     table2.alias = "test_cloud_table_alias2";
140 
141     SchemaMeta::Database database1;
142     database1.name = testCloudStore;
143     database1.alias = "test_cloud_database_alias";
144     database1.tables.emplace_back(table1);
145     database1.tables.emplace_back(table2);
146 
147     Serializable::json node = database1.Marshall();
148     SchemaMeta::Database database2;
149     database2.Unmarshal(node);
150 
151     EXPECT_EQ(database1.name, database2.name);
152     EXPECT_EQ(database1.alias, database2.alias);
153     std::vector<std::string> tableNames1 = database1.GetTableNames();
154     std::vector<std::string> tableNames2 = database2.GetTableNames();
155     EXPECT_EQ(tableNames1.size(), tableNames2.size());
156     for (uint32_t i = 0; i < tableNames1.size(); ++i) {
157         EXPECT_EQ(tableNames1[i], tableNames2[i]);
158     }
159 }
160 } // namespace OHOS::Test