613d1ae01aa1344ab8790cb6084a6c58ea598f89
[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
27 import java.util.LinkedHashMap;
28 import java.util.Map;
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;
44
45 /**
46  * Test task executor.
47  */
48 public class TaskSelectExecutorTest {
49     @Mock
50     private AxState axStateMock;
51
52     @Mock
53     private ApexInternalContext internalContextMock;
54
55     @Mock
56     private Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> nextExecutorMock;
57
58     @Mock
59     private AxTaskSelectionLogic taskSelectionLogicMock;
60
61     @Mock
62     private EnEvent incomingEvent;
63
64     /**
65      * Set up mocking.
66      */
67     @Before
68     public void startMocking() {
69         MockitoAnnotations.initMocks(this);
70
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();
74
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();
80
81         Mockito.doReturn(taskSelectionLogicMock).when(axStateMock).getTaskSelectionLogic();
82
83         Mockito.doReturn(new AxArtifactKey("Context:0.0.1")).when(internalContextMock).getKey();
84     }
85
86     @Test
87     public void testTaskSelectionExecutor() throws StateMachineException {
88         DummyTaskSelectExecutor executor = new DummyTaskSelectExecutor();
89
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());
99
100         executor.setParameters(new ExecutorParameters());
101         executor.setNext(nextExecutorMock);
102         assertEquals(nextExecutorMock, executor.getNext());
103         executor.setNext(null);
104         assertEquals(null, executor.getNext());
105
106         assertThatThrownBy(executor::cleanUp)
107             .hasMessage("cleanUp() not implemented on class");
108         Mockito.doReturn(null).when(taskSelectionLogicMock).getLogic();
109
110         assertThatThrownBy(executor::prepare)
111             .hasMessage("task selection logic cannot be null.");
112         Mockito.doReturn("some task logic").when(taskSelectionLogicMock).getLogic();
113
114         executor.prepare();
115
116         executor.executePre(0, new Properties(), incomingEvent);
117
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\"");
122
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);
128
129         executor.executePost(true);
130         assertEquals("Task1", executor.getOutgoing().getName());
131
132         executor.executePre(0, new Properties(), incomingEvent);
133
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);
139
140         executor.getOutgoing().setName("Task0");
141
142         executor.executePost(true);
143         assertEquals("Task0", executor.getOutgoing().getName());
144
145         assertThatThrownBy(() -> executor.executePre(0, null, incomingEvent))
146             .hasMessageMatching("^executionProperties is marked .*on.*ull but is null$");
147     }
148 }