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 --- .../executor/java/JavaTaskExecutorTest.java | 78 +++++------------ .../executor/java/JavaTaskSelectExecutorTest.java | 78 +++++------------ .../jruby/JrubyStateFinalizerExecutorTest.java | 63 +++++--------- .../executor/jruby/JrubyTaskExecutorTest.java | 73 ++++++---------- .../jruby/JrubyTaskSelectExecutorTest.java | 56 +++++-------- .../mvel/MvelStateFinalizerExecutorTest.java | 97 ++++++---------------- .../executor/mvel/MvelTaskExecutorTest.java | 77 +++++------------ .../executor/mvel/MvelTaskSelectExecutorTest.java | 79 +++++------------- 8 files changed, 174 insertions(+), 427 deletions(-) (limited to 'plugins') diff --git a/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaTaskExecutorTest.java b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaTaskExecutorTest.java index 8d7cca3aa..7f263245c 100644 --- a/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaTaskExecutorTest.java +++ b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaTaskExecutorTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * 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.java; +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,73 +68,37 @@ public class JavaTaskExecutorTest { } @Test - public void testJavaTaskExecutor() { + public void testJavaTaskExecutor() throws ContextException, StateMachineException { JavaTaskExecutor jte = new JavaTaskExecutor(); assertNotNull(jte); - try { - jte.prepare(); - fail("test should throw an exception here"); - } catch (Exception jteException) { - assertEquals(java.lang.NullPointerException.class, jteException.getClass()); - } - + assertThatThrownBy(jte::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"); - } - jte.setContext(null, task, internalContext); + internalContext = new ApexInternalContext(new AxPolicyModel()); - try { - jte.prepare(); - fail("test should throw an exception here"); - } catch (Exception jteException) { - assertEquals("instantiation error on Java class \"\"", jteException.getMessage()); - } + jte.setContext(null, task, internalContext); + assertThatThrownBy(jte::prepare) + .hasMessage("instantiation error on Java class \"\""); task.getTaskLogic().setLogic("java.lang.String"); - try { - jte.prepare(); - } catch (Exception jteException) { - fail("test should not throw an exception here"); - } - - try { - jte.execute(-1, new Properties(), null); - fail("test should throw an exception here"); - } catch (Exception jteException) { - assertEquals(java.lang.NullPointerException.class, jteException.getClass()); - } + jte.prepare(); + assertThatThrownBy(() -> jte.execute(-1, new Properties(), null)) + .isInstanceOf(java.lang.NullPointerException.class); Map incomingParameters = new HashMap<>(); - try { - jte.execute(-1, new Properties(), incomingParameters); - fail("test should throw an exception here"); - } catch (Exception jteException) { - assertEquals("task logic failed to run for task \"NULL:0.0.0\"", jteException.getMessage()); - } - + assertThatThrownBy(() -> jte.execute(-1, new Properties(), incomingParameters)) + .hasMessage("task logic failed to run for task \"NULL:0.0.0\""); task.getTaskLogic().setLogic("org.onap.policy.apex.plugins.executor.java.DummyJavaTaskLogic"); - try { - jte.prepare(); - jte.execute(-1, new Properties(), incomingParameters); - fail("test should throw an exception here"); - } catch (Exception jteException) { - assertEquals("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0", - jteException.getMessage()); - } + jte.prepare(); + assertThatThrownBy(() -> jte.execute(-1, new Properties(), incomingParameters)) + .hasMessage("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0"); + jte.prepare(); + Map returnMap = jte.execute(0, new Properties(), incomingParameters); + assertEquals(0, returnMap.size()); + jte.cleanUp(); - try { - jte.prepare(); - Map returnMap = jte.execute(0, new Properties(), incomingParameters); - assertEquals(0, returnMap.size()); - jte.cleanUp(); - } catch (Exception jteException) { - fail("test should not throw an exception here"); - } } } diff --git a/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaTaskSelectExecutorTest.java b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaTaskSelectExecutorTest.java index 38792c409..61acecd9b 100644 --- a/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaTaskSelectExecutorTest.java +++ b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaTaskSelectExecutorTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * 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.java; +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,40 @@ public class JavaTaskSelectExecutorTest { } @Test - public void testJavaTaskSelectExecutor() { + public void testJavaTaskSelectExecutor() throws StateMachineException, ContextException { JavaTaskSelectExecutor jtse = new JavaTaskSelectExecutor(); assertNotNull(jtse); - try { - jtse.prepare(); - fail("test should throw an exception here"); - } catch (Exception jtseException) { - assertEquals(java.lang.NullPointerException.class, jtseException.getClass()); - } - + assertThatThrownBy(jtse::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()); jtse.setContext(null, state, internalContext); - try { - jtse.prepare(); - fail("test should throw an exception here"); - } catch (Exception jtseException) { - assertEquals("instantiation error on Java class \"\"", jtseException.getMessage()); - } - + assertThatThrownBy(jtse::prepare) + .hasMessage("instantiation error on Java class \"\""); state.getTaskSelectionLogic().setLogic("java.lang.String"); - try { - jtse.prepare(); - } catch (Exception jtseException) { - fail("test should not throw an exception here"); - } - - try { - jtse.execute(-1, new Properties(), null); - fail("test should throw an exception here"); - } catch (Exception jtseException) { - assertEquals(java.lang.NullPointerException.class, jtseException.getClass()); - } + jtse.prepare(); + assertThatThrownBy(() -> jtse.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 { - jtse.execute(-1, new Properties(), event); - fail("test should throw an exception here"); - } catch (Exception jtseException) { - assertEquals("task selection logic failed to run for state \"NULL:0.0.0:NULL:NULL\"", - jtseException.getMessage()); - } - + assertThatThrownBy(() -> jtse.execute(-1, new Properties(), event)) + .hasMessage("task selection logic failed to run for state \"NULL:0.0.0:NULL:NULL\""); state.getTaskSelectionLogic() .setLogic("org.onap.policy.apex.plugins.executor.java.DummyJavaTaskSelectionLogic"); - try { - jtse.prepare(); + + jtse.prepare(); + assertThatThrownBy(() -> { jtse.execute(-1, new Properties(), event); - fail("test should throw an exception here"); - } catch (Exception jtseException) { - assertEquals("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\"", - jtseException.getMessage()); - } + }).hasMessage("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\""); - try { - jtse.prepare(); - AxArtifactKey taskKey = jtse.execute(0, new Properties(), event); - assertEquals("NULL:0.0.0", taskKey.getId()); - jtse.cleanUp(); - } catch (Exception jtseException) { - fail("test should not throw an exception here"); - } + jtse.prepare(); + AxArtifactKey taskKey = jtse.execute(0, new Properties(), event); + assertEquals("NULL:0.0.0", taskKey.getId()); + jtse.cleanUp(); } } diff --git a/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyStateFinalizerExecutorTest.java b/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyStateFinalizerExecutorTest.java index fd6f24474..eca9a45cd 100644 --- a/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyStateFinalizerExecutorTest.java +++ b/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyStateFinalizerExecutorTest.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.jruby; +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.lang.reflect.Field; import java.util.Properties; @@ -76,68 +77,44 @@ public class JrubyStateFinalizerExecutorTest { } @Test - public void testJrubyStateFinalizerExecutor() { + public void testJrubyStateFinalizerExecutor() throws StateMachineException, ContextException, + NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { JrubyStateFinalizerExecutor jsfe = new JrubyStateFinalizerExecutor(); assertNotNull(jsfe); - try { - Field fieldContainer = JrubyStateFinalizerExecutor.class.getDeclaredField("container"); - fieldContainer.setAccessible(true); - fieldContainer.set(jsfe, null); - jsfe.prepare(); - fail("test should throw an exception here"); - } catch (Exception jtseException) { - assertEquals(java.lang.NullPointerException.class, jtseException.getClass()); - } + Field fieldContainer = JrubyStateFinalizerExecutor.class.getDeclaredField("container"); + fieldContainer.setAccessible(true); + fieldContainer.set(jsfe, null); + assertThatThrownBy(jsfe::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); AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic(); jsfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext); - try { - jsfe.prepare(); - } catch (Exception jtseException) { - fail("test should not throw an exception here"); - } - try { - jsfe.execute(-1, new Properties(), null); - fail("test should throw an exception here"); - } catch (Exception jtseException) { - 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", jtseException.getMessage()); - } + jsfe.prepare(); + assertThatThrownBy(() -> jsfe.execute(-1, new Properties(), null)) + .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"); AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1")); - EnEvent event = new EnEvent(axEvent); final String jrubyLogic = "if executor.executionId == -1" + "\n return false" + "\n else " + "\n executor.setSelectedStateOutputName(\"SelectedOutputIsMe\")" + "\n return true" + "\n end"; stateFinalizerLogic.setLogic(jrubyLogic); + EnEvent event = new EnEvent(axEvent); state.getStateOutputs().put("SelectedOutputIsMe", null); - try { - jsfe.prepare(); - String stateOutput = jsfe.execute(0, new Properties(), event); - assertEquals("SelectedOutputIsMe", stateOutput); - jsfe.cleanUp(); - } catch (Exception jtseException) { - jtseException.printStackTrace(); - fail("test should not throw an exception here"); - } + jsfe.prepare(); + String stateOutput = jsfe.execute(0, new Properties(), event); + assertEquals("SelectedOutputIsMe", stateOutput); + jsfe.cleanUp(); } } diff --git a/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskExecutorTest.java b/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskExecutorTest.java index eb4707c5b..e2785d816 100644 --- a/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskExecutorTest.java +++ b/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskExecutorTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-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. @@ -21,9 +21,9 @@ package org.onap.policy.apex.plugins.executor.jruby; +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.lang.reflect.Field; import java.util.HashMap; @@ -38,6 +38,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; @@ -68,7 +69,8 @@ public class JrubyTaskExecutorTest { } @Test - public void testJrubyTaskExecutor() { + public void testJrubyTaskExecutor() throws StateMachineException, ContextException, + IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException { // Run test twice to check for incorrect shutdown activity jrubyExecutorTest(); jrubyExecutorTest(); @@ -77,63 +79,38 @@ public class JrubyTaskExecutorTest { /** * Test the JRuby executor. */ - private void jrubyExecutorTest() { + private void jrubyExecutorTest() throws StateMachineException, ContextException, + IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException { JrubyTaskExecutor jte = new JrubyTaskExecutor(); assertNotNull(jte); - try { - Field fieldContainer = JrubyTaskExecutor.class.getDeclaredField("container"); - fieldContainer.setAccessible(true); - fieldContainer.set(jte, null); - jte.prepare(); - fail("test should throw an exception here"); - } catch (Exception jtseException) { - assertEquals(java.lang.NullPointerException.class, jtseException.getClass()); - } - + Field fieldContainer = JrubyTaskExecutor.class.getDeclaredField("container"); + fieldContainer.setAccessible(true); + fieldContainer.set(jte, null); + assertThatThrownBy(jte::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()); + jte.setContext(null, task, internalContext); - try { - jte.prepare(); - } catch (Exception jtseException) { - fail("test should not throw an exception here"); - } - Map incomingParameters = new HashMap<>(); - try { - jte.execute(-1, new Properties(), incomingParameters); - fail("test should throw an exception here"); - } catch (Exception jteException) { - assertEquals("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0", - jteException.getMessage()); - } + jte.prepare(); + Map incomingParameters = new HashMap<>(); + assertThatThrownBy(() -> jte.execute(-1, new Properties(), incomingParameters)) + .hasMessage("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0"); final String jrubyLogic = "if executor.executionId == -1" + "\n return false" + "\n else " + "\n return true" + "\n end"; task.getTaskLogic().setLogic(jrubyLogic); - try { - jte.prepare(); - Map returnMap = jte.execute(0, new Properties(), incomingParameters); - assertEquals(0, returnMap.size()); - jte.cleanUp(); - } catch (Exception jteException) { - fail("test should not throw an exception here"); - } + jte.prepare(); + Map returnMap = jte.execute(0, new Properties(), incomingParameters); + assertEquals(0, returnMap.size()); + jte.cleanUp(); - try { - jte.prepare(); - Map returnMap = jte.execute(0, new Properties(), incomingParameters); - assertEquals(0, returnMap.size()); - jte.cleanUp(); - } catch (Exception jteException) { - fail("test should not throw an exception here"); - } + jte.prepare(); + Map returnMap1 = jte.execute(0, new Properties(), incomingParameters); + assertEquals(0, returnMap1.size()); + jte.cleanUp(); } } diff --git a/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskSelectExecutorTest.java b/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskSelectExecutorTest.java index 617db8d8c..59d1be4b4 100644 --- a/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskSelectExecutorTest.java +++ b/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskSelectExecutorTest.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.jruby; +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.lang.reflect.Field; import java.util.Properties; @@ -36,6 +37,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; @@ -68,56 +70,36 @@ public class JrubyTaskSelectExecutorTest { } @Test - public void testJrubyTaskSelectExecutor() { + public void testJrubyTaskSelectExecutor() throws StateMachineException, ContextException, + NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { JrubyTaskSelectExecutor jtse = new JrubyTaskSelectExecutor(); assertNotNull(jtse); assertNotNull(jtse.getOutputEventSet()); - try { - Field fieldContainer = JrubyTaskSelectExecutor.class.getDeclaredField("container"); - fieldContainer.setAccessible(true); - fieldContainer.set(jtse, null); - jtse.prepare(); - fail("test should throw an exception here"); - } catch (Exception jtseException) { - assertEquals(java.lang.NullPointerException.class, jtseException.getClass()); - } + Field fieldContainer = JrubyTaskSelectExecutor.class.getDeclaredField("container"); + fieldContainer.setAccessible(true); + fieldContainer.set(jtse, null); + + assertThatThrownBy(jtse::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()); + jtse.setContext(null, state, internalContext); - try { - jtse.prepare(); - } catch (Exception jtseException) { - fail("test should not throw an exception here"); - } + jtse.prepare(); AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1")); EnEvent event = new EnEvent(axEvent); - try { - jtse.execute(-1, new Properties(), event); - fail("test should throw an exception here"); - } catch (Exception jtseException) { - assertEquals("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\"", - jtseException.getMessage()); - } - + assertThatThrownBy(() -> jtse.execute(-1, new Properties(), event)) + .hasMessage("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\""); final String jrubyLogic = "if executor.executionId == -1" + "\n return false" + "\n else " + "\n return true" + "\n end"; state.getTaskSelectionLogic().setLogic(jrubyLogic); - try { - jtse.prepare(); - AxArtifactKey taskKey = jtse.execute(0, new Properties(), event); - assertEquals("NULL:0.0.0", taskKey.getId()); - jtse.cleanUp(); - } catch (Exception jtseException) { - fail("test should not throw an exception here"); - } + jtse.prepare(); + AxArtifactKey taskKey = jtse.execute(0, new Properties(), event); + assertEquals("NULL:0.0.0", taskKey.getId()); + jtse.cleanUp(); } } 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