diff options
author | waqas.ikram <waqas.ikram@ericsson.com> | 2018-06-07 16:01:35 +0100 |
---|---|---|
committer | waqas.ikram <waqas.ikram@ericsson.com> | 2018-06-08 11:48:06 +0100 |
commit | 6a9b54b275feff5369419a86997e94d0a95fc48e (patch) | |
tree | 8366bfb005057388b99c5639ed17496c881667f1 /auth/cli-editor | |
parent | 9289ac0afefe62f6c8e9cebddb611a8571bf5642 (diff) |
Fixing Sonar bugs and Vulnerabilities
Change-Id: Id5a95f23f1308dbb9f7f0c0f5567e238ecf830af
Issue-ID: POLICY-859
Signed-off-by: waqas.ikram <waqas.ikram@ericsson.com>
Diffstat (limited to 'auth/cli-editor')
3 files changed, 85 insertions, 22 deletions
diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexCLIEditorMain.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexCLIEditorMain.java index 4a2635efa..cb1a92eaf 100644 --- a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexCLIEditorMain.java +++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexCLIEditorMain.java @@ -78,7 +78,6 @@ public class ApexCLIEditorMain { // Read the command definitions try { commands = new JSONHandler<CLICommands>().read(CLICommands.class, parameters.getMetadataStream()); - LOGGER.debug("found " + commands.getCommandSet().size() + " commands"); } catch (final Exception e) { LOGGER.error("start of Apex command line editor failed, error reading command metadata from " + parameters.getMetadataLocation()); @@ -88,18 +87,19 @@ public class ApexCLIEditorMain { } // The JSON processing returns null if there is an empty file - if (null == commands) { + if (commands == null || commands.getCommandSet().isEmpty()) { LOGGER.error("start of Apex command line editor failed, no commands found in " + parameters.getApexPropertiesLocation()); errorCount++; return; } + LOGGER.debug("found " + commands.getCommandSet().size() + " commands"); + // Read the Apex properties try { apexModelProperties = new JSONHandler<ApexModelProperties>().read(ApexModelProperties.class, parameters.getApexPropertiesStream()); - LOGGER.debug("model properties are: " + apexModelProperties.toString()); } catch (final Exception e) { LOGGER.error("start of Apex command line editor failed, error reading Apex model properties from " + parameters.getApexPropertiesLocation()); @@ -116,6 +116,8 @@ public class ApexCLIEditorMain { return; } + LOGGER.debug("model properties are: " + apexModelProperties.toString()); + // Find the system commands final Set<KeywordNode> systemCommandNodes = new TreeSet<>(); for (final CLICommand command : commands.getCommandSet()) { diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestCLIEditorEventsContext.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestCLIEditorEventsContext.java index 43238d63d..4b8dbd23b 100644 --- a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestCLIEditorEventsContext.java +++ b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestCLIEditorEventsContext.java @@ -24,8 +24,12 @@ import static org.junit.Assert.assertEquals; import java.io.File; import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; import org.onap.policy.apex.model.utilities.TextFileUtils; @@ -35,6 +39,25 @@ import org.onap.policy.apex.model.utilities.TextFileUtils; public class TestCLIEditorEventsContext { // CHECKSTYLE:OFF: MagicNumber + private static final Path SRC_MAIN_FOLDER = Paths.get("src/main/resources/"); + private static final Path SRC_TEST_FOLDER = Paths.get("src/test/resources/"); + + private static final Path SUB_FOLDER = SRC_MAIN_FOLDER.resolve("examples/scripts/"); + + private static final String SPACES = "\\s+"; + private static final String EMPTY_STRING = ""; + + private static final Path APEX_AVRO_POLICY_FILE = SUB_FOLDER.resolve("TestPolicyAvroEventContext.apex"); + private static final Path APEX_JAVA_POLICY_FILE = SUB_FOLDER.resolve("TestPolicyJavaEventContext.apex"); + + private static final String FILE_NAME = "TestPolicyJavaEventsAndContext"; + private static final String JSON_FILE = FILE_NAME + ".json"; + private static final String LOG_FILE = FILE_NAME + ".log"; + + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + /** * Test java context model. * @@ -43,12 +66,12 @@ public class TestCLIEditorEventsContext { */ @Test public void testJavaContextModel() throws IOException, ApexModelException { - final File tempLogFile = File.createTempFile("TestPolicyJavaEventsAndContext", ".log"); - final File tempModelFile = File.createTempFile("TestPolicyJavaEventsAndContext", ".json"); - final String[] cliArgs = - new String[] {"-c", "src/main/resources/examples/scripts/TestPolicyJavaEventContext.apex", "-l", - tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()}; + final File tempLogFile = temporaryFolder.newFile(LOG_FILE); + final File tempModelFile = temporaryFolder.newFile(JSON_FILE); + + final String[] cliArgs = new String[] {"-c", APEX_JAVA_POLICY_FILE.toString(), "-l", + tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()}; final ApexCLIEditorMain cliEditor = new ApexCLIEditorMain(cliArgs); assertEquals(0, cliEditor.getErrorCount()); @@ -58,14 +81,11 @@ public class TestCLIEditorEventsContext { final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath()); // As a sanity check, count the number of non white space characters in log and model files - final int logCharCount = logString.replaceAll("\\s+", "").length(); - final int modelCharCount = modelString.replaceAll("\\s+", "").length(); + final int logCharCount = logString.replaceAll(SPACES, EMPTY_STRING).length(); + final int modelCharCount = modelString.replaceAll(SPACES, EMPTY_STRING).length(); assertEquals(25911, logCharCount); assertEquals(46138, modelCharCount); - - tempLogFile.delete(); - tempModelFile.delete(); } /** @@ -76,12 +96,12 @@ public class TestCLIEditorEventsContext { */ @Test public void testAvroContextModel() throws IOException, ApexModelException { - final File tempLogFile = File.createTempFile("TestPolicyAvroEventsAndContext", ".log"); - final File tempModelFile = File.createTempFile("TestPolicyAvroEventsAndContext", ".json"); - final String[] cliArgs = - new String[] {"-c", "src/main/resources/examples/scripts/TestPolicyAvroEventContext.apex", "-l", - tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()}; + final File tempLogFile = temporaryFolder.newFile(LOG_FILE); + final File tempModelFile = temporaryFolder.newFile(JSON_FILE); + + final String[] cliArgs = new String[] {"-c", APEX_AVRO_POLICY_FILE.toString(), "-l", + tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()}; final ApexCLIEditorMain cliEditor = new ApexCLIEditorMain(cliArgs); assertEquals(0, cliEditor.getErrorCount()); @@ -91,13 +111,51 @@ public class TestCLIEditorEventsContext { final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath()); // As a sanity check, count the number of non white space characters in log and model files - final int logCharCount = logString.replaceAll("\\s+", "").length(); - final int modelCharCount = modelString.replaceAll("\\s+", "").length(); + final int logCharCount = logString.replaceAll(SPACES, EMPTY_STRING).length(); + final int modelCharCount = modelString.replaceAll(SPACES, EMPTY_STRING).length(); assertEquals(30315, logCharCount); assertEquals(52930, modelCharCount); - tempLogFile.delete(); - tempModelFile.delete(); } + + @Test + public void test_emptyMetadataCommandFileWithEmptyJsonTag_errorcountGreaterThanOne() throws IOException { + + final File tempLogFile = temporaryFolder.newFile(LOG_FILE); + final File tempModelFile = temporaryFolder.newFile(JSON_FILE); + + final String modelFile = SRC_TEST_FOLDER.resolve("model").resolve("empty_commands.json").toString(); + final String apexPropertiesLocation = + SRC_MAIN_FOLDER.resolve("etc/editor").resolve("ApexModelProperties.json").toString(); + + final String[] cliArgs = + new String[] {"-c", APEX_AVRO_POLICY_FILE.toString(), "-l", tempLogFile.getAbsolutePath(), "-o", + tempModelFile.getAbsolutePath(), "-m", modelFile, "-a", apexPropertiesLocation}; + + final ApexCLIEditorMain objUnderTest = new ApexCLIEditorMain(cliArgs); + assertEquals(1, objUnderTest.getErrorCount()); + + } + + @Test + public void test_emptyMetadataCommandFile_errorcountGreaterThanOne() throws IOException { + + final File tempLogFile = temporaryFolder.newFile(LOG_FILE); + final File tempModelFile = temporaryFolder.newFile(JSON_FILE); + + final File modelFile = temporaryFolder.newFile("empty_commands.json"); + + final String apexPropertiesLocation = + SRC_MAIN_FOLDER.resolve("etc/editor").resolve("ApexModelProperties.json").toString(); + + final String[] cliArgs = new String[] {"-c", APEX_AVRO_POLICY_FILE.toString(), "-l", + tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-m", modelFile.getAbsolutePath(), + "-a", apexPropertiesLocation}; + + final ApexCLIEditorMain objUnderTest = new ApexCLIEditorMain(cliArgs); + assertEquals(1, objUnderTest.getErrorCount()); + + } + } diff --git a/auth/cli-editor/src/test/resources/model/empty_commands.json b/auth/cli-editor/src/test/resources/model/empty_commands.json new file mode 100644 index 000000000..49d1a202f --- /dev/null +++ b/auth/cli-editor/src/test/resources/model/empty_commands.json @@ -0,0 +1,3 @@ +{ + +} |