edfbcf20f917749ad1dc34b0a144b1ea29d43360
[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 import static org.junit.Assert.fail;
27
28 import java.util.LinkedHashMap;
29 import java.util.Map;
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;
45
46 /**
47  * Test task executor.
48  */
49 public class TaskSelectExecutorTest {
50     @Mock
51     private AxState axStateMock;
52
53     @Mock
54     private ApexInternalContext internalContextMock;
55
56     @Mock
57     private Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> nextExecutorMock;
58
59     @Mock
60     private AxTaskSelectionLogic taskSelectionLogicMock;
61
62     @Mock
63     private EnEvent incomingEvent;
64
65     /**
66      * Set up mocking.
67      */
68     @Before
69     public void startMocking() {
70         MockitoAnnotations.initMocks(this);
71
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();
75
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();
81
82         Mockito.doReturn(taskSelectionLogicMock).when(axStateMock).getTaskSelectionLogic();
83
84         Mockito.doReturn(new AxArtifactKey("Context:0.0.1")).when(internalContextMock).getKey();
85     }
86
87     @Test
88     public void testTaskSelectionExecutor() {
89         DummyTaskSelectExecutor executor = new DummyTaskSelectExecutor();
90
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());
100
101         executor.setParameters(new ExecutorParameters());
102         executor.setNext(nextExecutorMock);
103         assertEquals(nextExecutorMock, executor.getNext());
104         executor.setNext(null);
105         assertEquals(null, executor.getNext());
106
107         try {
108             executor.cleanUp();
109             fail("test should throw an exception");
110         } catch (Exception ex) {
111             assertEquals("cleanUp() not implemented on class", ex.getMessage());
112         }
113
114         Mockito.doReturn(null).when(taskSelectionLogicMock).getLogic();
115
116         try {
117             executor.prepare();
118             fail("test should throw an exception");
119         } catch (Exception ex) {
120             assertEquals("task selection logic cannot be null.", ex.getMessage());
121         }
122
123         Mockito.doReturn("some task logic").when(taskSelectionLogicMock).getLogic();
124
125         try {
126             executor.prepare();
127         } catch (StateMachineException e) {
128             fail("test should not throw an exception");
129         }
130
131         try {
132             executor.executePre(0, new Properties(), incomingEvent);
133         } catch (Exception e) {
134             fail("test should not throw an exception");
135         }
136
137         try {
138             executor.execute(0, new Properties(), incomingEvent);
139             fail("test should throw an exception");
140         } catch (Exception ex) {
141             assertEquals("execute() not implemented on class", ex.getMessage());
142         }
143
144         try {
145             executor.executePost(false);
146             fail("test should throw an exception");
147         } catch (Exception ex) {
148             assertEquals("execute-post: task selection logic failed on state \"State0Parent:0.0.1:Parent:State0\"",
149                 ex.getMessage());
150         }
151
152         executor.getExecutionContext().setMessage("Execution message");
153         try {
154             executor.executePost(false);
155             fail("test should throw an exception");
156         } catch (Exception ex) {
157             assertEquals("execute-post: task selection logic failed on state \"State0Parent:0.0.1:Parent:State0\", "
158                 + "user message: Execution message", ex.getMessage());
159         }
160
161         try {
162             executor.executePre(0, new Properties(), incomingEvent);
163         } catch (Exception e) {
164             fail("test should not throw an exception");
165         }
166
167         try {
168             executor.executePost(true);
169             assertEquals("Task1", executor.getOutgoing().getName());
170         } catch (Exception e) {
171             fail("test should not throw an exception");
172         }
173
174         try {
175             executor.executePre(0, new Properties(), incomingEvent);
176         } catch (Exception e) {
177             fail("test should not throw an exception");
178         }
179
180         executor.getOutgoing().setName("IDontExist");
181         try {
182             executor.executePost(true);
183             fail("test should throw an exception");
184         } catch (Exception ex) {
185             assertEquals("task \"IDontExist:0.0.0\" returned by task selection logic not defined "
186                 + "on state \"State0Parent:0.0.1:Parent:State0\"", ex.getMessage());
187         }
188
189         try {
190             executor.executePre(0, new Properties(), incomingEvent);
191         } catch (Exception e) {
192             fail("test should not throw an exception");
193         }
194
195         executor.getOutgoing().setName("Task0");
196
197         try {
198             executor.executePost(true);
199             assertEquals("Task0", executor.getOutgoing().getName());
200         } catch (Exception e) {
201             fail("test should not throw an exception");
202         }
203
204         assertThatThrownBy(() -> executor.executePre(0, null, incomingEvent))
205             .hasMessageMatching("^executionProperties is marked .*on.*ull but is null$");
206     }
207 }