4654c93f352405755b13f1e724a3868bd87cfc9b
[aai/model-loader.git] / src / test / java / org / openecomp / modelloader / util / JsonXmlConverterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * MODEL LOADER SERVICE
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.modelloader.util;
22
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
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 JsonXmlConverterTest {
39
40   @Test
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";
44
45     try {
46       byte[] encoded = Files.readAllBytes(Paths.get(XML_MODEL_FILE));
47       String originalXML = new String(encoded);
48
49       assertFalse(JsonXmlConverter.isValidJson(originalXML));
50
51       encoded = Files.readAllBytes(Paths.get(JSON_MODEL_FILE));
52       String originalJSON = new String(encoded);
53
54       assertTrue(JsonXmlConverter.isValidJson(originalJSON));
55
56       String xmlFromJson = JsonXmlConverter.convertJsonToXml(originalJSON);
57
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();
63
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();
69           break;
70         }
71       }
72
73       assertTrue(modelVid.equals("model-vid"));
74     } catch (Exception e) {
75       e.printStackTrace();
76       assertTrue(false);
77     }
78   }
79 }