aboutsummaryrefslogtreecommitdiffstats
path: root/core/src/test/java/org/onap/policy/apex/core/engine/executor/StateMachineExecutorTest.java
blob: 14cc89ee88b1e89e92b62643d52e1ad5684c2a2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020, 2023 Nordix Foundation.
 *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
 *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.core.engine.executor;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.policy.apex.context.ContextException;
import org.onap.policy.apex.context.parameters.SchemaParameters;
import org.onap.policy.apex.core.engine.ExecutorParameters;
import org.onap.policy.apex.core.engine.context.ApexInternalContext;
import org.onap.policy.apex.core.engine.event.EnEvent;
import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
import org.onap.policy.apex.model.basicmodel.service.ModelService;
import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
import org.onap.policy.apex.model.eventmodel.concepts.AxField;
import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
import org.onap.policy.apex.model.policymodel.concepts.AxState;
import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput;
import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskOutputType;
import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference;
import org.onap.policy.apex.model.policymodel.concepts.AxTask;
import org.onap.policy.apex.model.policymodel.concepts.AxTasks;
import org.onap.policy.common.parameters.ParameterService;

/**
 * Test task executor.
 */
@RunWith(MockitoJUnitRunner.class)
public class StateMachineExecutorTest {
    @Mock
    private ApexInternalContext internalContextMock;

    @Mock
    private Executor<EnEvent, Collection<EnEvent>, AxPolicy, ApexInternalContext> nextExecutorMock;

    @Mock
    private ExecutorFactory executorFactoryMock;

    @Mock
    private EnEvent incomingEventMock;

    private AxPolicy axPolicy = new AxPolicy();

    private DummyTaskSelectExecutor dummyTsle;

    private DummyStateFinalizerExecutor dummySfle;

