2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.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;
30 import javax.xml.parsers.DocumentBuilder;
31 import javax.xml.parsers.DocumentBuilderFactory;
33 import org.junit.Test;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Node;
36 import org.w3c.dom.NodeList;
38 public class JsonXmlConverterTest {
41 public void testConversion() throws Exception {
42 final String XML_MODEL_FILE = "src/test/resources/models/vnf-model.xml";
43 final String JSON_MODEL_FILE = "src/test/resources/models/vnf-model.json";
46 byte[] encoded = Files.readAllBytes(Paths.get(XML_MODEL_FILE));
47 String originalXML = new String(encoded);
49 assertFalse(JsonXmlConverter.isValidJson(originalXML));
51 encoded = Files.readAllBytes(Paths.get(JSON_MODEL_FILE));
52 String originalJSON = new String(encoded);
54 assertTrue(JsonXmlConverter.isValidJson(originalJSON));
56 String xmlFromJson = JsonXmlConverter.convertJsonToXml(originalJSON);
58 // Spot check one of the attributes
59 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
60 DocumentBuilder builder = factory.newDocumentBuilder();
61 Document doc = builder.parse(new ByteArrayInputStream(xmlFromJson.getBytes()));
62 NodeList nodeList = doc.getDocumentElement().getChildNodes();
64 String modelVid = "notFound";
65 for (int i = 0; i < nodeList.getLength(); i++) {
66 Node currentNode = nodeList.item(i);
67 if (currentNode.getNodeName().equals("model-name-version-id")) {
68 modelVid = currentNode.getTextContent();
73 assertTrue(modelVid.equals("model-vid"));
74 } catch (Exception e) {