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