Added oparent to sdc main
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / verificator / ToscaValidation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.verificator;
22
23 import com.aventstack.extentreports.Status;
24 import fj.data.Either;
25 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
26 import org.onap.sdc.toscaparser.api.Group;
27 import org.onap.sdc.toscaparser.api.Property;
28 import org.onap.sdc.toscaparser.api.elements.Metadata;
29 import org.onap.sdc.toscaparser.api.parameters.Input;
30 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
31 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
32 import org.openecomp.sdc.ci.tests.tosca.datatypes.*;
33
34 import java.lang.reflect.Field;
35 import java.util.HashMap;
36 import java.util.LinkedHashMap;
37 import java.util.List;
38 import java.util.Map;
39
40 public class ToscaValidation {
41
42         
43         /**
44          * @param expectedToscaDefinition - expected toscaDefinition object
45          * @param actualToscaDefinition - actual toscaDefinition object
46          * @return true if all validation success else return error map
47          */
48         public static Either<Boolean, Map<String, Object>> resourceToscaMetadataValidator(ToscaDefinition expectedToscaDefinition, ToscaDefinition actualToscaDefinition){
49                 
50                 SetupCDTest.getExtendTest().log(Status.INFO, "Going to validate resource TOSCA metadata...");
51                 Map<String, String> expectedMetadata = expectedToscaDefinition.getMetadata();
52                 Map<String, String> actualMetadata = actualToscaDefinition.getMetadata();
53                 Either<Boolean, Map<String, Object>> resourceToscaMetadataValidator = compareStringMapData(expectedMetadata, actualMetadata);
54                 if(resourceToscaMetadataValidator.isLeft()){
55                         SetupCDTest.getExtendTest().log(Status.INFO, "Resource TOSCA metadata verification success");
56                 }else{
57                         SetupCDTest.getExtendTest().log(Status.ERROR, "Resource TOSCA metadata verification failed" + resourceToscaMetadataValidator.right().value());
58                 }
59                 return resourceToscaMetadataValidator;
60         }
61
62         public static Boolean resourceToscaNodeTemplateMetadataValidator(Map<String, Map<String, String>> expectedMetadata, ToscaDefinition actualToscaDefinition){
63                 boolean isTestFailed = true;
64                 for(String nodeTemplateName : expectedMetadata.keySet()){
65                         Either<Boolean,Map<String,Object>> serviceToscaMetadataValidator = componentToscaNodeTemplateMetadataValidator(expectedMetadata.get(nodeTemplateName), actualToscaDefinition, nodeTemplateName, ComponentTypeEnum.RESOURCE, nodeTemplateName);
66                         if(!serviceToscaMetadataValidator.left().value()){
67                                 isTestFailed = false;
68                         }
69                 }
70                 return isTestFailed;
71         }
72         
73         public static Either<Boolean, Map<String, Object>> vfModuleJsonFileValidator(Map<String, VfModuleDefinition> expectedVfModulesDefinitionObject, Map<String, VfModuleDefinition> actualVfModulesDefinitionObject){
74                 SetupCDTest.getExtendTest().log(Status.INFO, "Going to validate vf module json file...");
75                 Either<Boolean,Map<String,Object>> vfModuleFileValidator = compareObjectMapData(expectedVfModulesDefinitionObject, actualVfModulesDefinitionObject);
76                 if(vfModuleFileValidator.isLeft()){
77                         SetupCDTest.getExtendTest().log(Status.INFO, "Vf module json file verification success");
78                 }else{
79                         SetupCDTest.getExtendTest().log(Status.ERROR, "Vf module json file verification failed" + vfModuleFileValidator.right().value().toString());
80                 }
81                 return vfModuleFileValidator;
82         }
83
84         public static Either<Boolean, Map<String, Object>> serviceToscaMetadataValidator(Map<String, String> expectedMetadata, ToscaDefinition actualToscaDefinition){
85
86                 SetupCDTest.getExtendTest().log(Status.INFO, "Going to validate service TOSCA metadata...");
87                 Map<String, String> actualMetadata = actualToscaDefinition.getMetadata();
88                 Either<Boolean,Map<String,Object>> serviceToscaMetadataValidator = compareStringMapData(expectedMetadata, actualMetadata);
89                 if(serviceToscaMetadataValidator.isLeft()){
90                         SetupCDTest.getExtendTest().log(Status.INFO, "Service TOSCA metadata verification success");
91                 }else{
92                         SetupCDTest.getExtendTest().log(Status.ERROR, "Service TOSCA metadata verification failed" + serviceToscaMetadataValidator.right().value().toString());
93                 }
94                 return serviceToscaMetadataValidator;
95         }
96
97         public static Either<Boolean, Map<String, Object>> serviceToscaGroupMetadataValidator(Map<String, ToscaGroupsTopologyTemplateDefinition> expectedServiceGroup, ToscaDefinition actualToscaDefinition){
98
99                 SetupCDTest.getExtendTest().log(Status.INFO, "Going to validate service TOSCA group metadata...");
100                 Map<String, ToscaGroupsTopologyTemplateDefinition> actualServiceGroups = actualToscaDefinition.getTopology_template().getGroups();
101                 Either<Boolean,Map<String,Object>> serviceToscaGroupMetadataValidator = compareServiceGroupMetadata(expectedServiceGroup, actualServiceGroups);
102                 if(serviceToscaGroupMetadataValidator.isLeft()){
103                         SetupCDTest.getExtendTest().log(Status.INFO, "Service TOSCA group metadata verification success");
104                 }else{
105                         SetupCDTest.getExtendTest().log(Status.ERROR, "Service TOSCA group metadata verification failed" + serviceToscaGroupMetadataValidator.right().value().toString());
106                 }
107                 return serviceToscaGroupMetadataValidator;
108         }
109
110     public static Either<Boolean,Map<String,Object>> compareServiceGroupMetadata(Map<String, ToscaGroupsTopologyTemplateDefinition> expectedServiceGroup, Map<String, ToscaGroupsTopologyTemplateDefinition> actualServiceGroups) {
111
112         Map<String, Object> errorMap = new HashMap<>();
113         for (String groupName : expectedServiceGroup.keySet()){
114             if (actualServiceGroups.get(groupName) == null ){
115                 errorMap.put("group/module [" + groupName + "]", " does not exist in TOSCA main yaml");
116             }else{
117                 compareServiceGroupData(expectedServiceGroup.get(groupName), actualServiceGroups.get(groupName), groupName, errorMap);
118             }
119         }
120         if(!errorMap.isEmpty()){
121             return Either.right(errorMap);
122         }
123         return Either.left(true);
124     }
125
126     public static Either<Boolean, Map<String, Object>> compareServiceGroupData(ToscaServiceGroupsMetadataDefinition expectedServiceGroupMetadata, ToscaServiceGroupsMetadataDefinition actualServiceGroupMetadata, String groupName, Map<String, Object> errorMap) {
127
128         Field[] declaredFields = expectedServiceGroupMetadata.getClass().getSuperclass().getFields();
129         for (Field field : declaredFields){
130                 if(!field.getName().equals("serialVersionUID")) {
131                                 try {
132                                         String expectedValue = (String) field.get(expectedServiceGroupMetadata);
133                                         String actualValue = (String) field.get(actualServiceGroupMetadata);
134                                         comparingServiceGroupMetadata(groupName, errorMap, field, expectedValue, actualValue);
135                                 } catch (IllegalAccessException e) {
136                                         e.printStackTrace();
137                                 }
138                         }
139         }
140
141         if(!errorMap.isEmpty()){
142             return Either.right(errorMap);
143         }
144         return Either.left(true);
145     }
146
147
148 //    ###########################UsingParser############################
149 public static Either<Boolean, Map<String, Object>> serviceToscaGroupMetadataValidatorUsingParser(Map<String, ToscaGroupsTopologyTemplateDefinition> expectedServiceGroup, List<Group> actualServiceGroups){
150
151         SetupCDTest.getExtendTest().log(Status.INFO, "Going to validate service TOSCA group metadata...");
152         Either<Boolean,Map<String,Object>> serviceToscaGroupMetadataValidator = compareServiceGroupMetadataUsingParser(expectedServiceGroup, actualServiceGroups);
153         if(serviceToscaGroupMetadataValidator.isLeft()){
154                 SetupCDTest.getExtendTest().log(Status.INFO, "Service TOSCA group metadata verification vs. tosca parser success");
155         }else{
156                 SetupCDTest.getExtendTest().log(Status.ERROR, "Service TOSCA group metadata verification vs. tosca parser failed" + serviceToscaGroupMetadataValidator.right().value().toString());
157         }
158         return serviceToscaGroupMetadataValidator;
159 }
160
161         public static Either<Boolean,Map<String,Object>> compareServiceGroupMetadataUsingParser(Map<String, ToscaGroupsTopologyTemplateDefinition> expectedServiceGroup, List<Group> actualServiceGroups) {
162
163                 Map<String, Object> errorMap = new HashMap<>();
164                 for (String groupName : expectedServiceGroup.keySet()){
165                         Group actualGroup = getServiceToscaParserGroupFromObject(groupName, actualServiceGroups);
166                         if (actualGroup == null ){
167                                 errorMap.put("group/module [" + groupName + "]", " does not exist in TOSCA main yaml");
168                         }else{
169                                 compareServiceGroupDataUsingParser(expectedServiceGroup.get(groupName), actualGroup.getMetadata(), groupName, errorMap);
170                         }
171                 }
172                 if(!errorMap.isEmpty()){
173                         return Either.right(errorMap);
174                 }
175                 return Either.left(true);
176         }
177
178         private static Group getServiceToscaParserGroupFromObject(String groupName, List<Group> actualServiceGroups) {
179                 for(Group group : actualServiceGroups){
180                         if(group.getName().equals(groupName)){
181                                 return group;
182                         }
183                 }
184                 return null;
185         }
186
187         public static Either<Boolean, Map<String, Object>> compareServiceGroupDataUsingParser(ToscaServiceGroupsMetadataDefinition expectedServiceGroupMetadata, Metadata actualServiceGroupMetadata, String groupName, Map<String, Object> errorMap) {
188
189                 Field[] declaredFields = expectedServiceGroupMetadata.getClass().getSuperclass().getFields();
190                 for (Field field : declaredFields){
191                         if(!field.getName().equals("serialVersionUID")) {
192                                 try {
193                                         String expectedValue = (String) field.get(expectedServiceGroupMetadata);
194                                         String actualValue = actualServiceGroupMetadata.getValue(field.getName());
195                                         comparingServiceGroupMetadata(groupName, errorMap, field, expectedValue, actualValue);
196                                 } catch (IllegalAccessException e) {
197                                         e.printStackTrace();
198                                 }
199                         }
200                 }
201
202                 if(!errorMap.isEmpty()){
203                         return Either.right(errorMap);
204                 }
205                 return Either.left(true);
206         }
207
208         private static void comparingServiceGroupMetadata(String groupName, Map<String, Object> errorMap, Field field, String expectedValue, String actualValue) {
209                 if (expectedValue != null && !expectedValue.trim().equals("")) {
210             if (actualValue != null) {
211                 Boolean result = compareStringValue(expectedValue, actualValue);
212                 if (!result) {
213                     errorMap.put("Data field [" + field.getName() + "] in group service metadata [" + groupName + "]", "expected: " + expectedValue + ", actual: " + actualValue);
214                 }
215             } else {
216                 errorMap.put("Data field [" + field.getName() + "] in group service metadata [" + groupName + "]", " does not exist in actual object");
217                 System.out.println("Data field [" + field.getName() + "] in group service metadata [" + groupName + "] does not exist in actual object");
218             }
219         }
220         }
221
222 //      ############################################################################################
223
224
225
226         //    ###########################Property UsingParser############################
227         public static Either<Boolean, Map<String, Object>> serviceToscaGroupPropertyValidatorUsingParser(Map<String, ToscaGroupsTopologyTemplateDefinition> expectedServiceGroup, List<Group> actualServiceGroups){
228
229                 SetupCDTest.getExtendTest().log(Status.INFO, "Going to validate service TOSCA group property...");
230                 Either<Boolean,Map<String,Object>> serviceToscaGroupMetadataValidator = compareServiceGroupPropertyUsingParser(expectedServiceGroup, actualServiceGroups);
231                 if(serviceToscaGroupMetadataValidator.isLeft()){
232                         SetupCDTest.getExtendTest().log(Status.INFO, "Service TOSCA group property verification vs. tosca parser success");
233                 }else{
234                         SetupCDTest.getExtendTest().log(Status.ERROR, "Service TOSCA group property verification vs. tosca parser failed" + serviceToscaGroupMetadataValidator.right().value().toString());
235                 }
236                 return serviceToscaGroupMetadataValidator;
237         }
238
239         public static Either<Boolean,Map<String,Object>> compareServiceGroupPropertyUsingParser(Map<String, ToscaGroupsTopologyTemplateDefinition> expectedServiceGroup, List<Group> actualServiceGroups) {
240
241                 Map<String, Object> errorMap = new HashMap<>();
242                 for (String groupName : expectedServiceGroup.keySet()){
243                         Group actualGroup = getServiceToscaParserGroupFromObject(groupName, actualServiceGroups);
244                         if (actualGroup == null ){
245                                 errorMap.put("group/module [" + groupName + "]", " does not exist in TOSCA main yaml");
246                         }else{
247                                 compareServiceGroupPropertyUsingParser(expectedServiceGroup.get(groupName).getProperties(), actualGroup.getProperties(), groupName, errorMap);
248                         }
249                 }
250                 if(!errorMap.isEmpty()){
251                         return Either.right(errorMap);
252                 }
253                 return Either.left(true);
254         }
255
256         public static Either<Boolean, Map<String, Object>> compareServiceGroupPropertyUsingParser(ToscaGroupPropertyDefinition expectedServiceGroupProperty, LinkedHashMap<String, Property> actualServiceGroupProperty, String groupName, Map<String, Object> errorMap) {
257
258                 Field[] declaredFields = expectedServiceGroupProperty.getClass().getDeclaredFields();
259                 for (Field field : declaredFields){
260                         if(!field.getName().equals("serialVersionUID")) {
261                                 try {
262                                         String expectedValue = null;
263                                         String actualValue = null;
264                                         if (field.get(expectedServiceGroupProperty) != null) {
265                                                 expectedValue = field.get(expectedServiceGroupProperty).toString();
266                                         }
267                                         if (actualServiceGroupProperty.get(field.getName()) != null && actualServiceGroupProperty.get(field.getName()).getValue() != null) {
268                                                 actualValue = actualServiceGroupProperty.get(field.getName()).getValue().toString();
269                                         }
270                                         comparingServiceGroupProperty(groupName, errorMap, field, expectedValue, actualValue);
271                                 } catch (IllegalAccessException e) {
272                                         e.printStackTrace();
273                                 }
274                         }
275                 }
276
277                 if(!errorMap.isEmpty()){
278                         return Either.right(errorMap);
279                 }
280                 return Either.left(true);
281         }
282
283 //      ############################################################################################
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301 //?-------
302         public static Either<Boolean, Map<String, Object>> serviceToscaGroupPropertyValidator(Map<String, ToscaGroupsTopologyTemplateDefinition> expectedServiceGroup, ToscaDefinition actualToscaDefinition){
303
304                 SetupCDTest.getExtendTest().log(Status.INFO, "Going to validate service TOSCA group property...");
305                 Map<String, ToscaGroupsTopologyTemplateDefinition> actualServiceGroups = actualToscaDefinition.getTopology_template().getGroups();
306                 Either<Boolean,Map<String,Object>> serviceToscaGroupPropertyValidator = compareServiceGroupProperty(expectedServiceGroup, actualServiceGroups);
307                 if(serviceToscaGroupPropertyValidator.isLeft()){
308                         SetupCDTest.getExtendTest().log(Status.INFO, "Service TOSCA group property verification success");
309                 }else{
310                         SetupCDTest.getExtendTest().log(Status.ERROR, "Service TOSCA group property verification failed" + serviceToscaGroupPropertyValidator.right().value().toString());
311                 }
312                 return serviceToscaGroupPropertyValidator;
313         }
314
315         public static Either<Boolean,Map<String,Object>> compareServiceGroupProperty(Map<String, ToscaGroupsTopologyTemplateDefinition> expectedServiceGroup, Map<String, ToscaGroupsTopologyTemplateDefinition> actualServiceGroups) {
316
317                 Map<String, Object> errorMap = new HashMap<>();
318                 for (String groupName : expectedServiceGroup.keySet()){
319                         if (actualServiceGroups.get(groupName) == null ){
320                                 errorMap.put("group/module [" + groupName + "]", " does not exist in TOSCA main yaml");
321                         }else{
322                                 compareServiceGroupProperty(expectedServiceGroup.get(groupName).getProperties(), actualServiceGroups.get(groupName).getProperties(), groupName, errorMap);
323                         }
324                 }
325                 if(!errorMap.isEmpty()){
326                         return Either.right(errorMap);
327                 }
328                 return Either.left(true);
329         }
330
331         public static Either<Boolean, Map<String, Object>> compareServiceGroupProperty(ToscaGroupPropertyDefinition expectedServiceGroupProperty, ToscaGroupPropertyDefinition actualServiceGroupProperty, String groupName, Map<String, Object> errorMap) {
332
333                 Field[] declaredFields = expectedServiceGroupProperty.getClass().getDeclaredFields();
334                 for (Field field : declaredFields){
335                         if(!field.getName().equals("serialVersionUID")) {
336                                 try {
337                                         String expectedValue = null;
338                                         String actualValue = null;
339                                         if(field.get(expectedServiceGroupProperty) != null) {
340                                                 expectedValue = field.get(expectedServiceGroupProperty).toString();
341                                         }
342                                         if(field.get(actualServiceGroupProperty) != null) {
343                                                 actualValue = field.get(actualServiceGroupProperty).toString();
344                                         }
345                                         comparingServiceGroupProperty(groupName, errorMap, field, expectedValue, actualValue);
346                                 } catch (IllegalAccessException e) {
347                                         e.printStackTrace();
348                                 }
349                         }
350                 }
351
352                 if(!errorMap.isEmpty()){
353                         return Either.right(errorMap);
354                 }
355                 return Either.left(true);
356         }
357
358         private static void comparingServiceGroupProperty(String groupName, Map<String, Object> errorMap, Field field, String expectedValue, String actualValue) {
359                 if (expectedValue != null && !expectedValue.trim().equals("")) {
360             if (actualValue != null) {
361                 Boolean result = compareStringValue(expectedValue, actualValue);
362                 if (!result) {
363                     errorMap.put("Data field [" + field.getName() + "] in group service property [" + groupName + "]", "expected: " + expectedValue + ", actual: " + actualValue);
364                 }
365             } else {
366                 errorMap.put("Data field [" + field.getName() + "] in group service property [" + groupName + "]", " does not exist in actual object");
367                 System.out.println("Data field [" + field.getName() + "] in group service property [" + groupName + "] does not exist in actual object");
368             }
369         }
370         }
371
372
373         //    ----------------------------------
374         public static Either<Boolean, Map<String, Object>> componentToscaNodeTemplateMetadataValidator(Map<String, String> expectedMetadata, ToscaDefinition actualToscaDefinition, String nodeTemplateName, ComponentTypeEnum componentType, String componentName){
375                 
376                 SetupCDTest.getExtendTest().log(Status.INFO, "Going to validate "+ componentName + " " + componentType.getValue() + " node template TOSCA metadata...");
377                 Map<String, String> actualMetadata = actualToscaDefinition.getTopology_template().getNode_templates().get(nodeTemplateName).getMetadata();
378                 Either<Boolean,Map<String,Object>> componentToscaMetadataValidator = compareStringMapData(expectedMetadata, actualMetadata);
379                 if(componentToscaMetadataValidator.isLeft()){
380                         SetupCDTest.getExtendTest().log(Status.INFO, " " + componentName + " " + componentType.getValue() + "TOSCA node template metadata verification success");
381                 }else{
382                         SetupCDTest.getExtendTest().log(Status.ERROR, " " +componentName + " " + componentType.getValue() + "TOSCA node template metadata verification failed" + componentToscaMetadataValidator.right().value().toString());
383                 }
384                 return componentToscaMetadataValidator;
385         }
386         
387         public static Either<Boolean, Map<String, Object>> serviceToscaMetadataValidatorAgainstParser(Map<String, String> expectedMetadata, Metadata actualMetadata){
388                 
389                 SetupCDTest.getExtendTest().log(Status.INFO, "Going to validate service TOSCA metadata...");
390
391                 Either<Boolean,Map<String,Object>> serviceToscaMetadataValidator = compareMetadataUsingToscaParser(expectedMetadata, actualMetadata);
392                 if(serviceToscaMetadataValidator.isLeft()){
393                         SetupCDTest.getExtendTest().log(Status.INFO, "Service TOSCA metadata verification vs. tosca parser success");
394                 }else{
395                         SetupCDTest.getExtendTest().log(Status.ERROR, "Service TOSCA metadata verification vs. tosca parser failed" + serviceToscaMetadataValidator.right().value().toString());
396                 }
397                 return serviceToscaMetadataValidator;
398         }
399
400         
401         public static Either<Boolean, Map<String, Object>> compareStringMapData(Map<String, String> expectedMetadata, Map<String, String> actualMetadata) {
402                 Either.left(false);
403                 Map<String, Object> errorMap = new HashMap<>();
404                 for(String key : expectedMetadata.keySet()){
405                         boolean isError = compareStringValue(expectedMetadata.get(key), actualMetadata.get(key));
406                         if(!isError){
407                                 errorMap.put("Data key["+key+"]", "expected: " + expectedMetadata.get(key) + ", actual: " + actualMetadata.get(key));
408                         }
409                 }
410                 if(!errorMap.isEmpty()){
411                         return Either.right(errorMap);
412                 }
413                 return Either.left(true);
414         }
415
416
417         public static Either<Boolean, Map<String, Object>> compareObjectMapData(Map<String, VfModuleDefinition> expectedObject, Map<String, VfModuleDefinition> actualObject) {
418                 Map<String, Object> errorMap = new HashMap<>();
419                 for(String key : expectedObject.keySet()){
420                         boolean isError = compareObjectValue(expectedObject.get(key), actualObject.get(key));
421                         if(!isError){
422                                 errorMap.put("Data key["+key+"]", "expected: " + expectedObject.get(key) + ", actual: " + actualObject.get(key));
423                         }
424                 }
425                 if(!errorMap.isEmpty()){
426                         return Either.right(errorMap);
427                 }
428                 return Either.left(true);
429         }
430
431         private static boolean compareObjectValue(VfModuleDefinition expected, VfModuleDefinition actual) {
432                 return expected.equals(actual);
433         }
434
435         public static Either<Boolean, Map<String, Object>> compareMetadataUsingToscaParser(Map<String, String> expectedMetadata, Metadata actualMetadata) {
436                 Map<String, Object> errorMap = new HashMap<>();
437                 for(String key : expectedMetadata.keySet()){
438                         boolean isError = compareStringValue(expectedMetadata.get(key), actualMetadata.getValue(key));
439                         if(!isError){
440                                 errorMap.put("Data key["+key+"]", "expected: " + expectedMetadata.get(key) + ", actual: " + actualMetadata.getValue(key));
441                         }
442                 }
443                 if(!errorMap.isEmpty()){
444                         return Either.right(errorMap);
445                 }
446                 return Either.left(true);
447         }
448         
449         private static boolean compareStringValue(String expected, String actual) {
450                 return expected.equals(actual);
451         }
452         
453         public static Either<Boolean, Map<String, Object>> toscaInputsValidator(Map<String, ToscaInputsTopologyTemplateDefinition> expectedInputs, Map<String, ToscaInputsTopologyTemplateDefinition> actualInputs){
454                 
455                 SetupCDTest.getExtendTest().log(Status.INFO, "Going to validate TOSCA inputs...");
456                 Either<Boolean,Map<String,Object>> toscaInputsValidator = compareInputs(expectedInputs, actualInputs);
457                 if(toscaInputsValidator.isLeft()){
458                         SetupCDTest.getExtendTest().log(Status.INFO, "TOSCA inputs verification success");
459                 }else{
460                         SetupCDTest.getExtendTest().log(Status.ERROR, "TOSCA inputs verification failed" + toscaInputsValidator.right().value().toString());
461                 }
462                 
463                 if(toscaInputsValidator.right() != null && ! toscaInputsValidator.right().equals("")){
464                         return toscaInputsValidator;
465                 }
466                 return Either.left(true);
467         }
468         
469         public static Either<Boolean,Map<String,Object>> compareInputs(Map<String, ToscaInputsTopologyTemplateDefinition> expectedInputs, Map<String, ToscaInputsTopologyTemplateDefinition> actualInputs) {
470                 
471                 Map<String, Object> errorMap = new HashMap<>();
472                 
473                 for (String inputName : expectedInputs.keySet()){
474                         if (actualInputs.get(inputName) == null ){
475                                 errorMap.put("input [" + inputName + "]", " does not exist in TOSCA main yaml");
476                         }else{
477                                 compareInputData(expectedInputs.get(inputName), actualInputs.get(inputName), errorMap);
478                         }
479                 }
480                 if(!errorMap.isEmpty()){
481                         return Either.right(errorMap);
482                 }
483                 return Either.left(true);
484         }
485
486         public static Either<Boolean, Map<String, Object>> compareInputData(ToscaInputsTopologyTemplateDefinition expectedInputDefinition, ToscaInputsTopologyTemplateDefinition actualInputDefinition, Map<String, Object> errorMap) {
487
488                 Field[] declaredFields = expectedInputDefinition.getClass().getDeclaredFields();
489                 for (Field field : declaredFields){
490                         try {
491                                 Object expectedValue = field.get(expectedInputDefinition);
492                                 Object actualValue = field.get(actualInputDefinition);
493 //                              verification exclude fields as (immutable, hidden, constraints, entry_schema) according Renana
494                                 if(expectedValue != null && !expectedValue.toString().trim().equals("") && field.getName() != "name" && field.getName() != "immutable" && field.getName() != "hidden" && field.getName() != "constraints" && field.getName() != "entry_schema" && field.getName() != "required") {
495                                         if (actualValue != null) {
496                                                 compareInputValue(expectedInputDefinition, errorMap, field, expectedValue, actualValue);
497                                         } else {
498                                                 errorMap.put("Data field [" + field.getName() + "] in input [" + expectedInputDefinition.getName() + "]", " does not exist in actual object");
499                                                 System.out.println("Data field [" + field.getName() + "] in input [" + expectedInputDefinition.getName() + "] does not exist in actual object");
500                                         }
501                                 }
502                         }catch (IllegalAccessException e) {
503                                 e.printStackTrace();
504                         }
505                 }
506                 
507                 if(!errorMap.isEmpty()){
508                         return Either.right(errorMap);
509                 }
510                 return Either.left(true);
511         }
512
513         public static void compareInputValue(ToscaInputsTopologyTemplateDefinition expectedInputDefinition, Map<String, Object> errorMap, Field field, Object expectedValue, Object actualValue) {
514                 if(field.getName().equals("value") || field.getName().equals("Default")){
515                         switch (expectedInputDefinition.getType()) {
516                         case "string":
517                                 if(! expectedValue.toString().replace("\n"," ").replaceAll("( +)", " ").equals(actualValue.toString().replace("\n"," ").replaceAll("( +)", " "))){
518                                         errorMap.put("Data field [" + field.getName()+"] in input [" + expectedInputDefinition.getName() + "]",  "expected: " + expectedValue + ", actual: " + actualValue);
519                                         System.out.println("Data field [" + field.getName()+"] in input [" + expectedInputDefinition.getName() + "]: expected: " + expectedValue + ", actual: " + actualValue);
520                                 }
521                                 break;
522                         case "float":
523                                         float newExpectedValue = convertObjectToFloat(expectedValue);
524                                         float newActualValue = convertObjectToFloat(actualValue);
525                                         if(newExpectedValue != newActualValue){
526                                                 errorMap.put("Data field [" + field.getName()+"] in input [" + expectedInputDefinition.getName() + "]",  "expected: " + newExpectedValue + ", actual: " + newActualValue);
527                                                 System.out.println("Data field [" + field.getName()+"] in input [" + expectedInputDefinition.getName() + "]: expected: " + newExpectedValue + ", actual: " + newActualValue);
528                                         }
529                                 break;
530                         case "boolean":
531                                 if(! expectedValue.toString().equalsIgnoreCase(actualValue.toString())){
532                                         errorMap.put("Data field [" + field.getName()+"] in input [" + expectedInputDefinition.getName() + "]",  "expected: " + expectedValue + ", actual: " + actualValue);
533                                         System.out.println("Data field [" + field.getName()+"] in input [" + expectedInputDefinition.getName() + "]: expected: " + expectedValue + ", actual: " + actualValue);
534                                 }
535                                 break;
536                         case "list":
537                                 expectedInputDefinition.getEntry_schema().get("type");
538                                 break;
539                         case "map":
540
541                                 break;
542                         default:
543                                 break;
544                         }
545
546
547                 }else{
548                         if(! expectedValue.equals(actualValue)){
549                                 errorMap.put("Data field [" + field.getName()+"] in input [" + expectedInputDefinition.getName() + "]",  "expected: " + expectedValue + ", actual: " + actualValue);
550                                 System.out.println("Data field [" + field.getName()+"] in input [" + expectedInputDefinition.getName() + "]: expected: " + expectedValue + ", actual: " + actualValue);
551                         }
552                 }
553                 
554         }
555         
556         
557         public static float convertObjectToFloat(Object object){
558                 
559                 float floatValue = 0;
560                 
561                 if(object instanceof Integer){
562                         floatValue = ((Integer)object).floatValue();
563                 }
564                 if(object instanceof Double){
565                         floatValue = ((Double)object).floatValue();
566                 }
567                 if(object instanceof Float){
568                         floatValue = ((Float) object).floatValue();
569                 }
570                 if(object instanceof String){
571                         floatValue = Float.parseFloat(object.toString());
572                 }
573                 
574                 if(object instanceof Long){
575                         floatValue = ((Long) object).floatValue();
576                 }
577                 return floatValue;
578         }
579
580         public static Either<Boolean, Map<String, Object>> toscaInputsValidatorAgainstParser(Map<String, ToscaInputsTopologyTemplateDefinition> expectedInputsMap, ISdcCsarHelper fdntCsarHelper) {
581                 SetupCDTest.getExtendTest().log(Status.INFO, "Going to convert tosca parser inputs output to ToscaInputsTopologyTemplateDefinition object...");
582                 if(!fdntCsarHelper.getServiceInputs().isEmpty()){
583                         if(expectedInputsMap != null && ! expectedInputsMap.isEmpty()){
584                                 return Either.left(true);
585                         }else{
586                                 Map<String, Object> errorMap = new HashMap<>();
587                                 errorMap.put("Inputs", " do not exist on actual service");
588                                 SetupCDTest.getExtendTest().log(Status.INFO, "Inputs do not exist on actual service csar");
589                                 return Either.right(errorMap);
590                         }
591                 }
592                 Map<String, ToscaInputsTopologyTemplateDefinition> actualInputsMap = convertInputsParserOutputToMap(fdntCsarHelper);
593                 return toscaInputsValidator(expectedInputsMap, actualInputsMap);
594         }
595
596         /**
597          * @param fdntCsarHelper convert list of inputs return from tosca parser to map of ToscaInputsTopologyTemplateDefinition
598          * @return field.get(actualServiceGroupProperty)
599          */
600         public static Map<String, ToscaInputsTopologyTemplateDefinition> convertInputsParserOutputToMap(ISdcCsarHelper fdntCsarHelper) {
601                 Map<String, ToscaInputsTopologyTemplateDefinition> actualInputsMap = new HashMap<>();
602                 List<Input> serviceInputs = fdntCsarHelper.getServiceInputs();
603                 for (Input input : serviceInputs){
604                         ToscaInputsTopologyTemplateDefinition actualInputDefinition = new ToscaInputsTopologyTemplateDefinition();
605                         actualInputDefinition.setDefault(input.getDefault());
606                         actualInputDefinition.setType(input.getType());
607                         actualInputDefinition.setDescription(input.getDescription());
608                         actualInputsMap.put(input.getName(), actualInputDefinition);
609                 }
610                 return actualInputsMap;
611         }
612         
613 }