From 0ab922a17b570f03df710fce58fd9e2f02bb9986 Mon Sep 17 00:00:00 2001 From: "adheli.tavares" Date: Tue, 11 Jun 2024 13:56:51 +0100 Subject: Convert junit4 to junit5 Issue-ID: POLICY-5041 Change-Id: I3c6fa0a5d1d2f0546f2eae52cbc6d3047de04a40 Signed-off-by: adheli.tavares --- .../core/engine/engine/impl/ApexEngineImpl.java | 14 +- .../core/engine/executor/StateMachineExecutor.java | 4 +- .../context/StateFinalizerExecutionContext.java | 4 +- .../executor/context/TaskExecutionContext.java | 4 +- .../context/TaskSelectionExecutionContext.java | 4 +- .../engine/executor/impl/ExecutorFactoryImpl.java | 18 +- .../apex/core/engine/EngineParametersTest.java | 17 +- .../apex/core/engine/ExecutorParametersTest.java | 20 +- .../engine/context/ApexInternalContextTest.java | 35 ++- .../engine/engine/impl/ApexEngineImplTest.java | 155 +++++++------ .../engine/engine/impl/DummyEnEventListener.java | 7 +- .../core/engine/engine/impl/DummyListener.java | 10 +- .../engine/impl/DummySlowEnEventListener.java | 18 +- .../core/engine/engine/impl/DummySmExecutor.java | 6 +- .../policy/apex/core/engine/event/DummyAxKey.java | 10 +- .../policy/apex/core/engine/event/EnEventTest.java | 46 ++-- .../apex/core/engine/event/EnExceptionTest.java | 15 +- .../policy/apex/core/engine/event/EnFieldTest.java | 26 +-- .../engine/executor/DummyFailingTaskExecutor.java | 6 +- .../executor/DummyStateFinalizerExecutor.java | 7 +- .../core/engine/executor/DummyTaskExecutor.java | 5 +- .../engine/executor/DummyTaskSelectExecutor.java | 2 +- .../core/engine/executor/StateExecutorTest.java | 34 +-- .../executor/StateFinalizerExecutorTest.java | 35 +-- .../engine/executor/StateMachineExecutorTest.java | 54 +++-- .../core/engine/executor/TaskExecutorTest.java | 88 ++++---- .../engine/executor/TaskSelectExecutorTest.java | 35 +-- .../engine/executor/context/AxStateFacadeTest.java | 41 ++-- .../engine/executor/context/AxTaskFacadeTest.java | 38 ++-- .../engine/executor/context/DummyContextAlbum.java | 12 +- .../StateFinalizerExecutionContextTest.java | 32 +-- .../executor/context/TaskExecutionContextTest.java | 28 +-- .../context/TaskSelectionExecutionContextTest.java | 32 +-- .../StateMachineRuntimeExceptionTest.java | 18 +- .../executor/impl/ExceutorFactoryImplTest.java | 239 --------------------- .../executor/impl/ExecutorFactoryImplTest.java | 238 ++++++++++++++++++++ .../infrastructure/threading/ThreadingTest.java | 24 +-- 37 files changed, 682 insertions(+), 699 deletions(-) delete mode 100644 core/src/test/java/org/onap/policy/apex/core/engine/executor/impl/ExceutorFactoryImplTest.java create mode 100644 core/src/test/java/org/onap/policy/apex/core/engine/executor/impl/ExecutorFactoryImplTest.java (limited to 'core') diff --git a/core/src/main/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImpl.java b/core/src/main/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImpl.java index 35139bfe4..773923444 100644 --- a/core/src/main/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImpl.java +++ b/core/src/main/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImpl.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2020, 2024 Nordix Foundation. * Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ @@ -193,7 +193,8 @@ public class ApexEngineImpl implements ApexEngine { } private void updateTaskBasedOnStateOutput(AxPolicyModel apexPolicyModel, Set updatedTasks, - AxState state, AxArtifactKey taskKey, AxStateTaskReference taskRef, AxTask task) { + AxState state, AxArtifactKey taskKey, AxStateTaskReference taskRef, + AxTask task) { Map outputEvents = new TreeMap<>(); AxStateOutput stateOutput = null; if (AxStateTaskOutputType.LOGIC.equals(taskRef.getStateTaskOutputType())) { @@ -279,14 +280,13 @@ public class ApexEngineImpl implements ApexEngine { } // Stop the engine if it is in state READY, if it is in state EXECUTING, wait for execution to finish for (int increment = ApexEngineConstants.STOP_EXECUTION_WAIT_TIMEOUT; increment > 0; - increment -= ApexEngineConstants.APEX_ENGINE_STOP_EXECUTION_WAIT_INCREMENT) { + increment -= ApexEngineConstants.APEX_ENGINE_STOP_EXECUTION_WAIT_INCREMENT) { ThreadUtilities.sleep(ApexEngineConstants.APEX_ENGINE_STOP_EXECUTION_WAIT_INCREMENT); synchronized (stateLockObj) { switch (state) { // Engine is OK to stop or has been stopped on return of an event - case READY: - case STOPPED: + case READY, STOPPED: state = AxEngineState.STOPPED; updateStatePrometheusMetric(); stateMachineHandler.stop(); @@ -388,7 +388,7 @@ public class ApexEngineImpl implements ApexEngine { String message = "execute(): triggered by event " + incomingEvent.toString(); LOGGER.debug(message); - // By default we return a null event on errors + // By default, we return a null event on errors Collection outgoingEvents = null; try { engineStats.executionEnter(incomingEvent.getKey()); @@ -406,7 +406,7 @@ public class ApexEngineImpl implements ApexEngine { try { synchronized (eventListeners) { if (eventListeners.isEmpty()) { - LOGGER.debug("handleEvent()<-{},{}, There is no listener registered to recieve outgoing event: {}", + LOGGER.debug("handleEvent()<-{},{}, There is no listener registered to receive outgoing event: {}", key.getId(), state, outgoingEvents); } for (final EnEventListener axEventListener : eventListeners.values()) { diff --git a/core/src/main/java/org/onap/policy/apex/core/engine/executor/StateMachineExecutor.java b/core/src/main/java/org/onap/policy/apex/core/engine/executor/StateMachineExecutor.java index 6cbc04678..6b8dd43ec 100644 --- a/core/src/main/java/org/onap/policy/apex/core/engine/executor/StateMachineExecutor.java +++ b/core/src/main/java/org/onap/policy/apex/core/engine/executor/StateMachineExecutor.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019, 2024 Nordix Foundation. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ @@ -129,7 +129,7 @@ public class StateMachineExecutor implements Executor execute(final long executionId, final Properties executionProperties, final EnEvent incomingEvent) throws StateMachineException, ContextException { // Check if there are any states on the state machine - if (stateExecutorMap.size() == 0) { + if (stateExecutorMap.isEmpty()) { throw new StateMachineException("no states defined on state machine"); } diff --git a/core/src/main/java/org/onap/policy/apex/core/engine/executor/context/StateFinalizerExecutionContext.java b/core/src/main/java/org/onap/policy/apex/core/engine/executor/context/StateFinalizerExecutionContext.java index 12e350390..7cdca18d8 100644 --- a/core/src/main/java/org/onap/policy/apex/core/engine/executor/context/StateFinalizerExecutionContext.java +++ b/core/src/main/java/org/onap/policy/apex/core/engine/executor/context/StateFinalizerExecutionContext.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020-2021 Nordix Foundation. + * Modifications Copyright (C) 2020-2021, 2024 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ @@ -86,7 +86,7 @@ public class StateFinalizerExecutionContext extends AbstractExecutionContext { /** * Logger for state finalizer execution, state finalizer logic can use this field to access and log to Apex logging. */ - public final XLogger logger = EXCEUTION_LOGGER; + public static final XLogger logger = EXCEUTION_LOGGER; // CHECKSTYLE:ON: checkstyle:visibilityModifier diff --git a/core/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContext.java b/core/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContext.java index a54252e05..d5bb370bc 100644 --- a/core/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContext.java +++ b/core/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContext.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. + * Modifications Copyright (C) 2020, 2024 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ @@ -83,7 +83,7 @@ public class TaskExecutionContext extends AbstractExecutionContext { /** * Logger for task execution, task logic can use this field to access and log to Apex logging. */ - public final XLogger logger = EXECUTION_LOGGER; + public static final XLogger logger = EXECUTION_LOGGER; // CHECKSTYLE:ON: checkstyle:VisibilityModifier diff --git a/core/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskSelectionExecutionContext.java b/core/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskSelectionExecutionContext.java index c79e907d9..62808dfb6 100644 --- a/core/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskSelectionExecutionContext.java +++ b/core/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskSelectionExecutionContext.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. + * Modifications Copyright (C) 2020, 2024 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ @@ -73,7 +73,7 @@ public class TaskSelectionExecutionContext extends AbstractExecutionContext { /** * Logger for task selection execution, task selection logic can use this field to access and log to Apex logging. */ - public final XLogger logger = EXECUTION_LOGGER; + public static final XLogger logger = EXECUTION_LOGGER; // CHECKSTYLE:ON: checkstyle:VisibilityModifier diff --git a/core/src/main/java/org/onap/policy/apex/core/engine/executor/impl/ExecutorFactoryImpl.java b/core/src/main/java/org/onap/policy/apex/core/engine/executor/impl/ExecutorFactoryImpl.java index 9dc841f48..53b041e36 100644 --- a/core/src/main/java/org/onap/policy/apex/core/engine/executor/impl/ExecutorFactoryImpl.java +++ b/core/src/main/java/org/onap/policy/apex/core/engine/executor/impl/ExecutorFactoryImpl.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2020, 2024 Nordix Foundation. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -157,21 +157,21 @@ public class ExecutorFactoryImpl implements ExecutorFactory { } // Get the class for the executor using reflection - Class executorPluginClass = null; + Class executorPluginClass; try { executorPluginClass = Class.forName(executorClassName); } catch (final ClassNotFoundException e) { - LOGGER.error("Apex executor class not found for executor plugin \"" + executorClassName + "\"", e); + LOGGER.error("Apex executor class not found for executor plugin \"{}\"", executorClassName, e); throw new StateMachineException( "Apex executor class not found for executor plugin \"" + executorClassName + "\"", e); } // Check the class is an executor if (!Executor.class.isAssignableFrom(executorPluginClass)) { - LOGGER.error("Specified Apex executor plugin class \"{}\" does not implment the Executor interface", + LOGGER.error("Specified Apex executor plugin class \"{}\" does not implement the Executor interface", executorClassName); throw new StateMachineException("Specified Apex executor plugin class \"" + executorClassName - + "\" does not implment the Executor interface"); + + "\" does not implement the Executor interface"); } return (Class>) executorPluginClass; @@ -181,14 +181,14 @@ public class ExecutorFactoryImpl implements ExecutorFactory { * Get an instance of an executor plugin class of the specified type and super type. * * @param logicFlavour The logic flavour of the logic - * @param executorClass The sub-class of the executor type to be instantiated + * @param executorClass The subclass of the executor type to be instantiated * @param executorSuperClass The super type of the class of executor to be instantiated * @return The instantiated class */ private Executor createExecutor(final String logicFlavour, final Class> executorClass, final Class> executorSuperClass) { - // It's OK for an executor class not to be defined but it's not all right to try and create + // It's OK for an executor class not to be defined, but it's not all right to try and create // a non-defined // executor class if (executorClass == null) { @@ -199,7 +199,7 @@ public class ExecutorFactoryImpl implements ExecutorFactory { } // Create an executor for the specified logic flavour - Object executorObject = null; + Executor executorObject; try { executorObject = executorClass.getDeclaredConstructor().newInstance(); } catch (final Exception e) { @@ -218,6 +218,6 @@ public class ExecutorFactoryImpl implements ExecutorFactory { throw new StateMachineRuntimeException(errorMessage); } - return (Executor) executorObject; + return executorObject; } } diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/EngineParametersTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/EngineParametersTest.java index 5427c3515..33c9b3efb 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/EngineParametersTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/EngineParametersTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. + * Modifications Copyright (C) 2020, 2024 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,26 +23,25 @@ package org.onap.policy.apex.core.engine; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.onap.policy.apex.context.parameters.ContextParameters; import org.onap.policy.common.parameters.ParameterService; /** * Test the executor parameters. - * */ -public class EngineParametersTest { +class EngineParametersTest { @Test - public void test() { + void test() { EngineParameters pars = new EngineParameters(); pars.setName("Name"); assertEquals("Name", pars.getName()); @@ -70,7 +69,7 @@ public class EngineParametersTest { } @Test - public void test_invalid() { + void test_invalid() { EngineParameters pars = new EngineParameters(); pars.setName("Name"); assertEquals("Name", pars.getName()); diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/ExecutorParametersTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/ExecutorParametersTest.java index 784580422..5e1ee81fd 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/ExecutorParametersTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/ExecutorParametersTest.java @@ -1,7 +1,8 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,20 +23,19 @@ package org.onap.policy.apex.core.engine; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.onap.policy.common.parameters.ParameterService; /** * Test the executor parameters. - * */ -public class ExecutorParametersTest { +class ExecutorParametersTest { @Test - public void test() { + void test() { ExecutorParameters pars = new ExecutorParameters(); pars.setName("Name"); assertEquals("Name", pars.getName()); @@ -47,8 +47,8 @@ public class ExecutorParametersTest { assertEquals("some.task.selection.executor.plugin.class", pars.getTaskSelectionExecutorPluginClass()); assertEquals("ExecutorParameters [name=Name, taskExecutorPluginClass=some.task.executor.plugin.class, " - + "taskSelectionExecutorPluginClass=some.task.selection.executor.plugin.class, " - + "stateFinalizerExecutorPluginClass=some.state.finalizer.plugin.class]", pars.toString()); + + "taskSelectionExecutorPluginClass=some.task.selection.executor.plugin.class, " + + "stateFinalizerExecutorPluginClass=some.state.finalizer.plugin.class]", pars.toString()); assertThat(pars.validate().getResult()).isNull(); assertTrue(pars.validate().isValid()); diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/context/ApexInternalContextTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/context/ApexInternalContextTest.java index 19828f6e2..e918deb12 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/context/ApexInternalContextTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/context/ApexInternalContextTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2020, 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,11 +22,11 @@ package org.onap.policy.apex.core.engine.context; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.onap.policy.apex.context.ContextException; import org.onap.policy.apex.context.parameters.ContextParameterConstants; import org.onap.policy.apex.context.parameters.DistributorParameters; @@ -42,20 +42,19 @@ import org.onap.policy.common.parameters.ParameterService; /** * Test the Apex engine internal context class. */ -public class ApexInternalContextTest { +class ApexInternalContextTest { private AxPolicyModel policyModel; private AxPolicyModel newVersionPolicyModel; private AxPolicyModel newPolicyModel; private AxContextAlbum album; - private AxContextAlbum newAlbum; private AxPolicyModel incompatiblePolicyModel; /** * Initialize parameters. */ - @Before - public void registerParameters() { + @BeforeEach + void registerParameters() { ParameterService.register(new SchemaParameters()); ParameterService.register(new DistributorParameters()); ParameterService.register(new LockManagerParameters()); @@ -65,8 +64,8 @@ public class ApexInternalContextTest { /** * Create policy model. */ - @Before - public void createPolicyModels() { + @BeforeEach + void createPolicyModels() { AxArtifactKey modelKey = new AxArtifactKey("PolicyModel:0.0.1"); policyModel = new AxPolicyModel(modelKey); @@ -109,7 +108,7 @@ public class ApexInternalContextTest { newPolicyModel.getSchemas().getSchemasMap().put(newSchemaKey, newSchema); AxArtifactKey newAlbumKey = new AxArtifactKey("NewAlbum:0.0.1"); - newAlbum = new AxContextAlbum(newAlbumKey, "Policy", true, newSchemaKey); + AxContextAlbum newAlbum = new AxContextAlbum(newAlbumKey, "Policy", true, newSchemaKey); newPolicyModel.getAlbums().getAlbumsMap().put(newAlbumKey, newAlbum); } @@ -117,8 +116,8 @@ public class ApexInternalContextTest { /** * Deregister parameters. */ - @After - public void deregisterParameters() { + @AfterEach + void deregisterParameters() { ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME); ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME); ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME); @@ -126,7 +125,7 @@ public class ApexInternalContextTest { } @Test - public void testAlbumInit() throws ContextException { + void testAlbumInit() throws ContextException { assertThatThrownBy(() -> new ApexInternalContext(null)) .hasMessage("internal context update failed, supplied model is null"); ApexInternalContext context = new ApexInternalContext(policyModel); @@ -140,17 +139,17 @@ public class ApexInternalContextTest { assertEquals(album.getId(), context.get(albumKey.getName(), albumKey.getVersion()).getKey().getId()); assertEquals(album.getId(), context.getAll(albumKey.getName()).iterator().next().getKey().getId()); assertEquals(album.getId(), - context.getAll(albumKey.getName(), albumKey.getVersion()).iterator().next().getKey().getId()); + context.getAll(albumKey.getName(), albumKey.getVersion()).iterator().next().getKey().getId()); context.clear(); assertEquals(1, context.getContextAlbums().size()); assertEquals("ApexInternalContext [contextAlbums={AxArtifactKey:(name=Album,version=0.0.1)", - context.toString().substring(0, 76)); + context.toString().substring(0, 76)); } @Test - public void testAlbumUpdate() throws ContextException { + void testAlbumUpdate() throws ContextException { ApexInternalContext context = new ApexInternalContext(policyModel); assertThatThrownBy(() -> context.update(null, false)) .hasMessage("internal context update failed, supplied model is null"); diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImplTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImplTest.java index 8d7df45cd..a66f8c683 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImplTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImplTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2021, 2023 Nordix Foundation. + * Modifications Copyright (C) 2019-2021, 2023-2024 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,25 +24,25 @@ package org.onap.policy.apex.core.engine.engine.impl; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.awaitility.Awaitility.await; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import io.prometheus.client.CollectorRegistry; import java.io.IOException; import java.lang.reflect.Field; import java.util.HashMap; import java.util.concurrent.TimeUnit; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.onap.policy.apex.context.parameters.ContextParameterConstants; import org.onap.policy.apex.context.parameters.DistributorParameters; import org.onap.policy.apex.context.parameters.LockManagerParameters; @@ -71,8 +71,8 @@ import org.onap.policy.common.parameters.ParameterService; /** * Test the engine implementation. */ -@RunWith(MockitoJUnitRunner.class) -public class ApexEngineImplTest { +@ExtendWith(MockitoExtension.class) +class ApexEngineImplTest { private static final String ENGINE_ID = "Engine:0.0.1"; private AxPolicyModel policyModel; @@ -85,8 +85,8 @@ public class ApexEngineImplTest { /** * Set up services. */ - @BeforeClass - public static void setup() { + @BeforeAll + static void setup() { ParameterService.register(new SchemaParameters()); ParameterService.register(new DistributorParameters()); ParameterService.register(new LockManagerParameters()); @@ -97,17 +97,17 @@ public class ApexEngineImplTest { /** * Set up mocking. */ - @Before - public void initializeMocking() throws ApexException { - Mockito.doThrow(new StateMachineException("mocked state machine exception", - new IOException("nexted exception"))).when(smHandlerMock).execute(Mockito.any()); + @BeforeEach + void initializeMocking() throws ApexException { + Mockito.lenient().doThrow(new StateMachineException("mocked state machine exception", + new IOException("nexted exception"))).when(smHandlerMock).execute(Mockito.any()); } /** * Create policy models. */ - @Before - public void createPolicyModels() { + @BeforeEach + void createPolicyModels() { AxArtifactKey modelKey = new AxArtifactKey("PolicyModel:0.0.1"); policyModel = new AxPolicyModel(modelKey); @@ -162,8 +162,8 @@ public class ApexEngineImplTest { /** * Clear registrations. */ - @AfterClass - public static void teardown() { + @AfterAll + static void teardown() { ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME); ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME); @@ -172,14 +172,14 @@ public class ApexEngineImplTest { } @Test - public void testSanity() throws ApexException { + void testSanity() throws ApexException { AxArtifactKey engineKey = new AxArtifactKey(ENGINE_ID); ApexEngineImpl engine = (ApexEngineImpl) new ApexEngineFactory().createApexEngine(engineKey); assertNotNull(engine); assertEquals(engineKey, engine.getKey()); assertThatThrownBy(engine::start).hasMessage("start()<-Engine:0.0.1,STOPPED, cannot start engine, " - + "engine has not been initialized, its model is not loaded"); + + "engine has not been initialized, its model is not loaded"); assertThatThrownBy(engine::stop) .hasMessage("stop()<-Engine:0.0.1,STOPPED, cannot stop engine, " + "engine is already stopped"); @@ -203,7 +203,7 @@ public class ApexEngineImplTest { } @Test - public void testListener() throws ApexException { + void testListener() throws ApexException { AxArtifactKey engineKey = new AxArtifactKey(ENGINE_ID); ApexEngineImpl engine = (ApexEngineImpl) new ApexEngineFactory().createApexEngine(engineKey); @@ -262,7 +262,7 @@ public class ApexEngineImplTest { } @Test - public void testEventKey() throws ApexException { + void testEventKey() throws ApexException { AxArtifactKey engineKey = new AxArtifactKey(ENGINE_ID); ApexEngineImpl engine = (ApexEngineImpl) new ApexEngineFactory().createApexEngine(engineKey); engine.updateModel(policyModel, false); @@ -315,7 +315,7 @@ public class ApexEngineImplTest { } @Test - public void testState() throws InterruptedException, ApexException { + void testState() throws ApexException { AxArtifactKey engineKey = new AxArtifactKey(ENGINE_ID); ApexEngineImpl engine = (ApexEngineImpl) new ApexEngineFactory().createApexEngine(engineKey); assertNotNull(engine); @@ -332,21 +332,13 @@ public class ApexEngineImplTest { assertEquals(AxEngineState.READY, engine.getState()); checkAxEngineStateMetric(AxEngineState.READY); - assertEquals(AxEngineState.READY, engine.getState()); - checkAxEngineStateMetric(AxEngineState.READY); - AxArtifactKey eventKey = new AxArtifactKey("Event:0.0.1"); EnEvent event = engine.createEvent(eventKey); assertEquals(eventKey, event.getKey()); - // 1 second is less than the 3 second wait on engine stopping + // 1 second is less than the 3 seconds wait on engine stopping slowListener.setWaitTime(1000); - (new Thread() { - @Override - public void run() { - engine.handleEvent(event); - } - }).start(); + (new Thread(() -> engine.handleEvent(event))).start(); await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> engine.getState().equals(AxEngineState.EXECUTING)); assertEquals(AxEngineState.EXECUTING, engine.getState()); checkAxEngineStateMetric(AxEngineState.EXECUTING); @@ -362,14 +354,9 @@ public class ApexEngineImplTest { assertEquals(AxEngineState.READY, engine.getState()); checkAxEngineStateMetric(AxEngineState.READY); - // 4 seconds is more than the 3 second wait on engine stopping + // 4 seconds is more than the 3 seconds wait on engine stopping slowListener.setWaitTime(4000); - (new Thread() { - @Override - public void run() { - engine.handleEvent(event); - } - }).start(); + (new Thread(() -> engine.handleEvent(event))).start(); await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> engine.getState().equals(AxEngineState.EXECUTING)); assertEquals(AxEngineState.EXECUTING, engine.getState()); @@ -384,8 +371,8 @@ public class ApexEngineImplTest { } @Test - public void testStateMachineError() throws InterruptedException, IllegalArgumentException, IllegalAccessException, - NoSuchFieldException, SecurityException, ApexException { + void testStateMachineError() throws IllegalArgumentException, IllegalAccessException, + NoSuchFieldException, SecurityException, ApexException { AxArtifactKey engineKey = new AxArtifactKey(ENGINE_ID); ApexEngineImpl engine = (ApexEngineImpl) new ApexEngineFactory().createApexEngine(engineKey); @@ -404,9 +391,6 @@ public class ApexEngineImplTest { assertEquals(AxEngineState.READY, engine.getState()); checkAxEngineStateMetric(AxEngineState.READY); - assertEquals(AxEngineState.READY, engine.getState()); - checkAxEngineStateMetric(AxEngineState.READY); - AxArtifactKey eventKey = new AxArtifactKey("Event:0.0.1"); EnEvent event = engine.createEvent(eventKey); assertEquals(eventKey, event.getKey()); @@ -419,9 +403,9 @@ public class ApexEngineImplTest { assertEquals(AxEngineState.STOPPED, engine.getState()); checkAxEngineStateMetric(AxEngineState.STOPPED); Mockito.doThrow(new StateMachineException("mocked state machine exception", - new IOException("nexted exception"))).when(smHandlerMock).start(); + new IOException("nexted exception"))).when(smHandlerMock).start(); assertThatThrownBy(engine::start).hasMessage("updateModel()<-Engine:0.0.1, error starting the engine state " - + "machines \"Engine:0.0.1\""); + + "machines \"Engine:0.0.1\""); assertEquals(AxEngineState.STOPPED, engine.getState()); checkAxEngineStateMetric(AxEngineState.STOPPED); @@ -431,8 +415,8 @@ public class ApexEngineImplTest { } @Test - public void testStateMachineHandler() throws InterruptedException, IllegalArgumentException, IllegalAccessException, - NoSuchFieldException, SecurityException, ApexException { + void testStateMachineHandler() throws IllegalArgumentException, IllegalAccessException, + NoSuchFieldException, SecurityException, ApexException { AxArtifactKey engineKey = new AxArtifactKey(ENGINE_ID); ApexEngineImpl engine = (ApexEngineImpl) new ApexEngineFactory().createApexEngine(engineKey); assertNotNull(engine); @@ -450,10 +434,24 @@ public class ApexEngineImplTest { EnEvent event = engine.createEvent(eventKey); assertEquals(eventKey, event.getKey()); + assertEngineStopStartState(engine, event); + + HashMap + smExMap = getAxEventStateMachineExecutorHashMap(engine); + + assertEquals(1, smExMap.size()); + DummySmExecutor dummyExecutor = new DummySmExecutor(null, event.getKey()); + smExMap.put(event.getAxEvent(), dummyExecutor); + ApexInternalContext internalContext = new ApexInternalContext(policyModelWithStates); + assertThatThrownBy(() -> dummyExecutor.setContext(null, null, internalContext)) + .isInstanceOf(NullPointerException.class); + engine.stop(); assertEquals(AxEngineState.STOPPED, engine.getState()); checkAxEngineStateMetric(AxEngineState.STOPPED); + assertThatThrownBy(engine::start).hasMessageContaining("updateModel()<-Engine:0.0.1, error starting the " + + "engine state machines \"Engine:0.0.1\""); assertEquals(AxEngineState.STOPPED, engine.getState()); checkAxEngineStateMetric(AxEngineState.STOPPED); @@ -461,14 +459,22 @@ public class ApexEngineImplTest { assertEquals(AxEngineState.READY, engine.getState()); checkAxEngineStateMetric(AxEngineState.READY); + // Works, Dummy executor fakes event execution + assertTrue(engine.handleEvent(event)); assertEquals(AxEngineState.READY, engine.getState()); checkAxEngineStateMetric(AxEngineState.READY); - // Can't work, state is not fully defined - assertFalse(engine.handleEvent(event)); - assertEquals(AxEngineState.READY, engine.getState()); - checkAxEngineStateMetric(AxEngineState.READY); + engine.stop(); + assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); + + engine.clear(); + assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); + } + private static HashMap getAxEventStateMachineExecutorHashMap(ApexEngineImpl engine) + throws NoSuchFieldException, IllegalAccessException { final Field smHandlerField = engine.getClass().getDeclaredField("stateMachineHandler"); smHandlerField.setAccessible(true); StateMachineHandler smHandler = (StateMachineHandler) smHandlerField.get(engine); @@ -477,45 +483,28 @@ public class ApexEngineImplTest { smExecutorMapField.setAccessible(true); @SuppressWarnings("unchecked") HashMap smExMap = (HashMap) smExecutorMapField - .get(smHandler); - - assertEquals(1, smExMap.size()); - DummySmExecutor dummyExecutor = new DummySmExecutor(null, event.getKey()); - smExMap.put(event.getAxEvent(), dummyExecutor); - ApexInternalContext internalContext = new ApexInternalContext(policyModelWithStates); - assertThatThrownBy(() -> dummyExecutor.setContext(null, null, internalContext)) - .isInstanceOf(NullPointerException.class); + .get(smHandler); + return smExMap; + } + private void assertEngineStopStartState(ApexEngineImpl engine, EnEvent event) throws ApexException { engine.stop(); assertEquals(AxEngineState.STOPPED, engine.getState()); checkAxEngineStateMetric(AxEngineState.STOPPED); - assertThatThrownBy(engine::start).hasMessageContaining("updateModel()<-Engine:0.0.1, error starting the " - + "engine state machines \"Engine:0.0.1\""); - assertEquals(AxEngineState.STOPPED, engine.getState()); - checkAxEngineStateMetric(AxEngineState.STOPPED); - engine.start(); assertEquals(AxEngineState.READY, engine.getState()); checkAxEngineStateMetric(AxEngineState.READY); - // Works, Dummy executor fakes event execution - assertTrue(engine.handleEvent(event)); + // Can't work, state is not fully defined + assertFalse(engine.handleEvent(event)); assertEquals(AxEngineState.READY, engine.getState()); checkAxEngineStateMetric(AxEngineState.READY); - - engine.stop(); - assertEquals(AxEngineState.STOPPED, engine.getState()); - checkAxEngineStateMetric(AxEngineState.STOPPED); - - engine.clear(); - assertEquals(AxEngineState.STOPPED, engine.getState()); - checkAxEngineStateMetric(AxEngineState.STOPPED); } private void checkAxEngineStateMetric(AxEngineState state) { Double stateMetric = CollectorRegistry.defaultRegistry - .getSampleValue("pdpa_engine_state", new String[]{"engine_instance_id"}, new String[]{ENGINE_ID}); + .getSampleValue("pdpa_engine_state", new String[] {"engine_instance_id"}, new String[] {ENGINE_ID}); assertEquals(stateMetric.intValue(), state.getStateIdentifier()); } } \ No newline at end of file diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummyEnEventListener.java b/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummyEnEventListener.java index b429295ad..2227379a6 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummyEnEventListener.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummyEnEventListener.java @@ -5,15 +5,15 @@ * 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========================================================= */ @@ -26,7 +26,6 @@ import org.onap.policy.apex.model.basicmodel.concepts.ApexException; /** * Dummy engine event listener for unit test. - * */ public class DummyEnEventListener implements EnEventListener { diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummyListener.java b/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummyListener.java index 6256ff498..565bb2348 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummyListener.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummyListener.java @@ -1,19 +1,20 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2024 Nordix Foundation. * ================================================================================ * 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========================================================= */ @@ -26,7 +27,6 @@ import org.onap.policy.apex.model.basicmodel.concepts.ApexException; /** * Dummy engine event listener for unit test. - * */ public class DummyListener implements EnEventListener { @@ -34,7 +34,7 @@ public class DummyListener implements EnEventListener { * {@inheritDoc}. */ @Override - public void onEnEvent(EnEvent enEvent) throws ApexException { + public void onEnEvent(EnEvent enEvent) { // Do nothing } } diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummySlowEnEventListener.java b/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummySlowEnEventListener.java index e794a7d04..fdc698145 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummySlowEnEventListener.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummySlowEnEventListener.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2021 Nordix Foundation. + * Modifications Copyright (C) 2019-2021, 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,15 +21,17 @@ package org.onap.policy.apex.core.engine.engine.impl; +import lombok.Getter; +import lombok.Setter; import org.onap.policy.apex.core.engine.engine.EnEventListener; import org.onap.policy.apex.core.engine.event.EnEvent; -import org.onap.policy.apex.model.basicmodel.concepts.ApexException; /** * Dummy engine event listener for unit test. * Thread.sleep is used to simulate a slow event listener. - * */ +@Setter +@Getter public class DummySlowEnEventListener implements EnEventListener { private long waitTime; @@ -38,19 +40,11 @@ public class DummySlowEnEventListener implements EnEventListener { * {@inheritDoc}. */ @Override - public void onEnEvent(EnEvent enEvent) throws ApexException { + public void onEnEvent(EnEvent enEvent) { try { Thread.sleep(waitTime); } catch (InterruptedException ie) { //Do nothing } } - - public long getWaitTime() { - return waitTime; - } - - public void setWaitTime(long waitTime) { - this.waitTime = waitTime; - } } diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummySmExecutor.java b/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummySmExecutor.java index df4d9279e..719a20e82 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummySmExecutor.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummySmExecutor.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019, 2024 Nordix Foundation. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -42,7 +42,7 @@ public class DummySmExecutor extends StateMachineExecutor { * Constructor. * * @param executorFactory the factory for executors - * @param owner the owner key + * @param owner the owner key */ public DummySmExecutor(ExecutorFactory executorFactory, AxArtifactKey owner) { super(executorFactory, owner); @@ -66,7 +66,7 @@ public class DummySmExecutor extends StateMachineExecutor { */ @Override public Collection execute(final long executionId, final Properties executionProperties, - final EnEvent incomingEvent) { + final EnEvent incomingEvent) { return List.of(incomingEvent); } diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/event/DummyAxKey.java b/core/src/test/java/org/onap/policy/apex/core/engine/event/DummyAxKey.java index eafa7f419..a806bed37 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/event/DummyAxKey.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/event/DummyAxKey.java @@ -1,25 +1,27 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2024 Nordix Foundation. * ================================================================================ * 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.event; +import java.io.Serial; import java.util.List; import org.apache.commons.lang3.NotImplementedException; import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; @@ -30,6 +32,8 @@ import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; * Dummy Key Class. */ public class DummyAxKey extends AxKey { + + @Serial private static final long serialVersionUID = 964899169013353800L; /** diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/event/EnEventTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/event/EnEventTest.java index 49b4f80d2..18a7070bf 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/event/EnEventTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/event/EnEventTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020-2021 Nordix Foundation + * Modifications Copyright (C) 2020-2021, 2024 Nordix Foundation * Modifications Copyright (C) 2022 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,18 +23,18 @@ package org.onap.policy.apex.core.engine.event; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.onap.policy.apex.context.parameters.ContextParameterConstants; import org.onap.policy.apex.context.parameters.SchemaParameters; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; @@ -51,12 +51,12 @@ import org.onap.policy.common.parameters.ParameterService; /** * Test the engine event class. */ -public class EnEventTest { +class EnEventTest { /** * Set up the services. */ - @Before - public void setupServices() { + @BeforeEach + void setupServices() { ModelService.registerModel(AxContextSchemas.class, new AxContextSchemas()); ModelService.registerModel(AxEvents.class, new AxEvents()); ParameterService.register(new SchemaParameters()); @@ -65,15 +65,15 @@ public class EnEventTest { /** * Tear down the services. */ - @After - public void teardownServices() { + @AfterEach + void teardownServices() { ModelService.deregisterModel(AxContextSchema.class); ModelService.deregisterModel(AxEvents.class); ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); } @Test - public void testEnEvent() { + void testEnEvent() { AxArtifactKey eventKey = new AxArtifactKey("Event:0.0.1"); assertThatThrownBy(() -> new EnEvent(eventKey)) .hasMessage("event definition is null or was not found in model service"); @@ -92,12 +92,12 @@ public class EnEventTest { event.setExceptionMessage("Something happened"); assertEquals("Something happened", event.getExceptionMessage()); AxConcept[] usedArtifactStackArray = - { eventKey }; + {eventKey}; event.setUserArtifactStack(usedArtifactStackArray); assertEquals(usedArtifactStackArray.length, event.getUserArtifactStack().length); assertEquals("EnEvent [axEvent=AxEvent:(key=AxArtifactKey:(name=Event,version=0.0.1),nameSpace=a.name.space," - + "source=some source,target=some target,parameter={},toscaPolicyState=), " - + "userArtifactStack=[AxArtifactKey:(name=Event,version=0.0.1)], map={}]", event.toString()); + + "source=some source,target=some target,parameter={},toscaPolicyState=), " + + "userArtifactStack=[AxArtifactKey:(name=Event,version=0.0.1)], map={}]", event.toString()); assertThatThrownBy(() -> event.put(null, null)) .hasMessage("null keys are illegal on method parameter \"key\""); assertThatThrownBy(() -> event.put("NonField", null)) @@ -105,7 +105,7 @@ public class EnEventTest { } @Test - public void testAxEvent() { + void testAxEvent() { AxArtifactKey eventKey = new AxArtifactKey("Event:0.0.1"); AxEvent axEvent = new AxEvent(eventKey, "a.name.space", "some source", "some target"); ModelService.getModel(AxEvents.class).getEventMap().put(eventKey, axEvent); @@ -116,7 +116,7 @@ public class EnEventTest { AxField axField = new AxField(fieldKey, fieldSchemaKey); AxConcept[] usedArtifactStackArrayMultiple = - { eventKey, fieldKey, new DummyAxKey() }; + {eventKey, fieldKey, new DummyAxKey()}; event.setUserArtifactStack(usedArtifactStackArrayMultiple); AxContextSchema schema = new AxContextSchema(fieldSchemaKey, "Java", "java.lang.Integer"); @@ -131,12 +131,12 @@ public class EnEventTest { assertThatThrownBy(() -> event.put("MyField", "Hello")) .hasMessage("Parent:0.0.1:MyParent:MyField: object \"Hello\" of class \"java.lang.String\" " - + "not compatible with class \"java.lang.Integer\""); + + "not compatible with class \"java.lang.Integer\""); event.put("MyField", 123); assertEquals(123, event.get("MyField")); - assertTrue(event.keySet().contains("MyField")); - assertTrue(event.values().contains(123)); + assertTrue(event.containsKey("MyField")); + assertTrue(event.containsValue(123)); assertEquals("MyField", event.entrySet().iterator().next().getKey()); event.putAll(event); diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/event/EnExceptionTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/event/EnExceptionTest.java index 5ca49e479..554de6bfd 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/event/EnExceptionTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/event/EnExceptionTest.java @@ -1,37 +1,38 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2024 Nordix Foundation. * ================================================================================ * 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.event; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Test the event exception class. */ -public class EnExceptionTest { +class EnExceptionTest { @Test - public void testEnException() { + void testEnException() { EnException ene = new EnException("Message"); assertEquals("Message", ene.getMessage()); diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/event/EnFieldTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/event/EnFieldTest.java index dbe2fa3c8..d88f79b58 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/event/EnFieldTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/event/EnFieldTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation + * Modifications Copyright (C) 2020, 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,13 +22,13 @@ package org.onap.policy.apex.core.engine.event; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.onap.policy.apex.context.parameters.ContextParameterConstants; import org.onap.policy.apex.context.parameters.SchemaParameters; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; @@ -42,12 +42,12 @@ import org.onap.policy.common.parameters.ParameterService; /** * Test the EnField class. */ -public class EnFieldTest { +class EnFieldTest { /** * Set up the services. */ - @Before - public void setupServices() { + @BeforeEach + void setupServices() { AxContextSchemas schemas = new AxContextSchemas(); ModelService.registerModel(AxContextSchemas.class, schemas); ParameterService.register(new SchemaParameters()); @@ -56,14 +56,14 @@ public class EnFieldTest { /** * Tear down the services. */ - @After - public void teardownServices() { + @AfterEach + void teardownServices() { ModelService.deregisterModel(AxContextSchemas.class); ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); } @Test - public void testEnField() { + void testEnField() { AxReferenceKey fieldKey = new AxReferenceKey("Parent", "0.0.1", "MyParent", "MyField"); AxArtifactKey fieldSchemaKey = new AxArtifactKey("FieldSchema:0.0.1"); AxField axField = new AxField(fieldKey, fieldSchemaKey); diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/executor/DummyFailingTaskExecutor.java b/core/src/test/java/org/onap/policy/apex/core/engine/executor/DummyFailingTaskExecutor.java index 4d4fb639f..02fa41bc8 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/executor/DummyFailingTaskExecutor.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/executor/DummyFailingTaskExecutor.java @@ -5,15 +5,15 @@ * 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========================================================= */ diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/executor/DummyStateFinalizerExecutor.java b/core/src/test/java/org/onap/policy/apex/core/engine/executor/DummyStateFinalizerExecutor.java index e4e3ddf0d..a7b2a43d4 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/executor/DummyStateFinalizerExecutor.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/executor/DummyStateFinalizerExecutor.java @@ -25,6 +25,7 @@ package org.onap.policy.apex.core.engine.executor; import java.util.Map; import java.util.Properties; import lombok.NoArgsConstructor; +import lombok.Setter; import org.onap.policy.apex.context.ContextException; import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; @@ -35,6 +36,7 @@ import org.onap.policy.apex.core.engine.executor.exception.StateMachineException public class DummyStateFinalizerExecutor extends StateFinalizerExecutor { private boolean override; + @Setter private boolean returnBad; public DummyStateFinalizerExecutor(final boolean override) { @@ -46,7 +48,7 @@ public class DummyStateFinalizerExecutor extends StateFinalizerExecutor { */ @Override public String execute(final long executionId, final Properties executionProperties, - final Map newIncomingFields) throws StateMachineException, ContextException { + final Map newIncomingFields) throws StateMachineException, ContextException { if (!override) { super.execute(executionId, executionProperties, newIncomingFields); @@ -59,7 +61,4 @@ public class DummyStateFinalizerExecutor extends StateFinalizerExecutor { } } - public void setReturnBad(boolean returnBad) { - this.returnBad = returnBad; - } } diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/executor/DummyTaskExecutor.java b/core/src/test/java/org/onap/policy/apex/core/engine/executor/DummyTaskExecutor.java index 8172eefcc..f7306c31e 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/executor/DummyTaskExecutor.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/executor/DummyTaskExecutor.java @@ -54,8 +54,9 @@ public class DummyTaskExecutor extends TaskExecutor { * {@inheritDoc}. */ @Override - public Map> execute(final long executionId, final Properties executionProperties, - final Map newIncomingFields) throws StateMachineException, ContextException { + public Map> execute(final long executionId, final Properties executionProperties, + final Map newIncomingFields) + throws StateMachineException, ContextException { if (!override) { super.execute(executionId, executionProperties, newIncomingFields); } diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/executor/DummyTaskSelectExecutor.java b/core/src/test/java/org/onap/policy/apex/core/engine/executor/DummyTaskSelectExecutor.java index a5525ac83..bfee99fe1 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/executor/DummyTaskSelectExecutor.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/executor/DummyTaskSelectExecutor.java @@ -54,7 +54,7 @@ public class DummyTaskSelectExecutor extends TaskSelectExecutor { */ @Override public AxArtifactKey execute(final long executionId, final Properties executionProperties, - final EnEvent newIncomingEvent) throws StateMachineException, ContextException { + final EnEvent newIncomingEvent) throws StateMachineException, ContextException { if (!override) { return super.execute(executionId, executionProperties, newIncomingEvent); } diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/executor/StateExecutorTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/executor/StateExecutorTest.java index fc1b339d2..f448514f1 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/executor/StateExecutorTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/executor/StateExecutorTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020, 2023 Nordix Foundation + * Modifications Copyright (C) 2020, 2023-2024 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,14 +22,15 @@ 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.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; 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; @@ -39,8 +40,8 @@ import org.onap.policy.apex.model.policymodel.concepts.AxState; /** * Test task executor. */ -@RunWith(MockitoJUnitRunner.class) -public class StateExecutorTest { +@ExtendWith(MockitoExtension.class) +class StateExecutorTest { @Mock private ApexInternalContext internalContextMock; @@ -56,30 +57,29 @@ public class StateExecutorTest { /** * Set up mocking. */ - @Before - public void startMocking() { - + @BeforeEach + void startMocking() { Mockito.doReturn(new AxReferenceKey("Policy:0.0.1:PolName:State0")).when(axStateMock).getKey(); } @Test - public void testStateExecutor() { + void testStateExecutor() { StateExecutor executor = new StateExecutor(executorFactoryMock); executor.setContext(null, axStateMock, internalContextMock); assertEquals("Policy:0.0.1:PolName:State0", 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()); + assertNull(executor.getOutgoing()); assertEquals(axStateMock, 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"); diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/executor/StateFinalizerExecutorTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/executor/StateFinalizerExecutorTest.java index e140291b4..fff89de0d 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/executor/StateFinalizerExecutorTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/executor/StateFinalizerExecutorTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020, 2023 Nordix Foundation. + * Modifications Copyright (C) 2020, 2023-2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,16 +22,17 @@ 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.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import java.util.Map; import java.util.Properties; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.onap.policy.apex.context.ContextException; import org.onap.policy.apex.core.engine.ExecutorParameters; import org.onap.policy.apex.core.engine.context.ApexInternalContext; @@ -44,8 +45,8 @@ import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic; /** * Test task executor. */ -@RunWith(MockitoJUnitRunner.class) -public class StateFinalizerExecutorTest { +@ExtendWith(MockitoExtension.class) +class StateFinalizerExecutorTest { @Mock private Executor parentMock; @@ -64,8 +65,8 @@ public class StateFinalizerExecutorTest { /** * Set up mocking. */ - @Before - public void startMocking() { + @BeforeEach + void startMocking() { AxState state = new AxState(); state.getStateOutputs().put("ValidOutput", null); @@ -76,24 +77,24 @@ public class StateFinalizerExecutorTest { } @Test - public void testStateFinalizerExecutor() throws StateMachineException, ContextException { + void testStateFinalizerExecutor() throws StateMachineException, ContextException { DummyStateFinalizerExecutor executor = new DummyStateFinalizerExecutor(); executor.setContext(parentMock, stateFinalizerLogicMock, internalContextMock); assertEquals("State:0.0.1:StateName:StateSFL", executor.getKey().getId()); - assertEquals(null, executor.getExecutionContext()); + assertNull(executor.getExecutionContext()); assertEquals(parentMock, executor.getParent()); assertEquals(internalContextMock, executor.getContext()); - assertEquals(null, executor.getNext()); - assertEquals(null, executor.getIncoming()); - assertEquals(null, executor.getOutgoing()); + assertNull(executor.getNext()); + assertNull(executor.getIncoming()); + assertNull(executor.getOutgoing()); assertEquals(stateFinalizerLogicMock, 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::cleanUp) .hasMessage("cleanUp() not implemented on class"); @@ -130,7 +131,7 @@ public class StateFinalizerExecutorTest { executor.getExecutionContext().setSelectedStateOutputName("ThisOutputDoesNotExist"); assertThatThrownBy(() -> executor.executePost(true)) .hasMessage("execute-post: state finalizer logic \"null\" selected output state " - + "\"ThisOutputDoesNotExist\" that does not exsist on state \"NULL:0.0.0:NULL:NULL\""); + + "\"ThisOutputDoesNotExist\" that does not exsist on state \"NULL:0.0.0:NULL:NULL\""); executor.executePre(0, new Properties(), incomingEvent); executor.getExecutionContext().setSelectedStateOutputName("ValidOutput"); diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/executor/StateMachineExecutorTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/executor/StateMachineExecutorTest.java index 14cc89ee8..4af2f3c74 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/executor/StateMachineExecutorTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/executor/StateMachineExecutorTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020, 2023 Nordix Foundation. + * Modifications Copyright (C) 2020, 2023-2024 Nordix Foundation. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ @@ -24,21 +24,21 @@ 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 static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.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.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.onap.policy.apex.context.ContextException; import org.onap.policy.apex.context.parameters.SchemaParameters; import org.onap.policy.apex.core.engine.ExecutorParameters; @@ -66,8 +66,8 @@ import org.onap.policy.common.parameters.ParameterService; /** * Test task executor. */ -@RunWith(MockitoJUnitRunner.class) -public class StateMachineExecutorTest { +@ExtendWith(MockitoExtension.class) +class StateMachineExecutorTest { @Mock private ApexInternalContext internalContextMock; @@ -80,17 +80,15 @@ public class StateMachineExecutorTest { @Mock private EnEvent incomingEventMock; - private AxPolicy axPolicy = new AxPolicy(); - - private DummyTaskSelectExecutor dummyTsle; + private final AxPolicy axPolicy = new AxPolicy(); private DummyStateFinalizerExecutor dummySfle; /** * Set up mocking. */ - @Before - public void startMocking() { + @BeforeEach + void startMocking() { axPolicy.setKey(new AxArtifactKey("Policy:0.0.1")); AxReferenceKey state0Key = new AxReferenceKey(axPolicy.getKey(), "state0"); @@ -128,8 +126,8 @@ public class StateMachineExecutorTest { event0.getParameterMap().put("Event1Field0", event1Field0Definition); event0.getParameterMap().put("UnusedField", event1Field0Definition); - Mockito.doReturn(event0Key).when(incomingEventMock).getKey(); - Mockito.doReturn(event0).when(incomingEventMock).getAxEvent(); + Mockito.lenient().doReturn(event0Key).when(incomingEventMock).getKey(); + Mockito.lenient().doReturn(event0).when(incomingEventMock).getAxEvent(); state0.setTrigger(event0Key); state1.setTrigger(event1Key); @@ -169,26 +167,26 @@ public class StateMachineExecutorTest { AxStateTaskReference str1 = new AxStateTaskReference(str1Key, AxStateTaskOutputType.LOGIC, sflKey); state1.getTaskReferences().put(task1Key, str1); - Mockito.doReturn(new DummyTaskExecutor(true)).when(executorFactoryMock).getTaskExecutor(Mockito.any(), + Mockito.lenient().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(), + DummyTaskSelectExecutor dummyTsle = new DummyTaskSelectExecutor(true); + Mockito.lenient().doReturn(dummyTsle).when(executorFactoryMock).getTaskSelectionExecutor(Mockito.any(), Mockito.any(), Mockito.any()); dummySfle = new DummyStateFinalizerExecutor(true); - Mockito.doReturn(dummySfle).when(executorFactoryMock).getStateFinalizerExecutor(Mockito.any(), + Mockito.lenient().doReturn(dummySfle).when(executorFactoryMock).getStateFinalizerExecutor(Mockito.any(), Mockito.any(), Mockito.any()); } - @After - public void cleardown() { + @AfterEach + public void tearDown() { ParameterService.clear(); ModelService.clear(); } @Test - public void testStateMachineExecutor() throws StateMachineException, ContextException { + void testStateMachineExecutor() throws StateMachineException, ContextException { StateMachineExecutor executor = new StateMachineExecutor(executorFactoryMock, new AxArtifactKey("OwnerKey:0.0.1")); @@ -290,7 +288,7 @@ public class StateMachineExecutorTest { } @Test - public void testStateOutput() throws StateMachineException { + void testStateOutput() throws StateMachineException { final StateOutput output = new StateOutput(axPolicy.getStateMap().get("State0").getStateOutputs().get("stateOutput0")); assertNotNull(output); @@ -326,7 +324,7 @@ public class StateMachineExecutorTest { output.setEventFields(incomingFieldDefinitionMap, eventFieldMaps); StateOutput outputCopy = new StateOutput(axPolicy.getStateMap().get("State0") - .getStateOutputs().get("stateOutput0")); + .getStateOutputs().get("stateOutput0")); EnEvent incomingEvent = new EnEvent(new AxArtifactKey("Event0:0.0.1")); outputCopy.copyUnsetFields(incomingEvent); diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/executor/TaskExecutorTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/executor/TaskExecutorTest.java index 1c4ffe4fc..da64aed49 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/executor/TaskExecutorTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/executor/TaskExecutorTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020, 2023 Nordix Foundation. + * Modifications Copyright (C) 2020, 2023-2024 Nordix Foundation. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,9 +23,9 @@ 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.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import java.util.ArrayList; import java.util.HashMap; @@ -34,12 +34,12 @@ 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.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.onap.policy.apex.context.ContextException; import org.onap.policy.apex.core.engine.ExecutorParameters; import org.onap.policy.apex.core.engine.TaskParameters; @@ -56,10 +56,10 @@ import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic; import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter; /** - * Test task excutor. + * Test task executor. */ -@RunWith(MockitoJUnitRunner.class) -public class TaskExecutorTest { +@ExtendWith(MockitoExtension.class) +class TaskExecutorTest { @Mock private AxTask axTaskMock; @@ -88,27 +88,25 @@ public class TaskExecutorTest { @Mock private AxTaskLogic taskLogicMock; - private Map inFieldMap; private Map outFieldMap; private List taskParametersFromConfig; - private Map outEvents = new TreeMap<>(); + private final Map outEvents = new TreeMap<>(); /** * Set up mocking. */ - @Before - public void startMocking() { + @BeforeEach + void startMocking() { AxArtifactKey task0Key = new AxArtifactKey("Task0:0.0.1"); - Mockito.doReturn(task0Key).when(axTaskMock).getKey(); - Mockito.doReturn(task0Key.getId()).when(axTaskMock).getId(); + Mockito.lenient().doReturn(task0Key).when(axTaskMock).getKey(); + Mockito.lenient().doReturn(task0Key.getId()).when(axTaskMock).getId(); - inFieldMap = Map.of("InField0", axInputFieldMock, "InField1", axOptionalInputFieldMock); outFieldMap = new LinkedHashMap<>(); - outFieldMap.put("OutField0", axOutputFieldMock); outFieldMap.put("OutField1", axOptionalOutputFieldMock); + Map inFieldMap = Map.of("InField0", axInputFieldMock, "InField1", axOptionalInputFieldMock); AxEvent inEvent = new AxEvent(); inEvent.setParameterMap(inFieldMap); AxEvent outEvent = new AxEvent(new AxArtifactKey("outputEvent:1.0.0")); @@ -116,19 +114,19 @@ public class TaskExecutorTest { outEvents.put(outEvent.getKey().getName(), outEvent); AxArtifactKey schemaKey = new AxArtifactKey("Schema:0.0.1"); - Mockito.doReturn(schemaKey).when(axInputFieldMock).getSchema(); - Mockito.doReturn(schemaKey).when(axOptionalInputFieldMock).getSchema(); - Mockito.doReturn(schemaKey).when(axMissingOutputFieldMock).getSchema(); + Mockito.lenient().doReturn(schemaKey).when(axInputFieldMock).getSchema(); + Mockito.lenient().doReturn(schemaKey).when(axOptionalInputFieldMock).getSchema(); + Mockito.lenient().doReturn(schemaKey).when(axMissingOutputFieldMock).getSchema(); - Mockito.doReturn(true).when(axOptionalInputFieldMock).getOptional(); - Mockito.doReturn(false).when(axMissingOutputFieldMock).getOptional(); + Mockito.lenient().doReturn(true).when(axOptionalInputFieldMock).getOptional(); + Mockito.lenient().doReturn(false).when(axMissingOutputFieldMock).getOptional(); - Mockito.doReturn(taskLogicMock).when(axTaskMock).getTaskLogic(); + Mockito.lenient().doReturn(taskLogicMock).when(axTaskMock).getTaskLogic(); - Mockito.doReturn(inEvent).when(axTaskMock).getInputEvent(); - Mockito.doReturn(outEvents).when(axTaskMock).getOutputEvents(); + Mockito.lenient().doReturn(inEvent).when(axTaskMock).getInputEvent(); + Mockito.lenient().doReturn(outEvents).when(axTaskMock).getOutputEvents(); - Mockito.doReturn(new AxArtifactKey("Context:0.0.1")).when(internalContextMock).getKey(); + Mockito.lenient().doReturn(new AxArtifactKey("Context:0.0.1")).when(internalContextMock).getKey(); Map taskParameters = new HashMap<>(); taskParameters.put("parameterKey2", new AxTaskParameter(new AxReferenceKey(), "parameterOriginalValue2")); @@ -141,29 +139,29 @@ public class TaskExecutorTest { } @Test - public void testTaskExecutor() throws StateMachineException, ContextException { + void testTaskExecutor() throws StateMachineException, ContextException { final DummyTaskExecutor executor = new DummyTaskExecutor(); executor.setContext(null, axTaskMock, internalContextMock); assertEquals("Task0:0.0.1", executor.getKey().getId()); - assertEquals(null, executor.getExecutionContext()); - assertEquals(null, executor.getParent()); + assertNull(executor.getExecutionContext()); + 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()); + assertNull(executor.getOutgoing()); assertNotNull(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.cleanUp()).hasMessageContaining("cleanUp() not implemented on class"); + assertThatThrownBy(executor::cleanUp).hasMessageContaining("cleanUp() not implemented on class"); Mockito.doReturn(null).when(taskLogicMock).getLogic(); - assertThatThrownBy(() -> executor.prepare()).hasMessageContaining("task logic cannot be null."); + assertThatThrownBy(executor::prepare).hasMessageContaining("task logic cannot be null."); Mockito.doReturn("some task logic").when(taskLogicMock).getLogic(); @@ -220,19 +218,19 @@ public class TaskExecutorTest { } @Test - public void testTaskExecutorForTaskParameters() { - DummyTaskExecutor executorForParmeterTest = new DummyTaskExecutor(false); + void testTaskExecutorForTaskParameters() { + DummyTaskExecutor executorForParameterTest = new DummyTaskExecutor(false); - executorForParmeterTest.setContext(null, axTaskMock, internalContextMock); - executorForParmeterTest.updateTaskParameters(taskParametersFromConfig); - assertNotNull(executorForParmeterTest.getSubject().getTaskParameters()); + executorForParameterTest.setContext(null, axTaskMock, internalContextMock); + executorForParameterTest.updateTaskParameters(taskParametersFromConfig); + assertNotNull(executorForParameterTest.getSubject().getTaskParameters()); // taskId matched, parameter value updated with the new value assertEquals("parameterNewValue0", - executorForParmeterTest.getSubject().getTaskParameters().get("parameterKey0").getTaskParameterValue()); + executorForParameterTest.getSubject().getTaskParameters().get("parameterKey0").getTaskParameterValue()); // taskId mismatch, so the parameter is not updated in the task - assertNull(executorForParmeterTest.getSubject().getTaskParameters().get("parameterKey1")); + assertNull(executorForParameterTest.getSubject().getTaskParameters().get("parameterKey1")); // taskId is not available, so parameter is updated in the task assertEquals("parameterNewValue2", - executorForParmeterTest.getSubject().getTaskParameters().get("parameterKey2").getTaskParameterValue()); + executorForParameterTest.getSubject().getTaskParameters().get("parameterKey2").getTaskParameterValue()); } } diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/executor/TaskSelectExecutorTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/executor/TaskSelectExecutorTest.java index cf3c59fa9..6f0d74e66 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/executor/TaskSelectExecutorTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/executor/TaskSelectExecutorTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020, 2023 Nordix Foundation. + * Modifications Copyright (C) 2020, 2023-2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,17 +22,18 @@ 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.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; 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; @@ -46,8 +47,8 @@ import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic; /** * Test task executor. */ -@RunWith(MockitoJUnitRunner.class) -public class TaskSelectExecutorTest { +@ExtendWith(MockitoExtension.class) +class TaskSelectExecutorTest { @Mock private AxState axStateMock; @@ -66,8 +67,8 @@ public class TaskSelectExecutorTest { /** * Set up mocking. */ - @Before - public void startMocking() { + @BeforeEach + void startMocking() { AxReferenceKey state0Key = new AxReferenceKey("State0Parent:0.0.1:Parent:State0"); Mockito.doReturn(state0Key).when(axStateMock).getKey(); @@ -82,24 +83,24 @@ public class TaskSelectExecutorTest { } @Test - public void testTaskSelectionExecutor() throws StateMachineException { + void testTaskSelectionExecutor() throws StateMachineException { DummyTaskSelectExecutor executor = new DummyTaskSelectExecutor(); executor.setContext(null, axStateMock, internalContextMock); assertEquals("State0Parent:0.0.1:Parent:State0", executor.getKey().getId()); - assertEquals(null, executor.getExecutionContext()); - assertEquals(null, executor.getParent()); + assertNull(executor.getExecutionContext()); + 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()); + assertNull(executor.getOutgoing()); assertEquals(axStateMock, 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::cleanUp) .hasMessage("cleanUp() not implemented on class"); diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/AxStateFacadeTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/AxStateFacadeTest.java index 54013e943..6744534d7 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/AxStateFacadeTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/AxStateFacadeTest.java @@ -1,37 +1,37 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2023 Nordix Foundation. + * Modifications Copyright (C) 2023-2024 Nordix Foundation. * ================================================================================ * 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.context; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import java.util.LinkedHashMap; import java.util.Map; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; 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; @@ -42,8 +42,8 @@ import org.onap.policy.apex.model.policymodel.concepts.AxTasks; /** * Test the state facade. */ -@RunWith(MockitoJUnitRunner.class) -public class AxStateFacadeTest { +@ExtendWith(MockitoExtension.class) +class AxStateFacadeTest { @Mock private AxState axStateMock; @@ -53,32 +53,33 @@ public class AxStateFacadeTest { /** * Set up mocking. */ - @Before - public void startMocking() { + @BeforeEach + void startMocking() { AxReferenceKey stateKey = new AxReferenceKey("StateParent:0.0.1:ParentName:StateName"); Mockito.doReturn(stateKey).when(axStateMock).getKey(); AxArtifactKey task0Key = new AxArtifactKey("Task0:0.0.1"); Mockito.doReturn(task0Key).when(axStateMock).getDefaultTask(); - - Map taskReferences = new LinkedHashMap<>();; + + Map taskReferences = new LinkedHashMap<>(); + ; taskReferences.put(task0Key, null); Mockito.doReturn(taskReferences).when(axStateMock).getTaskReferences(); AxTasks tasks = new AxTasks(); tasks.getTaskMap().put(task0Key, axTaskMock); - + ModelService.registerModel(AxTasks.class, tasks); Mockito.doReturn(task0Key).when(axTaskMock).getKey(); } @Test - public void testAxStateFacade() { + void testAxStateFacade() { AxStateFacade stateFacade = new AxStateFacade(axStateMock); - + assertEquals("StateName", stateFacade.getStateName()); assertEquals("StateParent:0.0.1:ParentName:StateName", stateFacade.getId()); - + assertEquals("Task0", stateFacade.getDefaultTaskKey().getName()); assertNull(stateFacade.getTaskKey(null)); assertEquals("Task0", stateFacade.getTaskKey("Task0").getName()); diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/AxTaskFacadeTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/AxTaskFacadeTest.java index 634b3b35c..8d17d9f87 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/AxTaskFacadeTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/AxTaskFacadeTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020, 2023 Nordix Foundation + * Modifications Copyright (C) 2020, 2023-2024 Nordix Foundation * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,18 +23,18 @@ package org.onap.policy.apex.core.engine.executor.context; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import java.util.Map; import java.util.TreeMap; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.onap.policy.apex.context.parameters.ContextParameterConstants; import org.onap.policy.apex.context.parameters.SchemaParameters; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; @@ -52,8 +52,8 @@ import org.onap.policy.common.parameters.ParameterService; /** * Test the state facade. */ -@RunWith(MockitoJUnitRunner.class) -public class AxTaskFacadeTest { +@ExtendWith(MockitoExtension.class) +class AxTaskFacadeTest { @Mock private AxTask axTaskMock; @@ -72,8 +72,8 @@ public class AxTaskFacadeTest { /** * Set up mocking. */ - @Before - public void startMocking() { + @BeforeEach + void startMocking() { AxContextSchemas schemas = new AxContextSchemas(); AxArtifactKey stringTypeKey = new AxArtifactKey("StringType:0.0.1"); @@ -108,14 +108,14 @@ public class AxTaskFacadeTest { ParameterService.register(new SchemaParameters()); } - @After - public void teardown() { + @AfterEach + void teardown() { ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); ModelService.clear(); } @Test - public void testAxStateFacade() { + void testAxStateFacade() { AxTaskFacade taskFacade = new AxTaskFacade(axTaskMock); assertEquals("Task0", taskFacade.getTaskName()); @@ -123,18 +123,18 @@ public class AxTaskFacadeTest { assertThatThrownBy(() -> taskFacade.getInFieldSchemaHelper("InFieldDoesntExist")) .hasMessage("no incoming field with name \"InFieldDoesntExist\" " + "defined on task " - + "\"Task0:0.0.1\""); + + "\"Task0:0.0.1\""); assertThatThrownBy(() -> taskFacade.getOutFieldSchemaHelper("OutFieldDoesntExist")) .hasMessage("no outgoing field with name \"OutFieldDoesntExist\" " + "defined on task " - + "\"Task0:0.0.1\""); + + "\"Task0:0.0.1\""); assertNotNull(taskFacade.getInFieldSchemaHelper("InField0")); assertNotNull(taskFacade.getOutFieldSchemaHelper("OutField0")); assertThatThrownBy(() -> taskFacade.getInFieldSchemaHelper("InFieldBad")) .hasMessage("schema helper cannot be created for task field \"InFieldBad\" " - + "with key \"null\" with schema \"null\""); + + "with key \"null\" with schema \"null\""); assertThatThrownBy(() -> taskFacade.getOutFieldSchemaHelper("OutFieldBad")) .hasMessage("schema helper cannot be created for task field \"OutFieldBad\" " - + "with key \"null\" with schema \"null\""); + + "with key \"null\" with schema \"null\""); } } diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/DummyContextAlbum.java b/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/DummyContextAlbum.java index efb53a682..2570dde1d 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/DummyContextAlbum.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/DummyContextAlbum.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,7 +28,6 @@ import java.util.Set; import lombok.AllArgsConstructor; import org.apache.commons.lang3.NotImplementedException; import org.onap.policy.apex.context.ContextAlbum; -import org.onap.policy.apex.context.ContextException; import org.onap.policy.apex.context.SchemaHelper; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; @@ -172,7 +172,7 @@ public class DummyContextAlbum implements ContextAlbum { * {@inheritDoc}. */ @Override - public void lockForReading(String key) throws ContextException { + public void lockForReading(String key) { throw new NotImplementedException("Not implemented on dummy class"); } @@ -180,7 +180,7 @@ public class DummyContextAlbum implements ContextAlbum { * {@inheritDoc}. */ @Override - public void lockForWriting(String key) throws ContextException { + public void lockForWriting(String key) { throw new NotImplementedException("Not implemented on dummy class"); } @@ -188,7 +188,7 @@ public class DummyContextAlbum implements ContextAlbum { * {@inheritDoc}. */ @Override - public void unlockForReading(String key) throws ContextException { + public void unlockForReading(String key) { throw new NotImplementedException("Not implemented on dummy class"); } @@ -196,7 +196,7 @@ public class DummyContextAlbum implements ContextAlbum { * {@inheritDoc}. */ @Override - public void unlockForWriting(String key) throws ContextException { + public void unlockForWriting(String key) { throw new NotImplementedException("Not implemented on dummy class"); } @@ -220,7 +220,7 @@ public class DummyContextAlbum implements ContextAlbum { * {@inheritDoc}. */ @Override - public void flush() throws ContextException { + public void flush() { throw new NotImplementedException("Not implemented on dummy class"); } } diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/StateFinalizerExecutionContextTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/StateFinalizerExecutionContextTest.java index 9c6aa2f07..8c008ba2d 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/StateFinalizerExecutionContextTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/StateFinalizerExecutionContextTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020, 2023 Nordix Foundation + * Modifications Copyright (C) 2020, 2023-2024 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,19 +22,19 @@ package org.onap.policy.apex.core.engine.executor.context; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.onap.policy.apex.context.ContextAlbum; import org.onap.policy.apex.core.engine.context.ApexInternalContext; import org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor; @@ -45,8 +45,8 @@ import org.onap.policy.apex.model.policymodel.concepts.AxState; /** * Test Task Execution Context. */ -@RunWith(MockitoJUnitRunner.class) -public class StateFinalizerExecutionContextTest { +@ExtendWith(MockitoExtension.class) +class StateFinalizerExecutionContextTest { @Mock private StateFinalizerExecutor stateFinalizerExecutorMock; @@ -62,8 +62,8 @@ public class StateFinalizerExecutionContextTest { /** * Set up mocking. */ - @Before - public void startMocking() { + @BeforeEach + void startMocking() { Set contextAlbumReferences = new LinkedHashSet<>(); contextAlbumReferences.add(new AxArtifactKey(("AlbumKey0:0.0.1"))); @@ -86,12 +86,12 @@ public class StateFinalizerExecutionContextTest { } @Test - public void test() { + void test() { final Map fields = new LinkedHashMap<>(); final Set stateOutputNames = new LinkedHashSet<>(); StateFinalizerExecutionContext sfec = new StateFinalizerExecutionContext(stateFinalizerExecutorMock, 0, null, - axStateMock, fields, stateOutputNames, internalContextMock); + axStateMock, fields, stateOutputNames, internalContextMock); assertNotNull(sfec); sfec.setMessage("SFEC Message"); @@ -103,8 +103,8 @@ public class StateFinalizerExecutionContextTest { ContextAlbum contextAlbum = sfec.getContextAlbum("AlbumKey0"); assertEquals("AlbumKey0:0.0.1", contextAlbum.getKey().getId()); - assertThatThrownBy(() -> sfec.getContextAlbum("AlbumKeyNonExistant")) - .hasMessage("cannot find definition of context album \"AlbumKeyNonExistant\" " - + "on state \"Parent:0.0.1:ParentName:StateName\""); + assertThatThrownBy(() -> sfec.getContextAlbum("AlbumKeyNonExistent")) + .hasMessage("cannot find definition of context album \"AlbumKeyNonExistent\" " + + "on state \"Parent:0.0.1:ParentName:StateName\""); } } diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContextTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContextTest.java index 7f813cc01..3b76249b7 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContextTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContextTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020, 2023 Nordix Foundation. + * Modifications Copyright (C) 2020, 2023-2024 Nordix Foundation. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,8 +23,8 @@ package org.onap.policy.apex.core.engine.executor.context; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import java.util.HashMap; import java.util.LinkedHashMap; @@ -33,12 +33,12 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.onap.policy.apex.context.ContextAlbum; import org.onap.policy.apex.core.engine.context.ApexInternalContext; import org.onap.policy.apex.core.engine.executor.TaskExecutor; @@ -50,8 +50,8 @@ import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter; /** * Test Task Execution Context. */ -@RunWith(MockitoJUnitRunner.class) -public class TaskExecutionContextTest { +@ExtendWith(MockitoExtension.class) +class TaskExecutionContextTest { @Mock private TaskExecutor taskExecutorMock; @@ -67,8 +67,8 @@ public class TaskExecutionContextTest { /** * Set up mocking. */ - @Before - public void startMocking() { + @BeforeEach + void startMocking() { Set contextAlbumReferences = new LinkedHashSet<>(); contextAlbumReferences.add(new AxArtifactKey(("AlbumKey0:0.0.1"))); @@ -95,7 +95,7 @@ public class TaskExecutionContextTest { } @Test - public void test() { + void test() { final Map inFields = new LinkedHashMap<>(); final List> outFieldsList = new LinkedList<>(); @@ -113,7 +113,7 @@ public class TaskExecutionContextTest { assertEquals("parameterValue1", parameters.get("parameterKey1")); assertEquals("parameterValue2", parameters.get("parameterKey2")); - assertThatThrownBy(() -> tec.getContextAlbum("AlbumKeyNonExistant")) - .hasMessageContaining("cannot find definition of context album \"AlbumKeyNonExistant\" on task \"null\""); + assertThatThrownBy(() -> tec.getContextAlbum("AlbumKeyNonExistent")) + .hasMessageContaining("cannot find definition of context album \"AlbumKeyNonExistent\" on task \"null\""); } } diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/TaskSelectionExecutionContextTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/TaskSelectionExecutionContextTest.java index 84f4817e0..b21dae10b 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/TaskSelectionExecutionContextTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/executor/context/TaskSelectionExecutionContextTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020, 2023 Nordix Foundation + * Modifications Copyright (C) 2020, 2023-2024 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,19 +22,19 @@ package org.onap.policy.apex.core.engine.executor.context; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.onap.policy.apex.context.ContextAlbum; import org.onap.policy.apex.core.engine.context.ApexInternalContext; import org.onap.policy.apex.core.engine.event.EnEvent; @@ -46,8 +46,8 @@ import org.onap.policy.apex.model.policymodel.concepts.AxState; /** * Test Task Execution Context. */ -@RunWith(MockitoJUnitRunner.class) -public class TaskSelectionExecutionContextTest { +@ExtendWith(MockitoExtension.class) +class TaskSelectionExecutionContextTest { @Mock private TaskSelectExecutor taskSelectExecutorMock; @@ -66,8 +66,8 @@ public class TaskSelectionExecutionContextTest { /** * Set up mocking. */ - @Before - public void startMocking() { + @BeforeEach + void startMocking() { Set contextAlbumReferences = new LinkedHashSet<>(); contextAlbumReferences.add(new AxArtifactKey(("AlbumKey0:0.0.1"))); @@ -90,11 +90,11 @@ public class TaskSelectionExecutionContextTest { } @Test - public void test() { + void test() { final AxArtifactKey outgoingEventKey = new AxArtifactKey("OutEvent:0.0.1"); TaskSelectionExecutionContext tsec = new TaskSelectionExecutionContext(taskSelectExecutorMock, 0, axStateMock, - incomingEventMock, outgoingEventKey, internalContextMock); + incomingEventMock, outgoingEventKey, internalContextMock); assertNotNull(tsec); tsec.setMessage("TSEC Message"); @@ -103,8 +103,8 @@ public class TaskSelectionExecutionContextTest { ContextAlbum contextAlbum = tsec.getContextAlbum("AlbumKey0"); assertEquals("AlbumKey0:0.0.1", contextAlbum.getKey().getId()); - assertThatThrownBy(() -> tsec.getContextAlbum("AlbumKeyNonExistant")) - .hasMessage("cannot find definition of context album \"AlbumKeyNonExistant\" " - + "on state \"Parent:0.0.1:ParentName:StateName\""); + assertThatThrownBy(() -> tsec.getContextAlbum("AlbumKeyNonExistent")) + .hasMessage("cannot find definition of context album \"AlbumKeyNonExistent\" " + + "on state \"Parent:0.0.1:ParentName:StateName\""); } } diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/executor/exception/StateMachineRuntimeExceptionTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/executor/exception/StateMachineRuntimeExceptionTest.java index 84f330308..35687c18d 100644 --- a/core/src/test/java/org/onap/policy/apex/core/engine/executor/exception/StateMachineRuntimeExceptionTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/engine/executor/exception/StateMachineRuntimeExceptionTest.java @@ -1,41 +1,41 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2024 Nordix Foundation. * ================================================================================ * 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.exception; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Test state machine runtime exception. - * */ -public class StateMachineRuntimeExceptionTest { +class StateMachineRuntimeExceptionTest { @Test - public void testException() { + void testException() { StateMachineRuntimeException smre = new StateMachineRuntimeException("Exception Message"); assertEquals("Exception Message", smre.getMessage()); - + smre = new StateMachineRuntimeException("Exception Message", new IOException()); assertEquals("Exception Message", smre.getMessage()); } diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/executor/impl/ExceutorFactoryImplTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/executor/impl/ExceutorFactoryImplTest.java deleted file mode 100644 index c8aa7eb08..000000000 --- a/core/src/test/java/org/onap/policy/apex/core/engine/executor/impl/ExceutorFactoryImplTest.java +++ /dev/null @@ -1,239 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020, 2023 Nordix Foundation - * ================================================================================ - * 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.impl; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - -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.core.engine.EngineParameters; -import org.onap.policy.apex.core.engine.ExecutorParameters; -import org.onap.policy.apex.core.engine.context.ApexInternalContext; -import org.onap.policy.apex.core.engine.executor.Executor; -import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; -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.AxTask; -import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic; -import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic; -import org.onap.policy.common.parameters.ParameterService; - -/** - * Test the executor factory implementation. - * - */ -@RunWith(MockitoJUnitRunner.class) -public class ExceutorFactoryImplTest { - @Mock - private ApexInternalContext internalContextMock; - - @Mock - private AxState stateMock; - - @Mock - private AxTaskSelectionLogic tslMock; - - @Mock - private AxTask taskMock; - - @Mock - private AxTaskLogic tlMock; - - @Mock - private AxStateFinalizerLogic sflMock; - - @Mock - private Executor parentMock; - - private ExecutorParameters executorPars; - - /** - * Set up mocking. - */ - @Before - public void startMocking() { - - Mockito.doReturn(tslMock).when(stateMock).getTaskSelectionLogic(); - Mockito.doReturn("Dummy").when(tslMock).getLogicFlavour(); - - Mockito.doReturn(tlMock).when(taskMock).getTaskLogic(); - Mockito.doReturn("Dummy").when(tlMock).getLogicFlavour(); - - Mockito.doReturn("Dummy").when(sflMock).getLogicFlavour(); - } - - @After - public void clearPars() { - ParameterService.clear(); - } - - @Test - public void testExecutorFactoryImplGood() throws StateMachineException { - setGoodPars(); - - ExecutorFactoryImpl factory = null; - - factory = new ExecutorFactoryImpl(); - - Mockito.doReturn(true).when(stateMock).checkSetTaskSelectionLogic(); - assertNotNull(factory.getTaskSelectionExecutor(null, stateMock, internalContextMock)); - Mockito.doReturn(false).when(stateMock).checkSetTaskSelectionLogic(); - assertNull(factory.getTaskSelectionExecutor(null, stateMock, internalContextMock)); - - assertNotNull(factory.getTaskExecutor(null, taskMock, internalContextMock)); - - assertNotNull(factory.getStateFinalizerExecutor(parentMock, sflMock, internalContextMock)); - } - - @Test - public void testExecutorFactoryImplNonExistant() { - setNonExistantPars(); - - assertThatThrownBy(() -> new ExecutorFactoryImpl()) - .hasMessage("Apex executor class not found for executor plugin " - + "\"org.onap.policy.apex.core.engine.executor.BadTaskExecutor\""); - executorPars.setTaskExecutorPluginClass(null); - - assertThatThrownBy(() -> new ExecutorFactoryImpl()) - .hasMessage("Apex executor class not found for executor plugin " - + "\"org.onap.policy.apex.core.engine.executor.BadTaskSelectExecutor\""); - executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyTaskExecutor"); - - assertThatThrownBy(() -> new ExecutorFactoryImpl()) - .hasMessage("Apex executor class not found for executor plugin " - + "\"org.onap.policy.apex.core.engine.executor.BadTaskSelectExecutor\""); - executorPars.setTaskSelectionExecutorPluginClass( - "org.onap.policy.apex.core.engine.executor.DummyTaskSelectExecutor"); - - assertThatThrownBy(() -> new ExecutorFactoryImpl()) - .hasMessage("Apex executor class not found for executor plugin " - + "\"org.onap.policy.apex.core.engine.executor.BadStateFinalizerExecutor\""); - } - - @Test - public void testExecutorFactoryImplBad() { - setBadPars(); - - assertThatThrownBy(() -> new ExecutorFactoryImpl()) - .hasMessage("Specified Apex executor plugin class \"java.lang.String\" " - + "does not implment the Executor interface"); - executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyTaskExecutor"); - - assertThatThrownBy(() -> new ExecutorFactoryImpl()) - .hasMessage("Specified Apex executor plugin class \"java.lang.String\" " - + "does not implment the Executor interface"); - executorPars.setTaskSelectionExecutorPluginClass( - "org.onap.policy.apex.core.engine.executor.DummyTaskSelectExecutor"); - - assertThatThrownBy(() -> new ExecutorFactoryImpl()) - .hasMessage("Specified Apex executor plugin class \"java.lang.String\" " - + "does not implment the Executor interface"); - } - - @Test - public void testExecutorFactoryCreateErrors() throws StateMachineException { - setGoodPars(); - - executorPars.setTaskExecutorPluginClass(null); - - final ExecutorFactoryImpl factory = new ExecutorFactoryImpl(); - - assertThatThrownBy(() -> factory.getTaskExecutor(null, taskMock, internalContextMock)) - .hasMessage("Executor plugin class not defined for \"Dummy\" executor of type " - + "\"org.onap.policy.apex.core.engine.executor.TaskExecutor\""); - executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyFailingTaskExecutor"); - - ExecutorFactoryImpl factoryInitError = new ExecutorFactoryImpl(); - - assertThatThrownBy(() -> factoryInitError.getTaskExecutor(null, taskMock, internalContextMock)) - .hasMessage("Instantiation error on \"Dummy\" executor of type " - + "\"org.onap.policy.apex.core.engine.executor.DummyFailingTaskExecutor\""); - executorPars.setTaskExecutorPluginClass( - "org.onap.policy.apex.core.engine.executor.DummyStateFinalizerExecutor"); - - ExecutorFactoryImpl factoryDummyError = new ExecutorFactoryImpl(); - - assertThatThrownBy(() -> factoryDummyError.getTaskExecutor(null, taskMock, internalContextMock)) - .hasMessage("Executor on \"Dummy\" " - + "of type \"class org.onap.policy.apex.core.engine.executor.DummyStateFinalizerExecutor\"" - + " is not an instance of \"org.onap.policy.apex.core.engine.executor.TaskExecutor\""); - } - - /** - * Set up good parameters. - */ - private void setGoodPars() { - executorPars = new ExecutorParameters(); - executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyTaskExecutor"); - executorPars.setTaskSelectionExecutorPluginClass( - "org.onap.policy.apex.core.engine.executor.DummyTaskSelectExecutor"); - executorPars.setStateFinalizerExecutorPluginClass( - "org.onap.policy.apex.core.engine.executor.DummyStateFinalizerExecutor"); - - EngineParameters enginePars = new EngineParameters(); - enginePars.getExecutorParameterMap().put("Dummy", executorPars); - - ParameterService.register(enginePars); - ParameterService.register(executorPars); - } - - /** - * Set up non existant parameters. - */ - private void setNonExistantPars() { - executorPars = new ExecutorParameters(); - executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.BadTaskExecutor"); - executorPars.setTaskSelectionExecutorPluginClass( - "org.onap.policy.apex.core.engine.executor.BadTaskSelectExecutor"); - executorPars.setStateFinalizerExecutorPluginClass( - "org.onap.policy.apex.core.engine.executor.BadStateFinalizerExecutor"); - - EngineParameters enginePars = new EngineParameters(); - enginePars.getExecutorParameterMap().put("Dummy", executorPars); - - ParameterService.register(enginePars, true); - ParameterService.register(executorPars, true); - } - - /** - * Set up bad parameters. - */ - private void setBadPars() { - executorPars = new ExecutorParameters(); - executorPars.setTaskExecutorPluginClass("java.lang.String"); - executorPars.setTaskSelectionExecutorPluginClass("java.lang.String"); - executorPars.setStateFinalizerExecutorPluginClass("java.lang.String"); - - EngineParameters enginePars = new EngineParameters(); - enginePars.getExecutorParameterMap().put("Dummy", executorPars); - - ParameterService.register(enginePars, true); - ParameterService.register(executorPars, true); - } -} diff --git a/core/src/test/java/org/onap/policy/apex/core/engine/executor/impl/ExecutorFactoryImplTest.java b/core/src/test/java/org/onap/policy/apex/core/engine/executor/impl/ExecutorFactoryImplTest.java new file mode 100644 index 000000000..f7020dd21 --- /dev/null +++ b/core/src/test/java/org/onap/policy/apex/core/engine/executor/impl/ExecutorFactoryImplTest.java @@ -0,0 +1,238 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020, 2023-2024 Nordix Foundation. + * ================================================================================ + * 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.impl; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.policy.apex.core.engine.EngineParameters; +import org.onap.policy.apex.core.engine.ExecutorParameters; +import org.onap.policy.apex.core.engine.context.ApexInternalContext; +import org.onap.policy.apex.core.engine.executor.Executor; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; +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.AxTask; +import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic; +import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic; +import org.onap.policy.common.parameters.ParameterService; + +/** + * Test the executor factory implementation. + */ +@ExtendWith(MockitoExtension.class) +class ExecutorFactoryImplTest { + @Mock + private ApexInternalContext internalContextMock; + + @Mock + private AxState stateMock; + + @Mock + private AxTaskSelectionLogic tslMock; + + @Mock + private AxTask taskMock; + + @Mock + private AxTaskLogic tlMock; + + @Mock + private AxStateFinalizerLogic sflMock; + + @Mock + private Executor parentMock; + + private ExecutorParameters executorPars; + + /** + * Set up mocking. + */ + @BeforeEach + void startMocking() { + + Mockito.lenient().doReturn(tslMock).when(stateMock).getTaskSelectionLogic(); + Mockito.lenient().doReturn("Dummy").when(tslMock).getLogicFlavour(); + + Mockito.lenient().doReturn(tlMock).when(taskMock).getTaskLogic(); + Mockito.lenient().doReturn("Dummy").when(tlMock).getLogicFlavour(); + + Mockito.lenient().doReturn("Dummy").when(sflMock).getLogicFlavour(); + } + + @AfterEach + void clearPars() { + ParameterService.clear(); + } + + @Test + void testExecutorFactoryImplGood() throws StateMachineException { + setGoodPars(); + + ExecutorFactoryImpl factory; + + factory = new ExecutorFactoryImpl(); + + Mockito.doReturn(true).when(stateMock).checkSetTaskSelectionLogic(); + assertNotNull(factory.getTaskSelectionExecutor(null, stateMock, internalContextMock)); + Mockito.doReturn(false).when(stateMock).checkSetTaskSelectionLogic(); + assertNull(factory.getTaskSelectionExecutor(null, stateMock, internalContextMock)); + + assertNotNull(factory.getTaskExecutor(null, taskMock, internalContextMock)); + + assertNotNull(factory.getStateFinalizerExecutor(parentMock, sflMock, internalContextMock)); + } + + @Test + void testExecutorFactoryImplNonExistent() { + setNonExistentPars(); + + assertThatThrownBy(ExecutorFactoryImpl::new) + .hasMessage("Apex executor class not found for executor plugin " + + "\"org.onap.policy.apex.core.engine.executor.BadTaskExecutor\""); + executorPars.setTaskExecutorPluginClass(null); + + assertThatThrownBy(ExecutorFactoryImpl::new) + .hasMessage("Apex executor class not found for executor plugin " + + "\"org.onap.policy.apex.core.engine.executor.BadTaskSelectExecutor\""); + executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyTaskExecutor"); + + assertThatThrownBy(ExecutorFactoryImpl::new) + .hasMessage("Apex executor class not found for executor plugin " + + "\"org.onap.policy.apex.core.engine.executor.BadTaskSelectExecutor\""); + executorPars.setTaskSelectionExecutorPluginClass( + "org.onap.policy.apex.core.engine.executor.DummyTaskSelectExecutor"); + + assertThatThrownBy(ExecutorFactoryImpl::new) + .hasMessage("Apex executor class not found for executor plugin " + + "\"org.onap.policy.apex.core.engine.executor.BadStateFinalizerExecutor\""); + } + + @Test + void testExecutorFactoryImplBad() { + setBadPars(); + + assertThatThrownBy(ExecutorFactoryImpl::new) + .hasMessage("Specified Apex executor plugin class \"java.lang.String\" " + + "does not implement the Executor interface"); + executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyTaskExecutor"); + + assertThatThrownBy(ExecutorFactoryImpl::new) + .hasMessage("Specified Apex executor plugin class \"java.lang.String\" " + + "does not implement the Executor interface"); + executorPars.setTaskSelectionExecutorPluginClass( + "org.onap.policy.apex.core.engine.executor.DummyTaskSelectExecutor"); + + assertThatThrownBy(ExecutorFactoryImpl::new) + .hasMessage("Specified Apex executor plugin class \"java.lang.String\" " + + "does not implement the Executor interface"); + } + + @Test + void testExecutorFactoryCreateErrors() throws StateMachineException { + setGoodPars(); + + executorPars.setTaskExecutorPluginClass(null); + + final ExecutorFactoryImpl factory = new ExecutorFactoryImpl(); + + assertThatThrownBy(() -> factory.getTaskExecutor(null, taskMock, internalContextMock)) + .hasMessage("Executor plugin class not defined for \"Dummy\" executor of type " + + "\"org.onap.policy.apex.core.engine.executor.TaskExecutor\""); + executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyFailingTaskExecutor"); + + ExecutorFactoryImpl factoryInitError = new ExecutorFactoryImpl(); + + assertThatThrownBy(() -> factoryInitError.getTaskExecutor(null, taskMock, internalContextMock)) + .hasMessage("Instantiation error on \"Dummy\" executor of type " + + "\"org.onap.policy.apex.core.engine.executor.DummyFailingTaskExecutor\""); + executorPars.setTaskExecutorPluginClass( + "org.onap.policy.apex.core.engine.executor.DummyStateFinalizerExecutor"); + + ExecutorFactoryImpl factoryDummyError = new ExecutorFactoryImpl(); + + assertThatThrownBy(() -> factoryDummyError.getTaskExecutor(null, taskMock, internalContextMock)) + .hasMessage("Executor on \"Dummy\" " + + "of type \"class org.onap.policy.apex.core.engine.executor.DummyStateFinalizerExecutor\"" + + " is not an instance of \"org.onap.policy.apex.core.engine.executor.TaskExecutor\""); + } + + /** + * Set up good parameters. + */ + private void setGoodPars() { + executorPars = new ExecutorParameters(); + executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyTaskExecutor"); + executorPars.setTaskSelectionExecutorPluginClass( + "org.onap.policy.apex.core.engine.executor.DummyTaskSelectExecutor"); + executorPars.setStateFinalizerExecutorPluginClass( + "org.onap.policy.apex.core.engine.executor.DummyStateFinalizerExecutor"); + + EngineParameters enginePars = new EngineParameters(); + enginePars.getExecutorParameterMap().put("Dummy", executorPars); + + ParameterService.register(enginePars); + ParameterService.register(executorPars); + } + + /** + * Set up non existent parameters. + */ + private void setNonExistentPars() { + executorPars = new ExecutorParameters(); + executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.BadTaskExecutor"); + executorPars.setTaskSelectionExecutorPluginClass( + "org.onap.policy.apex.core.engine.executor.BadTaskSelectExecutor"); + executorPars.setStateFinalizerExecutorPluginClass( + "org.onap.policy.apex.core.engine.executor.BadStateFinalizerExecutor"); + + EngineParameters enginePars = new EngineParameters(); + enginePars.getExecutorParameterMap().put("Dummy", executorPars); + + ParameterService.register(enginePars, true); + ParameterService.register(executorPars, true); + } + + /** + * Set up bad parameters. + */ + private void setBadPars() { + executorPars = new ExecutorParameters(); + executorPars.setTaskExecutorPluginClass("java.lang.String"); + executorPars.setTaskSelectionExecutorPluginClass("java.lang.String"); + executorPars.setStateFinalizerExecutorPluginClass("java.lang.String"); + + EngineParameters enginePars = new EngineParameters(); + enginePars.getExecutorParameterMap().put("Dummy", executorPars); + + ParameterService.register(enginePars, true); + ParameterService.register(executorPars, true); + } +} diff --git a/core/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTest.java b/core/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTest.java index 35ad9386a..356b800a8 100644 --- a/core/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTest.java +++ b/core/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. + * Modifications Copyright (C) 2020, 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,12 +21,12 @@ package org.onap.policy.apex.core.infrastructure.threading; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; import java.util.List; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; @@ -35,7 +35,7 @@ import org.slf4j.ext.XLoggerFactory; * * @author Liam Fallon (liam.fallon@ericsson.com) */ -public class ThreadingTest { +class ThreadingTest { private static final String LOCAL_NAME = "localName"; // Logger for this class @@ -45,18 +45,18 @@ public class ThreadingTest { * Test thread factory initialization. */ @Test - public void testThreadFactoryInitialization() { + void testThreadFactoryInitialization() { final ApplicationThreadFactory objUnderTest = new ApplicationThreadFactory(LOCAL_NAME, 0); - assertNotNull("Failed to create ApplicationThreadFactory threadFactory0", objUnderTest); + assertNotNull(objUnderTest, "Failed to create ApplicationThreadFactory threadFactory0"); logger.debug(objUnderTest.toString()); - assertTrue("Failed to name ApplicationThreadFactory threadFactory0", - objUnderTest.getName().startsWith("Apex-" + LOCAL_NAME)); + assertTrue(objUnderTest.getName().startsWith("Apex-" + LOCAL_NAME), + "Failed to name ApplicationThreadFactory threadFactory0"); final ApplicationThreadFactory objUnderTest1 = new ApplicationThreadFactory(LOCAL_NAME, 0); - assertNotNull("Failed to create ApplicationThreadFactory threadFactory1", objUnderTest1); + assertNotNull(objUnderTest1, "Failed to create ApplicationThreadFactory threadFactory1"); logger.debug(objUnderTest1.toString()); - assertTrue("Failed to name ApplicationThreadFactory threadFactory1", - objUnderTest1.getName().startsWith("Apex-" + LOCAL_NAME)); + assertTrue(objUnderTest1.getName().startsWith("Apex-" + LOCAL_NAME), + "Failed to name ApplicationThreadFactory threadFactory1"); testThreadFactory(objUnderTest); testThreadFactory(objUnderTest1); -- cgit 1.2.3-korg