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 --- .../jruby/JrubyStateFinalizerExecutorTest.java | 63 ++++++------------- .../executor/jruby/JrubyTaskExecutorTest.java | 73 ++++++++-------------- .../jruby/JrubyTaskSelectExecutorTest.java | 56 ++++++----------- 3 files changed, 64 insertions(+), 128 deletions(-) (limited to 'plugins/plugins-executor/plugins-executor-jruby/src') 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(); } } -- cgit 1.2.3-korg