Changes for checkstyle 8.32
[policy/apex-pdp.git] / auth / cli-codegen / src / test / java / org / onap / policy / apex / auth / clicodegen / SupportGenerationTester.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.auth.clicodegen;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.util.Arrays;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.TreeSet;
31 import org.junit.Test;
32 import org.stringtemplate.v4.STGroup;
33 import org.stringtemplate.v4.STGroupFile;
34
35 /**
36  * Test for the CG CLI Editor STG file.
37  *
38  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
39  */
40 public class SupportGenerationTester {
41
42     /**
43      * Get the chunks for the codegen.
44      *
45      * @return the chunks
46      */
47     private static Map<String, List<String>> getCodeGenChunks() {
48         // CHECKSTYLE:OFF: LineLength
49
50         final Map<String, List<String>> chunks = new LinkedHashMap<>();
51
52         chunks.put("/policyModel",
53                 Arrays.asList("name", "version", "uuid", "description", "declarations", "definitions"));
54         chunks.put("/schemaDecl", Arrays.asList("name", "version", "uuid", "description", "flavour", "schema"));
55         chunks.put("/ctxAlbumDecl", Arrays.asList("name", "version", "uuid", "description", "scope", "writable",
56                 "schemaName", "schemaVersion"));
57         chunks.put("/eventDecl",
58                 Arrays.asList("name", "version", "uuid", "description", "nameSpace", "source", "target", "fields"));
59         chunks.put("/eventDefField",
60                 Arrays.asList("eventName", "version", "fieldName", "fieldSchema", "fieldSchemaVersion", "optional"));
61         chunks.put("/taskDecl",
62                 Arrays.asList("name", "version", "uuid", "description", "infields", "outfields", "logic"));
63         chunks.put("/taskDefInputFields",
64                 Arrays.asList("taskName", "version", "fieldName", "fieldSchema", "fieldSchemaVersion"));
65         chunks.put("/taskDefOutputFields",
66                 Arrays.asList("taskName", "version", "fieldName", "fieldSchema", "fieldSchemaVersion"));
67         chunks.put("/taskDefLogic", Arrays.asList("taskName", "version", "flavour", "logic"));
68         chunks.put("/taskDefParameter", Arrays.asList("name", "version", "parName", "defaultValue"));
69         chunks.put("/taskDefCtxRef", Arrays.asList("name", "version", "albumName", "albumVersion"));
70         chunks.put("/policyDef", Arrays.asList("name", "version", "uuid", "description", "template", "firstState"));
71         chunks.put("/policyStateDef", Arrays.asList("policyName", "version", "stateName", "triggerName",
72                 "triggerVersion", "defaultTask", "defaultTaskVersion", "outputs", "tasks"));
73         chunks.put("/policyStateOutput", Arrays.asList("policyName", "version", "stateName", "outputName", "eventName",
74                 "eventVersion", "nextState"));
75         chunks.put("/policyStateTaskSelectionLogic",
76                 Arrays.asList("name", "version", "stateName", "logicFlavour", "logic"));
77         chunks.put("/policyStateTask", Arrays.asList("policyName", "version", "stateName", "taskLocalName", "taskName",
78                 "taskVersion", "outputType", "outputName"));
79         chunks.put("/policyStateFinalizerLogic",
80                 Arrays.asList("name", "version", "stateName", "finalizerLogicName", "logicFlavour", "logic"));
81         chunks.put("/policyStateContextRef",
82                 Arrays.asList("name", "version", "stateName", "albumName", "albumVersion"));
83
84         return chunks;
85         // CHECKSTYLE:ON: LineLength
86     }
87
88     /** Test STG load. */
89     @Test
90     public void testGenerationLoad() {
91         final DummyStErrorListener errListener = new DummyStErrorListener();
92         final STGroupFile stg = new STGroupFile(CodeGeneratorCliEditor.STG_FILE);
93         stg.setListener(errListener);
94
95         stg.getTemplateNames(); // dummy to compile group and get errors
96         assertEquals(0, errListener.getErrorCount());
97     }
98
99     /** Test STG chunks. */
100     @Test
101     public void testGenerationChunks() {
102         final DummyStErrorListener errListener = new DummyStErrorListener();
103         final STGroupFile stg = new STGroupFile(CodeGeneratorCliEditor.STG_FILE);
104         stg.setListener(errListener);
105
106         stg.getTemplateNames(); // dummy to compile group and get errors
107         final Map<String, List<String>> chunks = getCodeGenChunks();
108         String error = "";
109         final Set<String> definedNames = stg.getTemplateNames();
110         for (final STGroup group : stg.getImportedGroups()) {
111             definedNames.addAll(group.getTemplateNames());
112         }
113         final Set<String> requiredNames = chunks.keySet();
114
115         for (final String required : requiredNames) {
116             if (!definedNames.contains(required)) {
117                 error += " - target STG does not define template for <" + required + ">\n";
118             } else {
119                 final Set<String> definedParams = ((stg.getInstanceOf(required).getAttributes() == null)
120                         ? new TreeSet<String>() : stg.getInstanceOf(required).getAttributes().keySet());
121                 final List<String> requiredParams = chunks.get(required);
122                 for (final String reqArg : requiredParams) {
123                     if (!definedParams.contains(reqArg)) {
124                         error += " - target STG with template <" + required + "> does not define argument <" + reqArg
125                                 + ">\n";
126                     }
127                 }
128             }
129         }
130
131         if (!("".equals(error))) {
132             System.err.println(error);
133         }
134         assertEquals(0, error.length());
135     }
136 }