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 +++++---------- pom.xml | 5 + .../common/model/SampleDomainModelSaverTest.java | 31 +--- .../common/testclasses/TestPingClassTest.java | 83 +++------ .../context/distribution/ContextInstantiation.java | 105 +++-------- 8 files changed, 178 insertions(+), 544 deletions(-) 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(); } /** diff --git a/pom.xml b/pom.xml index 4529146ac..d2e9c8ece 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,11 @@ junit test + + org.assertj + assertj-core + test + org.awaitility awaitility diff --git a/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/testsuites/integration/common/model/SampleDomainModelSaverTest.java b/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/testsuites/integration/common/model/SampleDomainModelSaverTest.java index 241f69dc0..c7ab1d9a0 100644 --- a/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/testsuites/integration/common/model/SampleDomainModelSaverTest.java +++ b/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/testsuites/integration/common/model/SampleDomainModelSaverTest.java @@ -1,28 +1,29 @@ /*- * ============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.testsuites.integration.common.model; +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.File; import java.io.IOException; @@ -30,6 +31,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Comparator; import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; /** * Test the sample domain model saver. @@ -37,32 +39,19 @@ import org.junit.Test; public class SampleDomainModelSaverTest { @Test - public void testSampleDomainModelSaver() throws IOException { - try { - SampleDomainModelSaver.main(null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("java.lang.NullPointerException", exc.getClass().getName()); - } + public void testSampleDomainModelSaver() throws IOException, ApexException { + assertThatThrownBy(() -> SampleDomainModelSaver.main(null)).isInstanceOf(NullPointerException.class); String[] args0 = { "two", "arguments" }; - try { - SampleDomainModelSaver.main(args0); - } catch (Exception exc) { - fail("test should not throw an exception"); - } + SampleDomainModelSaver.main(args0); Path tempDirectory = Files.createTempDirectory("ApexModelTempDir"); String[] args1 = { tempDirectory.toString() }; - try { - SampleDomainModelSaver.main(args1); - } catch (Exception exc) { - fail("test should not throw an exception"); - } + SampleDomainModelSaver.main(args1); File tempDir = new File(tempDirectory.toString()); assertTrue(tempDir.isDirectory()); diff --git a/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/testsuites/integration/common/testclasses/TestPingClassTest.java b/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/testsuites/integration/common/testclasses/TestPingClassTest.java index 82f46c04a..92f86f434 100644 --- a/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/testsuites/integration/common/testclasses/TestPingClassTest.java +++ b/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/testsuites/integration/common/testclasses/TestPingClassTest.java @@ -21,8 +21,8 @@ package org.onap.policy.apex.testsuites.integration.common.testclasses; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; import org.junit.Test; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; @@ -32,7 +32,7 @@ import org.onap.policy.apex.model.basicmodel.concepts.ApexException; */ public class TestPingClassTest { @Test - public void testPingClass() { + public void testPingClass() throws ApexException { PingTestClass ptc = new PingTestClass(); ptc.setName("Hello"); @@ -46,84 +46,43 @@ public class TestPingClassTest { ptc.setPongTime(-1); assertEquals(-1, ptc.getPongTime()); - - try { - ptc.verify(); - fail("test should throw an exception"); - } catch (ApexException ae) { - assertEquals("TestPing is not valid, name does not start with \"Rose\"", ae.getMessage()); - } + assertThatThrownBy(() -> ptc.verify()).isInstanceOf(ApexException.class) + .hasMessageContaining("TestPing is not valid, name does not start with \"Rose\""); ptc.setName(null); - try { - ptc.verify(); - fail("test should throw an exception"); - } catch (ApexException ae) { - assertEquals("TestPing is not valid, name length null or less than 4", ae.getMessage()); - } + assertThatThrownBy(() -> ptc.verify()).isInstanceOf(ApexException.class) + .hasMessageContaining("TestPing is not valid, name length null or less than 4"); ptc.setName("Ros"); - try { - ptc.verify(); - fail("test should throw an exception"); - } catch (ApexException ae) { - assertEquals("TestPing is not valid, name length null or less than 4", ae.getMessage()); - } + assertThatThrownBy(() -> ptc.verify()).isInstanceOf(ApexException.class) + .hasMessageContaining("TestPing is not valid, name length null or less than 4"); ptc.setName("Rose"); - try { - ptc.verify(); - fail("test should throw an exception"); - } catch (ApexException ae) { - assertEquals("TestPing is not valid, description length null or less than 44", ae.getMessage()); - } + assertThatThrownBy(() -> ptc.verify()).isInstanceOf(ApexException.class) + .hasMessageContaining("TestPing is not valid, description length null or less than 44"); ptc.setDescription(null); - try { - ptc.verify(); - fail("test should throw an exception"); - } catch (ApexException ae) { - assertEquals("TestPing is not valid, description length null or less than 44", ae.getMessage()); - } + assertThatThrownBy(() -> ptc.verify()).isInstanceOf(ApexException.class) + .hasMessageContaining("TestPing is not valid, description length null or less than 44"); ptc.setDescription("A rose by any other name would smell as swee"); - try { - ptc.verify(); - fail("test should throw an exception"); - } catch (ApexException ae) { - assertEquals("TestPing is not valid, description length null or less than 44", ae.getMessage()); - } + assertThatThrownBy(() -> ptc.verify()).isInstanceOf(ApexException.class) + .hasMessageContaining("TestPing is not valid, description length null or less than 44"); ptc.setDescription("A rose by any other name would smell as swell"); - try { - ptc.verify(); - fail("test should throw an exception"); - } catch (ApexException ae) { - assertEquals("TestPing is not valid, description is incorrect", ae.getMessage()); - } + assertThatThrownBy(() -> ptc.verify()).isInstanceOf(ApexException.class) + .hasMessageContaining("TestPing is not valid, description is incorrect"); ptc.setDescription("A rose by any other name would smell as sweet"); - try { - ptc.verify(); - fail("test should throw an exception"); - } catch (ApexException ae) { - assertEquals("TestPing is not valid, pong time -1 is less than ping time 0", ae.getMessage()); - } + assertThatThrownBy(() -> ptc.verify()).isInstanceOf(ApexException.class) + .hasMessageContaining("TestPing is not valid, pong time -1 is less than ping time 0"); ptc.setPongTime(-2); - try { - ptc.verify(); - fail("test should throw an exception"); - } catch (ApexException ae) { - assertEquals("TestPing is not valid, pong time -2 is less than ping time 0", ae.getMessage()); - } + assertThatThrownBy(() -> ptc.verify()).isInstanceOf(ApexException.class) + .hasMessageContaining("TestPing is not valid, pong time -2 is less than ping time 0"); ptc.setPongTime(1); - try { - ptc.verify(); - } catch (ApexException ae) { - fail("test should not throw an exception"); - } + ptc.verify(); assertEquals( "PingTestClass(id=0, name=Rose, " diff --git a/testsuites/integration/integration-context-test/src/test/java/org/onap/policy/apex/testsuites/integration/context/distribution/ContextInstantiation.java b/testsuites/integration/integration-context-test/src/test/java/org/onap/policy/apex/testsuites/integration/context/distribution/ContextInstantiation.java index 475316a4d..9e9e4626e 100644 --- a/testsuites/integration/integration-context-test/src/test/java/org/onap/policy/apex/testsuites/integration/context/distribution/ContextInstantiation.java +++ b/testsuites/integration/integration-context-test/src/test/java/org/onap/policy/apex/testsuites/integration/context/distribution/ContextInstantiation.java @@ -21,6 +21,7 @@ package org.onap.policy.apex.testsuites.integration.context.distribution; +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; @@ -242,15 +243,8 @@ public class ContextInstantiation { assertTrue(externalContextAlbum.values().containsAll(mapValues)); // Check that clearing does not work - try { - externalContextAlbum.clear(); - fail(EXCEPTION_MESSAGE); - } catch (final ContextRuntimeException e) { - assertEquals("album \"ExternalContextAlbum:0.0.1\" clear() not allowed on read only albums", - e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } - + assertThatThrownBy(() -> externalContextAlbum.clear()).isInstanceOf(ContextRuntimeException.class) + .hasMessageContaining("album \"ExternalContextAlbum:0.0.1\" clear() not allowed on read only albums"); assertEquals(1, externalContextAlbum.size()); assertContextAlbumContains(externalContext, externalContextAlbum); @@ -258,32 +252,16 @@ public class ContextInstantiation { final Set> entrySet = externalContextAlbum.entrySet(); assertEquals(1, entrySet.size()); - try { - externalContextAlbum.get(null); - } catch (final ContextRuntimeException e) { - assertEquals("album \"ExternalContextAlbum:0.0.1\" null keys are illegal on keys for get()", - e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } - + assertThatThrownBy(() -> externalContextAlbum.get(null)).isInstanceOf(ContextRuntimeException.class) + .hasMessageContaining("album \"ExternalContextAlbum:0.0.1\" null keys are illegal on keys for get()"); final Object aObject = externalContextAlbum.get(EXTERNAL_CONTEXT); assertEquals(aObject, externalContext); // put null keys should fail, throws a runtime exception - try { - externalContextAlbum.put(null, null); - } catch (final ContextRuntimeException e) { - assertEquals("album \"ExternalContextAlbum:0.0.1\" null keys are illegal on keys for put()", - e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } - - try { - externalContextAlbum.put("TestExternalContextItem00A", null); - } catch (final ContextRuntimeException e) { - assertEquals(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItem00A\" for put()", e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + assertThatThrownBy(() -> externalContextAlbum.put(null, null)) + .hasMessageContaining("album \"ExternalContextAlbum:0.0.1\" null keys are illegal on keys for put()"); + assertThatThrownBy(() -> externalContextAlbum.put("TestExternalContextItem00A", null)) + .hasMessageContaining(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItem00A\" for put()"); assertEquals(tciAa, externalContextItem.getTestExternalContextItem00A()); // Should return the hash set @@ -305,29 +283,17 @@ public class ContextInstantiation { assertTrue(externalContextAlbum.put(EXTERNAL_CONTEXT, externalContext).equals(externalContextOther)); externalContextItem = (TestExternalContextItem) externalContextAlbum.get(EXTERNAL_CONTEXT); assertEquals(INT_VAL_3, externalContextItem.getTestExternalContextItem002().getIntValue()); + assertThatThrownBy(() -> externalContextAlbum.put("TestExternalContextItem00A", null)) + .hasMessageContaining(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItem00A\" for put()"); - try { - externalContextAlbum.put("TestExternalContextItem00A", null); - } catch (final ContextRuntimeException e) { - assert (e.getMessage().equals(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItem00A\" for put()")); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } assertTrue(externalContextAlbum.get(EXTERNAL_CONTEXT).equals(externalContext)); - try { - externalContextAlbum.put("TestExternalContextItemFFF", null); - } catch (final ContextRuntimeException e) { - assert (e.getMessage().equals(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItemFFF\" for put()")); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + assertThatThrownBy(() -> externalContextAlbum.put("TestExternalContextItemFFF", null)) + .hasMessageContaining(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItemFFF\" for put()"); assertEquals(1, externalContextAlbum.size()); - try { - externalContextAlbum.put("TestExternalContextItemFFF", null); - } catch (final ContextRuntimeException e) { - assertEquals(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItemFFF\" for put()", e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + assertThatThrownBy(() -> externalContextAlbum.put("TestExternalContextItemFFF", null)) + .hasMessageContaining(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItemFFF\" for put()"); assertEquals(1, externalContextAlbum.size()); // Should ignore remove @@ -341,23 +307,13 @@ public class ContextInstantiation { private void assertContextAlbumContains(final TestExternalContextItem externalContext, final ContextAlbum externalContextAlbum) { - try { - externalContextAlbum.containsKey(null); - } catch (final ContextRuntimeException e) { - assertEquals("null values are illegal on method parameter \"key\"", e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } - + assertThatThrownBy(() -> externalContextAlbum.containsKey(null)) + .hasMessageContaining("null values are illegal on method parameter \"key\""); assertTrue(externalContextAlbum.containsKey(EXTERNAL_CONTEXT)); assertTrue(!externalContextAlbum.containsKey(GLOBAL_CONTEXT_KEY)); - try { - externalContextAlbum.containsValue(null); - } catch (final ContextRuntimeException e) { - assertEquals("null values are illegal on method parameter \"value\"", e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } - + assertThatThrownBy(() -> externalContextAlbum.containsValue(null)) + .hasMessageContaining("null values are illegal on method parameter \"value\""); assertTrue(externalContextAlbum.containsValue(externalContext)); assertFalse(externalContextAlbum.containsValue("Hello")); } @@ -444,23 +400,12 @@ public class ContextInstantiation { private void assertPutMethods(final ContextAlbum policyContextAlbum, final TestContextStringItem contextStringItem, final Map valueMapA) { - try { - policyContextAlbum.put(TEST_POLICY_CONTEXT_ITEM000, contextStringItem); - fail(EXCEPTION_MESSAGE); - } catch (final ContextRuntimeException e) { - assertEquals(getMessage(TEST_POLICY_CONTEXT_ITEM000, "TestContextItem006", - TestContextStringItem.class.getName(), "stringValue=" + STRING_VAL), e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } - - try { - policyContextAlbum.putAll(valueMapA); - fail(EXCEPTION_MESSAGE); - } catch (final ContextRuntimeException e) { - assertEquals(getMessage(TEST_POLICY_CONTEXT_ITEM001, "TestContextItem003", - TestContextLongItem.class.getName(), "longValue=" + INT_VAL_3), e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + assertThatThrownBy(() -> policyContextAlbum.put(TEST_POLICY_CONTEXT_ITEM000, contextStringItem)) + .hasMessageContaining(getMessage(TEST_POLICY_CONTEXT_ITEM000, "TestContextItem006", + TestContextStringItem.class.getName(), "stringValue=" + STRING_VAL)); + assertThatThrownBy(() -> policyContextAlbum.putAll(valueMapA)) + .hasMessageContaining(getMessage(TEST_POLICY_CONTEXT_ITEM001, "TestContextItem003", + TestContextLongItem.class.getName(), "longValue=" + INT_VAL_3)); } private AxContextModel getAxContextModel() { -- cgit 1.2.3-korg