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;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.policy.apex.core.engine.ExecutorParameters;
37 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
38 import org.onap.policy.apex.core.engine.event.EnEvent;
39 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
41 import org.onap.policy.apex.model.policymodel.concepts.AxState;
42 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
47 public class StateFinalizerExecutorTest {
49 private Executor<?, ?, ?, ?> parentMock;
52 private ApexInternalContext internalContextMock;
55 private AxStateFinalizerLogic stateFinalizerLogicMock;
58 private Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> nextExecutorMock;
61 private EnEvent incomingEvent;
67 public void startMocking() {
68 MockitoAnnotations.initMocks(this);
70 AxState state = new AxState();
71 state.getStateOutputs().put("ValidOutput", null);
73 Mockito.doReturn(state).when(parentMock).getSubject();
75 Mockito.doReturn(new AxReferenceKey("State:0.0.1:StateName:StateSFL")).when(stateFinalizerLogicMock).getKey();
79 public void testStateFinalizerExecutor() {
80 DummyStateFinalizerExecutor executor = new DummyStateFinalizerExecutor();
82 executor.setContext(parentMock, stateFinalizerLogicMock, internalContextMock);
83 assertEquals("State:0.0.1:StateName:StateSFL", executor.getKey().getId());
84 assertEquals(null, executor.getExecutionContext());
85 assertEquals(parentMock, executor.getParent());
86 assertEquals(internalContextMock, executor.getContext());
87 assertEquals(null, executor.getNext());
88 assertEquals(null, executor.getIncoming());
89 assertEquals(null, executor.getOutgoing());
90 assertEquals(stateFinalizerLogicMock, executor.getSubject());
92 executor.setParameters(new ExecutorParameters());
93 executor.setNext(nextExecutorMock);
94 assertEquals(nextExecutorMock, executor.getNext());
95 executor.setNext(null);
96 assertEquals(null, executor.getNext());
100 fail("test should throw an exception");
101 } catch (Exception ex) {
102 assertEquals("cleanUp() not implemented on class", ex.getMessage());
105 Mockito.doReturn(null).when(stateFinalizerLogicMock).getLogic();
109 fail("test should throw an exception");
110 } catch (Exception ex) {
111 assertEquals("state finalizer logic cannot be null.", ex.getMessage());
114 Mockito.doReturn("some task logic").when(stateFinalizerLogicMock).getLogic();
118 } catch (StateMachineException e) {
119 fail("test should not throw an exception");
123 executor.executePre(0, new Properties(), incomingEvent);
124 } catch (Exception ex) {
125 assertEquals("task input fields \"[InField0]\" are missing for task \"Task0:0.0.1\"", ex.getMessage());
128 assertThatThrownBy(() -> executor.executePre(0, null, incomingEvent))
129 .hasMessageMatching("^executionProperties is marked .*on.*ull but is null$");
132 executor.executePre(0, new Properties(), incomingEvent);
133 } catch (Exception e) {
134 fail("test should not throw an exception");
138 executor.execute(0, new Properties(), incomingEvent);
139 fail("test should throw an exception");
140 } catch (Exception ex) {
141 assertEquals("execute() not implemented on abstract StateFinalizerExecutionContext class, "
142 + "only on its subclasses", ex.getMessage());
146 executor.executePost(false);
147 fail("test should throw an exception");
148 } catch (Exception ex) {
149 assertEquals("execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:NULL:NULL\" "
150 + "on finalizer logic null", ex.getMessage());
153 executor.getExecutionContext().setMessage("Execution message");
155 executor.executePost(false);
156 fail("test should throw an exception");
157 } catch (Exception ex) {
158 assertEquals("execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:NULL:NULL\" "
159 + "on finalizer logic null, user message: Execution message", ex.getMessage());
163 executor.executePre(0, new Properties(), incomingEvent);
164 } catch (Exception ex) {
165 fail("test should not throw an exception");
169 executor.executePost(true);
170 fail("test should throw an exception");
171 } catch (Exception ex) {
172 assertEquals("execute-post: state finalizer logic \"null\" did not select an output state",
177 executor.executePre(0, new Properties(), incomingEvent);
178 } catch (Exception ex) {
179 fail("test should not throw an exception");
182 executor.getExecutionContext().setSelectedStateOutputName("ThisOutputDoesNotExist");
184 executor.executePost(true);
185 fail("test should throw an exception");
186 } catch (Exception ex) {
188 "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");