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;
28 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.context.ContextException;
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() throws StateMachineException, ContextException {
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());
97 assertThatThrownBy(executor::cleanUp)
98 .hasMessage("cleanUp() not implemented on class");
99 Mockito.doReturn(null).when(stateFinalizerLogicMock).getLogic();
101 assertThatThrownBy(executor::prepare)
102 .hasMessage("state finalizer logic cannot be null.");
103 Mockito.doReturn("some task logic").when(stateFinalizerLogicMock).getLogic();
107 executor.executePre(0, new Properties(), incomingEvent);
108 assertThatThrownBy(() -> executor.executePre(0, null, incomingEvent))
109 .hasMessageMatching("^executionProperties is marked .*on.*ull but is null$");
111 executor.executePre(0, new Properties(), incomingEvent);
113 assertThatThrownBy(() -> executor.execute(0, new Properties(), incomingEvent))
114 .hasMessage("execute() not implemented on abstract StateFinalizerExecutionContext class, "
115 + "only on its subclasses");
116 assertThatThrownBy(() -> executor.executePost(false))
117 .hasMessage("execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:"
118 + "NULL:NULL\" on finalizer logic null");
119 executor.getExecutionContext().setMessage("Execution message");
120 assertThatThrownBy(() -> executor.executePost(false))
121 .hasMessage("execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:"
122 + "NULL:NULL\" on finalizer logic null, user message: Execution message");
123 executor.executePre(0, new Properties(), incomingEvent);
125 assertThatThrownBy(() -> executor.executePost(true))
126 .hasMessage("execute-post: state finalizer logic \"null\" did not select an output state");
127 executor.executePre(0, new Properties(), incomingEvent);
129 executor.getExecutionContext().setSelectedStateOutputName("ThisOutputDoesNotExist");
130 assertThatThrownBy(() -> executor.executePost(true))
131 .hasMessage("execute-post: state finalizer logic \"null\" selected output state "
132 + "\"ThisOutputDoesNotExist\" that does not exsist on state \"NULL:0.0.0:NULL:NULL\"");
133 executor.executePre(0, new Properties(), incomingEvent);
135 executor.getExecutionContext().setSelectedStateOutputName("ValidOutput");
136 executor.executePost(true);