aboutsummaryrefslogtreecommitdiffstats
path: root/auth/cli-codegen/src/test/java/org/onap/policy/apex/auth/clicodegen/GenerationTest.java
blob: 3b79e805c5663d688374d93a87dbc8326184b7c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.auth.clicodegen;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.junit.Test;
import org.onap.policy.apex.auth.clicodegen.CodeGeneratorCliEditor;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.STGroupFile;

/**
 * Test for the CG CLI Editor STG file.
 *
 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
 */
public class GenerationTest {

    /**
     * Get the chunks for the codegen.
     *
     * @return the chunks
     */
    private static Map<String, List<String>> getCodeGenChunks() {
        // CHECKSTYLE:OFF: LineLength

        final Map<String, List<String>> chunks = new LinkedHashMap<>();

        chunks.put("/policyModel",
                Arrays.asList("name", "version", "uuid", "description", "declarations", "definitions"));
        chunks.put("/schemaDecl", Arrays.asList("name", "version", "uuid", "description", "flavour", "schema"));
        chunks.put("/ctxAlbumDecl", Arrays.asList("name", "version", "uuid", "description", "scope", "writable",
                "schemaName", "schemaVersion"));
        chunks.put("/eventDecl",
                Arrays.asList("name", "version", "uuid", "description", "nameSpace", "source", "target", "fields"));
        chunks.put("/eventDefField",
                Arrays.asList("eventName", "version", "fieldName", "fieldSchema", "fieldSchemaVersion", "optional"));
        chunks.put("/taskDecl",
                Arrays.asList("name", "version", "uuid", "description", "infields", "outfields", "logic"));
        chunks.put("/taskDefInputFields",
                Arrays.asList("taskName", "version", "fieldName", "fieldSchema", "fieldSchemaVersion"));
        chunks.put("/taskDefOutputFields",
                Arrays.asList("taskName", "version", "fieldName", "fieldSchema", "fieldSchemaVersion"));
        chunks.put("/taskDefLogic", Arrays.asList("taskName", "version", "flavour", "logic"));
        chunks.put("/taskDefParameter", Arrays.asList("name", "version", "parName", "defaultValue"));
        chunks.put("/taskDefCtxRef", Arrays.asList("name", "version", "albumName", "albumVersion"));
        chunks.put("/policyDef", Arrays.asList("name", "version", "uuid", "description", "template", "firstState"));
        chunks.put("/policyStateDef", Arrays.asList("policyName", "version", "stateName", "triggerName",
                "triggerVersion", "defaultTask", "defaultTaskVersion", "outputs", "tasks"));
        chunks.put("/policyStateOutput", Arrays.asList("policyName", "version", "stateName", "outputName", "eventName",
                "eventVersion", "nextState"));
        chunks.put("/policyStateTaskSelectionLogic",
                Arrays.asList("name", "version", "stateName", "logicFlavour", "logic"));
        chunks.put("/policyStateTask", Arrays.asList("policyName", "version", "stateName", "taskLocalName", "taskName",
                "taskVersion", "outputType", "outputName"));
        chunks.put("/policyStateFinalizerLogic",
                Arrays.asList("name", "version", "stateName", "finalizerLogicName", "logicFlavour", "logic"));
        chunks.put("/policyStateContextRef",
                Arrays.asList("name", "version", "stateName", "albumName", "albumVersion"));

        return chunks;
        // CHECKSTYLE:ON: LineLength
    }

    /** Test STG load. */
    @Test
    public void testGenerationLoad() {
        final StErrorListener errListener = new StErrorListener();
        final STGroupFile stg = new STGroupFile(CodeGeneratorCliEditor.STG_FILE);
        stg.setListener(errListener);

        stg.getTemplateNames(); // dummy to compile group and get errors
        assertEquals(0, errListener.getErrorCount());
    }

    /** Test STG chunks. */
    @Test
    public void testGenerationChunks() {
        final StErrorListener errListener = new StErrorListener();
        final STGroupFile stg = new STGroupFile(CodeGeneratorCliEditor.STG_FILE);
        stg.setListener(errListener);

        stg.getTemplateNames(); // dummy to compile group and get errors
        final Map<String, List<String>> chunks = getCodeGenChunks();
        String error = "";
        final Set<String> definedNames = stg.getTemplateNames();
        for (final STGroup group : stg.getImportedGroups()) {
            definedNames.addAll(group.getTemplateNames());
        }
        final Set<String> requiredNames = chunks.keySet();

        for (final String required : requiredNames) {
            if (!definedNames.contains(required)) {
                error += " - target STG does not define template for <" + required + ">\n";
            } else {
                final Set<String> definedParams = ((stg.getInstanceOf(required).getAttributes() == null)
                        ? new TreeSet<String>() : stg.getInstanceOf(required).getAttributes().keySet());
                final List<String> requiredParams = chunks.get(required);
                for (final String reqArg : requiredParams) {
                    if (!definedParams.contains(reqArg)) {
                        error += " - target STG with template <" + required + "> does not define argument <" + reqArg
                                + ">\n";
                    }
                }
            }
        }

        if (!("".equals(error))) {
            System.err.println(error);
        }
        assertEquals(0, error.length());
    }
}