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