Renaming openecomp to onap
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / util / JsonXmlConverterTest.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * org.onap.aai\r
4  * ================================================================================\r
5  * Copyright © 2017 AT&T Intellectual Property.\r
6  * Copyright © 2017 Amdocs\r
7  * All rights reserved.\r
8  * ================================================================================\r
9  * Licensed under the Apache License, Version 2.0 (the "License");\r
10  * you may not use this file except in compliance with the License.\r
11  * You may obtain a copy of the License at\r
12  *\r
13  *     http://www.apache.org/licenses/LICENSE-2.0\r
14  *\r
15  * Unless required by applicable law or agreed to in writing, software\r
16  * distributed under the License is distributed on an "AS IS" BASIS,\r
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
18  * See the License for the specific language governing permissions and\r
19  * limitations under the License.\r
20  * ============LICENSE_END=========================================================\r
21  *\r
22  * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
23  */\r
24 package org.onap.aai.modelloader.util;\r
25 \r
26 import static org.junit.Assert.assertFalse;\r
27 import static org.junit.Assert.assertTrue;\r
28 \r
29 import java.io.ByteArrayInputStream;\r
30 import java.nio.file.Files;\r
31 import java.nio.file.Paths;\r
32 \r
33 import javax.xml.parsers.DocumentBuilder;\r
34 import javax.xml.parsers.DocumentBuilderFactory;\r
35 \r
36 import org.junit.Test;\r
37 import org.onap.aai.modelloader.util.JsonXmlConverter;\r
38 import org.w3c.dom.Document;\r
39 import org.w3c.dom.Node;\r
40 import org.w3c.dom.NodeList;\r
41 \r
42 public class JsonXmlConverterTest {\r
43 \r
44   @Test\r
45   public void testConversion() throws Exception {\r
46         final String XML_MODEL_FILE = "src/test/resources/models/l3-network-widget.xml";\r
47         final String JSON_MODEL_FILE = "src/test/resources/models/l3-network-widget.json";\r
48 \r
49     try {\r
50       byte[] encoded = Files.readAllBytes(Paths.get(XML_MODEL_FILE));\r
51       String originalXML = new String(encoded);\r
52 \r
53       assertFalse(JsonXmlConverter.isValidJson(originalXML));\r
54 \r
55       encoded = Files.readAllBytes(Paths.get(JSON_MODEL_FILE));\r
56       String originalJSON = new String(encoded);\r
57 \r
58       assertTrue(JsonXmlConverter.isValidJson(originalJSON));\r
59 \r
60       String xmlFromJson = JsonXmlConverter.convertJsonToXml(originalJSON);\r
61 \r
62       // Spot check one of the attributes\r
63       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();\r
64       DocumentBuilder builder = factory.newDocumentBuilder();\r
65       Document doc = builder.parse(new ByteArrayInputStream(xmlFromJson.getBytes()));\r
66       NodeList nodeList = doc.getDocumentElement().getChildNodes();\r
67 \r
68       String modelVid = "notFound";\r
69       for (int i = 0; i < nodeList.getLength(); i++) {\r
70         Node currentNode = nodeList.item(i);\r
71         if (currentNode.getNodeName().equals("model-invariant-id")) {\r
72           modelVid = currentNode.getTextContent();\r
73           break;\r
74         }\r
75       }\r
76 \r
77       assertTrue(modelVid.equals("3d560d81-57d0-438b-a2a1-5334dba0651a"));\r
78     } catch (Exception e) {\r
79       e.printStackTrace();\r
80       assertTrue(false);\r
81     }\r
82   }\r
83 }\r