    /**
     * Set up mocking.
     */
    @Before
    public void startMocking() {
        axPolicy.setKey(new AxArtifactKey("Policy:0.0.1"));

        AxReferenceKey state0Key = new AxReferenceKey(axPolicy.getKey(), "state0");
        AxState state0 = new AxState(state0Key);

        AxReferenceKey state1Key = new AxReferenceKey(axPolicy.getKey(), "state1");
        AxState state1 = new AxState(state1Key);

        axPolicy.getStateMap().put("State0", state0);
        axPolicy.getStateMap().put("State1", state1);
        axPolicy.setFirstState("state0");

        AxArtifactKey event0Key = new AxArtifactKey("Event0:0.0.1");
        AxEvent event0 = new AxEvent(event0Key, "a.name.space", "source", "target");
        AxArtifactKey event1Key = new AxArtifactKey("Event1:0.0.1");
        AxEvent event1 = new AxEvent(event1Key, "a.name.space", "source", "target");
        AxArtifactKey event2Key = new AxArtifactKey("Event2:0.0.1");
        AxEvent event2 = new AxEvent(event2Key, "a.name.space", "source", "target");
        AxEvents events = new AxEvents();
        events.getEventMap().put(event0Key, event0);
        events.getEventMap().put(event1Key, event1);
        events.getEventMap().put(event2Key, event2);
        ModelService.registerModel(AxEvents.class, events);

        AxReferenceKey fieldKey = new AxReferenceKey("Event1:0.0.1:event:Field0");
        AxArtifactKey stringSchemaKey = new AxArtifactKey("StringSchema:0.0.1");
        AxContextSchema stringSchema = new AxContextSchema(stringSchemaKey, "Java", "java.lang.String");
        AxContextSchemas schemas = new AxContextSchemas();
        schemas.getSchemasMap().put(stringSchemaKey, stringSchema);
        ModelService.registerModel(AxContextSchemas.class, schemas);

        AxField event1Field0Definition = new AxField(fieldKey, stringSchemaKey);
        event1.getParameterMap().put("Event1Field0", event1Field0Definition);

        event0.getParameterMap().put("Event1Field0", event1Field0Definition);
        event0.getParameterMap().put("UnusedField", event1Field0Definition);

        Mockito.doReturn(event0Key).when(incomingEventMock).getKey();
        Mockito.doReturn(event0).when(incomingEventMock).getAxEvent();

        state0.setTrigger(event0Key);
        state1.setTrigger(event1Key);

        AxArtifactKey task0Key = new AxArtifactKey("task0:0.0.1");
        AxTask task0 = new AxTask(task0Key);

        AxArtifactKey task1Key = new AxArtifactKey("task1:0.0.1");
        AxTask task1 = new AxTask(task1Key);

        AxTasks tasks = new AxTasks();
        tasks.getTaskMap().put(task0Key, task0);
        tasks.getTaskMap().put(task1Key, task1);
        ModelService.registerModel(AxTasks.class, tasks);

        ParameterService.register(new SchemaParameters());

        AxReferenceKey stateOutput0Key = new AxReferenceKey("Policy:0.0.1:state0:stateOutput0");
        AxStateOutput stateOutput0 = new AxStateOutput(stateOutput0Key, event1Key, state1.getKey());

        state0.getStateOutputs().put(stateOutput0Key.getLocalName(), stateOutput0);

        AxReferenceKey stateOutput1Key = new AxReferenceKey("Policy:0.0.1:state0:stateOutput1");
        AxStateOutput stateOutput1 = new AxStateOutput(stateOutput1Key, event2Key, AxReferenceKey.getNullKey());

        state1.getStateOutputs().put(stateOutput1Key.getLocalName(), stateOutput1);

        AxReferenceKey str0Key = new AxReferenceKey("Policy:0.0.1:state0:str0");
        AxStateTaskReference str0 = new AxStateTaskReference(str0Key, AxStateTaskOutputType.DIRECT, stateOutput0Key);
        state0.getTaskReferences().put(task0Key, str0);

        AxReferenceKey sflKey = new AxReferenceKey("Policy:0.0.1:state1:sfl");
        AxStateFinalizerLogic sfl = new AxStateFinalizerLogic(sflKey, "Java", "State fianlizer logic");
        state1.getStateFinalizerLogicMap().put("sfl", sfl);

        AxReferenceKey str1Key = new AxReferenceKey("Policy:0.0.1:state1:str1");
        AxStateTaskReference str1 = new AxStateTaskReference(str1Key, AxStateTaskOutputType.LOGIC, sflKey);
        state1.getTaskReferences().put(task1Key, str1);

        Mockito.doReturn(new DummyTaskExecutor(true)).when(executorFactoryMock).getTaskExecutor(Mockito.any(),
            Mockito.any(), Mockito.any());

        dummyTsle = new DummyTaskSelectExecutor(true);
        Mockito.doReturn(dummyTsle).when(executorFactoryMock).getTaskSelectionExecutor(Mockito.any(),
            Mockito.any(), Mockito.any());

        dummySfle = new DummyStateFinalizerExecutor(true);
        Mockito.doReturn(dummySfle).when(executorFactoryMock).getStateFinalizerExecutor(Mockito.any(),
            Mockito.any(), Mockito.any());
    }

    @After
    public void cleardown() {
        ParameterService.clear();
        ModelService.clear();
    }

