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