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 --- .../apex/examples/myfirstpolicy/MfpLogicTest.java | 183 ++++++++++ .../examples/myfirstpolicy/MfpModelCliTest.java | 96 ++++++ .../apex/examples/myfirstpolicy/MfpModelTest.java | 106 ++++++ .../examples/myfirstpolicy/MfpUseCaseTest.java | 383 +++++++++++++++++++++ .../apex/examples/myfirstpolicy/TestMfpLogic.java | 183 ---------- .../apex/examples/myfirstpolicy/TestMfpModel.java | 106 ------ .../examples/myfirstpolicy/TestMfpModelCli.java | 96 ------ .../examples/myfirstpolicy/TestMfpUseCase.java | 383 --------------------- 8 files changed, 768 insertions(+), 768 deletions(-) create mode 100644 examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/MfpLogicTest.java create mode 100644 examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/MfpModelCliTest.java create mode 100644 examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/MfpModelTest.java create mode 100644 examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/MfpUseCaseTest.java delete mode 100644 examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpLogic.java delete mode 100644 examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpModel.java delete mode 100644 examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpModelCli.java delete mode 100644 examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpUseCase.java (limited to 'examples/examples-myfirstpolicy/src') diff --git a/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/MfpLogicTest.java b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/MfpLogicTest.java new file mode 100644 index 000000000..f19e2c8c3 --- /dev/null +++ b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/MfpLogicTest.java @@ -0,0 +1,183 @@ +/*- + * ============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.examples.myfirstpolicy; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.apex.examples.myfirstpolicy.model.MfpDomainModelFactory; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicy; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.model.policymodel.concepts.AxState; +import org.onap.policy.apex.model.policymodel.concepts.AxTask; +import org.onap.policy.common.utils.resources.ResourceUtils; + +/** + * The Class TestMfpLogic. + */ +public class MfpLogicTest { + + private static final Map LOGICEXTENSIONS = new LinkedHashMap<>(); + + /** + * Test setup. + */ + @BeforeClass + public static void testMfpUseCaseSetup() { + LOGICEXTENSIONS.put("MVEL", "mvel"); + LOGICEXTENSIONS.put("JAVASCRIPT", "js"); + } + + /** + * Check logic for MyFirstPolicy#1. + */ + @Test + public void testMfp1TaskLogic() { + final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp1PolicyModel(); + assertNotNull(apexPolicyModel); + + final Map logics = new LinkedHashMap<>(); + logics.putAll(getTslLogics(apexPolicyModel)); + logics.putAll(getTaskLogics(apexPolicyModel)); + + for (final Entry logicvalue : logics.entrySet()) { + final String filename = "examples/models/MyFirstPolicy/1/" + logicvalue.getKey(); + final String logic = logicvalue.getValue(); + final String expectedlogic = ResourceUtils.getResourceAsString(filename); + assertNotNull("File " + filename + " was not found. It should contain logic for PolicyModel " + + apexPolicyModel.getKey(), expectedlogic); + assertEquals( + "The task in " + filename + " is not the same as the relevant logic in PolicyModel " + + apexPolicyModel.getKey(), + expectedlogic.replaceAll("\\s", ""), logic.replaceAll("\\s", "")); + } + } + + + /** + * Check logic for MyFirstPolicyAlt#1. + */ + @Test + public void testMfp1AltTaskLogic() { + final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp1AltPolicyModel(); + assertNotNull(apexPolicyModel); + + final Map logics = new LinkedHashMap<>(); + logics.putAll(getTslLogics(apexPolicyModel)); + logics.putAll(getTaskLogics(apexPolicyModel)); + + for (final Entry logicvalue : logics.entrySet()) { + final String filename = "examples/models/MyFirstPolicy/1/" + logicvalue.getKey(); + final String logic = logicvalue.getValue(); + final String expectedlogic = ResourceUtils.getResourceAsString(filename); + assertNotNull("File " + filename + " was not found. It should contain logic for PolicyModel " + + apexPolicyModel.getKey(), expectedlogic); + assertEquals( + "The task in " + filename + " is not the same as the relevant logic in PolicyModel " + + apexPolicyModel.getKey(), + expectedlogic.replaceAll("\\s", ""), logic.replaceAll("\\s", "")); + } + } + + /** + * Check logic for MyFirstPolicy2. + */ + @Test + public void testMfp2TaskLogic() { + final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp2PolicyModel(); + assertNotNull(apexPolicyModel); + + final Map logics = new LinkedHashMap<>(); + logics.putAll(getTslLogics(apexPolicyModel)); + logics.putAll(getTaskLogics(apexPolicyModel)); + + for (final Entry logicvalue : logics.entrySet()) { + final String filename = "examples/models/MyFirstPolicy/2/" + logicvalue.getKey(); + final String logic = logicvalue.getValue(); + final String expectedlogic = ResourceUtils.getResourceAsString(filename); + assertNotNull("File " + filename + " was not found. It should contain logic for PolicyModel " + + apexPolicyModel.getKey(), expectedlogic); + assertEquals( + "The task in " + filename + " is not the same as the relevant logic in PolicyModel " + + apexPolicyModel.getKey(), + expectedlogic.replaceAll("\\s", ""), logic.replaceAll("\\s", "")); + } + } + + /** + * Gets the TSL logics. + * + * @param apexPolicyModel the apex policy model + * @return the TSL logics + */ + private Map getTslLogics(final AxPolicyModel apexPolicyModel) { + final Map ret = new LinkedHashMap<>(); + for (final Entry policyentry : apexPolicyModel.getPolicies().getPolicyMap() + .entrySet()) { + for (final Entry statesentry : policyentry.getValue().getStateMap().entrySet()) { + final AxState state = statesentry.getValue(); + final String tsllogic = state.getTaskSelectionLogic().getLogic(); + final String tsllogicflavour = state.getTaskSelectionLogic().getLogicFlavour(); + if (tsllogic != null && tsllogic.trim().length() > 0) { + assertNotNull( + "Logic Type \"" + tsllogicflavour + "\" in state " + statesentry.getKey() + " in policy " + + policyentry.getKey() + " is not supported in this test", + LOGICEXTENSIONS.get(tsllogicflavour.toUpperCase())); + final String filename = policyentry.getKey().getName() + "_" + statesentry.getKey() + "TSL." + + LOGICEXTENSIONS.get(tsllogicflavour.toUpperCase()); + ret.put(filename, tsllogic); + } + } + } + return ret; + } + + /** + * Gets the task logics. + * + * @param apexPolicyModel the apex policy model + * @return the task logics + */ + private Map getTaskLogics(final AxPolicyModel apexPolicyModel) { + final Map ret = new LinkedHashMap<>(); + for (final Entry taskentry : apexPolicyModel.getTasks().getTaskMap().entrySet()) { + final AxTask task = taskentry.getValue(); + final String tasklogic = task.getTaskLogic().getLogic(); + final String tasklogicflavour = task.getTaskLogic().getLogicFlavour(); + assertTrue("No/Blank logic found in task " + taskentry.getKey(), + (tasklogic != null && tasklogic.trim().length() > 0)); + assertNotNull("Logic Type \"" + tasklogicflavour + "\" in task " + taskentry.getKey() + + " is not supported in this test", LOGICEXTENSIONS.get(tasklogicflavour.toUpperCase())); + final String filename = + taskentry.getKey().getName() + "." + LOGICEXTENSIONS.get(tasklogicflavour.toUpperCase()); + ret.put(filename, tasklogic); + } + return ret; + } +} diff --git a/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/MfpModelCliTest.java b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/MfpModelCliTest.java new file mode 100644 index 000000000..ce78b7671 --- /dev/null +++ b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/MfpModelCliTest.java @@ -0,0 +1,96 @@ +/*- + * ============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.examples.myfirstpolicy; + +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.io.IOException; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain; +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; + +/** + * Test MyFirstPolicyModel CLI. + */ +public class MfpModelCliTest { + private static AxPolicyModel testApexModel1; + private static AxPolicyModel testApexModel2; + + /** + * Setup the test. + * + * @throws Exception if there is an error + */ + @BeforeClass + public static void setup() throws Exception { + testApexModel1 = new TestMfpModelCreator.TestMfp1ModelCreator().getModel(); + testApexModel2 = new TestMfpModelCreator.TestMfp2ModelCreator().getModel(); + } + + /** + * Test CLI policy. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ApexModelException ifd there is an Apex Error + */ + @Test + public void testCliPolicy() throws IOException, ApexModelException { + + final File tempLogFile1 = File.createTempFile("TestMyFirstPolicy1CLI", ".log"); + final File tempModelFile1 = File.createTempFile("TestMyFirstPolicy1CLI", ".json"); + final File tempLogFile2 = File.createTempFile("TestMyFirstPolicy2CLI", ".log"); + final File tempModelFile2 = File.createTempFile("TestMyFirstPolicy2CLI", ".json"); + final String[] testApexModel1CliArgs = + { "-c", "src/main/resources/examples/models/MyFirstPolicy/1/MyFirstPolicyModel_0.0.1.apex", "-l", + tempLogFile1.getAbsolutePath(), "-o", tempModelFile1.getAbsolutePath() }; + final String[] testApexModel2CliArgs = + { "-c", "src/main/resources/examples/models/MyFirstPolicy/2/MyFirstPolicyModel_0.0.1.apex", "-l", + tempLogFile2.getAbsolutePath(), "-o", tempModelFile2.getAbsolutePath() }; + + new ApexCommandLineEditorMain(testApexModel1CliArgs); + new ApexCommandLineEditorMain(testApexModel2CliArgs); + + final ApexModelReader reader = new ApexModelReader<>(AxPolicyModel.class); + AxPolicyModel generatedmodel = reader.read(TextFileUtils.getTextFileAsString(tempModelFile1.getAbsolutePath())); + + assertEquals("Model generated from the CLI (" + testApexModel1CliArgs[1] + ") into file " + + tempModelFile1.getAbsolutePath() + " is not the same as the test Model for " + + testApexModel1.getKey(), testApexModel1, generatedmodel); + + generatedmodel = reader.read(TextFileUtils.getTextFileAsString(tempModelFile2.getAbsolutePath())); + assertEquals("Model generated from the CLI (" + testApexModel2CliArgs[1] + ") into file " + + tempModelFile2.getAbsolutePath() + " is not the same as the test Model for " + + testApexModel2.getKey(), testApexModel2, generatedmodel); + + tempLogFile1.delete(); + tempModelFile1.delete(); + + tempLogFile2.delete(); + tempModelFile2.delete(); + + } +} diff --git a/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/MfpModelTest.java b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/MfpModelTest.java new file mode 100644 index 000000000..d6ec2bb8d --- /dev/null +++ b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/MfpModelTest.java @@ -0,0 +1,106 @@ +/*- + * ============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.examples.myfirstpolicy; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.test.TestApexModel; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; + +/** + * Test MyFirstPolicy Model. + * + * @author John Keeney (john.keeney@ericsson.com) + */ +public class MfpModelTest { + + private static Connection connection; + private static TestApexModel testApexModel1; + private static TestApexModel testApexModel2; + + /** + * Setup. + * + * @throws Exception if there is an error + */ + @BeforeClass + public static void setup() throws Exception { + Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); + connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true"); + testApexModel1 = new TestApexModel<>(AxPolicyModel.class, new TestMfpModelCreator.TestMfp1ModelCreator()); + testApexModel2 = new TestApexModel<>(AxPolicyModel.class, new TestMfpModelCreator.TestMfp2ModelCreator()); + } + + /** + * Teardown. + * + * @throws Exception if there is an error + */ + @AfterClass + public static void teardown() throws Exception { + connection.close(); + new File("derby.log").delete(); + } + + /** + * Test model is valid. + * + * @throws Exception if there is an error + */ + @Test + public void testModelValid() throws Exception { + AxValidationResult result = testApexModel1.testApexModelValid(); + assertTrue("Model did not validate cleanly", result.isOk()); + + result = testApexModel2.testApexModelValid(); + assertTrue("Model did not validate cleanly", result.isOk()); + } + + /** + * Test model write and read XML. + * + * @throws Exception if there is an error + */ + @Test + public void testModelWriteReadXml() throws Exception { + testApexModel1.testApexModelWriteReadXml(); + testApexModel2.testApexModelWriteReadXml(); + } + + /** + * Test model write and read JSON. + * + * @throws Exception if there is an error + */ + @Test + public void testModelWriteReadJson() throws Exception { + testApexModel1.testApexModelWriteReadJson(); + testApexModel2.testApexModelWriteReadJson(); + } +} diff --git a/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/MfpUseCaseTest.java b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/MfpUseCaseTest.java new file mode 100644 index 000000000..1b9a5a5c7 --- /dev/null +++ b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/MfpUseCaseTest.java @@ -0,0 +1,383 @@ +/*- + * ============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.examples.myfirstpolicy; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.ContextParameters; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.core.engine.EngineParameters; +import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory; +import org.onap.policy.apex.core.engine.engine.impl.ApexEngineImpl; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.examples.myfirstpolicy.model.MfpDomainModelFactory; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; +import org.onap.policy.apex.model.eventmodel.concepts.AxField; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.plugins.executor.javascript.JavascriptExecutorParameters; +import org.onap.policy.apex.plugins.executor.mvel.MvelExecutorParameters; +import org.onap.policy.common.parameters.ParameterService; +import org.onap.policy.common.utils.resources.ResourceUtils; + +/** + * Test MyFirstPolicy Use Case. + */ +public class MfpUseCaseTest { + // CHECKSTYLE:OFF: MagicNumber + + private static ApexEngineImpl apexEngine; + + /** + * Test MFP use case setup. + */ + @BeforeClass + public static void testMfpUseCaseSetup() { + final AxArtifactKey key = new AxArtifactKey("MyFirstPolicyApexEngine", "0.0.1"); + apexEngine = (ApexEngineImpl) new ApexEngineFactory().createApexEngine(key); + } + + private static ContextParameters contextParameters; + private static SchemaParameters schemaParameters; + private static EngineParameters engineParameters; + + /** + * Before test. + */ + @BeforeClass + public static void beforeTest() { + schemaParameters = new SchemaParameters(); + + schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); + schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters()); + + ParameterService.register(schemaParameters); + + contextParameters = new ContextParameters(); + + contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME); + contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME); + contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME); + contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME); + + ParameterService.register(contextParameters); + ParameterService.register(contextParameters.getDistributorParameters()); + ParameterService.register(contextParameters.getLockManagerParameters()); + ParameterService.register(contextParameters.getPersistorParameters()); + + engineParameters = new EngineParameters(); + engineParameters.getExecutorParameterMap().put("MVEL", new MvelExecutorParameters()); + engineParameters.getExecutorParameterMap().put("JAVASCRIPT", new JavascriptExecutorParameters()); + ParameterService.register(engineParameters); + } + + /** + * After test. + */ + @AfterClass + public static void afterTest() { + ParameterService.deregister(engineParameters); + + ParameterService.deregister(contextParameters.getDistributorParameters()); + ParameterService.deregister(contextParameters.getLockManagerParameters()); + ParameterService.deregister(contextParameters.getPersistorParameters()); + ParameterService.deregister(contextParameters); + + ParameterService.deregister(schemaParameters); + } + + /** + * Test MyFirstPolicy#1 use case. + * + * @throws ApexException if there is an Apex error + * @throws InterruptedException if there is an Interruption. + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testMfp1Case() throws ApexException, InterruptedException, IOException { + final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp1PolicyModel(); + assertNotNull(apexPolicyModel); + + final TestSaleAuthListener listener = new TestSaleAuthListener("Test"); + apexEngine.addEventListener("listener", listener); + apexEngine.updateModel(apexPolicyModel); + apexEngine.start(); + + final AxEvent axEventin = apexPolicyModel.getEvents().get(new AxArtifactKey("SALE_INPUT:0.0.1")); + assertNotNull(axEventin); + final AxEvent axEventout = apexPolicyModel.getEvents().get(new AxArtifactKey("SALE_AUTH:0.0.1")); + assertNotNull(axEventout); + + EnEvent event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/1/EventIn_BoozeItem_084106GMT.json"); + apexEngine.handleEvent(event); + EnEvent resultout = listener.getResult(); + EnEvent resulexpected = + fillResultEvent(axEventout, "examples/events/MyFirstPolicy/1/EventOut_BoozeItem_084106GMT.json"); + assertEquals(resulexpected, resultout); + + event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/1/EventIn_BoozeItem_201713GMT.json"); + apexEngine.handleEvent(event); + resultout = listener.getResult(); + resulexpected = + fillResultEvent(axEventout, "examples/events/MyFirstPolicy/1/EventOut_BoozeItem_201713GMT.json"); + assertEquals(resulexpected, resultout); + assertEquals("ExecutionIDs are different", event.getExecutionId(), resultout.getExecutionId()); + + event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/1/EventIn_NonBoozeItem_101309GMT.json"); + apexEngine.handleEvent(event); + resultout = listener.getResult(); + resulexpected = + fillResultEvent(axEventout, "examples/events/MyFirstPolicy/1/EventOut_NonBoozeItem_101309GMT.json"); + assertEquals(resulexpected, resultout); + assertEquals("ExecutionIDs are different", event.getExecutionId(), resultout.getExecutionId()); + + apexEngine.stop(); + } + + /** + * Test MyFirstPolicy#2 use case. + * + * @throws ApexException if there is an Apex error + * @throws InterruptedException if there is an Interruption. + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testMfp2Case() throws ApexException, InterruptedException, IOException { + final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp2PolicyModel(); + assertNotNull(apexPolicyModel); + + final TestSaleAuthListener listener = new TestSaleAuthListener("Test"); + apexEngine.addEventListener("listener", listener); + apexEngine.updateModel(apexPolicyModel); + apexEngine.start(); + + final AxEvent axEventin = apexPolicyModel.getEvents().get(new AxArtifactKey("SALE_INPUT:0.0.1")); + assertNotNull(axEventin); + final AxEvent axEventout = apexPolicyModel.getEvents().get(new AxArtifactKey("SALE_AUTH:0.0.1")); + assertNotNull(axEventout); + + EnEvent event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/1/EventIn_BoozeItem_084106GMT.json"); + apexEngine.handleEvent(event); + EnEvent resultout = listener.getResult(); + EnEvent resultexpected = + fillResultEvent(axEventout, "examples/events/MyFirstPolicy/1/EventOut_BoozeItem_084106GMT.json"); + assertEquals(resultexpected, resultout); + assertEquals("ExecutionIDs are different", event.getExecutionId(), resultout.getExecutionId()); + + event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/1/EventIn_BoozeItem_201713GMT.json"); + apexEngine.handleEvent(event); + resultout = listener.getResult(); + resultexpected = + fillResultEvent(axEventout, "examples/events/MyFirstPolicy/1/EventOut_BoozeItem_201713GMT.json"); + assertEquals(resultexpected, resultout); + assertEquals("ExecutionIDs are different", event.getExecutionId(), resultout.getExecutionId()); + + event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/1/EventIn_NonBoozeItem_101309GMT.json"); + apexEngine.handleEvent(event); + resultout = listener.getResult(); + resultexpected = + fillResultEvent(axEventout, "examples/events/MyFirstPolicy/1/EventOut_NonBoozeItem_101309GMT.json"); + assertEquals(resultexpected, resultout); + assertEquals("ExecutionIDs are different", event.getExecutionId(), resultout.getExecutionId()); + + event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/2/EventIn_BoozeItem_101433CET_thurs.json"); + apexEngine.handleEvent(event); + resultout = listener.getResult(); + resultexpected = + fillResultEvent(axEventout, "examples/events/MyFirstPolicy/2/EventOut_BoozeItem_101433CET_thurs.json"); + assertEquals(resultexpected, resultout); + assertEquals("ExecutionIDs are different", event.getExecutionId(), resultout.getExecutionId()); + + event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/2/EventIn_BoozeItem_171937CET_sun.json"); + apexEngine.handleEvent(event); + resultout = listener.getResult(); + resultexpected = + fillResultEvent(axEventout, "examples/events/MyFirstPolicy/2/EventOut_BoozeItem_171937CET_sun.json"); + assertEquals(resultexpected, resultout); + assertEquals("ExecutionIDs are different", event.getExecutionId(), resultout.getExecutionId()); + + event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/2/EventIn_NonBoozeItem_111309CET_mon.json"); + apexEngine.handleEvent(event); + resultout = listener.getResult(); + resultexpected = + fillResultEvent(axEventout, "examples/events/MyFirstPolicy/2/EventOut_NonBoozeItem_111309CET_mon.json"); + assertEquals(resultexpected, resultout); + assertEquals("ExecutionIDs are different", event.getExecutionId(), resultout.getExecutionId()); + + apexEngine.stop(); + } + + /** + * Fill trigger event for test. + * + * @param event the event + * @param inputFile the input file + * @return the filled event + */ + private EnEvent fillTriggerEvent(final AxEvent event, final String inputFile) { + final EnEvent ret = new EnEvent(event.getKey()); + final GsonBuilder gb = new GsonBuilder(); + gb.serializeNulls().enableComplexMapKeySerialization(); + final JsonObject jsonObject = + gb.create().fromJson(ResourceUtils.getResourceAsString(inputFile), JsonObject.class); + assertNotNull(jsonObject); + assertTrue(jsonObject.has("name")); + assertTrue(ret.getName().equals(jsonObject.get("name").getAsString())); + assertTrue(ret.getAxEvent().getKey().getName().equals(jsonObject.get("name").getAsString())); + assertTrue(jsonObject.has("nameSpace")); + assertTrue(ret.getAxEvent().getNameSpace().equals(jsonObject.get("nameSpace").getAsString())); + assertTrue(jsonObject.has("version")); + assertTrue(ret.getAxEvent().getKey().getVersion().equals(jsonObject.get("version").getAsString())); + final List reserved = Arrays.asList("name", "nameSpace", "version", "source", "target"); + for (final Map.Entry e : jsonObject.entrySet()) { + if (reserved.contains(e.getKey())) { + continue; + } + assertTrue("Event file " + inputFile + " has a field " + e.getKey() + " but this is not defined for " + + event.getId(), (event.getParameterMap().containsKey(e.getKey()))); + if (jsonObject.get(e.getKey()).isJsonNull()) { + ret.put(e.getKey(), null); + } + } + for (final AxField field : event.getFields()) { + if (!field.getOptional()) { + assertTrue("Event file " + inputFile + " is missing a mandatory field " + field.getKey().getLocalName() + + " for " + event.getId(), jsonObject.has(field.getKey().getLocalName())); + } else { + ret.put(field.getKey().getLocalName(), null); + } + } + if (jsonObject.has("time") && !jsonObject.get("time").isJsonNull()) { + ret.put("time", jsonObject.get("time").getAsLong()); + } + if (jsonObject.has("sale_ID") && !jsonObject.get("sale_ID").isJsonNull()) { + ret.put("sale_ID", jsonObject.get("sale_ID").getAsLong()); + } + if (jsonObject.has("amount") && !jsonObject.get("amount").isJsonNull()) { + ret.put("amount", jsonObject.get("amount").getAsDouble()); + } + if (jsonObject.has("item_ID") && !jsonObject.get("item_ID").isJsonNull()) { + ret.put("item_ID", jsonObject.get("item_ID").getAsLong()); + } + if (jsonObject.has("quantity") && !jsonObject.get("quantity").isJsonNull()) { + ret.put("quantity", jsonObject.get("quantity").getAsInt()); + } + if (jsonObject.has("assistant_ID") && !jsonObject.get("assistant_ID").isJsonNull()) { + ret.put("assistant_ID", jsonObject.get("assistant_ID").getAsLong()); + } + if (jsonObject.has("branch_ID") && !jsonObject.get("branch_ID").isJsonNull()) { + ret.put("branch_ID", jsonObject.get("branch_ID").getAsLong()); + } + if (jsonObject.has("notes") && !jsonObject.get("notes").isJsonNull()) { + ret.put("notes", jsonObject.get("notes").getAsString()); + } + return ret; + } + + /** + * Fill result event for test. + * + * @param event the event + * @param inputFile the input file + * @return the filled event + */ + private EnEvent fillResultEvent(final AxEvent event, final String inputFile) { + final EnEvent ret = new EnEvent(event.getKey()); + final GsonBuilder gb = new GsonBuilder(); + gb.serializeNulls().enableComplexMapKeySerialization(); + final JsonObject jsonObject = + gb.create().fromJson(ResourceUtils.getResourceAsString(inputFile), JsonObject.class); + assertNotNull(jsonObject); + assertTrue(jsonObject.has("name")); + assertTrue(ret.getName().equals(jsonObject.get("name").getAsString())); + assertTrue(ret.getAxEvent().getKey().getName().equals(jsonObject.get("name").getAsString())); + assertTrue(jsonObject.has("nameSpace")); + assertTrue(ret.getAxEvent().getNameSpace().equals(jsonObject.get("nameSpace").getAsString())); + assertTrue(jsonObject.has("version")); + assertTrue(ret.getAxEvent().getKey().getVersion().equals(jsonObject.get("version").getAsString())); + final List reserved = Arrays.asList("name", "nameSpace", "version", "source", "target"); + for (final Map.Entry e : jsonObject.entrySet()) { + if (reserved.contains(e.getKey())) { + continue; + } + assertTrue("Event file " + inputFile + " has a field " + e.getKey() + " but this is not defined for " + + event.getId(), (event.getParameterMap().containsKey(e.getKey()))); + if (jsonObject.get(e.getKey()).isJsonNull()) { + ret.put(e.getKey(), null); + } + } + for (final AxField field : event.getFields()) { + if (!field.getOptional()) { + assertTrue("Event file " + inputFile + " is missing a mandatory field " + field.getKey().getLocalName() + + " for " + event.getId(), jsonObject.has(field.getKey().getLocalName())); + } else { + ret.put(field.getKey().getLocalName(), null); + } + } + if (jsonObject.has("time") && !jsonObject.get("time").isJsonNull()) { + ret.put("time", jsonObject.get("time").getAsLong()); + } + if (jsonObject.has("sale_ID") && !jsonObject.get("sale_ID").isJsonNull()) { + ret.put("sale_ID", jsonObject.get("sale_ID").getAsLong()); + } + if (jsonObject.has("amount") && !jsonObject.get("amount").isJsonNull()) { + ret.put("amount", jsonObject.get("amount").getAsDouble()); + } + if (jsonObject.has("item_ID") && !jsonObject.get("item_ID").isJsonNull()) { + ret.put("item_ID", jsonObject.get("item_ID").getAsLong()); + } + if (jsonObject.has("quantity") && !jsonObject.get("quantity").isJsonNull()) { + ret.put("quantity", jsonObject.get("quantity").getAsInt()); + } + if (jsonObject.has("assistant_ID") && !jsonObject.get("assistant_ID").isJsonNull()) { + ret.put("assistant_ID", jsonObject.get("assistant_ID").getAsLong()); + } + if (jsonObject.has("branch_ID") && !jsonObject.get("branch_ID").isJsonNull()) { + ret.put("branch_ID", jsonObject.get("branch_ID").getAsLong()); + } + if (jsonObject.has("notes") && !jsonObject.get("notes").isJsonNull()) { + ret.put("notes", jsonObject.get("notes").getAsString()); + } + if (jsonObject.has("authorised") && !jsonObject.get("authorised").isJsonNull()) { + ret.put("authorised", jsonObject.get("authorised").getAsString()); + } + if (jsonObject.has("message") && !jsonObject.get("message").isJsonNull()) { + ret.put("message", jsonObject.get("message").getAsString()); + } + return ret; + } +} diff --git a/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpLogic.java b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpLogic.java deleted file mode 100644 index e425088ff..000000000 --- a/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpLogic.java +++ /dev/null @@ -1,183 +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.examples.myfirstpolicy; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Map.Entry; - -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.apex.examples.myfirstpolicy.model.MfpDomainModelFactory; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicy; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; -import org.onap.policy.apex.model.policymodel.concepts.AxState; -import org.onap.policy.apex.model.policymodel.concepts.AxTask; -import org.onap.policy.common.utils.resources.ResourceUtils; - -/** - * The Class TestMfpLogic. - */ -public class TestMfpLogic { - - private static final Map LOGICEXTENSIONS = new LinkedHashMap<>(); - - /** - * Test setup. - */ - @BeforeClass - public static void testMfpUseCaseSetup() { - LOGICEXTENSIONS.put("MVEL", "mvel"); - LOGICEXTENSIONS.put("JAVASCRIPT", "js"); - } - - /** - * Check logic for MyFirstPolicy#1. - */ - @Test - public void testMfp1TaskLogic() { - final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp1PolicyModel(); - assertNotNull(apexPolicyModel); - - final Map logics = new LinkedHashMap<>(); - logics.putAll(getTslLogics(apexPolicyModel)); - logics.putAll(getTaskLogics(apexPolicyModel)); - - for (final Entry logicvalue : logics.entrySet()) { - final String filename = "examples/models/MyFirstPolicy/1/" + logicvalue.getKey(); - final String logic = logicvalue.getValue(); - final String expectedlogic = ResourceUtils.getResourceAsString(filename); - assertNotNull("File " + filename + " was not found. It should contain logic for PolicyModel " - + apexPolicyModel.getKey(), expectedlogic); - assertEquals( - "The task in " + filename + " is not the same as the relevant logic in PolicyModel " - + apexPolicyModel.getKey(), - expectedlogic.replaceAll("\\s", ""), logic.replaceAll("\\s", "")); - } - } - - - /** - * Check logic for MyFirstPolicyAlt#1. - */ - @Test - public void testMfp1AltTaskLogic() { - final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp1AltPolicyModel(); - assertNotNull(apexPolicyModel); - - final Map logics = new LinkedHashMap<>(); - logics.putAll(getTslLogics(apexPolicyModel)); - logics.putAll(getTaskLogics(apexPolicyModel)); - - for (final Entry logicvalue : logics.entrySet()) { - final String filename = "examples/models/MyFirstPolicy/1/" + logicvalue.getKey(); - final String logic = logicvalue.getValue(); - final String expectedlogic = ResourceUtils.getResourceAsString(filename); - assertNotNull("File " + filename + " was not found. It should contain logic for PolicyModel " - + apexPolicyModel.getKey(), expectedlogic); - assertEquals( - "The task in " + filename + " is not the same as the relevant logic in PolicyModel " - + apexPolicyModel.getKey(), - expectedlogic.replaceAll("\\s", ""), logic.replaceAll("\\s", "")); - } - } - - /** - * Check logic for MyFirstPolicy2. - */ - @Test - public void testMfp2TaskLogic() { - final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp2PolicyModel(); - assertNotNull(apexPolicyModel); - - final Map logics = new LinkedHashMap<>(); - logics.putAll(getTslLogics(apexPolicyModel)); - logics.putAll(getTaskLogics(apexPolicyModel)); - - for (final Entry logicvalue : logics.entrySet()) { - final String filename = "examples/models/MyFirstPolicy/2/" + logicvalue.getKey(); - final String logic = logicvalue.getValue(); - final String expectedlogic = ResourceUtils.getResourceAsString(filename); - assertNotNull("File " + filename + " was not found. It should contain logic for PolicyModel " - + apexPolicyModel.getKey(), expectedlogic); - assertEquals( - "The task in " + filename + " is not the same as the relevant logic in PolicyModel " - + apexPolicyModel.getKey(), - expectedlogic.replaceAll("\\s", ""), logic.replaceAll("\\s", "")); - } - } - - /** - * Gets the TSL logics. - * - * @param apexPolicyModel the apex policy model - * @return the TSL logics - */ - private Map getTslLogics(final AxPolicyModel apexPolicyModel) { - final Map ret = new LinkedHashMap<>(); - for (final Entry policyentry : apexPolicyModel.getPolicies().getPolicyMap() - .entrySet()) { - for (final Entry statesentry : policyentry.getValue().getStateMap().entrySet()) { - final AxState state = statesentry.getValue(); - final String tsllogic = state.getTaskSelectionLogic().getLogic(); - final String tsllogicflavour = state.getTaskSelectionLogic().getLogicFlavour(); - if (tsllogic != null && tsllogic.trim().length() > 0) { - assertNotNull( - "Logic Type \"" + tsllogicflavour + "\" in state " + statesentry.getKey() + " in policy " - + policyentry.getKey() + " is not supported in this test", - LOGICEXTENSIONS.get(tsllogicflavour.toUpperCase())); - final String filename = policyentry.getKey().getName() + "_" + statesentry.getKey() + "TSL." - + LOGICEXTENSIONS.get(tsllogicflavour.toUpperCase()); - ret.put(filename, tsllogic); - } - } - } - return ret; - } - - /** - * Gets the task logics. - * - * @param apexPolicyModel the apex policy model - * @return the task logics - */ - private Map getTaskLogics(final AxPolicyModel apexPolicyModel) { - final Map ret = new LinkedHashMap<>(); - for (final Entry taskentry : apexPolicyModel.getTasks().getTaskMap().entrySet()) { - final AxTask task = taskentry.getValue(); - final String tasklogic = task.getTaskLogic().getLogic(); - final String tasklogicflavour = task.getTaskLogic().getLogicFlavour(); - assertTrue("No/Blank logic found in task " + taskentry.getKey(), - (tasklogic != null && tasklogic.trim().length() > 0)); - assertNotNull("Logic Type \"" + tasklogicflavour + "\" in task " + taskentry.getKey() - + " is not supported in this test", LOGICEXTENSIONS.get(tasklogicflavour.toUpperCase())); - final String filename = - taskentry.getKey().getName() + "." + LOGICEXTENSIONS.get(tasklogicflavour.toUpperCase()); - ret.put(filename, tasklogic); - } - return ret; - } -} diff --git a/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpModel.java b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpModel.java deleted file mode 100644 index 808da4866..000000000 --- a/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpModel.java +++ /dev/null @@ -1,106 +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.examples.myfirstpolicy; - -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.sql.Connection; -import java.sql.DriverManager; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.apex.model.basicmodel.test.TestApexModel; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; - -/** - * Test MyFirstPolicy Model. - * - * @author John Keeney (john.keeney@ericsson.com) - */ -public class TestMfpModel { - - private static Connection connection; - private static TestApexModel testApexModel1; - private static TestApexModel testApexModel2; - - /** - * Setup. - * - * @throws Exception if there is an error - */ - @BeforeClass - public static void setup() throws Exception { - Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); - connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true"); - testApexModel1 = new TestApexModel<>(AxPolicyModel.class, new TestMfpModelCreator.TestMfp1ModelCreator()); - testApexModel2 = new TestApexModel<>(AxPolicyModel.class, new TestMfpModelCreator.TestMfp2ModelCreator()); - } - - /** - * Teardown. - * - * @throws Exception if there is an error - */ - @AfterClass - public static void teardown() throws Exception { - connection.close(); - new File("derby.log").delete(); - } - - /** - * Test model is valid. - * - * @throws Exception if there is an error - */ - @Test - public void testModelValid() throws Exception { - AxValidationResult result = testApexModel1.testApexModelValid(); - assertTrue("Model did not validate cleanly", result.isOk()); - - result = testApexModel2.testApexModelValid(); - assertTrue("Model did not validate cleanly", result.isOk()); - } - - /** - * Test model write and read XML. - * - * @throws Exception if there is an error - */ - @Test - public void testModelWriteReadXml() throws Exception { - testApexModel1.testApexModelWriteReadXml(); - testApexModel2.testApexModelWriteReadXml(); - } - - /** - * Test model write and read JSON. - * - * @throws Exception if there is an error - */ - @Test - public void testModelWriteReadJson() throws Exception { - testApexModel1.testApexModelWriteReadJson(); - testApexModel2.testApexModelWriteReadJson(); - } -} diff --git a/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpModelCli.java b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpModelCli.java deleted file mode 100644 index a63df03bc..000000000 --- a/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpModelCli.java +++ /dev/null @@ -1,96 +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.examples.myfirstpolicy; - -import static org.junit.Assert.assertEquals; - -import java.io.File; -import java.io.IOException; - -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain; -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; - -/** - * Test MyFirstPolicyModel CLI. - */ -public class TestMfpModelCli { - private static AxPolicyModel testApexModel1; - private static AxPolicyModel testApexModel2; - - /** - * Setup the test. - * - * @throws Exception if there is an error - */ - @BeforeClass - public static void setup() throws Exception { - testApexModel1 = new TestMfpModelCreator.TestMfp1ModelCreator().getModel(); - testApexModel2 = new TestMfpModelCreator.TestMfp2ModelCreator().getModel(); - } - - /** - * Test CLI policy. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws ApexModelException ifd there is an Apex Error - */ - @Test - public void testCliPolicy() throws IOException, ApexModelException { - - final File tempLogFile1 = File.createTempFile("TestMyFirstPolicy1CLI", ".log"); - final File tempModelFile1 = File.createTempFile("TestMyFirstPolicy1CLI", ".json"); - final File tempLogFile2 = File.createTempFile("TestMyFirstPolicy2CLI", ".log"); - final File tempModelFile2 = File.createTempFile("TestMyFirstPolicy2CLI", ".json"); - final String[] testApexModel1CliArgs = - { "-c", "src/main/resources/examples/models/MyFirstPolicy/1/MyFirstPolicyModel_0.0.1.apex", "-l", - tempLogFile1.getAbsolutePath(), "-o", tempModelFile1.getAbsolutePath() }; - final String[] testApexModel2CliArgs = - { "-c", "src/main/resources/examples/models/MyFirstPolicy/2/MyFirstPolicyModel_0.0.1.apex", "-l", - tempLogFile2.getAbsolutePath(), "-o", tempModelFile2.getAbsolutePath() }; - - new ApexCommandLineEditorMain(testApexModel1CliArgs); - new ApexCommandLineEditorMain(testApexModel2CliArgs); - - final ApexModelReader reader = new ApexModelReader<>(AxPolicyModel.class); - AxPolicyModel generatedmodel = reader.read(TextFileUtils.getTextFileAsString(tempModelFile1.getAbsolutePath())); - - assertEquals("Model generated from the CLI (" + testApexModel1CliArgs[1] + ") into file " - + tempModelFile1.getAbsolutePath() + " is not the same as the test Model for " - + testApexModel1.getKey(), testApexModel1, generatedmodel); - - generatedmodel = reader.read(TextFileUtils.getTextFileAsString(tempModelFile2.getAbsolutePath())); - assertEquals("Model generated from the CLI (" + testApexModel2CliArgs[1] + ") into file " - + tempModelFile2.getAbsolutePath() + " is not the same as the test Model for " - + testApexModel2.getKey(), testApexModel2, generatedmodel); - - tempLogFile1.delete(); - tempModelFile1.delete(); - - tempLogFile2.delete(); - tempModelFile2.delete(); - - } -} diff --git a/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpUseCase.java b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpUseCase.java deleted file mode 100644 index 8c95c9fec..000000000 --- a/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpUseCase.java +++ /dev/null @@ -1,383 +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.examples.myfirstpolicy; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import com.google.gson.GsonBuilder; -import com.google.gson.JsonObject; - -import java.io.IOException; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; -import org.onap.policy.apex.context.parameters.ContextParameterConstants; -import org.onap.policy.apex.context.parameters.ContextParameters; -import org.onap.policy.apex.context.parameters.SchemaParameters; -import org.onap.policy.apex.core.engine.EngineParameters; -import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory; -import org.onap.policy.apex.core.engine.engine.impl.ApexEngineImpl; -import org.onap.policy.apex.core.engine.event.EnEvent; -import org.onap.policy.apex.examples.myfirstpolicy.model.MfpDomainModelFactory; -import org.onap.policy.apex.model.basicmodel.concepts.ApexException; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; -import org.onap.policy.apex.model.eventmodel.concepts.AxField; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; -import org.onap.policy.apex.plugins.executor.javascript.JavascriptExecutorParameters; -import org.onap.policy.apex.plugins.executor.mvel.MvelExecutorParameters; -import org.onap.policy.common.parameters.ParameterService; -import org.onap.policy.common.utils.resources.ResourceUtils; - -/** - * Test MyFirstPolicy Use Case. - */ -public class TestMfpUseCase { - // CHECKSTYLE:OFF: MagicNumber - - private static ApexEngineImpl apexEngine; - - /** - * Test MFP use case setup. - */ - @BeforeClass - public static void testMfpUseCaseSetup() { - final AxArtifactKey key = new AxArtifactKey("MyFirstPolicyApexEngine", "0.0.1"); - apexEngine = (ApexEngineImpl) new ApexEngineFactory().createApexEngine(key); - } - - private static ContextParameters contextParameters; - private static SchemaParameters schemaParameters; - private static EngineParameters engineParameters; - - /** - * Before test. - */ - @BeforeClass - public static void beforeTest() { - schemaParameters = new SchemaParameters(); - - schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); - schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters()); - - ParameterService.register(schemaParameters); - - contextParameters = new ContextParameters(); - - contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME); - contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME); - contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME); - contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME); - - ParameterService.register(contextParameters); - ParameterService.register(contextParameters.getDistributorParameters()); - ParameterService.register(contextParameters.getLockManagerParameters()); - ParameterService.register(contextParameters.getPersistorParameters()); - - engineParameters = new EngineParameters(); - engineParameters.getExecutorParameterMap().put("MVEL", new MvelExecutorParameters()); - engineParameters.getExecutorParameterMap().put("JAVASCRIPT", new JavascriptExecutorParameters()); - ParameterService.register(engineParameters); - } - - /** - * After test. - */ - @AfterClass - public static void afterTest() { - ParameterService.deregister(engineParameters); - - ParameterService.deregister(contextParameters.getDistributorParameters()); - ParameterService.deregister(contextParameters.getLockManagerParameters()); - ParameterService.deregister(contextParameters.getPersistorParameters()); - ParameterService.deregister(contextParameters); - - ParameterService.deregister(schemaParameters); - } - - /** - * Test MyFirstPolicy#1 use case. - * - * @throws ApexException if there is an Apex error - * @throws InterruptedException if there is an Interruption. - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testMfp1Case() throws ApexException, InterruptedException, IOException { - final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp1PolicyModel(); - assertNotNull(apexPolicyModel); - - final TestSaleAuthListener listener = new TestSaleAuthListener("Test"); - apexEngine.addEventListener("listener", listener); - apexEngine.updateModel(apexPolicyModel); - apexEngine.start(); - - final AxEvent axEventin = apexPolicyModel.getEvents().get(new AxArtifactKey("SALE_INPUT:0.0.1")); - assertNotNull(axEventin); - final AxEvent axEventout = apexPolicyModel.getEvents().get(new AxArtifactKey("SALE_AUTH:0.0.1")); - assertNotNull(axEventout); - - EnEvent event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/1/EventIn_BoozeItem_084106GMT.json"); - apexEngine.handleEvent(event); - EnEvent resultout = listener.getResult(); - EnEvent resulexpected = - fillResultEvent(axEventout, "examples/events/MyFirstPolicy/1/EventOut_BoozeItem_084106GMT.json"); - assertEquals(resulexpected, resultout); - - event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/1/EventIn_BoozeItem_201713GMT.json"); - apexEngine.handleEvent(event); - resultout = listener.getResult(); - resulexpected = - fillResultEvent(axEventout, "examples/events/MyFirstPolicy/1/EventOut_BoozeItem_201713GMT.json"); - assertEquals(resulexpected, resultout); - assertEquals("ExecutionIDs are different", event.getExecutionId(), resultout.getExecutionId()); - - event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/1/EventIn_NonBoozeItem_101309GMT.json"); - apexEngine.handleEvent(event); - resultout = listener.getResult(); - resulexpected = - fillResultEvent(axEventout, "examples/events/MyFirstPolicy/1/EventOut_NonBoozeItem_101309GMT.json"); - assertEquals(resulexpected, resultout); - assertEquals("ExecutionIDs are different", event.getExecutionId(), resultout.getExecutionId()); - - apexEngine.stop(); - } - - /** - * Test MyFirstPolicy#2 use case. - * - * @throws ApexException if there is an Apex error - * @throws InterruptedException if there is an Interruption. - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testMfp2Case() throws ApexException, InterruptedException, IOException { - final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp2PolicyModel(); - assertNotNull(apexPolicyModel); - - final TestSaleAuthListener listener = new TestSaleAuthListener("Test"); - apexEngine.addEventListener("listener", listener); - apexEngine.updateModel(apexPolicyModel); - apexEngine.start(); - - final AxEvent axEventin = apexPolicyModel.getEvents().get(new AxArtifactKey("SALE_INPUT:0.0.1")); - assertNotNull(axEventin); - final AxEvent axEventout = apexPolicyModel.getEvents().get(new AxArtifactKey("SALE_AUTH:0.0.1")); - assertNotNull(axEventout); - - EnEvent event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/1/EventIn_BoozeItem_084106GMT.json"); - apexEngine.handleEvent(event); - EnEvent resultout = listener.getResult(); - EnEvent resultexpected = - fillResultEvent(axEventout, "examples/events/MyFirstPolicy/1/EventOut_BoozeItem_084106GMT.json"); - assertEquals(resultexpected, resultout); - assertEquals("ExecutionIDs are different", event.getExecutionId(), resultout.getExecutionId()); - - event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/1/EventIn_BoozeItem_201713GMT.json"); - apexEngine.handleEvent(event); - resultout = listener.getResult(); - resultexpected = - fillResultEvent(axEventout, "examples/events/MyFirstPolicy/1/EventOut_BoozeItem_201713GMT.json"); - assertEquals(resultexpected, resultout); - assertEquals("ExecutionIDs are different", event.getExecutionId(), resultout.getExecutionId()); - - event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/1/EventIn_NonBoozeItem_101309GMT.json"); - apexEngine.handleEvent(event); - resultout = listener.getResult(); - resultexpected = - fillResultEvent(axEventout, "examples/events/MyFirstPolicy/1/EventOut_NonBoozeItem_101309GMT.json"); - assertEquals(resultexpected, resultout); - assertEquals("ExecutionIDs are different", event.getExecutionId(), resultout.getExecutionId()); - - event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/2/EventIn_BoozeItem_101433CET_thurs.json"); - apexEngine.handleEvent(event); - resultout = listener.getResult(); - resultexpected = - fillResultEvent(axEventout, "examples/events/MyFirstPolicy/2/EventOut_BoozeItem_101433CET_thurs.json"); - assertEquals(resultexpected, resultout); - assertEquals("ExecutionIDs are different", event.getExecutionId(), resultout.getExecutionId()); - - event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/2/EventIn_BoozeItem_171937CET_sun.json"); - apexEngine.handleEvent(event); - resultout = listener.getResult(); - resultexpected = - fillResultEvent(axEventout, "examples/events/MyFirstPolicy/2/EventOut_BoozeItem_171937CET_sun.json"); - assertEquals(resultexpected, resultout); - assertEquals("ExecutionIDs are different", event.getExecutionId(), resultout.getExecutionId()); - - event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/2/EventIn_NonBoozeItem_111309CET_mon.json"); - apexEngine.handleEvent(event); - resultout = listener.getResult(); - resultexpected = - fillResultEvent(axEventout, "examples/events/MyFirstPolicy/2/EventOut_NonBoozeItem_111309CET_mon.json"); - assertEquals(resultexpected, resultout); - assertEquals("ExecutionIDs are different", event.getExecutionId(), resultout.getExecutionId()); - - apexEngine.stop(); - } - - /** - * Fill trigger event for test. - * - * @param event the event - * @param inputFile the input file - * @return the filled event - */ - private EnEvent fillTriggerEvent(final AxEvent event, final String inputFile) { - final EnEvent ret = new EnEvent(event.getKey()); - final GsonBuilder gb = new GsonBuilder(); - gb.serializeNulls().enableComplexMapKeySerialization(); - final JsonObject jsonObject = - gb.create().fromJson(ResourceUtils.getResourceAsString(inputFile), JsonObject.class); - assertNotNull(jsonObject); - assertTrue(jsonObject.has("name")); - assertTrue(ret.getName().equals(jsonObject.get("name").getAsString())); - assertTrue(ret.getAxEvent().getKey().getName().equals(jsonObject.get("name").getAsString())); - assertTrue(jsonObject.has("nameSpace")); - assertTrue(ret.getAxEvent().getNameSpace().equals(jsonObject.get("nameSpace").getAsString())); - assertTrue(jsonObject.has("version")); - assertTrue(ret.getAxEvent().getKey().getVersion().equals(jsonObject.get("version").getAsString())); - final List reserved = Arrays.asList("name", "nameSpace", "version", "source", "target"); - for (final Map.Entry e : jsonObject.entrySet()) { - if (reserved.contains(e.getKey())) { - continue; - } - assertTrue("Event file " + inputFile + " has a field " + e.getKey() + " but this is not defined for " - + event.getId(), (event.getParameterMap().containsKey(e.getKey()))); - if (jsonObject.get(e.getKey()).isJsonNull()) { - ret.put(e.getKey(), null); - } - } - for (final AxField field : event.getFields()) { - if (!field.getOptional()) { - assertTrue("Event file " + inputFile + " is missing a mandatory field " + field.getKey().getLocalName() - + " for " + event.getId(), jsonObject.has(field.getKey().getLocalName())); - } else { - ret.put(field.getKey().getLocalName(), null); - } - } - if (jsonObject.has("time") && !jsonObject.get("time").isJsonNull()) { - ret.put("time", jsonObject.get("time").getAsLong()); - } - if (jsonObject.has("sale_ID") && !jsonObject.get("sale_ID").isJsonNull()) { - ret.put("sale_ID", jsonObject.get("sale_ID").getAsLong()); - } - if (jsonObject.has("amount") && !jsonObject.get("amount").isJsonNull()) { - ret.put("amount", jsonObject.get("amount").getAsDouble()); - } - if (jsonObject.has("item_ID") && !jsonObject.get("item_ID").isJsonNull()) { - ret.put("item_ID", jsonObject.get("item_ID").getAsLong()); - } - if (jsonObject.has("quantity") && !jsonObject.get("quantity").isJsonNull()) { - ret.put("quantity", jsonObject.get("quantity").getAsInt()); - } - if (jsonObject.has("assistant_ID") && !jsonObject.get("assistant_ID").isJsonNull()) { - ret.put("assistant_ID", jsonObject.get("assistant_ID").getAsLong()); - } - if (jsonObject.has("branch_ID") && !jsonObject.get("branch_ID").isJsonNull()) { - ret.put("branch_ID", jsonObject.get("branch_ID").getAsLong()); - } - if (jsonObject.has("notes") && !jsonObject.get("notes").isJsonNull()) { - ret.put("notes", jsonObject.get("notes").getAsString()); - } - return ret; - } - - /** - * Fill result event for test. - * - * @param event the event - * @param inputFile the input file - * @return the filled event - */ - private EnEvent fillResultEvent(final AxEvent event, final String inputFile) { - final EnEvent ret = new EnEvent(event.getKey()); - final GsonBuilder gb = new GsonBuilder(); - gb.serializeNulls().enableComplexMapKeySerialization(); - final JsonObject jsonObject = - gb.create().fromJson(ResourceUtils.getResourceAsString(inputFile), JsonObject.class); - assertNotNull(jsonObject); - assertTrue(jsonObject.has("name")); - assertTrue(ret.getName().equals(jsonObject.get("name").getAsString())); - assertTrue(ret.getAxEvent().getKey().getName().equals(jsonObject.get("name").getAsString())); - assertTrue(jsonObject.has("nameSpace")); - assertTrue(ret.getAxEvent().getNameSpace().equals(jsonObject.get("nameSpace").getAsString())); - assertTrue(jsonObject.has("version")); - assertTrue(ret.getAxEvent().getKey().getVersion().equals(jsonObject.get("version").getAsString())); - final List reserved = Arrays.asList("name", "nameSpace", "version", "source", "target"); - for (final Map.Entry e : jsonObject.entrySet()) { - if (reserved.contains(e.getKey())) { - continue; - } - assertTrue("Event file " + inputFile + " has a field " + e.getKey() + " but this is not defined for " - + event.getId(), (event.getParameterMap().containsKey(e.getKey()))); - if (jsonObject.get(e.getKey()).isJsonNull()) { - ret.put(e.getKey(), null); - } - } - for (final AxField field : event.getFields()) { - if (!field.getOptional()) { - assertTrue("Event file " + inputFile + " is missing a mandatory field " + field.getKey().getLocalName() - + " for " + event.getId(), jsonObject.has(field.getKey().getLocalName())); - } else { - ret.put(field.getKey().getLocalName(), null); - } - } - if (jsonObject.has("time") && !jsonObject.get("time").isJsonNull()) { - ret.put("time", jsonObject.get("time").getAsLong()); - } - if (jsonObject.has("sale_ID") && !jsonObject.get("sale_ID").isJsonNull()) { - ret.put("sale_ID", jsonObject.get("sale_ID").getAsLong()); - } - if (jsonObject.has("amount") && !jsonObject.get("amount").isJsonNull()) { - ret.put("amount", jsonObject.get("amount").getAsDouble()); - } - if (jsonObject.has("item_ID") && !jsonObject.get("item_ID").isJsonNull()) { - ret.put("item_ID", jsonObject.get("item_ID").getAsLong()); - } - if (jsonObject.has("quantity") && !jsonObject.get("quantity").isJsonNull()) { - ret.put("quantity", jsonObject.get("quantity").getAsInt()); - } - if (jsonObject.has("assistant_ID") && !jsonObject.get("assistant_ID").isJsonNull()) { - ret.put("assistant_ID", jsonObject.get("assistant_ID").getAsLong()); - } - if (jsonObject.has("branch_ID") && !jsonObject.get("branch_ID").isJsonNull()) { - ret.put("branch_ID", jsonObject.get("branch_ID").getAsLong()); - } - if (jsonObject.has("notes") && !jsonObject.get("notes").isJsonNull()) { - ret.put("notes", jsonObject.get("notes").getAsString()); - } - if (jsonObject.has("authorised") && !jsonObject.get("authorised").isJsonNull()) { - ret.put("authorised", jsonObject.get("authorised").getAsString()); - } - if (jsonObject.has("message") && !jsonObject.get("message").isJsonNull()) { - ret.put("message", jsonObject.get("message").getAsString()); - } - return ret; - } -} -- cgit 1.2.3-korg