8ff0b0a3fbc3e112cd3cfc35b5f44fa18e6f903d
[ccsdk/sli/plugins.git] / restapi-call-node / provider / src / test / java / jtest / org / onap / ccsdk / sli / plugins / sshapicall / TestXmlParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
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
22 package jtest.org.onap.ccsdk.sli.plugins.sshapicall;
23
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.core.Is.is;
26
27 import java.io.BufferedReader;
28 import java.io.InputStreamReader;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Set;
35
36 import org.junit.Test;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
38 import org.onap.ccsdk.sli.plugins.restapicall.XmlParser;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class TestXmlParser {
43
44     private static final Logger log = LoggerFactory.getLogger(TestXmlParser.class);
45
46     @Test
47     public void test() throws Exception {
48         BufferedReader in = new BufferedReader(
49                 new InputStreamReader(ClassLoader.getSystemResourceAsStream("test3.xml"))
50         );
51         StringBuilder b = new StringBuilder();
52         String line;
53         while ((line = in.readLine()) != null)
54             b.append(line).append('\n');
55
56         Set<String> listNameList = new HashSet<String>();
57         listNameList.add("project.dependencies.dependency");
58         listNameList.add("project.build.plugins.plugin");
59         listNameList.add("project.build.plugins.plugin.executions.execution");
60         listNameList.add("project.build.pluginManagement.plugins.plugin");
61         listNameList.add("project.build.pluginManagement." +
62                         "plugins.plugin.configuration.lifecycleMappingMetadata.pluginExecutions.pluginExecution");
63
64         Map<String, String> mm = XmlParser.convertToProperties(b.toString(), listNameList);
65         logProperties(mm);
66         in.close();
67     }
68
69     @Test
70     public void testValidLength() throws Exception {
71         BufferedReader in = new BufferedReader(
72             new InputStreamReader(ClassLoader.getSystemResourceAsStream("test3.xml"))
73         );
74         StringBuilder b = new StringBuilder();
75         String line;
76         while ((line = in.readLine()) != null)
77             b.append(line).append('\n');
78
79         Set<String> listNameList = new HashSet<String>();
80         listNameList.add("ApplyGroupResponse.ApplyGroupResponseData.VrfDetails.VrfImport");
81         listNameList.add("ApplyGroupResponse.ApplyGroupResponseData.VrfDetails.VrfExport");
82
83         Map<String, String> mm = XmlParser.convertToProperties(b.toString(), listNameList);
84
85         assertThat(mm.get("ApplyGroupResponse.ApplyGroupResponseData.VrfDetails.VrfExport[5]"), is("SET_RESET_LP"));
86         assertThat(mm.get("ApplyGroupResponse.ApplyGroupResponseData.VrfDetails.VrfImport[0]"), is("SET_BVOIP_IN"));
87
88         logProperties(mm);
89         in.close();
90     }
91
92     @Test(expected = SvcLogicException.class)
93     public void testInvalidLength() throws Exception {
94         BufferedReader in = new BufferedReader(
95             new InputStreamReader(ClassLoader.getSystemResourceAsStream("invalidlength.xml"))
96         );
97         StringBuilder b = new StringBuilder();
98         String line;
99         while ((line = in.readLine()) != null)
100             b.append(line).append('\n');
101
102         Set<String> listNameList = new HashSet<String>();
103         listNameList.add("ApplyGroupResponse.ApplyGroupResponseData.VrfDetails.VrfImport");
104         listNameList.add("ApplyGroupResponse.ApplyGroupResponseData.VrfDetails.VrfExport");
105
106         Map<String, String> mm = XmlParser.convertToProperties(b.toString(), listNameList);
107         logProperties(mm);
108         in.close();
109     }
110
111     private void logProperties(Map<String, String> mm) {
112         List<String> ll = new ArrayList<>();
113         for (Object o : mm.keySet())
114             ll.add((String) o);
115         Collections.sort(ll);
116
117         log.info("Properties:");
118         for (String name : ll)
119             log.info("--- " + name + ": " + mm.get(name));
120     }
121 }