b6870f90ef8b4f71c8edb82dddef92c73df140ce
[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 \r
30 import javax.xml.parsers.DocumentBuilder;\r
31 import javax.xml.parsers.DocumentBuilderFactory;\r
32 \r
33 import org.junit.Test;\r
34 import org.onap.aai.modelloader.util.JsonXmlConverter;\r
35 import org.w3c.dom.Document;\r
36 import org.w3c.dom.Node;\r
37 import org.w3c.dom.NodeList;\r
38 \r
39 public class JsonXmlConverterTest {\r
40 \r
41   @Test\r
42   public void testConversion() throws Exception {\r
43         final String XML_MODEL_FILE = "src/test/resources/models/l3-network-widget.xml";\r
44         final String JSON_MODEL_FILE = "src/test/resources/models/l3-network-widget.json";\r
45 \r
46     try {\r
47       byte[] encoded = Files.readAllBytes(Paths.get(XML_MODEL_FILE));\r
48       String originalXML = new String(encoded);\r
49 \r
50       assertFalse(JsonXmlConverter.isValidJson(originalXML));\r
51 \r
52       encoded = Files.readAllBytes(Paths.get(JSON_MODEL_FILE));\r
53       String originalJSON = new String(encoded);\r
54 \r
55       assertTrue(JsonXmlConverter.isValidJson(originalJSON));\r
56 \r
57       String xmlFromJson = JsonXmlConverter.convertJsonToXml(originalJSON);\r
58 \r
59       // Spot check one of the attributes\r
60       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();\r
61       DocumentBuilder builder = factory.newDocumentBuilder();\r
62       Document doc = builder.parse(new ByteArrayInputStream(xmlFromJson.getBytes()));\r
63       NodeList nodeList = doc.getDocumentElement().getChildNodes();\r
64 \r
65       String modelVid = "notFound";\r
66       for (int i = 0; i < nodeList.getLength(); i++) {\r
67         Node currentNode = nodeList.item(i);\r
68         if (currentNode.getNodeName().equals("model-invariant-id")) {\r
69           modelVid = currentNode.getTextContent();\r
70           break;\r
71         }\r
72       }\r
73 \r
74       assertTrue(modelVid.equals("3d560d81-57d0-438b-a2a1-5334dba0651a"));\r
75     } catch (Exception e) {\r
76       e.printStackTrace();\r
77       assertTrue(false);\r
78     }\r
79   }\r
80 }\r