diff options
author | liamfallon <liam.fallon@est.tech> | 2018-12-03 22:48:10 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2018-12-04 09:59:17 +0000 |
commit | 259039fa91e3038c41f4a322d81e7ea07a78b96e (patch) | |
tree | b4206cbd4d28d32c33cbe7cda4971d01b68c755a /plugins/plugins-executor/plugins-executor-java | |
parent | dc2e6900325c0e69e7efb0346602d29ee634fbea (diff) |
Add unit test for Java executor
Code coverage in the Apex-PDP plugins is very low. JUnit coverage will be
increased for the Plugins over the Dublin design timeline.
Issue-ID: POLICY-1379
Change-Id: Ie70ab5a9be650818cb330ec5be2a89a5db6153c5
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'plugins/plugins-executor/plugins-executor-java')
10 files changed, 603 insertions, 26 deletions
diff --git a/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaStateFinalizerExecutor.java b/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaStateFinalizerExecutor.java index 8577d18fb..ef0a8d178 100644 --- a/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaStateFinalizerExecutor.java +++ b/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaStateFinalizerExecutor.java @@ -98,11 +98,7 @@ public class JavaStateFinalizerExecutor extends StateFinalizerExecutor { executePost(returnValue); // Send back the return event - if (returnValue) { - return getOutgoing(); - } else { - return null; - } + return getOutgoing(); } /** diff --git a/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaTaskExecutor.java b/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaTaskExecutor.java index 829434dc3..e9bf45305 100644 --- a/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaTaskExecutor.java +++ b/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaTaskExecutor.java @@ -97,11 +97,7 @@ public class JavaTaskExecutor extends TaskExecutor { executePost(returnValue); // Send back the return event - if (returnValue) { - return getOutgoing(); - } else { - return null; - } + return getOutgoing(); } /** diff --git a/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaTaskSelectExecutor.java b/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaTaskSelectExecutor.java index e642972af..02794cdf6 100644 --- a/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaTaskSelectExecutor.java +++ b/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaTaskSelectExecutor.java @@ -58,8 +58,10 @@ public class JavaTaskSelectExecutor extends TaskSelectExecutor { // Create the task logic object from the byte code of the class taskSelectionLogicObject = Class.forName(getSubject().getTaskSelectionLogic().getLogic()).newInstance(); } catch (final Exception e) { - LOGGER.error("instantiation error on Java class" + taskSelectionLogicObject, e); - throw new StateMachineException("instantiation error on Java class" + taskSelectionLogicObject, e); + LOGGER.error("instantiation error on Java class \"" + getSubject().getTaskSelectionLogic().getLogic() + + "\"", e); + throw new StateMachineException("instantiation error on Java class \"" + + getSubject().getTaskSelectionLogic().getLogic() + "\"", e); } } @@ -74,7 +76,7 @@ public class JavaTaskSelectExecutor extends TaskSelectExecutor { */ @Override public AxArtifactKey execute(final long executionId, final EnEvent incomingEvent) - throws StateMachineException, ContextException { + throws StateMachineException, ContextException { // Do execution pre work executePre(executionId, incomingEvent); @@ -84,26 +86,22 @@ public class JavaTaskSelectExecutor extends TaskSelectExecutor { // Find and call the method with the signature "public boolean getTask(final TaskSelectionExecutionContext // executor)" to invoke the task selection // logic in the Java class - final Method method = taskSelectionLogicObject.getClass().getDeclaredMethod("getTask", (Class[]) - new Class[] { TaskSelectionExecutionContext.class }); + final Method method = taskSelectionLogicObject.getClass().getDeclaredMethod("getTask", + (Class[]) new Class[] { TaskSelectionExecutionContext.class }); returnValue = (boolean) method.invoke(taskSelectionLogicObject, getExecutionContext()); } catch (final Exception e) { - LOGGER.error( - "execute: task selection logic failed to run for state \"" + getSubject().getKey().getId() + "\"", - e); + LOGGER.error("execute: task selection logic failed to run for state \"" + getSubject().getKey().getId() + + "\"", e); throw new StateMachineException( - "task selection logic failed to run for state \"" + getSubject().getKey().getId() + "\"", e); + "task selection logic failed to run for state \"" + getSubject().getKey().getId() + "\"", + e); } // Do the execution post work executePost(returnValue); // Send back the return event - if (returnValue) { - return getOutgoing(); - } else { - return null; - } + return getOutgoing(); } /** @@ -114,7 +112,7 @@ public class JavaTaskSelectExecutor extends TaskSelectExecutor { @Override public void cleanUp() throws StateMachineException { LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," - + getSubject().getTaskSelectionLogic().getLogicFlavour() + "," - + getSubject().getTaskSelectionLogic().getLogic()); + + getSubject().getTaskSelectionLogic().getLogicFlavour() + "," + + getSubject().getTaskSelectionLogic().getLogic()); } } diff --git a/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/DummyJavaStateFinalizerLogic.java b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/DummyJavaStateFinalizerLogic.java new file mode 100644 index 000000000..688f4f883 --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/DummyJavaStateFinalizerLogic.java @@ -0,0 +1,44 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.plugins.executor.java; + +import org.onap.policy.apex.core.engine.executor.context.StateFinalizerExecutionContext; + +/** + * This is a dummy task selection logic class. + * + */ +public class DummyJavaStateFinalizerLogic { + /** + * Gets the state output to use. + * + * @param context the execution context + * @return true if the state finalizer is selected + */ + public boolean getStateOutput(final StateFinalizerExecutionContext context) { + if (context.executionId == -1) { + return false; + } + + context.setSelectedStateOutputName("SelectedOutputIsMe"); + return true; + } +} diff --git a/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/DummyJavaTaskLogic.java b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/DummyJavaTaskLogic.java new file mode 100644 index 000000000..7e416833f --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/DummyJavaTaskLogic.java @@ -0,0 +1,44 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.plugins.executor.java; + +import org.onap.policy.apex.core.engine.executor.context.TaskExecutionContext; + +/** + * This is a dummy task logic class. + * + */ +public class DummyJavaTaskLogic { + /** + * Sets up the return event in the execution context. + * + * @param context the execution context + * @return the true if the return event was set in the context, false on errors. + */ + public boolean getEvent(final TaskExecutionContext context) { + if (context.executionId == -1) { + return false; + + } + + return true; + } +} diff --git a/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/DummyJavaTaskSelectionLogic.java b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/DummyJavaTaskSelectionLogic.java new file mode 100644 index 000000000..f54ed0bea --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/DummyJavaTaskSelectionLogic.java @@ -0,0 +1,43 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.plugins.executor.java; + +import org.onap.policy.apex.core.engine.executor.context.TaskSelectionExecutionContext; + +/** + * This is a dummy task selection logic class. + * + */ +public class DummyJavaTaskSelectionLogic { + /** + * Gets the task to use for the state. + * + * @param context the execution context + * @return true if the task is selected, false otherwise + */ + public boolean getTask(final TaskSelectionExecutionContext context) { + if (context.executionId == -1) { + return false; + } + + return true; + } +} diff --git a/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaExecutorParametersTest.java b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaExecutorParametersTest.java new file mode 100644 index 000000000..847146b8b --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaExecutorParametersTest.java @@ -0,0 +1,36 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.plugins.executor.java; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; + +/** + * Test the JavaExecutorParameters class + */ +public class JavaExecutorParametersTest { + + @Test + public void testJavaExecutorParameters() { + assertNotNull(new JavaExecutorParameters()); + } +} diff --git a/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaStateFinalizerExecutorTest.java b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaStateFinalizerExecutorTest.java new file mode 100644 index 000000000..c8522547a --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaStateFinalizerExecutorTest.java @@ -0,0 +1,153 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.plugins.executor.java; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.DistributorParameters; +import org.onap.policy.apex.context.parameters.LockManagerParameters; +import org.onap.policy.apex.context.parameters.PersistorParameters; +import org.onap.policy.apex.core.engine.EngineParameterConstants; +import org.onap.policy.apex.core.engine.EngineParameters; +import org.onap.policy.apex.core.engine.context.ApexInternalContext; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.core.engine.executor.StateExecutor; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; +import org.onap.policy.apex.core.engine.executor.impl.ExecutorFactoryImpl; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; +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.AxStateFinalizerLogic; +import org.onap.policy.common.parameters.ParameterService; + +/** + * Test the JavaStateFinalizerExecutor class. + * + */ +public class JavaStateFinalizerExecutorTest { + @Before + public void initiateParameters() { + ParameterService.register(new DistributorParameters()); + ParameterService.register(new LockManagerParameters()); + ParameterService.register(new PersistorParameters()); + ParameterService.register(new EngineParameters()); + } + + @After + public void clearParameters() { + ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME); + ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME); + ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME); + ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME); + } + + @Test + public void testJavaStateFinalizerExecutor() { + JavaStateFinalizerExecutor jsfe = new JavaStateFinalizerExecutor(); + assertNotNull(jsfe); + + try { + jsfe.prepare(); + fail("test should throw an exception here"); + } catch (Exception jtseException) { + assertEquals(java.lang.NullPointerException.class, jtseException.getClass()); + } + + ApexInternalContext internalContext = null; + try { + internalContext = new ApexInternalContext(new AxPolicyModel()); + } catch (ContextException e) { + fail("test should not throw an exception here"); + } + + StateExecutor parentStateExcutor = null; + try { + parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl()); + } catch (StateMachineException e) { + } + AxState state = new AxState(); + parentStateExcutor.setContext(null, state , internalContext); + AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic(); + jsfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext); + + try { + jsfe.prepare(); + fail("test should throw an exception here"); + } catch (Exception jtseException) { + assertEquals("instantiation error on Java class \"\"", jtseException.getMessage()); + } + + stateFinalizerLogic.setLogic("java.lang.String"); + + try { + jsfe.prepare(); + } catch (Exception jtseException) { + fail("test should not throw an exception here"); + } + + try { + jsfe.execute(-1, null); + fail("test should throw an exception here"); + } catch (Exception jtseException) { + assertEquals("state finalizer logic failed to run for state finalizer \"NULL:0.0.0:NULL:NULL\"", + jtseException.getMessage()); + } + + AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1")); + EnEvent event = new EnEvent(axEvent); + try { + jsfe.execute(-1, event); + fail("test should throw an exception here"); + } catch (Exception jtseException) { + assertEquals("state finalizer logic failed to run for state finalizer \"NULL:0.0.0:NULL:NULL\"", + jtseException.getMessage()); + } + + stateFinalizerLogic.setLogic("org.onap.policy.apex.plugins.executor.java.DummyJavaStateFinalizerLogic"); + try { + jsfe.prepare(); + jsfe.execute(-1, event); + fail("test should throw an exception here"); + } catch (Exception jtseException) { + assertEquals("execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:NULL:NULL\" " + + "on finalizer logic NULL:0.0.0:NULL:NULL", jtseException.getMessage()); + } + + state.getStateOutputs().put("SelectedOutputIsMe", null); + try { + jsfe.prepare(); + String stateOutput = jsfe.execute(0, event); + assertEquals("SelectedOutputIsMe", stateOutput); + jsfe.cleanUp(); + } catch (Exception jtseException) { + jtseException.printStackTrace(); + fail("test should not throw an exception here"); + } + } +} diff --git a/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaTaskExecutorTest.java b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaTaskExecutorTest.java new file mode 100644 index 000000000..74d0c8260 --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaTaskExecutorTest.java @@ -0,0 +1,132 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.plugins.executor.java; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.DistributorParameters; +import org.onap.policy.apex.context.parameters.LockManagerParameters; +import org.onap.policy.apex.context.parameters.PersistorParameters; +import org.onap.policy.apex.core.engine.context.ApexInternalContext; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.model.policymodel.concepts.AxTask; +import org.onap.policy.common.parameters.ParameterService; + +/** + * Test the JavaTaskExecutor class. + * + */ +public class JavaTaskExecutorTest { + @Before + public void initiateParameters() { + ParameterService.register(new DistributorParameters()); + ParameterService.register(new LockManagerParameters()); + ParameterService.register(new PersistorParameters()); + } + + @After + public void clearParameters() { + ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME); + ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME); + ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME); + } + + @Test + public void testJavaTaskExecutor() { + JavaTaskExecutor jte = new JavaTaskExecutor(); + assertNotNull(jte); + + try { + jte.prepare(); + fail("test should throw an exception here"); + } catch (Exception jteException) { + assertEquals(java.lang.NullPointerException.class, jteException.getClass()); + } + + AxTask task = new AxTask(); + ApexInternalContext internalContext = null; + try { + internalContext = new ApexInternalContext(new AxPolicyModel()); + } catch (ContextException e) { + fail("test should not throw an exception here"); + } + jte.setContext(null, task, internalContext); + + try { + jte.prepare(); + fail("test should throw an exception here"); + } catch (Exception jteException) { + assertEquals("instantiation error on Java class \"\"", jteException.getMessage()); + } + + task.getTaskLogic().setLogic("java.lang.String"); + + try { + jte.prepare(); + } catch (Exception jteException) { + fail("test should not throw an exception here"); + } + + try { + jte.execute(-1, null); + fail("test should throw an exception here"); + } catch (Exception jteException) { + assertEquals(java.lang.NullPointerException.class, jteException.getClass()); + } + + Map<String, Object> incomingParameters = new HashMap<>(); + try { + jte.execute(-1, incomingParameters); + fail("test should throw an exception here"); + } catch (Exception jteException) { + assertEquals("task logic failed to run for task \"NULL:0.0.0\"", jteException.getMessage()); + } + + task.getTaskLogic().setLogic("org.onap.policy.apex.plugins.executor.java.DummyJavaTaskLogic"); + try { + jte.prepare(); + jte.execute(-1, incomingParameters); + fail("test should throw an exception here"); + } catch (Exception jteException) { + assertEquals("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0", + jteException.getMessage()); + } + + try { + jte.prepare(); + Map<String, Object> returnMap = jte.execute(0, incomingParameters); + assertEquals(0, returnMap.size()); + jte.cleanUp(); + } catch (Exception jteException) { + fail("test should not throw an exception here"); + } + } +} diff --git a/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaTaskSelectExecutorTest.java b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaTaskSelectExecutorTest.java new file mode 100644 index 000000000..dbb250f8a --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaTaskSelectExecutorTest.java @@ -0,0 +1,135 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.plugins.executor.java; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.DistributorParameters; +import org.onap.policy.apex.context.parameters.LockManagerParameters; +import org.onap.policy.apex.context.parameters.PersistorParameters; +import org.onap.policy.apex.core.engine.context.ApexInternalContext; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.model.policymodel.concepts.AxState; +import org.onap.policy.common.parameters.ParameterService; + +/** + * Test the JavaTaskSelectExecutor class. + * + */ +public class JavaTaskSelectExecutorTest { + @Before + public void initiateParameters() { + ParameterService.register(new DistributorParameters()); + ParameterService.register(new LockManagerParameters()); + ParameterService.register(new PersistorParameters()); + } + + @After + public void clearParameters() { + ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME); + ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME); + ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME); + } + + @Test + public void testJavaTaskSelectExecutor() { + JavaTaskSelectExecutor jtse = new JavaTaskSelectExecutor(); + assertNotNull(jtse); + + try { + jtse.prepare(); + fail("test should throw an exception here"); + } catch (Exception jtseException) { + assertEquals(java.lang.NullPointerException.class, jtseException.getClass()); + } + + AxState state = new AxState(); + ApexInternalContext internalContext = null; + try { + internalContext = new ApexInternalContext(new AxPolicyModel()); + } catch (ContextException e) { + fail("test should not throw an exception here"); + } + jtse.setContext(null, state, internalContext); + + try { + jtse.prepare(); + fail("test should throw an exception here"); + } catch (Exception jtseException) { + assertEquals("instantiation error on Java class \"\"", jtseException.getMessage()); + } + + state.getTaskSelectionLogic().setLogic("java.lang.String"); + + try { + jtse.prepare(); + } catch (Exception jtseException) { + fail("test should not throw an exception here"); + } + + try { + jtse.execute(-1, null); + fail("test should throw an exception here"); + } catch (Exception jtseException) { + assertEquals(java.lang.NullPointerException.class, jtseException.getClass()); + } + + AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1")); + EnEvent event = new EnEvent(axEvent); + try { + jtse.execute(-1, event); + fail("test should throw an exception here"); + } catch (Exception jtseException) { + assertEquals("task selection logic failed to run for state \"NULL:0.0.0:NULL:NULL\"", + jtseException.getMessage()); + } + + state.getTaskSelectionLogic() + .setLogic("org.onap.policy.apex.plugins.executor.java.DummyJavaTaskSelectionLogic"); + try { + jtse.prepare(); + jtse.execute(-1, event); + fail("test should throw an exception here"); + } catch (Exception jtseException) { + assertEquals("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\"", + jtseException.getMessage()); + } + + try { + jtse.prepare(); + AxArtifactKey taskKey = jtse.execute(0, event); + assertEquals("NULL:0.0.0", taskKey.getId()); + jtse.cleanUp(); + } catch (Exception jtseException) { + fail("test should not throw an exception here"); + } + } +} |