1 /* 2 * Copyright (c) 2023 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 "cloud/subscription.h" 17 18 #include <gtest/gtest.h> 19 20 using namespace testing::ext; 21 using namespace OHOS::DistributedData; 22 namespace OHOS::Test { 23 class SubscriptionTest : public testing::Test { 24 public: 25 const std::map<std::string, std::string> testRelation = { { "testUserId", "testBundleName" } }; 26 const std::map<std::string, uint64_t> testExpiresTime = { { "1h", 3600 } }; SetUpTestCase(void)27 static void SetUpTestCase(void) {}; TearDownTestCase(void)28 static void TearDownTestCase(void) {}; SetUp()29 void SetUp() {}; TearDown()30 void TearDown() {}; 31 }; 32 33 /** 34 * @tc.name: RelationMarshal 35 * @tc.desc: relation marshal. 36 * @tc.type: FUNC 37 */ 38 HWTEST_F(SubscriptionTest, RelationMarshal, TestSize.Level1) 39 { 40 Subscription::Relation relation; 41 relation.id = "testId"; 42 relation.bundleName = "testBundleName"; 43 relation.relations = testRelation; 44 Subscription::json node; 45 relation.Marshal(node); 46 ASSERT_EQ(node["id"], "testId"); 47 ASSERT_EQ(node["bundleName"], "testBundleName"); 48 ASSERT_EQ(node["relations"], testRelation); 49 } 50 51 /** 52 * @tc.name: RelationUnmarshal 53 * @tc.desc: relation unmarshal. 54 * @tc.type: FUNC 55 */ 56 HWTEST_F(SubscriptionTest, RelationUnmarshal, TestSize.Level1) 57 { 58 Subscription::json node; 59 node["id"] = "testId"; 60 node["bundleName"] = "testBundleName"; 61 node["relations"] = testRelation; 62 Subscription::Relation relation; 63 relation.Unmarshal(node); 64 ASSERT_EQ(relation.id, "testId"); 65 ASSERT_EQ(relation.bundleName, "testBundleName"); 66 ASSERT_EQ(relation.relations, testRelation); 67 } 68 69 /** 70 * @tc.name: Marshal 71 * @tc.desc: marshal. 72 * @tc.type: FUNC 73 */ 74 HWTEST_F(SubscriptionTest, Marshal, TestSize.Level1) 75 { 76 Subscription subscription; 77 subscription.userId = 100; 78 subscription.id = "testId"; 79 subscription.expiresTime = testExpiresTime; 80 Subscription::json node; 81 subscription.Marshal(node); 82 ASSERT_EQ(node["userId"], 100); 83 ASSERT_EQ(node["id"], "testId"); 84 ASSERT_EQ(node["expiresTime"], testExpiresTime); 85 } 86 87 /** 88 * @tc.name: Unmarshal 89 * @tc.desc: unmarshal. 90 * @tc.type: FUNC 91 */ 92 HWTEST_F(SubscriptionTest, Unmarshal, TestSize.Level1) 93 { 94 Subscription::json node; 95 node["userId"] = 100; 96 node["id"] = "testId"; 97 node["expiresTime"] = testExpiresTime; 98 Subscription subscription; 99 subscription.Unmarshal(node); 100 ASSERT_EQ(subscription.userId, 100); 101 ASSERT_EQ(subscription.id, "testId"); 102 ASSERT_EQ(subscription.expiresTime, testExpiresTime); 103 } 104 105 /** 106 * @tc.name: GetKey 107 * @tc.desc: get key. 108 * @tc.type: FUNC 109 */ 110 HWTEST_F(SubscriptionTest, GetKey, TestSize.Level1) 111 { 112 Subscription subscription; 113 subscription.userId = 100; 114 std::string key = subscription.GetKey(); 115 ASSERT_EQ(key, "CLOUD_SUBSCRIPTION###100"); 116 } 117 118 /** 119 * @tc.name: GetRelationKey 120 * @tc.desc: get relation key. 121 * @tc.type: FUNC 122 */ 123 HWTEST_F(SubscriptionTest, GetRelationKey, TestSize.Level1) 124 { 125 Subscription subscription; 126 subscription.userId = 100; 127 std::string relationKey = subscription.GetRelationKey("testBundleName"); 128 ASSERT_EQ(relationKey, "CLOUD_RELATION###100###testBundleName"); 129 } 130 131 /** 132 * @tc.name: GetKey002 133 * @tc.desc: get key. 134 * @tc.type: FUNC 135 */ 136 HWTEST_F(SubscriptionTest, GetKey002, TestSize.Level1) 137 { 138 std::string key = Subscription::GetKey(100); 139 ASSERT_EQ(key, "CLOUD_SUBSCRIPTION###100"); 140 } 141 142 /** 143 * @tc.name: GetRelationKey002 144 * @tc.desc: get relation key. 145 * @tc.type: FUNC 146 */ 147 HWTEST_F(SubscriptionTest, GetRelationKey002, TestSize.Level1) 148 { 149 std::string relationKey = Subscription::GetRelationKey(100, "testBundleName"); 150 ASSERT_EQ(relationKey, "CLOUD_RELATION###100###testBundleName"); 151 } 152 153 /** 154 * @tc.name: GetPrefix 155 * @tc.desc: get prefix. 156 * @tc.type: FUNC 157 */ 158 HWTEST_F(SubscriptionTest, GetPrefix, TestSize.Level1) 159 { 160 std::initializer_list<std::string> fields = { "field1", "field2" }; 161 std::string prefix = Subscription::GetPrefix(fields); 162 ASSERT_EQ(prefix, "CLOUD_SUBSCRIPTION###field1###field2"); 163 } 164 165 /** 166 * @tc.name: GetMinExpireTime001 167 * @tc.desc: 168 * @tc.type: FUNC 169 * @tc.require: 170 * @tc.author: SQL 171 */ 172 HWTEST_F(SubscriptionTest, GetMinExpireTime001, TestSize.Level1) 173 { 174 Subscription subscription; 175 subscription.userId = 100; 176 subscription.id = "testId"; 177 subscription.expiresTime = testExpiresTime; 178 auto expire = subscription.GetMinExpireTime(); 179 EXPECT_EQ(expire, 3600); 180 } 181 182 /** 183 * @tc.name: GetMinExpireTime002 184 * @tc.desc: 185 * @tc.type: FUNC 186 * @tc.require: 187 * @tc.author: SQL 188 */ 189 HWTEST_F(SubscriptionTest, GetMinExpireTime002, TestSize.Level1) 190 { 191 Subscription subscription; 192 subscription.userId = 100; 193 subscription.id = "testId"; 194 std::map<std::string, uint64_t> testExpiresTimes = { { } }; 195 subscription.expiresTime = testExpiresTimes; 196 auto expire = subscription.GetMinExpireTime(); 197 EXPECT_EQ(expire, 0); 198 } 199 } // namespace OHOS::Test