Update babel dependency in model-loader
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / util / TestJsonXmlConverter.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.hamcrest.CoreMatchers.is;
24 import static org.hamcrest.MatcherAssert.assertThat;
25
26 import java.io.ByteArrayInputStream;
27 import java.nio.file.Files;
28 import java.nio.file.Paths;
29
30 import javax.xml.parsers.DocumentBuilder;
31 import javax.xml.parsers.DocumentBuilderFactory;
32
33 import org.junit.Test;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Node;
36 import org.w3c.dom.NodeList;
37
38 public class TestJsonXmlConverter {
39
40     private static final String XML_MODEL_FILE = "src/test/resources/models/l3-network-widget.xml";
41     private static final String JSON_MODEL_FILE = "src/test/resources/models/l3-network-widget.json";
42
43     @Test
44     public void testJsonIsValid() {
45         assertThat(JsonXmlConverter.isValidJson("{}"), is(true));
46         assertThat(JsonXmlConverter.isValidJson("[]"), is(true));
47
48         assertThat(JsonXmlConverter.isValidJson("{"), is(false));
49         assertThat(JsonXmlConverter.isValidJson("["), is(false));
50     }
51
52     @Test
53     public void testConversion() throws Exception {
54         byte[] encoded = Files.readAllBytes(Paths.get(XML_MODEL_FILE));
55         assertThat(JsonXmlConverter.isValidJson(new String(encoded)), is(false));
56         encoded = Files.readAllBytes(Paths.get(JSON_MODEL_FILE));
57         String originalJson = new String(encoded);
58
59         assertThat(JsonXmlConverter.isValidJson(originalJson), is(true));
60
61         String xmlFromJson = JsonXmlConverter.convertJsonToXml(originalJson);
62
63         // Spot check one of the attributes
64         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
65         factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
66         DocumentBuilder builder = factory.newDocumentBuilder();
67         Document doc = builder.parse(new ByteArrayInputStream(xmlFromJson.getBytes()));
68         NodeList nodeList = doc.getDocumentElement().getChildNodes();
69
70         String modelVid = "notFound";
71         for (int i = 0; i < nodeList.getLength(); i++) {
72             Node currentNode = nodeList.item(i);
73             if (currentNode.getNodeName().equals("model-invariant-id")) {
74                 modelVid = currentNode.getTextContent();
75                 break;
76             }
77         }
78
79         assertThat(modelVid.equals("3d560d81-57d0-438b-a2a1-5334dba0651a"), is(true));
80
81         // Convert the XML form back into JSON
82         JsonXmlConverter.convertXmlToJson(xmlFromJson);
83     }
84 }