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 --- .../adaptive/AnomalyDetectionDbWriteTest.java | 63 +++++ .../adaptive/AnomalyDetectionModelTest.java | 85 +++++++ .../adaptive/AnomalyDetectionTslUseCaseTest.java | 233 +++++++++++++++++++ .../examples/adaptive/AutoLearnDbWriteTest.java | 64 ++++++ .../apex/examples/adaptive/AutoLearnModelTest.java | 85 +++++++ .../examples/adaptive/AutoLearnTslUseCaseTest.java | 256 +++++++++++++++++++++ .../adaptive/TestAnomalyDetectionDbWrite.java | 63 ----- .../adaptive/TestAnomalyDetectionModel.java | 85 ------- .../adaptive/TestAnomalyDetectionTslUseCase.java | 233 ------------------- .../examples/adaptive/TestAutoLearnDbWrite.java | 64 ------ .../apex/examples/adaptive/TestAutoLearnModel.java | 85 ------- .../examples/adaptive/TestAutoLearnTslUseCase.java | 256 --------------------- 12 files changed, 786 insertions(+), 786 deletions(-) create mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionDbWriteTest.java create mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionModelTest.java create mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionTslUseCaseTest.java create mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnDbWriteTest.java create mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnModelTest.java create mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnTslUseCaseTest.java delete mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionDbWrite.java delete mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionModel.java delete mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionTslUseCase.java delete mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnDbWrite.java delete mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnModel.java delete mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnTslUseCase.java (limited to 'examples/examples-adaptive') diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionDbWriteTest.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionDbWriteTest.java new file mode 100644 index 000000000..885704497 --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionDbWriteTest.java @@ -0,0 +1,63 @@ +/*- + * ============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.adaptive; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.dao.DaoParameters; +import org.onap.policy.apex.model.basicmodel.test.TestApexModel; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; + +public class AnomalyDetectionDbWriteTest { + private Connection connection; + TestApexModel testApexModel; + + /** + * Sets up embedded Derby database and the Apex anomaly detection model for the tests. + * @throws Exception exception to be thrown while setting up the database connection + */ + @Before + public void setup() throws Exception { + Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); + connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true"); + + testApexModel = new TestApexModel<>(AxPolicyModel.class, new TestAnomalyDetectionModelCreator()); + } + + @After + public void teardown() throws Exception { + connection.close(); + new File("derby.log").delete(); + } + + @Test + public void testModelWriteReadJpa() throws Exception { + final DaoParameters DaoParameters = new DaoParameters(); + DaoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao"); + DaoParameters.setPersistenceUnit("AdaptiveModelsTest"); + testApexModel.testApexModelWriteReadJpa(DaoParameters); + } +} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionModelTest.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionModelTest.java new file mode 100644 index 000000000..552ca2f37 --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionModelTest.java @@ -0,0 +1,85 @@ +/*- + * ============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.adaptive; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.dao.DaoParameters; +import org.onap.policy.apex.model.basicmodel.test.TestApexModel; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; + +public class AnomalyDetectionModelTest { + private Connection connection; + TestApexModel testApexModel; + + /** + * Sets up embedded Derby database and the Apex anomaly detection model for the tests. + * @throws Exception exception to be thrown while setting up the database connection + */ + @Before + public void setup() throws Exception { + Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); + connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true"); + + testApexModel = new TestApexModel<>(AxPolicyModel.class, new TestAnomalyDetectionModelCreator()); + } + + @After + public void teardown() throws Exception { + connection.close(); + new File("derby.log").delete(); + } + + @Test + public void testModelValid() throws Exception { + final AxValidationResult result = testApexModel.testApexModelValid(); + assertTrue(result.toString().equals(VALID_MODEL_STRING)); + } + + @Test + public void testModelWriteReadXml() throws Exception { + testApexModel.testApexModelWriteReadXml(); + } + + @Test + public void testModelWriteReadJson() throws Exception { + testApexModel.testApexModelWriteReadJson(); + } + + @Test + public void testModelWriteReadJpa() throws Exception { + final DaoParameters DaoParameters = new DaoParameters(); + DaoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao"); + DaoParameters.setPersistenceUnit("AdaptiveModelsTest"); + + testApexModel.testApexModelWriteReadJpa(DaoParameters); + } + + private static final String VALID_MODEL_STRING = "***validation of model successful***"; +} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionTslUseCaseTest.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionTslUseCaseTest.java new file mode 100644 index 000000000..40df82cdb --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionTslUseCaseTest.java @@ -0,0 +1,233 @@ +/*- + * ============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.adaptive; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.Random; + +import org.junit.After; +import org.junit.Before; +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.ApexEngine; +import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; +import org.onap.policy.apex.examples.adaptive.model.AdaptiveDomainModelFactory; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.plugins.executor.java.JavaExecutorParameters; +import org.onap.policy.apex.plugins.executor.mvel.MvelExecutorParameters; +import org.onap.policy.common.parameters.ParameterService; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This policy passes, and recieves a Double event context filed called "EVCDouble".
+ * The policy tries to detect anomalies in the pattern of values for EVCDouble
+ * See the 2 test cases below (1 short, 1 long) + * + * @author John Keeney (John.Keeney@ericsson.com) + */ +public class AnomalyDetectionTslUseCaseTest { + private static final XLogger LOGGER = XLoggerFactory.getXLogger(AnomalyDetectionTslUseCaseTest.class); + + private static final int MAXITERATIONS = 3660; + private static final Random RAND = new Random(System.currentTimeMillis()); + + private SchemaParameters schemaParameters; + private ContextParameters contextParameters; + private EngineParameters engineParameters; + + /** + * Before test. + */ + @Before + public 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("JAVA", new JavaExecutorParameters()); + ParameterService.register(engineParameters); + } + + /** + * After test. + */ + @After + public void afterTest() { + ParameterService.deregister(engineParameters); + + ParameterService.deregister(contextParameters.getDistributorParameters()); + ParameterService.deregister(contextParameters.getLockManagerParameters()); + ParameterService.deregister(contextParameters.getPersistorParameters()); + ParameterService.deregister(contextParameters); + + ParameterService.deregister(schemaParameters); + } + + /** + * Test anomaly detection tsl. + * + * @throws ApexException the apex exception + * @throws InterruptedException the interrupted exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + // once through the long running test below + public void testAnomalyDetectionTsl() throws ApexException, InterruptedException, IOException { + final AxPolicyModel apexPolicyModel = new AdaptiveDomainModelFactory().getAnomalyDetectionPolicyModel(); + assertNotNull(apexPolicyModel); + + final AxValidationResult validationResult = new AxValidationResult(); + apexPolicyModel.validate(validationResult); + assertTrue(validationResult.isValid()); + + final AxArtifactKey key = new AxArtifactKey("AnomalyTSLApexEngine", "0.0.1"); + + final ApexEngine apexEngine1 = new ApexEngineFactory().createApexEngine(key); + + final TestApexActionListener listener1 = new TestApexActionListener("TestListener1"); + apexEngine1.addEventListener("listener", listener1); + apexEngine1.updateModel(apexPolicyModel); + apexEngine1.start(); + final EnEvent triggerEvent = + apexEngine1.createEvent(new AxArtifactKey("AnomalyDetectionTriggerEvent", "0.0.1")); + final double rval = RAND.nextGaussian(); + triggerEvent.put("Iteration", 0); + triggerEvent.put("MonitoredValue", rval); + LOGGER.info("Triggering policy in Engine 1 with " + triggerEvent); + apexEngine1.handleEvent(triggerEvent); + final EnEvent result = listener1.getResult(); + LOGGER.info("Receiving action event {} ", result); + assertEquals("ExecutionIDs are different", triggerEvent.getExecutionId(), result.getExecutionId()); + triggerEvent.clear(); + result.clear(); + ThreadUtilities.sleep(1); + apexEngine1.stop(); + } + + /** + * This policy passes, and recieves a Double event context filed called "EVCDouble"
+ * The policy tries to detect anomalies in the pattern of values for EVCDouble
+ * This test case generates a SineWave-like pattern for the parameter, repeating every 360 iterations. (These Period + * should probably be set using TaskParameters!) Every 361st value is a random number!, so should be identified as + * an Anomaly. The policy has 3 Decide Tasks, and the Decide TaskSelectionLogic picks one depending on the + * 'Anomaliness' of the input data.
+ * To plot the results grep debug results for the string "************", paste into excel and delete non-relevant + * columns
+ * + * @throws ApexException the apex exception + * @throws InterruptedException the interrupted exception + * @throws IOException Signals that an I/O exception has occurred. + */ + // Test is disabled by default. uncomment below, or execute using the main() method + // @Test + // EG Dos command: apex-core.engine> mvn + // -Dtest=org.onap.policy.apex.core.engine.ml.TestAnomalyDetectionTslUseCase test | findstr /L /C:"Apex [main] DEBUG + // c.e.a.e.TaskSelectionExecutionLogging - + // TestAnomalyDetectionTSL_Policy0000DecideStateTaskSelectionLogic.getTask():" + public void testAnomalyDetectionTslmain() throws ApexException, InterruptedException, IOException { + + final AxPolicyModel apexPolicyModel = new AdaptiveDomainModelFactory().getAnomalyDetectionPolicyModel(); + assertNotNull(apexPolicyModel); + + final AxValidationResult validationResult = new AxValidationResult(); + apexPolicyModel.validate(validationResult); + assertTrue(validationResult.isValid()); + + final AxArtifactKey key = new AxArtifactKey("AnomalyTSLApexEngine", "0.0.1"); + final EngineParameters parameters = new EngineParameters(); + parameters.getExecutorParameterMap().put("MVEL", new MvelExecutorParameters()); + parameters.getExecutorParameterMap().put("JAVA", new JavaExecutorParameters()); + + final ApexEngine apexEngine1 = new ApexEngineFactory().createApexEngine(key); + + final TestApexActionListener listener1 = new TestApexActionListener("TestListener1"); + apexEngine1.addEventListener("listener1", listener1); + apexEngine1.updateModel(apexPolicyModel); + apexEngine1.start(); + + final EnEvent triggerEvent = + apexEngine1.createEvent(new AxArtifactKey("AnomalyDetectionTriggerEvent", "0.0.1")); + assertNotNull(triggerEvent); + + for (int iteration = 0; iteration < MAXITERATIONS; iteration++) { + // Trigger the policy in engine 1 + + double value = (Math.sin(Math.toRadians(iteration))) + (RAND.nextGaussian() / 25.0); + // lets make every 361st number a random value to perhaps flag as an anomaly + if (((iteration + 45) % 361) == 0) { + value = (RAND.nextGaussian() * 2.0); + } + triggerEvent.put("Iteration", iteration); + triggerEvent.put("MonitoredValue", value); + LOGGER.info("Iteration " + iteration + ":\tTriggering policy in Engine 1 with " + triggerEvent); + apexEngine1.handleEvent(triggerEvent); + final EnEvent result = listener1.getResult(); + LOGGER.info("Iteration " + iteration + ":\tReceiving action event {} ", result); + triggerEvent.clear(); + result.clear(); + } + apexEngine1.stop(); + ThreadUtilities.sleep(1000); + } + + /** + * The main method. + * + * @param args the arguments + * @throws ApexException the apex exception + * @throws InterruptedException the interrupted exception + * @throws IOException Signals that an I/O exception has occurred. + */ + public static void main(final String[] args) throws ApexException, InterruptedException, IOException { + new AnomalyDetectionTslUseCaseTest().testAnomalyDetectionTslmain(); + } +} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnDbWriteTest.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnDbWriteTest.java new file mode 100644 index 000000000..828a56133 --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnDbWriteTest.java @@ -0,0 +1,64 @@ +/*- + * ============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.adaptive; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.dao.DaoParameters; +import org.onap.policy.apex.model.basicmodel.test.TestApexModel; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; + +public class AutoLearnDbWriteTest { + private Connection connection; + TestApexModel testApexModel; + + /** + * Sets up embedded Derby database and the Apex AutoLearn model for the tests. + * @throws Exception exception to be thrown while setting up the database connection + */ + @Before + public void setup() throws Exception { + Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); + connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true"); + + testApexModel = new TestApexModel<>(AxPolicyModel.class, new TestAutoLearnModelCreator()); + } + + @After + public void teardown() throws Exception { + connection.close(); + new File("derby.log").delete(); + } + + @Test + public void testModelWriteReadJpa() throws Exception { + final DaoParameters DaoParameters = new DaoParameters(); + DaoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao"); + DaoParameters.setPersistenceUnit("AdaptiveModelsTest"); + + testApexModel.testApexModelWriteReadJpa(DaoParameters); + } +} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnModelTest.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnModelTest.java new file mode 100644 index 000000000..f4cddc580 --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnModelTest.java @@ -0,0 +1,85 @@ +/*- + * ============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.adaptive; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.dao.DaoParameters; +import org.onap.policy.apex.model.basicmodel.test.TestApexModel; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; + +public class AutoLearnModelTest { + private Connection connection; + TestApexModel testApexModel; + + /** + * Sets up embedded Derby database and the Apex AutoLearn model for the tests. + * @throws Exception exception to be thrown while setting up the database connection + */ + @Before + public void setup() throws Exception { + Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); + connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true"); + + testApexModel = new TestApexModel<>(AxPolicyModel.class, new TestAutoLearnModelCreator()); + } + + @After + public void teardown() throws Exception { + connection.close(); + new File("derby.log").delete(); + } + + @Test + public void testModelValid() throws Exception { + final AxValidationResult result = testApexModel.testApexModelValid(); + assertTrue(result.toString().equals(VALID_MODEL_STRING)); + } + + @Test + public void testModelWriteReadXml() throws Exception { + testApexModel.testApexModelWriteReadXml(); + } + + @Test + public void testModelWriteReadJson() throws Exception { + testApexModel.testApexModelWriteReadJson(); + } + + @Test + public void testModelWriteReadJpa() throws Exception { + final DaoParameters DaoParameters = new DaoParameters(); + DaoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao"); + DaoParameters.setPersistenceUnit("AdaptiveModelsTest"); + + testApexModel.testApexModelWriteReadJpa(DaoParameters); + } + + private static final String VALID_MODEL_STRING = "***validation of model successful***"; +} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnTslUseCaseTest.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnTslUseCaseTest.java new file mode 100644 index 000000000..bf6c08f16 --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnTslUseCaseTest.java @@ -0,0 +1,256 @@ +/*- + * ============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.adaptive; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.Random; + +import org.junit.After; +import org.junit.Before; +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.ApexEngine; +import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; +import org.onap.policy.apex.examples.adaptive.model.AdaptiveDomainModelFactory; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.plugins.executor.java.JavaExecutorParameters; +import org.onap.policy.apex.plugins.executor.mvel.MvelExecutorParameters; +import org.onap.policy.common.parameters.ParameterService; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +// TODO: Auto-generated Javadoc +/** + * Test Auto learning in TSL. + * + * @author John Keeney (John.Keeney@ericsson.com) + */ +public class AutoLearnTslUseCaseTest { + private static final XLogger LOGGER = XLoggerFactory.getXLogger(AutoLearnTslUseCaseTest.class); + + private static final int MAXITERATIONS = 1000; + private static final Random rand = new Random(System.currentTimeMillis()); + + private SchemaParameters schemaParameters; + private ContextParameters contextParameters; + private EngineParameters engineParameters; + + /** + * Before test. + */ + @Before + public 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("JAVA", new JavaExecutorParameters()); + ParameterService.register(engineParameters); + } + + /** + * After test. + */ + @After + public void afterTest() { + ParameterService.deregister(engineParameters); + + ParameterService.deregister(contextParameters.getDistributorParameters()); + ParameterService.deregister(contextParameters.getLockManagerParameters()); + ParameterService.deregister(contextParameters.getPersistorParameters()); + ParameterService.deregister(contextParameters); + + ParameterService.deregister(schemaParameters); + } + + /** + * Test auto learn tsl. + * + * @throws ApexException the apex exception + * @throws InterruptedException the interrupted exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + // once through the long running test below + public void testAutoLearnTsl() throws ApexException, InterruptedException, IOException { + final AxPolicyModel apexPolicyModel = new AdaptiveDomainModelFactory().getAutoLearnPolicyModel(); + assertNotNull(apexPolicyModel); + + final AxValidationResult validationResult = new AxValidationResult(); + apexPolicyModel.validate(validationResult); + assertTrue(validationResult.isValid()); + + final AxArtifactKey key = new AxArtifactKey("AADMApexEngine", "0.0.1"); + + final ApexEngine apexEngine1 = new ApexEngineFactory().createApexEngine(key); + + final TestApexActionListener listener1 = new TestApexActionListener("TestListener1"); + apexEngine1.addEventListener("listener", listener1); + apexEngine1.updateModel(apexPolicyModel); + apexEngine1.start(); + final EnEvent triggerEvent = apexEngine1.createEvent(new AxArtifactKey("AutoLearnTriggerEvent", "0.0.1")); + final double rval = rand.nextGaussian(); + triggerEvent.put("MonitoredValue", rval); + triggerEvent.put("LastMonitoredValue", 0D); + LOGGER.info("Triggering policy in Engine 1 with " + triggerEvent); + apexEngine1.handleEvent(triggerEvent); + final EnEvent result = listener1.getResult(); + LOGGER.info("Receiving action event {} ", result); + assertEquals("ExecutionIDs are different", triggerEvent.getExecutionId(), result.getExecutionId()); + triggerEvent.clear(); + result.clear(); + ThreadUtilities.sleep(10); + apexEngine1.stop(); + } + + /** + * This policy passes, and receives a Double event context filed called "EVCDouble"
+ * The policy tries to keep the value at 50, with a Min -100, Max 100 (These should probably be set using + * TaskParameters!)
+ * The policy has 7 Decide Tasks that manipulate the value of this field in unknown ways.
+ * The Decide TSL learns the effect of each task, and then selects the appropriate task to get the value back to + * 50
+ * After the value settles close to 50 for a while, the test Rests the value to to random number and then + * continues
+ * To plot the results grep stdout debug results for the string "*******", paste into excel and delete non-relevant + * columns
+ * + * @throws ApexException the apex exception + * @throws InterruptedException the interrupted exception + * @throws IOException Signals that an I/O exception has occurred. + */ + // @Test + public void testAutoLearnTslMain() throws ApexException, InterruptedException, IOException { + + final double dwant = 50.0; + final double toleranceTileJump = 3.0; + + final AxPolicyModel apexPolicyModel = new AdaptiveDomainModelFactory().getAutoLearnPolicyModel(); + assertNotNull(apexPolicyModel); + + final AxValidationResult validationResult = new AxValidationResult(); + apexPolicyModel.validate(validationResult); + assertTrue(validationResult.isValid()); + + final AxArtifactKey key = new AxArtifactKey("AADMApexEngine", "0.0.1"); + final EngineParameters parameters = new EngineParameters(); + parameters.getExecutorParameterMap().put("MVEL", new MvelExecutorParameters()); + parameters.getExecutorParameterMap().put("JAVA", new JavaExecutorParameters()); + + final ApexEngine apexEngine1 = new ApexEngineFactory().createApexEngine(key); + + final TestApexActionListener listener1 = new TestApexActionListener("TestListener1"); + apexEngine1.addEventListener("listener1", listener1); + apexEngine1.updateModel(apexPolicyModel); + apexEngine1.start(); + + final EnEvent triggerEvent = apexEngine1.createEvent(new AxArtifactKey("AutoLearnTriggerEvent", "0.0.1")); + assertNotNull(triggerEvent); + final double dmin = -100; + final double dmax = 100; + + double rval = (((rand.nextGaussian() + 1) / 2) * (dmax - dmin)) + dmin; + triggerEvent.put("MonitoredValue", rval); + triggerEvent.put("LastMonitoredValue", 0); + + double avval = 0; + double distance; + double avcount = 0; + + for (int iteration = 0; iteration < MAXITERATIONS; iteration++) { + // Trigger the policy in engine 1 + LOGGER.info("Triggering policy in Engine 1 with " + triggerEvent); + apexEngine1.handleEvent(triggerEvent); + final EnEvent result = listener1.getResult(); + LOGGER.info("Receiving action event {} ", result); + triggerEvent.clear(); + + double val = (Double) result.get("MonitoredValue"); + final double prevval = (Double) result.get("LastMonitoredValue"); + + triggerEvent.put("MonitoredValue", prevval); + triggerEvent.put("LastMonitoredValue", val); + + avcount = Math.min((avcount + 1), 20); // maintain average of only the last 20 values + avval = ((avval * (avcount - 1)) + val) / (avcount); + + distance = Math.abs(dwant - avval); + if (distance < toleranceTileJump) { + rval = (((rand.nextGaussian() + 1) / 2) * (dmax - dmin)) + dmin; + val = rval; + triggerEvent.put("MonitoredValue", val); + LOGGER.info("Iteration " + iteration + ": Average " + avval + " has become closer (" + distance + + ") than " + toleranceTileJump + " to " + dwant + " so reseting val:\t\t\t\t\t\t\t\t" + val); + avval = 0; + avcount = 0; + } + LOGGER.info("Iteration " + iteration + ": \tpreval\t" + prevval + "\tval\t" + val + "\tavval\t" + avval); + + result.clear(); + ThreadUtilities.sleep(10); + } + + apexEngine1.stop(); + ThreadUtilities.sleep(1000); + } + + /** + * The main method. + * + * @param args the arguments + * @throws ApexException the apex exception + * @throws InterruptedException the interrupted exception + * @throws IOException Signals that an I/O exception has occurred. + */ + public static void main(final String[] args) throws ApexException, InterruptedException, IOException { + new AutoLearnTslUseCaseTest().testAutoLearnTslMain(); + } +} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionDbWrite.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionDbWrite.java deleted file mode 100644 index 8f54bf12f..000000000 --- a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionDbWrite.java +++ /dev/null @@ -1,63 +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.adaptive; - -import java.io.File; -import java.sql.Connection; -import java.sql.DriverManager; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.dao.DaoParameters; -import org.onap.policy.apex.model.basicmodel.test.TestApexModel; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; - -public class TestAnomalyDetectionDbWrite { - private Connection connection; - TestApexModel testApexModel; - - /** - * Sets up embedded Derby database and the Apex anomaly detection model for the tests. - * @throws Exception exception to be thrown while setting up the database connection - */ - @Before - public void setup() throws Exception { - Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); - connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true"); - - testApexModel = new TestApexModel<>(AxPolicyModel.class, new TestAnomalyDetectionModelCreator()); - } - - @After - public void teardown() throws Exception { - connection.close(); - new File("derby.log").delete(); - } - - @Test - public void testModelWriteReadJpa() throws Exception { - final DaoParameters DaoParameters = new DaoParameters(); - DaoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao"); - DaoParameters.setPersistenceUnit("AdaptiveModelsTest"); - testApexModel.testApexModelWriteReadJpa(DaoParameters); - } -} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionModel.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionModel.java deleted file mode 100644 index 9e631ef2e..000000000 --- a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionModel.java +++ /dev/null @@ -1,85 +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.adaptive; - -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.sql.Connection; -import java.sql.DriverManager; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.apex.model.basicmodel.dao.DaoParameters; -import org.onap.policy.apex.model.basicmodel.test.TestApexModel; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; - -public class TestAnomalyDetectionModel { - private Connection connection; - TestApexModel testApexModel; - - /** - * Sets up embedded Derby database and the Apex anomaly detection model for the tests. - * @throws Exception exception to be thrown while setting up the database connection - */ - @Before - public void setup() throws Exception { - Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); - connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true"); - - testApexModel = new TestApexModel<>(AxPolicyModel.class, new TestAnomalyDetectionModelCreator()); - } - - @After - public void teardown() throws Exception { - connection.close(); - new File("derby.log").delete(); - } - - @Test - public void testModelValid() throws Exception { - final AxValidationResult result = testApexModel.testApexModelValid(); - assertTrue(result.toString().equals(VALID_MODEL_STRING)); - } - - @Test - public void testModelWriteReadXml() throws Exception { - testApexModel.testApexModelWriteReadXml(); - } - - @Test - public void testModelWriteReadJson() throws Exception { - testApexModel.testApexModelWriteReadJson(); - } - - @Test - public void testModelWriteReadJpa() throws Exception { - final DaoParameters DaoParameters = new DaoParameters(); - DaoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao"); - DaoParameters.setPersistenceUnit("AdaptiveModelsTest"); - - testApexModel.testApexModelWriteReadJpa(DaoParameters); - } - - private static final String VALID_MODEL_STRING = "***validation of model successful***"; -} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionTslUseCase.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionTslUseCase.java deleted file mode 100644 index c29a3cb6e..000000000 --- a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionTslUseCase.java +++ /dev/null @@ -1,233 +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.adaptive; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.util.Random; - -import org.junit.After; -import org.junit.Before; -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.ApexEngine; -import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory; -import org.onap.policy.apex.core.engine.event.EnEvent; -import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; -import org.onap.policy.apex.examples.adaptive.model.AdaptiveDomainModelFactory; -import org.onap.policy.apex.model.basicmodel.concepts.ApexException; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; -import org.onap.policy.apex.plugins.executor.java.JavaExecutorParameters; -import org.onap.policy.apex.plugins.executor.mvel.MvelExecutorParameters; -import org.onap.policy.common.parameters.ParameterService; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; - -/** - * This policy passes, and recieves a Double event context filed called "EVCDouble".
- * The policy tries to detect anomalies in the pattern of values for EVCDouble
- * See the 2 test cases below (1 short, 1 long) - * - * @author John Keeney (John.Keeney@ericsson.com) - */ -public class TestAnomalyDetectionTslUseCase { - private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestAnomalyDetectionTslUseCase.class); - - private static final int MAXITERATIONS = 3660; - private static final Random RAND = new Random(System.currentTimeMillis()); - - private SchemaParameters schemaParameters; - private ContextParameters contextParameters; - private EngineParameters engineParameters; - - /** - * Before test. - */ - @Before - public 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("JAVA", new JavaExecutorParameters()); - ParameterService.register(engineParameters); - } - - /** - * After test. - */ - @After - public void afterTest() { - ParameterService.deregister(engineParameters); - - ParameterService.deregister(contextParameters.getDistributorParameters()); - ParameterService.deregister(contextParameters.getLockManagerParameters()); - ParameterService.deregister(contextParameters.getPersistorParameters()); - ParameterService.deregister(contextParameters); - - ParameterService.deregister(schemaParameters); - } - - /** - * Test anomaly detection tsl. - * - * @throws ApexException the apex exception - * @throws InterruptedException the interrupted exception - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - // once through the long running test below - public void testAnomalyDetectionTsl() throws ApexException, InterruptedException, IOException { - final AxPolicyModel apexPolicyModel = new AdaptiveDomainModelFactory().getAnomalyDetectionPolicyModel(); - assertNotNull(apexPolicyModel); - - final AxValidationResult validationResult = new AxValidationResult(); - apexPolicyModel.validate(validationResult); - assertTrue(validationResult.isValid()); - - final AxArtifactKey key = new AxArtifactKey("AnomalyTSLApexEngine", "0.0.1"); - - final ApexEngine apexEngine1 = new ApexEngineFactory().createApexEngine(key); - - final TestApexActionListener listener1 = new TestApexActionListener("TestListener1"); - apexEngine1.addEventListener("listener", listener1); - apexEngine1.updateModel(apexPolicyModel); - apexEngine1.start(); - final EnEvent triggerEvent = - apexEngine1.createEvent(new AxArtifactKey("AnomalyDetectionTriggerEvent", "0.0.1")); - final double rval = RAND.nextGaussian(); - triggerEvent.put("Iteration", 0); - triggerEvent.put("MonitoredValue", rval); - LOGGER.info("Triggering policy in Engine 1 with " + triggerEvent); - apexEngine1.handleEvent(triggerEvent); - final EnEvent result = listener1.getResult(); - LOGGER.info("Receiving action event {} ", result); - assertEquals("ExecutionIDs are different", triggerEvent.getExecutionId(), result.getExecutionId()); - triggerEvent.clear(); - result.clear(); - ThreadUtilities.sleep(1); - apexEngine1.stop(); - } - - /** - * This policy passes, and recieves a Double event context filed called "EVCDouble"
- * The policy tries to detect anomalies in the pattern of values for EVCDouble
- * This test case generates a SineWave-like pattern for the parameter, repeating every 360 iterations. (These Period - * should probably be set using TaskParameters!) Every 361st value is a random number!, so should be identified as - * an Anomaly. The policy has 3 Decide Tasks, and the Decide TaskSelectionLogic picks one depending on the - * 'Anomaliness' of the input data.
- * To plot the results grep debug results for the string "************", paste into excel and delete non-relevant - * columns
- * - * @throws ApexException the apex exception - * @throws InterruptedException the interrupted exception - * @throws IOException Signals that an I/O exception has occurred. - */ - // Test is disabled by default. uncomment below, or execute using the main() method - // @Test - // EG Dos command: apex-core.engine> mvn - // -Dtest=org.onap.policy.apex.core.engine.ml.TestAnomalyDetectionTslUseCase test | findstr /L /C:"Apex [main] DEBUG - // c.e.a.e.TaskSelectionExecutionLogging - - // TestAnomalyDetectionTSL_Policy0000DecideStateTaskSelectionLogic.getTask():" - public void testAnomalyDetectionTslmain() throws ApexException, InterruptedException, IOException { - - final AxPolicyModel apexPolicyModel = new AdaptiveDomainModelFactory().getAnomalyDetectionPolicyModel(); - assertNotNull(apexPolicyModel); - - final AxValidationResult validationResult = new AxValidationResult(); - apexPolicyModel.validate(validationResult); - assertTrue(validationResult.isValid()); - - final AxArtifactKey key = new AxArtifactKey("AnomalyTSLApexEngine", "0.0.1"); - final EngineParameters parameters = new EngineParameters(); - parameters.getExecutorParameterMap().put("MVEL", new MvelExecutorParameters()); - parameters.getExecutorParameterMap().put("JAVA", new JavaExecutorParameters()); - - final ApexEngine apexEngine1 = new ApexEngineFactory().createApexEngine(key); - - final TestApexActionListener listener1 = new TestApexActionListener("TestListener1"); - apexEngine1.addEventListener("listener1", listener1); - apexEngine1.updateModel(apexPolicyModel); - apexEngine1.start(); - - final EnEvent triggerEvent = - apexEngine1.createEvent(new AxArtifactKey("AnomalyDetectionTriggerEvent", "0.0.1")); - assertNotNull(triggerEvent); - - for (int iteration = 0; iteration < MAXITERATIONS; iteration++) { - // Trigger the policy in engine 1 - - double value = (Math.sin(Math.toRadians(iteration))) + (RAND.nextGaussian() / 25.0); - // lets make every 361st number a random value to perhaps flag as an anomaly - if (((iteration + 45) % 361) == 0) { - value = (RAND.nextGaussian() * 2.0); - } - triggerEvent.put("Iteration", iteration); - triggerEvent.put("MonitoredValue", value); - LOGGER.info("Iteration " + iteration + ":\tTriggering policy in Engine 1 with " + triggerEvent); - apexEngine1.handleEvent(triggerEvent); - final EnEvent result = listener1.getResult(); - LOGGER.info("Iteration " + iteration + ":\tReceiving action event {} ", result); - triggerEvent.clear(); - result.clear(); - } - apexEngine1.stop(); - ThreadUtilities.sleep(1000); - } - - /** - * The main method. - * - * @param args the arguments - * @throws ApexException the apex exception - * @throws InterruptedException the interrupted exception - * @throws IOException Signals that an I/O exception has occurred. - */ - public static void main(final String[] args) throws ApexException, InterruptedException, IOException { - new TestAnomalyDetectionTslUseCase().testAnomalyDetectionTslmain(); - } -} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnDbWrite.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnDbWrite.java deleted file mode 100644 index d4c1ab193..000000000 --- a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnDbWrite.java +++ /dev/null @@ -1,64 +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.adaptive; - -import java.io.File; -import java.sql.Connection; -import java.sql.DriverManager; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.dao.DaoParameters; -import org.onap.policy.apex.model.basicmodel.test.TestApexModel; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; - -public class TestAutoLearnDbWrite { - private Connection connection; - TestApexModel testApexModel; - - /** - * Sets up embedded Derby database and the Apex AutoLearn model for the tests. - * @throws Exception exception to be thrown while setting up the database connection - */ - @Before - public void setup() throws Exception { - Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); - connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true"); - - testApexModel = new TestApexModel<>(AxPolicyModel.class, new TestAutoLearnModelCreator()); - } - - @After - public void teardown() throws Exception { - connection.close(); - new File("derby.log").delete(); - } - - @Test - public void testModelWriteReadJpa() throws Exception { - final DaoParameters DaoParameters = new DaoParameters(); - DaoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao"); - DaoParameters.setPersistenceUnit("AdaptiveModelsTest"); - - testApexModel.testApexModelWriteReadJpa(DaoParameters); - } -} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnModel.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnModel.java deleted file mode 100644 index 9bf7ce57a..000000000 --- a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnModel.java +++ /dev/null @@ -1,85 +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.adaptive; - -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.sql.Connection; -import java.sql.DriverManager; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.apex.model.basicmodel.dao.DaoParameters; -import org.onap.policy.apex.model.basicmodel.test.TestApexModel; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; - -public class TestAutoLearnModel { - private Connection connection; - TestApexModel testApexModel; - - /** - * Sets up embedded Derby database and the Apex AutoLearn model for the tests. - * @throws Exception exception to be thrown while setting up the database connection - */ - @Before - public void setup() throws Exception { - Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); - connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true"); - - testApexModel = new TestApexModel<>(AxPolicyModel.class, new TestAutoLearnModelCreator()); - } - - @After - public void teardown() throws Exception { - connection.close(); - new File("derby.log").delete(); - } - - @Test - public void testModelValid() throws Exception { - final AxValidationResult result = testApexModel.testApexModelValid(); - assertTrue(result.toString().equals(VALID_MODEL_STRING)); - } - - @Test - public void testModelWriteReadXml() throws Exception { - testApexModel.testApexModelWriteReadXml(); - } - - @Test - public void testModelWriteReadJson() throws Exception { - testApexModel.testApexModelWriteReadJson(); - } - - @Test - public void testModelWriteReadJpa() throws Exception { - final DaoParameters DaoParameters = new DaoParameters(); - DaoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao"); - DaoParameters.setPersistenceUnit("AdaptiveModelsTest"); - - testApexModel.testApexModelWriteReadJpa(DaoParameters); - } - - private static final String VALID_MODEL_STRING = "***validation of model successful***"; -} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnTslUseCase.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnTslUseCase.java deleted file mode 100644 index 10d69a457..000000000 --- a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnTslUseCase.java +++ /dev/null @@ -1,256 +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.adaptive; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.util.Random; - -import org.junit.After; -import org.junit.Before; -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.ApexEngine; -import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory; -import org.onap.policy.apex.core.engine.event.EnEvent; -import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; -import org.onap.policy.apex.examples.adaptive.model.AdaptiveDomainModelFactory; -import org.onap.policy.apex.model.basicmodel.concepts.ApexException; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; -import org.onap.policy.apex.plugins.executor.java.JavaExecutorParameters; -import org.onap.policy.apex.plugins.executor.mvel.MvelExecutorParameters; -import org.onap.policy.common.parameters.ParameterService; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; - -// TODO: Auto-generated Javadoc -/** - * Test Auto learning in TSL. - * - * @author John Keeney (John.Keeney@ericsson.com) - */ -public class TestAutoLearnTslUseCase { - private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestAutoLearnTslUseCase.class); - - private static final int MAXITERATIONS = 1000; - private static final Random rand = new Random(System.currentTimeMillis()); - - private SchemaParameters schemaParameters; - private ContextParameters contextParameters; - private EngineParameters engineParameters; - - /** - * Before test. - */ - @Before - public 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("JAVA", new JavaExecutorParameters()); - ParameterService.register(engineParameters); - } - - /** - * After test. - */ - @After - public void afterTest() { - ParameterService.deregister(engineParameters); - - ParameterService.deregister(contextParameters.getDistributorParameters()); - ParameterService.deregister(contextParameters.getLockManagerParameters()); - ParameterService.deregister(contextParameters.getPersistorParameters()); - ParameterService.deregister(contextParameters); - - ParameterService.deregister(schemaParameters); - } - - /** - * Test auto learn tsl. - * - * @throws ApexException the apex exception - * @throws InterruptedException the interrupted exception - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - // once through the long running test below - public void testAutoLearnTsl() throws ApexException, InterruptedException, IOException { - final AxPolicyModel apexPolicyModel = new AdaptiveDomainModelFactory().getAutoLearnPolicyModel(); - assertNotNull(apexPolicyModel); - - final AxValidationResult validationResult = new AxValidationResult(); - apexPolicyModel.validate(validationResult); - assertTrue(validationResult.isValid()); - - final AxArtifactKey key = new AxArtifactKey("AADMApexEngine", "0.0.1"); - - final ApexEngine apexEngine1 = new ApexEngineFactory().createApexEngine(key); - - final TestApexActionListener listener1 = new TestApexActionListener("TestListener1"); - apexEngine1.addEventListener("listener", listener1); - apexEngine1.updateModel(apexPolicyModel); - apexEngine1.start(); - final EnEvent triggerEvent = apexEngine1.createEvent(new AxArtifactKey("AutoLearnTriggerEvent", "0.0.1")); - final double rval = rand.nextGaussian(); - triggerEvent.put("MonitoredValue", rval); - triggerEvent.put("LastMonitoredValue", 0D); - LOGGER.info("Triggering policy in Engine 1 with " + triggerEvent); - apexEngine1.handleEvent(triggerEvent); - final EnEvent result = listener1.getResult(); - LOGGER.info("Receiving action event {} ", result); - assertEquals("ExecutionIDs are different", triggerEvent.getExecutionId(), result.getExecutionId()); - triggerEvent.clear(); - result.clear(); - ThreadUtilities.sleep(10); - apexEngine1.stop(); - } - - /** - * This policy passes, and receives a Double event context filed called "EVCDouble"
- * The policy tries to keep the value at 50, with a Min -100, Max 100 (These should probably be set using - * TaskParameters!)
- * The policy has 7 Decide Tasks that manipulate the value of this field in unknown ways.
- * The Decide TSL learns the effect of each task, and then selects the appropriate task to get the value back to - * 50
- * After the value settles close to 50 for a while, the test Rests the value to to random number and then - * continues
- * To plot the results grep stdout debug results for the string "*******", paste into excel and delete non-relevant - * columns
- * - * @throws ApexException the apex exception - * @throws InterruptedException the interrupted exception - * @throws IOException Signals that an I/O exception has occurred. - */ - // @Test - public void testAutoLearnTslMain() throws ApexException, InterruptedException, IOException { - - final double dwant = 50.0; - final double toleranceTileJump = 3.0; - - final AxPolicyModel apexPolicyModel = new AdaptiveDomainModelFactory().getAutoLearnPolicyModel(); - assertNotNull(apexPolicyModel); - - final AxValidationResult validationResult = new AxValidationResult(); - apexPolicyModel.validate(validationResult); - assertTrue(validationResult.isValid()); - - final AxArtifactKey key = new AxArtifactKey("AADMApexEngine", "0.0.1"); - final EngineParameters parameters = new EngineParameters(); - parameters.getExecutorParameterMap().put("MVEL", new MvelExecutorParameters()); - parameters.getExecutorParameterMap().put("JAVA", new JavaExecutorParameters()); - - final ApexEngine apexEngine1 = new ApexEngineFactory().createApexEngine(key); - - final TestApexActionListener listener1 = new TestApexActionListener("TestListener1"); - apexEngine1.addEventListener("listener1", listener1); - apexEngine1.updateModel(apexPolicyModel); - apexEngine1.start(); - - final EnEvent triggerEvent = apexEngine1.createEvent(new AxArtifactKey("AutoLearnTriggerEvent", "0.0.1")); - assertNotNull(triggerEvent); - final double dmin = -100; - final double dmax = 100; - - double rval = (((rand.nextGaussian() + 1) / 2) * (dmax - dmin)) + dmin; - triggerEvent.put("MonitoredValue", rval); - triggerEvent.put("LastMonitoredValue", 0); - - double avval = 0; - double distance; - double avcount = 0; - - for (int iteration = 0; iteration < MAXITERATIONS; iteration++) { - // Trigger the policy in engine 1 - LOGGER.info("Triggering policy in Engine 1 with " + triggerEvent); - apexEngine1.handleEvent(triggerEvent); - final EnEvent result = listener1.getResult(); - LOGGER.info("Receiving action event {} ", result); - triggerEvent.clear(); - - double val = (Double) result.get("MonitoredValue"); - final double prevval = (Double) result.get("LastMonitoredValue"); - - triggerEvent.put("MonitoredValue", prevval); - triggerEvent.put("LastMonitoredValue", val); - - avcount = Math.min((avcount + 1), 20); // maintain average of only the last 20 values - avval = ((avval * (avcount - 1)) + val) / (avcount); - - distance = Math.abs(dwant - avval); - if (distance < toleranceTileJump) { - rval = (((rand.nextGaussian() + 1) / 2) * (dmax - dmin)) + dmin; - val = rval; - triggerEvent.put("MonitoredValue", val); - LOGGER.info("Iteration " + iteration + ": Average " + avval + " has become closer (" + distance - + ") than " + toleranceTileJump + " to " + dwant + " so reseting val:\t\t\t\t\t\t\t\t" + val); - avval = 0; - avcount = 0; - } - LOGGER.info("Iteration " + iteration + ": \tpreval\t" + prevval + "\tval\t" + val + "\tavval\t" + avval); - - result.clear(); - ThreadUtilities.sleep(10); - } - - apexEngine1.stop(); - ThreadUtilities.sleep(1000); - } - - /** - * The main method. - * - * @param args the arguments - * @throws ApexException the apex exception - * @throws InterruptedException the interrupted exception - * @throws IOException Signals that an I/O exception has occurred. - */ - public static void main(final String[] args) throws ApexException, InterruptedException, IOException { - new TestAutoLearnTslUseCase().testAutoLearnTslMain(); - } -} -- cgit 1.2.3-korg