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;
31 import org.junit.Test;
32 import org.stringtemplate.v4.STGroup;
33 import org.stringtemplate.v4.STGroupFile;
36 * Test for the CG CLI Editor STG file.
38 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
40 public class SupportGenerationTester {
43 * Get the chunks for the codegen.
47 private static Map<String, List<String>> getCodeGenChunks() {
48 // CHECKSTYLE:OFF: LineLength
50 final Map<String, List<String>> chunks = new LinkedHashMap<>();
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"));
85 // CHECKSTYLE:ON: LineLength
90 public void testGenerationLoad() {
91 final DummyStErrorListener errListener = new DummyStErrorListener();
92 final STGroupFile stg = new STGroupFile(CodeGeneratorCliEditor.STG_FILE);
93 stg.setListener(errListener);
95 stg.getTemplateNames(); // dummy to compile group and get errors
96 assertEquals(0, errListener.getErrorCount());
99 /** Test STG chunks. */
101 public void testGenerationChunks() {
102 final DummyStErrorListener errListener = new DummyStErrorListener();
103 final STGroupFile stg = new STGroupFile(CodeGeneratorCliEditor.STG_FILE);
104 stg.setListener(errListener);
106 stg.getTemplateNames(); // dummy to compile group and get errors
107 final Map<String, List<String>> chunks = getCodeGenChunks();
109 final Set<String> definedNames = stg.getTemplateNames();
110 for (final STGroup group : stg.getImportedGroups()) {
111 definedNames.addAll(group.getTemplateNames());
113 final Set<String> requiredNames = chunks.keySet();
115 for (final String required : requiredNames) {
116 if (!definedNames.contains(required)) {
117 error += " - target STG does not define template for <" + required + ">\n";
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
131 if (!("".equals(error))) {
132 System.err.println(error);
134 assertEquals(0, error.length());