From b6c674891e00f3bac43bd7b4fca06e7a87b8dd34 Mon Sep 17 00:00:00 2001 From: lapentafd Date: Thu, 27 May 2021 10:18:49 +0100 Subject: Fix Sonar Issues on Apex-pdp Changes made into cli-codegen, cli-editor, and core-engine Renamed one test class to match other test classes Issue-ID: POLICY-3093 Change-Id: Ib2d947782021590ffc08d426e7a1607a8c33f98a Signed-off-by: lapentafd --- .../apex/auth/clicodegen/CgStringRenderer.java | 5 +- .../auth/clicodegen/CodeGeneratorCliEditor.java | 36 +++--- .../auth/clicodegen/SupportGenerationTest.java | 137 +++++++++++++++++++++ .../auth/clicodegen/SupportGenerationTester.java | 136 -------------------- 4 files changed, 158 insertions(+), 156 deletions(-) create mode 100644 auth/cli-codegen/src/test/java/org/onap/policy/apex/auth/clicodegen/SupportGenerationTest.java delete mode 100644 auth/cli-codegen/src/test/java/org/onap/policy/apex/auth/clicodegen/SupportGenerationTester.java (limited to 'auth/cli-codegen') diff --git a/auth/cli-codegen/src/main/java/org/onap/policy/apex/auth/clicodegen/CgStringRenderer.java b/auth/cli-codegen/src/main/java/org/onap/policy/apex/auth/clicodegen/CgStringRenderer.java index b2cd9b9ca..c7235afb6 100644 --- a/auth/cli-codegen/src/main/java/org/onap/policy/apex/auth/clicodegen/CgStringRenderer.java +++ b/auth/cli-codegen/src/main/java/org/onap/policy/apex/auth/clicodegen/CgStringRenderer.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,7 +43,7 @@ public class CgStringRenderer implements AttributeRenderer { if (obj == null) { return null; } - String ret = obj.toString(); + var ret = obj.toString(); if (ret.length() == 0) { return "\"\""; } @@ -53,7 +54,7 @@ public class CgStringRenderer implements AttributeRenderer { } if ("doDescription".equals(format)) { - String ret = obj.toString(); + var ret = obj.toString(); if (ret.contains("\n") || ret.contains("\"")) { ret = "LS" + "\n" + ret + "\n" + "LE"; } else { diff --git a/auth/cli-codegen/src/main/java/org/onap/policy/apex/auth/clicodegen/CodeGeneratorCliEditor.java b/auth/cli-codegen/src/main/java/org/onap/policy/apex/auth/clicodegen/CodeGeneratorCliEditor.java index 53acc9cf2..6ad5fbc3c 100644 --- a/auth/cli-codegen/src/main/java/org/onap/policy/apex/auth/clicodegen/CodeGeneratorCliEditor.java +++ b/auth/cli-codegen/src/main/java/org/onap/policy/apex/auth/clicodegen/CodeGeneratorCliEditor.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019 Samsung Electronics Co., Ltd. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -172,7 +172,7 @@ public class CodeGeneratorCliEditor { */ public void addSchemaDeclaration(final String name, final String version, final String uuid, final String description, final String flavour, final String schema) { - final ST st = stg.getInstanceOf("schemaDecl"); + final var st = stg.getInstanceOf("schemaDecl"); st.add(NAME, name); st.add(VERSION, version); st.add(UUID, uuid); @@ -188,7 +188,7 @@ public class CodeGeneratorCliEditor { * @param codeGenCliEditorBuilder The parameters for the context album */ public void addContextAlbumDeclaration(CodeGenCliEditorBuilder codeGenCliEditorBuilder) { - final ST st = stg.getInstanceOf("ctxAlbumDecl"); + final var st = stg.getInstanceOf("ctxAlbumDecl"); st.add(NAME, codeGenCliEditorBuilder.getName()); st.add(VERSION, codeGenCliEditorBuilder.getVersion()); st.add(UUID, codeGenCliEditorBuilder.getUuid()); @@ -213,7 +213,7 @@ public class CodeGeneratorCliEditor { */ public ST createEventFieldDefinition(final String eventName, final String version, final String fieldName, final String fieldSchema, final String fieldSchemaVersion, final boolean optional) { - final ST st = stg.getInstanceOf("eventDefField"); + final var st = stg.getInstanceOf("eventDefField"); st.add(EVENT_NAME, eventName); st.add(VERSION, version); st.add(FIELD_NAME, fieldName); @@ -234,7 +234,7 @@ public class CodeGeneratorCliEditor { */ public ST createTaskDefLogic(final String taskName, final String version, final String flavour, final String logic) { - final ST st = stg.getInstanceOf("taskDefLogic"); + final var st = stg.getInstanceOf("taskDefLogic"); st.add(TASK_NAME, taskName); st.add(VERSION, version); st.add(FLAVOUR, flavour); @@ -248,7 +248,7 @@ public class CodeGeneratorCliEditor { * @param eventDeclarationBuilder param object for event declaration */ public void addEventDeclaration(EventDeclarationBuilder eventDeclarationBuilder) { - final ST st = stg.getInstanceOf("eventDecl"); + final var st = stg.getInstanceOf("eventDecl"); st.add(NAME, eventDeclarationBuilder.getName()); st.add(VERSION, eventDeclarationBuilder.getVersion()); st.add(UUID, eventDeclarationBuilder.getUuid()); @@ -272,7 +272,7 @@ public class CodeGeneratorCliEditor { * @param taskDeclarationBuilder builder for the task declaration parameters */ public void addTaskDeclaration(TaskDeclarationBuilder taskDeclarationBuilder) { - final ST st = stg.getInstanceOf("taskDecl"); + final var st = stg.getInstanceOf("taskDecl"); st.add(NAME, taskDeclarationBuilder.getName()); st.add(VERSION, taskDeclarationBuilder.getVersion()); st.add(UUID, taskDeclarationBuilder.getUuid()); @@ -298,7 +298,7 @@ public class CodeGeneratorCliEditor { */ public void addPolicyDefinition(final String name, final String version, final String uuid, final String description, final String template, final String firstState, final List states) { - final ST st = stg.getInstanceOf("policyDef"); + final var st = stg.getInstanceOf("policyDef"); st.add(NAME, name); st.add(VERSION, version); st.add(UUID, uuid); @@ -321,7 +321,7 @@ public class CodeGeneratorCliEditor { */ public ST createTaskDefinitionInfields(final String taskName, final String version, final String fieldName, final String fieldSchema, final String fieldSchemaVersion) { - final ST st = stg.getInstanceOf("taskDefInputFields"); + final var st = stg.getInstanceOf("taskDefInputFields"); st.add(TASK_NAME, taskName); st.add(VERSION, version); st.add(FIELD_NAME, fieldName); @@ -342,7 +342,7 @@ public class CodeGeneratorCliEditor { */ public ST createTaskDefinitionOutfields(final String taskName, final String version, final String fieldName, final String fieldSchema, final String fieldSchemaVersion) { - final ST st = stg.getInstanceOf("taskDefOutputFields"); + final var st = stg.getInstanceOf("taskDefOutputFields"); st.add(TASK_NAME, taskName); st.add(VERSION, version); st.add(FIELD_NAME, fieldName); @@ -362,7 +362,7 @@ public class CodeGeneratorCliEditor { */ public ST createTaskDefinitionParameters(final String name, final String version, final String parName, final String defaultValue) { - final ST st = stg.getInstanceOf("taskDefParameter"); + final var st = stg.getInstanceOf("taskDefParameter"); st.add(NAME, name); st.add(VERSION, version); st.add(PAR_NAME, parName); @@ -381,7 +381,7 @@ public class CodeGeneratorCliEditor { */ public ST createTaskDefinitionContextRef(final String name, final String version, final String albumName, final String albumVersion) { - final ST st = stg.getInstanceOf("taskDefCtxRef"); + final var st = stg.getInstanceOf("taskDefCtxRef"); st.add(NAME, name); st.add(VERSION, version); st.add(ALBUM_NAME, albumName); @@ -396,7 +396,7 @@ public class CodeGeneratorCliEditor { * @return a CLI command for a policy state task definition */ public ST createPolicyStateTask(PolicyStateTaskBuilder policyStateTaskBuilder) { - final ST st = stg.getInstanceOf("policyStateTask"); + final var st = stg.getInstanceOf("policyStateTask"); st.add(POLICY_NAME, policyStateTaskBuilder.getPolicyName()); st.add(VERSION, policyStateTaskBuilder.getVersion()); st.add(STATE_NAME, policyStateTaskBuilder.getStateName()); @@ -423,7 +423,7 @@ public class CodeGeneratorCliEditor { public ST createPolicyStateOutput(final String policyName, final String version, final String stateName, final String outputName, final String eventName, final String eventVersion, final String nextState) { - final ST st = stg.getInstanceOf("policyStateOutput"); + final var st = stg.getInstanceOf("policyStateOutput"); st.add(POLICY_NAME, policyName); st.add(VERSION, version); st.add(STATE_NAME, stateName); @@ -441,7 +441,7 @@ public class CodeGeneratorCliEditor { * @return a CLI command for a policy state definition */ public ST createPolicyStateDef(PolicyStateDefBuilder policyStateDefBuilder) { - final ST st = stg.getInstanceOf("policyStateDef"); + final var st = stg.getInstanceOf("policyStateDef"); st.add(POLICY_NAME, policyStateDefBuilder.getPolicyName()); st.add(VERSION, policyStateDefBuilder.getVersion()); st.add(STATE_NAME, policyStateDefBuilder.getStateName()); @@ -469,7 +469,7 @@ public class CodeGeneratorCliEditor { */ public ST createPolicyStateDefTaskSelLogic(final String name, final String version, final String stateName, final String logicFlavour, final String logic) { - final ST st = stg.getInstanceOf("policyStateTaskSelectionLogic"); + final var st = stg.getInstanceOf("policyStateTaskSelectionLogic"); st.add(NAME, name); st.add(VERSION, version); st.add(STATE_NAME, stateName); @@ -491,7 +491,7 @@ public class CodeGeneratorCliEditor { */ public ST createPolicyStateDefFinalizerLogic(final String name, final String version, final String stateName, final String finalizerLogicName, final String logicFlavour, final String logic) { - final ST st = stg.getInstanceOf("policyStateFinalizerLogic"); + final var st = stg.getInstanceOf("policyStateFinalizerLogic"); st.add(NAME, name); st.add(VERSION, version); st.add(STATE_NAME, stateName); @@ -513,7 +513,7 @@ public class CodeGeneratorCliEditor { */ public ST createPolicyStateDefContextRef(final String name, final String version, final String stateName, final String albumName, final String albumVersion) { - final ST st = stg.getInstanceOf("policyStateContextRef"); + final var st = stg.getInstanceOf("policyStateContextRef"); st.add(NAME, name); st.add(VERSION, version); st.add(STATE_NAME, stateName); diff --git a/auth/cli-codegen/src/test/java/org/onap/policy/apex/auth/clicodegen/SupportGenerationTest.java b/auth/cli-codegen/src/test/java/org/onap/policy/apex/auth/clicodegen/SupportGenerationTest.java new file mode 100644 index 000000000..aed334590 --- /dev/null +++ b/auth/cli-codegen/src/test/java/org/onap/policy/apex/auth/clicodegen/SupportGenerationTest.java @@ -0,0 +1,137 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * 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.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 SupportGenerationTest { + + /** + * Get the chunks for the codegen. + * + * @return the chunks + */ + private static Map> getCodeGenChunks() { + // CHECKSTYLE:OFF: LineLength + + final Map> 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 DummyStErrorListener errListener = new DummyStErrorListener(); + 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 DummyStErrorListener errListener = new DummyStErrorListener(); + final STGroupFile stg = new STGroupFile(CodeGeneratorCliEditor.STG_FILE); + stg.setListener(errListener); + + stg.getTemplateNames(); // dummy to compile group and get errors + final Map> chunks = getCodeGenChunks(); + String error = ""; + final Set definedNames = stg.getTemplateNames(); + for (final STGroup group : stg.getImportedGroups()) { + definedNames.addAll(group.getTemplateNames()); + } + final Set 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 definedParams = ((stg.getInstanceOf(required).getAttributes() == null) + ? new TreeSet() : stg.getInstanceOf(required).getAttributes().keySet()); + final List 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()); + } +} diff --git a/auth/cli-codegen/src/test/java/org/onap/policy/apex/auth/clicodegen/SupportGenerationTester.java b/auth/cli-codegen/src/test/java/org/onap/policy/apex/auth/clicodegen/SupportGenerationTester.java deleted file mode 100644 index 3e010d056..000000000 --- a/auth/cli-codegen/src/test/java/org/onap/policy/apex/auth/clicodegen/SupportGenerationTester.java +++ /dev/null @@ -1,136 +0,0 @@ -/*- - * ============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.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 SupportGenerationTester { - - /** - * Get the chunks for the codegen. - * - * @return the chunks - */ - private static Map> getCodeGenChunks() { - // CHECKSTYLE:OFF: LineLength - - final Map> 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 DummyStErrorListener errListener = new DummyStErrorListener(); - 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 DummyStErrorListener errListener = new DummyStErrorListener(); - final STGroupFile stg = new STGroupFile(CodeGeneratorCliEditor.STG_FILE); - stg.setListener(errListener); - - stg.getTemplateNames(); // dummy to compile group and get errors - final Map> chunks = getCodeGenChunks(); - String error = ""; - final Set definedNames = stg.getTemplateNames(); - for (final STGroup group : stg.getImportedGroups()) { - definedNames.addAll(group.getTemplateNames()); - } - final Set 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 definedParams = ((stg.getInstanceOf(required).getAttributes() == null) - ? new TreeSet() : stg.getInstanceOf(required).getAttributes().keySet()); - final List 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()); - } -} -- cgit 1.2.3-korg