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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.auth.clicodegen;
23 import static org.junit.Assert.assertEquals;
25 import java.util.Arrays;
26 import java.util.LinkedHashMap;
27 import java.util.List;
30 import java.util.TreeSet;
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;
38 * Test for the CG CLI Editor STG file.
40 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
42 public class SupportGenerationTester {
45 * Get the chunks for the codegen.
49 private static Map<String, List<String>> getCodeGenChunks() {
50 // CHECKSTYLE:OFF: LineLength
52 final Map<String, List<String>> chunks = new LinkedHashMap<>();
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"));
87 // CHECKSTYLE:ON: LineLength
92 public void testGenerationLoad() {
93 final DummyStErrorListener errListener = new DummyStErrorListener();
94 final STGroupFile stg = new STGroupFile(CodeGeneratorCliEditor.STG_FILE);
95 stg.setListener(errListener);
97 stg.getTemplateNames(); // dummy to compile group and get errors
98 assertEquals(0, errListener.getErrorCount());
101 /** Test STG chunks. */
103 public void testGenerationChunks() {
104 final DummyStErrorListener errListener = new DummyStErrorListener();
105 final STGroupFile stg = new STGroupFile(CodeGeneratorCliEditor.STG_FILE);
106 stg.setListener(errListener);
108 stg.getTemplateNames(); // dummy to compile group and get errors
109 final Map<String, List<String>> chunks = getCodeGenChunks();
111 final Set<String> definedNames = stg.getTemplateNames();
112 for (final STGroup group : stg.getImportedGroups()) {
113 definedNames.addAll(group.getTemplateNames());
115 final Set<String> requiredNames = chunks.keySet();
117 for (final String required : requiredNames) {
118 if (!definedNames.contains(required)) {
119 error += " - target STG does not define template for <" + required + ">\n";
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
133 if (!("".equals(error))) {
134 System.err.println(error);
136 assertEquals(0, error.length());