2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.modelloader.util;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
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;
36 public class JsonXmlConverterTest {
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";
44 byte[] encoded = Files.readAllBytes(Paths.get(XML_MODEL_FILE));
45 String originalXML = new String(encoded);
47 assertFalse(JsonXmlConverter.isValidJson(originalXML));
49 encoded = Files.readAllBytes(Paths.get(JSON_MODEL_FILE));
50 String originalJSON = new String(encoded);
52 assertTrue(JsonXmlConverter.isValidJson(originalJSON));
54 String xmlFromJson = JsonXmlConverter.convertJsonToXml(originalJSON);
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();
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();
72 assertTrue(modelVid.equals("3d560d81-57d0-438b-a2a1-5334dba0651a"));
73 } catch (Exception e) {