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;
28 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() {
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());
108 fail("test should throw an exception");
109 } catch (Exception ex) {
110 assertEquals("cleanUp() not implemented on class", ex.getMessage());
113 Mockito.doReturn(null).when(taskSelectionLogicMock).getLogic();
117 fail("test should throw an exception");
118 } catch (Exception ex) {
119 assertEquals("task selection logic cannot be null.", ex.getMessage());
122 Mockito.doReturn("some task logic").when(taskSelectionLogicMock).getLogic();
126 } catch (StateMachineException e) {
127 fail("test should not throw an exception");
131 executor.executePre(0, new Properties(), incomingEvent);
132 } catch (Exception ex) {
133 assertEquals("task input fields \"[InField0]\" are missing for task \"Task0:0.0.1\"", ex.getMessage());
137 executor.executePre(0, new Properties(), incomingEvent);
138 } catch (Exception e) {
139 fail("test should not throw an exception");
143 executor.execute(0, new Properties(), incomingEvent);
144 fail("test should throw an exception");
145 } catch (Exception ex) {
146 assertEquals("execute() not implemented on class", ex.getMessage());
150 executor.executePost(false);
151 fail("test should throw an exception");
152 } catch (Exception ex) {
153 assertEquals("execute-post: task selection logic failed on state \"State0Parent:0.0.1:Parent:State0\"",
157 executor.getExecutionContext().setMessage("Execution message");
159 executor.executePost(false);
160 fail("test should throw an exception");
161 } catch (Exception ex) {
162 assertEquals("execute-post: task selection logic failed on state \"State0Parent:0.0.1:Parent:State0\", "
163 + "user message: Execution message", ex.getMessage());
167 executor.executePre(0, new Properties(), incomingEvent);
168 } catch (Exception e) {
169 fail("test should not throw an exception");
173 executor.executePost(true);
174 assertEquals("Task1", executor.getOutgoing().getName());
175 } catch (Exception e) {
176 fail("test should not throw an exception");
180 executor.executePre(0, new Properties(), incomingEvent);
181 } catch (Exception e) {
182 fail("test should not throw an exception");
185 executor.getOutgoing().setName("IDontExist");
187 executor.executePost(true);
188 fail("test should throw an exception");
189 } catch (Exception ex) {
190 assertEquals("task \"IDontExist:0.0.0\" returned by task selection logic not defined "
191 + "on state \"State0Parent:0.0.1:Parent:State0\"", ex.getMessage());
195 executor.executePre(0, new Properties(), incomingEvent);
196 } catch (Exception e) {
197 fail("test should not throw an exception");
200 executor.getOutgoing().setName("Task0");
203 executor.executePost(true);
204 assertEquals("Task0", executor.getOutgoing().getName());
205 } catch (Exception e) {
206 fail("test should not throw an exception");
210 executor.executePre(0, null, incomingEvent);
211 fail("test should throw an exception");
212 } catch (Exception ex) {
213 assertEquals("executionProperties is marked @NonNull but is null", ex.getMessage());