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