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;
27 import java.util.Properties;
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;
45 public class StateFinalizerExecutorTest {
47 private Executor<?, ?, ?, ?> parentMock;
50 private ApexInternalContext internalContextMock;
53 private AxStateFinalizerLogic stateFinalizerLogicMock;
56 private Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> nextExecutorMock;
59 private EnEvent incomingEvent;
65 public void startMocking() {
66 MockitoAnnotations.initMocks(this);
68 AxState state = new AxState();
69 state.getStateOutputs().put("ValidOutput", null);
71 Mockito.doReturn(state).when(parentMock).getSubject();
73 Mockito.doReturn(new AxReferenceKey("State:0.0.1:StateName:StateSFL")).when(stateFinalizerLogicMock).getKey();
77 public void testStateFinalizerExecutor() {
78 DummyStateFinalizerExecutor executor = new DummyStateFinalizerExecutor();
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());
90 executor.setParameters(new ExecutorParameters());
91 executor.setNext(nextExecutorMock);
92 assertEquals(nextExecutorMock, executor.getNext());
93 executor.setNext(null);
94 assertEquals(null, executor.getNext());
98 fail("test should throw an exception");
99 } catch (Exception ex) {
100 assertEquals("cleanUp() not implemented on class", ex.getMessage());
103 Mockito.doReturn(null).when(stateFinalizerLogicMock).getLogic();
107 fail("test should throw an exception");
108 } catch (Exception ex) {
109 assertEquals("state finalizer logic cannot be null.", ex.getMessage());
112 Mockito.doReturn("some task logic").when(stateFinalizerLogicMock).getLogic();
116 } catch (StateMachineException e) {
117 fail("test should not throw an exception");
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());
127 executor.executePre(0, null, incomingEvent);
128 } catch (Exception ex) {
129 assertEquals("executionProperties is marked @NonNull but is null", ex.getMessage());
133 executor.executePre(0, new Properties(), incomingEvent);
134 } catch (Exception e) {
135 fail("test should not throw an exception");
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());
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());
154 executor.getExecutionContext().setMessage("Execution message");
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());
164 executor.executePre(0, new Properties(), incomingEvent);
165 } catch (Exception ex) {
166 fail("test should not throw an exception");
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",
178 executor.executePre(0, new Properties(), incomingEvent);
179 } catch (Exception ex) {
180 fail("test should not throw an exception");
183 executor.getExecutionContext().setSelectedStateOutputName("ThisOutputDoesNotExist");
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\"",
194 executor.executePre(0, new Properties(), incomingEvent);
195 } catch (Exception ex) {
196 fail("test should not throw an exception");
199 executor.getExecutionContext().setSelectedStateOutputName("ValidOutput");
201 executor.executePost(true);
202 } catch (Exception ex) {
203 fail("test should not throw an exception");