2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
24 package org.onap.appc.generator.yang2json;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.google.common.base.Optional;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
36 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
37 import org.opendaylight.yangtools.yang.parser.repo.YangTextSchemaContextResolver;
38 import org.onap.appc.generator.JsonHelper;
41 import java.io.IOException;
42 import java.net.MalformedURLException;
46 public class Yang2JsonGeneratorTest {
48 private static SchemaContext yangSchema = null;
49 private static JsonNode jsonNode = null;
51 public void readIOfiles() throws IOException{
52 if(yangSchema == null){
61 public void testJsonRpcNum(){
63 int rpcYangNum = yangSchema.getModules().iterator().next().getRpcs().size();
64 int rpcJsonNum = jsonNode.get("paths").size();
65 Assert.assertTrue(rpcYangNum == rpcJsonNum);
68 public void testJsonRpcsAndInOutParams(){
70 Map<String/*rpcOperation*/,Set<String> /*bodyInputParams*/> jsonBodyInputParams = new HashMap();
71 Map<String/*rpcOperation*/,Set<String> /*bodyOutputParams*/> jsonBodyOutputParams= new HashMap();
72 JsonHelper.populateRpcInputOutputParamsFromJson(jsonNode,jsonBodyInputParams, jsonBodyOutputParams);
74 Set<RpcDefinition> rpcDefinitions = yangSchema.getModules().iterator().next().getRpcs();
75 for(RpcDefinition rpcDef : rpcDefinitions){
76 String rpcOperation = rpcDef.getQName().getLocalName();
77 //verify all yang rpc operations & all input yang params/groupings exist in yang2json
78 Assert.assertTrue(jsonBodyInputParams.containsKey(rpcOperation));
79 Collection<DataSchemaNode> inputChildNodes = rpcDef.getInput().getChildNodes();
80 if(inputChildNodes != null || !inputChildNodes.isEmpty()) {
81 Assert.assertTrue(rpcOperation+": yang and yang2json input params size is not equal!",jsonBodyInputParams.get(rpcOperation).size() == inputChildNodes.size());
82 Set<String> inputYangParams = getYangParams(inputChildNodes);
83 Assert.assertTrue(rpcOperation+": yang and yang2json input params are not same!",jsonBodyInputParams.get(rpcOperation).containsAll(inputYangParams));
86 //verify all yang rpc operations & all output yang params/groupings exist in yang2json
87 Assert.assertTrue(jsonBodyOutputParams.containsKey(rpcOperation));
88 Collection<DataSchemaNode> outputChildNodes = rpcDef.getOutput().getChildNodes();
89 if(outputChildNodes != null || !outputChildNodes.isEmpty()) {
90 Assert.assertTrue(rpcOperation+": yang and yang2json output params size is not equal!",jsonBodyOutputParams.get(rpcOperation).size()== outputChildNodes.size());
91 Set<String> outputYangParams = getYangParams(outputChildNodes);
92 Assert.assertTrue(rpcOperation+": yang and yang2json output params are not same!",jsonBodyOutputParams.get(rpcOperation).containsAll(outputYangParams));
98 private Set<String> getYangParams(Collection<DataSchemaNode> dataSchemaNodes) {
99 Set<String> yangParams = new HashSet();
100 for(DataSchemaNode child : dataSchemaNodes){
101 yangParams.add(child.getQName().getLocalName());
108 private static void yangParse(){
112 String yangFileName = System.getProperty("inputFile");
113 YangTextSchemaContextResolver yangContextResolver = null;
114 yangContextResolver = YangTextSchemaContextResolver.create("yang-context-resolver");
115 URL url = new File(yangFileName).toURI().toURL();
116 yangContextResolver.registerSource(url);
117 Optional<SchemaContext> yangSchemaContext = yangContextResolver.getSchemaContext();
118 yangSchema = yangSchemaContext.get();
119 } catch (MalformedURLException e) {
121 } catch (YangSyntaxErrorException e) {
123 } catch (IOException e) {
125 } catch (SchemaSourceException e) {
133 private static void jsonRead ()throws IOException {
134 String jsonFilePath = System.getProperty("outputFile");
135 File file = new File(jsonFilePath);
136 ObjectMapper mapper = new ObjectMapper();
137 jsonNode = mapper.readTree(file);