8f65444970b3bbba88fecab0e7f49a1ff7241347
[policy/apex-pdp.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.core.engine.executor;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26
27 import java.util.Map;
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;
42
43 /**
44  * Test task executor.
45  */
46 public class StateFinalizerExecutorTest {
47     @Mock
48     private Executor<?, ?, ?, ?> parentMock;
49
50     @Mock
51     private ApexInternalContext internalContextMock;
52
53     @Mock
54     private AxStateFinalizerLogic stateFinalizerLogicMock;
55
56     @Mock
57     private Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> nextExecutorMock;
58
59     @Mock
60     private EnEvent incomingEvent;
61
62     /**
63      * Set up mocking.
64      */
65     @Before
66     public void startMocking() {
67         MockitoAnnotations.initMocks(this);
68
69         AxState state = new AxState();
70         state.getStateOutputs().put("ValidOutput", null);
71
72         Mockito.doReturn(state).when(parentMock).getSubject();
73
74         Mockito.doReturn(new AxReferenceKey("State:0.0.1:StateName:StateSFL")).when(stateFinalizerLogicMock).getKey();
75     }
76
77     @Test
78     public void testStateFinalizerExecutor() throws StateMachineException, ContextException {
79         DummyStateFinalizerExecutor executor = new DummyStateFinalizerExecutor();
80
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());
90
91         executor.setParameters(new ExecutorParameters());
92         executor.setNext(nextExecutorMock);
93         assertEquals(nextExecutorMock, executor.getNext());
94         executor.setNext(null);
95         assertEquals(null, executor.getNext());
96
97         assertThatThrownBy(executor::cleanUp)
98             .hasMessage("cleanUp() not implemented on class");
99         Mockito.doReturn(null).when(stateFinalizerLogicMock).getLogic();
100
101         assertThatThrownBy(executor::prepare)
102             .hasMessage("state finalizer logic cannot be null.");
103         Mockito.doReturn("some task logic").when(stateFinalizerLogicMock).getLogic();
104
105         executor.prepare();
106
107         executor.executePre(0, new Properties(), incomingEvent);
108         assertThatThrownBy(() -> executor.executePre(0, null, incomingEvent))
109             .hasMessageMatching("^executionProperties is marked .*on.*ull but is null$");
110
111         executor.executePre(0, new Properties(), incomingEvent);
112
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);
124
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);
128
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);
134
135         executor.getExecutionContext().setSelectedStateOutputName("ValidOutput");
136         executor.executePost(true);
137
138     }
139 }