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