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 #define LOG_TAG "CustomUtdJsonParserTest"
16 #include <gtest/gtest.h>
17 
18 #include <unistd.h>
19 
20 #include "logger.h"
21 #include "utd_common.h"
22 #include "custom_utd_json_parser.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS::UDMF;
26 using namespace OHOS;
27 namespace OHOS::Test {
28 constexpr const char* TEST_DATA1 = "{\
29     \"UniformDataTypeDeclarations\": [{\
30     \"typeId\": \"com.example.utdtest.document\",\
31     \"belongingToTypes\": [\"com.example.utdtest2.document\"],\
32     \"FilenameExtensions\": [\".mydocument\", \".mydoc\"],\
33     \"mimeTypes\": [\"application/my-document\", \"application/my-doc\"],\
34     \"description\": \"My document.\",\
35     \"referenceURL\": \"http://www.mycompany.com/my-document.html\",\
36     \"iconFile\": \"resources/my-document.png\"\
37     }],\
38     \"ReferenceUniformDataTypeDeclarations\": [{\
39     \"typeId\": \"com.example.utdtest2.document\",\
40     \"belongingToTypes\": [\"general.object\"],\
41     \"FilenameExtensions\": [\".mydocument2\", \".mydoc2\"],\
42     \"mimeTypes\": [\"application/my-document2\", \"application/my-doc2\"],\
43     \"description\": \"My document 2.\",\
44     \"referenceURL\": \"http://www.mycompany.com/my-document2.html\",\
45     \"iconFile\": \"resources/my-document2.png\"\
46     }]}";
47 
48 class CustomUtdJsonParserTest : public testing::Test {
49 public:
50     static void SetUpTestCase();
51     static void TearDownTestCase();
52     void SetUp() override;
53     void TearDown() override;
54 };
55 
SetUpTestCase()56 void CustomUtdJsonParserTest::SetUpTestCase()
57 {
58 }
59 
TearDownTestCase()60 void CustomUtdJsonParserTest::TearDownTestCase()
61 {
62 }
63 
SetUp()64 void CustomUtdJsonParserTest::SetUp()
65 {
66 }
67 
TearDown()68 void CustomUtdJsonParserTest::TearDown()
69 {
70 }
71 
72 /**
73 * @tc.name: ParseJsonData001
74 * @tc.desc: ParseJson
75 * @tc.type: FUNC
76 */
77 HWTEST_F(CustomUtdJsonParserTest, ParseJsonData001, TestSize.Level1)
78 {
79     LOG_INFO(UDMF_TEST, "ParseJsonData001 begin.");
80     std::vector<TypeDescriptorCfg> typesCfg1;
81     std::vector<TypeDescriptorCfg> typesCfg2;
82     CustomUtdJsonParser parser;
83     parser.ParseUserCustomUtdJson(TEST_DATA1, typesCfg1, typesCfg2);
84     TypeDescriptorCfg type1 = *(typesCfg1.begin());
85 
86     EXPECT_EQ(type1.typeId, "com.example.utdtest.document");
87     EXPECT_EQ(*(type1.belongingToTypes.begin()), "com.example.utdtest2.document");
88     EXPECT_EQ(*(type1.filenameExtensions.begin()), ".mydocument");
89     EXPECT_EQ(*(type1.mimeTypes.begin()), "application/my-document");
90     EXPECT_EQ(type1.description, "My document.");
91     EXPECT_EQ(type1.referenceURL, "http://www.mycompany.com/my-document.html");
92     EXPECT_EQ(type1.iconFile, "resources/my-document.png");
93 
94     TypeDescriptorCfg type2 = *(typesCfg2.begin());
95     EXPECT_EQ(type2.typeId, "com.example.utdtest2.document");
96     EXPECT_EQ(*(type2.belongingToTypes.begin()), "general.object");
97     EXPECT_EQ(*(type2.filenameExtensions.begin()), ".mydocument2");
98     EXPECT_EQ(*(type2.mimeTypes.begin()), "application/my-document2");
99     EXPECT_EQ(type2.description, "My document 2.");
100     EXPECT_EQ(type2.referenceURL, "http://www.mycompany.com/my-document2.html");
101     EXPECT_EQ(type2.iconFile, "resources/my-document2.png");
102 
103     LOG_INFO(UDMF_TEST, "ParseJsonData001 end.");
104 }
105 } // OHOS::Test