diff options
author | waynedunican <wayne.dunican@est.tech> | 2020-07-13 11:13:53 +0100 |
---|---|---|
committer | waynedunican <wayne.dunican@est.tech> | 2020-07-15 14:25:13 +0100 |
commit | aac3c1d70c439910e25a2634f872f497486509ec (patch) | |
tree | 8b0f552dc22bfa479bcac96094b67d193c6f49c1 | |
parent | 871421fb99f7e6221b2c084e43a9388f618f882d (diff) |
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 <wayne.dunican@est.tech>
24 files changed, 1261 insertions, 2024 deletions
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<String, Object> 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<String, Object> returnMap = jte.execute(0, new Properties(), incomingParameters); + assertEquals(0, returnMap.size()); + jte.cleanUp(); - try { - jte.prepare(); - Map<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> returnMap = jte.execute(0, new Properties(), incomingParameters);
+ assertEquals(0, returnMap.size());
+ jte.cleanUp();
- try {
- jte.prepare();
- Map<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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(); } } diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListenerTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListenerTest.java index d31a9fa37..079be6bd2 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListenerTest.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListenerTest.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.service.engine.engdep; +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.fail; import java.net.InetSocketAddress; import java.util.ArrayList; @@ -75,13 +76,8 @@ public class EngDepMessageListenerTest { EngDepMessageListener listener = new EngDepMessageListener(dummyEngineService); listener.startProcessorThread(); - try { - listener.onMessage("bad string message"); - fail("test should throw an exception"); - } catch (Exception uoe) { - assertEquals("String messages are not supported on the EngDep protocol", uoe.getMessage()); - } - + assertThatThrownBy(() -> listener.onMessage("bad string message")) + .hasMessage("String messages are not supported on the EngDep protocol"); List<Message> messageList = new ArrayList<>(); messageList.add(new StartEngine(new AxArtifactKey("Start:0.0.1"))); listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); @@ -131,40 +127,36 @@ public class EngDepMessageListenerTest { await().until(messageQueue::isEmpty); assertEquals("UpdateModel:0.0.1", dummyEngineService.getUpdateModelKey().getId()); - try { - messageList.clear(); - messageList.add(new Response(new AxArtifactKey("UpdateModel:0.0.1"), false, - new GetEngineInfo(new AxArtifactKey("EngineInfo:0.0.1")))); - listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); - await().until(messageQueue::isEmpty); - assertEquals("UpdateModel:0.0.1", dummyEngineService.getUpdateModelKey().getId()); - - messageList.clear(); - Message badMessage0 = new DummyMessage(null, null); - messageList.add(badMessage0); - listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); - await().until(messageQueue::isEmpty); - - messageList.clear(); - Message badMessage1 = new DummyMessage(new DummyAction(null), null); - messageList.add(badMessage1); - listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); - await().until(messageQueue::isEmpty); - - messageList.clear(); - Message badMessage2 = new DummyMessage(new DummyAction("throw exception"), null); - messageList.add(badMessage2); - listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); - await().until(messageQueue::isEmpty); - - messageList.clear(); - Mockito.doReturn(false).when(webSocketMock).isOpen(); - messageList.add(new StartEngine(new AxArtifactKey("Start:0.0.1"))); - listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); - await().until(messageQueue::isEmpty); - } catch (Exception e) { - fail("test should not throw exceptions on bad messages"); - } + messageList.clear(); + messageList.add(new Response(new AxArtifactKey("UpdateModel:0.0.1"), false, + new GetEngineInfo(new AxArtifactKey("EngineInfo:0.0.1")))); + listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); + await().until(messageQueue::isEmpty); + assertEquals("UpdateModel:0.0.1", dummyEngineService.getUpdateModelKey().getId()); + messageList.clear(); + Message badMessage0 = new DummyMessage(null, null); + messageList.add(badMessage0); + listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); + await().until(messageQueue::isEmpty); + + messageList.clear(); + Message badMessage1 = new DummyMessage(new DummyAction(null), null); + messageList.add(badMessage1); + listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); + await().until(messageQueue::isEmpty); + + messageList.clear(); + Message badMessage2 = new DummyMessage(new DummyAction("throw exception"), null); + messageList.add(badMessage2); + listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); + await().until(messageQueue::isEmpty); + + messageList.clear(); + Mockito.doReturn(false).when(webSocketMock).isOpen(); + messageList.add(new StartEngine(new AxArtifactKey("Start:0.0.1"))); + listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); + await().until(messageQueue::isEmpty); + listener.stopProcessorThreads(); } } diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventConverterTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventConverterTest.java index 99b2654ab..ceadc4aae 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventConverterTest.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventConverterTest.java @@ -1,27 +1,27 @@ /*- * ============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. * 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.service.engine.event; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import org.junit.Test; import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.Apex2JsonEventConverter; @@ -37,69 +37,27 @@ public class JsonEventConverterTest { public void testJsonEventConverter() { Apex2JsonEventConverter converter = new Apex2JsonEventConverter(); - try { - converter.init(null); - fail("test should throw an exception"); - } catch (Exception ie) { - assertEquals("specified consumer properties are not applicable to the JSON event protocol", - ie.getMessage()); - } - - try { - converter.init(new EventProtocolParameters() { - }); - fail("test should throw an exception"); - } catch (Exception ie) { - assertEquals("specified consumer properties are not applicable to the JSON event protocol", - ie.getMessage()); - } - + assertThatThrownBy(() -> converter.init(null)) + .hasMessage("specified consumer properties are not applicable to the JSON event protocol"); + assertThatThrownBy(() -> converter.init(new EventProtocolParameters() {})) + .hasMessage("specified consumer properties are not applicable to the JSON event protocol"); JsonEventProtocolParameters pars = new JsonEventProtocolParameters(); converter.init(pars); - try { - converter.toApexEvent(null, null); - fail("test should throw an exception"); - } catch (Exception tae) { - assertEquals("event processing failed, event is null", tae.getMessage()); - } - - try { - converter.toApexEvent(null, 1); - fail("test should throw an exception"); - } catch (Exception tae) { - assertEquals("error converting event \"1\" to a string", tae.getMessage()); - } - - try { - converter.toApexEvent(null, "[{\"aKey\": 1},{\"aKey\": 2}]"); - fail("test should throw an exception"); - } catch (Exception tae) { - assertEquals("Failed to unmarshal JSON event: event received without mandatory parameter \"name\" " - + "on configuration or on event, event=[{\"aKey\": 1},{\"aKey\": 2}]", tae.getMessage()); - } - - try { - converter.toApexEvent(null, "[1,2,3]"); - fail("test should throw an exception"); - } catch (Exception tae) { - assertEquals("Failed to unmarshal JSON event: incoming event ([1,2,3]) is a JSON object array " - + "containing an invalid object 1.0, event=[1,2,3]", tae.getMessage()); - } - - try { - converter.fromApexEvent(null); - fail("test should throw an exception"); - } catch (Exception tae) { - assertEquals("event processing failed, Apex event is null", tae.getMessage()); - } - - try { - converter.fromApexEvent(new ApexEvent("Event", "0.0.1", "a.name.space", "here", "there")); - fail("test should throw an exception"); - } catch (Exception tae) { - assertEquals("Model for org.onap.policy.apex.model.eventmodel.concepts.AxEvents not found in model service", - tae.getMessage()); - } + assertThatThrownBy(() -> converter.toApexEvent(null, null)) + .hasMessage("event processing failed, event is null"); + assertThatThrownBy(() -> converter.toApexEvent(null, 1)) + .hasMessage("error converting event \"1\" to a string"); + assertThatThrownBy(() -> converter.toApexEvent(null, "[{\"aKey\": 1},{\"aKey\": 2}]")) + .hasMessage("Failed to unmarshal JSON event: event received without mandatory parameter \"name\" " + + "on configuration or on event, event=[{\"aKey\": 1},{\"aKey\": 2}]"); + assertThatThrownBy(() -> converter.toApexEvent(null, "[1,2,3]")) + .hasMessage("Failed to unmarshal JSON event: incoming event ([1,2,3]) is a JSON object array " + + "containing an invalid object 1.0, event=[1,2,3]"); + assertThatThrownBy(() -> converter.fromApexEvent(null)) + .hasMessage("event processing failed, Apex event is null"); + assertThatThrownBy(() -> converter.fromApexEvent(new ApexEvent("Event", "0.0.1", "a.name.space", + "here", "there"))) + .hasMessage("Model for org.onap.policy.apex.model.eventmodel.concepts.AxEvents not found in model service"); } } diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventHandlerForPojoTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventHandlerForPojoTest.java index b21f41190..de84acca1 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventHandlerForPojoTest.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventHandlerForPojoTest.java @@ -21,9 +21,9 @@ package org.onap.policy.apex.service.engine.event; +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.io.ByteArrayInputStream; import java.io.IOException; @@ -216,35 +216,16 @@ public class JsonEventHandlerForPojoTest { logger.debug("input event\n" + apexEventJsonStringIn); - try { - jsonEventConverter.toApexEvent("PojoEvent", apexEventJsonStringIn); - fail("test should throw an exception"); - } catch (Exception tae) { - assertEquals( - "Failed to unmarshal JSON event: error parsing PojoEvent:0.0.1 event from Json. " - + "Field BAD_POJO_PAR not found on POJO event definition.", - tae.getMessage().substring(0, 133)); - } - + assertThatThrownBy(() -> jsonEventConverter.toApexEvent("PojoEvent", apexEventJsonStringIn)) + .hasMessageContaining("Failed to unmarshal JSON event: error parsing PojoEvent:0.0.1 event from Json. " + + "Field BAD_POJO_PAR not found on POJO event definition."); pars.setPojoField("POJO_PAR"); - try { - jsonEventConverter.toApexEvent("PojoNoFieldEvent", apexEventJsonStringIn); - fail("test should throw an exception"); - } catch (Exception tae) { - assertEquals( - "Failed to unmarshal JSON event: error parsing PojoNoFieldEvent:0.0.1 event from Json, " - + "Field POJO_PAR not found, no fields defined on event.", - tae.getMessage().substring(0, 139)); - } - - try { - jsonEventConverter.toApexEvent("PojoTooManyFieldsEvent", apexEventJsonStringIn); - fail("test should throw an exception"); - } catch (Exception tae) { - assertEquals( - "Failed to unmarshal JSON event: error parsing PojoTooManyFieldsEvent:0.0.1 event from Json, " - + "Field POJO_PAR, one and only one field may be defined on a POJO event definition.", - tae.getMessage().substring(0, 173)); - } + assertThatThrownBy(() -> jsonEventConverter.toApexEvent("PojoNoFieldEvent", apexEventJsonStringIn)) + .hasMessageContaining("Failed to unmarshal JSON event: error parsing PojoNoFieldEvent:0.0.1 " + + "event from Json, Field POJO_PAR not found, no fields defined on event."); + assertThatThrownBy(() -> jsonEventConverter.toApexEvent("PojoTooManyFieldsEvent", apexEventJsonStringIn)) + .hasMessageContaining("Failed to unmarshal JSON event: error parsing PojoTooManyFieldsEvent:0.0.1" + + " event from Json, Field POJO_PAR, one and only one field may be defined on a " + + "POJO event definition."); } } diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventHandlerTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventHandlerTest.java index b65ebd3dc..241c92978 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventHandlerTest.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventHandlerTest.java @@ -21,11 +21,11 @@ package org.onap.policy.apex.service.engine.event; +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.Assert.fail; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -103,35 +103,30 @@ public class JsonEventHandlerTest { */ @Test public void testJsontoApexEvent() throws ApexException { - try { - final Apex2JsonEventConverter jsonEventConverter = new Apex2JsonEventConverter(); - assertNotNull(jsonEventConverter); - jsonEventConverter.init(new JsonEventProtocolParameters()); + final Apex2JsonEventConverter jsonEventConverter = new Apex2JsonEventConverter(); + assertNotNull(jsonEventConverter); + jsonEventConverter.init(new JsonEventProtocolParameters()); - final String apexEventJsonStringIn = SupportJsonEventGenerator.jsonEvent(); + final String apexEventJsonStringIn = SupportJsonEventGenerator.jsonEvent(); - logger.debug("input event\n" + apexEventJsonStringIn); + logger.debug("input event\n" + apexEventJsonStringIn); - final List<ApexEvent> apexEventList = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); - for (final ApexEvent apexEvent : apexEventList) { - assertNotNull(apexEvent); + final List<ApexEvent> apexEventList = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + for (final ApexEvent apexEvent : apexEventList) { + assertNotNull(apexEvent); - logger.debug(apexEvent.toString()); + logger.debug(apexEvent.toString()); - assertEquals("BasicEvent", apexEvent.getName()); - assertEquals("0.0.1", apexEvent.getVersion()); - assertEquals("org.onap.policy.apex.events", apexEvent.getNameSpace()); - assertEquals("test", apexEvent.getSource()); - assertEquals("apex", apexEvent.getTarget()); - assertEquals(12345, apexEvent.get("intPar")); - - final Object testMatchCaseSelected = apexEvent.get("TestMatchCaseSelected"); - assertNull(testMatchCaseSelected); - } - } catch (final Exception e) { - e.printStackTrace(); - throw new ApexException("Exception reading Apex event JSON file", e); + assertEquals("BasicEvent", apexEvent.getName()); + assertEquals("0.0.1", apexEvent.getVersion()); + assertEquals("org.onap.policy.apex.events", apexEvent.getNameSpace()); + assertEquals("test", apexEvent.getSource()); + assertEquals("apex", apexEvent.getTarget()); + assertEquals(12345, apexEvent.get("intPar")); + final Object testMatchCaseSelected = apexEvent.get("TestMatchCaseSelected"); + assertNull(testMatchCaseSelected); } + } /** @@ -141,142 +136,96 @@ public class JsonEventHandlerTest { */ @Test public void testJsontoApexBadEvent() throws ApexException { - try { - final Apex2JsonEventConverter jsonEventConverter = new Apex2JsonEventConverter(); - assertNotNull(jsonEventConverter); - jsonEventConverter.init(new JsonEventProtocolParameters()); + final Apex2JsonEventConverter jsonEventConverter = new Apex2JsonEventConverter(); + assertNotNull(jsonEventConverter); + jsonEventConverter.init(new JsonEventProtocolParameters()); + assertThatThrownBy(() -> { + String apexEventJsonStringIn = null; + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNoName(); + jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + }).hasMessageContaining("Failed to unmarshal JSON event: event received without " + + "mandatory parameter \"name\" "); + assertThatThrownBy(() -> { + String apexEventJsonStringIn = null; + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventBadName(); + jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + }).hasMessageContaining("Failed to unmarshal JSON event: field \"name\" with value \"%%%%\" is invalid"); + assertThatThrownBy(() -> { + String apexEventJsonStringIn = null; + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNoExName(); + jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + }).hasMessageContaining("Failed to unmarshal JSON event: an event definition for an event named \"I_DONT_EXI"); + String apexEventJsonStringIn1 = null; + apexEventJsonStringIn1 = SupportJsonEventGenerator.jsonEventNoVersion(); + ApexEvent event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn1).get(0); + assertEquals("0.0.1", event.getVersion()); + assertThatThrownBy(() -> { + String apexEventJsonStringIn = null; + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventBadVersion(); + jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + }).hasMessageContaining("Failed to unmarshal JSON event: field \"version\" with value \"#####\" is invalid"); + assertThatThrownBy(() -> { + String apexEventJsonStringIn = null; + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNoExVersion(); + jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + }).hasMessageContaining("Failed to unmarshal JSON event: an event definition for an event named " + + "\"BasicEvent\" with version \"1.2.3\" not found in Apex model"); + apexEventJsonStringIn1 = SupportJsonEventGenerator.jsonEventNoNamespace(); + event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn1).get(0); + assertEquals("org.onap.policy.apex.events", event.getNameSpace()); + + assertThatThrownBy(() -> { + String apexEventJsonStringIn = null; + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventBadNamespace(); + jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + }).hasMessageContaining("Failed to unmarshal JSON event: " + "field \"nameSpace\" with value \"hello.&&&&\" " + + "is invalid"); + assertThatThrownBy(() -> { + String apexEventJsonStringIn = null; + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNoExNamespace(); + jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + }).hasMessageContaining("Failed to unmarshal JSON event: namespace \"pie.in.the.sky\" " + + "on event \"BasicEvent\" does not" + " match namespace \"org.onap.policy.apex.events\" " + + "for that event in the Apex model"); + apexEventJsonStringIn1 = SupportJsonEventGenerator.jsonEventNoSource(); + event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn1).get(0); + assertEquals("source", event.getSource()); + + assertThatThrownBy(() -> { + String apexEventJsonStringIn = null; + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventBadSource(); + jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + }).hasMessageContaining("Failed to unmarshal JSON event: field \"source\" with value \"%!@**@!\" is invalid"); + apexEventJsonStringIn1 = SupportJsonEventGenerator.jsonEventNoTarget(); + event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn1).get(0); + assertEquals("target", event.getTarget()); + + assertThatThrownBy(() -> { + String apexEventJsonStringIn = null; + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventBadTarget(); + jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + }).hasMessageContaining("Failed to unmarshal JSON event: field \"target\" with value \"KNIO(*S)A(S)D\" " + + "is invalid"); + assertThatThrownBy(() -> { String apexEventJsonStringIn = null; - - try { - apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNoName(); - jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); - fail("Test should throw an exception here"); - } catch (final ApexEventException e) { - assertEquals("Failed to unmarshal JSON event: event received without mandatory parameter \"name\" ", - e.getMessage().substring(0, 82)); - } - - try { - apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventBadName(); - jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); - fail("Test should throw an exception here"); - } catch (final ApexEventException e) { - assertEquals("Failed to unmarshal JSON event: field \"name\" with value \"%%%%\" is invalid", - e.getMessage().substring(0, 73)); - } - - try { - apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNoExName(); - jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); - fail("Test should throw an exception here"); - } catch (final ApexEventException e) { - assertEquals("Failed to unmarshal JSON event: an event definition for an event named \"I_DONT_EXI", - e.getMessage().substring(0, 82)); - } - - apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNoVersion(); - ApexEvent event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); - assertEquals("0.0.1", event.getVersion()); - - try { - apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventBadVersion(); - jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); - fail("Test should throw an exception here"); - } catch (final ApexEventException e) { - assertEquals("Failed to unmarshal JSON event: field \"version\" with value \"#####\" is invalid", - e.getMessage().substring(0, 77)); - } - - try { - apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNoExVersion(); - jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); - fail("Test should throw an exception here"); - } catch (final ApexEventException e) { - assertEquals( - "Failed to unmarshal JSON event: an event definition for an event named " - + "\"BasicEvent\" with version \"1.2.3\" not found in Apex model", - e.getMessage().substring(0, 128)); - } - - apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNoNamespace(); - event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); - assertEquals("org.onap.policy.apex.events", event.getNameSpace()); - - try { - apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventBadNamespace(); - jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); - fail("Test should throw an exception here"); - } catch (final ApexEventException e) { - assertEquals( - "Failed to unmarshal JSON event: " + "field \"nameSpace\" with value \"hello.&&&&\" is invalid", - e.getMessage().substring(0, 84)); - } - - try { - apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNoExNamespace(); - jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); - fail("Test should throw an exception here"); - } catch (final ApexEventException e) { - assertEquals("Failed to unmarshal JSON event: namespace \"pie.in.the.sky\" " - + "on event \"BasicEvent\" does not" + " match namespace \"org.onap.policy.apex.events\" " - + "for that event in the Apex model", e.getMessage().substring(0, 168)); - } - - apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNoSource(); - event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); - assertEquals("source", event.getSource()); - - try { - apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventBadSource(); - jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); - fail("Test should throw an exception here"); - } catch (final ApexEventException e) { - assertEquals("Failed to unmarshal JSON event: field \"source\" with value \"%!@**@!\" is invalid", - e.getMessage().substring(0, 78)); - } - - apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNoTarget(); - event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); - assertEquals("target", event.getTarget()); - - try { - apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventBadTarget(); - jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); - fail("Test should throw an exception here"); - } catch (final ApexEventException e) { - assertEquals("Failed to unmarshal JSON event: field \"target\" with value \"KNIO(*S)A(S)D\" is invalid", - e.getMessage().substring(0, 84)); - } - - try { - apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventMissingFields(); - jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); - fail("Test should throw an exception here"); - } catch (final ApexEventException e) { - assertEquals( - "Failed to unmarshal JSON event: error parsing BasicEvent:0.0.1 " - + "event from Json. Field \"intPar\" is missing, but is mandatory.", - e.getMessage().substring(0, 124)); - } - - apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNullFields(); - event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); - assertEquals(null, event.get("TestSlogan")); - assertEquals(-1, event.get("intPar")); - - // Set the missing fields as optional in the model - final AxEvent eventDefinition = ModelService.getModel(AxEvents.class).get("BasicEvent"); - eventDefinition.getParameterMap().get("intPar").setOptional(true); - apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventMissingFields(); - event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); - assertEquals(null, event.get("TestSlogan")); - assertEquals(null, event.get("intPar")); - } catch (final Exception e) { - e.printStackTrace(); - throw new ApexException("Exception reading Apex event JSON file", e); - } + jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + }).hasMessageContaining("Failed to unmarshal JSON event: error parsing BasicEvent:0.0.1 " + + "event from Json. Field \"intPar\" is missing, but is mandatory."); + apexEventJsonStringIn1 = SupportJsonEventGenerator.jsonEventNullFields(); + event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn1).get(0); + assertEquals(null, event.get("TestSlogan")); + assertEquals(-1, event.get("intPar")); + + // Set the missing fields as optional in the model + final AxEvent eventDefinition = ModelService.getModel(AxEvents.class).get("BasicEvent"); + eventDefinition.getParameterMap().get("intPar").setOptional(true); + + apexEventJsonStringIn1 = SupportJsonEventGenerator.jsonEventMissingFields(); + event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn1).get(0); + assertEquals(null, event.get("TestSlogan")); + assertEquals(null, event.get("intPar")); } /** @@ -286,31 +235,26 @@ public class JsonEventHandlerTest { */ @Test public void testApexEventToJson() throws ApexException { - try { - final Apex2JsonEventConverter jsonEventConverter = new Apex2JsonEventConverter(); - jsonEventConverter.init(new JsonEventProtocolParameters()); - assertNotNull(jsonEventConverter); + final Apex2JsonEventConverter jsonEventConverter = new Apex2JsonEventConverter(); + jsonEventConverter.init(new JsonEventProtocolParameters()); + assertNotNull(jsonEventConverter); - final Map<String, Object> basicEventMap = new HashMap<String, Object>(); - basicEventMap.put("intPar", 12345); + final Map<String, Object> basicEventMap = new HashMap<String, Object>(); + basicEventMap.put("intPar", 12345); - final ApexEvent basicEvent = - new ApexEvent("BasicEvent", "0.0.1", "org.onap.policy.apex.events", "test", "apex"); - basicEvent.putAll(basicEventMap); + final ApexEvent basicEvent = + new ApexEvent("BasicEvent", "0.0.1", "org.onap.policy.apex.events", "test", "apex"); + basicEvent.putAll(basicEventMap); - final String apexEvent0000JsonString = (String) jsonEventConverter.fromApexEvent(basicEvent); + final String apexEvent0000JsonString = (String) jsonEventConverter.fromApexEvent(basicEvent); - logger.debug(apexEvent0000JsonString); + logger.debug(apexEvent0000JsonString); - assertTrue(apexEvent0000JsonString.contains("\"name\": \"BasicEvent\"")); - assertTrue(apexEvent0000JsonString.contains("\"version\": \"0.0.1\"")); - assertTrue(apexEvent0000JsonString.contains("\"nameSpace\": \"org.onap.policy.apex.events\"")); - assertTrue(apexEvent0000JsonString.contains("\"source\": \"test\"")); - assertTrue(apexEvent0000JsonString.contains("\"target\": \"apex\"")); - assertTrue(apexEvent0000JsonString.contains("\"intPar\": 12345")); - } catch (final Exception e) { - e.printStackTrace(); - throw new ApexException("Exception reading Apex event JSON file", e); - } + assertTrue(apexEvent0000JsonString.contains("\"name\": \"BasicEvent\"")); + assertTrue(apexEvent0000JsonString.contains("\"version\": \"0.0.1\"")); + assertTrue(apexEvent0000JsonString.contains("\"nameSpace\": \"org.onap.policy.apex.events\"")); + assertTrue(apexEvent0000JsonString.contains("\"source\": \"test\"")); + assertTrue(apexEvent0000JsonString.contains("\"target\": \"apex\"")); + assertTrue(apexEvent0000JsonString.contains("\"intPar\": 12345")); } } diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexCommandLineArgumentsTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexCommandLineArgumentsTest.java index 40375b1f6..09fbe91fe 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexCommandLineArgumentsTest.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexCommandLineArgumentsTest.java @@ -1,28 +1,28 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-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. * 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.service.engine.main; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import org.junit.After; import org.junit.Test; @@ -30,7 +30,7 @@ import org.onap.policy.apex.model.basicmodel.concepts.ApexException; /** * Test Apex Command Line Arguments. - * + * * @author Liam Fallon (liam.fallon@ericsson.com) */ public class ApexCommandLineArgumentsTest { @@ -40,223 +40,138 @@ public class ApexCommandLineArgumentsTest { } @Test - public void testCommandLineArguments() { + public void testCommandLineArguments() throws ApexException { final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments(); final String[] args00 = { "" }; - try { + assertThatThrownBy(() -> { apexArguments.parse(args00); apexArguments.validate(); - fail("Test should throw an exception here"); - } catch (final ApexException e) { - assertEquals("Apex configuration file was not specified as an argument", e.getMessage()); - } - + }).hasMessage("Apex configuration file was not specified as an argument"); final String[] args01 = { "-h" }; - try { - final String result = apexArguments.parse(args01); - assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); - } catch (final ApexException e) { - e.printStackTrace(); - fail("Test should not throw an exception"); - } + + final String result = apexArguments.parse(args01); + assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); final String[] args02 = { "-v" }; - try { - final String result = apexArguments.parse(args02); - assertTrue(result.startsWith("Apex Adaptive Policy Engine")); - } catch (final ApexException e) { - e.printStackTrace(); - fail("Test should not throw an exception"); - } + final String result02 = apexArguments.parse(args02); + assertTrue(result02.startsWith("Apex Adaptive Policy Engine")); final String[] args03 = { "-v", "-h" }; - try { - final String result = apexArguments.parse(args03); - assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); - } catch (final ApexException e) { - e.printStackTrace(); - fail("Test should not throw an exception"); - } + + final String result03 = apexArguments.parse(args03); + assertTrue(result03.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); final String[] args04 = { "-h", "-v" }; - try { - final String result = apexArguments.parse(args04); - assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); - } catch (final ApexException e) { - e.printStackTrace(); - fail("Test should not throw an exception"); - } + + final String result04 = apexArguments.parse(args04); + assertTrue(result04.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); final String[] args05 = { "-a" }; - try { - apexArguments.parse(args05); - } catch (final ApexException e) { - assertEquals("invalid command line arguments specified : Unrecognized option: -a", e.getMessage()); - } - + assertThatThrownBy(() -> apexArguments.parse(args05)) + .hasMessage("invalid command line arguments specified : Unrecognized option: -a"); final String[] args06 = { "-c", "hello", "-m", "goodbye", "-h", "-v" }; - try { - final String result = apexArguments.parse(args06); - assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); - } catch (final ApexException e) { - assertEquals("invalid command line arguments specified : Unrecognized option: -a", e.getMessage()); - } + final String result06 = apexArguments.parse(args06); + assertTrue(result06.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); final String[] args07 = { "-c", "hello", "-m", "goodbye", "-h", "aaa" }; - try { - final String result = apexArguments.parse(args07); - assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); - } catch (final ApexException e) { - assertEquals("too many command line arguments specified : [-c, hello, -m, goodbye, -h, aaa]", - e.getMessage()); - } + assertThatThrownBy(() -> apexArguments.parse(args07)) + .hasMessage("too many command line arguments specified : [-c, hello, -m, goodbye, -h, aaa]"); } @Test - public void testCommandLineFileParameters() { + public void testCommandLineFileParameters() throws ApexException { final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments(); final String[] args00 = { "-c", "zooby" }; - try { + assertThatThrownBy(() -> { apexArguments.parse(args00); apexArguments.validate(); - fail("Test should throw an exception here"); - } catch (final ApexException e) { - assertEquals("Apex configuration file \"zooby\" does not exist", e.getMessage()); - } - + }).hasMessage("Apex configuration file \"zooby\" does not exist"); final String[] args01 = { "-c" }; - try { + assertThatThrownBy(() -> { apexArguments.parse(args01); apexArguments.validate(); - fail("Test should throw an exception here"); - } catch (final ApexException e) { - assertEquals("invalid command line arguments specified : Missing argument for option: c", e.getMessage()); - } - + }).hasMessage("invalid command line arguments specified : Missing argument for option: c"); final String[] args02 = { "-c", "src/test/resources/parameters/goodParams.json" }; - try { - apexArguments.parse(args02); - apexArguments.validate(); - } catch (final ApexException e) { - e.printStackTrace(); - fail("Test should not throw an exception"); - } + apexArguments.parse(args02); + apexArguments.validate(); final String[] args03 = { "-c", "src/test/resources/parameters/goodParams.json", "-m", "zooby" }; - try { + assertThatThrownBy(() -> { apexArguments.parse(args03); apexArguments.validate(); - fail("Test should throw an exception here"); - } catch (final ApexException e) { - assertEquals("Apex model file \"zooby\" does not exist", e.getMessage()); - } - + }).hasMessage("Apex model file \"zooby\" does not exist"); final String[] args04 = { "-m" }; - try { + assertThatThrownBy(() -> { apexArguments.parse(args04); apexArguments.validate(); - fail("Test should throw an exception here"); - } catch (final ApexException e) { - assertEquals("invalid command line arguments specified : Missing argument for option: m", e.getMessage()); - } - + }).hasMessage("invalid command line arguments specified : Missing argument for option: m"); final String[] args05 = { "-c", "src/test/resources/parameters/goodParams.json", "-m" }; - try { + assertThatThrownBy(() -> { apexArguments.parse(args05); apexArguments.validate(); - fail("Test should throw an exception here"); - } catch (final ApexException e) { - assertEquals("invalid command line arguments specified : Missing argument for option: m", e.getMessage()); - } - + }).hasMessage("invalid command line arguments specified : Missing argument for option: m"); final String[] args06 = { "-c", "src/test/resources/parameters/goodParams.json", "-m", "src/test/resources/main/DummyModelFile.json" }; - try { - apexArguments.parse(args06); - apexArguments.validate(); - } catch (final ApexException e) { - e.printStackTrace(); - fail("Test should not throw an exception"); - } + apexArguments.parse(args06); + apexArguments.validate(); final String[] args07 = { "-c", "parameters/goodParams.json", "-m", "main/DummyModelFile.json" }; - try { - apexArguments.parse(args07); - apexArguments.validate(); - } catch (final ApexException e) { - e.printStackTrace(); - fail("Test should not throw an exception"); - } + + apexArguments.parse(args07); + apexArguments.validate(); } @Test - public void testCommandLineRelativeRootParameters() { + public void testCommandLineRelativeRootParameters() throws ApexException { final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments(); final String[] args00 = { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "zooby" }; - try { + assertThatThrownBy(() -> { apexArguments.parse(args00); apexArguments.validate(); - fail("Test should throw an exception here"); - } catch (final ApexException e) { - assertTrue(e.getMessage().contains("zooby\" does not exist or is not a directory")); - } - + }).hasMessageContaining("zooby\" does not exist or is not a directory"); final String[] args01 = { "-rfr" }; - try { + assertThatThrownBy(() -> { apexArguments.parse(args01); apexArguments.validate(); - fail("Test should throw an exception here"); - } catch (final ApexException e) { - assertEquals("invalid command line arguments specified : Missing argument for option: rfr", e.getMessage()); - } - + }).hasMessage("invalid command line arguments specified : Missing argument for option: rfr"); final String[] args02 = { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "pom.xml" }; - try { + assertThatThrownBy(() -> { apexArguments.parse(args02); apexArguments.validate(); - fail("Test should throw an exception here"); - } catch (final ApexException e) { - assertTrue(e.getMessage().contains("pom.xml\" does not exist or is not a directory")); - } - + }).hasMessageContaining("pom.xml\" does not exist or is not a directory"); final String[] args03 = { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "target" }; - try { - apexArguments.parse(args03); - apexArguments.validate(); - } catch (final ApexException e) { - fail("Test should not throw an exception here"); - } + + apexArguments.parse(args03); + apexArguments.validate(); final String[] args04 = { "-c", "src/test/resources/parameters/goodParamsRelative.json", "-rfr", "src/test/resources" }; - try { - apexArguments.parse(args04); - apexArguments.validate(); - } catch (final ApexException e) { - fail("Test should not throw an exception here"); - } + + apexArguments.parse(args04); + apexArguments.validate(); + } } diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ApexParametersTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ApexParametersTest.java index ff97eaec2..330de42dd 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ApexParametersTest.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ApexParametersTest.java @@ -21,6 +21,7 @@ package org.onap.policy.apex.service.engine.parameters; +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.assertNotNull; @@ -44,16 +45,13 @@ public class ApexParametersTest { final String[] args = {"-c", "src/test/resources/parameters/javaPropertiesOK.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - assertTrue(parameters.checkJavaPropertiesSet()); - assertEquals("property0", parameters.getJavaProperties()[0][0]); - assertEquals("property0Value", parameters.getJavaProperties()[0][1]); - assertEquals("property1", parameters.getJavaProperties()[1][0]); - assertEquals("property1Value", parameters.getJavaProperties()[1][1]); - } catch (final ParameterException e) { - fail("This test should not throw an exception"); - } + ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertTrue(parameters.checkJavaPropertiesSet()); + assertEquals("property0", parameters.getJavaProperties()[0][0]); + assertEquals("property0Value", parameters.getJavaProperties()[0][1]); + assertEquals("property1", parameters.getJavaProperties()[1][0]); + assertEquals("property1Value", parameters.getJavaProperties()[1][1]); + } @Test @@ -61,12 +59,9 @@ public class ApexParametersTest { final String[] args = {"-c", "src/test/resources/parameters/javaPropertiesEmpty.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - assertFalse(parameters.checkJavaPropertiesSet()); - } catch (final ParameterException pe) { - fail("This test should not throw an exception"); - } + ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertFalse(parameters.checkJavaPropertiesSet()); + } @Test @@ -74,15 +69,11 @@ public class ApexParametersTest { final String[] args = {"-c", "src/test/resources/parameters/javaPropertiesBad.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException pe) { - assertTrue(pe.getMessage().contains("java properties array entries must have one key and one value")); - assertTrue(pe.getMessage().contains("java properties key is null or blank")); - assertTrue(pe.getMessage().contains("java properties value is null or blank")); - assertTrue(pe.getMessage().contains("java properties array entry is null")); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessageContaining("java properties array entries must have one key and one value") + .hasMessageContaining("java properties key is null or blank") + .hasMessageContaining("java properties value is null or blank") + .hasMessageContaining("java properties array entry is null"); } @Test diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ContextParameterTests.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ContextParameterTests.java index f6d39f029..018fda668 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ContextParameterTests.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ContextParameterTests.java @@ -21,6 +21,7 @@ package org.onap.policy.apex.service.engine.parameters; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; @@ -43,14 +44,10 @@ public class ContextParameterTests { final String[] args = {"-c", "src/test/resources/parameters/serviceContextNoParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from \"src/test/resources/parameters/serviceContextNoParams.json\"\n" + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from \"src/test/resources/parameters/serviceContextNoParams.json\"\n" + "(ParameterRuntimeException):could not find field \"parameterClassName\" in " - + "\"contextParameters\" entry", e.getMessage()); - } + + "\"contextParameters\" entry"); } @Test @@ -58,15 +55,11 @@ public class ContextParameterTests { final String[] args = {"-c", "src/test/resources/parameters/serviceContextBadParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from \"src/test/resources/parameters/serviceContextBadParams.json\"" + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from \"src/test/resources/parameters/serviceContextBadParams.json\"" + "\n(ParameterRuntimeException):failed to deserialize the parameters for " + "\"contextParameters\" to parameter class " - + "\"hello\"\njava.lang.ClassNotFoundException: hello", e.getMessage()); - } + + "\"hello\"\njava.lang.ClassNotFoundException: hello"); } @Test @@ -74,15 +67,11 @@ public class ContextParameterTests { final String[] args = {"-c", "src/test/resources/parameters/serviceContextBadPluginNameParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from " - + "\"src/test/resources/parameters/serviceContextBadPluginNameParams.json\"\n" - + "(ParameterRuntimeException):could not find field \"parameterClassName\" in " - + "\"contextParameters\" entry", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from " + + "\"src/test/resources/parameters/serviceContextBadPluginNameParams.json\"\n" + + "(ParameterRuntimeException):could not find field \"parameterClassName\" in " + + "\"contextParameters\" entry"); } @Test @@ -90,19 +79,13 @@ public class ContextParameterTests { final String[] args = {"-c", "src/test/resources/parameters/serviceContextBadClassParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals( - "error reading parameters from " - + "\"src/test/resources/parameters/serviceContextBadClassParams.json\"" - + "\n(ParameterRuntimeException):failed to deserialize " - + "the parameters for \"contextParameters\"" - + " to parameter class \"java.lang.Integer\"\ncom.google.gson.JsonSyntaxException: " - + "java.lang.IllegalStateException: Expected NUMBER but was BEGIN_OBJECT at path $", - e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from " + + "\"src/test/resources/parameters/serviceContextBadClassParams.json\"" + + "\n(ParameterRuntimeException):failed to deserialize " + + "the parameters for \"contextParameters\"" + + " to parameter class \"java.lang.Integer\"\ncom.google.gson.JsonSyntaxException: " + + "java.lang.IllegalStateException: Expected NUMBER but was BEGIN_OBJECT at path $"); } @Test @@ -110,126 +93,106 @@ public class ContextParameterTests { final String[] args = {"-c", "src/test/resources/parameters/serviceContextBadPluginClassParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from " - + "\"src/test/resources/parameters/serviceContextBadPluginClassParams.json\"" - + "\n(ClassCastException):class org.onap.policy.apex.service.engine.parameters." - + "dummyclasses.SuperDooperExecutorParameters" - + " cannot be cast to class org.onap.policy.apex.context.parameters.ContextParameters " - + "(org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperExecutorParameters and " - + "org.onap.policy.apex.context.parameters.ContextParameters are " - + "in unnamed module of loader 'app')", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from " + + "\"src/test/resources/parameters/serviceContextBadPluginClassParams.json\"" + + "\n(ClassCastException):class org.onap.policy.apex.service.engine.parameters." + + "dummyclasses.SuperDooperExecutorParameters" + + " cannot be cast to class org.onap.policy.apex.context.parameters.ContextParameters " + + "(org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperExecutorParameters and " + + "org.onap.policy.apex.context.parameters.ContextParameters are " + + "in unnamed module of loader 'app')"); } @Test - public void testOkFlushParamTest() { + public void testOkFlushParamTest() throws ParameterException { final String[] args = {"-c", "src/test/resources/parameters/serviceContextOKFlushParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - assertEquals("org.onap.policy.apex.context.parameters.ContextParameters", parameters - .getEngineServiceParameters().getEngineParameters().getContextParameters().getClass().getName()); - assertEquals(123456, parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() - .getPersistorParameters().getFlushPeriod()); - } catch (final ParameterException e) { - fail("This test should not throw any exception: " + e.getMessage()); - } + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertEquals("org.onap.policy.apex.context.parameters.ContextParameters", parameters + .getEngineServiceParameters().getEngineParameters().getContextParameters().getClass().getName()); + assertEquals(123456, parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getPersistorParameters().getFlushPeriod()); + } @Test - public void testOkDefaultParamTest() { + public void testOkDefaultParamTest() throws ParameterException { final String[] args = {"-c", "src/test/resources/parameters/serviceContextOKDefaultParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - assertEquals("org.onap.policy.apex.context.parameters.ContextParameters", parameters - .getEngineServiceParameters().getEngineParameters().getContextParameters().getClass().getName()); - assertEquals(300000, parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() - .getPersistorParameters().getFlushPeriod()); - } catch (final ParameterException e) { - fail("This test should not throw any exception: " + e.getMessage()); - } + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertEquals("org.onap.policy.apex.context.parameters.ContextParameters", parameters + .getEngineServiceParameters().getEngineParameters().getContextParameters().getClass().getName()); + assertEquals(300000, parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getPersistorParameters().getFlushPeriod()); + } @Test - public void testOkDistParamTest() { + public void testOkDistParamTest() throws ParameterException { final String[] args = {"-c", "src/test/resources/parameters/serviceContextOKDistParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - assertEquals("org.onap.policy.apex.context.parameters.ContextParameters", parameters - .getEngineServiceParameters().getEngineParameters().getContextParameters().getClass().getName()); - assertEquals("org.onap.policy.apex.context.parameters.DistributorParameters", - parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() - .getDistributorParameters().getClass().getName()); - } catch (final ParameterException e) { - fail("This test should not throw any exception: " + e.getMessage()); - } + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertEquals("org.onap.policy.apex.context.parameters.ContextParameters", parameters + .getEngineServiceParameters().getEngineParameters().getContextParameters().getClass().getName()); + assertEquals("org.onap.policy.apex.context.parameters.DistributorParameters", + parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getDistributorParameters().getClass().getName()); + } @Test - public void testOkFullDefaultParamTest() { + public void testOkFullDefaultParamTest() throws ParameterException { final String[] args = {"-c", "src/test/resources/parameters/goodParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - assertEquals("org.onap.policy.apex.context.parameters.ContextParameters", parameters - .getEngineServiceParameters().getEngineParameters().getContextParameters().getClass().getName()); - assertEquals("org.onap.policy.apex.context.parameters.DistributorParameters", - parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() - .getDistributorParameters().getClass().getName()); - assertEquals("org.onap.policy.apex.context.parameters.LockManagerParameters", - parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() - .getLockManagerParameters().getClass().getName()); - assertEquals("org.onap.policy.apex.context.parameters.PersistorParameters", - parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() - .getPersistorParameters().getClass().getName()); - assertEquals(300000, parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() - .getPersistorParameters().getFlushPeriod()); - } catch (final ParameterException e) { - fail("This test should not throw any exception: " + e.getMessage()); - } + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertEquals("org.onap.policy.apex.context.parameters.ContextParameters", parameters + .getEngineServiceParameters().getEngineParameters().getContextParameters().getClass().getName()); + assertEquals("org.onap.policy.apex.context.parameters.DistributorParameters", + parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getDistributorParameters().getClass().getName()); + assertEquals("org.onap.policy.apex.context.parameters.LockManagerParameters", + parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getLockManagerParameters().getClass().getName()); + assertEquals("org.onap.policy.apex.context.parameters.PersistorParameters", + parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getPersistorParameters().getClass().getName()); + assertEquals(300000, parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getPersistorParameters().getFlushPeriod()); + } @Test - public void testOkFullParamTest() { + public void testOkFullParamTest() throws ParameterException { final String[] args = {"-c", "src/test/resources/parameters/serviceContextOKFullParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - assertEquals("org.onap.policy.apex.context.parameters.ContextParameters", parameters - .getEngineServiceParameters().getEngineParameters().getContextParameters().getClass().getName()); - assertEquals("org.onap.policy.apex.context.parameters.LockManagerParameters", - parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() - .getLockManagerParameters().getClass().getName()); - assertEquals("org.onap.policy.apex.context.parameters.PersistorParameters", - parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() - .getPersistorParameters().getClass().getName()); - assertEquals(123456, parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() - .getPersistorParameters().getFlushPeriod()); - - final SuperDooperDistributorParameters infinispanParameters = - (SuperDooperDistributorParameters) parameters.getEngineServiceParameters().getEngineParameters() - .getContextParameters().getDistributorParameters(); - assertEquals("org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperDistributorParameters", - infinispanParameters.getClass().getName()); - assertEquals("my/lovely/configFile.xml", infinispanParameters.getConfigFile()); - assertEquals("holy/stone.xml", infinispanParameters.getJgroupsFile()); - assertEquals(false, infinispanParameters.isPreferIPv4Stack()); - assertEquals("fatherted", infinispanParameters.getJgroupsBindAddress()); - - } catch (final ParameterException e) { - fail("This test should not throw any exception: " + e.getMessage()); - } + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertEquals("org.onap.policy.apex.context.parameters.ContextParameters", parameters + .getEngineServiceParameters().getEngineParameters().getContextParameters().getClass().getName()); + assertEquals("org.onap.policy.apex.context.parameters.LockManagerParameters", + parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getLockManagerParameters().getClass().getName()); + assertEquals("org.onap.policy.apex.context.parameters.PersistorParameters", + parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getPersistorParameters().getClass().getName()); + assertEquals(123456, parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getPersistorParameters().getFlushPeriod()); + + final SuperDooperDistributorParameters infinispanParameters = + (SuperDooperDistributorParameters) parameters.getEngineServiceParameters().getEngineParameters() + .getContextParameters().getDistributorParameters(); + assertEquals("org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperDistributorParameters", + infinispanParameters.getClass().getName()); + assertEquals("my/lovely/configFile.xml", infinispanParameters.getConfigFile()); + assertEquals("holy/stone.xml", infinispanParameters.getJgroupsFile()); + assertEquals(false, infinispanParameters.isPreferIPv4Stack()); + assertEquals("fatherted", infinispanParameters.getJgroupsBindAddress()); + } @Test @@ -237,18 +200,14 @@ public class ContextParameterTests { final String[] args = {"-c", "src/test/resources/parameters/serviceContextBadClassDistParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from " - + "\"src/test/resources/parameters/serviceContextBadClassDistParams.json\"\n" - + "(ClassCastException):class " - + "org.onap.policy.apex.context.parameters.ContextParameters cannot be cast to class" - + " org.onap.policy.apex.context.parameters.DistributorParameters (org.onap.policy.apex.context." - + "parameters.ContextParameters and org.onap.policy.apex.context.parameters.DistributorParameters " - + "are in unnamed module of loader 'app')", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from " + + "\"src/test/resources/parameters/serviceContextBadClassDistParams.json\"\n" + + "(ClassCastException):class " + + "org.onap.policy.apex.context.parameters.ContextParameters cannot be cast to class" + + " org.onap.policy.apex.context.parameters.DistributorParameters (org.onap.policy.apex.context." + + "parameters.ContextParameters and org.onap.policy.apex.context.parameters.DistributorParameters " + + "are in unnamed module of loader 'app')"); } @Test @@ -256,18 +215,14 @@ public class ContextParameterTests { final String[] args = {"-c", "src/test/resources/parameters/serviceContextBadClassLockParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from " - + "\"src/test/resources/parameters/serviceContextBadClassLockParams.json\"\n" - + "(ClassCastException):class " - + "org.onap.policy.apex.context.parameters.ContextParameters cannot be cast to class " - + "org.onap.policy.apex.context.parameters.LockManagerParameters (org.onap.policy.apex.context." - + "parameters.ContextParameters and org.onap.policy.apex.context.parameters.LockManagerParameters " - + "are in unnamed module of loader 'app')", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from " + + "\"src/test/resources/parameters/serviceContextBadClassLockParams.json\"\n" + + "(ClassCastException):class " + + "org.onap.policy.apex.context.parameters.ContextParameters cannot be cast to class " + + "org.onap.policy.apex.context.parameters.LockManagerParameters (org.onap.policy.apex.context." + + "parameters.ContextParameters and org.onap.policy.apex.context.parameters.LockManagerParameters " + + "are in unnamed module of loader 'app')"); } @Test @@ -275,17 +230,13 @@ public class ContextParameterTests { final String[] args = {"-c", "src/test/resources/parameters/serviceContextBadClassPersistParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from " - + "\"src/test/resources/parameters/serviceContextBadClassPersistParams.json\"\n" - + "(ClassCastException):class " - + "org.onap.policy.apex.context.parameters.ContextParameters cannot be cast to class " - + "org.onap.policy.apex.context.parameters.PersistorParameters (org.onap.policy.apex.context." - + "parameters.ContextParameters and org.onap.policy.apex.context.parameters.PersistorParameters " - + "are in unnamed module of loader 'app')", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from " + + "\"src/test/resources/parameters/serviceContextBadClassPersistParams.json\"\n" + + "(ClassCastException):class " + + "org.onap.policy.apex.context.parameters.ContextParameters cannot be cast to class " + + "org.onap.policy.apex.context.parameters.PersistorParameters (org.onap.policy.apex.context." + + "parameters.ContextParameters and org.onap.policy.apex.context.parameters.PersistorParameters " + + "are in unnamed module of loader 'app')"); } } diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ExecutorParameterTests.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ExecutorParameterTests.java index 16a892f93..5d1b3f726 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ExecutorParameterTests.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ExecutorParameterTests.java @@ -21,9 +21,9 @@ package org.onap.policy.apex.service.engine.parameters; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import org.junit.After; import org.junit.Test; @@ -44,17 +44,14 @@ public class ExecutorParameterTests { } @Test - public void testNoParamsTest() { + public void testNoParamsTest() throws ParameterException { final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorNoParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - assertEquals(0, - parameters.getEngineServiceParameters().getEngineParameters().getExecutorParameterMap().size()); - } catch (final ParameterException e) { - fail("This test should not throw any exception: " + e.getMessage()); - } + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertEquals(0, + parameters.getEngineServiceParameters().getEngineParameters().getExecutorParameterMap().size()); + } @Test @@ -62,15 +59,11 @@ public class ExecutorParameterTests { final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorBadParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from " + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from " + "\"src/test/resources/parameters/serviceExecutorBadParams.json\"\n" + "(ParameterRuntimeException):value of \"executorParameters:ZOOBY\" entry is not " - + "a parameter JSON object", e.getMessage()); - } + + "a parameter JSON object"); } @Test @@ -78,15 +71,11 @@ public class ExecutorParameterTests { final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorNoExecutorParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from " - + "\"src/test/resources/parameters/serviceExecutorNoExecutorParams.json\"\n" - + "(ParameterRuntimeException):no \"executorParameters\" entry found in parameters," - + " at least one executor parameter entry must be specified", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from " + + "\"src/test/resources/parameters/serviceExecutorNoExecutorParams.json\"\n" + + "(ParameterRuntimeException):no \"executorParameters\" entry found in parameters," + + " at least one executor parameter entry must be specified"); } @Test @@ -94,15 +83,11 @@ public class ExecutorParameterTests { final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorEmptyParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from " - + "\"src/test/resources/parameters/serviceExecutorEmptyParams.json\"\n" - + "(ParameterRuntimeException):could not find field \"parameterClassName\" " - + "in \"executorParameters:ZOOBY\" entry", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from " + + "\"src/test/resources/parameters/serviceExecutorEmptyParams.json\"\n" + + "(ParameterRuntimeException):could not find field \"parameterClassName\" " + + "in \"executorParameters:ZOOBY\" entry"); } @Test @@ -110,15 +95,11 @@ public class ExecutorParameterTests { final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorBadPluginNameParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from " - + "\"src/test/resources/parameters/serviceExecutorBadPluginNameParams.json\"\n" - + "(ParameterRuntimeException):could not find field \"parameterClassName\" " - + "in \"executorParameters:ZOOBY\" entry", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from " + + "\"src/test/resources/parameters/serviceExecutorBadPluginNameParams.json\"\n" + + "(ParameterRuntimeException):could not find field \"parameterClassName\" " + + "in \"executorParameters:ZOOBY\" entry"); } @Test @@ -126,15 +107,11 @@ public class ExecutorParameterTests { final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorBadPluginValueObjectParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from " - + "\"src/test/resources/parameters/serviceExecutorBadPluginValueObjectParams.json\"\n" - + "(ParameterRuntimeException):value for field \"parameterClassName\" " - + "of \"executorParameters:LOOBY\" entry is not a plain string", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from " + + "\"src/test/resources/parameters/serviceExecutorBadPluginValueObjectParams.json\"\n" + + "(ParameterRuntimeException):value for field \"parameterClassName\" " + + "of \"executorParameters:LOOBY\" entry is not a plain string"); } @Test @@ -142,15 +119,11 @@ public class ExecutorParameterTests { final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorBadPluginValueBlankParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from " - + "\"src/test/resources/parameters/serviceExecutorBadPluginValueBlankParams.json\"\n" - + "(ParameterRuntimeException):value for field \"parameterClassName\" " - + "in \"executorParameters:LOOBY\" entry is not specified or is blank", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from " + + "\"src/test/resources/parameters/serviceExecutorBadPluginValueBlankParams.json\"\n" + + "(ParameterRuntimeException):value for field \"parameterClassName\" " + + "in \"executorParameters:LOOBY\" entry is not specified or is blank"); } @Test @@ -158,38 +131,31 @@ public class ExecutorParameterTests { final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorBadPluginValueParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from" - + " \"src/test/resources/parameters/serviceExecutorBadPluginValueParams.json\"\n" - + "(ParameterRuntimeException):failed to deserialize the parameters " - + "for \"executorParameters:LOOBY\" to parameter class \"helloworld\"\n" - + "java.lang.ClassNotFoundException: helloworld", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from" + + " \"src/test/resources/parameters/serviceExecutorBadPluginValueParams.json\"\n" + + "(ParameterRuntimeException):failed to deserialize the parameters " + + "for \"executorParameters:LOOBY\" to parameter class \"helloworld\"\n" + + "java.lang.ClassNotFoundException: helloworld"); } @Test - public void testGoodParametersTest() { + public void testGoodParametersTest() throws ParameterException { final String[] args = {"-c", "src/test/resources/parameters/goodParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - - assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName()); - assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion()); - assertEquals(45, parameters.getEngineServiceParameters().getId()); - assertEquals(19, parameters.getEngineServiceParameters().getInstanceCount()); - assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort()); - } catch (final ParameterException e) { - fail("This test should not throw any exception: " + e.getMessage()); - } + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + + assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName()); + assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion()); + assertEquals(45, parameters.getEngineServiceParameters().getId()); + assertEquals(19, parameters.getEngineServiceParameters().getInstanceCount()); + assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort()); + } @Test - public void testRelativeParametersTest() { + public void testRelativeParametersTest() throws ParameterException { // @formatter:off final String[] args = { "-rfr", @@ -200,18 +166,14 @@ public class ExecutorParameterTests { // @formatter:on final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - - assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName()); - assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion()); - assertEquals(45, parameters.getEngineServiceParameters().getId()); - assertEquals(19, parameters.getEngineServiceParameters().getInstanceCount()); - assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort()); - assertTrue(parameters.getEngineServiceParameters().getPolicyModelFileName() - .endsWith("policymodels/SmallModel.json")); - } catch (final ParameterException e) { - fail("This test should not throw any exception: " + e.getMessage()); - } + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + + assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName()); + assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion()); + assertEquals(45, parameters.getEngineServiceParameters().getId()); + assertEquals(19, parameters.getEngineServiceParameters().getInstanceCount()); + assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort()); + assertTrue(parameters.getEngineServiceParameters().getPolicyModelFileName() + .endsWith("policymodels/SmallModel.json")); } } diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ParameterTests.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ParameterTests.java index cbd9ee058..4948ab5f3 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ParameterTests.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ParameterTests.java @@ -21,10 +21,10 @@ package org.onap.policy.apex.service.engine.parameters; +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.Assert.fail; import java.util.Arrays; import org.junit.Test; @@ -51,13 +51,9 @@ public class ParameterTests { final String[] args = {"-c", "src/test/resources/parameters/invalidNoFile.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertTrue(e.getMessage().startsWith("error reading parameters from \"src")); - assertTrue(e.getMessage().contains("FileNotFoundException")); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessageStartingWith("error reading parameters from \"src") + .hasMessageContaining("FileNotFoundException"); } @Test @@ -65,13 +61,9 @@ public class ParameterTests { final String[] args = {"-c", "src/test/resources/parameters/empty.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertTrue(e.getMessage() - .startsWith("validation error(s) on parameters from \"src/test/resources/parameters/empty.json\"")); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessageStartingWith("validation error(s) on parameters from \"src/test/resources/parameters/empty" + + ".json\""); } @Test @@ -79,20 +71,16 @@ public class ParameterTests { final String[] args = {"-c", "src/test/resources/parameters/noParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("validation error(s) on parameters from \"src/test/resources/parameters/noParams.json\"\n" - + "parameter group \"APEX_PARAMETERS\" type " - + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " - + "parameter group has status INVALID\n" + " parameter group \"UNDEFINED\" INVALID, " - + "engine service parameters are not specified\n" - + " parameter group map \"eventOutputParameters\" INVALID, " - + "at least one event output must be specified\n" - + " parameter group map \"eventInputParameters\" INVALID, " - + "at least one event input must be specified\n", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("validation error(s) on parameters from \"src/test/resources/parameters/noParams.json\"\n" + + "parameter group \"APEX_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " + + "parameter group has status INVALID\n" + " parameter group \"UNDEFINED\" INVALID, " + + "engine service parameters are not specified\n" + + " parameter group map \"eventOutputParameters\" INVALID, " + + "at least one event output must be specified\n" + + " parameter group map \"eventInputParameters\" INVALID, " + + "at least one event input must be specified\n"); } @Test @@ -100,24 +88,19 @@ public class ParameterTests { final String[] args = {"-c", "src/test/resources/parameters/blankParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - - assertEquals("validation error(s) on parameters from \"src/test/resources/parameters/blankParams.json\"\n" - + "parameter group \"APEX_PARAMETERS\" type " - + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " - + "parameter group has status INVALID\n" + " parameter group \"ENGINE_SERVICE_PARAMETERS\" type " - + "\"org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters\" " - + "INVALID, parameter group has status INVALID\n" - + " field \"id\" type \"int\" value \"-1\" INVALID, " - + "id not specified or specified value [-1] invalid, must be specified as id >= 0\n" - + " parameter group map \"eventOutputParameters\" INVALID, " - + "at least one event output must be specified\n" - + " parameter group map \"eventInputParameters\" INVALID, " - + "at least one event input must be specified\n", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("validation error(s) on parameters from \"src/test/resources/parameters/blankParams.json\"\n" + + "parameter group \"APEX_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " + + "parameter group has status INVALID\n" + " parameter group \"ENGINE_SERVICE_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters\" " + + "INVALID, parameter group has status INVALID\n" + + " field \"id\" type \"int\" value \"-1\" INVALID, " + + "id not specified or specified value [-1] invalid, must be specified as id >= 0\n" + + " parameter group map \"eventOutputParameters\" INVALID, " + + "at least one event output must be specified\n" + + " parameter group map \"eventInputParameters\" INVALID, " + + "at least one event input must be specified\n"); } @Test @@ -125,45 +108,41 @@ public class ParameterTests { final String[] args = {"-c", "src/test/resources/parameters/badParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("validation error(s) on parameters from \"src/test/resources/parameters/badParams.json\"\n" - + "parameter group \"APEX_PARAMETERS\" type " - + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " - + "parameter group has status INVALID\n" + " parameter group \"hello there\" type " - + "\"org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters\" " - + "INVALID, parameter group has status INVALID\n" - + " field \"name\" type \"java.lang.String\" value \"hello there\" INVALID, " - + "name is invalid, it must match regular expression[A-Za-z0-9\\-_\\.]+\n" - + " field \"id\" type \"int\" value \"-45\" INVALID, id not specified or " - + "specified value [-45] invalid, must be specified as id >= 0\n" - + " field \"instanceCount\" type \"int\" value \"-345\" INVALID, " - + "instanceCount [-345] invalid, must be specified as instanceCount >= 1\n" - + " field \"deploymentPort\" type \"int\" value \"65536\" INVALID, " - + "deploymentPort [65536] invalid, must be specified as 1024 <= port <= 65535\n" - + " field \"policyModelFileName\" type \"java.lang.String\" " - + "value \"/some/file/name.xml\" INVALID, not found\n" - + " parameter group map \"eventOutputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"FirstProducer\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", parameter group has status INVALID\n" + " parameter group \"FILE\" type " - + "\"org.onap.policy.apex.service.engine.event.impl." - + "filecarrierplugin.FileCarrierTechnologyParameters\" INVALID, " - + "parameter group has status INVALID\n" - + " field \"fileName\" type \"java.lang.String\" value \"null\" INVALID, " - + "\"null\" invalid, must be specified as a non-empty string\n" - + " parameter group map \"eventInputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"TheFileConsumer1\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", parameter group has status INVALID\n" + " parameter group \"FILE\" type " - + "\"org.onap.policy.apex.service.engine.event.impl." - + "filecarrierplugin.FileCarrierTechnologyParameters\" INVALID, " - + "parameter group has status INVALID\n" - + " field \"fileName\" type \"java.lang.String\" value \"null\" INVALID, " - + "\"null\" invalid, must be specified as a non-empty string\n", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("validation error(s) on parameters from \"src/test/resources/parameters/badParams.json\"\n" + + "parameter group \"APEX_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " + + "parameter group has status INVALID\n" + " parameter group \"hello there\" type " + + "\"org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters\" " + + "INVALID, parameter group has status INVALID\n" + + " field \"name\" type \"java.lang.String\" value \"hello there\" INVALID, " + + "name is invalid, it must match regular expression[A-Za-z0-9\\-_\\.]+\n" + + " field \"id\" type \"int\" value \"-45\" INVALID, id not specified or " + + "specified value [-45] invalid, must be specified as id >= 0\n" + + " field \"instanceCount\" type \"int\" value \"-345\" INVALID, " + + "instanceCount [-345] invalid, must be specified as instanceCount >= 1\n" + + " field \"deploymentPort\" type \"int\" value \"65536\" INVALID, " + + "deploymentPort [65536] invalid, must be specified as 1024 <= port <= 65535\n" + + " field \"policyModelFileName\" type \"java.lang.String\" " + + "value \"/some/file/name.xml\" INVALID, not found\n" + + " parameter group map \"eventOutputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"FirstProducer\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", parameter group has status INVALID\n" + " parameter group \"FILE\" type " + + "\"org.onap.policy.apex.service.engine.event.impl." + + "filecarrierplugin.FileCarrierTechnologyParameters\" INVALID, " + + "parameter group has status INVALID\n" + + " field \"fileName\" type \"java.lang.String\" value \"null\" INVALID, " + + "\"null\" invalid, must be specified as a non-empty string\n" + + " parameter group map \"eventInputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"TheFileConsumer1\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", parameter group has status INVALID\n" + " parameter group \"FILE\" type " + + "\"org.onap.policy.apex.service.engine.event.impl." + + "filecarrierplugin.FileCarrierTechnologyParameters\" INVALID, " + + "parameter group has status INVALID\n" + + " field \"fileName\" type \"java.lang.String\" value \"null\" INVALID, " + + "\"null\" invalid, must be specified as a non-empty string\n"); } @Test @@ -171,125 +150,111 @@ public class ParameterTests { final String[] args = {"-c", "src/test/resources/parameters/badParamsModelNotFile.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals( - "validation error(s) on parameters from " - + "\"src/test/resources/parameters/badParamsModelNotFile.json\"\n" - + "parameter group \"APEX_PARAMETERS\" type " - + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " - + "parameter group has status INVALID\n" + " parameter group \"MyApexEngine\" type " - + "\"org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters\" " - + "INVALID, parameter group has status INVALID\n" + " field \"policyModelFileName\" " - + "type \"java.lang.String\" value \"src/test\" INVALID, is not a plain file\n", - e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("validation error(s) on parameters from " + + "\"src/test/resources/parameters/badParamsModelNotFile.json\"\n" + + "parameter group \"APEX_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " + + "parameter group has status INVALID\n" + " parameter group \"MyApexEngine\" type " + + "\"org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters\" " + + "INVALID, parameter group has status INVALID\n" + " field \"policyModelFileName\" " + + "type \"java.lang.String\" value \"src/test\" INVALID, is not a plain file\n"); } @Test - public void testGoodParametersTest() { + public void testGoodParametersTest() throws ParameterException { final String[] args = {"-c", "src/test/resources/parameters/goodParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - - assertEquals(2, parameters.getEventInputParameters().size()); - assertEquals(2, parameters.getEventOutputParameters().size()); + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - assertTrue(parameters.getEventOutputParameters().containsKey("FirstProducer")); - assertTrue(parameters.getEventOutputParameters().containsKey("MyOtherProducer")); - assertEquals("FILE", parameters.getEventOutputParameters().get("FirstProducer") - .getCarrierTechnologyParameters().getLabel()); - assertEquals("FILE", parameters.getEventOutputParameters().get("MyOtherProducer") - .getCarrierTechnologyParameters().getLabel()); - assertEquals(ApexFileEventProducer.class.getName(), parameters.getEventOutputParameters() - .get("MyOtherProducer").getCarrierTechnologyParameters().getEventProducerPluginClass()); - assertEquals(ApexFileEventConsumer.class.getName(), parameters.getEventOutputParameters() - .get("MyOtherProducer").getCarrierTechnologyParameters().getEventConsumerPluginClass()); - assertEquals("JSON", - parameters.getEventOutputParameters().get("FirstProducer").getEventProtocolParameters().getLabel()); - assertEquals("JSON", parameters.getEventOutputParameters().get("MyOtherProducer") - .getEventProtocolParameters().getLabel()); + assertEquals(2, parameters.getEventInputParameters().size()); + assertEquals(2, parameters.getEventOutputParameters().size()); + assertTrue(parameters.getEventOutputParameters().containsKey("FirstProducer")); + assertTrue(parameters.getEventOutputParameters().containsKey("MyOtherProducer")); + assertEquals("FILE", parameters.getEventOutputParameters().get("FirstProducer") + .getCarrierTechnologyParameters().getLabel()); + assertEquals("FILE", parameters.getEventOutputParameters().get("MyOtherProducer") + .getCarrierTechnologyParameters().getLabel()); + assertEquals(ApexFileEventProducer.class.getName(), parameters.getEventOutputParameters() + .get("MyOtherProducer").getCarrierTechnologyParameters().getEventProducerPluginClass()); + assertEquals(ApexFileEventConsumer.class.getName(), parameters.getEventOutputParameters() + .get("MyOtherProducer").getCarrierTechnologyParameters().getEventConsumerPluginClass()); + assertEquals("JSON", + parameters.getEventOutputParameters().get("FirstProducer").getEventProtocolParameters().getLabel()); + assertEquals("JSON", parameters.getEventOutputParameters().get("MyOtherProducer") + .getEventProtocolParameters().getLabel()); - assertTrue(parameters.getEventInputParameters().containsKey("TheFileConsumer1")); - assertTrue(parameters.getEventInputParameters().containsKey("MySuperDooperConsumer1")); - assertEquals("FILE", parameters.getEventInputParameters().get("TheFileConsumer1") - .getCarrierTechnologyParameters().getLabel()); - assertEquals("SUPER_DOOPER", parameters.getEventInputParameters().get("MySuperDooperConsumer1") - .getCarrierTechnologyParameters().getLabel()); - assertEquals("JSON", parameters.getEventInputParameters().get("TheFileConsumer1") - .getEventProtocolParameters().getLabel()); - assertEquals("SUPER_TOK_DEL", parameters.getEventInputParameters().get("MySuperDooperConsumer1") - .getEventProtocolParameters().getLabel()); - assertEquals(ApexFileEventProducer.class.getName(), parameters.getEventInputParameters() - .get("TheFileConsumer1").getCarrierTechnologyParameters().getEventProducerPluginClass()); - assertEquals(ApexFileEventConsumer.class.getName(), parameters.getEventInputParameters() - .get("TheFileConsumer1").getCarrierTechnologyParameters().getEventConsumerPluginClass()); - assertEquals(SuperDooperEventProducer.class.getName(), parameters.getEventInputParameters() - .get("MySuperDooperConsumer1").getCarrierTechnologyParameters().getEventProducerPluginClass()); - assertEquals(SuperDooperEventSubscriber.class.getName(), parameters.getEventInputParameters() - .get("MySuperDooperConsumer1").getCarrierTechnologyParameters().getEventConsumerPluginClass()); - } catch (final ParameterException e) { - fail("This test should not throw an exception"); - } + assertTrue(parameters.getEventInputParameters().containsKey("TheFileConsumer1")); + assertTrue(parameters.getEventInputParameters().containsKey("MySuperDooperConsumer1")); + assertEquals("FILE", parameters.getEventInputParameters().get("TheFileConsumer1") + .getCarrierTechnologyParameters().getLabel()); + assertEquals("SUPER_DOOPER", parameters.getEventInputParameters().get("MySuperDooperConsumer1") + .getCarrierTechnologyParameters().getLabel()); + assertEquals("JSON", parameters.getEventInputParameters().get("TheFileConsumer1") + .getEventProtocolParameters().getLabel()); + assertEquals("SUPER_TOK_DEL", parameters.getEventInputParameters().get("MySuperDooperConsumer1") + .getEventProtocolParameters().getLabel()); + assertEquals(ApexFileEventProducer.class.getName(), parameters.getEventInputParameters() + .get("TheFileConsumer1").getCarrierTechnologyParameters().getEventProducerPluginClass()); + assertEquals(ApexFileEventConsumer.class.getName(), parameters.getEventInputParameters() + .get("TheFileConsumer1").getCarrierTechnologyParameters().getEventConsumerPluginClass()); + assertEquals(SuperDooperEventProducer.class.getName(), parameters.getEventInputParameters() + .get("MySuperDooperConsumer1").getCarrierTechnologyParameters().getEventProducerPluginClass()); + assertEquals(SuperDooperEventSubscriber.class.getName(), parameters.getEventInputParameters() + .get("MySuperDooperConsumer1").getCarrierTechnologyParameters().getEventConsumerPluginClass()); } @Test - public void testSuperDooperParametersTest() { + public void testSuperDooperParametersTest() throws ParameterException { final String[] args = {"-c", "src/test/resources/parameters/superDooperParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + + assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName()); + assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion()); + assertEquals(45, parameters.getEngineServiceParameters().getId()); + assertEquals(345, parameters.getEngineServiceParameters().getInstanceCount()); + assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort()); - assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName()); - assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion()); - assertEquals(45, parameters.getEngineServiceParameters().getId()); - assertEquals(345, parameters.getEngineServiceParameters().getInstanceCount()); - assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort()); + final CarrierTechnologyParameters prodCarrierTech = + parameters.getEventOutputParameters().get("FirstProducer").getCarrierTechnologyParameters(); + final EventProtocolParameters prodEventProt = + parameters.getEventOutputParameters().get("FirstProducer").getEventProtocolParameters(); + final CarrierTechnologyParameters consCarrierTech = + parameters.getEventInputParameters().get("MySuperDooperConsumer1").getCarrierTechnologyParameters(); + final EventProtocolParameters consEventProt = + parameters.getEventInputParameters().get("MySuperDooperConsumer1").getEventProtocolParameters(); - final CarrierTechnologyParameters prodCarrierTech = - parameters.getEventOutputParameters().get("FirstProducer").getCarrierTechnologyParameters(); - final EventProtocolParameters prodEventProt = - parameters.getEventOutputParameters().get("FirstProducer").getEventProtocolParameters(); - final CarrierTechnologyParameters consCarrierTech = - parameters.getEventInputParameters().get("MySuperDooperConsumer1").getCarrierTechnologyParameters(); - final EventProtocolParameters consEventProt = - parameters.getEventInputParameters().get("MySuperDooperConsumer1").getEventProtocolParameters(); + assertEquals("SUPER_DOOPER", prodCarrierTech.getLabel()); + assertEquals("SUPER_TOK_DEL", prodEventProt.getLabel()); + assertEquals("SUPER_DOOPER", consCarrierTech.getLabel()); + assertEquals("JSON", consEventProt.getLabel()); - assertEquals("SUPER_DOOPER", prodCarrierTech.getLabel()); - assertEquals("SUPER_TOK_DEL", prodEventProt.getLabel()); - assertEquals("SUPER_DOOPER", consCarrierTech.getLabel()); - assertEquals("JSON", consEventProt.getLabel()); + assertTrue(prodCarrierTech instanceof SuperDooperCarrierTechnologyParameters); - assertTrue(prodCarrierTech instanceof SuperDooperCarrierTechnologyParameters); + final SuperDooperCarrierTechnologyParameters superDooperParameters = + (SuperDooperCarrierTechnologyParameters) prodCarrierTech; + assertEquals("somehost:12345", superDooperParameters.getBootstrapServers()); + assertEquals("0", superDooperParameters.getAcks()); + assertEquals(25, superDooperParameters.getRetries()); + assertEquals(98765, superDooperParameters.getBatchSize()); + assertEquals(21, superDooperParameters.getLingerTime()); + assertEquals(50505050, superDooperParameters.getBufferMemory()); + assertEquals("first-group-id", superDooperParameters.getGroupId()); + assertFalse(superDooperParameters.isEnableAutoCommit()); + assertEquals(441, superDooperParameters.getAutoCommitTime()); + assertEquals(987, superDooperParameters.getSessionTimeout()); + assertEquals("producer-out", superDooperParameters.getProducerTopic()); + assertEquals(101, superDooperParameters.getConsumerPollTime()); + assertEquals("some.key.serailizer", superDooperParameters.getKeySerializer()); + assertEquals("some.value.serailizer", superDooperParameters.getValueSerializer()); + assertEquals("some.key.deserailizer", superDooperParameters.getKeyDeserializer()); + assertEquals("some.value.deserailizer", superDooperParameters.getValueDeserializer()); - final SuperDooperCarrierTechnologyParameters superDooperParameters = - (SuperDooperCarrierTechnologyParameters) prodCarrierTech; - assertEquals("somehost:12345", superDooperParameters.getBootstrapServers()); - assertEquals("0", superDooperParameters.getAcks()); - assertEquals(25, superDooperParameters.getRetries()); - assertEquals(98765, superDooperParameters.getBatchSize()); - assertEquals(21, superDooperParameters.getLingerTime()); - assertEquals(50505050, superDooperParameters.getBufferMemory()); - assertEquals("first-group-id", superDooperParameters.getGroupId()); - assertFalse(superDooperParameters.isEnableAutoCommit()); - assertEquals(441, superDooperParameters.getAutoCommitTime()); - assertEquals(987, superDooperParameters.getSessionTimeout()); - assertEquals("producer-out", superDooperParameters.getProducerTopic()); - assertEquals(101, superDooperParameters.getConsumerPollTime()); - assertEquals("some.key.serailizer", superDooperParameters.getKeySerializer()); - assertEquals("some.value.serailizer", superDooperParameters.getValueSerializer()); - assertEquals("some.key.deserailizer", superDooperParameters.getKeyDeserializer()); - assertEquals("some.value.deserailizer", superDooperParameters.getValueDeserializer()); + final String[] consumerTopics = {"consumer-out-0", "consumer-out-1", "consumer-out-2"}; + assertEquals(Arrays.asList(consumerTopics), superDooperParameters.getConsumerTopicList()); - final String[] consumerTopics = {"consumer-out-0", "consumer-out-1", "consumer-out-2"}; - assertEquals(Arrays.asList(consumerTopics), superDooperParameters.getConsumerTopicList()); - } catch (final ParameterException e) { - fail("This test should not throw an exception"); - } } } diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ProducerConsumerTests.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ProducerConsumerTests.java index df1960aa0..a80aa09c8 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ProducerConsumerTests.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ProducerConsumerTests.java @@ -21,6 +21,7 @@ package org.onap.policy.apex.service.engine.parameters; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -39,37 +40,33 @@ import org.onap.policy.common.parameters.ParameterException; */ public class ProducerConsumerTests { @Test - public void testGoodParametersTest() { + public void testGoodParametersTest() throws ParameterException { final String[] args = {"-c", "src/test/resources/parameters/goodParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - - assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName()); - assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion()); - assertEquals(45, parameters.getEngineServiceParameters().getId()); - assertEquals(19, parameters.getEngineServiceParameters().getInstanceCount()); - assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort()); - assertEquals("FILE", parameters.getEventOutputParameters().get("FirstProducer") - .getCarrierTechnologyParameters().getLabel()); - assertEquals("JSON", - parameters.getEventOutputParameters().get("FirstProducer").getEventProtocolParameters().getLabel()); - assertEquals("FILE", parameters.getEventOutputParameters().get("MyOtherProducer") - .getCarrierTechnologyParameters().getLabel()); - assertEquals("JSON", parameters.getEventOutputParameters().get("MyOtherProducer") - .getEventProtocolParameters().getLabel()); - assertEquals("FILE", parameters.getEventInputParameters().get("TheFileConsumer1") - .getCarrierTechnologyParameters().getLabel()); - assertEquals("JSON", parameters.getEventInputParameters().get("TheFileConsumer1") - .getEventProtocolParameters().getLabel()); - assertEquals("SUPER_DOOPER", parameters.getEventInputParameters().get("MySuperDooperConsumer1") - .getCarrierTechnologyParameters().getLabel()); - assertEquals("SUPER_TOK_DEL", parameters.getEventInputParameters().get("MySuperDooperConsumer1") - .getEventProtocolParameters().getLabel()); - } catch (final ParameterException e) { - fail("This test should not throw an exception"); - } + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + + assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName()); + assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion()); + assertEquals(45, parameters.getEngineServiceParameters().getId()); + assertEquals(19, parameters.getEngineServiceParameters().getInstanceCount()); + assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort()); + assertEquals("FILE", parameters.getEventOutputParameters().get("FirstProducer") + .getCarrierTechnologyParameters().getLabel()); + assertEquals("JSON", + parameters.getEventOutputParameters().get("FirstProducer").getEventProtocolParameters().getLabel()); + assertEquals("FILE", parameters.getEventOutputParameters().get("MyOtherProducer") + .getCarrierTechnologyParameters().getLabel()); + assertEquals("JSON", parameters.getEventOutputParameters().get("MyOtherProducer") + .getEventProtocolParameters().getLabel()); + assertEquals("FILE", parameters.getEventInputParameters().get("TheFileConsumer1") + .getCarrierTechnologyParameters().getLabel()); + assertEquals("JSON", parameters.getEventInputParameters().get("TheFileConsumer1") + .getEventProtocolParameters().getLabel()); + assertEquals("SUPER_DOOPER", parameters.getEventInputParameters().get("MySuperDooperConsumer1") + .getCarrierTechnologyParameters().getLabel()); + assertEquals("SUPER_TOK_DEL", parameters.getEventInputParameters().get("MySuperDooperConsumer1") + .getEventProtocolParameters().getLabel()); } @Test @@ -77,20 +74,16 @@ public class ProducerConsumerTests { final String[] args = {"-c", "src/test/resources/parameters/prodConsNoCT.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("validation error(s) on parameters from \"src/test/resources/parameters/prodConsNoCT.json\"\n" - + "parameter group \"APEX_PARAMETERS\" type " - + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " - + "parameter group has status INVALID\n" - + " parameter group map \"eventInputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"aConsumer\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID," - + " parameter group has status INVALID\n" + " parameter group \"UNDEFINED\" INVALID, " - + "event handler carrierTechnologyParameters not specified or blank\n", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("validation error(s) on parameters from \"src/test/resources/parameters/prodConsNoCT.json\"\n" + + "parameter group \"APEX_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " + + "parameter group has status INVALID\n" + + " parameter group map \"eventInputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"aConsumer\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID," + + " parameter group has status INVALID\n" + " parameter group \"UNDEFINED\" INVALID, " + + "event handler carrierTechnologyParameters not specified or blank\n"); } @Test @@ -98,29 +91,25 @@ public class ProducerConsumerTests { final String[] args = {"-c", "src/test/resources/parameters/prodConsNoEP.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("validation error(s) on parameters from \"src/test/resources/parameters/prodConsNoEP.json\"\n" - + "parameter group \"APEX_PARAMETERS\" type " - + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " - + "parameter group has status INVALID\n" - + " parameter group map \"eventOutputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"aProducer\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", parameter group has status INVALID\n" + " parameter group \"UNDEFINED\" INVALID, " - + "event handler eventProtocolParameters not specified or blank\n" - + " parameter group map \"eventInputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"aConsumer\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", parameter group has status INVALID\n" + " parameter group \"FILE\" type " - + "\"org.onap.policy.apex.service.engine.event.impl." - + "filecarrierplugin.FileCarrierTechnologyParameters\" INVALID, " - + "parameter group has status INVALID\n" - + " field \"fileName\" type \"java.lang.String\" value \"null\" INVALID, " - + "\"null\" invalid, must be specified as a non-empty string\n", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("validation error(s) on parameters from \"src/test/resources/parameters/prodConsNoEP.json\"\n" + + "parameter group \"APEX_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " + + "parameter group has status INVALID\n" + + " parameter group map \"eventOutputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"aProducer\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", parameter group has status INVALID\n" + " parameter group \"UNDEFINED\" INVALID, " + + "event handler eventProtocolParameters not specified or blank\n" + + " parameter group map \"eventInputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"aConsumer\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", parameter group has status INVALID\n" + " parameter group \"FILE\" type " + + "\"org.onap.policy.apex.service.engine.event.impl." + + "filecarrierplugin.FileCarrierTechnologyParameters\" INVALID, " + + "parameter group has status INVALID\n" + + " field \"fileName\" type \"java.lang.String\" value \"null\" INVALID, " + + "\"null\" invalid, must be specified as a non-empty string\n"); } @Test @@ -128,14 +117,10 @@ public class ProducerConsumerTests { final String[] args = {"-c", "src/test/resources/parameters/prodConsNoCTParClass.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from \"src/test/resources/parameters/prodConsNoCTParClass.json\"\n" - + "(ParameterRuntimeException):carrier technology \"SUPER_DOOPER\" " - + "parameter \"parameterClassName\" value \"null\" invalid in JSON file", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from \"src/test/resources/parameters/prodConsNoCTParClass.json\"\n" + + "(ParameterRuntimeException):carrier technology \"SUPER_DOOPER\" " + + "parameter \"parameterClassName\" value \"null\" invalid in JSON file"); } @Test @@ -143,18 +128,14 @@ public class ProducerConsumerTests { final String[] args = {"-c", "src/test/resources/parameters/prodConsMismatchCTParClass.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from " - + "\"src/test/resources/parameters/prodConsMismatchCTParClass.json\"\n" - + "(ParameterRuntimeException):carrier technology \"SUPER_LOOPER\" " - + "does not match plugin \"SUPER_DOOPER\" in \"" + "org.onap.policy.apex.service.engine." - + "parameters.dummyclasses.SuperDooperCarrierTechnologyParameters" - + "\", specify correct carrier technology parameter plugin " - + "in parameter \"parameterClassName\"", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from " + + "\"src/test/resources/parameters/prodConsMismatchCTParClass.json\"\n" + + "(ParameterRuntimeException):carrier technology \"SUPER_LOOPER\" " + + "does not match plugin \"SUPER_DOOPER\" in \"" + "org.onap.policy.apex.service.engine." + + "parameters.dummyclasses.SuperDooperCarrierTechnologyParameters" + + "\", specify correct carrier technology parameter plugin " + + "in parameter \"parameterClassName\""); } @Test @@ -162,38 +143,31 @@ public class ProducerConsumerTests { final String[] args = {"-c", "src/test/resources/parameters/prodConsWrongTypeCTParClass.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from " - + "\"src/test/resources/parameters/prodConsWrongTypeCTParClass.json\"\n" - + "(ParameterRuntimeException):could not create default parameters for carrier technology " - + "\"SUPER_DOOPER\"\n" + "class org.onap.policy.apex.service.engine.parameters.dummyclasses." - + "SuperTokenDelimitedEventProtocolParameters cannot be cast to class " - + "org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters (org.onap." - + "policy.apex.service.engine.parameters.dummyclasses.SuperTokenDelimitedEventProtocolParameters " - + "and org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters are in" - + " unnamed module of loader 'app')", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from " + + "\"src/test/resources/parameters/prodConsWrongTypeCTParClass.json\"\n" + + "(ParameterRuntimeException):could not create default parameters for carrier technology " + + "\"SUPER_DOOPER\"\n" + "class org.onap.policy.apex.service.engine.parameters.dummyclasses." + + "SuperTokenDelimitedEventProtocolParameters cannot be cast to class " + + "org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters (org.onap." + + "policy.apex.service.engine.parameters.dummyclasses.SuperTokenDelimitedEventProtocolParameters " + + "and org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters are in" + + " unnamed module of loader 'app')"); } @Test - public void testOkFileNameCarrierTechnology() { + public void testOkFileNameCarrierTechnology() throws ParameterException { final String[] args = {"-c", "src/test/resources/parameters/prodConsOKFileName.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - final FileCarrierTechnologyParameters fileParams = (FileCarrierTechnologyParameters) parameters - .getEventOutputParameters().get("aProducer").getCarrierTechnologyParameters(); - assertTrue(fileParams.getFileName().endsWith("target/aaa.json")); - assertEquals(false, fileParams.isStandardError()); - assertEquals(false, fileParams.isStandardIo()); - assertEquals(false, fileParams.isStreamingMode()); - } catch (final ParameterException e) { - fail("This test should not throw an exception"); - } + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + final FileCarrierTechnologyParameters fileParams = (FileCarrierTechnologyParameters) parameters + .getEventOutputParameters().get("aProducer").getCarrierTechnologyParameters(); + assertTrue(fileParams.getFileName().endsWith("target/aaa.json")); + assertEquals(false, fileParams.isStandardError()); + assertEquals(false, fileParams.isStandardIo()); + assertEquals(false, fileParams.isStreamingMode()); + } @Test @@ -201,25 +175,21 @@ public class ProducerConsumerTests { final String[] args = {"-c", "src/test/resources/parameters/prodConsBadFileName.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("validation error(s) on parameters from " - + "\"src/test/resources/parameters/prodConsBadFileName.json\"\n" - + "parameter group \"APEX_PARAMETERS\" type " - + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " - + "parameter group has status INVALID\n" - + " parameter group map \"eventOutputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"aProducer\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" " - + "INVALID, parameter group has status INVALID\n" + " parameter group \"FILE\" type " - + "\"org.onap.policy.apex.service.engine.event.impl." - + "filecarrierplugin.FileCarrierTechnologyParameters\" INVALID, " - + "parameter group has status INVALID\n" + " field \"fileName\" type " - + "\"java.lang.String\" value \"null\" INVALID, " - + "\"null\" invalid, must be specified as a non-empty string\n", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("validation error(s) on parameters from " + + "\"src/test/resources/parameters/prodConsBadFileName.json\"\n" + + "parameter group \"APEX_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " + + "parameter group has status INVALID\n" + + " parameter group map \"eventOutputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"aProducer\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" " + + "INVALID, parameter group has status INVALID\n" + " parameter group \"FILE\" type " + + "\"org.onap.policy.apex.service.engine.event.impl." + + "filecarrierplugin.FileCarrierTechnologyParameters\" INVALID, " + + "parameter group has status INVALID\n" + " field \"fileName\" type " + + "\"java.lang.String\" value \"null\" INVALID, " + + "\"null\" invalid, must be specified as a non-empty string\n"); } @Test @@ -227,18 +197,12 @@ public class ProducerConsumerTests { final String[] args = {"-c", "src/test/resources/parameters/prodConsBadEPParClass.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals( - "error reading parameters from \"src/test/resources/parameters/prodConsBadEPParClass.json\"\n" - + "(ParameterRuntimeException):event protocol \"SUPER_TOK_DEL\" " - + "does not match plugin \"JSON\" in \"org.onap.policy.apex.service.engine.event.impl" - + ".jsonprotocolplugin.JsonEventProtocolParameters" - + "\", specify correct event protocol parameter plugin in parameter \"parameterClassName\"", - e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from \"src/test/resources/parameters/prodConsBadEPParClass.json\"\n" + + "(ParameterRuntimeException):event protocol \"SUPER_TOK_DEL\" " + + "does not match plugin \"JSON\" in \"org.onap.policy.apex.service.engine.event.impl" + + ".jsonprotocolplugin.JsonEventProtocolParameters" + + "\", specify correct event protocol parameter plugin in parameter \"parameterClassName\""); } @Test @@ -246,14 +210,10 @@ public class ProducerConsumerTests { final String[] args = {"-c", "src/test/resources/parameters/prodConsNoEPParClass.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from \"src/test/resources/parameters/prodConsNoEPParClass.json\"\n" - + "(ParameterRuntimeException):event protocol \"SUPER_TOK_DEL\" parameter " - + "\"parameterClassName\" value \"null\" invalid in JSON file", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from \"src/test/resources/parameters/prodConsNoEPParClass.json\"\n" + + "(ParameterRuntimeException):event protocol \"SUPER_TOK_DEL\" parameter " + + "\"parameterClassName\" value \"null\" invalid in JSON file"); } @Test @@ -261,20 +221,14 @@ public class ProducerConsumerTests { final String[] args = {"-c", "src/test/resources/parameters/prodConsMismatchEPParClass.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals( - "error reading parameters from " - + "\"src/test/resources/parameters/prodConsMismatchEPParClass.json\"\n" - + "(ParameterRuntimeException):event protocol \"SUPER_TOK_BEL\" " - + "does not match plugin \"SUPER_TOK_DEL\" in " - + "\"org.onap.policy.apex.service.engine.parameters.dummyclasses." - + "SuperTokenDelimitedEventProtocolParameters\", " - + "specify correct event protocol parameter plugin in parameter \"parameterClassName\"", - e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from " + + "\"src/test/resources/parameters/prodConsMismatchEPParClass.json\"\n" + + "(ParameterRuntimeException):event protocol \"SUPER_TOK_BEL\" " + + "does not match plugin \"SUPER_TOK_DEL\" in " + + "\"org.onap.policy.apex.service.engine.parameters.dummyclasses." + + "SuperTokenDelimitedEventProtocolParameters\", " + + "specify correct event protocol parameter plugin in parameter \"parameterClassName\""); } @Test @@ -282,19 +236,15 @@ public class ProducerConsumerTests { final String[] args = {"-c", "src/test/resources/parameters/prodConsWrongTypeEPParClass.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("error reading parameters from " - + "\"src/test/resources/parameters/prodConsWrongTypeEPParClass.json\"\n" - + "(ParameterRuntimeException):could not create default parameters for event protocol " - + "\"SUPER_TOK_DEL\"\n" + "class org.onap.policy.apex.service.engine." - + "parameters.dummyclasses.SuperDooperCarrierTechnologyParameters " - + "cannot be cast to class org.onap.policy.apex.service." - + "parameters.eventprotocol.EventProtocolParameters (org.onap.policy.apex.service.engine.parameters" - + ".dummyclasses.SuperDooperCarrierTechnologyParameters and org.onap.policy.apex.service.parameters" - + ".eventprotocol.EventProtocolParameters are in unnamed module of loader 'app')", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("error reading parameters from " + + "\"src/test/resources/parameters/prodConsWrongTypeEPParClass.json\"\n" + + "(ParameterRuntimeException):could not create default parameters for event protocol " + + "\"SUPER_TOK_DEL\"\n" + "class org.onap.policy.apex.service.engine." + + "parameters.dummyclasses.SuperDooperCarrierTechnologyParameters " + + "cannot be cast to class org.onap.policy.apex.service." + + "parameters.eventprotocol.EventProtocolParameters (org.onap.policy.apex.service.engine.parameters" + + ".dummyclasses.SuperDooperCarrierTechnologyParameters and org.onap.policy.apex.service.parameters" + + ".eventprotocol.EventProtocolParameters are in unnamed module of loader 'app')"); } } diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/SyncParameterTests.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/SyncParameterTests.java index be2c22c93..91f8c6699 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/SyncParameterTests.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/SyncParameterTests.java @@ -21,9 +21,9 @@ package org.onap.policy.apex.service.engine.parameters; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.util.Arrays; import org.junit.Test; @@ -48,21 +48,17 @@ public class SyncParameterTests { final String[] args = {"-c", "src/test/resources/parameters/syncBadParamsNoSyncWithPeer.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("validation error(s) on parameters from " - + "\"src/test/resources/parameters/syncBadParamsNoSyncWithPeer.json\"\n" - + "parameter group \"APEX_PARAMETERS\" type " - + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " - + "parameter group has status INVALID\n" - + " parameter group map \"eventOutputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"SyncProducer0\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", specified peered mode \"SYNCHRONOUS\" " - + "peer is illegal on eventOutputParameters \"SyncProducer0\" \n", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("validation error(s) on parameters from " + + "\"src/test/resources/parameters/syncBadParamsNoSyncWithPeer.json\"\n" + + "parameter group \"APEX_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " + + "parameter group has status INVALID\n" + + " parameter group map \"eventOutputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"SyncProducer0\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", specified peered mode \"SYNCHRONOUS\" " + + "peer is illegal on eventOutputParameters \"SyncProducer0\" \n"); } @Test @@ -70,21 +66,17 @@ public class SyncParameterTests { final String[] args = {"-c", "src/test/resources/parameters/syncBadParamsNotSyncWithPeer.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("validation error(s) on parameters from " - + "\"src/test/resources/parameters/syncBadParamsNotSyncWithPeer.json\"\n" - + "parameter group \"APEX_PARAMETERS\" type " - + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " - + "parameter group has status INVALID\n" - + " parameter group map \"eventOutputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"SyncProducer0\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", specified peered mode \"SYNCHRONOUS\" peer is illegal " - + "on eventOutputParameters \"SyncProducer0\" \n", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("validation error(s) on parameters from " + + "\"src/test/resources/parameters/syncBadParamsNotSyncWithPeer.json\"\n" + + "parameter group \"APEX_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " + + "parameter group has status INVALID\n" + + " parameter group map \"eventOutputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"SyncProducer0\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", specified peered mode \"SYNCHRONOUS\" peer is illegal " + + "on eventOutputParameters \"SyncProducer0\" \n"); } @Test @@ -92,32 +84,28 @@ public class SyncParameterTests { final String[] args = {"-c", "src/test/resources/parameters/syncBadParamsBadPeers.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("validation error(s) on parameters from " - + "\"src/test/resources/parameters/syncBadParamsBadPeers.json\"\n" - + "parameter group \"APEX_PARAMETERS\" type " - + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " - + "parameter group has status INVALID\n" - + " parameter group map \"eventOutputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"SyncProducer0\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", peer \"SyncProducer1 for peered mode SYNCHRONOUS does not exist " - + "or is not defined with the same peered mode\n" + " parameter group \"SyncProducer1\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", peer \"SyncProducer0 for peered mode SYNCHRONOUS does not exist " - + "or is not defined with the same peered mode\n" - + " parameter group map \"eventInputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"SyncConsumer0\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", peer \"SyncConsumer1 for peered mode SYNCHRONOUS does not exist " - + "or is not defined with the same peered mode\n" + " parameter group \"SyncConsumer1\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", peer \"SyncConsumer0 for peered mode SYNCHRONOUS does not exist " - + "or is not defined with the same peered mode\n", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("validation error(s) on parameters from " + + "\"src/test/resources/parameters/syncBadParamsBadPeers.json\"\n" + + "parameter group \"APEX_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " + + "parameter group has status INVALID\n" + + " parameter group map \"eventOutputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"SyncProducer0\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", peer \"SyncProducer1 for peered mode SYNCHRONOUS does not exist " + + "or is not defined with the same peered mode\n" + " parameter group \"SyncProducer1\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", peer \"SyncProducer0 for peered mode SYNCHRONOUS does not exist " + + "or is not defined with the same peered mode\n" + + " parameter group map \"eventInputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"SyncConsumer0\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", peer \"SyncConsumer1 for peered mode SYNCHRONOUS does not exist " + + "or is not defined with the same peered mode\n" + " parameter group \"SyncConsumer1\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", peer \"SyncConsumer0 for peered mode SYNCHRONOUS does not exist " + + "or is not defined with the same peered mode\n"); } @Test @@ -125,34 +113,30 @@ public class SyncParameterTests { final String[] args = {"-c", "src/test/resources/parameters/syncBadParamsInvalidTimeout.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("validation error(s) on parameters from " - + "\"src/test/resources/parameters/syncBadParamsInvalidTimeout.json\"\n" - + "parameter group \"APEX_PARAMETERS\" type " - + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " - + "parameter group has status INVALID\n" - + " parameter group map \"eventOutputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"SyncProducer0\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID," - + " specified peered mode \"SYNCHRONOUS\" timeout value \"-10\" is illegal, " - + "specify a non-negative timeout value in milliseconds\n" - + " parameter group \"SyncProducer1\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" " - + "INVALID, specified peered mode \"SYNCHRONOUS\" timeout value \"-3\" is illegal, " - + "specify a non-negative timeout value in milliseconds\n" - + " parameter group map \"eventInputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"SyncConsumer0\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" " - + "INVALID, specified peered mode \"SYNCHRONOUS\" timeout value \"-1\" is illegal, " - + "specify a non-negative timeout value in milliseconds\n" - + " parameter group \"SyncConsumer1\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID," - + " specified peered mode \"SYNCHRONOUS\" timeout value \"-99999999\" is illegal, " - + "specify a non-negative timeout value in milliseconds\n", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("validation error(s) on parameters from " + + "\"src/test/resources/parameters/syncBadParamsInvalidTimeout.json\"\n" + + "parameter group \"APEX_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " + + "parameter group has status INVALID\n" + + " parameter group map \"eventOutputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"SyncProducer0\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID," + + " specified peered mode \"SYNCHRONOUS\" timeout value \"-10\" is illegal, " + + "specify a non-negative timeout value in milliseconds\n" + + " parameter group \"SyncProducer1\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" " + + "INVALID, specified peered mode \"SYNCHRONOUS\" timeout value \"-3\" is illegal, " + + "specify a non-negative timeout value in milliseconds\n" + + " parameter group map \"eventInputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"SyncConsumer0\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" " + + "INVALID, specified peered mode \"SYNCHRONOUS\" timeout value \"-1\" is illegal, " + + "specify a non-negative timeout value in milliseconds\n" + + " parameter group \"SyncConsumer1\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID," + + " specified peered mode \"SYNCHRONOUS\" timeout value \"-99999999\" is illegal, " + + "specify a non-negative timeout value in milliseconds\n"); } @Test @@ -160,21 +144,17 @@ public class SyncParameterTests { final String[] args = {"-c", "src/test/resources/parameters/syncBadParamsBadTimeout.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("validation error(s) on parameters from " - + "\"src/test/resources/parameters/syncBadParamsBadTimeout.json\"\n" - + "parameter group \"APEX_PARAMETERS\" type " - + "\"org.onap.policy.apex.service.parameters.ApexParameters\" " - + "INVALID, parameter group has status INVALID\n" - + " parameter group map \"eventOutputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"MyOtherProducer\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" " - + "INVALID, specified peered mode \"SYNCHRONOUS\" " - + "timeout is illegal on eventOutputParameters \"MyOtherProducer\"\n", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("validation error(s) on parameters from " + + "\"src/test/resources/parameters/syncBadParamsBadTimeout.json\"\n" + + "parameter group \"APEX_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.ApexParameters\" " + + "INVALID, parameter group has status INVALID\n" + + " parameter group map \"eventOutputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"MyOtherProducer\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" " + + "INVALID, specified peered mode \"SYNCHRONOUS\" " + + "timeout is illegal on eventOutputParameters \"MyOtherProducer\"\n"); } @Test @@ -182,36 +162,30 @@ public class SyncParameterTests { final String[] args = {"-c", "src/test/resources/parameters/syncBadParamsUnpairedTimeout.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals( - "validation error(s) on parameters from " - + "\"src/test/resources/parameters/syncBadParamsUnpairedTimeout.json\"\n" - + "parameter group \"APEX_PARAMETERS\" type " - + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " - + "parameter group has status INVALID\n" - + " parameter group map \"eventOutputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"SyncProducer0\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", peer \"SyncConsumer0 for peered mode SYNCHRONOUS timeout 10 on event handler " - + "\"SyncProducer0\" does not equal timeout 1 on event handler \"SyncConsumer0\"\n" - + " parameter group \"SyncProducer1\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", peer \"SyncConsumer1 for peered mode SYNCHRONOUS timeout 3 on event handler " - + "\"SyncProducer1\" does not equal timeout 99999999 on event handler \"SyncConsumer1\"\n" - + " parameter group map \"eventInputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"SyncConsumer0\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", peer \"SyncProducer0 for peered mode SYNCHRONOUS timeout 1 on event handler " - + "\"SyncConsumer0\" does not equal timeout 10 on event handler \"SyncProducer0\"\n" - + " parameter group \"SyncConsumer1\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", peer \"SyncProducer1 for peered mode SYNCHRONOUS timeout 99999999 on event handler " - + "\"SyncConsumer1\" does not equal timeout 3 on event handler \"SyncProducer1\"\n" + "", - e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("validation error(s) on parameters from " + + "\"src/test/resources/parameters/syncBadParamsUnpairedTimeout.json\"\n" + + "parameter group \"APEX_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " + + "parameter group has status INVALID\n" + + " parameter group map \"eventOutputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"SyncProducer0\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", peer \"SyncConsumer0 for peered mode SYNCHRONOUS timeout 10 on event handler " + + "\"SyncProducer0\" does not equal timeout 1 on event handler \"SyncConsumer0\"\n" + + " parameter group \"SyncProducer1\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", peer \"SyncConsumer1 for peered mode SYNCHRONOUS timeout 3 on event handler " + + "\"SyncProducer1\" does not equal timeout 99999999 on event handler \"SyncConsumer1\"\n" + + " parameter group map \"eventInputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"SyncConsumer0\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", peer \"SyncProducer0 for peered mode SYNCHRONOUS timeout 1 on event handler " + + "\"SyncConsumer0\" does not equal timeout 10 on event handler \"SyncProducer0\"\n" + + " parameter group \"SyncConsumer1\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", peer \"SyncProducer1 for peered mode SYNCHRONOUS timeout 99999999 on event handler " + + "\"SyncConsumer1\" does not equal timeout 3 on event handler \"SyncProducer1\"\n" + ""); } @Test @@ -219,19 +193,16 @@ public class SyncParameterTests { final String[] args = {"-c", "src/test/resources/parameters/syncGoodParamsProducerTimeout.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - assertEquals(12345, parameters.getEventInputParameters().get("SyncConsumer0") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - assertEquals(1, parameters.getEventInputParameters().get("SyncConsumer1") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - assertEquals(12345, parameters.getEventOutputParameters().get("SyncProducer0") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - assertEquals(1, parameters.getEventOutputParameters().get("SyncProducer1") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - } catch (final Exception e) { - fail("This test should not throw an exception"); - } + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertEquals(12345, parameters.getEventInputParameters().get("SyncConsumer0") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(1, parameters.getEventInputParameters().get("SyncConsumer1") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(12345, parameters.getEventOutputParameters().get("SyncProducer0") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(1, parameters.getEventOutputParameters().get("SyncProducer1") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + } @Test @@ -239,19 +210,16 @@ public class SyncParameterTests { final String[] args = {"-c", "src/test/resources/parameters/syncGoodParamsConsumerTimeout.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - assertEquals(12345, parameters.getEventInputParameters().get("SyncConsumer0") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - assertEquals(1, parameters.getEventInputParameters().get("SyncConsumer1") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - assertEquals(12345, parameters.getEventOutputParameters().get("SyncProducer0") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - assertEquals(1, parameters.getEventOutputParameters().get("SyncProducer1") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - } catch (final Exception e) { - fail("This test should not throw an exception"); - } + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertEquals(12345, parameters.getEventInputParameters().get("SyncConsumer0") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(1, parameters.getEventInputParameters().get("SyncConsumer1") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(12345, parameters.getEventOutputParameters().get("SyncProducer0") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(1, parameters.getEventOutputParameters().get("SyncProducer1") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + } @Test @@ -259,19 +227,16 @@ public class SyncParameterTests { final String[] args = {"-c", "src/test/resources/parameters/syncGoodParamsBothTimeout.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - assertEquals(12345, parameters.getEventInputParameters().get("SyncConsumer0") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - assertEquals(1, parameters.getEventInputParameters().get("SyncConsumer1") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - assertEquals(12345, parameters.getEventOutputParameters().get("SyncProducer0") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - assertEquals(1, parameters.getEventOutputParameters().get("SyncProducer1") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - } catch (final Exception e) { - fail("This test should not throw an exception"); - } + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertEquals(12345, parameters.getEventInputParameters().get("SyncConsumer0") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(1, parameters.getEventInputParameters().get("SyncConsumer1") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(12345, parameters.getEventOutputParameters().get("SyncProducer0") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(1, parameters.getEventOutputParameters().get("SyncProducer1") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + } @Test @@ -279,28 +244,24 @@ public class SyncParameterTests { final String[] args = {"-c", "src/test/resources/parameters/syncUnusedConsumerPeers.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("validation error(s) on parameters from " - + "\"src/test/resources/parameters/syncUnusedConsumerPeers.json\"\n" - + "parameter group \"APEX_PARAMETERS\" type " - + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " - + "parameter group has status INVALID\n" - + " parameter group map \"eventOutputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"SyncProducer1\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" " - + "INVALID, peer \"SyncConsumer0 for peered mode SYNCHRONOUS, " - + "value \"SyncProducer0\" on peer \"SyncConsumer0\" " - + "does not equal event handler \"SyncProducer1\"\n" - + " parameter group map \"eventInputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"SyncConsumer1\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" " - + "INVALID, peer \"SyncProducer1 for peered mode SYNCHRONOUS, " - + "value \"SyncConsumer0\" on peer \"SyncProducer1\" " - + "does not equal event handler \"SyncConsumer1\"\n", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("validation error(s) on parameters from " + + "\"src/test/resources/parameters/syncUnusedConsumerPeers.json\"\n" + + "parameter group \"APEX_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " + + "parameter group has status INVALID\n" + + " parameter group map \"eventOutputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"SyncProducer1\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" " + + "INVALID, peer \"SyncConsumer0 for peered mode SYNCHRONOUS, " + + "value \"SyncProducer0\" on peer \"SyncConsumer0\" " + + "does not equal event handler \"SyncProducer1\"\n" + + " parameter group map \"eventInputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"SyncConsumer1\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" " + + "INVALID, peer \"SyncProducer1 for peered mode SYNCHRONOUS, " + + "value \"SyncConsumer0\" on peer \"SyncProducer1\" " + + "does not equal event handler \"SyncConsumer1\"\n"); } @Test @@ -308,36 +269,30 @@ public class SyncParameterTests { final String[] args = {"-c", "src/test/resources/parameters/syncMismatchedPeers.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals( - "validation error(s) on parameters from " - + "\"src/test/resources/parameters/syncMismatchedPeers.json\"\n" - + "parameter group \"APEX_PARAMETERS\" type " - + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " - + "parameter group has status INVALID\n" - + " parameter group map \"eventOutputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"SyncProducer0\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", peer \"SyncConsumer1 for peered mode SYNCHRONOUS, value \"SyncProducer1\" " - + "on peer \"SyncConsumer1\" does not equal event handler \"SyncProducer0\"\n" - + " parameter group \"SyncProducer1\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", peer \"SyncConsumer0 for peered mode SYNCHRONOUS, value \"SyncProducer0\" " - + "on peer \"SyncConsumer0\" does not equal event handler \"SyncProducer1\"\n" - + " parameter group map \"eventInputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"SyncConsumer0\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", peer \"SyncProducer0 for peered mode SYNCHRONOUS, value \"SyncConsumer1\" " - + "on peer \"SyncProducer0\" does not equal event handler \"SyncConsumer0\"\n" - + " parameter group \"SyncConsumer1\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", peer \"SyncProducer1 for peered mode SYNCHRONOUS, value \"SyncConsumer0\" " - + "on peer \"SyncProducer1\" does not equal event handler \"SyncConsumer1\"\n", - e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("validation error(s) on parameters from " + + "\"src/test/resources/parameters/syncMismatchedPeers.json\"\n" + + "parameter group \"APEX_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " + + "parameter group has status INVALID\n" + + " parameter group map \"eventOutputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"SyncProducer0\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", peer \"SyncConsumer1 for peered mode SYNCHRONOUS, value \"SyncProducer1\" " + + "on peer \"SyncConsumer1\" does not equal event handler \"SyncProducer0\"\n" + + " parameter group \"SyncProducer1\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", peer \"SyncConsumer0 for peered mode SYNCHRONOUS, value \"SyncProducer0\" " + + "on peer \"SyncConsumer0\" does not equal event handler \"SyncProducer1\"\n" + + " parameter group map \"eventInputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"SyncConsumer0\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", peer \"SyncProducer0 for peered mode SYNCHRONOUS, value \"SyncConsumer1\" " + + "on peer \"SyncProducer0\" does not equal event handler \"SyncConsumer0\"\n" + + " parameter group \"SyncConsumer1\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", peer \"SyncProducer1 for peered mode SYNCHRONOUS, value \"SyncConsumer0\" " + + "on peer \"SyncProducer1\" does not equal event handler \"SyncConsumer1\"\n"); } @Test @@ -345,26 +300,22 @@ public class SyncParameterTests { final String[] args = {"-c", "src/test/resources/parameters/syncUnusedProducerPeers.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("validation error(s) on parameters from " - + "\"src/test/resources/parameters/syncUnusedProducerPeers.json\"\n" - + "parameter group \"APEX_PARAMETERS\" type " - + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " - + "parameter group has status INVALID\n" - + " parameter group map \"eventOutputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"SyncProducer0\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", peer \"SyncConsumer0 for peered mode SYNCHRONOUS, value \"SyncProducer1\" on peer " - + "\"SyncConsumer0\" does not equal event handler \"SyncProducer0\"\n" - + " parameter group map \"eventInputParameters\" INVALID, parameter group map has status " - + "INVALID\n parameter group \"SyncConsumer0\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" - + ", peer \"SyncProducer1 for peered mode SYNCHRONOUS, value \"SyncConsumer1\" on peer " - + "\"SyncProducer1\" does not equal event handler \"SyncConsumer0\"\n", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("validation error(s) on parameters from " + + "\"src/test/resources/parameters/syncUnusedProducerPeers.json\"\n" + + "parameter group \"APEX_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " + + "parameter group has status INVALID\n" + + " parameter group map \"eventOutputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"SyncProducer0\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", peer \"SyncConsumer0 for peered mode SYNCHRONOUS, value \"SyncProducer1\" on peer " + + "\"SyncConsumer0\" does not equal event handler \"SyncProducer0\"\n" + + " parameter group map \"eventInputParameters\" INVALID, parameter group map has status " + + "INVALID\n parameter group \"SyncConsumer0\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" + + ", peer \"SyncProducer1 for peered mode SYNCHRONOUS, value \"SyncConsumer1\" on peer " + + "\"SyncProducer1\" does not equal event handler \"SyncConsumer0\"\n"); } @Test @@ -372,100 +323,92 @@ public class SyncParameterTests { final String[] args = {"-c", "src/test/resources/parameters/syncMismatchedTimeout.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - new ApexParameterHandler().getParameters(arguments); - fail("This test should throw an exception"); - } catch (final ParameterException e) { - assertEquals("validation error(s) on parameters from " - + "\"src/test/resources/parameters/syncMismatchedTimeout.json\"\n" - + "parameter group \"APEX_PARAMETERS\" type " - + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " - + "parameter group has status INVALID\n" + " parameter group map \"eventOutputParameters\" " - + "INVALID, parameter group map has status INVALID\n" - + " parameter group \"SyncProducer1\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID," - + " peer \"SyncConsumer1 for peered mode SYNCHRONOUS timeout 456 " - + "on event handler \"SyncProducer1\" does not equal timeout 123 " - + "on event handler \"SyncConsumer1\"\n" - + " parameter group map \"eventInputParameters\" INVALID, " - + "parameter group map has status INVALID\n" + " parameter group \"SyncConsumer1\" type " - + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID," - + " peer \"SyncProducer1 for peered mode SYNCHRONOUS timeout 123 " - + "on event handler \"SyncConsumer1\" does not equal timeout 456 " - + "on event handler \"SyncProducer1\"\n", e.getMessage()); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessage("validation error(s) on parameters from " + + "\"src/test/resources/parameters/syncMismatchedTimeout.json\"\n" + + "parameter group \"APEX_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " + + "parameter group has status INVALID\n" + " parameter group map \"eventOutputParameters\" " + + "INVALID, parameter group map has status INVALID\n" + + " parameter group \"SyncProducer1\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID," + + " peer \"SyncConsumer1 for peered mode SYNCHRONOUS timeout 456 " + + "on event handler \"SyncProducer1\" does not equal timeout 123 " + + "on event handler \"SyncConsumer1\"\n" + + " parameter group map \"eventInputParameters\" INVALID, " + + "parameter group map has status INVALID\n" + " parameter group \"SyncConsumer1\" type " + + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID," + + " peer \"SyncProducer1 for peered mode SYNCHRONOUS timeout 123 " + + "on event handler \"SyncConsumer1\" does not equal timeout 456 " + + "on event handler \"SyncProducer1\"\n"); } @Test - public void testSyncGoodParametersTest() { + public void testSyncGoodParametersTest() throws ParameterException { final String[] args = {"-c", "src/test/resources/parameters/syncGoodParams.json"}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - try { - final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - - assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName()); - assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion()); - assertEquals(45, parameters.getEngineServiceParameters().getId()); - assertEquals(19, parameters.getEngineServiceParameters().getInstanceCount()); - assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort()); - - final CarrierTechnologyParameters prodCT0 = - parameters.getEventOutputParameters().get("SyncProducer0").getCarrierTechnologyParameters(); - final EventProtocolParameters prodEP0 = - parameters.getEventOutputParameters().get("SyncProducer0").getEventProtocolParameters(); - final CarrierTechnologyParameters consCT0 = - parameters.getEventInputParameters().get("SyncConsumer0").getCarrierTechnologyParameters(); - final EventProtocolParameters consEP0 = - parameters.getEventInputParameters().get("SyncConsumer0").getEventProtocolParameters(); - final CarrierTechnologyParameters prodCT1 = - parameters.getEventOutputParameters().get("SyncProducer1").getCarrierTechnologyParameters(); - final EventProtocolParameters prodEP1 = - parameters.getEventOutputParameters().get("SyncProducer1").getEventProtocolParameters(); - final CarrierTechnologyParameters consCT1 = - parameters.getEventInputParameters().get("SyncConsumer1").getCarrierTechnologyParameters(); - final EventProtocolParameters consEP1 = - parameters.getEventInputParameters().get("SyncConsumer1").getEventProtocolParameters(); - - assertEquals("FILE", prodCT0.getLabel()); - assertEquals("JSON", prodEP0.getLabel()); - assertEquals("FILE", consCT0.getLabel()); - assertEquals("JSON", consEP0.getLabel()); - assertEquals("FILE", prodCT1.getLabel()); - assertEquals("JSON", prodEP1.getLabel()); - assertEquals("SUPER_DOOPER", consCT1.getLabel()); - assertEquals("SUPER_TOK_DEL", consEP1.getLabel()); - - assertTrue(consCT1 instanceof SuperDooperCarrierTechnologyParameters); - assertTrue(consEP1 instanceof SuperTokenDelimitedEventProtocolParameters); - - final SuperDooperCarrierTechnologyParameters superDooperParameters = - (SuperDooperCarrierTechnologyParameters) consCT1; - assertEquals("localhost:9092", superDooperParameters.getBootstrapServers()); - assertEquals("all", superDooperParameters.getAcks()); - assertEquals(0, superDooperParameters.getRetries()); - assertEquals(16384, superDooperParameters.getBatchSize()); - assertEquals(1, superDooperParameters.getLingerTime()); - assertEquals(33554432, superDooperParameters.getBufferMemory()); - assertEquals("default-group-id", superDooperParameters.getGroupId()); - assertTrue(superDooperParameters.isEnableAutoCommit()); - assertEquals(1000, superDooperParameters.getAutoCommitTime()); - assertEquals(30000, superDooperParameters.getSessionTimeout()); - assertEquals("apex-out", superDooperParameters.getProducerTopic()); - assertEquals(100, superDooperParameters.getConsumerPollTime()); - assertEquals("org.apache.superDooper.common.serialization.StringSerializer", - superDooperParameters.getKeySerializer()); - assertEquals("org.apache.superDooper.common.serialization.StringSerializer", - superDooperParameters.getValueSerializer()); - assertEquals("org.apache.superDooper.common.serialization.StringDeserializer", - superDooperParameters.getKeyDeserializer()); - assertEquals("org.apache.superDooper.common.serialization.StringDeserializer", - superDooperParameters.getValueDeserializer()); - - final String[] consumerTopics = {"apex-in"}; - assertEquals(Arrays.asList(consumerTopics), superDooperParameters.getConsumerTopicList()); - } catch (final ParameterException e) { - fail("This test should not throw an exception"); - } + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + + assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName()); + assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion()); + assertEquals(45, parameters.getEngineServiceParameters().getId()); + assertEquals(19, parameters.getEngineServiceParameters().getInstanceCount()); + assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort()); + + final CarrierTechnologyParameters prodCT0 = + parameters.getEventOutputParameters().get("SyncProducer0").getCarrierTechnologyParameters(); + final EventProtocolParameters prodEP0 = + parameters.getEventOutputParameters().get("SyncProducer0").getEventProtocolParameters(); + final CarrierTechnologyParameters consCT0 = + parameters.getEventInputParameters().get("SyncConsumer0").getCarrierTechnologyParameters(); + final EventProtocolParameters consEP0 = + parameters.getEventInputParameters().get("SyncConsumer0").getEventProtocolParameters(); + final CarrierTechnologyParameters prodCT1 = + parameters.getEventOutputParameters().get("SyncProducer1").getCarrierTechnologyParameters(); + final EventProtocolParameters prodEP1 = + parameters.getEventOutputParameters().get("SyncProducer1").getEventProtocolParameters(); + final CarrierTechnologyParameters consCT1 = + parameters.getEventInputParameters().get("SyncConsumer1").getCarrierTechnologyParameters(); + final EventProtocolParameters consEP1 = + parameters.getEventInputParameters().get("SyncConsumer1").getEventProtocolParameters(); + + assertEquals("FILE", prodCT0.getLabel()); + assertEquals("JSON", prodEP0.getLabel()); + assertEquals("FILE", consCT0.getLabel()); + assertEquals("JSON", consEP0.getLabel()); + assertEquals("FILE", prodCT1.getLabel()); + assertEquals("JSON", prodEP1.getLabel()); + assertEquals("SUPER_DOOPER", consCT1.getLabel()); + assertEquals("SUPER_TOK_DEL", consEP1.getLabel()); + + assertTrue(consCT1 instanceof SuperDooperCarrierTechnologyParameters); + assertTrue(consEP1 instanceof SuperTokenDelimitedEventProtocolParameters); + + final SuperDooperCarrierTechnologyParameters superDooperParameters = + (SuperDooperCarrierTechnologyParameters) consCT1; + assertEquals("localhost:9092", superDooperParameters.getBootstrapServers()); + assertEquals("all", superDooperParameters.getAcks()); + assertEquals(0, superDooperParameters.getRetries()); + assertEquals(16384, superDooperParameters.getBatchSize()); + assertEquals(1, superDooperParameters.getLingerTime()); + assertEquals(33554432, superDooperParameters.getBufferMemory()); + assertEquals("default-group-id", superDooperParameters.getGroupId()); + assertTrue(superDooperParameters.isEnableAutoCommit()); + assertEquals(1000, superDooperParameters.getAutoCommitTime()); + assertEquals(30000, superDooperParameters.getSessionTimeout()); + assertEquals("apex-out", superDooperParameters.getProducerTopic()); + assertEquals(100, superDooperParameters.getConsumerPollTime()); + assertEquals("org.apache.superDooper.common.serialization.StringSerializer", + superDooperParameters.getKeySerializer()); + assertEquals("org.apache.superDooper.common.serialization.StringSerializer", + superDooperParameters.getValueSerializer()); + assertEquals("org.apache.superDooper.common.serialization.StringDeserializer", + superDooperParameters.getKeyDeserializer()); + assertEquals("org.apache.superDooper.common.serialization.StringDeserializer", + superDooperParameters.getValueDeserializer()); + + final String[] consumerTopics = {"apex-in"}; + assertEquals(Arrays.asList(consumerTopics), superDooperParameters.getConsumerTopicList()); } } diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterHandler.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterHandler.java index 4b79dc589..d39cbecf0 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterHandler.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterHandler.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.services.onappf.parameters; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.io.FileNotFoundException; import org.junit.Test; @@ -44,13 +45,9 @@ public class TestApexStarterParameterHandler { final ApexStarterCommandLineArguments emptyArguments = new ApexStarterCommandLineArguments(); emptyArguments.parse(emptyArgumentString); - try { - new ApexStarterParameterHandler().getParameters(emptyArguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getCause() instanceof CoderException); - assertTrue(e.getCause().getCause() instanceof FileNotFoundException); - } + assertThatThrownBy(() -> new ApexStarterParameterHandler().getParameters(emptyArguments)) + .hasCauseInstanceOf(CoderException.class) + .hasRootCauseInstanceOf(FileNotFoundException.class); } @Test @@ -60,12 +57,8 @@ public class TestApexStarterParameterHandler { final ApexStarterCommandLineArguments noArguments = new ApexStarterCommandLineArguments(); noArguments.parse(noArgumentString); - try { - new ApexStarterParameterHandler().getParameters(noArguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().contains("no parameters found")); - } + assertThatThrownBy(() -> new ApexStarterParameterHandler().getParameters(noArguments)) + .hasMessageContaining("no parameters found"); } @Test @@ -75,13 +68,9 @@ public class TestApexStarterParameterHandler { final ApexStarterCommandLineArguments invalidArguments = new ApexStarterCommandLineArguments(); invalidArguments.parse(invalidArgumentString); - try { - new ApexStarterParameterHandler().getParameters(invalidArguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith("error reading parameters from")); - assertTrue(e.getCause() instanceof CoderException); - } + assertThatThrownBy(() -> new ApexStarterParameterHandler().getParameters(invalidArguments)) + .hasMessageStartingWith("error reading parameters from") + .hasCauseInstanceOf(CoderException.class); } @Test @@ -91,11 +80,8 @@ public class TestApexStarterParameterHandler { final ApexStarterCommandLineArguments noArguments = new ApexStarterCommandLineArguments(); noArguments.parse(noArgumentString); - try { - new ApexStarterParameterHandler().getParameters(noArguments); - } catch (final Exception e) { - assertTrue(e.getMessage().contains("is null")); - } + assertThatThrownBy(() -> new ApexStarterParameterHandler().getParameters(noArguments)) + .hasMessageContaining("is null"); } @Test @@ -118,13 +104,9 @@ public class TestApexStarterParameterHandler { final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); arguments.parse(apexStarterConfigParameters); - try { - new ApexStarterParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().contains( - "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string")); - } + assertThatThrownBy(() -> new ApexStarterParameterHandler().getParameters(arguments)) + .hasMessageContaining("field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a " + + "non-blank string"); } @Test @@ -147,10 +129,7 @@ public class TestApexStarterParameterHandler { public void testApexStarterInvalidOption() throws ApexStarterException { final String[] apexStarterConfigParameters = { "-d" }; final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); - try { - arguments.parse(apexStarterConfigParameters); - } catch (final Exception exp) { - assertTrue(exp.getMessage().startsWith("invalid command line arguments specified")); - } + assertThatThrownBy(() -> arguments.parse(apexStarterConfigParameters)) + .hasMessageStartingWith("invalid command line arguments specified"); } } diff --git a/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/SchemaUtilsTest.java b/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/SchemaUtilsTest.java index 3d6519a65..6af2959ed 100644 --- a/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/SchemaUtilsTest.java +++ b/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/SchemaUtilsTest.java @@ -21,8 +21,8 @@ package org.onap.policy.apex.tools.model.generator; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -77,40 +77,30 @@ public class SchemaUtilsTest { @Test public void testSchemaUtilsErrors() throws ApexEventException { AxEvent event = avroModel.getEvents().get("CustomerContextEventIn"); - AxContextSchema avroCtxtSchema = avroModel.getSchemas().get("ctxtTopologyNodesDecl"); - AxArtifactKey topoNodesKey = new AxArtifactKey("albumTopoNodes", "0.0.1"); - try { - SchemaUtils.getEventSchema(event); - fail("test should throw an exception"); - } catch (Exception apEx) { - assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas" - + " not found in model service", apEx.getMessage()); - } - - try { + assertThatThrownBy(() -> SchemaUtils.getEventSchema(event)) + .hasMessage("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas" + + " not found in model service"); + + assertThatThrownBy(() -> { Map<String, Schema> preexistingParamSchemas = new LinkedHashMap<>(); SchemaUtils.getEventParameterSchema(event.getParameterMap().get("links"), preexistingParamSchemas); - fail("test should throw an exception"); - } catch (Exception apEx) { - assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas" - + " not found in model service", apEx.getMessage()); - } + }).hasMessage("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas" + + " not found in model service"); List<Field> skeletonFields = SchemaUtils.getSkeletonEventSchemaFields(); assertEquals(5, skeletonFields.size()); - try { + AxContextSchema avroCtxtSchema = avroModel.getSchemas().get("ctxtTopologyNodesDecl"); + AxArtifactKey topoNodesKey = new AxArtifactKey("albumTopoNodes", "0.0.1"); + assertThatThrownBy(() -> { AvroSchemaHelper schemaHelper = (AvroSchemaHelper) new SchemaHelperFactory() .createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey()); Map<String, Schema> schemaMap = new LinkedHashMap<>(); SchemaUtils.processSubSchemas(schemaHelper.getAvroSchema(), schemaMap); - fail("test should throw an exception"); - } catch (Exception apEx) { - assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas" - + " not found in model service", apEx.getMessage()); - } + }).hasMessage("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas" + + " not found in model service"); } @Test @@ -124,8 +114,6 @@ public class SchemaUtilsTest { ModelService.registerModel(AxContextSchemas.class, avroModel.getSchemas()); AxEvent event = avroModel.getEvents().get("CustomerContextEventIn"); - AxContextSchema avroCtxtSchema = avroModel.getSchemas().get("ctxtTopologyNodesDecl"); - AxArtifactKey topoNodesKey = new AxArtifactKey("albumTopoNodes", "0.0.1"); Schema eventSchema = SchemaUtils.getEventSchema(event); assertEquals("{\"type\":\"record\",\"name\":\"CustomerContextEventIn\"", @@ -136,19 +124,18 @@ public class SchemaUtilsTest { SchemaUtils.getEventParameterSchema(event.getParameterMap().get("links"), preexistingParamSchemas); assertEquals("\"string\"", epSchema.toString()); + AxContextSchema avroCtxtSchema = avroModel.getSchemas().get("ctxtTopologyNodesDecl"); + AxArtifactKey topoNodesKey = new AxArtifactKey("albumTopoNodes", "0.0.1"); List<Field> skeletonFields = SchemaUtils.getSkeletonEventSchemaFields(); assertEquals(5, skeletonFields.size()); - try { + assertThatThrownBy(() -> { AvroSchemaHelper schemaHelper = (AvroSchemaHelper) new SchemaHelperFactory() .createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey()); Map<String, Schema> schemaMap = new LinkedHashMap<>(); SchemaUtils.processSubSchemas(schemaHelper.getAvroSchema(), schemaMap); - fail("test should throw an exception"); - } catch (Exception apEx) { - assertEquals("context schema helper parameters not found for context schema \"Avro\"", apEx.getMessage()); - } + }).hasMessage("context schema helper parameters not found for context schema \"Avro\""); schemaParameters.getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters()); @@ -156,11 +143,7 @@ public class SchemaUtilsTest { (AvroSchemaHelper) new SchemaHelperFactory().createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey()); Map<String, Schema> schemaMap = new LinkedHashMap<>(); - try { - SchemaUtils.processSubSchemas(schemaHelper.getAvroSchema(), schemaMap); - } catch (Exception exc) { - fail("test should not throw an exception"); - } + SchemaUtils.processSubSchemas(schemaHelper.getAvroSchema(), schemaMap); eventSchema = SchemaUtils.getEventSchema(event); assertEquals("{\"type\":\"record\",\"name\":\"CustomerContextEventIn\"", @@ -179,12 +162,8 @@ public class SchemaUtilsTest { assertEquals(5, skeletonFields.size()); schemaParameters.getSchemaHelperParameterMap().put("Avro", new JavaSchemaHelperParameters()); - try { - ep2Schema = SchemaUtils.getEventParameterSchema(inField, preexistingParamSchemas); - fail("test should throw an exception"); - } catch (Exception apEx) { - assertEquals("FieldParent:0.0.1:NULL:Field: class/type", apEx.getMessage().substring(0, 40)); - } + assertThatThrownBy(() -> SchemaUtils.getEventParameterSchema(inField, preexistingParamSchemas)) + .hasMessageContaining("FieldParent:0.0.1:NULL:Field: class/type"); ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); ModelService.clear(); diff --git a/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/model2cli/Model2CliTest.java b/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/model2cli/Model2CliTest.java index a109e47da..1263dc4fd 100644 --- a/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/model2cli/Model2CliTest.java +++ b/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/model2cli/Model2CliTest.java @@ -22,7 +22,6 @@ package org.onap.policy.apex.tools.model.generator.model2cli; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.io.ByteArrayOutputStream; import java.io.File; @@ -36,13 +35,9 @@ import org.junit.Test; public class Model2CliTest { @Test public void testModel2Cli() { - try { - final String[] cliArgs = {"-h"}; + final String[] cliArgs = {"-h"}; - Model2CliMain.main(cliArgs); - } catch (Exception exc) { - fail("test should not throw an exception"); - } + Model2CliMain.main(cliArgs); } @Test @@ -104,27 +99,27 @@ public class Model2CliTest { } @Test - public void testModel2CliAnomaly() { + public void testModel2CliAnomaly() throws IOException { testModel2CliModel("target/examples/models/Adaptive", "AnomalyDetectionPolicyModel"); } @Test - public void testModel2CliAutoLearn() { + public void testModel2CliAutoLearn() throws IOException { testModel2CliModel("target/examples/models/Adaptive", "AutoLearnPolicyModel"); } @Test - public void testModel2CliJms() { + public void testModel2CliJms() throws IOException { testModel2CliModel("target/examples/models/JMS", "JMSTestModel"); } @Test - public void testModel2CliMfp() { + public void testModel2CliMfp() throws IOException { testModel2CliModel("target/examples/models/MyFirstPolicy/2", "MyFirstPolicyModel_0.0.1"); } @Test - public void testModel2CliSample() { + public void testModel2CliSample() throws IOException { testModel2CliModel("target/examples/models/SampleDomain", "SamplePolicyModelJAVASCRIPT"); } @@ -151,26 +146,23 @@ public class Model2CliTest { * * @param modelName the name of the model file */ - private void testModel2CliModel(final String modelPath, final String modelName) { - try { - File tempFile = File.createTempFile(modelName, ".apex"); - tempFile.deleteOnExit(); - - // @formatter:off - final String[] cliArgs = { - "-m", - modelPath + "/" + modelName + ".json", - "-o", - tempFile.getCanonicalPath(), - "-ow" - }; - // @formatter:on - runModel2Cli(cliArgs); - - assertTrue(tempFile.isFile()); - assertTrue(tempFile.length() > 0); - } catch (Exception e) { - fail("test should not throw an exception"); - } + private void testModel2CliModel(final String modelPath, final String modelName) throws IOException { + File tempFile = File.createTempFile(modelName, ".apex"); + tempFile.deleteOnExit(); + + // @formatter:off + final String[] cliArgs = { + "-m", + modelPath + "/" + modelName + ".json", + "-o", + tempFile.getCanonicalPath(), + "-ow" + }; + // @formatter:on + runModel2Cli(cliArgs); + + assertTrue(tempFile.isFile()); + assertTrue(tempFile.length() > 0); + } } diff --git a/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/model2event/Model2EventTest.java b/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/model2event/Model2EventTest.java index f7c913054..58c860d1c 100644 --- a/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/model2event/Model2EventTest.java +++ b/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/model2event/Model2EventTest.java @@ -1,19 +1,20 @@ /*- * ============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. * 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========================================================= */ @@ -21,14 +22,12 @@ package org.onap.policy.apex.tools.model.generator.model2event; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.PrintStream; import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException; /** * Test the Model2Event utility. @@ -36,14 +35,11 @@ import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException; public class Model2EventTest { @Test public void testModel2Event() { - try { - final String[] EventArgs = - { "-h" }; - - Model2EventMain.main(EventArgs); - } catch (Exception exc) { - fail("test should not throw an exception"); - } + final String[] EventArgs = + { "-h" }; + + Model2EventMain.main(EventArgs); + } @Test @@ -112,28 +108,28 @@ public class Model2EventTest { } @Test - public void testModel2EventAnomaly() { + public void testModel2EventAnomaly() throws IOException { testModel2EventModel("AnomalyDetectionPolicyModel"); } @Test - public void testModel2EventAutoLearn() { + public void testModel2EventAutoLearn() throws IOException { testModel2EventModel("AutoLearnPolicyModel"); } @Test - public void testModel2EventMfp() { + public void testModel2EventMfp() throws IOException { testModel2EventModel("MyFirstPolicyModel"); } @Test - public void testModel2EventSample() { + public void testModel2EventSample() throws IOException { testModel2EventModel("SamplePolicyModelJAVASCRIPT"); } /** * Run the application. - * + * * @param eventArgs the command arguments * @return a string containing the command output */ @@ -151,31 +147,27 @@ public class Model2EventTest { /** * Test Event generation. - * + * * @param modelName the name of the model file */ - private void testModel2EventModel(String modelName) { - try { - File tempFile = File.createTempFile(modelName, ".apex"); - tempFile.deleteOnExit(); + private void testModel2EventModel(String modelName) throws IOException { + File tempFile = File.createTempFile(modelName, ".apex"); + tempFile.deleteOnExit(); - final String[] eventArgs0 = - { "-m", "src/test/resources/models/" + modelName + ".json", "-t", "stimuli" }; - final String outputString0 = runModel2Event(eventArgs0); + final String[] eventArgs0 = + { "-m", "src/test/resources/models/" + modelName + ".json", "-t", "stimuli" }; + final String outputString0 = runModel2Event(eventArgs0); - assertTrue(outputString0.contains("type: stimuli")); + assertTrue(outputString0.contains("type: stimuli")); - final String[] eventArgs1 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "response" }; - final String outputString1 = runModel2Event(eventArgs1); + final String[] eventArgs1 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "response" }; + final String outputString1 = runModel2Event(eventArgs1); - assertTrue(outputString1.contains("type: response")); + assertTrue(outputString1.contains("type: response")); - final String[] eventArgs2 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "internal" }; - final String outputString2 = runModel2Event(eventArgs2); + final String[] eventArgs2 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "internal" }; + final String outputString2 = runModel2Event(eventArgs2); - assertTrue(outputString2.contains("type: internal")); - } catch (Exception e) { - throw new ApexRuntimeException("test should not throw an exception", e); - } + assertTrue(outputString2.contains("type: internal")); } } diff --git a/tools/simple-wsclient/src/test/java/org/onap/policy/apex/tools/simple/wsclient/WsClientTest.java b/tools/simple-wsclient/src/test/java/org/onap/policy/apex/tools/simple/wsclient/WsClientTest.java index 9ca7e9296..8ff185504 100644 --- a/tools/simple-wsclient/src/test/java/org/onap/policy/apex/tools/simple/wsclient/WsClientTest.java +++ b/tools/simple-wsclient/src/test/java/org/onap/policy/apex/tools/simple/wsclient/WsClientTest.java @@ -1,19 +1,20 @@ /*- * ============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. * 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========================================================= */ @@ -21,7 +22,6 @@ package org.onap.policy.apex.tools.simple.wsclient; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -35,14 +35,10 @@ import org.junit.Test; public class WsClientTest { @Test public void testWsClient() { - try { - final String[] EventArgs = - { "-h" }; - - WsClientMain.main(EventArgs); - } catch (Exception exc) { - fail("test should not throw an exception"); - } + final String[] EventArgs = + { "-h" }; + + WsClientMain.main(EventArgs); } @Test |