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 "cloud/subscription.h" 17 18 #include <gtest/gtest.h> 19 20 using namespace testing::ext; 21 using namespace OHOS::DistributedData; 22 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 } }; 27 }; 28 29 /** 30 * @tc.name: RelationMarshal 31 * @tc.desc: relation marshal. 32 * @tc.type: FUNC 33 */ 34 HWTEST_F(SubscriptionTest, RelationMarshal, TestSize.Level1) 35 { 36 Subscription::Relation relation; 37 relation.id = "testId"; 38 relation.bundleName = "testBundleName"; 39 relation.relations = testRelation; 40 Subscription::json node; 41 relation.Marshal(node); 42 ASSERT_EQ(node["id"], "testId"); 43 ASSERT_EQ(node["bundleName"], "testBundleName"); 44 ASSERT_EQ(node["relations"], testRelation); 45 } 46 47 /** 48 * @tc.name: RelationUnmarshal 49 * @tc.desc: relation unmarshal. 50 * @tc.type: FUNC 51 */ 52 HWTEST_F(SubscriptionTest, RelationUnmarshal, TestSize.Level1) 53 { 54 Subscription::json node; 55 node["id"] = "testId"; 56 node["bundleName"] = "testBundleName"; 57 node["relations"] = testRelation; 58 Subscription::Relation relation; 59 relation.Unmarshal(node); 60 ASSERT_EQ(relation.id, "testId"); 61 ASSERT_EQ(relation.bundleName, "testBundleName"); 62 ASSERT_EQ(relation.relations, testRelation); 63 } 64 65 /** 66 * @tc.name: Marshal 67 * @tc.desc: marshal. 68 * @tc.type: FUNC 69 */ 70 HWTEST_F(SubscriptionTest, Marshal, TestSize.Level1) 71 { 72 Subscription subscription; 73 subscription.userId = 100; 74 subscription.id = "testId"; 75 subscription.expiresTime = testExpiresTime; 76 Subscription::json node; 77 subscription.Marshal(node); 78 ASSERT_EQ(node["userId"], 100); 79 ASSERT_EQ(node["id"], "testId"); 80 ASSERT_EQ(node["expiresTime"], testExpiresTime); 81 } 82 83 /** 84 * @tc.name: Unmarshal 85 * @tc.desc: unmarshal. 86 * @tc.type: FUNC 87 */ 88 HWTEST_F(SubscriptionTest, Unmarshal, TestSize.Level1) 89 { 90 Subscription::json node; 91 node["userId"] = 100; 92 node["id"] = "testId"; 93 node["expiresTime"] = testExpiresTime; 94 Subscription subscription; 95 subscription.Unmarshal(node); 96 ASSERT_EQ(subscription.userId, 100); 97 ASSERT_EQ(subscription.id, "testId"); 98 ASSERT_EQ(subscription.expiresTime, testExpiresTime); 99 } 100 101 /** 102 * @tc.name: GetKey 103 * @tc.desc: get key. 104 * @tc.type: FUNC 105 */ 106 HWTEST_F(SubscriptionTest, GetKey, TestSize.Level1) 107 { 108 Subscription subscription; 109 subscription.userId = 100; 110 std::string key = subscription.GetKey(); 111 ASSERT_EQ(key, "CLOUD_SUBSCRIPTION###100"); 112 } 113 114 /** 115 * @tc.name: GetRelationKey 116 * @tc.desc: get relation key. 117 * @tc.type: FUNC 118 */ 119 HWTEST_F(SubscriptionTest, GetRelationKey, TestSize.Level1) 120 { 121 Subscription subscription; 122 subscription.userId = 100; 123 std::string relationKey = subscription.GetRelationKey("testBundleName"); 124 ASSERT_EQ(relationKey, "CLOUD_RELATION###100###testBundleName"); 125 } 126 127 /** 128 * @tc.name: GetKey002 129 * @tc.desc: get key. 130 * @tc.type: FUNC 131 */ 132 HWTEST_F(SubscriptionTest, GetKey002, TestSize.Level1) 133 { 134 std::string key = Subscription::GetKey(100); 135 ASSERT_EQ(key, "CLOUD_SUBSCRIPTION###100"); 136 } 137 138 /** 139 * @tc.name: GetRelationKey002 140 * @tc.desc: get relation key. 141 * @tc.type: FUNC 142 */ 143 HWTEST_F(SubscriptionTest, GetRelationKey002, TestSize.Level1) 144 { 145 std::string relationKey = Subscription::GetRelationKey(100, "testBundleName"); 146 ASSERT_EQ(relationKey, "CLOUD_RELATION###100###testBundleName"); 147 } 148 149 /** 150 * @tc.name: GetPrefix 151 * @tc.desc: get prefix. 152 * @tc.type: FUNC 153 */ 154 HWTEST_F(SubscriptionTest, GetPrefix, TestSize.Level1) 155 { 156 std::initializer_list<std::string> fields = { "field1", "field2" }; 157 std::string prefix = Subscription::GetPrefix(fields); 158 ASSERT_EQ(prefix, "CLOUD_SUBSCRIPTION###field1###field2"); 159 }