More license header changes to appc-client files
[appc.git] / appc-client / client-kit / src / test / java / org / onap / appc / generator / yang2json / Yang2JsonGeneratorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.generator.yang2json;
25
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;
39
40 import java.io.File;
41 import java.io.IOException;
42 import java.net.MalformedURLException;
43 import java.net.URL;
44 import java.util.*;
45
46 public class Yang2JsonGeneratorTest {
47
48     private static SchemaContext yangSchema = null;
49     private static JsonNode jsonNode = null;
50
51     public void readIOfiles() throws IOException{
52         if(yangSchema == null){
53             yangParse();
54         }
55         if(jsonNode == null){
56             jsonRead();
57         }
58
59     }
60
61     public void testJsonRpcNum(){
62
63         int rpcYangNum = yangSchema.getModules().iterator().next().getRpcs().size();
64         int rpcJsonNum = jsonNode.get("paths").size();
65         Assert.assertTrue(rpcYangNum == rpcJsonNum);
66     }
67
68     public void testJsonRpcsAndInOutParams(){
69
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);
73
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));
84             }
85
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));
93             }
94         }
95     }
96
97
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());
102         }
103         return yangParams;
104     }
105
106
107
108     private static void yangParse(){
109
110         try {
111             //sourceFileName
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) {
120             e.printStackTrace();
121         } catch (YangSyntaxErrorException e) {
122             e.printStackTrace();
123         } catch (IOException e) {
124             e.printStackTrace();
125         } catch (SchemaSourceException e) {
126             e.printStackTrace();
127         }
128
129
130     }
131
132
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);
138     }
139 }