Update snapshot and/or references of policy/pap to latest snapshots
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / parameters / TestPapParameterHandler.java
index cb14748..6b6431f 100644 (file)
@@ -1,6 +1,8 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
+ *  Modifications Copyright (C) 2020-2021 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.pap.main.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;
+import org.onap.policy.common.utils.cmd.CommandLineException;
+import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.pap.main.PolicyPapException;
 import org.onap.policy.pap.main.startstop.PapCommandLineArguments;
 
@@ -36,70 +41,52 @@ import org.onap.policy.pap.main.startstop.PapCommandLineArguments;
 public class TestPapParameterHandler {
 
     @Test
-    public void testParameterHandlerNoParameterFile() throws PolicyPapException {
+    public void testParameterHandlerNoParameterFile() throws PolicyPapException, CommandLineException {
         final String[] noArgumentString = { "-c", "parameters/NoParameterFile.json" };
 
         final PapCommandLineArguments noArguments = new PapCommandLineArguments();
         noArguments.parse(noArgumentString);
 
-        try {
-            new PapParameterHandler().getParameters(noArguments);
-            fail("test should throw an exception here");
-        } catch (final Exception e) {
-            assertTrue(e.getMessage().contains("FileNotFoundException"));
-        }
+        assertThatThrownBy(() -> new PapParameterHandler().getParameters(noArguments))
+            .hasCauseInstanceOf(CoderException.class)
+            .hasRootCauseInstanceOf(FileNotFoundException.class);
     }
 
     @Test
-    public void testParameterHandlerEmptyParameters() throws PolicyPapException {
+    public void testParameterHandlerEmptyParameters() throws PolicyPapException, CommandLineException {
         final String[] emptyArgumentString = { "-c", "parameters/EmptyParameters.json" };
 
         final PapCommandLineArguments emptyArguments = new PapCommandLineArguments();
         emptyArguments.parse(emptyArgumentString);
 
-        try {
-            new PapParameterHandler().getParameters(emptyArguments);
-            fail("test should throw an exception here");
-        } catch (final Exception e) {
-            assertEquals("no parameters found in \"parameters/EmptyParameters.json\"", e.getMessage());
-        }
+        assertThatThrownBy(() -> new PapParameterHandler().getParameters(emptyArguments))
+            .hasMessageContaining("no parameters found in \"parameters/EmptyParameters.json\"");
     }
 
     @Test
-    public void testParameterHandlerInvalidParameters() throws PolicyPapException {
+    public void testParameterHandlerInvalidParameters() throws PolicyPapException, CommandLineException {
         final String[] invalidArgumentString = { "-c", "parameters/InvalidParameters.json" };
 
         final PapCommandLineArguments invalidArguments = new PapCommandLineArguments();
         invalidArguments.parse(invalidArgumentString);
 
-        try {
-            new PapParameterHandler().getParameters(invalidArguments);
-            fail("test should throw an exception here");
-        } catch (final Exception e) {
-            assertEquals("error reading parameters from \"parameters/InvalidParameters.json\"\n"
-                    + "(JsonSyntaxException):java.lang.IllegalStateException: "
-                    + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name", e.getMessage());
-        }
+        assertThatThrownBy(() -> new PapParameterHandler().getParameters(invalidArguments))
+            .hasMessageStartingWith("error reading parameters from \"parameters/InvalidParameters.json\"")
+            .hasCauseInstanceOf(CoderException.class);
     }
 
     @Test
-    public void testParameterHandlerNoParameters() throws PolicyPapException {
+    public void testParameterHandlerNoParameters() throws PolicyPapException, CommandLineException {
         final String[] noArgumentString = { "-c", "parameters/NoParameters.json" };
 
         final PapCommandLineArguments noArguments = new PapCommandLineArguments();
         noArguments.parse(noArgumentString);
 
-        try {
-            new PapParameterHandler().getParameters(noArguments);
-            fail("test should throw an exception here");
-        } catch (final Exception e) {
-            assertTrue(e.getMessage().contains(
-                    "field \"name\" type \"java.lang.String\" value \"null\" INVALID, must be a non-blank string"));
-        }
+        assertThatThrownBy(() -> new PapParameterHandler().getParameters(noArguments)).hasMessageContaining("is null");
     }
 
     @Test
-    public void testParameterHandlerMinumumParameters() throws PolicyPapException {
+    public void testParameterHandlerMinumumParameters() throws PolicyPapException, CommandLineException {
         final String[] minArgumentString = { "-c", "parameters/MinimumParameters.json" };
 
         final PapCommandLineArguments minArguments = new PapCommandLineArguments();
@@ -110,7 +97,7 @@ public class TestPapParameterHandler {
     }
 
     @Test
-    public void testPapParameterGroup() throws PolicyPapException {
+    public void testPapParameterGroup() throws PolicyPapException, CommandLineException {
         final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" };
 
         final PapCommandLineArguments arguments = new PapCommandLineArguments();
@@ -122,23 +109,18 @@ public class TestPapParameterHandler {
     }
 
     @Test
-    public void testPapParameterGroup_InvalidName() throws PolicyPapException {
+    public void testPapParameterGroup_InvalidName() throws PolicyPapException, CommandLineException {
         final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters_InvalidName.json" };
 
         final PapCommandLineArguments arguments = new PapCommandLineArguments();
         arguments.parse(papConfigParameters);
 
-        try {
-            new PapParameterHandler().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 PapParameterHandler().getParameters(arguments))
+            .hasMessageContaining("\"name\" value \" \" INVALID, is blank");
     }
 
     @Test
-    public void testPapVersion() throws PolicyPapException {
+    public void testPapVersion() throws PolicyPapException, CommandLineException {
         final String[] papConfigParameters = { "-v" };
         final PapCommandLineArguments arguments = new PapCommandLineArguments();
         final String version = arguments.parse(papConfigParameters);
@@ -146,7 +128,7 @@ public class TestPapParameterHandler {
     }
 
     @Test
-    public void testPapHelp() throws PolicyPapException {
+    public void testPapHelp() throws PolicyPapException, CommandLineException {
         final String[] papConfigParameters = { "-h" };
         final PapCommandLineArguments arguments = new PapCommandLineArguments();
         final String help = arguments.parse(papConfigParameters);
@@ -154,13 +136,10 @@ public class TestPapParameterHandler {
     }
 
     @Test
-    public void testPapInvalidOption() throws PolicyPapException {
+    public void testPapInvalidOption() throws PolicyPapException, CommandLineException {
         final String[] papConfigParameters = { "-d" };
         final PapCommandLineArguments arguments = new PapCommandLineArguments();
-        try {
-            arguments.parse(papConfigParameters);
-        } catch (final Exception exp) {
-            assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));
-        }
+        assertThatThrownBy(() -> arguments.parse(papConfigParameters))
+            .hasMessageStartingWith("invalid command line arguments specified");
     }
 }