9f12c99d6149671272c1b48037746f678536b9f5
[nonrtric/plt/ranpm.git] / pm-file-converter / components / xmltransform / xmltransform_test.go
1 // -
2 //
3 //      ========================LICENSE_START=================================
4 //      O-RAN-SC
5 //      %%
6 //      Copyright (C) 2022: Nordix Foundation
7 //      %%
8 //      Licensed under the Apache License, Version 2.0 (the "License");
9 //      you may not use this file except in compliance with the License.
10 //      You may obtain a copy of the License at
11 //
12 //           http://www.apache.org/licenses/LICENSE-2.0
13 //
14 //      Unless required by applicable law or agreed to in writing, software
15 //      distributed under the License is distributed on an "AS IS" BASIS,
16 //      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 //      See the License for the specific language governing permissions and
18 //      limitations under the License.
19 //      ========================LICENSE_END===================================
20
21 package xmltransform
22
23 import (
24         "bytes"
25         "fmt"
26         "io"
27         "main/common/dataTypes"
28         "os"
29         "testing"
30 )
31
32 func TestXMLToJSONConv_Success(t *testing.T) {
33         // Prepare the input data
34         var evt_data dataTypes.XmlFileEventHeader = dataTypes.XmlFileEventHeader{
35                 ProductName:       "O-RAN SC",
36                 VendorName:        "Ericsson AB",
37                 Location:          "Ireland",
38                 Compression:       "gzip",
39                 SourceName:        "GNODEB-0",
40                 FileFormatType:    "xml",
41                 FileFormatVersion: "1.0",
42                 Name:              "Sample Event",
43                 ChangeIdentifier:  "Sample Change ID",
44                 InternalLocation:  "Sample Internal Location",
45                 TimeZoneOffset:    "+05:00",
46                 ObjectStoreBucket: "Sample Bucket",
47         }
48
49         filename := "A20230515.0700_0100-0715_0100_GNODEB-0.xml"
50         fi, err := os.Open(filename)
51
52         if err != nil {
53                 t.Fatalf("File %s - cannot be opened  - discarding message, error details: %s", filename, err.Error())
54         }
55         defer fi.Close()
56
57         reader := fi
58
59         var buf3 bytes.Buffer
60         _, err2 := io.Copy(&buf3, reader)
61         if err2 != nil {
62                 t.Fatalf("File %s - cannot be read, discarding message, %s", filename, err.Error())
63                 return
64         }
65         file_bytes := buf3.Bytes()
66         b, err := xml_to_json_conv(&file_bytes, &evt_data)
67
68         json_filename := "A20230515.0700_0100-0715_0100_GNODEB-0.json"
69
70         err3 := os.WriteFile(json_filename, b, 0644)
71
72         if err3 != nil {
73                 t.Fatalf("Error writing %s to JSON :, %s ", json_filename, err3.Error())
74         }
75
76         if err != nil {
77                 t.Fatalf("Cannot convert file %s - discarding message, %s", filename, err.Error())
78         }
79
80         if len(b) <= 100000 {
81                 t.Fatalf("Conversion of %s returned %d bytes", filename, len(b))
82         }
83
84         fi, err = os.Open(json_filename)
85
86         if err != nil {
87                 t.Fatalf("File %s - cannot be opened  - discarding message, error details: %s", json_filename, err.Error())
88         } else {
89                 fmt.Println("XML file Converted to JSON")
90         }
91         defer fi.Close()
92 }