From aac3c1d70c439910e25a2634f872f497486509ec Mon Sep 17 00:00:00 2001 From: waynedunican Date: Mon, 13 Jul 2020 11:13:53 +0100 Subject: Replace try/catch with assertj Replaced try/catch blocks with assertj assertions in apex-pdp. Last batch of changes Issue-ID: POLICY-2451 Change-Id: I39bd02fdbd8389818dcf1b786189c1e344ddcea5 Signed-off-by: waynedunican --- .../mvel/MvelStateFinalizerExecutorTest.java | 97 ++++++---------------- .../executor/mvel/MvelTaskExecutorTest.java | 77 +++++------------ .../executor/mvel/MvelTaskSelectExecutorTest.java | 79 +++++------------- 3 files changed, 66 insertions(+), 187 deletions(-) (limited to 'plugins/plugins-executor/plugins-executor-mvel') diff --git a/plugins/plugins-executor/plugins-executor-mvel/src/test/java/org/onap/policy/apex/plugins/executor/mvel/MvelStateFinalizerExecutorTest.java b/plugins/plugins-executor/plugins-executor-mvel/src/test/java/org/onap/policy/apex/plugins/executor/mvel/MvelStateFinalizerExecutorTest.java index 54ab78e61..2cd19c6f6 100644 --- a/plugins/plugins-executor/plugins-executor-mvel/src/test/java/org/onap/policy/apex/plugins/executor/mvel/MvelStateFinalizerExecutorTest.java +++ b/plugins/plugins-executor/plugins-executor-mvel/src/test/java/org/onap/policy/apex/plugins/executor/mvel/MvelStateFinalizerExecutorTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2020 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,9 +21,9 @@ package org.onap.policy.apex.plugins.executor.mvel; +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.fail; import java.util.Properties; import org.junit.After; @@ -80,30 +81,18 @@ public class MvelStateFinalizerExecutorTest { } @Test - public void testJavaStateFinalizerExecutor() { + public void testJavaStateFinalizerExecutor() throws StateMachineException, ContextException { MvelStateFinalizerExecutor msfe = new MvelStateFinalizerExecutor(); assertNotNull(msfe); - try { - msfe.prepare(); - fail("test should throw an exception here"); - } catch (Exception msfeException) { - assertEquals(java.lang.NullPointerException.class, msfeException.getClass()); - } - + assertThatThrownBy(msfe::prepare) + .isInstanceOf(java.lang.NullPointerException.class); ApexInternalContext internalContext = null; - try { - internalContext = new ApexInternalContext(new AxPolicyModel()); - } catch (ContextException e) { - fail("test should not throw an exception here"); - } + internalContext = new ApexInternalContext(new AxPolicyModel()); StateExecutor parentStateExcutor = null; - try { - parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl()); - } catch (StateMachineException e) { - fail("test should not throw an exception here"); - } + + parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl()); AxState state = new AxState(); parentStateExcutor.setContext(null, state, internalContext); @@ -111,69 +100,31 @@ public class MvelStateFinalizerExecutorTest { msfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext); stateFinalizerLogic.setLogic("x > 1 2 ()"); - try { - msfe.prepare(); - fail("test should throw an exception here"); - } catch (Exception msfeException) { - assertEquals("failed to compile MVEL code for state NULL:0.0.0:NULL:NULL", msfeException.getMessage()); - } - + assertThatThrownBy(msfe::prepare) + .hasMessage("failed to compile MVEL code for state NULL:0.0.0:NULL:NULL"); stateFinalizerLogic.setLogic("java.lang.String"); - try { - msfe.prepare(); - } catch (Exception msfeException) { - fail("test should not throw an exception here"); - } - - try { - msfe.execute(-1, new Properties(), null); - fail("test should throw an exception here"); - } catch (Exception msfeException) { - assertEquals("failed to execute MVEL code for state NULL:0.0.0:NULL:NULL", - msfeException.getMessage()); - } + msfe.prepare(); + assertThatThrownBy(() -> msfe.execute(-1, new Properties(), null)) + .hasMessage("failed to execute MVEL code for state NULL:0.0.0:NULL:NULL"); AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1")); EnEvent event = new EnEvent(axEvent); - try { - msfe.execute(-1, new Properties(), event); - fail("test should throw an exception here"); - } catch (Exception msfeException) { - assertEquals("failed to execute MVEL code for state NULL:0.0.0:NULL:NULL", - msfeException.getMessage()); - } - + assertThatThrownBy(() -> msfe.execute(-1, new Properties(), event)) + .hasMessage("failed to execute MVEL code for state NULL:0.0.0:NULL:NULL"); stateFinalizerLogic.setLogic("executionId !=-1"); - try { - msfe.prepare(); - msfe.execute(-1, new Properties(), event); - fail("test should throw an exception here"); - } catch (Exception msfeException) { - assertEquals( - "execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:NULL:NULL\" " - + "on finalizer logic NULL:0.0.0:NULL:NULL", - msfeException.getMessage()); - } - + msfe.prepare(); + assertThatThrownBy(() -> msfe.execute(-1, new Properties(), event)) + .hasMessage("execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:" + + "NULL:NULL\" on finalizer logic NULL:0.0.0:NULL:NULL"); stateFinalizerLogic.setLogic( "if (executionId == -1) {return false;}setSelectedStateOutputName(\"SelectedOutputIsMe\");" + "return true;"); state.getStateOutputs().put("SelectedOutputIsMe", null); - try { - msfe.prepare(); - String stateOutput = msfe.execute(0, new Properties(), event); - assertEquals("SelectedOutputIsMe", stateOutput); - } catch (Exception msfeException) { - LOGGER.warn("Unexpected exception happened here.", msfeException); - fail("test should not throw an exception here"); - } finally { - try { - msfe.cleanUp(); - } catch (StateMachineException msfeException) { - LOGGER.warn("Unexpected exception happened here.", msfeException); - fail("test should not throw an exception here"); - } - } + + msfe.prepare(); + String stateOutput = msfe.execute(0, new Properties(), event); + assertEquals("SelectedOutputIsMe", stateOutput); + msfe.cleanUp(); } } diff --git a/plugins/plugins-executor/plugins-executor-mvel/src/test/java/org/onap/policy/apex/plugins/executor/mvel/MvelTaskExecutorTest.java b/plugins/plugins-executor/plugins-executor-mvel/src/test/java/org/onap/policy/apex/plugins/executor/mvel/MvelTaskExecutorTest.java index e4169274a..a4d2cbf2b 100644 --- a/plugins/plugins-executor/plugins-executor-mvel/src/test/java/org/onap/policy/apex/plugins/executor/mvel/MvelTaskExecutorTest.java +++ b/plugins/plugins-executor/plugins-executor-mvel/src/test/java/org/onap/policy/apex/plugins/executor/mvel/MvelTaskExecutorTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2020 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,9 +21,9 @@ package org.onap.policy.apex.plugins.executor.mvel; +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.fail; import java.util.HashMap; import java.util.Map; @@ -36,6 +37,7 @@ import org.onap.policy.apex.context.parameters.DistributorParameters; import org.onap.policy.apex.context.parameters.LockManagerParameters; import org.onap.policy.apex.context.parameters.PersistorParameters; import org.onap.policy.apex.core.engine.context.ApexInternalContext; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; import org.onap.policy.apex.model.policymodel.concepts.AxTask; import org.onap.policy.common.parameters.ParameterService; @@ -66,74 +68,37 @@ public class MvelTaskExecutorTest { } @Test - public void testMvelTaskExecutor() { + public void testMvelTaskExecutor() throws StateMachineException, ContextException { MvelTaskExecutor mte = new MvelTaskExecutor(); assertNotNull(mte); - try { - mte.prepare(); - fail("test should throw an exception here"); - } catch (Exception mteException) { - assertEquals(java.lang.NullPointerException.class, mteException.getClass()); - } - + assertThatThrownBy(mte::prepare) + .isInstanceOf(java.lang.NullPointerException.class); AxTask task = new AxTask(); ApexInternalContext internalContext = null; - try { - internalContext = new ApexInternalContext(new AxPolicyModel()); - } catch (ContextException e) { - fail("test should not throw an exception here"); - } + internalContext = new ApexInternalContext(new AxPolicyModel()); mte.setContext(null, task, internalContext); task.getTaskLogic().setLogic("x > 1 2 ()"); - try { - mte.prepare(); - fail("test should throw an exception here"); - } catch (Exception mteException) { - assertEquals("failed to compile MVEL code for task NULL:0.0.0", mteException.getMessage()); - } - + assertThatThrownBy(mte::prepare) + .hasMessage("failed to compile MVEL code for task NULL:0.0.0"); task.getTaskLogic().setLogic("x"); - try { - mte.prepare(); - } catch (Exception mteException) { - fail("test should not throw an exception here"); - } - - try { - mte.execute(-1, new Properties(), null); - fail("test should throw an exception here"); - } catch (Exception mteException) { - assertEquals(java.lang.NullPointerException.class, mteException.getClass()); - } + mte.prepare(); + assertThatThrownBy(() -> mte.execute(-1, new Properties(), null)) + .isInstanceOf(java.lang.NullPointerException.class); Map incomingParameters = new HashMap<>(); - try { - mte.execute(-1, new Properties(), incomingParameters); - fail("test should throw an exception here"); - } catch (Exception mteException) { - assertEquals("failed to execute MVEL code for task NULL:0.0.0", mteException.getMessage()); - } - + assertThatThrownBy(() -> mte.execute(-1, new Properties(), incomingParameters)) + .hasMessage("failed to execute MVEL code for task NULL:0.0.0"); task.getTaskLogic().setLogic("executionId != -1"); - try { - mte.prepare(); - mte.execute(-1, new Properties(), incomingParameters); - fail("test should throw an exception here"); - } catch (Exception mteException) { - assertEquals("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0", - mteException.getMessage()); - } + mte.prepare(); + assertThatThrownBy(() -> mte.execute(-1, new Properties(), incomingParameters)) + .hasMessage("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0"); - try { - mte.prepare(); - Map returnMap = mte.execute(0, new Properties(), incomingParameters); - assertEquals(0, returnMap.size()); - mte.cleanUp(); - } catch (Exception mteException) { - fail("test should not throw an exception here"); - } + mte.prepare(); + Map returnMap = mte.execute(0, new Properties(), incomingParameters); + assertEquals(0, returnMap.size()); + mte.cleanUp(); } } diff --git a/plugins/plugins-executor/plugins-executor-mvel/src/test/java/org/onap/policy/apex/plugins/executor/mvel/MvelTaskSelectExecutorTest.java b/plugins/plugins-executor/plugins-executor-mvel/src/test/java/org/onap/policy/apex/plugins/executor/mvel/MvelTaskSelectExecutorTest.java index cd458fdac..d81c3d13b 100644 --- a/plugins/plugins-executor/plugins-executor-mvel/src/test/java/org/onap/policy/apex/plugins/executor/mvel/MvelTaskSelectExecutorTest.java +++ b/plugins/plugins-executor/plugins-executor-mvel/src/test/java/org/onap/policy/apex/plugins/executor/mvel/MvelTaskSelectExecutorTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2020 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,9 +21,9 @@ package org.onap.policy.apex.plugins.executor.mvel; +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.fail; import java.util.Properties; import org.junit.After; @@ -35,6 +36,7 @@ import org.onap.policy.apex.context.parameters.LockManagerParameters; import org.onap.policy.apex.context.parameters.PersistorParameters; import org.onap.policy.apex.core.engine.context.ApexInternalContext; import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; @@ -67,76 +69,37 @@ public class MvelTaskSelectExecutorTest { } @Test - public void testJavaTaskSelectExecutor() { + public void testJavaTaskSelectExecutor() throws StateMachineException, ContextException { MvelTaskSelectExecutor mtse = new MvelTaskSelectExecutor(); assertNotNull(mtse); - try { - mtse.prepare(); - fail("test should throw an exception here"); - } catch (Exception mtseException) { - assertEquals(java.lang.NullPointerException.class, mtseException.getClass()); - } - + assertThatThrownBy(mtse::prepare) + .isInstanceOf(java.lang.NullPointerException.class); AxState state = new AxState(); ApexInternalContext internalContext = null; - try { - internalContext = new ApexInternalContext(new AxPolicyModel()); - } catch (ContextException e) { - fail("test should not throw an exception here"); - } + internalContext = new ApexInternalContext(new AxPolicyModel()); mtse.setContext(null, state, internalContext); state.getTaskSelectionLogic().setLogic("x > 1 2 ()"); - try { - mtse.prepare(); - fail("test should throw an exception here"); - } catch (Exception mtseException) { - assertEquals("failed to compile MVEL code for state NULL:0.0.0:NULL:NULL", mtseException.getMessage()); - } - + assertThatThrownBy(mtse::prepare) + .hasMessage("failed to compile MVEL code for state NULL:0.0.0:NULL:NULL"); state.getTaskSelectionLogic().setLogic("java.lang.String"); - try { - mtse.prepare(); - } catch (Exception mtseException) { - fail("test should not throw an exception here"); - } - - try { - mtse.execute(-1, new Properties(), null); - fail("test should throw an exception here"); - } catch (Exception mtseException) { - assertEquals(java.lang.NullPointerException.class, mtseException.getClass()); - } + mtse.prepare(); + assertThatThrownBy(() -> mtse.execute(-1, new Properties(), null)) + .isInstanceOf(java.lang.NullPointerException.class); AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1")); EnEvent event = new EnEvent(axEvent); - try { - mtse.execute(-1, new Properties(), event); - fail("test should throw an exception here"); - } catch (Exception mtseException) { - assertEquals("failed to execute MVEL code for state NULL:0.0.0:NULL:NULL", - mtseException.getMessage()); - } - + assertThatThrownBy(() -> mtse.execute(-1, new Properties(), event)) + .hasMessage("failed to execute MVEL code for state NULL:0.0.0:NULL:NULL"); state.getTaskSelectionLogic().setLogic("executionId != -1"); - try { - mtse.prepare(); - mtse.execute(-1, new Properties(), event); - fail("test should throw an exception here"); - } catch (Exception mtseException) { - assertEquals("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\"", - mtseException.getMessage()); - } - - try { - mtse.prepare(); - AxArtifactKey taskKey = mtse.execute(0, new Properties(), event); - assertEquals("NULL:0.0.0", taskKey.getId()); - mtse.cleanUp(); - } catch (Exception mtseException) { - fail("test should not throw an exception here"); - } + mtse.prepare(); + assertThatThrownBy(() -> mtse.execute(-1, new Properties(), event)) + .hasMessageContaining("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\""); + mtse.prepare(); + AxArtifactKey taskKey = mtse.execute(0, new Properties(), event); + assertEquals("NULL:0.0.0", taskKey.getId()); + mtse.cleanUp(); } } -- cgit 1.2.3-korg