6fb28bca83d4859046f3069e6fe23707b3a783f5
[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.Map;
27 import java.util.Properties;
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.AxReferenceKey;
39 import org.onap.policy.apex.model.policymodel.concepts.AxState;
40 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
41
42 /**
43  * Test task executor.
44  */
45 public class StateFinalizerExecutorTest {
46     @Mock
47     private Executor<?, ?, ?, ?> parentMock;
48
49     @Mock
50     private ApexInternalContext internalContextMock;
51
52     @Mock
53     private AxStateFinalizerLogic stateFinalizerLogicMock;
54
55     @Mock
56     private Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> nextExecutorMock;
57
58     @Mock
59     private EnEvent incomingEvent;
60
61     /**
62      * Set up mocking.
63      */
64     @Before
65     public void startMocking() {
66         MockitoAnnotations.initMocks(this);
67
68         AxState state = new AxState();
69         state.getStateOutputs().put("ValidOutput", null);
70
71         Mockito.doReturn(state).when(parentMock).getSubject();
72
73         Mockito.doReturn(new AxReferenceKey("State:0.0.1:StateName:StateSFL")).when(stateFinalizerLogicMock).getKey();
74     }
75
76     @Test
77     public void testStateFinalizerExecutor() {
78         DummyStateFinalizerExecutor executor = new DummyStateFinalizerExecutor();
79
80         executor.setContext(parentMock, stateFinalizerLogicMock, internalContextMock);
81         assertEquals("State:0.0.1:StateName:StateSFL", executor.getKey().getId());
82         assertEquals(null, executor.getExecutionContext());
83         assertEquals(parentMock, executor.getParent());
84         assertEquals(internalContextMock, executor.getContext());
85         assertEquals(null, executor.getNext());
86         assertEquals(null, executor.getIncoming());
87         assertEquals(null, executor.getOutgoing());
88         assertEquals(stateFinalizerLogicMock, executor.getSubject());
89
90         executor.setParameters(new ExecutorParameters());
91         executor.setNext(nextExecutorMock);
92         assertEquals(nextExecutorMock, executor.getNext());
93         executor.setNext(null);
94         assertEquals(null, executor.getNext());
95
96         try {
97             executor.cleanUp();
98             fail("test should throw an exception");
99         } catch (Exception ex) {
100             assertEquals("cleanUp() not implemented on class", ex.getMessage());
101         }
102
103         Mockito.doReturn(null).when(stateFinalizerLogicMock).getLogic();
104
105         try {
106             executor.prepare();
107             fail("test should throw an exception");
108         } catch (Exception ex) {
109             assertEquals("state finalizer logic cannot be null.", ex.getMessage());
110         }
111
112         Mockito.doReturn("some task logic").when(stateFinalizerLogicMock).getLogic();
113
114         try {
115             executor.prepare();
116         } catch (StateMachineException e) {
117             fail("test should not throw an exception");
118         }
119
120         try {
121             executor.executePre(0, new Properties(), incomingEvent);
122         } catch (Exception ex) {
123             assertEquals("task input fields \"[InField0]\" are missing for task \"Task0:0.0.1\"", ex.getMessage());
124         }
125
126         try {
127             executor.executePre(0, null, incomingEvent);
128         } catch (Exception ex) {
129             assertEquals("executionProperties is marked @NonNull but is null", ex.getMessage());
130         }
131
132         try {
133             executor.executePre(0, new Properties(), incomingEvent);
134         } catch (Exception e) {
135             fail("test should not throw an exception");
136         }
137
138         try {
139             executor.execute(0, new Properties(), incomingEvent);
140             fail("test should throw an exception");
141         } catch (Exception ex) {
142             assertEquals("execute() not implemented on abstract StateFinalizerExecutionContext class, "
143                             + "only on its subclasses", ex.getMessage());
144         }
145
146         try {
147             executor.executePost(false);
148             fail("test should throw an exception");
149         } catch (Exception ex) {
150             assertEquals("execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:NULL:NULL\" "
151                             + "on finalizer logic null", ex.getMessage());
152         }
153
154         executor.getExecutionContext().setMessage("Execution message");
155         try {
156             executor.executePost(false);
157             fail("test should throw an exception");
158         } catch (Exception ex) {
159             assertEquals("execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:NULL:NULL\" "
160                             + "on finalizer logic null, user message: Execution message", ex.getMessage());
161         }
162
163         try {
164             executor.executePre(0, new Properties(), incomingEvent);
165         } catch (Exception ex) {
166             fail("test should not throw an exception");
167         }
168
169         try {
170             executor.executePost(true);
171             fail("test should throw an exception");
172         } catch (Exception ex) {
173             assertEquals("execute-post: state finalizer logic \"null\" did not select an output state",
174                             ex.getMessage());
175         }
176
177         try {
178             executor.executePre(0, new Properties(), incomingEvent);
179         } catch (Exception ex) {
180             fail("test should not throw an exception");
181         }
182
183         executor.getExecutionContext().setSelectedStateOutputName("ThisOutputDoesNotExist");
184         try {
185             executor.executePost(true);
186             fail("test should throw an exception");
187         } catch (Exception ex) {
188             assertEquals("execute-post: state finalizer logic \"null\" selected output state "
189                             + "\"ThisOutputDoesNotExist\" that does not exsist on state \"NULL:0.0.0:NULL:NULL\"",
190                             ex.getMessage());
191         }
192
193         try {
194             executor.executePre(0, new Properties(), incomingEvent);
195         } catch (Exception ex) {
196             fail("test should not throw an exception");
197         }
198
199         executor.getExecutionContext().setSelectedStateOutputName("ValidOutput");
200         try {
201             executor.executePost(true);
202         } catch (Exception ex) {
203             fail("test should not throw an exception");
204         }
205     }
206 }