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 #include "event_json_parser_test.h"
16
17 #include <iostream>
18
19 #include "event_json_parser.h"
20 #include "flat_json_parser.h"
21 #include "hiview_platform.h"
22
23 namespace OHOS {
24 namespace HiviewDFX {
SetUpTestCase()25 void EventJsonParserTest::SetUpTestCase() {}
26
TearDownTestCase()27 void EventJsonParserTest::TearDownTestCase() {}
28
SetUp()29 void EventJsonParserTest::SetUp()
30 {
31 HiviewPlatform &platform = HiviewPlatform::GetInstance();
32 if (!platform.InitEnvironment()) {
33 std::cout << "fail to init environment" << std::endl;
34 } else {
35 std::cout << "init environment successful" << std::endl;
36 }
37 }
38
TearDown()39 void EventJsonParserTest::TearDown() {}
40
41 /**
42 * @tc.name: EventJsonParserTest001
43 * @tc.desc: parse a event and check Json info
44 * @tc.type: FUNC
45 * @tc.require: issueI62WJT
46 */
47 HWTEST_F(EventJsonParserTest, EventJsonParserTest001, testing::ext::TestSize.Level0)
48 {
49 printf("start EventJsonParserTest001\n");
50 constexpr char JSON_STR[] = "{\"domain_\":\"HIVIEWDFX\",\"name_\":\"PLUGIN_LOAD\",\"type_\":4,\
51 \"PARAM_A\":\"param a\",\"PARAM_B\":\"param b\"}";
52 auto sysEvent = std::make_shared<SysEvent>("SysEventService", nullptr, JSON_STR);
53 std::string defFile = "/system/etc/hiview/hisysevent.def";
54 std::vector<std::string> defFiles;
55 defFiles.emplace_back(defFile);
56 auto sysEventParser = std::make_unique<EventJsonParser>(defFiles);
57 ASSERT_TRUE(sysEventParser->HandleEventJson(sysEvent));
58 ASSERT_TRUE(sysEventParser->GetTagByDomainAndName("abc", "abc") == "");
59 ASSERT_TRUE(sysEventParser->GetTagByDomainAndName("DEMO", "abc") == "");
60 ASSERT_TRUE(sysEventParser->GetTypeByDomainAndName("DEMO", "abc") == 0);
61
62 DuplicateIdFilter filter;
63 ASSERT_FALSE(filter.IsDuplicateEvent(0));
64 ASSERT_FALSE(filter.IsDuplicateEvent(1));
65 ASSERT_TRUE(filter.IsDuplicateEvent(1));
66
67 FlatJsonParser parser("{\"level\":[abc]}");
68 std::cout << "FlatJsonParser:" << parser.Print() << std::endl;
69 ASSERT_TRUE(parser.Print() == "{\"level\":[abc]}");
70 }
71 } // namespace HiviewDFX
72 } // namespace OHOS
73