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
16use crate::common::*;
17use asset_sdk::*;
18
19#[test]
20fn update_same_secret() {
21    let alias = function!().as_bytes();
22    let secret = function!().as_bytes();
23    add_default_asset(alias, secret).unwrap();
24
25    let mut query = AssetMap::new();
26    query.insert_attr(Tag::Alias, alias.to_owned());
27
28    let mut update = AssetMap::new();
29    update.insert_attr(Tag::Secret, secret.to_owned());
30
31    asset_sdk::Manager::build().unwrap().update(&query, &update).unwrap();
32
33    remove_by_alias(alias).unwrap();
34}
35
36#[test]
37fn update_different_secret() {
38    let alias = function!().as_bytes();
39    let secret = function!().as_bytes();
40    add_default_asset(alias, secret).unwrap();
41
42    let mut query = AssetMap::new();
43    query.insert_attr(Tag::Alias, alias.to_owned());
44
45    let secret_new = "update_different_secret_new".as_bytes();
46
47    let mut update = AssetMap::new();
48    update.insert_attr(Tag::Secret, secret_new.to_owned());
49
50    asset_sdk::Manager::build().unwrap().update(&query, &update).unwrap();
51
52    let res = query_all_by_alias(alias).unwrap();
53    assert_eq!(1, res.len());
54    assert_eq!(secret_new, res[0].get_bytes_attr(&Tag::Secret).unwrap());
55
56    remove_by_alias(alias).unwrap();
57}
58
59#[test]
60fn update_attr_normal() {
61    let alias = function!().as_bytes();
62    let secret = function!().as_bytes();
63    add_default_asset(alias, secret).unwrap();
64
65    let mut query = AssetMap::new();
66    query.insert_attr(Tag::Alias, alias.to_owned());
67
68    let label_normal = "update_attr_normal".as_bytes();
69    let mut update = AssetMap::new();
70    update.insert_attr(Tag::DataLabelNormal1, label_normal.to_owned());
71
72    asset_sdk::Manager::build().unwrap().update(&query, &update).unwrap();
73    let query_res = &query_attr_by_alias(alias).unwrap()[0];
74    let label_normal_query = query_res.get_bytes_attr(&Tag::DataLabelNormal1).unwrap();
75    assert_eq!(label_normal, label_normal_query);
76
77    remove_by_alias(alias).unwrap();
78}
79
80#[test]
81fn update_non_exist() {
82    let alias = function!().as_bytes();
83    let label_normal = function!().as_bytes();
84
85    let mut query = AssetMap::new();
86    query.insert_attr(Tag::Alias, alias.to_owned());
87
88    let mut update = AssetMap::new();
89    update.insert_attr(Tag::DataLabelNormal1, label_normal.to_owned());
90
91    expect_error_eq(ErrCode::NotFound, asset_sdk::Manager::build().unwrap().update(&query, &update).unwrap_err());
92}
93