6bad0c6c29e43d7251dc27c7ab865b32685ff163
[so.git] / bpmn / MSOCoreBPMN / src / test / java / org / onap / so / bpmn / core / xml / XmlToolTest.java
1 /*
2 * ============LICENSE_START=======================================================
3  * ONAP : SO
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19 */
20
21 package org.onap.so.bpmn.core.xml;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25
26 import java.util.Optional;
27
28 import org.junit.Test;
29
30 public class XmlToolTest {
31
32         private String response = "<Response>good</Response>";
33         private String responseModified = "<Response>veryGood</Response>";
34         private String encodeResponse = "&lt;Response&gt;good&lt;/Response&gt;";
35         private String encodeResponseNamespace = "<Response xmlns:ns2=\"http://ecomp.att.com/mso/request/types/v1\">good</Response>";
36         private String attribute  = "<Response>good&\"bad\"</Response>";
37         private String updatedAttribute = "&lt;Response&gt;good&amp;&quot;bad&quot;&lt;/Response&gt;";
38
39         private String content = "<dummy><configuration-event>" +
40                         "<event-type>test</event-type>" +
41                         "<event-correlator-type>test</event-correlator-type>" +
42                         "<event-correlator>123</event-correlator>" +
43                         "<event-parameters><event-parameter>" +
44                         "<tag-name>test</tag-name>" +
45                         "<tag-value>test</tag-value></event-parameter></event-parameters>" +
46                         "</configuration-event></dummy>";
47         
48         @Test
49         public void test() throws Exception {
50                 Object xmlMessage = new String(response);
51                 String xmlResponse = XmlTool.normalize(xmlMessage);
52                 assertEquals(response, xmlResponse.toString());
53                 String xmlEncode = XmlTool.encode(xmlMessage);
54                 assertEquals(encodeResponse, xmlEncode.toString());
55                 Optional<String> optXml = XmlTool.modifyElement(response, "Response", "veryGood");
56                 Object obj1 = new String(optXml.get().toString());
57                 String noPreamble = XmlTool.removePreamble(obj1);
58                 assertEquals(responseModified, noPreamble.toString());
59                 Object obj2 = new String(encodeResponseNamespace);
60                 String noNamespace = XmlTool.removeNamespaces(obj2);
61                 assertEquals(response, noNamespace.toString());
62                 Object obj3 = new String(attribute);
63                 
64                 assertEquals(null, XmlTool.normalize(null));
65                 assertEquals(null, XmlTool.encode(null));
66                 assertEquals(null, XmlTool.removePreamble(null));
67                 assertEquals(null, XmlTool.removeNamespaces(null));
68                 assertEquals(Optional.empty(), XmlTool.modifyElement(null, "Response", "veryGood"));
69         }
70
71         @Test
72         public void normalizeTest() throws Exception {
73                 String response = XmlTool.normalize(content);
74                 assertNotNull(response);
75         }
76
77         @Test
78         public void modifyElementTest() throws Exception {
79                 String response = XmlTool.modifyElement(content, "event-type", "uCPE-VMS").get();
80                 assertNotNull(response);
81         }
82 }