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