2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.generator.yang2json;
27 import com.fasterxml.jackson.databind.JsonNode;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.google.common.base.Optional;
30 import org.junit.Assert;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
37 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
38 import org.opendaylight.yangtools.yang.parser.repo.YangTextSchemaContextResolver;
39 import org.openecomp.appc.generator.JsonHelper;
42 import java.io.IOException;
43 import java.net.MalformedURLException;
47 public class Yang2JsonGeneratorTest {
49 private static SchemaContext yangSchema = null;
50 private static JsonNode jsonNode = null;
52 public void readIOfiles() throws IOException{
53 if(yangSchema == null){
62 public void testJsonRpcNum(){
64 int rpcYangNum = yangSchema.getModules().iterator().next().getRpcs().size();
65 int rpcJsonNum = jsonNode.get("paths").size();
66 Assert.assertTrue(rpcYangNum == rpcJsonNum);
69 public void testJsonRpcsAndInOutParams(){
71 Map<String/*rpcOperation*/,Set<String> /*bodyInputParams*/> jsonBodyInputParams = new HashMap();
72 Map<String/*rpcOperation*/,Set<String> /*bodyOutputParams*/> jsonBodyOutputParams= new HashMap();
73 JsonHelper.populateRpcInputOutputParamsFromJson(jsonNode,jsonBodyInputParams, jsonBodyOutputParams);
75 Set<RpcDefinition> rpcDefinitions = yangSchema.getModules().iterator().next().getRpcs();
76 for(RpcDefinition rpcDef : rpcDefinitions){
77 String rpcOperation = rpcDef.getQName().getLocalName();
78 //verify all yang rpc operations & all input yang params/groupings exist in yang2json
79 Assert.assertTrue(jsonBodyInputParams.containsKey(rpcOperation));
80 Collection<DataSchemaNode> inputChildNodes = rpcDef.getInput().getChildNodes();
81 if(inputChildNodes != null || !inputChildNodes.isEmpty()) {
82 Assert.assertTrue(rpcOperation+": yang and yang2json input params size is not equal!",jsonBodyInputParams.get(rpcOperation).size() == inputChildNodes.size());
83 Set<String> inputYangParams = getYangParams(inputChildNodes);
84 Assert.assertTrue(rpcOperation+": yang and yang2json input params are not same!",jsonBodyInputParams.get(rpcOperation).containsAll(inputYangParams));
87 //verify all yang rpc operations & all output yang params/groupings exist in yang2json
88 Assert.assertTrue(jsonBodyOutputParams.containsKey(rpcOperation));
89 Collection<DataSchemaNode> outputChildNodes = rpcDef.getOutput().getChildNodes();
90 if(outputChildNodes != null || !outputChildNodes.isEmpty()) {
91 Assert.assertTrue(rpcOperation+": yang and yang2json output params size is not equal!",jsonBodyOutputParams.get(rpcOperation).size()== outputChildNodes.size());
92 Set<String> outputYangParams = getYangParams(outputChildNodes);
93 Assert.assertTrue(rpcOperation+": yang and yang2json output params are not same!",jsonBodyOutputParams.get(rpcOperation).containsAll(outputYangParams));
99 private Set<String> getYangParams(Collection<DataSchemaNode> dataSchemaNodes) {
100 Set<String> yangParams = new HashSet();
101 for(DataSchemaNode child : dataSchemaNodes){
102 yangParams.add(child.getQName().getLocalName());
109 private static void yangParse(){
113 String yangFileName = System.getProperty("inputFile");
114 YangTextSchemaContextResolver yangContextResolver = null;
115 yangContextResolver = YangTextSchemaContextResolver.create("yang-context-resolver");
116 URL url = new File(yangFileName).toURI().toURL();
117 yangContextResolver.registerSource(url);
118 Optional<SchemaContext> yangSchemaContext = yangContextResolver.getSchemaContext();
119 yangSchema = yangSchemaContext.get();
120 } catch (MalformedURLException e) {
122 } catch (YangSyntaxErrorException e) {
124 } catch (IOException e) {
126 } catch (SchemaSourceException e) {
134 private static void jsonRead ()throws IOException {
135 String jsonFilePath = System.getProperty("outputFile");
136 File file = new File(jsonFilePath);
137 ObjectMapper mapper = new ObjectMapper();
138 jsonNode = mapper.readTree(file);