Changes for checkstyle 8.32
[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 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.policy.apex.core.engine.ExecutorParameters;
37 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
38 import org.onap.policy.apex.core.engine.event.EnEvent;
39 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
42 import org.onap.policy.apex.model.policymodel.concepts.AxState;
43 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference;
44 import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic;
45
46 /**
47  * Test task executor.
48  */
49 public class TaskSelectExecutorTest {
50     @Mock
51     private AxState axStateMock;
52
53     @Mock
54     private ApexInternalContext internalContextMock;
55
56     @Mock
57     private Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> nextExecutorMock;
58
59     @Mock
60     private AxTaskSelectionLogic taskSelectionLogicMock;
61
62     @Mock
63     private EnEvent incomingEvent;
64
65     /**
66      * Set up mocking.
67      */
68     @Before
69     public void startMocking() {
70         MockitoAnnotations.initMocks(this);
71
72         AxReferenceKey state0Key = new AxReferenceKey("State0Parent:0.0.1:Parent:State0");
73         Mockito.doReturn(state0Key).when(axStateMock).getKey();
74         Mockito.doReturn(state0Key.getId()).when(axStateMock).getId();
75
76         Map<AxArtifactKey, AxStateTaskReference> taskReferences = new LinkedHashMap<>();
77         taskReferences.put(new AxArtifactKey("Task0:0.0.0"), null);
78         taskReferences.put(new AxArtifactKey("Task1:0.0.0"), null);
79         Mockito.doReturn(taskReferences).when(axStateMock).getTaskReferences();
80         Mockito.doReturn(new AxArtifactKey("Task1:0.0.0")).when(axStateMock).getDefaultTask();
81
82         Mockito.doReturn(taskSelectionLogicMock).when(axStateMock).getTaskSelectionLogic();
83
84         Mockito.doReturn(new AxArtifactKey("Context:0.0.1")).when(internalContextMock).getKey();
85     }
86
87     @Test
88     public void testTaskSelectionExecutor() {
89         DummyTaskSelectExecutor executor = new DummyTaskSelectExecutor();
90
91         executor.setContext(null, axStateMock, internalContextMock);
92         assertEquals("State0Parent:0.0.1:Parent:State0", executor.getKey().getId());
93         assertEquals(null, executor.getExecutionContext());
94         assertEquals(null, executor.getParent());
95         assertEquals(internalContextMock, executor.getContext());
96         assertEquals(null, executor.getNext());
97         assertEquals(null, executor.getIncoming());
98         assertEquals(null, executor.getOutgoing());
99         assertEquals(axStateMock, executor.getSubject());
100
101         executor.setParameters(new ExecutorParameters());
102         executor.setNext(nextExecutorMock);
103         assertEquals(nextExecutorMock, executor.getNext());
104         executor.setNext(null);
105         assertEquals(null, executor.getNext());
106
107         try {
108             executor.cleanUp();
109             fail("test should throw an exception");
110         } catch (Exception ex) {
111             assertEquals("cleanUp() not implemented on class", ex.getMessage());
112         }
113
114         Mockito.doReturn(null).when(taskSelectionLogicMock).getLogic();
115
116         try {
117             executor.prepare();
118             fail("test should throw an exception");
119         } catch (Exception ex) {
120             assertEquals("task selection logic cannot be null.", ex.getMessage());
121         }
122
123         Mockito.doReturn("some task logic").when(taskSelectionLogicMock).getLogic();
124
125         try {
126             executor.prepare();
127         } catch (StateMachineException e) {
128             fail("test should not throw an exception");
129         }
130
131         try {
132             executor.executePre(0, new Properties(), incomingEvent);
133         } catch (Exception ex) {
134             assertEquals("task input fields \"[InField0]\" are missing for task \"Task0:0.0.1\"", ex.getMessage());
135         }
136
137         try {
138             executor.executePre(0, new Properties(), incomingEvent);
139         } catch (Exception e) {
140             fail("test should not throw an exception");
141         }
142
143         try {
144             executor.execute(0, new Properties(), incomingEvent);
145             fail("test should throw an exception");
146         } catch (Exception ex) {
147             assertEquals("execute() not implemented on class", ex.getMessage());
148         }
149
150         try {
151             executor.executePost(false);
152             fail("test should throw an exception");
153         } catch (Exception ex) {
154             assertEquals("execute-post: task selection logic failed on state \"State0Parent:0.0.1:Parent:State0\"",
155                 ex.getMessage());
156         }
157
158         executor.getExecutionContext().setMessage("Execution message");
159         try {
160             executor.executePost(false);
161             fail("test should throw an exception");
162         } catch (Exception ex) {
163             assertEquals("execute-post: task selection logic failed on state \"State0Parent:0.0.1:Parent:State0\", "
164                 + "user message: Execution message", ex.getMessage());
165         }
166
167         try {
168             executor.executePre(0, new Properties(), incomingEvent);
169         } catch (Exception e) {
170             fail("test should not throw an exception");
171         }
172
173         try {
174             executor.executePost(true);
175             assertEquals("Task1", executor.getOutgoing().getName());
176         } catch (Exception e) {
177             fail("test should not throw an exception");
178         }
179
180         try {
181             executor.executePre(0, new Properties(), incomingEvent);
182         } catch (Exception e) {
183             fail("test should not throw an exception");
184         }
185
186         executor.getOutgoing().setName("IDontExist");
187         try {
188             executor.executePost(true);
189             fail("test should throw an exception");
190         } catch (Exception ex) {
191             assertEquals("task \"IDontExist:0.0.0\" returned by task selection logic not defined "
192                 + "on state \"State0Parent:0.0.1:Parent:State0\"", ex.getMessage());
193         }
194
195         try {
196             executor.executePre(0, new Properties(), incomingEvent);
197         } catch (Exception e) {
198             fail("test should not throw an exception");
199         }
200
201         executor.getOutgoing().setName("Task0");
202
203         try {
204             executor.executePost(true);
205             assertEquals("Task0", executor.getOutgoing().getName());
206         } catch (Exception e) {
207             fail("test should not throw an exception");
208         }
209
210         assertThatThrownBy(() -> executor.executePre(0, null, incomingEvent))
211             .hasMessageMatching("^executionProperties is marked .*on.*ull but is null$");
212     }
213 }