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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.engine.executor;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
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;
44 public class StateFinalizerExecutorTest {
46 private Executor<?, ?, ?, ?> parentMock;
49 private ApexInternalContext internalContextMock;
52 private AxStateFinalizerLogic stateFinalizerLogicMock;
55 private Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> nextExecutorMock;
58 private EnEvent incomingEvent;
64 public void startMocking() {
65 MockitoAnnotations.initMocks(this);
67 AxState state = new AxState();
68 state.getStateOutputs().put("ValidOutput", null);
70 Mockito.doReturn(state).when(parentMock).getSubject();
72 Mockito.doReturn(new AxReferenceKey("State:0.0.1:StateName:StateSFL")).when(stateFinalizerLogicMock).getKey();
76 public void testStateFinalizerExecutor() {
77 DummyStateFinalizerExecutor executor = new DummyStateFinalizerExecutor();
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());
89 executor.setParameters(new ExecutorParameters());
90 executor.setNext(nextExecutorMock);
91 assertEquals(nextExecutorMock, executor.getNext());
92 executor.setNext(null);
93 assertEquals(null, executor.getNext());
97 fail("test should throw an exception");
98 } catch (Exception ex) {
99 assertEquals("cleanUp() not implemented on class", ex.getMessage());
102 Mockito.doReturn(null).when(stateFinalizerLogicMock).getLogic();
106 fail("test should throw an exception");
107 } catch (Exception ex) {
108 assertEquals("state finalizer logic cannot be null.", ex.getMessage());
111 Mockito.doReturn("some task logic").when(stateFinalizerLogicMock).getLogic();
115 } catch (StateMachineException e) {
116 fail("test should not throw an exception");
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());
126 executor.executePre(0, null, incomingEvent);
127 } catch (Exception e) {
128 fail("test should not throw an exception");
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());
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());
147 executor.getExecutionContext().setMessage("Execution message");
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());
157 executor.executePre(0, null, incomingEvent);
158 } catch (Exception ex) {
159 fail("test should not throw an exception");
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",
171 executor.executePre(0, null, incomingEvent);
172 } catch (Exception ex) {
173 fail("test should not throw an exception");
176 executor.getExecutionContext().setSelectedStateOutputName("ThisOutputDoesNotExist");
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\"",
187 executor.executePre(0, null, incomingEvent);
188 } catch (Exception ex) {
189 fail("test should not throw an exception");
192 executor.getExecutionContext().setSelectedStateOutputName("ValidOutput");
194 executor.executePost(true);
195 } catch (Exception ex) {
196 fail("test should not throw an exception");