9d91f8f68a0c1e89aca779372b8f4ad79a2b5e58
[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
32 import org.junit.Test;
33 import org.onap.policy.apex.auth.clicodegen.CodeGeneratorCliEditor;
34 import org.stringtemplate.v4.STGroup;
35 import org.stringtemplate.v4.STGroupFile;
36
37 /**
38  * Test for the CG CLI Editor STG file.
39  *
40  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
41  */
42 public class SupportGenerationTester {
43
44     /**
45      * Get the chunks for the codegen.
46      *
47      * @return the chunks
48      */
49     private static Map<String, List<String>> getCodeGenChunks() {
50         // CHECKSTYLE:OFF: LineLength
51
52         final Map<String, List<String>> chunks = new LinkedHashMap<>();
53
54         chunks.put("/policyModel",
55                 Arrays.asList("name", "version", "uuid", "description", "declarations", "definitions"));
56         chunks.put("/schemaDecl", Arrays.asList("name", "version", "uuid", "description", "flavour", "schema"));
57         chunks.put("/ctxAlbumDecl", Arrays.asList("name", "version", "uuid", "description", "scope", "writable",
58                 "schemaName", "schemaVersion"));
59         chunks.put("/eventDecl",
60                 Arrays.asList("name", "version", "uuid", "description", "nameSpace", "source", "target", "fields"));
61         chunks.put("/eventDefField",
62                 Arrays.asList("eventName", "version", "fieldName", "fieldSchema", "fieldSchemaVersion", "optional"));
63         chunks.put("/taskDecl",
64                 Arrays.asList("name", "version", "uuid", "description", "infields", "outfields", "logic"));
65         chunks.put("/taskDefInputFields",
66                 Arrays.asList("taskName", "version", "fieldName", "fieldSchema", "fieldSchemaVersion"));
67         chunks.put("/taskDefOutputFields",
68                 Arrays.asList("taskName", "version", "fieldName", "fieldSchema", "fieldSchemaVersion"));
69         chunks.put("/taskDefLogic", Arrays.asList("taskName", "version", "flavour", "logic"));
70         chunks.put("/taskDefParameter", Arrays.asList("name", "version", "parName", "defaultValue"));
71         chunks.put("/taskDefCtxRef", Arrays.asList("name", "version", "albumName", "albumVersion"));
72         chunks.put("/policyDef", Arrays.asList("name", "version", "uuid", "description", "template", "firstState"));
73         chunks.put("/policyStateDef", Arrays.asList("policyName", "version", "stateName", "triggerName",
74                 "triggerVersion", "defaultTask", "defaultTaskVersion", "outputs", "tasks"));
75         chunks.put("/policyStateOutput", Arrays.asList("policyName", "version", "stateName", "outputName", "eventName",
76                 "eventVersion", "nextState"));
77         chunks.put("/policyStateTaskSelectionLogic",
78                 Arrays.asList("name", "version", "stateName", "logicFlavour", "logic"));
79         chunks.put("/policyStateTask", Arrays.asList("policyName", "version", "stateName", "taskLocalName", "taskName",
80                 "taskVersion", "outputType", "outputName"));
81         chunks.put("/policyStateFinalizerLogic",
82                 Arrays.asList("name", "version", "stateName", "finalizerLogicName", "logicFlavour", "logic"));
83         chunks.put("/policyStateContextRef",
84                 Arrays.asList("name", "version", "stateName", "albumName", "albumVersion"));
85
86         return chunks;
87         // CHECKSTYLE:ON: LineLength
88     }
89
90     /** Test STG load. */
91     @Test
92     public void testGenerationLoad() {
93         final DummyStErrorListener errListener = new DummyStErrorListener();
94         final STGroupFile stg = new STGroupFile(CodeGeneratorCliEditor.STG_FILE);
95         stg.setListener(errListener);
96
97         stg.getTemplateNames(); // dummy to compile group and get errors
98         assertEquals(0, errListener.getErrorCount());
99     }
100
101     /** Test STG chunks. */
102     @Test
103     public void testGenerationChunks() {
104         final DummyStErrorListener errListener = new DummyStErrorListener();
105         final STGroupFile stg = new STGroupFile(CodeGeneratorCliEditor.STG_FILE);
106         stg.setListener(errListener);
107
108         stg.getTemplateNames(); // dummy to compile group and get errors
109         final Map<String, List<String>> chunks = getCodeGenChunks();
110         String error = "";
111         final Set<String> definedNames = stg.getTemplateNames();
112         for (final STGroup group : stg.getImportedGroups()) {
113             definedNames.addAll(group.getTemplateNames());
114         }
115         final Set<String> requiredNames = chunks.keySet();
116
117         for (final String required : requiredNames) {
118             if (!definedNames.contains(required)) {
119                 error += " - target STG does not define template for <" + required + ">\n";
120             } else {
121                 final Set<String> definedParams = ((stg.getInstanceOf(required).getAttributes() == null)
122                         ? new TreeSet<String>() : stg.getInstanceOf(required).getAttributes().keySet());
123                 final List<String> requiredParams = chunks.get(required);
124                 for (final String reqArg : requiredParams) {
125                     if (!definedParams.contains(reqArg)) {
126                         error += " - target STG with template <" + required + "> does not define argument <" + reqArg
127                                 + ">\n";
128                     }
129                 }
130             }
131         }
132
133         if (!("".equals(error))) {
134             System.err.println(error);
135         }
136         assertEquals(0, error.length());
137     }
138 }