From 6ae054c03543bc4d323ff0ae710c0b57e4f4e610 Mon Sep 17 00:00:00 2001 From: waynedunican Date: Mon, 6 Jul 2020 09:31:10 +0100 Subject: Replace try/catch blocks with assertj - apex-pdp Replaced try/catch blocks in apex-pdp with assertj assertions Issue-ID: POLICY-2451 Change-Id: I83375b9e0f38d399b84bb68ce5d441e1cec73ef5 Signed-off-by: waynedunican --- .../client/editor/rest/ApexEditorStartupTest.java | 118 ++++-------- .../rest/handling/ApexEditorRestResourceTest.java | 201 ++++----------------- .../editor/rest/handling/bean/BeansTest.java | 31 +--- .../monitoring/rest/MonitoringRestMainTest.java | 148 +++++---------- 4 files changed, 117 insertions(+), 381 deletions(-) (limited to 'client') diff --git a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/ApexEditorStartupTest.java b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/ApexEditorStartupTest.java index fdf1e804a..4d6c93222 100644 --- a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/ApexEditorStartupTest.java +++ b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/ApexEditorStartupTest.java @@ -21,6 +21,7 @@ package org.onap.policy.apex.client.editor.rest; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.awaitility.Awaitility.await; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -70,14 +71,9 @@ public class ApexEditorStartupTest { public void testBadArg0() throws IOException, InterruptedException { final String[] args = new String[] { "12321" }; - try { - runEditor(args); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getLocalizedMessage().startsWith( - "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error," - + " too many command line arguments specified : [12321]")); - } + assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class) + .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)" + + " parameter error, too many command line arguments specified : [12321]"); } /** @@ -90,14 +86,9 @@ public class ApexEditorStartupTest { public void testBadArg1() throws IOException, InterruptedException { final String[] args = new String[] { "12321 12322 12323" }; - try { - runEditor(args); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getLocalizedMessage().startsWith( - "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error," - + " too many command line arguments specified : [12321 12322 12323]")); - } + assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class) + .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)" + + " parameter error, too many command line arguments specified : [12321 12322 12323]"); } /** @@ -110,14 +101,9 @@ public class ApexEditorStartupTest { public void testBadArg2() throws IOException, InterruptedException { final String[] args = new String[] { "-z" }; - try { - runEditor(args); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getLocalizedMessage().startsWith( - "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error," - + " invalid command line arguments specified : Unrecognized option: -z")); - } + assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class) + .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)" + + " parameter error, invalid command line arguments specified : Unrecognized option: -z"); } /** @@ -130,14 +116,9 @@ public class ApexEditorStartupTest { public void testBadArg3() throws IOException, InterruptedException { final String[] args = new String[] { "--hello" }; - try { - runEditor(args); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getLocalizedMessage().startsWith( - "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error," - + " invalid command line arguments specified : Unrecognized option: --hello")); - } + assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class) + .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)" + + " parameter error, invalid command line arguments specified : Unrecognized option: --hello"); } @@ -151,16 +132,11 @@ public class ApexEditorStartupTest { public void testBadArg4() throws IOException, InterruptedException { final String[] args = new String[] { "-l", "+++++" }; - try { - runEditor(args); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getLocalizedMessage() - .startsWith("Apex Editor REST endpoint (ApexEditorMain: " + assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class) + .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: " + "Config=[ApexEditorParameters: URI=http://+++++:18989/apexservices/, TTL=-1sec], " + "State=STOPPED) parameters invalid, listen address is not valid. " - + "Illegal character in hostname at index 7: http://+++++:18989/apexservices/")); - } + + "Illegal character in hostname at index 7: http://+++++:18989/apexservices/"); } /** @@ -173,13 +149,8 @@ public class ApexEditorStartupTest { public void testHelp0() throws IOException, InterruptedException { final String[] args = new String[] { "--help" }; - try { - runEditor(args); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("usage: org.onap.policy.apex.client.editor.rest.ApexEditorMain [options...]")); - } + assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class) + .hasMessageContaining("usage: org.onap.policy.apex.client.editor.rest.ApexEditorMain [options...]"); } /** @@ -192,13 +163,8 @@ public class ApexEditorStartupTest { public void testHelp1() throws IOException, InterruptedException { final String[] args = new String[] { "-h" }; - try { - runEditor(args); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("usage: org.onap.policy.apex.client.editor.rest.ApexEditorMain [options...]")); - } + assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class) + .hasMessageContaining("usage: org.onap.policy.apex.client.editor.rest.ApexEditorMain [options...]"); } /** @@ -258,14 +224,9 @@ public class ApexEditorStartupTest { public void testPortArgSpace() throws IOException, InterruptedException { final String[] args = new String[] { "-p 12321" }; - try { - runEditor(args); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith( - "Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED) parameter error," - + " error parsing argument \"port\" :For input string: \" 12321\"")); - } + assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class) + .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: Config=[null], State=STOPPED)" + + " parameter error, error parsing argument \"port\" :For input string: \" 12321\""); } /** @@ -278,15 +239,10 @@ public class ApexEditorStartupTest { public void testBadPortArgs0() throws IOException, InterruptedException { final String[] args = new String[] { "-p0" }; - try { - runEditor(args); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("Apex Editor REST endpoint (ApexEditorMain: " + assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class) + .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: " + "Config=[ApexEditorParameters: URI=http://localhost:0/apexservices/, TTL=-1sec], " - + "State=STOPPED) parameters invalid, port must be between 1024 and 65535")); - } + + "State=STOPPED) parameters invalid, port must be between 1024 and 65535"); } /** @@ -299,15 +255,10 @@ public class ApexEditorStartupTest { public void testBadPortArgs1023() throws IOException, InterruptedException { final String[] args = new String[] { "-p1023" }; - try { - runEditor(args); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("Apex Editor REST endpoint (ApexEditorMain: " + assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class) + .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: " + "Config=[ApexEditorParameters: URI=http://localhost:1023/apexservices/, TTL=-1sec], " - + "State=STOPPED) parameters invalid, port must be between 1024 and 65535")); - } + + "State=STOPPED) parameters invalid, port must be between 1024 and 65535"); } /** @@ -320,15 +271,10 @@ public class ApexEditorStartupTest { public void testBadPortArgs65536() throws IOException, InterruptedException { final String[] args = new String[] { "-p65536" }; - try { - runEditor(args); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("Apex Editor REST endpoint (ApexEditorMain: " + assertThatThrownBy(() -> runEditor(args)).isInstanceOf(ApexEditorParameterException.class) + .hasMessageContaining("Apex Editor REST endpoint (ApexEditorMain: " + "Config=[ApexEditorParameters: URI=http://localhost:65536/apexservices/, TTL=-1sec], " - + "State=STOPPED) parameters invalid, port must be between 1024 and 65535")); - } + + "State=STOPPED) parameters invalid, port must be between 1024 and 65535"); } /** diff --git a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/ApexEditorRestResourceTest.java b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/ApexEditorRestResourceTest.java index 642628f97..ace427052 100644 --- a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/ApexEditorRestResourceTest.java +++ b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/ApexEditorRestResourceTest.java @@ -21,6 +21,7 @@ package org.onap.policy.apex.client.editor.rest.handling; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -60,11 +61,7 @@ public class ApexEditorRestResourceTest extends JerseyTest { final int corruptSessionId = ApexEditorRestResource.createCorruptSession(); - try { - target("editor/" + corruptSessionId + "/Model/Analyse").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + target("editor/" + corruptSessionId + "/Model/Analyse").request().get(ApexApiResult.class); result = target("editor/" + sessionId + "/Model/Analyse").request().get(ApexApiResult.class); assertEquals(Result.SUCCESS, result.getResult()); @@ -73,11 +70,7 @@ public class ApexEditorRestResourceTest extends JerseyTest { result = target("editor/12345/Model/Analyse").request().get(ApexApiResult.class); assertEquals(Result.FAILED, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Model/Validate").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + target("editor/" + corruptSessionId + "/Model/Validate").request().get(ApexApiResult.class); result = target("editor/" + sessionId + "/Model/Validate").request().get(ApexApiResult.class); assertEquals(Result.FAILED, result.getResult()); @@ -99,11 +92,7 @@ public class ApexEditorRestResourceTest extends JerseyTest { result = target("editor/" + sessionId + "/Model/Create").request().post(csEntity, ApexApiResult.class); assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Model/Create").request().post(csEntity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + target("editor/" + corruptSessionId + "/Model/Create").request().post(csEntity, ApexApiResult.class); result = target("editor/-12345/Model/Update").request().put(csEntity, ApexApiResult.class); assertEquals(ApexApiResult.Result.FAILED, result.getResult()); @@ -114,17 +103,9 @@ public class ApexEditorRestResourceTest extends JerseyTest { result = target("editor/" + sessionId + "/Model/Update").request().put(csEntity, ApexApiResult.class); assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Model/Update").request().put(csEntity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + target("editor/" + corruptSessionId + "/Model/Update").request().put(csEntity, ApexApiResult.class); - try { - result = target("editor/" + corruptSessionId + "/Model/GetKey").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + result = target("editor/" + corruptSessionId + "/Model/GetKey").request().get(ApexApiResult.class); result = target("editor/" + sessionId + "/Model/GetKey").request().get(ApexApiResult.class); assertEquals(Result.SUCCESS, result.getResult()); @@ -133,11 +114,7 @@ public class ApexEditorRestResourceTest extends JerseyTest { result = target("editor/12345/Model/GetKey").request().get(ApexApiResult.class); assertEquals(Result.FAILED, result.getResult()); - try { - result = target("editor/" + corruptSessionId + "/Model/Get").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + result = target("editor/" + corruptSessionId + "/Model/Get").request().get(ApexApiResult.class); result = target("editor/" + sessionId + "/Model/Get").request().get(ApexApiResult.class); assertEquals(Result.SUCCESS, result.getResult()); @@ -158,11 +135,7 @@ public class ApexEditorRestResourceTest extends JerseyTest { resultString = target("editor/12345/Model/Download").request().get(String.class); assertEquals("", resultString); - try { - result = target("editor/" + corruptSessionId + "/KeyInformation/Get").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + result = target("editor/" + corruptSessionId + "/KeyInformation/Get").request().get(ApexApiResult.class); result = target("editor/" + sessionId + "/KeyInformation/Get").request().get(ApexApiResult.class); assertEquals(Result.SUCCESS, result.getResult()); @@ -171,11 +144,7 @@ public class ApexEditorRestResourceTest extends JerseyTest { result = target("editor/12345/KeyInformation/Get").request().get(ApexApiResult.class); assertEquals(Result.FAILED, result.getResult()); - try { - result = target("editor/" + corruptSessionId + "/Model/Delete").request().delete(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + result = target("editor/" + corruptSessionId + "/Model/Delete").request().delete(ApexApiResult.class); result = target("editor/" + sessionId + "/Model/Delete").request().delete(ApexApiResult.class); assertEquals(Result.SUCCESS, result.getResult()); @@ -196,11 +165,7 @@ public class ApexEditorRestResourceTest extends JerseyTest { result = target("editor/-12345/Validate/ContextSchema").request().get(ApexApiResult.class); assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Validate/ContextSchema").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + target("editor/" + corruptSessionId + "/Validate/ContextSchema").request().get(ApexApiResult.class); result = target("editor/" + sessionId + "/Validate/ContextSchema").request().get(ApexApiResult.class); assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); @@ -232,12 +197,8 @@ public class ApexEditorRestResourceTest extends JerseyTest { .queryParam("version", (String) null).request().get(ApexApiResult.class); assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - try { - target("editor/" + corruptSessionId + "/ContextSchema/Get").queryParam("name", (String) null) + target("editor/" + corruptSessionId + "/ContextSchema/Get").queryParam("name", (String) null) .queryParam("version", (String) null).request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } String csString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," + "\"schemaFlavour\" : \"Java\"," + "\"schemaDefinition\" : \"java.lang.String\"," @@ -251,12 +212,9 @@ public class ApexEditorRestResourceTest extends JerseyTest { result = target("editor/" + sessionId + "/ContextSchema/Create").request().post(csEntity, ApexApiResult.class); assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - try { - target("editor/" + corruptSessionId + "/ContextSchema/Create").request().post(csEntity, + target("editor/" + corruptSessionId + "/ContextSchema/Create").request().post(csEntity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + csString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," + "\"schemaFlavour\" : \"Java\"," + "\"schemaDefinition\" : \"my.perfect.String\"," @@ -270,11 +228,7 @@ public class ApexEditorRestResourceTest extends JerseyTest { result = target("editor/" + sessionId + "/ContextSchema/Update").request().put(csEntity, ApexApiResult.class); assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - try { - target("editor/" + corruptSessionId + "/ContextSchema/Update").request().put(csEntity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + target("editor/" + corruptSessionId + "/ContextSchema/Update").request().put(csEntity, ApexApiResult.class); result = target("editor/" + sessionId + "/ContextSchema/Get").queryParam("name", "Hello") .queryParam("version", (String) null).request().get(ApexApiResult.class); @@ -289,23 +243,15 @@ public class ApexEditorRestResourceTest extends JerseyTest { .queryParam("version", (String) null).request().get(ApexApiResult.class); assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - try { - target("editor/" + corruptSessionId + "/ContextSchema/Get").queryParam("name", "Hello") + target("editor/" + corruptSessionId + "/ContextSchema/Get").queryParam("name", "Hello") .queryParam("version", (String) null).request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } result = target("editor/" + sessionId + "/Validate/ContextSchema").queryParam("name", (String) null) .queryParam("version", (String) null).request().get(ApexApiResult.class); assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - try { - target("editor/" + corruptSessionId + "/ContextSchema/Delete").queryParam("name", "Hello") + target("editor/" + corruptSessionId + "/ContextSchema/Delete").queryParam("name", "Hello") .queryParam("version", "0.0.2").request().delete(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } result = target("editor/-123345/ContextSchema/Delete").queryParam("name", (String) null) .queryParam("version", (String) null).request().delete(ApexApiResult.class); @@ -331,11 +277,7 @@ public class ApexEditorRestResourceTest extends JerseyTest { result = target("editor/-12345/Validate/ContextAlbum").request().get(ApexApiResult.class); assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Validate/ContextAlbum").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + target("editor/" + corruptSessionId + "/Validate/ContextAlbum").request().get(ApexApiResult.class); result = target("editor/" + sessionId + "/Validate/ContextAlbum").request().get(ApexApiResult.class); assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); @@ -380,11 +322,7 @@ public class ApexEditorRestResourceTest extends JerseyTest { result = target("editor/" + sessionId + "/ContextAlbum/Create").request().post(caEntity, ApexApiResult.class); assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - try { - target("editor/" + corruptSessionId + "/ContextAlbum/Create").request().post(caEntity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + target("editor/" + corruptSessionId + "/ContextAlbum/Create").request().post(caEntity, ApexApiResult.class); caString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," + "\"scope\" : \"Global\"," + "\"writeable\" : false," @@ -399,18 +337,10 @@ public class ApexEditorRestResourceTest extends JerseyTest { result = target("editor/" + sessionId + "/ContextAlbum/Update").request().put(caEntity, ApexApiResult.class); assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - try { - target("editor/" + corruptSessionId + "/ContextAlbum/Update").request().put(caEntity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + target("editor/" + corruptSessionId + "/ContextAlbum/Update").request().put(caEntity, ApexApiResult.class); - try { - target("editor/" + corruptSessionId + "/ContextAlbum/Get").queryParam("name", "Hello") + target("editor/" + corruptSessionId + "/ContextAlbum/Get").queryParam("name", "Hello") .queryParam("version", (String) null).request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } result = target("editor/" + sessionId + "/ContextAlbum/Get").queryParam("name", "Hello") .queryParam("version", (String) null).request().get(ApexApiResult.class); @@ -429,12 +359,8 @@ public class ApexEditorRestResourceTest extends JerseyTest { .queryParam("version", (String) null).request().get(ApexApiResult.class); assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - try { - target("editor/" + corruptSessionId + "/ContextAlbum/Delete").queryParam("name", (String) null) + target("editor/" + corruptSessionId + "/ContextAlbum/Delete").queryParam("name", (String) null) .queryParam("version", (String) null).request().delete(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } result = target("editor/-123345/ContextAlbum/Delete").queryParam("name", (String) null) .queryParam("version", (String) null).request().delete(ApexApiResult.class); @@ -464,11 +390,7 @@ public class ApexEditorRestResourceTest extends JerseyTest { result = target("editor/" + sessionId + "/Validate/Event").request().get(ApexApiResult.class); assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Validate/Event").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + target("editor/" + corruptSessionId + "/Validate/Event").request().get(ApexApiResult.class); result = target("editor/" + sessionId + "/Validate/Event").queryParam("name", (String) null) .queryParam("version", (String) null).request().get(ApexApiResult.class); @@ -511,11 +433,7 @@ public class ApexEditorRestResourceTest extends JerseyTest { result = target("editor/" + sessionId + "/Event/Create").request().post(entity, ApexApiResult.class); assertEquals(ApexApiResult.Result.CONCEPT_EXISTS, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Event/Create").request().post(entity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + target("editor/" + corruptSessionId + "/Event/Create").request().post(entity, ApexApiResult.class); entityString = "{" + "\"name\" : \"Hiya\"," + "\"version\" : \"0.0.2\"," + "\"namespace\" : \"somewhere.over.the.rainbow\"," + "\"source\" : \"beginning\"," @@ -570,11 +488,7 @@ public class ApexEditorRestResourceTest extends JerseyTest { result = target("editor/" + sessionId + "/Event/Update").request().put(entity, ApexApiResult.class); assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Event/Update").request().put(entity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + target("editor/" + corruptSessionId + "/Event/Update").request().put(entity, ApexApiResult.class); entityString = "{" + "\"name\" : null," + "\"version\" : \"0.0.2\"," + "\"namespace\" : \"somewhere.over.someone.elses.rainbow\"," @@ -607,12 +521,8 @@ public class ApexEditorRestResourceTest extends JerseyTest { .queryParam("version", (String) null).request().get(ApexApiResult.class); assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Event/Get").queryParam("name", "Hello") + target("editor/" + corruptSessionId + "/Event/Get").queryParam("name", "Hello") .queryParam("version", (String) null).request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } result = target("editor/" + sessionId + "/Validate/Event").queryParam("name", (String) null) .queryParam("version", (String) null).request().get(ApexApiResult.class); @@ -624,12 +534,8 @@ public class ApexEditorRestResourceTest extends JerseyTest { .queryParam("version", (String) null).request().get(ApexApiResult.class); assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Event/Delete").queryParam("name", (String) null) + target("editor/" + corruptSessionId + "/Event/Delete").queryParam("name", (String) null) .queryParam("version", (String) null).request().delete(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } result = target("editor/-123345/Event/Delete").queryParam("name", (String) null) .queryParam("version", (String) null).request().delete(ApexApiResult.class); @@ -705,11 +611,7 @@ public class ApexEditorRestResourceTest extends JerseyTest { result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); assertEquals(ApexApiResult.Result.CONCEPT_EXISTS, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + target("editor/" + corruptSessionId + "/Task/Create").request().post(entity, ApexApiResult.class); entityString = "{" + "\"name\" : \"Hiya\"," + "\"version\" : \"0.0.2\"," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," @@ -896,11 +798,7 @@ public class ApexEditorRestResourceTest extends JerseyTest { result = target("editor/" + sessionId + "/Task/Update").request().put(entity, ApexApiResult.class); assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Task/Update").request().put(entity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + target("editor/" + corruptSessionId + "/Task/Update").request().put(entity, ApexApiResult.class); entityString = "{" + "\"name\" : null," + "\"version\" : \"0.0.2\"," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," @@ -932,12 +830,8 @@ public class ApexEditorRestResourceTest extends JerseyTest { .request().get(ApexApiResult.class); assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Task/Get").queryParam("name", "Hello") + target("editor/" + corruptSessionId + "/Task/Get").queryParam("name", "Hello") .queryParam("version", (String) null).request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } result = target("editor/" + sessionId + "/Validate/Event").queryParam("name", (String) null) .queryParam("version", (String) null).request().get(ApexApiResult.class); @@ -949,12 +843,8 @@ public class ApexEditorRestResourceTest extends JerseyTest { .queryParam("version", (String) null).request().get(ApexApiResult.class); assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Task/Delete").queryParam("name", (String) null) + target("editor/" + corruptSessionId + "/Task/Delete").queryParam("name", (String) null) .queryParam("version", (String) null).request().delete(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } result = target("editor/-123345/Task/Delete").queryParam("name", (String) null) .queryParam("version", (String) null).request().delete(ApexApiResult.class); @@ -984,12 +874,7 @@ public class ApexEditorRestResourceTest extends JerseyTest { result = target("editor/" + sessionId + "/Model/Validate").request().get(ApexApiResult.class); assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Model/Validate").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - + target("editor/" + corruptSessionId + "/Model/Validate").request().get(ApexApiResult.class); result = target("editor/" + sessionId + "/Model/Validate").queryParam("name", (String) null) .queryParam("version", (String) null).request().get(ApexApiResult.class); assertEquals(ApexApiResult.Result.FAILED, result.getResult()); @@ -1039,11 +924,7 @@ public class ApexEditorRestResourceTest extends JerseyTest { result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); assertEquals(ApexApiResult.Result.CONCEPT_EXISTS, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + target("editor/" + corruptSessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); entityString = "{" + "\"name\" : \"GoodTaSeeYa\"," + "\"version\" : \"0.0.2\"," + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," @@ -1411,11 +1292,7 @@ public class ApexEditorRestResourceTest extends JerseyTest { .put(entity, ApexApiResult.class); assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Policy/Update").request().put(entity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } + target("editor/" + corruptSessionId + "/Policy/Update").request().put(entity, ApexApiResult.class); entityString = "{" + "\"name\" : null," + "\"version\" : \"0.0.2\"," + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," @@ -1465,12 +1342,8 @@ public class ApexEditorRestResourceTest extends JerseyTest { .queryParam("version", (String) null).request().get(ApexApiResult.class); assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Policy/Get").queryParam("name", "Hello") + target("editor/" + corruptSessionId + "/Policy/Get").queryParam("name", "Hello") .queryParam("version", (String) null).request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } result = target("editor/" + sessionId + "/Validate/Event").queryParam("name", (String) null) .queryParam("version", (String) null).request().get(ApexApiResult.class); @@ -1482,12 +1355,8 @@ public class ApexEditorRestResourceTest extends JerseyTest { .queryParam("version", (String) null).request().get(ApexApiResult.class); assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - try { - target("editor/" + corruptSessionId + "/Policy/Delete").queryParam("name", (String) null) + target("editor/" + corruptSessionId + "/Policy/Delete").queryParam("name", (String) null) .queryParam("version", (String) null).request().delete(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } result = target("editor/-123345/Policy/Delete").queryParam("name", (String) null) .queryParam("version", (String) null).request().delete(ApexApiResult.class); diff --git a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeansTest.java b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeansTest.java index ba773b1ac..bdf47fedc 100644 --- a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeansTest.java +++ b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeansTest.java @@ -1,6 +1,7 @@ /*- * ============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. @@ -20,9 +21,9 @@ package org.onap.policy.apex.client.editor.rest.handling.bean; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; import org.junit.Test; @@ -60,29 +61,9 @@ public class BeansTest { assertNull(beanFake.get("name")); assertNull(beanFake.get("field1")); - try { - beanFake.get("iDontExist"); - fail("test should throw an exception here"); - } catch (final IllegalArgumentException e) { - assertNotNull(e); - } - try { - beanFake.get("nome"); - fail("test should throw an exception here"); - } catch (final IllegalArgumentException e) { - assertNotNull(e); - } - try { - beanFake.get("field2"); - fail("test should throw an exception here"); - } catch (final IllegalArgumentException e) { - assertNotNull(e); - } - try { - beanFake.get("field3"); - fail("test should throw an exception here"); - } catch (final IllegalArgumentException e) { - assertNotNull(e); - } + assertThatThrownBy(() -> beanFake.get("iDontExist")).isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> beanFake.get("nome")).isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> beanFake.get("field2")).isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> beanFake.get("field3")).isInstanceOf(IllegalArgumentException.class); } } diff --git a/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/MonitoringRestMainTest.java b/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/MonitoringRestMainTest.java index 688d29b37..b376c9bfa 100644 --- a/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/MonitoringRestMainTest.java +++ b/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/MonitoringRestMainTest.java @@ -21,9 +21,11 @@ package org.onap.policy.apex.client.monitoring.rest; +import static org.assertj.core.api.Assertions.assertThatCode; +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 static org.junit.Assert.assertNotNull; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -38,24 +40,14 @@ import org.junit.Test; public class MonitoringRestMainTest { @Test public void testMonitoringClientBad() { - try { - final String[] eventArgs = {"-z"}; - - ApexMonitoringRestMain.main(eventArgs); - } catch (Exception exc) { - fail("test should not throw an exception"); - } + final String[] eventArgs = {"-z"}; + assertThatCode(() -> ApexMonitoringRestMain.main(eventArgs)).doesNotThrowAnyException(); } @Test public void testMonitoringClientOk() { - try { - final String[] eventArgs = {"-t", "1"}; - - ApexMonitoringRestMain.main(eventArgs); - } catch (Exception exc) { - fail("test should not throw an exception"); - } + final String[] eventArgs = {"-t", "1"}; + assertThatCode(() -> ApexMonitoringRestMain.main(eventArgs)).doesNotThrowAnyException(); } @Test @@ -72,142 +64,93 @@ public class MonitoringRestMainTest { public void testMonitoringClientBadOptions() { final String[] eventArgs = {"-zabbu"}; - try { - new ApexMonitoringRestMain(eventArgs, System.out); - fail("test should throw an exception"); - } catch (Exception ex) { - assertEquals( - "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) " - + "parameter error, invalid command line arguments specified " + ": Unrecognized option: -zabbu", - ex.getMessage().substring(0, 170)); - } + assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out)) + .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) " + + "parameter error, invalid command line arguments specified " + ": Unrecognized option: -zabbu"); } @Test public void testMonitoringClientHelp() { final String[] eventArgs = {"-h"}; - try { - new ApexMonitoringRestMain(eventArgs, System.out); - fail("test should throw an exception"); - } catch (Exception ex) { - assertEquals("usage: org.onap.policy.apex.client.monitoring.rest.ApexMonitoringRestMain [options...]", - ex.getMessage().substring(0, 86)); - } + assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out)) + .hasMessageContaining("usage: org.onap.policy.apex.client.monitoring.rest." + + "ApexMonitoringRestMain [options...]"); } @Test public void testMonitoringClientPortBad() { final String[] eventArgs = {"-p", "hello"}; - try { - new ApexMonitoringRestMain(eventArgs, System.out); - fail("test should throw an exception"); - } catch (Exception ex) { - assertEquals( - "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) " - + "parameter error, error parsing argument \"port\" :For input string: \"hello\"", - ex.getMessage().substring(0, 156)); - } + assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out)) + .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) " + + "parameter error, error parsing argument \"port\" :For input string: \"hello\""); } @Test public void testMonitoringClientPortNegative() { final String[] eventArgs = {"-p", "-1"}; - try { - new ApexMonitoringRestMain(eventArgs, System.out); - fail("test should throw an exception"); - } catch (Exception ex) { - assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: " - + "URI=http://localhost:-1/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, " - + "port must be greater than 1023 and less than 65536", ex.getMessage().substring(0, 227)); - } + assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out)) + .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRest" + + "Parameters: URI=http://localhost:-1/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, " + + "port must be greater than 1023 and less than 65536"); } @Test public void testMonitoringClientTtlTooSmall() { final String[] eventArgs = {"-t", "-2"}; - try { - new ApexMonitoringRestMain(eventArgs, System.out); - fail("test should throw an exception"); - } catch (Exception ex) { - assertEquals( - "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: " - + "URI=http://localhost:18989/apexservices/, TTL=-2sec], State=STOPPED) parameters invalid, " - + "time to live must be greater than -1 (set to -1 to wait forever)", - ex.getMessage().substring(0, 244)); - } + assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out)) + .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRest" + + "Parameters: URI=http://localhost:18989/apexservices/, TTL=-2sec], State=STOPPED) parameters invalid, " + + "time to live must be greater than -1 (set to -1 to wait forever)"); } @Test public void testMonitoringClientTooManyPars() { final String[] eventArgs = {"-t", "10", "-p", "12344", "aaa", "bbb"}; - try { - new ApexMonitoringRestMain(eventArgs, System.out); - fail("test should throw an exception"); - } catch (Exception ex) { - assertEquals( - "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) " - + "parameter error, too many command line arguments specified : [aaa, bbb]", - ex.getMessage().substring(0, 154)); - } + assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out)) + .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) " + + "parameter error, too many command line arguments specified : [aaa, bbb]"); } @Test public void testMonitoringClientTtlNotNumber() { final String[] eventArgs = {"-t", "timetolive"}; - try { - new ApexMonitoringRestMain(eventArgs, System.out); - fail("test should throw an exception"); - } catch (Exception ex) { - assertEquals( - "Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) " - + "parameter error, error parsing argument \"time-to-live\" :" + "For input string: \"timetolive\"", - ex.getMessage().substring(0, 169)); - } + assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out)) + .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) " + + "parameter error, error parsing argument \"time-to-live\" :" + "For input string: \"" + + "timetolive\""); } @Test public void testMonitoringClientPortTooBig() { final String[] eventArgs = {"-p", "65536"}; - try { - new ApexMonitoringRestMain(eventArgs, System.out); - fail("test should throw an exception"); - } catch (Exception ex) { - assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: " - + "URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, " - + "port must be greater than 1023 and less than 65536", ex.getMessage().substring(0, 230)); - } + assertThatThrownBy(() -> new ApexMonitoringRestMain(eventArgs, System.out)) + .hasMessageContaining("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoring" + + "RestParameters: URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, " + + "port must be greater than 1023 and less than 65536"); } @Test public void testMonitoringClientDefaultPars() { - try { - ApexMonitoringRest monRest = new ApexMonitoringRest(); - monRest.shutdown(); - - } catch (Exception ex) { - fail("test should not throw an exception"); - } + ApexMonitoringRest monRest = new ApexMonitoringRest(); + assertNotNull(monRest); + assertThatCode(() -> monRest.shutdown()).isNull(); } @Test public void testMonitoringOneSecStart() { final String[] eventArgs = {"-t", "1"}; - try { - ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out); - monRestMain.init(); - monRestMain.shutdown(); - - } catch (Exception ex) { - fail("test should not throw an exception"); - } + ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out); + assertNotNull(monRestMain); + monRestMain.init(); + assertThatCode(() -> monRestMain.shutdown()).isNull(); } @Test @@ -222,15 +165,12 @@ public class MonitoringRestMainTest { monRestMain.init(); } }; - - try { + assertThatCode(() -> { monThread.start(); await().atMost(6, TimeUnit.SECONDS) .until(() -> monRestMain.getState().equals(ApexMonitoringRestMain.ServicesState.RUNNING)); monRestMain.shutdown(); - } catch (Exception ex) { - fail("test should not throw an exception"); - } + }).doesNotThrowAnyException(); } /** -- cgit 1.2.3-korg