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