From 825ae627d4378c5cc7ab4b7d5f4b4ffefcb7977e Mon Sep 17 00:00:00 2001 From: Dinh Danh Le Date: Wed, 29 Aug 2018 17:21:52 +0100 Subject: Fix checkstyle warnings in examples Change-Id: Iad533f3987792d8713426234f3277c1ef6b72284 Issue-ID: POLICY-1034 Signed-off-by: Dinh Danh Le --- .../apex/examples/myfirstpolicy/TestMFPLogic.java | 183 ---------- .../apex/examples/myfirstpolicy/TestMFPModel.java | 106 ------ .../examples/myfirstpolicy/TestMFPModelCLI.java | 96 ------ .../myfirstpolicy/TestMFPModelCreator.java | 106 ------ .../examples/myfirstpolicy/TestMFPUseCase.java | 377 --------------------- .../apex/examples/myfirstpolicy/TestMfpLogic.java | 183 ++++++++++ .../apex/examples/myfirstpolicy/TestMfpModel.java | 106 ++++++ .../examples/myfirstpolicy/TestMfpModelCli.java | 96 ++++++ .../myfirstpolicy/TestMfpModelCreator.java | 106 ++++++ .../examples/myfirstpolicy/TestMfpUseCase.java | 377 +++++++++++++++++++++ 10 files changed, 868 insertions(+), 868 deletions(-) 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/TestMFPModelCreator.java delete mode 100644 examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMFPUseCase.java create mode 100644 examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpLogic.java create mode 100644 examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpModel.java create mode 100644 examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpModelCli.java create mode 100644 examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpModelCreator.java create mode 100644 examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpUseCase.java (limited to 'examples/examples-myfirstpolicy/src/test/java/org') 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 697a3d99a..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 64ff5b1ab..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 fe9d3a205..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.ApexCLIEditorMain; -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 ApexCLIEditorMain(testApexModel1CliArgs); - new ApexCLIEditorMain(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/TestMFPModelCreator.java b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMFPModelCreator.java deleted file mode 100644 index 2f51f2851..000000000 --- a/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMFPModelCreator.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 org.onap.policy.apex.examples.myfirstpolicy.model.MFPDomainModelFactory; -import org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; - -/** - * Create the MyFirstPolicyModel - base class. - * - * @author John Keeney (John.Keeney@ericsson.com) - */ -public abstract class TestMFPModelCreator implements TestApexModelCreator { - - /* - * (non-Javadoc) - * - * @see org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator#getMalstructuredModel() - */ - @Override - public AxPolicyModel getMalstructuredModel() { - return null; - } - - /* - * (non-Javadoc) - * - * @see org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator#getObservationModel() - */ - @Override - public AxPolicyModel getObservationModel() { - return null; - } - - /* - * (non-Javadoc) - * - * @see org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator#getWarningModel() - */ - @Override - public AxPolicyModel getWarningModel() { - return getModel(); - } - - /* - * (non-Javadoc) - * - * @see org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator#getInvalidModel() - */ - @Override - public AxPolicyModel getInvalidModel() { - return null; - } - - /** - * Create the MyFirstPolicyModel #1. - */ - public static class TestMFP1ModelCreator extends TestMFPModelCreator { - - /* - * (non-Javadoc) - * - * @see org.onap.policy.apex.model.basicmodel.handling.ApexModelCreator#getModel() - */ - @Override - public AxPolicyModel getModel() { - return new MFPDomainModelFactory().getMFP1PolicyModel(); - } - } - - /** - * Create the MyFirstPolicyModel#2. - */ - public static class TestMFP2ModelCreator extends TestMFPModelCreator { - - /* - * (non-Javadoc) - * - * @see org.onap.policy.apex.model.basicmodel.handling.ApexModelCreator#getModel() - */ - @Override - public AxPolicyModel getModel() { - return new MFPDomainModelFactory().getMFP2PolicyModel(); - } - } - -} 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 1068a5398..000000000 --- a/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMFPUseCase.java +++ /dev/null @@ -1,377 +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; - - @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); - } - - @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 resulexpected = - fillResultEvent(axEventout, "examples/events/MyFirstPolicy/1/EventOut_BoozeItem_084106GMT.json"); - assertEquals(resulexpected, 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(); - 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()); - - event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/2/EventIn_BoozeItem_101433CET_thurs.json"); - apexEngine.handleEvent(event); - resultout = listener.getResult(); - resulexpected = - fillResultEvent(axEventout, "examples/events/MyFirstPolicy/2/EventOut_BoozeItem_101433CET_thurs.json"); - assertEquals(resulexpected, 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(); - resulexpected = - fillResultEvent(axEventout, "examples/events/MyFirstPolicy/2/EventOut_BoozeItem_171937CET_sun.json"); - assertEquals(resulexpected, 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(); - resulexpected = - fillResultEvent(axEventout, "examples/events/MyFirstPolicy/2/EventOut_NonBoozeItem_111309CET_mon.json"); - assertEquals(resulexpected, 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 new file mode 100644 index 000000000..651bd9280 --- /dev/null +++ b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpLogic.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 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 new file mode 100644 index 000000000..808da4866 --- /dev/null +++ b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpModel.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 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 new file mode 100644 index 000000000..0d6b4737c --- /dev/null +++ b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpModelCli.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.ApexCLIEditorMain; +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 ApexCLIEditorMain(testApexModel1CliArgs); + new ApexCLIEditorMain(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/TestMfpModelCreator.java b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpModelCreator.java new file mode 100644 index 000000000..ebbdac7c2 --- /dev/null +++ b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpModelCreator.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 org.onap.policy.apex.examples.myfirstpolicy.model.MFPDomainModelFactory; +import org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; + +/** + * Create the MyFirstPolicyModel - base class. + * + * @author John Keeney (John.Keeney@ericsson.com) + */ +public abstract class TestMfpModelCreator implements TestApexModelCreator { + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator#getMalstructuredModel() + */ + @Override + public AxPolicyModel getMalstructuredModel() { + return null; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator#getObservationModel() + */ + @Override + public AxPolicyModel getObservationModel() { + return null; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator#getWarningModel() + */ + @Override + public AxPolicyModel getWarningModel() { + return getModel(); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator#getInvalidModel() + */ + @Override + public AxPolicyModel getInvalidModel() { + return null; + } + + /** + * Create the MyFirstPolicyModel #1. + */ + public static class TestMfp1ModelCreator extends TestMfpModelCreator { + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.handling.ApexModelCreator#getModel() + */ + @Override + public AxPolicyModel getModel() { + return new MFPDomainModelFactory().getMFP1PolicyModel(); + } + } + + /** + * Create the MyFirstPolicyModel#2. + */ + public static class TestMfp2ModelCreator extends TestMfpModelCreator { + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.handling.ApexModelCreator#getModel() + */ + @Override + public AxPolicyModel getModel() { + return new MFPDomainModelFactory().getMFP2PolicyModel(); + } + } + +} 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 new file mode 100644 index 000000000..0d6328286 --- /dev/null +++ b/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpUseCase.java @@ -0,0 +1,377 @@ +/*- + * ============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; + + @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); + } + + @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 resulexpected = + fillResultEvent(axEventout, "examples/events/MyFirstPolicy/1/EventOut_BoozeItem_084106GMT.json"); + assertEquals(resulexpected, 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(); + 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()); + + event = fillTriggerEvent(axEventin, "examples/events/MyFirstPolicy/2/EventIn_BoozeItem_101433CET_thurs.json"); + apexEngine.handleEvent(event); + resultout = listener.getResult(); + resulexpected = + fillResultEvent(axEventout, "examples/events/MyFirstPolicy/2/EventOut_BoozeItem_101433CET_thurs.json"); + assertEquals(resulexpected, 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(); + resulexpected = + fillResultEvent(axEventout, "examples/events/MyFirstPolicy/2/EventOut_BoozeItem_171937CET_sun.json"); + assertEquals(resulexpected, 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(); + resulexpected = + fillResultEvent(axEventout, "examples/events/MyFirstPolicy/2/EventOut_NonBoozeItem_111309CET_mon.json"); + assertEquals(resulexpected, 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