    @Test
    public void testStateMachineExecutor() throws StateMachineException, ContextException {
        StateMachineExecutor executor =
            new StateMachineExecutor(executorFactoryMock, new AxArtifactKey("OwnerKey:0.0.1"));

        assertThatThrownBy(() -> executor.execute(0, null, incomingEventMock))
            .hasMessage("no states defined on state machine");
        executor.setContext(null, axPolicy, internalContextMock);
        assertEquals("Policy:0.0.1", executor.getKey().getId());
        assertNull(executor.getParent());
        assertEquals(internalContextMock, executor.getContext());
        assertNull(executor.getNext());
        assertNull(executor.getIncoming());
        assertTrue(executor.getOutgoing().isEmpty());
        assertEquals(axPolicy, executor.getSubject());

        executor.setParameters(new ExecutorParameters());
        executor.setNext(nextExecutorMock);
        assertEquals(nextExecutorMock, executor.getNext());
        executor.setNext(null);
        assertNull(executor.getNext());

        assertThatThrownBy(() -> executor.executePre(0, null, null))
            .hasMessage("execution pre work not implemented on class");
        assertThatThrownBy(() -> executor.executePost(false))
            .hasMessage("execution post work not implemented on class");
        assertThatThrownBy(executor::prepare)
            .isInstanceOf(NullPointerException.class);
        axPolicy.setFirstState("BadState");
        executor.setContext(null, axPolicy, internalContextMock);
        assertThatThrownBy(() -> executor.execute(0, null, incomingEventMock))
            .hasMessage("first state not defined on state machine");
        axPolicy.setFirstState("state0");
        executor.setContext(null, axPolicy, internalContextMock);
        executor.execute(0, null, incomingEventMock);

        DummyTaskSelectExecutor.setTaskNo(0);
        executor.execute(0, null, incomingEventMock);

        AxReferenceKey badStateKey = new AxReferenceKey("Policy:0.0.1:PName:BadState");
        axPolicy.getStateMap().get("State1").getStateOutputs().get("stateOutput1").setNextState(badStateKey);
        DummyTaskSelectExecutor.setTaskNo(0);
        assertThatThrownBy(() -> executor.execute(0, null, incomingEventMock))
            .hasMessage("state execution failed, next state \"Policy:0.0.1:PName:BadState\" not found");
        axPolicy.getStateMap().get("State1").getStateOutputs().get("stateOutput1")
            .setNextState(AxReferenceKey.getNullKey());
        DummyTaskSelectExecutor.setTaskNo(0);
        executor.execute(0, null, incomingEventMock);

        axPolicy.getStateMap().get("State1").setTrigger(new AxArtifactKey("BadTrigger:0.0.1"));
        DummyTaskSelectExecutor.setTaskNo(0);
        assertThatThrownBy(() -> executor.execute(0, null, incomingEventMock))
            .hasMessage("incoming event \"Event1:0.0.1\" does not match trigger \"BadTrigger:0.0.1\" "
                + "of state \"Policy:0.0.1:NULL:state1\"");
        axPolicy.getStateMap().get("State1").setTrigger(new AxArtifactKey("Event1:0.0.1"));
        DummyTaskSelectExecutor.setTaskNo(0);
        executor.execute(0, null, incomingEventMock);

        AxStateFinalizerLogic savedSfl = axPolicy.getStateMap().get("State1").getStateFinalizerLogicMap().get("sfl");
        axPolicy.getStateMap().get("State1").getStateFinalizerLogicMap().put("sfl", null);
        assertThatThrownBy(() -> executor.setContext(null, axPolicy, internalContextMock))
            .hasMessage("state finalizer logic on task reference "
                + "\"AxStateTaskReference:(stateKey=AxReferenceKey:(parentKeyName=Policy,"
                + "parentKeyVersion=0.0.1,parentLocalName=state1,localName=str1),"
                + "outputType=LOGIC,output=AxReferenceKey:(parentKeyName=Policy,parentKeyVersion=0.0.1,"
                + "parentLocalName=state1,localName=sfl))\" on state \"Policy:0.0.1:NULL:state1\" " + "does not exist");
        axPolicy.getStateMap().get("State1").getStateFinalizerLogicMap().put("sfl", savedSfl);
        executor.setContext(null, axPolicy, internalContextMock);

        DummyTaskSelectExecutor.setTaskNo(0);
        executor.execute(0, null, incomingEventMock);

        AxArtifactKey task1Key = new AxArtifactKey("task1:0.0.1");
        axPolicy.getStateMap().get("State1").getTaskReferences().get(task1Key)
            .setStateTaskOutputType(AxStateTaskOutputType.UNDEFINED);
        assertThatThrownBy(() -> executor.setContext(null, axPolicy, internalContextMock))
            .hasMessage("invalid state output type on task reference \"AxStateTaskReference:(stateKey"
                + "=AxReferenceKey:(parentKeyName=Policy,parentKeyVersion=0.0.1,parentLocalName=state1,localName=str1),"
                + "outputType=UNDEFINED,output=AxReferenceKey:(parentKeyName=Policy,"
                + "parentKeyVersion=0.0.1,parentLocalName=state1,localName=sfl))\" "
                + "on state \"Policy:0.0.1:NULL:state1\"");
        axPolicy.getStateMap().get("State1").getTaskReferences().get(task1Key)
            .setStateTaskOutputType(AxStateTaskOutputType.LOGIC);
        executor.setContext(null, axPolicy, internalContextMock);

        DummyTaskSelectExecutor.setTaskNo(0);
        executor.execute(0, null, incomingEventMock);

        DummyTaskSelectExecutor.setTaskNo(0);
        dummySfle.setReturnBad(true);
        assertThatThrownBy(() -> executor.execute(0, null, incomingEventMock))
            .hasMessage("State execution of state \"Policy:0.0.1:NULL:state1\" on task \"task1:0.0.1\""
                + " failed: state output definition for state output \"stateOutputBad\" not found for "
                + "state \"Policy:0.0.1:NULL:state1\"");
        DummyTaskSelectExecutor.setTaskNo(0);
        dummySfle.setReturnBad(false);
        executor.execute(0, null, incomingEventMock);

        assertThatThrownBy(executor::cleanUp)
            .hasMessage("cleanUp() not implemented on class");
    }

