Merge "Automation of nodeb health check"
[ric-plt/e2mgr.git] / E2Manager / utils / xml_utils.go
1 //
2 // Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 //  platform project (RICP).
18
19 package utils
20
21 import (
22         "fmt"
23         "io/ioutil"
24         "path/filepath"
25         "strings"
26         "testing"
27 )
28
29 func CleanXML(payload []byte) []byte {
30         xmlStr := string(payload)
31         normalized := strings.NewReplacer("\r","","\n",""," ","").Replace(xmlStr)
32
33         return []byte(normalized)
34 }
35
36 func ReadXmlFile(t *testing.T, xmlPath string) []byte {
37         path, err := filepath.Abs(xmlPath)
38         if err != nil {
39                 t.Fatal(err)
40         }
41         xmlAsBytes, err := ioutil.ReadFile(path)
42         if err != nil {
43                 t.Fatal(err)
44         }
45
46         return xmlAsBytes
47 }
48
49 func ReadXmlFileNoTest(xmlPath string) ([]byte, error) {
50         path, err := filepath.Abs(xmlPath)
51         if err != nil {
52                 return nil,err
53         }
54         xmlAsBytes, err := ioutil.ReadFile(path)
55         if err != nil {
56                 return nil,err
57         }
58
59         return xmlAsBytes,nil
60 }
61
62 func NormalizeXml(payload []byte) []byte {
63         xmlStr := string(payload)
64         normalized := strings.NewReplacer("&lt;", "<", "&gt;", ">").Replace(xmlStr)
65
66         return []byte(normalized)
67 }
68
69 func ReplaceEmptyTagsWithSelfClosing(responsePayload []byte, emptyTagsToReplace []string) []byte {
70         emptyTagVsSelfClosingTagPairs := make([]string, len(emptyTagsToReplace)*2)
71         j := 0
72
73         for i := 0; i < len(emptyTagsToReplace); i++ {
74                 emptyTagVsSelfClosingTagPairs[j] = fmt.Sprintf("<%[1]s></%[1]s>", emptyTagsToReplace[i])
75                 emptyTagVsSelfClosingTagPairs[j+1] = fmt.Sprintf("<%s/>", emptyTagsToReplace[i])
76                 j += 2
77         }
78         responseString := strings.NewReplacer(emptyTagVsSelfClosingTagPairs...).Replace(string(responsePayload))
79         return []byte(responseString)
80 }