More license header changes to appc-client files
[appc.git] / appc-client / client-kit / src / test / java / json2java / Json2JavaGeneratorTest.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 json2java;
25
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.appc.generator.JsonHelper;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.lang.reflect.Field;
36 import java.lang.reflect.Method;
37 import java.util.*;
38 import java.util.regex.Pattern;
39
40 public class Json2JavaGeneratorTest {
41
42     private static JsonNode jsonNode = null;
43     private static final String MODEL_PACKAGE = "org.onap.appc.client.lcm.model";
44     private static final String GENERATED_LCM_API_CLASS = "org.onap.appc.client.lcm.api.LifeCycleManagerStateful";
45
46     //@Before
47     public void readIOfiles() throws IOException{
48         if(jsonNode == null){
49             jsonRead();
50         }
51
52     }
53
54     //@Test
55     public void testGeneratedJavaModel() {
56         Iterator<Map.Entry<String, JsonNode>> definitions = jsonNode.get("definitions").fields();
57         for (; definitions.hasNext(); ) {
58             Map.Entry<String, JsonNode> definitionEntry = definitions.next();
59             String definitionEntryKey = definitionEntry.getKey();
60             String className = MODEL_PACKAGE + "." + definitionEntryKey;
61             Class<?> generatedClass = null;
62             String errMsg = "the " + className + " was supposed to be generated, but not found";
63             try {
64                 generatedClass = Class.forName(className);
65                 Assert.assertNotNull(errMsg, generatedClass);
66             } catch (ClassNotFoundException e) {
67                 Assert.fail(errMsg);
68             }
69
70             JsonNode properties = definitionEntry.getValue().get("properties");
71             if (generatedClass != null && properties != null && properties.fields() != null) {
72                 Field[] declaredFields = generatedClass.getDeclaredFields();
73                 Set<String> generatedFieldNames = new HashSet();
74                 for(Field field : declaredFields){
75                     generatedFieldNames.add(field.getName().toLowerCase());
76                 }
77                 Iterator<Map.Entry<String, JsonNode>> propertiesFields = properties.fields();
78                 int totalExpectedFields = 0;
79                 for (; propertiesFields.hasNext(); ) {
80                     totalExpectedFields++;
81                     Map.Entry<String, JsonNode> propertyEntry = propertiesFields.next();
82                     String propertyEntryKey = propertyEntry.getKey();
83                     String fieldNameFromJson = propertyEntryKey.replaceAll(Pattern.quote("-"),"").toLowerCase();
84                     errMsg = "the field " + propertyEntryKey + " for " + className + " was supposed to be generated, but not found";
85                     boolean contains = generatedFieldNames.contains(fieldNameFromJson);
86                     Assert.assertTrue(errMsg, contains);
87                 }
88                 Assert.assertEquals("number of fields in "+className+" are not as expected!",totalExpectedFields,generatedFieldNames.size());
89             }
90         }
91     }
92
93     //@Test
94     public void testGeneratedJavaAPI() {
95         Map<String/*rpcOperation*/,Set<String> /*bodyInputParams*/> jsonBodyInputParams = new HashMap();
96         Map<String/*rpcOperation*/,Set<String> /*bodyOutputParams*/> jsonBodyOutputParams= new HashMap();
97         JsonHelper.populateRpcInputOutputSchemaRefFromJson(jsonNode,jsonBodyInputParams, jsonBodyOutputParams);
98         Assert.assertFalse(jsonBodyInputParams.isEmpty());
99         Assert.assertFalse(jsonBodyOutputParams.isEmpty());
100
101         //verify LifecycleManagementStatefulService was generated
102         Class<?> generatedClass = null;
103         String errMsg = "the " + GENERATED_LCM_API_CLASS + " was supposed to be generated, but not found";
104         try {
105             generatedClass = Class.forName(GENERATED_LCM_API_CLASS);
106             Assert.assertNotNull(errMsg, generatedClass);
107         } catch (ClassNotFoundException e) {
108             Assert.fail(errMsg);
109         }
110
111         //verify LifecycleManagementStatefulService was generated with methods
112         Method[] declaredMethods = generatedClass.getDeclaredMethods();
113         Assert.assertNotNull("no method was generated for "+GENERATED_LCM_API_CLASS,declaredMethods);
114
115         //verify correctness of input parameters and return type of each method
116         Set<String> generatedNonVoidMethods = new HashSet();
117         Set<String> generatedVoidMethods = new HashSet();
118         for(Method method : declaredMethods){
119             String returnType = method.getReturnType().getName();
120             Class<?>[] parameterTypes = method.getParameterTypes();
121             String methodName = method.getName().toLowerCase();
122             if(returnType.equals("void")){
123                 generatedVoidMethods.add(methodName);
124             }else {
125                 generatedNonVoidMethods.add(methodName);
126                 //verify correctness of return type
127                 String returnTypeSuffix = JsonHelper.getStringSuffix(returnType,".");
128                 Set<String> jsonOutputParams = jsonBodyOutputParams.get(methodName);
129                 Assert.assertNotNull(methodName+ " was not expected to return anything!",jsonOutputParams);
130                 boolean contains = jsonOutputParams.contains(returnTypeSuffix);
131                 Assert.assertTrue(methodName+ " was not expected to be with "+returnTypeSuffix+" return type!", contains);
132             }
133             //verify correctness of method input parameters
134             for (Class<?> parameterType :parameterTypes){
135                 String parameterTypeSuffix = JsonHelper.getStringSuffix(parameterType.getName(), ".");
136                 if(returnType.equals("void") && parameterTypeSuffix.equals("ResponseHandler")){
137                     continue;
138                 }
139                 Set<String> jsonInputParams = jsonBodyInputParams.get(methodName);
140                 Assert.assertNotNull(methodName+ " was not expected to be with any input parameter!",jsonInputParams);
141                 boolean contains = jsonInputParams.contains(parameterTypeSuffix);
142                 Assert.assertTrue(methodName+ " was not expected to be with "+parameterTypeSuffix+" parameter!", contains);
143             }
144
145         }
146
147         //verify total number of generated methods is same as expected
148         Assert.assertEquals("Total number of generated methods are not as expected!!",jsonBodyInputParams.size()*2,declaredMethods.length);
149         //verify all expected methods(void and non void) were generated
150         for(Map.Entry<String, Set<String>> rpcInputParams : jsonBodyInputParams.entrySet()){
151             errMsg = "none void method " + rpcInputParams.getKey() + "(case insensitive) for " + GENERATED_LCM_API_CLASS + " was supposed to be generated, but not found";
152             boolean contains = generatedNonVoidMethods.contains(rpcInputParams.getKey());
153             Assert.assertTrue(errMsg, contains);
154
155             errMsg = "void method " + rpcInputParams.getKey() + "(case insensitive) for " + GENERATED_LCM_API_CLASS + " was supposed to be generated, but not found";
156             contains = generatedVoidMethods.contains(rpcInputParams.getKey());
157             Assert.assertTrue(errMsg, contains);
158         }
159
160     }
161
162
163     private static void jsonRead ()throws IOException {
164         String jsonFilePath = System.getProperty("outputFile");
165         File file = new File(jsonFilePath);
166         ObjectMapper mapper = new ObjectMapper();
167         jsonNode = mapper.readTree(file);
168     }
169
170 }