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;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.mockito.MockitoAnnotations;
40 import org.onap.policy.apex.context.ContextException;
41 import org.onap.policy.apex.core.engine.ExecutorParameters;
42 import org.onap.policy.apex.core.engine.TaskParameters;
43 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
44 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
47 import org.onap.policy.apex.model.eventmodel.concepts.AxInputField;
48 import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField;
49 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
50 import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic;
51 import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter;
56 public class TaskExecutorTest {
58 private AxTask axTaskMock;
61 private ApexInternalContext internalContextMock;
64 private AxInputField axInputFieldMock;
67 private AxInputField axOptionalInputFieldMock;
70 private AxOutputField axOutputFieldMock;
73 private AxOutputField axOptionalOutputFieldMock;
76 private AxOutputField axMissingOutputFieldMock;
79 private Executor<Map<String, Object>, Map<String, Object>, AxTask, ApexInternalContext> nextExecutorMock;
82 private AxTaskLogic taskLogicMock;
84 private LinkedHashMap<String, Object> inFieldMap;
85 private LinkedHashMap<String, Object> outFieldMap;
86 private List<TaskParameters> taskParametersFromConfig;
92 public void startMocking() {
93 MockitoAnnotations.initMocks(this);
95 AxArtifactKey task0Key = new AxArtifactKey("Task0:0.0.1");
96 Mockito.doReturn(task0Key).when(axTaskMock).getKey();
97 Mockito.doReturn(task0Key.getId()).when(axTaskMock).getId();
99 inFieldMap = new LinkedHashMap<>();
100 outFieldMap = new LinkedHashMap<>();
102 inFieldMap.put("InField0", axInputFieldMock);
103 inFieldMap.put("InField1", axOptionalInputFieldMock);
104 outFieldMap.put("OutField0", axOutputFieldMock);
105 outFieldMap.put("OutField0", axOptionalOutputFieldMock);
107 AxArtifactKey schemaKey = new AxArtifactKey("Schema:0.0.1");
108 Mockito.doReturn(schemaKey).when(axInputFieldMock).getSchema();
109 Mockito.doReturn(schemaKey).when(axOptionalInputFieldMock).getSchema();
110 Mockito.doReturn(schemaKey).when(axOutputFieldMock).getSchema();
111 Mockito.doReturn(schemaKey).when(axOptionalOutputFieldMock).getSchema();
112 Mockito.doReturn(schemaKey).when(axMissingOutputFieldMock).getSchema();
114 Mockito.doReturn(true).when(axOptionalInputFieldMock).getOptional();
115 Mockito.doReturn(true).when(axOptionalOutputFieldMock).getOptional();
116 Mockito.doReturn(false).when(axMissingOutputFieldMock).getOptional();
118 Mockito.doReturn(inFieldMap).when(axTaskMock).getInputFields();
119 Mockito.doReturn(outFieldMap).when(axTaskMock).getOutputFields();
120 Mockito.doReturn(taskLogicMock).when(axTaskMock).getTaskLogic();
122 Mockito.doReturn(new AxArtifactKey("Context:0.0.1")).when(internalContextMock).getKey();
124 Map<String, AxTaskParameter> taskParameters = new HashMap<>();
125 taskParameters.put("parameterKey2", new AxTaskParameter(new AxReferenceKey(), "parameterOriginalValue2"));
126 Mockito.doReturn(taskParameters).when(axTaskMock).getTaskParameters();
128 taskParametersFromConfig = new ArrayList<>();
129 taskParametersFromConfig.add(new TaskParameters("parameterKey0", "parameterNewValue0", "Task0:0.0.1"));
130 taskParametersFromConfig.add(new TaskParameters("parameterKey1", "parameterNewValue1", "Task1:0.0.1"));
131 taskParametersFromConfig.add(new TaskParameters("parameterKey2", "parameterNewValue2", null));
135 public void testTaskExecutor() throws StateMachineException, ContextException {
136 final DummyTaskExecutor executor = new DummyTaskExecutor();
137 executor.setContext(null, axTaskMock, internalContextMock);
138 assertEquals("Task0:0.0.1", executor.getKey().getId());
139 assertEquals(null, executor.getExecutionContext());
140 assertEquals(null, executor.getParent());
141 assertEquals(internalContextMock, executor.getContext());
142 assertEquals(null, executor.getNext());
143 assertEquals(null, executor.getIncoming());
144 assertEquals(null, executor.getOutgoing());
145 assertNotNull(executor.getSubject());
147 executor.setParameters(new ExecutorParameters());
148 executor.setNext(nextExecutorMock);
149 assertEquals(nextExecutorMock, executor.getNext());
150 executor.setNext(null);
151 assertEquals(null, executor.getNext());
153 assertThatThrownBy(() -> executor.cleanUp()).hasMessageContaining("cleanUp() not implemented on class");
155 Mockito.doReturn(null).when(taskLogicMock).getLogic();
157 assertThatThrownBy(() -> executor.prepare()).hasMessageContaining("task logic cannot be null.");
159 Mockito.doReturn("some task logic").when(taskLogicMock).getLogic();
163 Map<String, Object> incomingFields = new LinkedHashMap<>();
165 assertThatThrownBy(() -> executor.executePre(0, new Properties(), incomingFields))
166 .hasMessageContaining("task input fields \"[InField0]\" are missing for task \"Task0:0.0.1\"");
168 incomingFields.put("InField0", "A Value");
170 executor.executePre(0, new Properties(), incomingFields);
172 assertThatThrownBy(() -> executor.execute(0, new Properties(), incomingFields))
173 .hasMessageContaining("execute() not implemented on abstract TaskExecutor class, only on its subclasses");
175 assertThatThrownBy(() -> executor.executePost(false)).hasMessageContaining(
176 "execute-post: task logic execution failure on task \"Task0\" in model Context:0.0.1");
178 executor.getExecutionContext().setMessage("Execution message");
180 assertThatThrownBy(() -> executor.executePost(false)).hasMessageContaining(
181 "execute-post: task logic execution failure on task \"Task0\" in model Context:0.0.1, "
182 + "user message: Execution message");
184 executor.executePost(true);
186 outFieldMap.put("MissingField", axMissingOutputFieldMock);
188 assertThatThrownBy(() -> executor.executePost(true))
189 .hasMessageContaining("task output fields \"[MissingField]\" are missing for task \"Task0:0.0.1\"");
191 outFieldMap.remove("MissingField");
192 executor.getExecutionContext().outFields.put("BadExtraField", "Howdy!");
194 assertThatThrownBy(() -> executor.executePost(true))
195 .hasMessageContaining("task output fields \"[BadExtraField]\" are unwanted for task \"Task0:0.0.1\"");
197 executor.getExecutionContext().outFields.remove("BadExtraField");
198 outFieldMap.put("InField1", axMissingOutputFieldMock);
199 executor.executePost(true);
201 outFieldMap.put("InField0", axMissingOutputFieldMock);
202 executor.executePost(true);
204 executor.getExecutionContext().outFields.put("InField0", "Output Value");
205 executor.executePost(true);
207 executor.getExecutionContext().outFields.remove("InField0");
208 executor.executePost(true);
210 assertThatThrownBy(() -> executor.executePre(0, null, incomingFields))
211 .hasMessageMatching("^executionProperties is marked .*on.*ull but is null$");
215 public void testTaskExecutorForTaskParameters() {
216 DummyTaskExecutor executorForParmeterTest = new DummyTaskExecutor(false);
218 executorForParmeterTest.setContext(null, axTaskMock, internalContextMock);
219 executorForParmeterTest.updateTaskParameters(taskParametersFromConfig);
220 assertNotNull(executorForParmeterTest.getSubject().getTaskParameters());
221 // taskId matched, parameter value updated with the new value
222 assertEquals("parameterNewValue0",
223 executorForParmeterTest.getSubject().getTaskParameters().get("parameterKey0").getTaskParameterValue());
224 // taskId mismatch, so the parameter is not updated in the task
225 assertNull(executorForParmeterTest.getSubject().getTaskParameters().get("parameterKey1"));
226 // taskId is not available, so parameter is updated in the task
227 assertEquals("parameterNewValue2",
228 executorForParmeterTest.getSubject().getTaskParameters().get("parameterKey2").getTaskParameterValue());