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