diff options
author | a.sreekumar <ajith.sreekumar@est.tech> | 2020-02-13 11:17:54 +0000 |
---|---|---|
committer | a.sreekumar <ajith.sreekumar@est.tech> | 2020-02-20 17:03:05 +0000 |
commit | 8f8df7ee1ddb20b03cedb17dccdc79fc291cc678 (patch) | |
tree | 6ed8b1ebb110992c5e7985100478ab5b216d185d /core/core-engine/src/test/java | |
parent | 8d22852d50a9b76dd05f68fef2717bb37f3b5bc6 (diff) |
Passing taskParameters from ApexConfig to policy logic
TaskParameters can be used to pass parameters from ApexConfig to the policy logic. In the config, these are optional.
Usage as below:
{
"engineParameters": {
"taskParameters": [
{
"key": "ParameterKey1",
"value": "ParameterValue1"
},
{
"taskId": "TaskIdVal",
"key": "ParameterKey2",
"value": "ParameterValue2"
}
]
}
}
In the taskLogic, taskParameters can be accessed as below:
eg: executor.parameters.get("ParameterKey1"))
If taskId is provided in ApexConfig for an entry, then that
parameter is updated only for that particular task. Otherwise, the task
parameter is added to all tasks.
Change-Id: I9e1b3d3697428309e7d86db40b63ffe822935b69
Issue-ID: POLICY-2364
Signed-off-by: a.sreekumar <ajith.sreekumar@est.tech>
Diffstat (limited to 'core/core-engine/src/test/java')
4 files changed, 128 insertions, 129 deletions
diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/EngineParametersTest.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/EngineParametersTest.java index 1b3489790..ba936f24a 100644 --- a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/EngineParametersTest.java +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/EngineParametersTest.java @@ -1,19 +1,20 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * 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========================================================= */ @@ -21,11 +22,13 @@ package org.onap.policy.apex.core.engine; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import java.util.ArrayList; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; - import org.junit.Test; import org.onap.policy.apex.context.parameters.ContextParameters; import org.onap.policy.common.parameters.ParameterService; @@ -41,20 +44,47 @@ public class EngineParametersTest { EngineParameters pars = new EngineParameters(); pars.setName("Name"); assertEquals("Name", pars.getName()); - + ContextParameters contextPars = new ContextParameters(); pars.setContextParameters(contextPars); assertEquals(contextPars, pars.getContextParameters()); - + Map<String, ExecutorParameters> executorParameterMap = new LinkedHashMap<>(); executorParameterMap.put("Executor", new ExecutorParameters()); pars.setExecutorParameterMap(executorParameterMap); assertEquals(executorParameterMap, pars.getExecutorParameterMap()); - + + List<TaskParameters> taskParameters = new ArrayList<>(); + taskParameters.add(new TaskParameters("param1key", "param1value", "param1taskId")); + taskParameters.add(new TaskParameters("param1key", "param1value", null)); + pars.setTaskParameters(taskParameters); assertTrue(pars.validate().isValid()); - + ParameterService.register(pars); ParameterService.deregister(pars); } + + @Test + public void test_invalid() { + EngineParameters pars = new EngineParameters(); + pars.setName("Name"); + assertEquals("Name", pars.getName()); + + ContextParameters contextPars = new ContextParameters(); + + pars.setContextParameters(contextPars); + assertEquals(contextPars, pars.getContextParameters()); + + Map<String, ExecutorParameters> executorParameterMap = Map.of("Executor", new ExecutorParameters()); + pars.setExecutorParameterMap(executorParameterMap); + assertEquals(executorParameterMap, pars.getExecutorParameterMap()); + + pars.setTaskParameters(List.of(new TaskParameters(null, "param1value", "param1taskId"))); + assertFalse(pars.validate().isValid()); + pars.setTaskParameters(List.of(new TaskParameters(" ", "param1value", "param1taskId"))); + assertFalse(pars.validate().isValid()); + pars.setTaskParameters(List.of(new TaskParameters("param1key", "", "param1taskId"))); + assertFalse(pars.validate().isValid()); + } } diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/DummyTaskExecutor.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/DummyTaskExecutor.java index 8b9d1bc07..8d1aa5f0d 100644 --- a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/DummyTaskExecutor.java +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/DummyTaskExecutor.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,6 @@ package org.onap.policy.apex.core.engine.executor; import java.util.Map; import java.util.Properties; - import org.onap.policy.apex.context.ContextException; import org.onap.policy.apex.core.engine.event.EnEvent; import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; @@ -71,7 +70,7 @@ public class DummyTaskExecutor extends TaskExecutor { @Override public AxTask getSubject() { if (!override) { - super.getSubject(); + return super.getSubject(); } AxArtifactKey taskKey = new AxArtifactKey("FirstTask:0.0.1"); diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/TaskExecutorTest.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/TaskExecutorTest.java index eb2d11177..81b7d9424 100644 --- a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/TaskExecutorTest.java +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/TaskExecutorTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,27 +21,34 @@ package org.onap.policy.apex.core.engine.executor; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertNull; +import java.util.ArrayList; +import java.util.HashMap; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Properties; - import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.onap.policy.apex.context.ContextException; import org.onap.policy.apex.core.engine.ExecutorParameters; +import org.onap.policy.apex.core.engine.TaskParameters; import org.onap.policy.apex.core.engine.context.ApexInternalContext; import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; import org.onap.policy.apex.model.eventmodel.concepts.AxInputField; import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField; import org.onap.policy.apex.model.policymodel.concepts.AxTask; import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic; +import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter; /** * Test task excutor. @@ -75,6 +83,7 @@ public class TaskExecutorTest { private LinkedHashMap<String, Object> inFieldMap; private LinkedHashMap<String, Object> outFieldMap; + private List<TaskParameters> taskParametersFromConfig; /** * Set up mocking. @@ -111,12 +120,20 @@ public class TaskExecutorTest { Mockito.doReturn(taskLogicMock).when(axTaskMock).getTaskLogic(); Mockito.doReturn(new AxArtifactKey("Context:0.0.1")).when(internalContextMock).getKey(); + + Map<String, AxTaskParameter> taskParameters = new HashMap<>(); + taskParameters.put("parameterKey2", new AxTaskParameter(new AxReferenceKey(), "parameterOriginalValue2")); + Mockito.doReturn(taskParameters).when(axTaskMock).getTaskParameters(); + + taskParametersFromConfig = new ArrayList<>(); + taskParametersFromConfig.add(new TaskParameters("parameterKey0", "parameterNewValue0", "Task0:0.0.1")); + taskParametersFromConfig.add(new TaskParameters("parameterKey1", "parameterNewValue1", "Task1:0.0.1")); + taskParametersFromConfig.add(new TaskParameters("parameterKey2", "parameterNewValue2", null)); } @Test - public void testTaskExecutor() { - DummyTaskExecutor executor = new DummyTaskExecutor(); - + public void testTaskExecutor() throws StateMachineException, ContextException { + final DummyTaskExecutor executor = new DummyTaskExecutor(); executor.setContext(null, axTaskMock, internalContextMock); assertEquals("Task0:0.0.1", executor.getKey().getId()); assertEquals(null, executor.getExecutionContext()); @@ -133,135 +150,81 @@ public class TaskExecutorTest { executor.setNext(null); assertEquals(null, executor.getNext()); - try { - executor.cleanUp(); - fail("test should throw an exception"); - } catch (Exception ex) { - assertEquals("cleanUp() not implemented on class", ex.getMessage()); - } + assertThatThrownBy(() -> executor.cleanUp()).hasMessageContaining("cleanUp() not implemented on class"); Mockito.doReturn(null).when(taskLogicMock).getLogic(); - try { - executor.prepare(); - fail("test should throw an exception"); - } catch (Exception ex) { - assertEquals("task logic cannot be null.", ex.getMessage()); - } + assertThatThrownBy(() -> executor.prepare()).hasMessageContaining("task logic cannot be null."); Mockito.doReturn("some task logic").when(taskLogicMock).getLogic(); - try { - executor.prepare(); - } catch (StateMachineException e) { - fail("test should not throw an exception"); - } + executor.prepare(); Map<String, Object> incomingFields = new LinkedHashMap<>(); - try { - executor.executePre(0, new Properties(), incomingFields); - } catch (Exception ex) { - assertEquals("task input fields \"[InField0]\" are missing for task \"Task0:0.0.1\"", ex.getMessage()); - } + + assertThatThrownBy(() -> executor.executePre(0, new Properties(), incomingFields)) + .hasMessageContaining("task input fields \"[InField0]\" are missing for task \"Task0:0.0.1\""); incomingFields.put("InField0", "A Value"); - try { - executor.executePre(0, new Properties(), incomingFields); - } catch (Exception e) { - fail("test should not throw an exception"); - } - - try { - executor.execute(0, new Properties(), incomingFields); - fail("test should throw an exception"); - } catch (Exception ex) { - assertEquals("execute() not implemented on abstract TaskExecutor class, only on its subclasses", - ex.getMessage()); - } - - try { - executor.execute(0, new Properties(), incomingFields); - fail("test should throw an exception"); - } catch (Exception ex) { - assertEquals("execute() not implemented on abstract TaskExecutor class, only on its subclasses", - ex.getMessage()); - } - - try { - executor.executePost(false); - fail("test should throw an exception"); - } catch (Exception ex) { - assertEquals("execute-post: task logic execution failure on task \"Task0\" in model Context:0.0.1", - ex.getMessage()); - } + + executor.executePre(0, new Properties(), incomingFields); + + assertThatThrownBy(() -> executor.execute(0, new Properties(), incomingFields)) + .hasMessageContaining("execute() not implemented on abstract TaskExecutor class, only on its subclasses"); + + assertThatThrownBy(() -> executor.executePost(false)).hasMessageContaining( + "execute-post: task logic execution failure on task \"Task0\" in model Context:0.0.1"); executor.getExecutionContext().setMessage("Execution message"); - try { - executor.executePost(false); - fail("test should throw an exception"); - } catch (Exception ex) { - assertEquals("execute-post: task logic execution failure on task \"Task0\" in model Context:0.0.1, " - + "user message: Execution message", ex.getMessage()); - } - - try { - executor.executePost(true); - } catch (Exception e) { - fail("test should not throw an exception"); - } + + assertThatThrownBy(() -> executor.executePost(false)).hasMessageContaining( + "execute-post: task logic execution failure on task \"Task0\" in model Context:0.0.1, " + + "user message: Execution message"); + + executor.executePost(true); outFieldMap.put("MissingField", axMissingOutputFieldMock); - try { - executor.executePost(true); - fail("test should throw an exception"); - } catch (Exception ex) { - assertEquals("task output fields \"[MissingField]\" are missing for task \"Task0:0.0.1\"", ex.getMessage()); - } + + assertThatThrownBy(() -> executor.executePost(true)) + .hasMessageContaining("task output fields \"[MissingField]\" are missing for task \"Task0:0.0.1\""); outFieldMap.remove("MissingField"); executor.getExecutionContext().outFields.put("BadExtraField", "Howdy!"); - try { - executor.executePost(true); - fail("test should throw an exception"); - } catch (Exception ex) { - assertEquals("task output fields \"[BadExtraField]\" are unwanted for task \"Task0:0.0.1\"", - ex.getMessage()); - } + + assertThatThrownBy(() -> executor.executePost(true)) + .hasMessageContaining("task output fields \"[BadExtraField]\" are unwanted for task \"Task0:0.0.1\""); executor.getExecutionContext().outFields.remove("BadExtraField"); outFieldMap.put("InField1", axMissingOutputFieldMock); - try { - executor.executePost(true); - } catch (Exception ex) { - fail("test should not throw an exception"); - } + executor.executePost(true); outFieldMap.put("InField0", axMissingOutputFieldMock); - try { - executor.executePost(true); - } catch (Exception ex) { - fail("test should not throw an exception"); - } + executor.executePost(true); executor.getExecutionContext().outFields.put("InField0", "Output Value"); - try { - executor.executePost(true); - } catch (Exception ex) { - fail("test should not throw an exception"); - } + executor.executePost(true); executor.getExecutionContext().outFields.remove("InField0"); - try { - executor.executePost(true); - } catch (Exception ex) { - fail("test should not throw an exception"); - } - - try { - executor.executePre(0, null, incomingFields); - fail("test should throw an exception"); - } catch (Exception ex) { - assertEquals("executionProperties is marked @NonNull but is null", ex.getMessage()); - } + executor.executePost(true); + + assertThatThrownBy(() -> executor.executePre(0, null, incomingFields)) + .hasMessageContaining("executionProperties is marked @NonNull but is null"); + } + + @Test + public void testTaskExecutorForTaskParameters() { + DummyTaskExecutor executorForParmeterTest = new DummyTaskExecutor(false); + + executorForParmeterTest.setContext(null, axTaskMock, internalContextMock); + executorForParmeterTest.updateTaskParameters(taskParametersFromConfig); + assertNotNull(executorForParmeterTest.getSubject().getTaskParameters()); + // taskId matched, parameter value updated with the new value + assertEquals("parameterNewValue0", + executorForParmeterTest.getSubject().getTaskParameters().get("parameterKey0").getTaskParameterValue()); + // taskId mismatch, so the parameter is not updated in the task + assertNull(executorForParmeterTest.getSubject().getTaskParameters().get("parameterKey1")); + // taskId is not available, so parameter is updated in the task + assertEquals("parameterNewValue2", + executorForParmeterTest.getSubject().getTaskParameters().get("parameterKey2").getTaskParameterValue()); } } diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContextTest.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContextTest.java index 29c536ed4..f8bdc4bdd 100644 --- a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContextTest.java +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContextTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,15 +21,15 @@ package org.onap.policy.apex.core.engine.executor.context; +import static org.assertj.core.api.Assertions.assertThatThrownBy; 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.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; - import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -38,7 +39,9 @@ import org.onap.policy.apex.context.ContextAlbum; import org.onap.policy.apex.core.engine.context.ApexInternalContext; import org.onap.policy.apex.core.engine.executor.TaskExecutor; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; import org.onap.policy.apex.model.policymodel.concepts.AxTask; +import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter; /** * Test Task Execution Context. @@ -69,6 +72,11 @@ public class TaskExecutionContextTest { Mockito.doReturn(contextAlbumReferences).when(axTaskMock).getContextAlbumReferences(); + Map<String, AxTaskParameter> taskParameters = new HashMap<>(); + taskParameters.put("parameterKey1", new AxTaskParameter(new AxReferenceKey(), "parameterValue1")); + taskParameters.put("parameterKey2", new AxTaskParameter(new AxReferenceKey(), "parameterValue2")); + Mockito.doReturn(taskParameters).when(axTaskMock).getTaskParameters(); + Map<AxArtifactKey, ContextAlbum> contextAlbumMap = new LinkedHashMap<>(); AxArtifactKey album0Key = new AxArtifactKey("AlbumKey0:0.0.1"); AxArtifactKey album1Key = new AxArtifactKey("AlbumKey1:0.0.1"); @@ -97,12 +105,11 @@ public class TaskExecutionContextTest { ContextAlbum contextAlbum = tec.getContextAlbum("AlbumKey0"); assertEquals("AlbumKey0:0.0.1", contextAlbum.getKey().getId()); - try { - tec.getContextAlbum("AlbumKeyNonExistant"); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("cannot find definition of context album \"AlbumKeyNonExistant\" on task \"null\"", - exc.getMessage()); - } + Map<String, String> parameters = tec.getParameters(); + assertEquals("parameterValue1", parameters.get("parameterKey1")); + assertEquals("parameterValue2", parameters.get("parameterKey2")); + + assertThatThrownBy(() -> tec.getContextAlbum("AlbumKeyNonExistant")) + .hasMessageContaining("cannot find definition of context album \"AlbumKeyNonExistant\" on task \"null\""); } } |