8e907e12af1b93d33802b2045cd4b07f45e5d166
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.core.engine.executor;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
25
26 import java.util.LinkedHashMap;
27 import java.util.Map;
28 import java.util.Properties;
29
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.mockito.MockitoAnnotations;
35 import org.onap.policy.apex.core.engine.ExecutorParameters;
36 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
37 import org.onap.policy.apex.core.engine.event.EnEvent;
38 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
41 import org.onap.policy.apex.model.policymodel.concepts.AxState;
42 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference;
43 import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic;
44
45 /**
46  * Test task executor.
47  */
48 public class TaskSelectExecutorTest {
49     @Mock
50     private AxState axStateMock;
51
52     @Mock
53     private ApexInternalContext internalContextMock;
54
55     @Mock
56     private Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> nextExecutorMock;
57
58     @Mock
59     private AxTaskSelectionLogic taskSelectionLogicMock;
60
61     @Mock
62     private EnEvent incomingEvent;
63
64     /**
65      * Set up mocking.
66      */
67     @Before
68     public void startMocking() {
69         MockitoAnnotations.initMocks(this);
70
71         AxReferenceKey state0Key = new AxReferenceKey("State0Parent:0.0.1:Parent:State0");
72         Mockito.doReturn(state0Key).when(axStateMock).getKey();
73         Mockito.doReturn(state0Key.getId()).when(axStateMock).getId();
74
75         Map<AxArtifactKey, AxStateTaskReference> taskReferences = new LinkedHashMap<>();
76         taskReferences.put(new AxArtifactKey("Task0:0.0.0"), null);
77         taskReferences.put(new AxArtifactKey("Task1:0.0.0"), null);
78         Mockito.doReturn(taskReferences).when(axStateMock).getTaskReferences();
79         Mockito.doReturn(new AxArtifactKey("Task1:0.0.0")).when(axStateMock).getDefaultTask();
80
81         Mockito.doReturn(taskSelectionLogicMock).when(axStateMock).getTaskSelectionLogic();
82
83         Mockito.doReturn(new AxArtifactKey("Context:0.0.1")).when(internalContextMock).getKey();
84     }
85
86     @Test
87     public void testTaskSelectionExecutor() {
88         DummyTaskSelectExecutor executor = new DummyTaskSelectExecutor();
89
90         executor.setContext(null, axStateMock, internalContextMock);
91         assertEquals("State0Parent:0.0.1:Parent:State0", executor.getKey().getId());
92         assertEquals(null, executor.getExecutionContext());
93         assertEquals(null, executor.getParent());
94         assertEquals(internalContextMock, executor.getContext());
95         assertEquals(null, executor.getNext());
96         assertEquals(null, executor.getIncoming());
97         assertEquals(null, executor.getOutgoing());
98         assertEquals(axStateMock, executor.getSubject());
99
100         executor.setParameters(new ExecutorParameters());
101         executor.setNext(nextExecutorMock);
102         assertEquals(nextExecutorMock, executor.getNext());
103         executor.setNext(null);
104         assertEquals(null, executor.getNext());
105
106         try {
107             executor.cleanUp();
108             fail("test should throw an exception");
109         } catch (Exception ex) {
110             assertEquals("cleanUp() not implemented on class", ex.getMessage());
111         }
112
113         Mockito.doReturn(null).when(taskSelectionLogicMock).getLogic();
114
115         try {
116             executor.prepare();
117             fail("test should throw an exception");
118         } catch (Exception ex) {
119             assertEquals("task selection logic cannot be null.", ex.getMessage());
120         }
121
122         Mockito.doReturn("some task logic").when(taskSelectionLogicMock).getLogic();
123
124         try {
125             executor.prepare();
126         } catch (StateMachineException e) {
127             fail("test should not throw an exception");
128         }
129
130         try {
131             executor.executePre(0, new Properties(), incomingEvent);
132         } catch (Exception ex) {
133             assertEquals("task input fields \"[InField0]\" are missing for task \"Task0:0.0.1\"", ex.getMessage());
134         }
135
136         try {
137             executor.executePre(0, new Properties(), incomingEvent);
138         } catch (Exception e) {
139             fail("test should not throw an exception");
140         }
141
142         try {
143             executor.execute(0, new Properties(), incomingEvent);
144             fail("test should throw an exception");
145         } catch (Exception ex) {
146             assertEquals("execute() not implemented on class", ex.getMessage());
147         }
148
149         try {
150             executor.executePost(false);
151             fail("test should throw an exception");
152         } catch (Exception ex) {
153             assertEquals("execute-post: task selection logic failed on state \"State0Parent:0.0.1:Parent:State0\"",
154                     ex.getMessage());
155         }
156
157         executor.getExecutionContext().setMessage("Execution message");
158         try {
159             executor.executePost(false);
160             fail("test should throw an exception");
161         } catch (Exception ex) {
162             assertEquals("execute-post: task selection logic failed on state \"State0Parent:0.0.1:Parent:State0\", "
163                     + "user message: Execution message", ex.getMessage());
164         }
165
166         try {
167             executor.executePre(0, new Properties(), incomingEvent);
168         } catch (Exception e) {
169             fail("test should not throw an exception");
170         }
171
172         try {
173             executor.executePost(true);
174             assertEquals("Task1", executor.getOutgoing().getName());
175         } catch (Exception e) {
176             fail("test should not throw an exception");
177         }
178
179         try {
180             executor.executePre(0, new Properties(), incomingEvent);
181         } catch (Exception e) {
182             fail("test should not throw an exception");
183         }
184
185         executor.getOutgoing().setName("IDontExist");
186         try {
187             executor.executePost(true);
188             fail("test should throw an exception");
189         } catch (Exception ex) {
190             assertEquals("task \"IDontExist:0.0.0\" returned by task selection logic not defined "
191                     + "on state \"State0Parent:0.0.1:Parent:State0\"", ex.getMessage());
192         }
193
194         try {
195             executor.executePre(0, new Properties(), incomingEvent);
196         } catch (Exception e) {
197             fail("test should not throw an exception");
198         }
199
200         executor.getOutgoing().setName("Task0");
201
202         try {
203             executor.executePost(true);
204             assertEquals("Task0", executor.getOutgoing().getName());
205         } catch (Exception e) {
206             fail("test should not throw an exception");
207         }
208
209         try {
210             executor.executePre(0, null, incomingEvent);
211             fail("test should throw an exception");
212         } catch (Exception ex) {
213             assertEquals("executionProperties is marked @NonNull but is null", ex.getMessage());
214         }
215     }
216 }