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.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
29 import java.util.LinkedHashMap;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.policy.apex.context.parameters.SchemaParameters;
38 import org.onap.policy.apex.core.engine.ExecutorParameters;
39 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
40 import org.onap.policy.apex.core.engine.event.EnEvent;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
43 import org.onap.policy.apex.model.basicmodel.service.ModelService;
44 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
45 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
46 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
47 import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
48 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
49 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
50 import org.onap.policy.apex.model.policymodel.concepts.AxState;
51 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
52 import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput;
53 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskOutputType;
54 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference;
55 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
56 import org.onap.policy.apex.model.policymodel.concepts.AxTasks;
57 import org.onap.policy.common.parameters.ParameterService;
62 public class StateMachineExecutorTest {
64 private ApexInternalContext internalContextMock;
67 private Executor<EnEvent, EnEvent, AxPolicy, ApexInternalContext> nextExecutorMock;
70 private ExecutorFactory executorFactoryMock;
73 private EnEvent incomingEventMock;
75 private AxPolicy axPolicy = new AxPolicy();
77 private DummyTaskSelectExecutor dummyTsle;
79 private DummyStateFinalizerExecutor dummySfle;
85 public void startMocking() {
86 MockitoAnnotations.initMocks(this);
88 axPolicy.setKey(new AxArtifactKey("Policy:0.0.1"));
90 AxReferenceKey state0Key = new AxReferenceKey(axPolicy.getKey(), "state0");
91 AxState state0 = new AxState(state0Key);
93 AxReferenceKey state1Key = new AxReferenceKey(axPolicy.getKey(), "state1");
94 AxState state1 = new AxState(state1Key);
96 axPolicy.getStateMap().put("State0", state0);
97 axPolicy.getStateMap().put("State1", state1);
98 axPolicy.setFirstState("state0");
100 AxArtifactKey event0Key = new AxArtifactKey("Event0:0.0.1");
101 AxEvent event0 = new AxEvent(event0Key, "a.name.space", "source", "target");
102 AxArtifactKey event1Key = new AxArtifactKey("Event1:0.0.1");
103 AxEvent event1 = new AxEvent(event1Key, "a.name.space", "source", "target");
104 AxArtifactKey event2Key = new AxArtifactKey("Event2:0.0.1");
105 AxEvent event2 = new AxEvent(event2Key, "a.name.space", "source", "target");
106 AxEvents events = new AxEvents();
107 events.getEventMap().put(event0Key, event0);
108 events.getEventMap().put(event1Key, event1);
109 events.getEventMap().put(event2Key, event2);
110 ModelService.registerModel(AxEvents.class, events);
112 AxReferenceKey fieldKey = new AxReferenceKey("Event1:0.0.1:event:Field0");
113 AxArtifactKey stringSchemaKey = new AxArtifactKey("StringSchema:0.0.1");
114 AxContextSchema stringSchema = new AxContextSchema(stringSchemaKey, "Java", "java.lang.String");
115 AxContextSchemas schemas = new AxContextSchemas();
116 schemas.getSchemasMap().put(stringSchemaKey, stringSchema);
117 ModelService.registerModel(AxContextSchemas.class, schemas);
119 AxField event1Field0Definition = new AxField(fieldKey, stringSchemaKey);
120 event1.getParameterMap().put("Event1Field0", event1Field0Definition);
122 event0.getParameterMap().put("Event1Field0", event1Field0Definition);
123 event0.getParameterMap().put("UnusedField", event1Field0Definition);
125 Mockito.doReturn(event0Key).when(incomingEventMock).getKey();
126 Mockito.doReturn(event0).when(incomingEventMock).getAxEvent();
128 state0.setTrigger(event0Key);
129 state1.setTrigger(event1Key);
131 AxArtifactKey task0Key = new AxArtifactKey("task0:0.0.1");
132 AxTask task0 = new AxTask(task0Key);
134 AxArtifactKey task1Key = new AxArtifactKey("task1:0.0.1");
135 AxTask task1 = new AxTask(task1Key);
137 AxTasks tasks = new AxTasks();
138 tasks.getTaskMap().put(task0Key, task0);
139 tasks.getTaskMap().put(task1Key, task1);
140 ModelService.registerModel(AxTasks.class, tasks);
142 ParameterService.register(new SchemaParameters());
144 AxReferenceKey stateOutput0Key = new AxReferenceKey("Policy:0.0.1:state0:stateOutput0");
145 AxStateOutput stateOutput0 = new AxStateOutput(stateOutput0Key, event1Key, state1.getKey());
147 state0.getStateOutputs().put(stateOutput0Key.getLocalName(), stateOutput0);
149 AxReferenceKey stateOutput1Key = new AxReferenceKey("Policy:0.0.1:state0:stateOutput1");
150 AxStateOutput stateOutput1 = new AxStateOutput(stateOutput1Key, event2Key, AxReferenceKey.getNullKey());
152 state1.getStateOutputs().put(stateOutput1Key.getLocalName(), stateOutput1);
154 AxReferenceKey str0Key = new AxReferenceKey("Policy:0.0.1:state0:str0");
155 AxStateTaskReference str0 = new AxStateTaskReference(str0Key, AxStateTaskOutputType.DIRECT, stateOutput0Key);
156 state0.getTaskReferences().put(task0Key, str0);
158 AxReferenceKey sflKey = new AxReferenceKey("Policy:0.0.1:state1:sfl");
159 AxStateFinalizerLogic sfl = new AxStateFinalizerLogic(sflKey, "Java", "State fianlizer logic");
160 state1.getStateFinalizerLogicMap().put("sfl", sfl);
162 AxReferenceKey str1Key = new AxReferenceKey("Policy:0.0.1:state1:str1");
163 AxStateTaskReference str1 = new AxStateTaskReference(str1Key, AxStateTaskOutputType.LOGIC, sflKey);
164 state1.getTaskReferences().put(task1Key, str1);
166 Mockito.doReturn(new DummyTaskExecutor(true)).when(executorFactoryMock).getTaskExecutor(Mockito.anyObject(),
167 Mockito.anyObject(), Mockito.anyObject());
169 dummyTsle = new DummyTaskSelectExecutor(true);
170 Mockito.doReturn(dummyTsle).when(executorFactoryMock).getTaskSelectionExecutor(Mockito.anyObject(),
171 Mockito.anyObject(), Mockito.anyObject());
173 dummySfle = new DummyStateFinalizerExecutor(true);
174 Mockito.doReturn(dummySfle).when(executorFactoryMock).getStateFinalizerExecutor(Mockito.anyObject(),
175 Mockito.anyObject(), Mockito.anyObject());
179 public void cleardown() {
180 ParameterService.clear();
181 ModelService.clear();
185 public void testStateMachineExecutor() {
186 StateMachineExecutor executor =
187 new StateMachineExecutor(executorFactoryMock, new AxArtifactKey("OwnerKey:0.0.1"));
190 executor.execute(0, null, incomingEventMock);
191 fail("test should throw an exception");
192 } catch (Exception ex) {
193 assertEquals("no states defined on state machine", ex.getMessage());
196 executor.setContext(null, axPolicy, internalContextMock);
197 assertEquals("Policy:0.0.1", executor.getKey().getId());
198 assertEquals(null, executor.getParent());
199 assertEquals(internalContextMock, executor.getContext());
200 assertEquals(null, executor.getNext());
201 assertEquals(null, executor.getIncoming());
202 assertEquals(null, executor.getOutgoing());
203 assertEquals(axPolicy, executor.getSubject());
205 executor.setParameters(new ExecutorParameters());
206 executor.setNext(nextExecutorMock);
207 assertEquals(nextExecutorMock, executor.getNext());
208 executor.setNext(null);
209 assertEquals(null, executor.getNext());
212 executor.executePre(0, null, null);
213 fail("test should throw an exception");
214 } catch (Exception ex) {
215 assertEquals("execution pre work not implemented on class", ex.getMessage());
219 executor.executePost(false);
220 fail("test should throw an exception");
221 } catch (Exception ex) {
222 assertEquals("execution post work not implemented on class", ex.getMessage());
227 fail("test should throw an exception");
228 } catch (Exception e) {
229 assertTrue(e instanceof NullPointerException);
232 axPolicy.setFirstState("BadState");
233 executor.setContext(null, axPolicy, internalContextMock);
235 executor.execute(0, null, incomingEventMock);
236 fail("test should throw an exception");
237 } catch (Exception ex) {
238 assertEquals("first state not defined on state machine", ex.getMessage());
241 axPolicy.setFirstState("state0");
242 executor.setContext(null, axPolicy, internalContextMock);
244 executor.execute(0, null, incomingEventMock);
245 } catch (Exception ex) {
246 fail("test should not throw an exception");
249 dummyTsle.setTaskNo(0);
251 executor.execute(0, null, incomingEventMock);
252 } catch (Exception ex) {
253 fail("test should not throw an exception");
256 AxReferenceKey badStateKey = new AxReferenceKey("Policy:0.0.1:PName:BadState");
257 axPolicy.getStateMap().get("State1").getStateOutputs().get("stateOutput1").setNextState(badStateKey);
258 dummyTsle.setTaskNo(0);
260 executor.execute(0, null, incomingEventMock);
261 fail("test should throw an exception");
262 } catch (Exception ex) {
263 assertEquals("state execution failed, next state \"Policy:0.0.1:PName:BadState\" not found",
267 axPolicy.getStateMap().get("State1").getStateOutputs().get("stateOutput1")
268 .setNextState(AxReferenceKey.getNullKey());
269 dummyTsle.setTaskNo(0);
271 executor.execute(0, null, incomingEventMock);
272 } catch (Exception ex) {
273 fail("test should not throw an exception");
276 axPolicy.getStateMap().get("State1").setTrigger(new AxArtifactKey("BadTrigger:0.0.1"));
277 dummyTsle.setTaskNo(0);
279 executor.execute(0, null, incomingEventMock);
280 fail("test should throw an exception");
281 } catch (Exception ex) {
282 assertEquals("incoming event \"Event1:0.0.1\" does not match trigger \"BadTrigger:0.0.1\" "
283 + "of state \"Policy:0.0.1:NULL:state1\"", ex.getMessage());
286 axPolicy.getStateMap().get("State1").setTrigger(new AxArtifactKey("Event1:0.0.1"));
287 dummyTsle.setTaskNo(0);
289 executor.execute(0, null, incomingEventMock);
290 } catch (Exception ex) {
291 fail("test should not throw an exception");
294 AxStateFinalizerLogic savedSfl = axPolicy.getStateMap().get("State1").getStateFinalizerLogicMap().get("sfl");
295 axPolicy.getStateMap().get("State1").getStateFinalizerLogicMap().put("sfl", null);
297 executor.setContext(null, axPolicy, internalContextMock);
298 fail("test should throw an exception");
299 } catch (Exception ex) {
300 assertEquals("state finalizer logic on task reference "
301 + "\"AxStateTaskReference:(stateKey=AxReferenceKey:(parentKeyName=Policy,"
302 + "parentKeyVersion=0.0.1,parentLocalName=state1,localName=str1),"
303 + "outputType=LOGIC,output=AxReferenceKey:(parentKeyName=Policy,parentKeyVersion=0.0.1,"
304 + "parentLocalName=state1,localName=sfl))\" on state \"Policy:0.0.1:NULL:state1\" " + "does not exist",
308 axPolicy.getStateMap().get("State1").getStateFinalizerLogicMap().put("sfl", savedSfl);
309 executor.setContext(null, axPolicy, internalContextMock);
311 dummyTsle.setTaskNo(0);
313 executor.execute(0, null, incomingEventMock);
314 } catch (Exception ex) {
315 fail("test should not throw an exception");
318 AxArtifactKey task1Key = new AxArtifactKey("task1:0.0.1");
320 axPolicy.getStateMap().get("State1").getTaskReferences().get(task1Key)
321 .setStateTaskOutputType(AxStateTaskOutputType.UNDEFINED);
322 executor.setContext(null, axPolicy, internalContextMock);
323 fail("test should throw an exception");
324 } catch (Exception ex) {
325 assertEquals("invalid state output type on task reference \"AxStateTaskReference:(stateKey=AxReferenceKey:"
326 + "(parentKeyName=Policy,parentKeyVersion=0.0.1,parentLocalName=state1,localName=str1),"
327 + "outputType=UNDEFINED,output=AxReferenceKey:(parentKeyName=Policy,"
328 + "parentKeyVersion=0.0.1,parentLocalName=state1,localName=sfl))\" "
329 + "on state \"Policy:0.0.1:NULL:state1\"", ex.getMessage());
332 axPolicy.getStateMap().get("State1").getTaskReferences().get(task1Key)
333 .setStateTaskOutputType(AxStateTaskOutputType.LOGIC);
334 executor.setContext(null, axPolicy, internalContextMock);
336 dummyTsle.setTaskNo(0);
338 executor.execute(0, null, incomingEventMock);
339 } catch (Exception ex) {
340 fail("test should not throw an exception");
343 dummyTsle.setTaskNo(0);
344 dummySfle.setReturnBad(true);
346 executor.execute(0, null, incomingEventMock);
347 fail("test should throw an exception");
348 } catch (Exception ex) {
349 assertEquals("State execution of state \"Policy:0.0.1:NULL:state1\" on task \"task1:0.0.1\" failed: "
350 + "state output definition for state output \"stateOutputBad\" not found for "
351 + "state \"Policy:0.0.1:NULL:state1\"", ex.getMessage());
354 dummyTsle.setTaskNo(0);
355 dummySfle.setReturnBad(false);
357 executor.execute(0, null, incomingEventMock);
358 } catch (Exception ex) {
359 fail("test should not throw an exception");
364 fail("test should throw an exception");
365 } catch (Exception ex) {
366 assertEquals("cleanUp() not implemented on class", ex.getMessage());
371 public void testStateOutput() {
373 new StateOutput(axPolicy.getStateMap().get("State0").getStateOutputs().get("stateOutput0"));
374 assertNotNull(output);
376 assertEquals("stateOutput0", output.getStateOutputDefinition().getKey().getLocalName());
379 output.setEventFields(null, null);
380 fail("test should throw an exception");
381 } catch (Exception ex) {
382 assertEquals("incomingFieldDefinitionMap may not be null", ex.getMessage());
385 Map<String, AxField> incomingFieldDefinitionMap = new LinkedHashMap<>();
387 output.setEventFields(incomingFieldDefinitionMap, null);
388 fail("test should throw an exception");
389 } catch (Exception ex) {
390 assertEquals("eventFieldMap may not be null", ex.getMessage());
393 Map<String, Object> eventFieldMap = new LinkedHashMap<>();
395 output.setEventFields(incomingFieldDefinitionMap, eventFieldMap);
396 } catch (Exception ex) {
397 fail("test should not throw an exception");
400 eventFieldMap.put("key", "Value");
402 output.setEventFields(incomingFieldDefinitionMap, eventFieldMap);
403 fail("test should throw an exception");
404 } catch (Exception ex) {
405 assertEquals("field definitions and values do not match for event Event1:0.0.1\n[]\n[key]",
409 AxField axBadFieldDefinition = new AxField();
410 incomingFieldDefinitionMap.put("key", axBadFieldDefinition);
412 output.setEventFields(incomingFieldDefinitionMap, eventFieldMap);
413 fail("test should throw an exception");
414 } catch (Exception ex) {
415 assertEquals("field \"key\" does not exist on event \"Event1:0.0.1\"", ex.getMessage());
418 incomingFieldDefinitionMap.clear();
419 eventFieldMap.clear();
420 AxArtifactKey stringSchemaKey = new AxArtifactKey("StringSchema:0.0.1");
421 AxReferenceKey fieldKey = new AxReferenceKey("Event1:0.0.1:event:Field0");
422 AxField event1Field0Definition = new AxField(fieldKey, stringSchemaKey);
423 incomingFieldDefinitionMap.put("Event1Field0", event1Field0Definition);
424 eventFieldMap.put("Event1Field0", "Value");
426 output.setEventFields(incomingFieldDefinitionMap, eventFieldMap);
427 } catch (Exception ex) {
428 fail("test should not throw an exception");
431 output = new StateOutput(axPolicy.getStateMap().get("State0").getStateOutputs().get("stateOutput0"));
433 EnEvent incomingEvent = new EnEvent(new AxArtifactKey("Event0:0.0.1"));
434 output.copyUnsetFields(incomingEvent);
436 incomingEvent.put("Event1Field0", "Hello");
437 output.copyUnsetFields(incomingEvent);