    @Test
    public void testStateOutput() throws StateMachineException {
        final StateOutput output =
            new StateOutput(axPolicy.getStateMap().get("State0").getStateOutputs().get("stateOutput0"));
        assertNotNull(output);

        assertEquals("stateOutput0", output.getStateOutputDefinition().getKey().getLocalName());

        assertThatThrownBy(() -> output.setEventFields(null, null))
            .hasMessage("incomingFieldDefinitionMap may not be null");
        Map<String, AxEvent> incomingFieldDefinitionMap = new LinkedHashMap<>();
        assertThatThrownBy(() -> output.setEventFields(incomingFieldDefinitionMap, null))
            .hasMessage("eventFieldMaps may not be null");
        Map<String, Map<String, Object>> eventFieldMaps = new LinkedHashMap<>();
        output.setEventFields(incomingFieldDefinitionMap, eventFieldMaps);
        AxEvent event = new AxEvent(new AxArtifactKey("Event1", "0.0.1"));
        event.setParameterMap(Map.of("key", new AxField()));
        incomingFieldDefinitionMap.put("Event1", event);
        eventFieldMaps.put("Event1", Map.of("key2", "value"));
        assertThatThrownBy(() -> output.setEventFields(incomingFieldDefinitionMap, eventFieldMaps))
            .hasMessage("field definitions and values do not match for event Event1:0.0.1\n[key]\n[key2]");

        eventFieldMaps.put("Event1", Map.of("key", "value"));
        assertThatThrownBy(() -> output.setEventFields(incomingFieldDefinitionMap, eventFieldMaps))
            .hasMessage("field \"key\" does not exist on event \"Event1:0.0.1\"");

        incomingFieldDefinitionMap.clear();
        eventFieldMaps.clear();
        AxArtifactKey stringSchemaKey = new AxArtifactKey("StringSchema:0.0.1");
        AxReferenceKey fieldKey = new AxReferenceKey("Event1:0.0.1:event:Field0");
        AxField event1Field0Definition = new AxField(fieldKey, stringSchemaKey);
        event.setParameterMap(Map.of("Event1Field0", event1Field0Definition));
        incomingFieldDefinitionMap.put("Event1", event);
        eventFieldMaps.put("Event1", Map.of("Event1Field0", "Value"));
        output.setEventFields(incomingFieldDefinitionMap, eventFieldMaps);

        StateOutput outputCopy = new StateOutput(axPolicy.getStateMap().get("State0")
                .getStateOutputs().get("stateOutput0"));

        EnEvent incomingEvent = new EnEvent(new AxArtifactKey("Event0:0.0.1"));
        outputCopy.copyUnsetFields(incomingEvent);
        incomingEvent.put("Event1Field0", "Hello");
        outputCopy.copyUnsetFields(incomingEvent);
    }
}