From 62475a30ef2d425fe04df35ef2dac53c7ab5306a Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Fri, 16 Nov 2018 20:59:14 +0900 Subject: Rename test classes in apex-pdp Make test classes name consistence by putting 'Test' at end Issue-ID: POLICY-1263 Change-Id: I0179388d84826e698276a1995dd8173a40b5fd2b Signed-off-by: Parshad Patel --- .../CommandLineEditorEventsContextTest.java | 171 +++++++++++++ .../clieditor/CommandLineEditorOptionsTest.java | 277 +++++++++++++++++++++ .../clieditor/CommandLineEditorScriptingTest.java | 132 ++++++++++ .../apex/auth/clieditor/ContextAlbumsTest.java | 97 ++++++++ .../policy/apex/auth/clieditor/FileMacroTest.java | 113 +++++++++ .../policy/apex/auth/clieditor/LogicBlockTest.java | 129 ++++++++++ .../TestCommandLineEditorEventsContext.java | 171 ------------- .../clieditor/TestCommandLineEditorOptions.java | 277 --------------------- .../clieditor/TestCommandLineEditorScripting.java | 132 ---------- .../apex/auth/clieditor/TestContextAlbums.java | 97 -------- .../policy/apex/auth/clieditor/TestFileMacro.java | 113 --------- .../policy/apex/auth/clieditor/TestLogicBlock.java | 129 ---------- 12 files changed, 919 insertions(+), 919 deletions(-) create mode 100644 auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/CommandLineEditorEventsContextTest.java create mode 100644 auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/CommandLineEditorOptionsTest.java create mode 100644 auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/CommandLineEditorScriptingTest.java create mode 100644 auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/ContextAlbumsTest.java create mode 100644 auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/FileMacroTest.java create mode 100644 auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/LogicBlockTest.java delete mode 100644 auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestCommandLineEditorEventsContext.java delete mode 100644 auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestCommandLineEditorOptions.java delete mode 100644 auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestCommandLineEditorScripting.java delete mode 100644 auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestContextAlbums.java delete mode 100644 auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestFileMacro.java delete mode 100644 auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestLogicBlock.java (limited to 'auth/cli-editor/src/test') diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/CommandLineEditorEventsContextTest.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/CommandLineEditorEventsContextTest.java new file mode 100644 index 000000000..efd12f4e4 --- /dev/null +++ b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/CommandLineEditorEventsContextTest.java @@ -0,0 +1,171 @@ +/*- + * ============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.clieditor; + +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; + +/** + * The Class TestCLIEditorEventsContext. + */ +public class CommandLineEditorEventsContextTest { + // 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. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ApexModelException if an Apex error happens + */ + @Test + public void testJavaContextModel() throws IOException, ApexModelException { + + 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 ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs); + assertEquals(0, cliEditor.getErrorCount()); + + // Get the model and log into strings + final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath()); + 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(SPACES, EMPTY_STRING).length(); + final int modelCharCount = modelString.replaceAll(SPACES, EMPTY_STRING).length(); + + assertEquals(25962, logCharCount); + assertEquals(46189, modelCharCount); + } + + /** + * Test avro context model. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ApexModelException if an Apex error happens + */ + @Test + public void testAvroContextModel() throws IOException, ApexModelException { + + 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 ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs); + assertEquals(0, cliEditor.getErrorCount()); + + // Get the model and log into strings + final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath()); + 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(SPACES, EMPTY_STRING).length(); + final int modelCharCount = modelString.replaceAll(SPACES, EMPTY_STRING).length(); + + assertEquals(30407, logCharCount); + assertEquals(53022, modelCharCount); + + } + + @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 ApexCommandLineEditorMain objUnderTest = new ApexCommandLineEditorMain(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 ApexCommandLineEditorMain objUnderTest = new ApexCommandLineEditorMain(cliArgs); + assertEquals(1, objUnderTest.getErrorCount()); + + } + +} diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/CommandLineEditorOptionsTest.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/CommandLineEditorOptionsTest.java new file mode 100644 index 000000000..227cb23f5 --- /dev/null +++ b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/CommandLineEditorOptionsTest.java @@ -0,0 +1,277 @@ +/*- + * ============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.clieditor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; +import org.onap.policy.apex.model.utilities.TextFileUtils; + +/** + * The Class TestCLIEditorOptions. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class CommandLineEditorOptionsTest { + // CHECKSTYLE:OFF: MagicNumber + + /** + * Test script options log model. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ApexModelException if there is an Apex error + */ + @Test + public void testScriptOptionsLogModel() throws IOException, ApexModelException { + final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log"); + final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json"); + + final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l", + tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()}; + + final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs); + assertEquals(0, cliEditor.getErrorCount()); + + // Get the model and log into strings + final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath()); + 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(); + + assertEquals(1204, logCharCount); + assertEquals(2924, modelCharCount); + + tempLogFile.delete(); + tempModelFile.delete(); + } + + /** + * Test script options no log no model spec. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ApexModelException if there is an Apex error + */ + @Test + public void testScriptOptionsNoLogNoModelSpec() throws IOException, ApexModelException { + final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log"); + final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json"); + + final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l", + tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-nl", "-nm"}; + + final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs); + assertEquals(0, cliEditor.getErrorCount()); + + // Get the model and log into strings + final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath()); + 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(); + + assertEquals(0, logCharCount); + assertEquals(0, modelCharCount); + + tempLogFile.delete(); + tempModelFile.delete(); + } + + /** + * Test script options log no model spec. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ApexModelException if there is an Apex error + */ + @Test + public void testScriptOptionsLogNoModelSpec() throws IOException, ApexModelException { + final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log"); + final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json"); + + final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l", + tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-nm"}; + + final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs); + assertEquals(0, cliEditor.getErrorCount()); + + // Get the model and log into strings + final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath()); + final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath()); + + System.err.println(modelString); + // 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(); + + assertEquals(1204, logCharCount); + assertEquals(0, modelCharCount); + + tempLogFile.delete(); + tempModelFile.delete(); + } + + /** + * Test script options no log model spec. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ApexModelException if there is an Apex error + */ + @Test + public void testScriptOptionsNoLogModelSpec() throws IOException, ApexModelException { + final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log"); + final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json"); + + final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l", + tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-nl"}; + + final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs); + assertEquals(0, cliEditor.getErrorCount()); + + // Get the model and log into strings + final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath()); + 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(); + + assertEquals(0, logCharCount); + assertEquals(2924, modelCharCount); + + tempLogFile.delete(); + tempModelFile.delete(); + } + + /** + * Test script options no log no model no spec. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ApexModelException if there is an Apex error + */ + @Test + public void testScriptOptionsNoLogNoModelNoSpec() throws IOException, ApexModelException { + final String[] cliArgs = + new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-nl", "-nm"}; + + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + System.setOut(new PrintStream(baos)); + final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs); + assertEquals(0, cliEditor.getErrorCount()); + + // Cursor for log + assertFalse(baos.toString().contains(">")); + + // Curly bracket from JSON model + assertFalse(baos.toString().contains("{")); + } + + /** + * Test script options log model no spec. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ApexModelException if there is an Apex error + */ + @Test + public void testScriptOptionsLogModelNoSpec() throws IOException, ApexModelException { + final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex"}; + + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + final PrintStream stdout = System.out; + System.setOut(new PrintStream(baos)); + final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs); + assertEquals(0, cliEditor.getErrorCount()); + + // Cursor for log + assertTrue(baos.toString().contains(">")); + + // Curly bracket from JSON model + assertTrue(baos.toString().contains("{")); + + System.setOut(stdout); + } + + /** + * Test script options input output model. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ApexModelException if there is an Apex error + */ + @Test + public void testScriptOptionsInputOutputModel() throws IOException, ApexModelException { + final File tempLogFileIn = File.createTempFile("ShellPolicyModelIn", ".log"); + final File tempLogFileOut = File.createTempFile("ShellPolicyModelOut", ".log"); + final File tempModelFileIn = File.createTempFile("ShellPolicyModelIn", ".json"); + final File tempModelFileOut = File.createTempFile("ShellPolicyModelOut", ".json"); + + // Generate input model + final String[] cliArgsIn = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", + "-l", tempLogFileIn.getAbsolutePath(), "-o", tempModelFileIn.getAbsolutePath()}; + + final ApexCommandLineEditorMain cliEditorIn = new ApexCommandLineEditorMain(cliArgsIn); + assertEquals(0, cliEditorIn.getErrorCount()); + + // Get the model and log into strings + final String tempLogFileInString = TextFileUtils.getTextFileAsString(tempLogFileIn.getCanonicalPath()); + final String tempModelFileInString = TextFileUtils.getTextFileAsString(tempModelFileIn.getCanonicalPath()); + + // As a sanity check, count the number of non white space characters in log and model files + final int tempLogFileInCharCount = tempLogFileInString.replaceAll("\\s+", "").length(); + final int tempModelFileInCharCount = tempModelFileInString.replaceAll("\\s+", "").length(); + + assertEquals(1204, tempLogFileInCharCount); + assertEquals(2924, tempModelFileInCharCount); + + final String[] cliArgsOut = new String[] {"-i", tempModelFileIn.getAbsolutePath(), "-c", + "src/main/resources/examples/scripts/ShellPolicyModelAddSchema.apex", "-l", + tempLogFileOut.getAbsolutePath(), "-o", tempModelFileOut.getAbsolutePath()}; + + final ApexCommandLineEditorMain cliEditorOut = new ApexCommandLineEditorMain(cliArgsOut); + assertEquals(0, cliEditorOut.getErrorCount()); + + // Get the model and log into strings + final String tempLogFileOutString = TextFileUtils.getTextFileAsString(tempLogFileOut.getCanonicalPath()); + final String tempModelFileOutString = TextFileUtils.getTextFileAsString(tempModelFileOut.getCanonicalPath()); + + // As a sanity check, count the number of non white space characters in log and model files + final int tempLogFileOutCharCount = tempLogFileOutString.replaceAll("\\s+", "").length(); + final int tempModelFileOutCharCount = tempModelFileOutString.replaceAll("\\s+", "").length(); + + assertEquals(1154, tempLogFileOutCharCount); + assertEquals(3356, tempModelFileOutCharCount); + + tempLogFileIn.delete(); + tempModelFileIn.delete(); + tempLogFileOut.delete(); + tempModelFileOut.delete(); + } +} diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/CommandLineEditorScriptingTest.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/CommandLineEditorScriptingTest.java new file mode 100644 index 000000000..35b725f2a --- /dev/null +++ b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/CommandLineEditorScriptingTest.java @@ -0,0 +1,132 @@ +/*- + * ============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.clieditor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.net.URL; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.common.utils.resources.ResourceUtils; + +/** + * The Class TestCLIEditorScripting. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class CommandLineEditorScriptingTest { + + private File tempModelFile; + private File tempLogFile; + + private String[] samplePolicyArgs; + + private String[] samplePolicyMapArgs; + + /** + * Initialise args. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Before + public void initialiseArgs() throws IOException { + tempModelFile = File.createTempFile("SampleLBPolicyMap", ".json"); + tempLogFile = File.createTempFile("SampleLBPolicyMap", ".log"); + + samplePolicyArgs = new String[] {"-c", "src/test/resources/scripts/SampleLBPolicy.apex", "-o", + tempModelFile.getAbsolutePath(), "-l", tempLogFile.getAbsolutePath()}; + + samplePolicyMapArgs = new String[] {"-c", "src/test/resources/scripts/SampleLBPolicy_WithMap.apex", "-o", + tempModelFile.getAbsolutePath(), "-l", tempLogFile.getAbsolutePath()}; + } + + /** + * Removes the generated files. + */ + @After + public void removeGeneratedFiles() { + tempModelFile.delete(); + tempLogFile.delete(); + } + + /** + * Test sample Fuzzy LB policy script. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ApexModelException if there is an Apex error + */ + @Test + public void testSamplePolicyScript() throws IOException, ApexModelException { + final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(samplePolicyArgs); + assertEquals(0, cliEditor.getErrorCount()); + + // Read the file from disk + final ApexModelReader modelReader = new ApexModelReader<>(AxPolicyModel.class); + + final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath()); + final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream()); + + final URL compareModelUrl = + ResourceUtils.getLocalFile("src/test/resources/compare/FuzzyPolicyModel_Compare.json"); + final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream()); + + // Ignore key info UUIDs + writtenModel.getKeyInformation().getKeyInfoMap().clear(); + compareModel.getKeyInformation().getKeyInfoMap().clear(); + + assertTrue(writtenModel.equals(compareModel)); + } + + /** + * Test sample Fuzzy LB map policy script. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ApexModelException if there is an Apex error + */ + @Test + public void testSampleMapPolicyScript() throws IOException, ApexModelException { + tempModelFile.delete(); + + final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(samplePolicyMapArgs); + assertEquals(0, cliEditor.getErrorCount()); + + assertTrue(tempModelFile.isFile()); + + // Read the file from disk + final ApexModelReader modelReader = new ApexModelReader<>(AxPolicyModel.class); + + final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath()); + final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream()); + + final AxValidationResult validationResult = new AxValidationResult(); + writtenModel.validate(validationResult); + assertEquals(AxValidationResult.ValidationResult.OBSERVATION, validationResult.getValidationResult()); + } +} diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/ContextAlbumsTest.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/ContextAlbumsTest.java new file mode 100644 index 000000000..21f1e1969 --- /dev/null +++ b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/ContextAlbumsTest.java @@ -0,0 +1,97 @@ +/*- + * ============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.clieditor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.net.URL; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.common.utils.resources.ResourceUtils; + +/** + * The Class TestContextAlbums. + */ +public class ContextAlbumsTest { + private String[] logicBlockArgs; + + private File tempModelFile; + + /** + * Creates the temp files. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Before + public void createTempFiles() throws IOException { + tempModelFile = File.createTempFile("TestPolicyModel", ".json"); + + logicBlockArgs = new String[] {"-c", "src/test/resources/scripts/ContextAlbums.apex", "-o", + tempModelFile.getAbsolutePath(), "-nl"}; + } + + /** + * Removes the generated models. + */ + @After + public void removeGeneratedModels() { + tempModelFile.delete(); + } + + /** + * Test logic block. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ApexModelException if there is an Apex error + */ + @Test + public void testLogicBlock() throws IOException, ApexModelException { + final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(logicBlockArgs); + assertEquals(1, cliEditor.getErrorCount()); + + // Read the file from disk + final ApexModelReader modelReader = new ApexModelReader<>(AxPolicyModel.class); + modelReader.setValidateFlag(false); + + final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath()); + final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream()); + assertNotNull(writtenModel); + + final URL compareModelUrl = + ResourceUtils.getLocalFile("src/test/resources/compare/ContextAlbumsModel_Compare.json"); + final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream()); + + // Ignore key info UUIDs + writtenModel.getKeyInformation().getKeyInfoMap().clear(); + compareModel.getKeyInformation().getKeyInfoMap().clear(); + + assertTrue(writtenModel.equals(compareModel)); + } +} diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/FileMacroTest.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/FileMacroTest.java new file mode 100644 index 000000000..5bded368e --- /dev/null +++ b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/FileMacroTest.java @@ -0,0 +1,113 @@ +/*- + * ============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.clieditor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.nio.file.Paths; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.model.utilities.TextFileUtils; +import org.onap.policy.common.utils.resources.ResourceUtils; + +/** + * Test FileMacro in the CLI. + */ +public class FileMacroTest { + private String[] fileMacroArgs; + + private File tempModelFile; + private File tempLogFile; + + /** + * Creates the temp files. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Before + public void createTempFiles() throws IOException { + tempModelFile = File.createTempFile("TestPolicyModel", ".json"); + tempLogFile = File.createTempFile("TestPolicyModel", ".log"); + + fileMacroArgs = new String[] {"-c", "src/test/resources/scripts/FileMacro.apex", "-l", + tempLogFile.getCanonicalPath(), "-o", tempModelFile.getCanonicalPath(), "-if", "true"}; + } + + /** + * Removes the generated models. + */ + @After + public void removeGeneratedModels() { + tempModelFile.delete(); + } + + /** + * Test logic block macro in CLI scripts. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ApexModelException if there is an Apex error + */ + @Test + public void testLogicBlock() throws IOException, ApexModelException { + final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(fileMacroArgs); + // We expect eight errors + assertEquals(8, cliEditor.getErrorCount()); + + // Read the file from disk + final ApexModelReader modelReader = new ApexModelReader<>(AxPolicyModel.class); + modelReader.setValidateFlag(false); + + final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath()); + final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream()); + + final URL compareModelUrl = + ResourceUtils.getLocalFile("src/test/resources/compare/FileMacroModel_Compare.json"); + final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream()); + + // Ignore key info UUIDs + writtenModel.getKeyInformation().getKeyInfoMap().clear(); + compareModel.getKeyInformation().getKeyInfoMap().clear(); + + assertTrue(writtenModel.equals(compareModel)); + + // The output event is in this file + final File outputLogFile = new File(tempLogFile.getCanonicalPath()); + + final String outputLogString = TextFileUtils.getTextFileAsString(outputLogFile.getCanonicalPath()) + .replace(Paths.get("").toAbsolutePath().toString() + File.separator, "").replaceAll("\\s+", ""); + + // We compare the log to what we expect to get + final String outputLogCompareString = TextFileUtils + .getTextFileAsString("src/test/resources/compare/FileMacro_Compare.log").replaceAll("\\s+", ""); + + // Check what we got is what we expected to get + assertEquals(outputLogCompareString, outputLogString); + } +} diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/LogicBlockTest.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/LogicBlockTest.java new file mode 100644 index 000000000..8f6f193c1 --- /dev/null +++ b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/LogicBlockTest.java @@ -0,0 +1,129 @@ +/*- + * ============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.clieditor; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.net.URL; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.common.utils.resources.ResourceUtils; + +// TODO: Auto-generated Javadoc +/** + * The Class TestLogicBlock. + */ +public class LogicBlockTest { + private String[] logicBlockArgs; + private String[] avroSchemaArgs; + + private File tempLogicModelFile; + private File tempAvroModelFile; + + /** + * Creates the temp files. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Before + public void createTempFiles() throws IOException { + tempLogicModelFile = File.createTempFile("TestLogicPolicyModel", ".json"); + tempAvroModelFile = File.createTempFile("TestAvroPolicyModel", ".json"); + + logicBlockArgs = new String[] {"-c", "src/test/resources/scripts/LogicBlock.apex", "-o", + tempLogicModelFile.getCanonicalPath(), "-if", "true", "-nl"}; + + avroSchemaArgs = new String[] {"-c", "src/test/resources/scripts/AvroSchema.apex", "-o", + tempAvroModelFile.getCanonicalPath(), "-nl"}; + } + + /** + * Removes the temp files. + */ + @After + public void removeTempFiles() { + tempLogicModelFile.delete(); + tempAvroModelFile.delete(); + } + + /** + * Test logic block. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ApexModelException if there is an Apex error + */ + @Test + public void testLogicBlock() throws IOException, ApexModelException { + new ApexCommandLineEditorMain(logicBlockArgs); + + // Read the file from disk + final ApexModelReader modelReader = new ApexModelReader<>(AxPolicyModel.class); + modelReader.setValidateFlag(false); + + final URL writtenModelUrl = ResourceUtils.getLocalFile(tempLogicModelFile.getCanonicalPath()); + final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream()); + + final URL compareModelUrl = + ResourceUtils.getLocalFile("src/test/resources/compare/LogicBlockModel_Compare.json"); + final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream()); + + // Ignore key info UUIDs + writtenModel.getKeyInformation().getKeyInfoMap().clear(); + compareModel.getKeyInformation().getKeyInfoMap().clear(); + + assertTrue(writtenModel.equals(compareModel)); + } + + /** + * Test avro schema. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ApexModelException if there is an Apex error + */ + @Test + public void testAvroSchema() throws IOException, ApexModelException { + new ApexCommandLineEditorMain(avroSchemaArgs); + + // Read the file from disk + final ApexModelReader modelReader = new ApexModelReader<>(AxPolicyModel.class); + modelReader.setValidateFlag(false); + + final URL writtenModelUrl = ResourceUtils.getLocalFile(tempAvroModelFile.getCanonicalPath()); + final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream()); + + final URL compareModelUrl = + ResourceUtils.getLocalFile("src/test/resources/compare/AvroSchemaModel_Compare.json"); + final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream()); + + // Ignore key info UUIDs + writtenModel.getKeyInformation().getKeyInfoMap().clear(); + compareModel.getKeyInformation().getKeyInfoMap().clear(); + + assertTrue(writtenModel.equals(compareModel)); + } +} diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestCommandLineEditorEventsContext.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestCommandLineEditorEventsContext.java deleted file mode 100644 index b0d82b6fa..000000000 --- a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestCommandLineEditorEventsContext.java +++ /dev/null @@ -1,171 +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.clieditor; - -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; - -/** - * The Class TestCLIEditorEventsContext. - */ -public class TestCommandLineEditorEventsContext { - // 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. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws ApexModelException if an Apex error happens - */ - @Test - public void testJavaContextModel() throws IOException, ApexModelException { - - 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 ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs); - assertEquals(0, cliEditor.getErrorCount()); - - // Get the model and log into strings - final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath()); - 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(SPACES, EMPTY_STRING).length(); - final int modelCharCount = modelString.replaceAll(SPACES, EMPTY_STRING).length(); - - assertEquals(25962, logCharCount); - assertEquals(46189, modelCharCount); - } - - /** - * Test avro context model. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws ApexModelException if an Apex error happens - */ - @Test - public void testAvroContextModel() throws IOException, ApexModelException { - - 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 ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs); - assertEquals(0, cliEditor.getErrorCount()); - - // Get the model and log into strings - final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath()); - 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(SPACES, EMPTY_STRING).length(); - final int modelCharCount = modelString.replaceAll(SPACES, EMPTY_STRING).length(); - - assertEquals(30407, logCharCount); - assertEquals(53022, modelCharCount); - - } - - @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 ApexCommandLineEditorMain objUnderTest = new ApexCommandLineEditorMain(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 ApexCommandLineEditorMain objUnderTest = new ApexCommandLineEditorMain(cliArgs); - assertEquals(1, objUnderTest.getErrorCount()); - - } - -} diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestCommandLineEditorOptions.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestCommandLineEditorOptions.java deleted file mode 100644 index 5ff87c01b..000000000 --- a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestCommandLineEditorOptions.java +++ /dev/null @@ -1,277 +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.clieditor; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.PrintStream; - -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; -import org.onap.policy.apex.model.utilities.TextFileUtils; - -/** - * The Class TestCLIEditorOptions. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestCommandLineEditorOptions { - // CHECKSTYLE:OFF: MagicNumber - - /** - * Test script options log model. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws ApexModelException if there is an Apex error - */ - @Test - public void testScriptOptionsLogModel() throws IOException, ApexModelException { - final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log"); - final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json"); - - final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l", - tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()}; - - final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs); - assertEquals(0, cliEditor.getErrorCount()); - - // Get the model and log into strings - final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath()); - 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(); - - assertEquals(1204, logCharCount); - assertEquals(2924, modelCharCount); - - tempLogFile.delete(); - tempModelFile.delete(); - } - - /** - * Test script options no log no model spec. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws ApexModelException if there is an Apex error - */ - @Test - public void testScriptOptionsNoLogNoModelSpec() throws IOException, ApexModelException { - final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log"); - final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json"); - - final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l", - tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-nl", "-nm"}; - - final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs); - assertEquals(0, cliEditor.getErrorCount()); - - // Get the model and log into strings - final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath()); - 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(); - - assertEquals(0, logCharCount); - assertEquals(0, modelCharCount); - - tempLogFile.delete(); - tempModelFile.delete(); - } - - /** - * Test script options log no model spec. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws ApexModelException if there is an Apex error - */ - @Test - public void testScriptOptionsLogNoModelSpec() throws IOException, ApexModelException { - final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log"); - final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json"); - - final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l", - tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-nm"}; - - final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs); - assertEquals(0, cliEditor.getErrorCount()); - - // Get the model and log into strings - final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath()); - final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath()); - - System.err.println(modelString); - // 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(); - - assertEquals(1204, logCharCount); - assertEquals(0, modelCharCount); - - tempLogFile.delete(); - tempModelFile.delete(); - } - - /** - * Test script options no log model spec. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws ApexModelException if there is an Apex error - */ - @Test - public void testScriptOptionsNoLogModelSpec() throws IOException, ApexModelException { - final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log"); - final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json"); - - final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l", - tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-nl"}; - - final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs); - assertEquals(0, cliEditor.getErrorCount()); - - // Get the model and log into strings - final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath()); - 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(); - - assertEquals(0, logCharCount); - assertEquals(2924, modelCharCount); - - tempLogFile.delete(); - tempModelFile.delete(); - } - - /** - * Test script options no log no model no spec. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws ApexModelException if there is an Apex error - */ - @Test - public void testScriptOptionsNoLogNoModelNoSpec() throws IOException, ApexModelException { - final String[] cliArgs = - new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-nl", "-nm"}; - - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - System.setOut(new PrintStream(baos)); - final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs); - assertEquals(0, cliEditor.getErrorCount()); - - // Cursor for log - assertFalse(baos.toString().contains(">")); - - // Curly bracket from JSON model - assertFalse(baos.toString().contains("{")); - } - - /** - * Test script options log model no spec. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws ApexModelException if there is an Apex error - */ - @Test - public void testScriptOptionsLogModelNoSpec() throws IOException, ApexModelException { - final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex"}; - - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - final PrintStream stdout = System.out; - System.setOut(new PrintStream(baos)); - final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs); - assertEquals(0, cliEditor.getErrorCount()); - - // Cursor for log - assertTrue(baos.toString().contains(">")); - - // Curly bracket from JSON model - assertTrue(baos.toString().contains("{")); - - System.setOut(stdout); - } - - /** - * Test script options input output model. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws ApexModelException if there is an Apex error - */ - @Test - public void testScriptOptionsInputOutputModel() throws IOException, ApexModelException { - final File tempLogFileIn = File.createTempFile("ShellPolicyModelIn", ".log"); - final File tempLogFileOut = File.createTempFile("ShellPolicyModelOut", ".log"); - final File tempModelFileIn = File.createTempFile("ShellPolicyModelIn", ".json"); - final File tempModelFileOut = File.createTempFile("ShellPolicyModelOut", ".json"); - - // Generate input model - final String[] cliArgsIn = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", - "-l", tempLogFileIn.getAbsolutePath(), "-o", tempModelFileIn.getAbsolutePath()}; - - final ApexCommandLineEditorMain cliEditorIn = new ApexCommandLineEditorMain(cliArgsIn); - assertEquals(0, cliEditorIn.getErrorCount()); - - // Get the model and log into strings - final String tempLogFileInString = TextFileUtils.getTextFileAsString(tempLogFileIn.getCanonicalPath()); - final String tempModelFileInString = TextFileUtils.getTextFileAsString(tempModelFileIn.getCanonicalPath()); - - // As a sanity check, count the number of non white space characters in log and model files - final int tempLogFileInCharCount = tempLogFileInString.replaceAll("\\s+", "").length(); - final int tempModelFileInCharCount = tempModelFileInString.replaceAll("\\s+", "").length(); - - assertEquals(1204, tempLogFileInCharCount); - assertEquals(2924, tempModelFileInCharCount); - - final String[] cliArgsOut = new String[] {"-i", tempModelFileIn.getAbsolutePath(), "-c", - "src/main/resources/examples/scripts/ShellPolicyModelAddSchema.apex", "-l", - tempLogFileOut.getAbsolutePath(), "-o", tempModelFileOut.getAbsolutePath()}; - - final ApexCommandLineEditorMain cliEditorOut = new ApexCommandLineEditorMain(cliArgsOut); - assertEquals(0, cliEditorOut.getErrorCount()); - - // Get the model and log into strings - final String tempLogFileOutString = TextFileUtils.getTextFileAsString(tempLogFileOut.getCanonicalPath()); - final String tempModelFileOutString = TextFileUtils.getTextFileAsString(tempModelFileOut.getCanonicalPath()); - - // As a sanity check, count the number of non white space characters in log and model files - final int tempLogFileOutCharCount = tempLogFileOutString.replaceAll("\\s+", "").length(); - final int tempModelFileOutCharCount = tempModelFileOutString.replaceAll("\\s+", "").length(); - - assertEquals(1154, tempLogFileOutCharCount); - assertEquals(3356, tempModelFileOutCharCount); - - tempLogFileIn.delete(); - tempModelFileIn.delete(); - tempLogFileOut.delete(); - tempModelFileOut.delete(); - } -} diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestCommandLineEditorScripting.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestCommandLineEditorScripting.java deleted file mode 100644 index 1b49fb362..000000000 --- a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestCommandLineEditorScripting.java +++ /dev/null @@ -1,132 +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.clieditor; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.io.IOException; -import java.net.URL; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; -import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; -import org.onap.policy.common.utils.resources.ResourceUtils; - -/** - * The Class TestCLIEditorScripting. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestCommandLineEditorScripting { - - private File tempModelFile; - private File tempLogFile; - - private String[] samplePolicyArgs; - - private String[] samplePolicyMapArgs; - - /** - * Initialise args. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Before - public void initialiseArgs() throws IOException { - tempModelFile = File.createTempFile("SampleLBPolicyMap", ".json"); - tempLogFile = File.createTempFile("SampleLBPolicyMap", ".log"); - - samplePolicyArgs = new String[] {"-c", "src/test/resources/scripts/SampleLBPolicy.apex", "-o", - tempModelFile.getAbsolutePath(), "-l", tempLogFile.getAbsolutePath()}; - - samplePolicyMapArgs = new String[] {"-c", "src/test/resources/scripts/SampleLBPolicy_WithMap.apex", "-o", - tempModelFile.getAbsolutePath(), "-l", tempLogFile.getAbsolutePath()}; - } - - /** - * Removes the generated files. - */ - @After - public void removeGeneratedFiles() { - tempModelFile.delete(); - tempLogFile.delete(); - } - - /** - * Test sample Fuzzy LB policy script. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws ApexModelException if there is an Apex error - */ - @Test - public void testSamplePolicyScript() throws IOException, ApexModelException { - final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(samplePolicyArgs); - assertEquals(0, cliEditor.getErrorCount()); - - // Read the file from disk - final ApexModelReader modelReader = new ApexModelReader<>(AxPolicyModel.class); - - final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath()); - final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream()); - - final URL compareModelUrl = - ResourceUtils.getLocalFile("src/test/resources/compare/FuzzyPolicyModel_Compare.json"); - final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream()); - - // Ignore key info UUIDs - writtenModel.getKeyInformation().getKeyInfoMap().clear(); - compareModel.getKeyInformation().getKeyInfoMap().clear(); - - assertTrue(writtenModel.equals(compareModel)); - } - - /** - * Test sample Fuzzy LB map policy script. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws ApexModelException if there is an Apex error - */ - @Test - public void testSampleMapPolicyScript() throws IOException, ApexModelException { - tempModelFile.delete(); - - final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(samplePolicyMapArgs); - assertEquals(0, cliEditor.getErrorCount()); - - assertTrue(tempModelFile.isFile()); - - // Read the file from disk - final ApexModelReader modelReader = new ApexModelReader<>(AxPolicyModel.class); - - final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath()); - final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream()); - - final AxValidationResult validationResult = new AxValidationResult(); - writtenModel.validate(validationResult); - assertEquals(AxValidationResult.ValidationResult.OBSERVATION, validationResult.getValidationResult()); - } -} diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestContextAlbums.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestContextAlbums.java deleted file mode 100644 index a45212007..000000000 --- a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestContextAlbums.java +++ /dev/null @@ -1,97 +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.clieditor; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.io.IOException; -import java.net.URL; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; -import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; -import org.onap.policy.common.utils.resources.ResourceUtils; - -/** - * The Class TestContextAlbums. - */ -public class TestContextAlbums { - private String[] logicBlockArgs; - - private File tempModelFile; - - /** - * Creates the temp files. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Before - public void createTempFiles() throws IOException { - tempModelFile = File.createTempFile("TestPolicyModel", ".json"); - - logicBlockArgs = new String[] {"-c", "src/test/resources/scripts/ContextAlbums.apex", "-o", - tempModelFile.getAbsolutePath(), "-nl"}; - } - - /** - * Removes the generated models. - */ - @After - public void removeGeneratedModels() { - tempModelFile.delete(); - } - - /** - * Test logic block. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws ApexModelException if there is an Apex error - */ - @Test - public void testLogicBlock() throws IOException, ApexModelException { - final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(logicBlockArgs); - assertEquals(1, cliEditor.getErrorCount()); - - // Read the file from disk - final ApexModelReader modelReader = new ApexModelReader<>(AxPolicyModel.class); - modelReader.setValidateFlag(false); - - final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath()); - final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream()); - assertNotNull(writtenModel); - - final URL compareModelUrl = - ResourceUtils.getLocalFile("src/test/resources/compare/ContextAlbumsModel_Compare.json"); - final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream()); - - // Ignore key info UUIDs - writtenModel.getKeyInformation().getKeyInfoMap().clear(); - compareModel.getKeyInformation().getKeyInfoMap().clear(); - - assertTrue(writtenModel.equals(compareModel)); - } -} diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestFileMacro.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestFileMacro.java deleted file mode 100644 index 3195bf025..000000000 --- a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestFileMacro.java +++ /dev/null @@ -1,113 +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.clieditor; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.io.IOException; -import java.net.URL; -import java.nio.file.Paths; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; -import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; -import org.onap.policy.apex.model.utilities.TextFileUtils; -import org.onap.policy.common.utils.resources.ResourceUtils; - -/** - * Test FileMacro in the CLI. - */ -public class TestFileMacro { - private String[] fileMacroArgs; - - private File tempModelFile; - private File tempLogFile; - - /** - * Creates the temp files. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Before - public void createTempFiles() throws IOException { - tempModelFile = File.createTempFile("TestPolicyModel", ".json"); - tempLogFile = File.createTempFile("TestPolicyModel", ".log"); - - fileMacroArgs = new String[] {"-c", "src/test/resources/scripts/FileMacro.apex", "-l", - tempLogFile.getCanonicalPath(), "-o", tempModelFile.getCanonicalPath(), "-if", "true"}; - } - - /** - * Removes the generated models. - */ - @After - public void removeGeneratedModels() { - tempModelFile.delete(); - } - - /** - * Test logic block macro in CLI scripts. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws ApexModelException if there is an Apex error - */ - @Test - public void testLogicBlock() throws IOException, ApexModelException { - final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(fileMacroArgs); - // We expect eight errors - assertEquals(8, cliEditor.getErrorCount()); - - // Read the file from disk - final ApexModelReader modelReader = new ApexModelReader<>(AxPolicyModel.class); - modelReader.setValidateFlag(false); - - final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath()); - final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream()); - - final URL compareModelUrl = - ResourceUtils.getLocalFile("src/test/resources/compare/FileMacroModel_Compare.json"); - final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream()); - - // Ignore key info UUIDs - writtenModel.getKeyInformation().getKeyInfoMap().clear(); - compareModel.getKeyInformation().getKeyInfoMap().clear(); - - assertTrue(writtenModel.equals(compareModel)); - - // The output event is in this file - final File outputLogFile = new File(tempLogFile.getCanonicalPath()); - - final String outputLogString = TextFileUtils.getTextFileAsString(outputLogFile.getCanonicalPath()) - .replace(Paths.get("").toAbsolutePath().toString() + File.separator, "").replaceAll("\\s+", ""); - - // We compare the log to what we expect to get - final String outputLogCompareString = TextFileUtils - .getTextFileAsString("src/test/resources/compare/FileMacro_Compare.log").replaceAll("\\s+", ""); - - // Check what we got is what we expected to get - assertEquals(outputLogCompareString, outputLogString); - } -} diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestLogicBlock.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestLogicBlock.java deleted file mode 100644 index 1a4f75576..000000000 --- a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/TestLogicBlock.java +++ /dev/null @@ -1,129 +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.clieditor; - -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.io.IOException; -import java.net.URL; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; -import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; -import org.onap.policy.common.utils.resources.ResourceUtils; - -// TODO: Auto-generated Javadoc -/** - * The Class TestLogicBlock. - */ -public class TestLogicBlock { - private String[] logicBlockArgs; - private String[] avroSchemaArgs; - - private File tempLogicModelFile; - private File tempAvroModelFile; - - /** - * Creates the temp files. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Before - public void createTempFiles() throws IOException { - tempLogicModelFile = File.createTempFile("TestLogicPolicyModel", ".json"); - tempAvroModelFile = File.createTempFile("TestAvroPolicyModel", ".json"); - - logicBlockArgs = new String[] {"-c", "src/test/resources/scripts/LogicBlock.apex", "-o", - tempLogicModelFile.getCanonicalPath(), "-if", "true", "-nl"}; - - avroSchemaArgs = new String[] {"-c", "src/test/resources/scripts/AvroSchema.apex", "-o", - tempAvroModelFile.getCanonicalPath(), "-nl"}; - } - - /** - * Removes the temp files. - */ - @After - public void removeTempFiles() { - tempLogicModelFile.delete(); - tempAvroModelFile.delete(); - } - - /** - * Test logic block. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws ApexModelException if there is an Apex error - */ - @Test - public void testLogicBlock() throws IOException, ApexModelException { - new ApexCommandLineEditorMain(logicBlockArgs); - - // Read the file from disk - final ApexModelReader modelReader = new ApexModelReader<>(AxPolicyModel.class); - modelReader.setValidateFlag(false); - - final URL writtenModelUrl = ResourceUtils.getLocalFile(tempLogicModelFile.getCanonicalPath()); - final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream()); - - final URL compareModelUrl = - ResourceUtils.getLocalFile("src/test/resources/compare/LogicBlockModel_Compare.json"); - final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream()); - - // Ignore key info UUIDs - writtenModel.getKeyInformation().getKeyInfoMap().clear(); - compareModel.getKeyInformation().getKeyInfoMap().clear(); - - assertTrue(writtenModel.equals(compareModel)); - } - - /** - * Test avro schema. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws ApexModelException if there is an Apex error - */ - @Test - public void testAvroSchema() throws IOException, ApexModelException { - new ApexCommandLineEditorMain(avroSchemaArgs); - - // Read the file from disk - final ApexModelReader modelReader = new ApexModelReader<>(AxPolicyModel.class); - modelReader.setValidateFlag(false); - - final URL writtenModelUrl = ResourceUtils.getLocalFile(tempAvroModelFile.getCanonicalPath()); - final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream()); - - final URL compareModelUrl = - ResourceUtils.getLocalFile("src/test/resources/compare/AvroSchemaModel_Compare.json"); - final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream()); - - // Ignore key info UUIDs - writtenModel.getKeyInformation().getKeyInfoMap().clear(); - compareModel.getKeyInformation().getKeyInfoMap().clear(); - - assertTrue(writtenModel.equals(compareModel)); - } -} -- cgit 1.2.3-korg