2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.core.xml;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
26 import java.util.Optional;
28 import org.junit.Test;
30 public class XmlToolTest {
32 private String response = "<Response>good</Response>";
33 private String responseModified = "<Response>veryGood</Response>";
34 private String encodeResponse = "<Response>good</Response>";
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 = "<Response>good&"bad"</Response>";
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>";
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);
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"));
72 public void normalizeTest() throws Exception {
73 String response = XmlTool.normalize(content);
74 assertNotNull(response);
78 public void modifyElementTest() throws Exception {
79 String response = XmlTool.modifyElement(content, "event-type", "uCPE-VMS").get();
80 assertNotNull(response);