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.fail;
28 import java.util.LinkedHashMap;
30 import java.util.Properties;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.policy.apex.core.engine.ExecutorParameters;
38 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
39 import org.onap.policy.apex.core.engine.event.EnEvent;
40 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
43 import org.onap.policy.apex.model.policymodel.concepts.AxState;
44 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference;
45 import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic;
50 public class TaskSelectExecutorTest {
52 private AxState axStateMock;
55 private ApexInternalContext internalContextMock;
58 private Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> nextExecutorMock;
61 private AxTaskSelectionLogic taskSelectionLogicMock;
64 private EnEvent incomingEvent;
70 public void startMocking() {
71 MockitoAnnotations.initMocks(this);
73 AxReferenceKey state0Key = new AxReferenceKey("State0Parent:0.0.1:Parent:State0");
74 Mockito.doReturn(state0Key).when(axStateMock).getKey();
75 Mockito.doReturn(state0Key.getId()).when(axStateMock).getId();
77 Map<AxArtifactKey, AxStateTaskReference> taskReferences = new LinkedHashMap<>();
78 taskReferences.put(new AxArtifactKey("Task0:0.0.0"), null);
79 taskReferences.put(new AxArtifactKey("Task1:0.0.0"), null);
80 Mockito.doReturn(taskReferences).when(axStateMock).getTaskReferences();
81 Mockito.doReturn(new AxArtifactKey("Task1:0.0.0")).when(axStateMock).getDefaultTask();
83 Mockito.doReturn(taskSelectionLogicMock).when(axStateMock).getTaskSelectionLogic();
85 Mockito.doReturn(new AxArtifactKey("Context:0.0.1")).when(internalContextMock).getKey();
89 public void testTaskSelectionExecutor() {
90 DummyTaskSelectExecutor executor = new DummyTaskSelectExecutor();
92 executor.setContext(null, axStateMock, internalContextMock);
93 assertEquals("State0Parent:0.0.1:Parent:State0", executor.getKey().getId());
94 assertEquals(null, executor.getExecutionContext());
95 assertEquals(null, executor.getParent());
96 assertEquals(internalContextMock, executor.getContext());
97 assertEquals(null, executor.getNext());
98 assertEquals(null, executor.getIncoming());
99 assertEquals(null, executor.getOutgoing());
100 assertEquals(axStateMock, executor.getSubject());
102 executor.setParameters(new ExecutorParameters());
103 executor.setNext(nextExecutorMock);
104 assertEquals(nextExecutorMock, executor.getNext());
105 executor.setNext(null);
106 assertEquals(null, executor.getNext());
110 fail("test should throw an exception");
111 } catch (Exception ex) {
112 assertEquals("cleanUp() not implemented on class", ex.getMessage());
115 Mockito.doReturn(null).when(taskSelectionLogicMock).getLogic();
119 fail("test should throw an exception");
120 } catch (Exception ex) {
121 assertEquals("task selection logic cannot be null.", ex.getMessage());
124 Mockito.doReturn("some task logic").when(taskSelectionLogicMock).getLogic();
128 } catch (StateMachineException e) {
129 fail("test should not throw an exception");
133 executor.executePre(0, new Properties(), incomingEvent);
134 } catch (Exception ex) {
135 assertEquals("task input fields \"[InField0]\" are missing for task \"Task0:0.0.1\"", ex.getMessage());
139 executor.executePre(0, new Properties(), incomingEvent);
140 } catch (Exception e) {
141 fail("test should not throw an exception");
145 executor.execute(0, new Properties(), incomingEvent);
146 fail("test should throw an exception");
147 } catch (Exception ex) {
148 assertEquals("execute() not implemented on class", ex.getMessage());
152 executor.executePost(false);
153 fail("test should throw an exception");
154 } catch (Exception ex) {
155 assertEquals("execute-post: task selection logic failed on state \"State0Parent:0.0.1:Parent:State0\"",
159 executor.getExecutionContext().setMessage("Execution message");
161 executor.executePost(false);
162 fail("test should throw an exception");
163 } catch (Exception ex) {
164 assertEquals("execute-post: task selection logic failed on state \"State0Parent:0.0.1:Parent:State0\", "
165 + "user message: Execution message", ex.getMessage());
169 executor.executePre(0, new Properties(), incomingEvent);
170 } catch (Exception e) {
171 fail("test should not throw an exception");
175 executor.executePost(true);
176 assertEquals("Task1", executor.getOutgoing().getName());
177 } catch (Exception e) {
178 fail("test should not throw an exception");
182 executor.executePre(0, new Properties(), incomingEvent);
183 } catch (Exception e) {
184 fail("test should not throw an exception");
187 executor.getOutgoing().setName("IDontExist");
189 executor.executePost(true);
190 fail("test should throw an exception");
191 } catch (Exception ex) {
192 assertEquals("task \"IDontExist:0.0.0\" returned by task selection logic not defined "
193 + "on state \"State0Parent:0.0.1:Parent:State0\"", ex.getMessage());
197 executor.executePre(0, new Properties(), incomingEvent);
198 } catch (Exception e) {
199 fail("test should not throw an exception");
202 executor.getOutgoing().setName("Task0");
205 executor.executePost(true);
206 assertEquals("Task0", executor.getOutgoing().getName());
207 } catch (Exception e) {
208 fail("test should not throw an exception");
211 assertThatThrownBy(() -> executor.executePre(0, null, incomingEvent))
212 .hasMessageMatching("^executionProperties is marked .*on.*ull but is null$");