817b1615f815d132f5fe946b60bffa5909166a12
[policy/apex-pdp.git] / core / core-engine / src / test / java / org / onap / policy / apex / core / engine / executor / TaskSelectExecutorTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.core.engine.executor;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
27
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30 import java.util.Properties;
31
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;
46
47 /**
48  * Test task executor.
49  */
50 public class TaskSelectExecutorTest {
51     @Mock
52     private AxState axStateMock;
53
54     @Mock
55     private ApexInternalContext internalContextMock;
56
57     @Mock
58     private Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> nextExecutorMock;
59
60     @Mock
61     private AxTaskSelectionLogic taskSelectionLogicMock;
62
63     @Mock
64     private EnEvent incomingEvent;
65
66     /**
67      * Set up mocking.
68      */
69     @Before
70     public void startMocking() {
71         MockitoAnnotations.initMocks(this);
72
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();
76
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();
82
83         Mockito.doReturn(taskSelectionLogicMock).when(axStateMock).getTaskSelectionLogic();
84
85         Mockito.doReturn(new AxArtifactKey("Context:0.0.1")).when(internalContextMock).getKey();
86     }
87
88     @Test
89     public void testTaskSelectionExecutor() {
90         DummyTaskSelectExecutor executor = new DummyTaskSelectExecutor();
91
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());
101
102         executor.setParameters(new ExecutorParameters());
103         executor.setNext(nextExecutorMock);
104         assertEquals(nextExecutorMock, executor.getNext());
105         executor.setNext(null);
106         assertEquals(null, executor.getNext());
107
108         try {
109             executor.cleanUp();
110             fail("test should throw an exception");
111         } catch (Exception ex) {
112             assertEquals("cleanUp() not implemented on class", ex.getMessage());
113         }
114
115         Mockito.doReturn(null).when(taskSelectionLogicMock).getLogic();
116
117         try {
118             executor.prepare();
119             fail("test should throw an exception");
120         } catch (Exception ex) {
121             assertEquals("task selection logic cannot be null.", ex.getMessage());
122         }
123
124         Mockito.doReturn("some task logic").when(taskSelectionLogicMock).getLogic();
125
126         try {
127             executor.prepare();
128         } catch (StateMachineException e) {
129             fail("test should not throw an exception");
130         }
131
132         try {
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());
136         }
137
138         try {
139             executor.executePre(0, new Properties(), incomingEvent);
140         } catch (Exception e) {
141             fail("test should not throw an exception");
142         }
143
144         try {
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());
149         }
150
151         try {
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\"",
156                 ex.getMessage());
157         }
158
159         executor.getExecutionContext().setMessage("Execution message");
160         try {
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());
166         }
167
168         try {
169             executor.executePre(0, new Properties(), incomingEvent);
170         } catch (Exception e) {
171             fail("test should not throw an exception");
172         }
173
174         try {
175             executor.executePost(true);
176             assertEquals("Task1", executor.getOutgoing().getName());
177         } catch (Exception e) {
178             fail("test should not throw an exception");
179         }
180
181         try {
182             executor.executePre(0, new Properties(), incomingEvent);
183         } catch (Exception e) {
184             fail("test should not throw an exception");
185         }
186
187         executor.getOutgoing().setName("IDontExist");
188         try {
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());
194         }
195
196         try {
197             executor.executePre(0, new Properties(), incomingEvent);
198         } catch (Exception e) {
199             fail("test should not throw an exception");
200         }
201
202         executor.getOutgoing().setName("Task0");
203
204         try {
205             executor.executePost(true);
206             assertEquals("Task0", executor.getOutgoing().getName());
207         } catch (Exception e) {
208             fail("test should not throw an exception");
209         }
210
211         assertThatThrownBy(() -> executor.executePre(0, null, incomingEvent))
212             .hasMessageMatching("^executionProperties is marked .*on.*ull but is null$");
213     }
214 }