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