2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.engine.executor;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
26 import java.util.LinkedHashMap;
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.core.engine.ExecutorParameters;
35 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
36 import org.onap.policy.apex.core.engine.event.EnEvent;
37 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
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.AxStateTaskReference;
42 import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic;
47 public class TaskSelectExecutorTest {
49 private AxState axStateMock;
52 private ApexInternalContext internalContextMock;
55 private Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> nextExecutorMock;
58 private AxTaskSelectionLogic taskSelectionLogicMock;
61 private EnEvent incomingEvent;
67 public void startMocking() {
68 MockitoAnnotations.initMocks(this);
70 AxReferenceKey state0Key = new AxReferenceKey("State0Parent:0.0.1:Parent:State0");
71 Mockito.doReturn(state0Key).when(axStateMock).getKey();
72 Mockito.doReturn(state0Key.getId()).when(axStateMock).getId();
74 Map<AxArtifactKey, AxStateTaskReference> taskReferences = new LinkedHashMap<>();
75 taskReferences.put(new AxArtifactKey("Task0:0.0.0"), null);
76 taskReferences.put(new AxArtifactKey("Task1:0.0.0"), null);
77 Mockito.doReturn(taskReferences).when(axStateMock).getTaskReferences();
78 Mockito.doReturn(new AxArtifactKey("Task1:0.0.0")).when(axStateMock).getDefaultTask();
80 Mockito.doReturn(taskSelectionLogicMock).when(axStateMock).getTaskSelectionLogic();
82 Mockito.doReturn(new AxArtifactKey("Context:0.0.1")).when(internalContextMock).getKey();
86 public void testTaskSelectionExecutor() {
87 DummyTaskSelectExecutor executor = new DummyTaskSelectExecutor();
89 executor.setContext(null, axStateMock, internalContextMock);
90 assertEquals("State0Parent:0.0.1:Parent:State0", executor.getKey().getId());
91 assertEquals(null, executor.getExecutionContext());
92 assertEquals(null, executor.getParent());
93 assertEquals(internalContextMock, executor.getContext());
94 assertEquals(null, executor.getNext());
95 assertEquals(null, executor.getIncoming());
96 assertEquals(null, executor.getOutgoing());
97 assertEquals(axStateMock, executor.getSubject());
99 executor.setParameters(new ExecutorParameters());
100 executor.setNext(nextExecutorMock);
101 assertEquals(nextExecutorMock, executor.getNext());
102 executor.setNext(null);
103 assertEquals(null, executor.getNext());
107 fail("test should throw an exception");
108 } catch (Exception ex) {
109 assertEquals("cleanUp() not implemented on class", ex.getMessage());
112 Mockito.doReturn(null).when(taskSelectionLogicMock).getLogic();
116 fail("test should throw an exception");
117 } catch (Exception ex) {
118 assertEquals("task selection logic cannot be null.", ex.getMessage());
121 Mockito.doReturn("some task logic").when(taskSelectionLogicMock).getLogic();
125 } catch (StateMachineException e) {
126 fail("test should not throw an exception");
130 executor.executePre(0, incomingEvent);
131 } catch (Exception ex) {
132 assertEquals("task input fields \"[InField0]\" are missing for task \"Task0:0.0.1\"", ex.getMessage());
136 executor.executePre(0, incomingEvent);
137 } catch (Exception e) {
138 fail("test should not throw an exception");
142 executor.execute(0, incomingEvent);
143 fail("test should throw an exception");
144 } catch (Exception ex) {
145 assertEquals("execute() not implemented on class", ex.getMessage());
149 executor.executePost(false);
150 fail("test should throw an exception");
151 } catch (Exception ex) {
152 assertEquals("execute-post: task selection logic failed on state \"State0Parent:0.0.1:Parent:State0\"",
156 executor.getExecutionContext().setMessage("Execution message");
158 executor.executePost(false);
159 fail("test should throw an exception");
160 } catch (Exception ex) {
161 assertEquals("execute-post: task selection logic failed on state \"State0Parent:0.0.1:Parent:State0\", "
162 + "user message: Execution message", ex.getMessage());
166 executor.executePre(0, incomingEvent);
167 } catch (Exception e) {
168 fail("test should not throw an exception");
172 executor.executePost(true);
173 assertEquals("Task1", executor.getOutgoing().getName());
174 } catch (Exception e) {
175 fail("test should not throw an exception");
179 executor.executePre(0, incomingEvent);
180 } catch (Exception e) {
181 fail("test should not throw an exception");
184 executor.getOutgoing().setName("IDontExist");
186 executor.executePost(true);
187 fail("test should throw an exception");
188 } catch (Exception ex) {
189 assertEquals("task \"IDontExist:0.0.0\" returned by task selection logic not defined "
190 + "on state \"State0Parent:0.0.1:Parent:State0\"", ex.getMessage());
194 executor.executePre(0, incomingEvent);
195 } catch (Exception e) {
196 fail("test should not throw an exception");
199 executor.getOutgoing().setName("Task0");
202 executor.executePost(true);
203 assertEquals("Task0", executor.getOutgoing().getName());
204 } catch (Exception e) {
205 fail("test should not throw an exception");