diff options
author | a.sreekumar <ajith.sreekumar@bell.ca> | 2021-06-22 15:55:46 +0100 |
---|---|---|
committer | a.sreekumar <ajith.sreekumar@bell.ca> | 2021-06-28 10:36:48 +0100 |
commit | 926646d8e5e86e680a119360f93d7bdb46c89435 (patch) | |
tree | 7ff431e59672b19f658faed87c80b4d147253c9c /core/core-engine/src/test/java | |
parent | 63637d939697451d3ac63216d938780a157ff895 (diff) |
Changes to support multiple outputs from a state
This review addresses two main changes:
1) inputFields and outputFields are not tied to task definition anymore.
Instead inputEvent and outputEvents associated to a task is populated
as part of the policy state definition, as the state definition have
the information anyway.
- Clean up of the usage of inputFields and outputFields in task
definition will happen in a future review
- inputFields and outputFields defined in task definition in
policies until honolulu will not make the policy invalid as the
changes are done in backward compatible way.
2) Multiple output events can come out of a final state now.
- Define another policy state output with the relevant eventName in
the command file
- In the task logic, create a map to store the fields of the relevant
outputEvent, and then just call
"executor.addFieldsToOutput(<the_map_of_fields>)"
These 2 steps are enough to send multiple events to relevant
components as per the apex configuration.
Change-Id: Id88ca402704106404f529e595e1a76f6bf167876
Issue-ID: POLICY-3336
Signed-off-by: a.sreekumar <ajith.sreekumar@bell.ca>
Diffstat (limited to 'core/core-engine/src/test/java')
6 files changed, 92 insertions, 53 deletions
diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummySmExecutor.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummySmExecutor.java index 18d32140b..df4d9279e 100644 --- a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummySmExecutor.java +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummySmExecutor.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2021 Bell Canada. 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. @@ -21,6 +22,8 @@ package org.onap.policy.apex.core.engine.engine.impl; +import java.util.Collection; +import java.util.List; import java.util.Properties; import org.onap.policy.apex.core.engine.event.EnEvent; import org.onap.policy.apex.core.engine.executor.ExecutorFactory; @@ -62,8 +65,9 @@ public class DummySmExecutor extends StateMachineExecutor { * {@inheritDoc}. */ @Override - public EnEvent execute(final long executionId, final Properties executionProperties, final EnEvent incomingEvent) { - return incomingEvent; + public Collection<EnEvent> execute(final long executionId, final Properties executionProperties, + final EnEvent incomingEvent) { + return List.of(incomingEvent); } /** diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/DummyTaskExecutor.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/DummyTaskExecutor.java index 8d1aa5f0d..75c4c0d7c 100644 --- a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/DummyTaskExecutor.java +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/DummyTaskExecutor.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2021 Bell Canada. 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. @@ -27,12 +28,14 @@ import org.onap.policy.apex.context.ContextException; 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.eventmodel.concepts.AxEvent; import org.onap.policy.apex.model.policymodel.concepts.AxTask; /** * Dummy task executor for testing. */ public class DummyTaskExecutor extends TaskExecutor { + private static final String EVENT_KEY = "Event1:0.0.1"; private boolean override; public DummyTaskExecutor() { @@ -54,14 +57,14 @@ public class DummyTaskExecutor extends TaskExecutor { * {@inheritDoc}. */ @Override - public Map<String, Object> execute(final long executionId, final Properties executionProperties, + public Map<String, Map<String, Object>> execute(final long executionId, final Properties executionProperties, final Map<String, Object> newIncomingFields) throws StateMachineException, ContextException { if (!override) { super.execute(executionId, executionProperties, newIncomingFields); } - AxArtifactKey event0Key = new AxArtifactKey("Event0:0.0.1"); - return new EnEvent(event0Key); + AxArtifactKey eventKey = new AxArtifactKey(EVENT_KEY); + return Map.of(eventKey.getName(), new EnEvent(eventKey)); } /** @@ -74,7 +77,9 @@ public class DummyTaskExecutor extends TaskExecutor { } AxArtifactKey taskKey = new AxArtifactKey("FirstTask:0.0.1"); - return new AxTask(taskKey); + AxTask task = new AxTask(taskKey); + task.setOutputEvents(Map.of("Event1", new AxEvent(new AxArtifactKey(EVENT_KEY)))); + return task; } /** diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/StateMachineExecutorTest.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/StateMachineExecutorTest.java index 2acb57681..2d274bd2e 100644 --- a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/StateMachineExecutorTest.java +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/StateMachineExecutorTest.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2020 Nordix Foundation. + * Modifications Copyright (C) 2021 Bell Canada. 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. @@ -24,7 +25,10 @@ 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; @@ -65,7 +69,7 @@ public class StateMachineExecutorTest { private ApexInternalContext internalContextMock; @Mock - private Executor<EnEvent, EnEvent, AxPolicy, ApexInternalContext> nextExecutorMock; + private Executor<EnEvent, Collection<EnEvent>, AxPolicy, ApexInternalContext> nextExecutorMock; @Mock private ExecutorFactory executorFactoryMock; @@ -191,18 +195,18 @@ public class StateMachineExecutorTest { .hasMessage("no states defined on state machine"); executor.setContext(null, axPolicy, internalContextMock); assertEquals("Policy:0.0.1", executor.getKey().getId()); - assertEquals(null, executor.getParent()); + assertNull(executor.getParent()); assertEquals(internalContextMock, executor.getContext()); - assertEquals(null, executor.getNext()); - assertEquals(null, executor.getIncoming()); - assertEquals(null, executor.getOutgoing()); + 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); - assertEquals(null, executor.getNext()); + assertNull(executor.getNext()); assertThatThrownBy(() -> executor.executePre(0, null, null)) .hasMessage("execution pre work not implemented on class"); @@ -294,34 +298,37 @@ public class StateMachineExecutorTest { assertThatThrownBy(() -> output.setEventFields(null, null)) .hasMessage("incomingFieldDefinitionMap may not be null"); - Map<String, AxField> incomingFieldDefinitionMap = new LinkedHashMap<>(); + Map<String, AxEvent> incomingFieldDefinitionMap = new LinkedHashMap<>(); assertThatThrownBy(() -> output.setEventFields(incomingFieldDefinitionMap, null)) - .hasMessage("eventFieldMap may not be null"); - Map<String, Object> eventFieldMap = new LinkedHashMap<>(); - output.setEventFields(incomingFieldDefinitionMap, eventFieldMap); - - eventFieldMap.put("key", "Value"); - assertThatThrownBy(() -> output.setEventFields(incomingFieldDefinitionMap, eventFieldMap)) - .hasMessage("field definitions and values do not match for event Event1:0.0.1\n[]\n[key]"); - AxField axBadFieldDefinition = new AxField(); - incomingFieldDefinitionMap.put("key", axBadFieldDefinition); - assertThatThrownBy(() -> output.setEventFields(incomingFieldDefinitionMap, eventFieldMap)) + .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(); - eventFieldMap.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); - incomingFieldDefinitionMap.put("Event1Field0", event1Field0Definition); - eventFieldMap.put("Event1Field0", "Value"); - output.setEventFields(incomingFieldDefinitionMap, eventFieldMap); + 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); } diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/TaskExecutorTest.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/TaskExecutorTest.java index bbedb886c..1ddc3f5b5 100644 --- a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/TaskExecutorTest.java +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/TaskExecutorTest.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2020 Nordix Foundation. + * Modifications Copyright (C) 2021 Bell Canada. 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. @@ -32,6 +33,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.TreeMap; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -44,6 +46,8 @@ import org.onap.policy.apex.core.engine.context.ApexInternalContext; 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.eventmodel.concepts.AxEvent; +import org.onap.policy.apex.model.eventmodel.concepts.AxField; import org.onap.policy.apex.model.eventmodel.concepts.AxInputField; import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField; import org.onap.policy.apex.model.policymodel.concepts.AxTask; @@ -76,14 +80,16 @@ public class TaskExecutorTest { private AxOutputField axMissingOutputFieldMock; @Mock - private Executor<Map<String, Object>, Map<String, Object>, AxTask, ApexInternalContext> nextExecutorMock; + private Executor<Map<String, Object>, Map<String, Map<String, Object>>, AxTask, + ApexInternalContext> nextExecutorMock; @Mock private AxTaskLogic taskLogicMock; - private LinkedHashMap<String, Object> inFieldMap; - private LinkedHashMap<String, Object> outFieldMap; + private Map<String, AxField> inFieldMap; + private Map<String, AxField> outFieldMap; private List<TaskParameters> taskParametersFromConfig; + private Map<String, AxEvent> outEvents = new TreeMap<>(); /** * Set up mocking. @@ -96,13 +102,17 @@ public class TaskExecutorTest { Mockito.doReturn(task0Key).when(axTaskMock).getKey(); Mockito.doReturn(task0Key.getId()).when(axTaskMock).getId(); - inFieldMap = new LinkedHashMap<>(); + inFieldMap = Map.of("InField0", axInputFieldMock, "InField1", axOptionalInputFieldMock); outFieldMap = new LinkedHashMap<>(); - inFieldMap.put("InField0", axInputFieldMock); - inFieldMap.put("InField1", axOptionalInputFieldMock); outFieldMap.put("OutField0", axOutputFieldMock); - outFieldMap.put("OutField0", axOptionalOutputFieldMock); + outFieldMap.put("OutField1", axOptionalOutputFieldMock); + + AxEvent inEvent = new AxEvent(); + inEvent.setParameterMap(inFieldMap); + AxEvent outEvent = new AxEvent(new AxArtifactKey("outputEvent:1.0.0")); + outEvent.setParameterMap(outFieldMap); + outEvents.put(outEvent.getKey().getName(), outEvent); AxArtifactKey schemaKey = new AxArtifactKey("Schema:0.0.1"); Mockito.doReturn(schemaKey).when(axInputFieldMock).getSchema(); @@ -119,6 +129,9 @@ public class TaskExecutorTest { Mockito.doReturn(outFieldMap).when(axTaskMock).getOutputFields(); Mockito.doReturn(taskLogicMock).when(axTaskMock).getTaskLogic(); + Mockito.doReturn(inEvent).when(axTaskMock).getInputEvent(); + Mockito.doReturn(outEvents).when(axTaskMock).getOutputEvents(); + Mockito.doReturn(new AxArtifactKey("Context:0.0.1")).when(internalContextMock).getKey(); Map<String, AxTaskParameter> taskParameters = new HashMap<>(); @@ -162,9 +175,6 @@ public class TaskExecutorTest { Map<String, Object> incomingFields = new LinkedHashMap<>(); - assertThatThrownBy(() -> executor.executePre(0, new Properties(), incomingFields)) - .hasMessageContaining("task input fields \"[InField0]\" are missing for task \"Task0:0.0.1\""); - incomingFields.put("InField0", "A Value"); executor.executePre(0, new Properties(), incomingFields); @@ -184,15 +194,16 @@ public class TaskExecutorTest { executor.executePost(true); outFieldMap.put("MissingField", axMissingOutputFieldMock); - - assertThatThrownBy(() -> executor.executePost(true)) - .hasMessageContaining("task output fields \"[MissingField]\" are missing for task \"Task0:0.0.1\""); + outEvents.get("outputEvent").getParameterMap().put("MissingField", axMissingOutputFieldMock); + assertThatThrownBy(() -> executor.executePost(true)).hasMessageContaining( + "Fields for task output events \"[outputEvent]\" are missing for task \"Task0:0.0.1\""); outFieldMap.remove("MissingField"); + outEvents.get("outputEvent").getParameterMap().remove("MissingField"); executor.getExecutionContext().outFields.put("BadExtraField", "Howdy!"); - assertThatThrownBy(() -> executor.executePost(true)) - .hasMessageContaining("task output fields \"[BadExtraField]\" are unwanted for task \"Task0:0.0.1\""); + assertThatThrownBy(() -> executor.executePost(true)).hasMessageContaining( + "task output event \"[outputEvent]\" contains fields that are unwanted for task \"Task0:0.0.1\""); executor.getExecutionContext().outFields.remove("BadExtraField"); outFieldMap.put("InField1", axMissingOutputFieldMock); @@ -202,6 +213,7 @@ public class TaskExecutorTest { executor.executePost(true); executor.getExecutionContext().outFields.put("InField0", "Output Value"); + outEvents.get("outputEvent").getParameterMap().put("InField0", axMissingOutputFieldMock); executor.executePost(true); executor.getExecutionContext().outFields.remove("InField0"); diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/context/AxTaskFacadeTest.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/context/AxTaskFacadeTest.java index 8ef78efe9..6f8402e55 100644 --- a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/context/AxTaskFacadeTest.java +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/context/AxTaskFacadeTest.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2020 Nordix Foundation + * Modifications Copyright (C) 2021 Bell Canada. 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. @@ -25,8 +26,8 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import java.util.LinkedHashMap; import java.util.Map; +import java.util.TreeMap; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -40,6 +41,8 @@ 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.AxField; import org.onap.policy.apex.model.eventmodel.concepts.AxInputField; import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField; import org.onap.policy.apex.model.policymodel.concepts.AxTask; @@ -83,13 +86,18 @@ public class AxTaskFacadeTest { Mockito.doReturn(task0Key).when(axTaskMock).getKey(); Mockito.doReturn(task0Key.getId()).when(axTaskMock).getId(); - Map<String, AxInputField> inFieldMap = new LinkedHashMap<>(); - Map<String, AxOutputField> outFieldMap = new LinkedHashMap<>(); + Map<String, AxField> inFieldMap = Map.of("InField0", axInputFieldMock, "InFieldBad", axInputFieldBadMock); + Map<String, AxField> outFieldMap = Map.of("OutField0", axOutputFieldMock, "OutFieldBad", axOutputFieldBadMock); - inFieldMap.put("InField0", axInputFieldMock); - inFieldMap.put("InFieldBad", axInputFieldBadMock); - outFieldMap.put("OutField0", axOutputFieldMock); - outFieldMap.put("OutFieldBad", axOutputFieldBadMock); + AxEvent inEvent = new AxEvent(); + inEvent.setParameterMap(inFieldMap); + AxEvent outEvent = new AxEvent(new AxArtifactKey("outputEvent:1.0.0")); + outEvent.setParameterMap(outFieldMap); + Map<String, AxEvent> outEvents = new TreeMap<>(); + outEvents.put(outEvent.getKey().getName(), outEvent); + + Mockito.doReturn(inEvent).when(axTaskMock).getInputEvent(); + Mockito.doReturn(outEvents).when(axTaskMock).getOutputEvents(); Mockito.doReturn(inFieldMap).when(axTaskMock).getInputFields(); Mockito.doReturn(outFieldMap).when(axTaskMock).getOutputFields(); diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContextTest.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContextTest.java index f8bdc4bdd..24c504822 100644 --- a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContextTest.java +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContextTest.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2020 Nordix Foundation. + * Modifications Copyright (C) 2021 Bell Canada. 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. @@ -28,6 +29,8 @@ import static org.junit.Assert.assertNotNull; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.List; import java.util.Map; import java.util.Set; import org.junit.Before; @@ -93,10 +96,10 @@ public class TaskExecutionContextTest { @Test public void test() { final Map<String, Object> inFields = new LinkedHashMap<>(); - final Map<String, Object> outFields = new LinkedHashMap<>(); + final List<Map<String, Object>> outFieldsList = new LinkedList<>(); - TaskExecutionContext tec = new TaskExecutionContext(taskExecutorMock, 0, null, axTaskMock, inFields, outFields, - internalContextMock); + TaskExecutionContext tec = new TaskExecutionContext(taskExecutorMock, 0, null, axTaskMock, inFields, + outFieldsList, internalContextMock); assertNotNull(tec); tec.setMessage("TEC Message"); |