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;
28 import java.util.LinkedHashMap;
30 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.AxArtifactKey;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
42 import org.onap.policy.apex.model.policymodel.concepts.AxState;
43 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference;
44 import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic;
49 public class TaskSelectExecutorTest {
51 private AxState axStateMock;
54 private ApexInternalContext internalContextMock;
57 private Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> nextExecutorMock;
60 private AxTaskSelectionLogic taskSelectionLogicMock;
63 private EnEvent incomingEvent;
69 public void startMocking() {
70 MockitoAnnotations.initMocks(this);
72 AxReferenceKey state0Key = new AxReferenceKey("State0Parent:0.0.1:Parent:State0");
73 Mockito.doReturn(state0Key).when(axStateMock).getKey();
74 Mockito.doReturn(state0Key.getId()).when(axStateMock).getId();
76 Map<AxArtifactKey, AxStateTaskReference> taskReferences = new LinkedHashMap<>();
77 taskReferences.put(new AxArtifactKey("Task0:0.0.0"), null);
78 taskReferences.put(new AxArtifactKey("Task1:0.0.0"), null);
79 Mockito.doReturn(taskReferences).when(axStateMock).getTaskReferences();
80 Mockito.doReturn(new AxArtifactKey("Task1:0.0.0")).when(axStateMock).getDefaultTask();
82 Mockito.doReturn(taskSelectionLogicMock).when(axStateMock).getTaskSelectionLogic();
84 Mockito.doReturn(new AxArtifactKey("Context:0.0.1")).when(internalContextMock).getKey();
88 public void testTaskSelectionExecutor() {
89 DummyTaskSelectExecutor executor = new DummyTaskSelectExecutor();
91 executor.setContext(null, axStateMock, internalContextMock);
92 assertEquals("State0Parent:0.0.1:Parent:State0", executor.getKey().getId());
93 assertEquals(null, executor.getExecutionContext());
94 assertEquals(null, executor.getParent());
95 assertEquals(internalContextMock, executor.getContext());
96 assertEquals(null, executor.getNext());
97 assertEquals(null, executor.getIncoming());
98 assertEquals(null, executor.getOutgoing());
99 assertEquals(axStateMock, executor.getSubject());
101 executor.setParameters(new ExecutorParameters());
102 executor.setNext(nextExecutorMock);
103 assertEquals(nextExecutorMock, executor.getNext());
104 executor.setNext(null);
105 assertEquals(null, executor.getNext());
109 fail("test should throw an exception");
110 } catch (Exception ex) {
111 assertEquals("cleanUp() not implemented on class", ex.getMessage());
114 Mockito.doReturn(null).when(taskSelectionLogicMock).getLogic();
118 fail("test should throw an exception");
119 } catch (Exception ex) {
120 assertEquals("task selection logic cannot be null.", ex.getMessage());
123 Mockito.doReturn("some task logic").when(taskSelectionLogicMock).getLogic();
127 } catch (StateMachineException e) {
128 fail("test should not throw an exception");
132 executor.executePre(0, new Properties(), incomingEvent);
133 } catch (Exception ex) {
134 assertEquals("task input fields \"[InField0]\" are missing for task \"Task0:0.0.1\"", ex.getMessage());
138 executor.executePre(0, new Properties(), incomingEvent);
139 } catch (Exception e) {
140 fail("test should not throw an exception");
144 executor.execute(0, new Properties(), incomingEvent);
145 fail("test should throw an exception");
146 } catch (Exception ex) {
147 assertEquals("execute() not implemented on class", ex.getMessage());
151 executor.executePost(false);
152 fail("test should throw an exception");
153 } catch (Exception ex) {
154 assertEquals("execute-post: task selection logic failed on state \"State0Parent:0.0.1:Parent:State0\"",
158 executor.getExecutionContext().setMessage("Execution message");
160 executor.executePost(false);
161 fail("test should throw an exception");
162 } catch (Exception ex) {
163 assertEquals("execute-post: task selection logic failed on state \"State0Parent:0.0.1:Parent:State0\", "
164 + "user message: Execution message", ex.getMessage());
168 executor.executePre(0, new Properties(), incomingEvent);
169 } catch (Exception e) {
170 fail("test should not throw an exception");
174 executor.executePost(true);
175 assertEquals("Task1", executor.getOutgoing().getName());
176 } catch (Exception e) {
177 fail("test should not throw an exception");
181 executor.executePre(0, new Properties(), incomingEvent);
182 } catch (Exception e) {
183 fail("test should not throw an exception");
186 executor.getOutgoing().setName("IDontExist");
188 executor.executePost(true);
189 fail("test should throw an exception");
190 } catch (Exception ex) {
191 assertEquals("task \"IDontExist:0.0.0\" returned by task selection logic not defined "
192 + "on state \"State0Parent:0.0.1:Parent:State0\"", ex.getMessage());
196 executor.executePre(0, new Properties(), incomingEvent);
197 } catch (Exception e) {
198 fail("test should not throw an exception");
201 executor.getOutgoing().setName("Task0");
204 executor.executePost(true);
205 assertEquals("Task0", executor.getOutgoing().getName());
206 } catch (Exception e) {
207 fail("test should not throw an exception");
210 assertThatThrownBy(() -> executor.executePre(0, null, incomingEvent))
211 .hasMessageMatching("^executionProperties is marked .*on.*ull but is null$");