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;
27 import java.util.LinkedHashMap;
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.AxArtifactKey;
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.AxStateTaskReference;
43 import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic;
48 public class TaskSelectExecutorTest {
50 private AxState axStateMock;
53 private ApexInternalContext internalContextMock;
56 private Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> nextExecutorMock;
59 private AxTaskSelectionLogic taskSelectionLogicMock;
62 private EnEvent incomingEvent;
68 public void startMocking() {
69 MockitoAnnotations.initMocks(this);
71 AxReferenceKey state0Key = new AxReferenceKey("State0Parent:0.0.1:Parent:State0");
72 Mockito.doReturn(state0Key).when(axStateMock).getKey();
73 Mockito.doReturn(state0Key.getId()).when(axStateMock).getId();
75 Map<AxArtifactKey, AxStateTaskReference> taskReferences = new LinkedHashMap<>();
76 taskReferences.put(new AxArtifactKey("Task0:0.0.0"), null);
77 taskReferences.put(new AxArtifactKey("Task1:0.0.0"), null);
78 Mockito.doReturn(taskReferences).when(axStateMock).getTaskReferences();
79 Mockito.doReturn(new AxArtifactKey("Task1:0.0.0")).when(axStateMock).getDefaultTask();
81 Mockito.doReturn(taskSelectionLogicMock).when(axStateMock).getTaskSelectionLogic();
83 Mockito.doReturn(new AxArtifactKey("Context:0.0.1")).when(internalContextMock).getKey();
87 public void testTaskSelectionExecutor() throws StateMachineException {
88 DummyTaskSelectExecutor executor = new DummyTaskSelectExecutor();
90 executor.setContext(null, axStateMock, internalContextMock);
91 assertEquals("State0Parent:0.0.1:Parent:State0", executor.getKey().getId());
92 assertEquals(null, executor.getExecutionContext());
93 assertEquals(null, executor.getParent());
94 assertEquals(internalContextMock, executor.getContext());
95 assertEquals(null, executor.getNext());
96 assertEquals(null, executor.getIncoming());
97 assertEquals(null, executor.getOutgoing());
98 assertEquals(axStateMock, executor.getSubject());
100 executor.setParameters(new ExecutorParameters());
101 executor.setNext(nextExecutorMock);
102 assertEquals(nextExecutorMock, executor.getNext());
103 executor.setNext(null);
104 assertEquals(null, executor.getNext());
106 assertThatThrownBy(executor::cleanUp)
107 .hasMessage("cleanUp() not implemented on class");
108 Mockito.doReturn(null).when(taskSelectionLogicMock).getLogic();
110 assertThatThrownBy(executor::prepare)
111 .hasMessage("task selection logic cannot be null.");
112 Mockito.doReturn("some task logic").when(taskSelectionLogicMock).getLogic();
116 executor.executePre(0, new Properties(), incomingEvent);
118 assertThatThrownBy(() -> executor.execute(0, new Properties(), incomingEvent))
119 .hasMessage("execute() not implemented on class");
120 assertThatThrownBy(() -> executor.executePost(false))
121 .hasMessage("execute-post: task selection logic failed on state \"State0Parent:0.0.1:Parent:State0\"");
123 executor.getExecutionContext().setMessage("Execution message");
124 assertThatThrownBy(() -> executor.executePost(false))
125 .hasMessageContaining("execute-post: task selection logic failed on state \""
126 + "State0Parent:0.0.1:Parent:State0\", user message: Execution message");
127 executor.executePre(0, new Properties(), incomingEvent);
129 executor.executePost(true);
130 assertEquals("Task1", executor.getOutgoing().getName());
132 executor.executePre(0, new Properties(), incomingEvent);
134 executor.getOutgoing().setName("IDontExist");
135 assertThatThrownBy(() -> executor.executePost(true))
136 .hasMessageContaining("task \"IDontExist:0.0.0\" returned by task selection logic not defined "
137 + "on state \"State0Parent:0.0.1:Parent:State0\"");
138 executor.executePre(0, new Properties(), incomingEvent);
140 executor.getOutgoing().setName("Task0");
142 executor.executePost(true);
143 assertEquals("Task0", executor.getOutgoing().getName());
145 assertThatThrownBy(() -> executor.executePre(0, null, incomingEvent))
146 .hasMessageMatching("^executionProperties is marked .*on.*ull but is null$");