2e8ef9577bb09a653664eb9469654a13da40af99
[appc.git] / appc-client / client-kit / src / test / java / org / openecomp / appc / generator / yang2json / Yang2JsonGeneratorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.generator.yang2json;
26
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;
40
41 import java.io.File;
42 import java.io.IOException;
43 import java.net.MalformedURLException;
44 import java.net.URL;
45 import java.util.*;
46
47 public class Yang2JsonGeneratorTest {
48
49     private static SchemaContext yangSchema = null;
50     private static JsonNode jsonNode = null;
51
52     public void readIOfiles() throws IOException{
53         if(yangSchema == null){
54             yangParse();
55         }
56         if(jsonNode == null){
57             jsonRead();
58         }
59
60     }
61
62     public void testJsonRpcNum(){
63
64         int rpcYangNum = yangSchema.getModules().iterator().next().getRpcs().size();
65         int rpcJsonNum = jsonNode.get("paths").size();
66         Assert.assertTrue(rpcYangNum == rpcJsonNum);
67     }
68
69     public void testJsonRpcsAndInOutParams(){
70
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);
74
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));
85             }
86
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));
94             }
95         }
96     }
97
98
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());
103         }
104         return yangParams;
105     }
106
107
108
109     private static void yangParse(){
110
111         try {
112             //sourceFileName
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) {
121             e.printStackTrace();
122         } catch (YangSyntaxErrorException e) {
123             e.printStackTrace();
124         } catch (IOException e) {
125             e.printStackTrace();
126         } catch (SchemaSourceException e) {
127             e.printStackTrace();
128         }
129
130
131     }
132
133
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);
139     }
140 }