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=========================================================
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;
34 import java.io.IOException;
35 import java.lang.reflect.Field;
36 import java.lang.reflect.Method;
38 import java.util.regex.Pattern;
40 public class Json2JavaGeneratorTest {
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";
47 public void readIOfiles() throws IOException{
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";
64 generatedClass = Class.forName(className);
65 Assert.assertNotNull(errMsg, generatedClass);
66 } catch (ClassNotFoundException e) {
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());
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);
88 Assert.assertEquals("number of fields in "+className+" are not as expected!",totalExpectedFields,generatedFieldNames.size());
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());
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";
105 generatedClass = Class.forName(GENERATED_LCM_API_CLASS);
106 Assert.assertNotNull(errMsg, generatedClass);
107 } catch (ClassNotFoundException e) {
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);
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);
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);
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")){
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);
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);
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);
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);