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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.core.engine.executor;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
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;
46 public class StateFinalizerExecutorTest {
48 private Executor<?, ?, ?, ?> parentMock;
51 private ApexInternalContext internalContextMock;
54 private AxStateFinalizerLogic stateFinalizerLogicMock;
57 private Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> nextExecutorMock;
60 private EnEvent incomingEvent;
66 public void startMocking() {
67 MockitoAnnotations.initMocks(this);
69 AxState state = new AxState();
70 state.getStateOutputs().put("ValidOutput", null);
72 Mockito.doReturn(state).when(parentMock).getSubject();
74 Mockito.doReturn(new AxReferenceKey("State:0.0.1:StateName:StateSFL")).when(stateFinalizerLogicMock).getKey();
78 public void testStateFinalizerExecutor() {
79 DummyStateFinalizerExecutor executor = new DummyStateFinalizerExecutor();
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());
91 executor.setParameters(new ExecutorParameters());
92 executor.setNext(nextExecutorMock);
93 assertEquals(nextExecutorMock, executor.getNext());
94 executor.setNext(null);
95 assertEquals(null, executor.getNext());
99 fail("test should throw an exception");
100 } catch (Exception ex) {
101 assertEquals("cleanUp() not implemented on class", ex.getMessage());
104 Mockito.doReturn(null).when(stateFinalizerLogicMock).getLogic();
108 fail("test should throw an exception");
109 } catch (Exception ex) {
110 assertEquals("state finalizer logic cannot be null.", ex.getMessage());
113 Mockito.doReturn("some task logic").when(stateFinalizerLogicMock).getLogic();
117 } catch (StateMachineException e) {
118 fail("test should not throw an exception");
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());
127 assertThatThrownBy(() -> executor.executePre(0, null, incomingEvent))
128 .hasMessageMatching("^executionProperties is marked .*on.*ull but is null$");
131 executor.executePre(0, new Properties(), incomingEvent);
132 } catch (Exception e) {
133 fail("test should not throw an exception");
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());
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());
152 executor.getExecutionContext().setMessage("Execution message");
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());
162 executor.executePre(0, new Properties(), incomingEvent);
163 } catch (Exception ex) {
164 fail("test should not throw an exception");
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",
176 executor.executePre(0, new Properties(), incomingEvent);
177 } catch (Exception ex) {
178 fail("test should not throw an exception");
181 executor.getExecutionContext().setSelectedStateOutputName("ThisOutputDoesNotExist");
183 executor.executePost(true);
184 fail("test should throw an exception");
185 } catch (Exception ex) {
187 "execute-post: state finalizer logic \"null\" selected output state "
188 + "\"ThisOutputDoesNotExist\" that does not exsist on state \"NULL:0.0.0:NULL:NULL\"",
193 executor.executePre(0, new Properties(), incomingEvent);
194 } catch (Exception ex) {
195 fail("test should not throw an exception");
198 executor.getExecutionContext().setSelectedStateOutputName("ValidOutput");
200 executor.executePost(true);
201 } catch (Exception ex) {
202 fail("test should not throw an exception");