2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.core.engine.executor;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.LinkedHashMap;
32 import java.util.List;
34 import java.util.Properties;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.MockitoAnnotations;
41 import org.onap.policy.apex.context.ContextException;
42 import org.onap.policy.apex.core.engine.ExecutorParameters;
43 import org.onap.policy.apex.core.engine.TaskParameters;
44 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
45 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
48 import org.onap.policy.apex.model.eventmodel.concepts.AxInputField;
49 import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField;
50 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
51 import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic;
52 import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter;
57 public class TaskExecutorTest {
59 private AxTask axTaskMock;
62 private ApexInternalContext internalContextMock;
65 private AxInputField axInputFieldMock;
68 private AxInputField axOptionalInputFieldMock;
71 private AxOutputField axOutputFieldMock;
74 private AxOutputField axOptionalOutputFieldMock;
77 private AxOutputField axMissingOutputFieldMock;
80 private Executor<Map<String, Object>, Map<String, Object>, AxTask, ApexInternalContext> nextExecutorMock;
83 private AxTaskLogic taskLogicMock;
85 private LinkedHashMap<String, Object> inFieldMap;
86 private LinkedHashMap<String, Object> outFieldMap;
87 private List<TaskParameters> taskParametersFromConfig;
93 public void startMocking() {
94 MockitoAnnotations.initMocks(this);
96 AxArtifactKey task0Key = new AxArtifactKey("Task0:0.0.1");
97 Mockito.doReturn(task0Key).when(axTaskMock).getKey();
98 Mockito.doReturn(task0Key.getId()).when(axTaskMock).getId();
100 inFieldMap = new LinkedHashMap<>();
101 outFieldMap = new LinkedHashMap<>();
103 inFieldMap.put("InField0", axInputFieldMock);
104 inFieldMap.put("InField1", axOptionalInputFieldMock);
105 outFieldMap.put("OutField0", axOutputFieldMock);
106 outFieldMap.put("OutField0", axOptionalOutputFieldMock);
108 AxArtifactKey schemaKey = new AxArtifactKey("Schema:0.0.1");
109 Mockito.doReturn(schemaKey).when(axInputFieldMock).getSchema();
110 Mockito.doReturn(schemaKey).when(axOptionalInputFieldMock).getSchema();
111 Mockito.doReturn(schemaKey).when(axOutputFieldMock).getSchema();
112 Mockito.doReturn(schemaKey).when(axOptionalOutputFieldMock).getSchema();
113 Mockito.doReturn(schemaKey).when(axMissingOutputFieldMock).getSchema();
115 Mockito.doReturn(true).when(axOptionalInputFieldMock).getOptional();
116 Mockito.doReturn(true).when(axOptionalOutputFieldMock).getOptional();
117 Mockito.doReturn(false).when(axMissingOutputFieldMock).getOptional();
119 Mockito.doReturn(inFieldMap).when(axTaskMock).getInputFields();
120 Mockito.doReturn(outFieldMap).when(axTaskMock).getOutputFields();
121 Mockito.doReturn(taskLogicMock).when(axTaskMock).getTaskLogic();
123 Mockito.doReturn(new AxArtifactKey("Context:0.0.1")).when(internalContextMock).getKey();
125 Map<String, AxTaskParameter> taskParameters = new HashMap<>();
126 taskParameters.put("parameterKey2", new AxTaskParameter(new AxReferenceKey(), "parameterOriginalValue2"));
127 Mockito.doReturn(taskParameters).when(axTaskMock).getTaskParameters();
129 taskParametersFromConfig = new ArrayList<>();
130 taskParametersFromConfig.add(new TaskParameters("parameterKey0", "parameterNewValue0", "Task0:0.0.1"));
131 taskParametersFromConfig.add(new TaskParameters("parameterKey1", "parameterNewValue1", "Task1:0.0.1"));
132 taskParametersFromConfig.add(new TaskParameters("parameterKey2", "parameterNewValue2", null));
136 public void testTaskExecutor() throws StateMachineException, ContextException {
137 final DummyTaskExecutor executor = new DummyTaskExecutor();
138 executor.setContext(null, axTaskMock, internalContextMock);
139 assertEquals("Task0:0.0.1", executor.getKey().getId());
140 assertEquals(null, executor.getExecutionContext());
141 assertEquals(null, executor.getParent());
142 assertEquals(internalContextMock, executor.getContext());
143 assertEquals(null, executor.getNext());
144 assertEquals(null, executor.getIncoming());
145 assertEquals(null, executor.getOutgoing());
146 assertNotNull(executor.getSubject());
148 executor.setParameters(new ExecutorParameters());
149 executor.setNext(nextExecutorMock);
150 assertEquals(nextExecutorMock, executor.getNext());
151 executor.setNext(null);
152 assertEquals(null, executor.getNext());
154 assertThatThrownBy(() -> executor.cleanUp()).hasMessageContaining("cleanUp() not implemented on class");
156 Mockito.doReturn(null).when(taskLogicMock).getLogic();
158 assertThatThrownBy(() -> executor.prepare()).hasMessageContaining("task logic cannot be null.");
160 Mockito.doReturn("some task logic").when(taskLogicMock).getLogic();
164 Map<String, Object> incomingFields = new LinkedHashMap<>();
166 assertThatThrownBy(() -> executor.executePre(0, new Properties(), incomingFields))
167 .hasMessageContaining("task input fields \"[InField0]\" are missing for task \"Task0:0.0.1\"");
169 incomingFields.put("InField0", "A Value");
171 executor.executePre(0, new Properties(), incomingFields);
173 assertThatThrownBy(() -> executor.execute(0, new Properties(), incomingFields))
174 .hasMessageContaining("execute() not implemented on abstract TaskExecutor class, only on its subclasses");
176 assertThatThrownBy(() -> executor.executePost(false)).hasMessageContaining(
177 "execute-post: task logic execution failure on task \"Task0\" in model Context:0.0.1");
179 executor.getExecutionContext().setMessage("Execution message");
181 assertThatThrownBy(() -> executor.executePost(false)).hasMessageContaining(
182 "execute-post: task logic execution failure on task \"Task0\" in model Context:0.0.1, "
183 + "user message: Execution message");
185 executor.executePost(true);
187 outFieldMap.put("MissingField", axMissingOutputFieldMock);
189 assertThatThrownBy(() -> executor.executePost(true))
190 .hasMessageContaining("task output fields \"[MissingField]\" are missing for task \"Task0:0.0.1\"");
192 outFieldMap.remove("MissingField");
193 executor.getExecutionContext().outFields.put("BadExtraField", "Howdy!");
195 assertThatThrownBy(() -> executor.executePost(true))
196 .hasMessageContaining("task output fields \"[BadExtraField]\" are unwanted for task \"Task0:0.0.1\"");
198 executor.getExecutionContext().outFields.remove("BadExtraField");
199 outFieldMap.put("InField1", axMissingOutputFieldMock);
200 executor.executePost(true);
202 outFieldMap.put("InField0", axMissingOutputFieldMock);
203 executor.executePost(true);
205 executor.getExecutionContext().outFields.put("InField0", "Output Value");
206 executor.executePost(true);
208 executor.getExecutionContext().outFields.remove("InField0");
209 executor.executePost(true);
211 assertThatThrownBy(() -> executor.executePre(0, null, incomingFields))
212 .hasMessageMatching("^executionProperties is marked .*on.*ull but is null$");
216 public void testTaskExecutorForTaskParameters() {
217 DummyTaskExecutor executorForParmeterTest = new DummyTaskExecutor(false);
219 executorForParmeterTest.setContext(null, axTaskMock, internalContextMock);
220 executorForParmeterTest.updateTaskParameters(taskParametersFromConfig);
221 assertNotNull(executorForParmeterTest.getSubject().getTaskParameters());
222 // taskId matched, parameter value updated with the new value
223 assertEquals("parameterNewValue0",
224 executorForParmeterTest.getSubject().getTaskParameters().get("parameterKey0").getTaskParameterValue());
225 // taskId mismatch, so the parameter is not updated in the task
226 assertNull(executorForParmeterTest.getSubject().getTaskParameters().get("parameterKey1"));
227 // taskId is not available, so parameter is updated in the task
228 assertEquals("parameterNewValue2",
229 executorForParmeterTest.getSubject().getTaskParameters().get("parameterKey2").getTaskParameterValue());