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