diff options
Diffstat (limited to 'auth/cli-editor/src/test/java/org/onap')
3 files changed, 303 insertions, 0 deletions
diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaEditorTest.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaEditorTest.java new file mode 100644 index 000000000..10bb63329 --- /dev/null +++ b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaEditorTest.java @@ -0,0 +1,109 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.clieditor.tosca; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +import java.io.File; +import java.io.IOException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.resources.TextFileUtils; + +/** + * Class to perform unit test of Apex cli tosca editor}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class ApexCliToscaEditorTest { + + private File tempOutputToscaFile; + private File tempLogFile; + String[] sampleArgs; + + /** + * Initialise args. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Before + public void initialiseArgs() throws IOException { + + tempOutputToscaFile = File.createTempFile("ToscaPolicyOutput", ".json"); + tempLogFile = File.createTempFile("ApexCliTosca", ".log"); + sampleArgs = new String[] { + "-c", CommonTestData.COMMAND_FILE_NAME, + "-ac", CommonTestData.APEX_CONFIG_FILE_NAME, + "-t", CommonTestData.INPUT_TOSCA_TEMPLATE_FILE_NAME, + "-ot", tempOutputToscaFile.getAbsolutePath(), + "-l", tempLogFile.getAbsolutePath() + }; + } + + /** + * Removes the generated files. + */ + @After + public void removeGeneratedFiles() { + tempOutputToscaFile.delete(); + tempLogFile.delete(); + } + + @Test + public void testApexCliToscaParameterParser() { + ApexCliToscaParameters params = new ApexCliToscaParameterParser().parse(sampleArgs); + assertEquals(CommonTestData.APEX_CONFIG_FILE_NAME, params.getApexConfigFileName()); + assertEquals(CommonTestData.COMMAND_FILE_NAME, params.getCommandFileName()); + assertEquals(CommonTestData.INPUT_TOSCA_TEMPLATE_FILE_NAME, params.getInputToscaTemplateFileName()); + assertEquals(tempOutputToscaFile.getAbsolutePath(), params.getOutputToscaPolicyFileName()); + assertEquals(tempLogFile.getAbsolutePath(), params.getLogFileName()); + } + + @Test + public void testApexCliTosca_success() throws IOException { + final ApexCliToscaEditorMain cliEditor = new ApexCliToscaEditorMain(sampleArgs); + String outputTosca = TextFileUtils.getTextFileAsString(tempOutputToscaFile.getAbsolutePath()); + String outputToscaCompare = TextFileUtils.getTextFileAsString( + "src/test/resources/tosca/ToscaPolicyOutput_compare.json"); + assertEquals(outputToscaCompare, outputTosca); + assertFalse(cliEditor.isFailure()); + } + + @Test + public void testApexCliTosca_no_args() { + String[] noArgs = new String[] {}; + assertThatThrownBy(() -> new ApexCliToscaEditorMain(noArgs)).hasMessage("Insufficient arguments provided."); + } + + @Test + public void testApexCliTosca_missing_commandfile() { + String[] sampleArgs = new String[] { + "-ac", CommonTestData.APEX_CONFIG_FILE_NAME, + "-t", CommonTestData.INPUT_TOSCA_TEMPLATE_FILE_NAME, + "-ot", tempOutputToscaFile.getAbsolutePath(), + "-l", tempLogFile.getAbsolutePath() + }; + assertThatThrownBy(() -> new ApexCliToscaEditorMain(sampleArgs)).hasMessage("Insufficient arguments provided."); + } +} diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/tosca/CommonTestData.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/tosca/CommonTestData.java new file mode 100644 index 000000000..ce88f9795 --- /dev/null +++ b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/tosca/CommonTestData.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.clieditor.tosca; + +/** + * Class to hold/create all parameters for test cases. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class CommonTestData { + + private CommonTestData() { + // This class cannot be initialized + } + + public static final String INPUT_TOSCA_TEMPLATE_FILE_NAME = "src/test/resources/tosca/ToscaTemplate.json"; + public static final String APEX_CONFIG_FILE_NAME = "src/test/resources/tosca/ApexConfig.json"; + public static final String COMMAND_FILE_NAME = "src/test/resources/tosca/PolicyModel.apex"; + public static final String POLICY_MODEL_FILE_NAME = "src/test/resources/tosca/PolicyModel.json"; +} diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/utils/CliUtilsTest.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/utils/CliUtilsTest.java new file mode 100644 index 000000000..2dfd6f6c9 --- /dev/null +++ b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/utils/CliUtilsTest.java @@ -0,0 +1,156 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.clieditor.utils; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.auth.clieditor.tosca.ApexCliToscaParameterParser; +import org.onap.policy.apex.auth.clieditor.tosca.ApexCliToscaParameters; +import org.onap.policy.apex.auth.clieditor.tosca.CommonTestData; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.resources.TextFileUtils; + +/** + * Class to perform unit test of {@link CliUtils}}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class CliUtilsTest { + + private File tempOutputToscaFile; + private File tempLogFile; + private String policyModelFilePath; + String[] sampleArgs; + + /** + * Initialise args. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Before + public void initialiseArgs() throws IOException { + + tempOutputToscaFile = File.createTempFile("ToscaPolicyOutput", ".json"); + tempLogFile = File.createTempFile("ApexCliTosca", ".log"); + policyModelFilePath = CommonTestData.POLICY_MODEL_FILE_NAME; + sampleArgs = new String[] {"-c", CommonTestData.COMMAND_FILE_NAME, "-ac", CommonTestData.APEX_CONFIG_FILE_NAME, + "-t", CommonTestData.INPUT_TOSCA_TEMPLATE_FILE_NAME, "-ot", tempOutputToscaFile.getAbsolutePath(), "-l", + tempLogFile.getAbsolutePath()}; + } + + /** + * Removes the generated files. + */ + @After + public void removeGeneratedFiles() { + tempOutputToscaFile.delete(); + tempLogFile.delete(); + } + + @Test + public void testCreateToscaServiceTemplate() throws IOException, CoderException { + ApexCliToscaParameters params = new ApexCliToscaParameterParser().parse(sampleArgs); + CliUtils.createToscaServiceTemplate(params, policyModelFilePath); + String outputTosca = TextFileUtils.getTextFileAsString(tempOutputToscaFile.getAbsolutePath()); + String outputToscaCompare = + TextFileUtils.getTextFileAsString("src/test/resources/tosca/ToscaPolicyOutput_compare.json"); + assertEquals(outputToscaCompare, outputTosca); + } + + @Test + public void testValidateReadableFile_validfile() { + CliUtils.validateReadableFile("Apex Config File", CommonTestData.APEX_CONFIG_FILE_NAME); + } + + @Test + public void testValidateReadableFile_invalidfile() { + String invalidFileName = "src/test/resources/tosca/ApexConfigxyz.json"; + assertThatThrownBy(() -> CliUtils.validateReadableFile("Apex Config File", invalidFileName)) + .hasMessage("File " + invalidFileName + " of type Apex Config File does not exist"); + } + + @Test + public void testValidateWritableFile_validfile() { + CliUtils.validateWritableFile("Output Tosca Policy File", tempOutputToscaFile.getAbsolutePath()); + } + + @Test + public void testValidateWritableFile_invalidfile() { + String invalidFileName = "src/test/resources/tosca"; + assertThatThrownBy(() -> CliUtils.validateWritableFile("Output Tosca Policy File", invalidFileName)) + .hasMessage("File " + invalidFileName + " of type Output Tosca Policy File is not a normal file"); + } + + @Test + public void testValidateWritableDirectory_validdirectory() { + CliUtils.validateWritableDirectory("Working Directory", "src/test/resources/tosca"); + } + + @Test + public void testValidateWritableDirectory_invaliddirectory() { + assertThatThrownBy(() -> CliUtils.validateWritableDirectory("Working Directory", + CommonTestData.APEX_CONFIG_FILE_NAME)).hasMessage("directory " + CommonTestData.APEX_CONFIG_FILE_NAME + + " of type Working Directory is not a directory"); + } + + @Test + public void testGenerateArgumentsForCliEditor_success() { + ApexCliToscaParameters params = new ApexCliToscaParameterParser().parse(sampleArgs); + Properties optionVariableMap = new Properties(); + optionVariableMap.setProperty("c", "commandFileName"); + optionVariableMap.setProperty("ac", "apexConfigFileName"); + optionVariableMap.setProperty("t", "inputToscaTemplateFileName"); + optionVariableMap.setProperty("ot", "outputToscaPolicyFileName"); + optionVariableMap.setProperty("l", "logFileName"); + List<String> cliArgsList = + CliUtils.generateArgumentsForCliEditor(params, optionVariableMap, ApexCliToscaParameters.class); + assertTrue(cliArgsList.containsAll(Arrays.asList(sampleArgs))); + } + + @Test + public void testGenerateArgumentsForCliEditor_invalidvariable() { + ApexCliToscaParameters params = new ApexCliToscaParameterParser().parse(sampleArgs); + Properties optionVariableMap = new Properties(); + optionVariableMap.setProperty("c", "invalidFileName"); + List<String> cliArgsList = + CliUtils.generateArgumentsForCliEditor(params, optionVariableMap, ApexCliToscaParameters.class); + assertEquals(0, cliArgsList.size()); + } + + @Test + public void testGenerateArgumentsForCliEditor_missingoption() { + ApexCliToscaParameters params = new ApexCliToscaParameterParser().parse(sampleArgs); + Properties optionVariableMap = new Properties(); + List<String> cliArgsList = + CliUtils.generateArgumentsForCliEditor(params, optionVariableMap, ApexCliToscaParameters.class); + assertEquals(0, cliArgsList.size()); + } +} |