From b8e3df6f43e46159c210ac7d1ec6983871d52ea5 Mon Sep 17 00:00:00 2001 From: waynedunican Date: Fri, 10 Jul 2020 08:25:22 +0100 Subject: Replaced try/catch blocks with assertj - apex-pdp Removed try/catch blocks in apex-pdp with assertj assertions Part IV Issue-ID: POLICY-2451 Change-Id: I4b6accb9f944195db9ccf8a5de165b169301ac7f Signed-off-by: waynedunican --- .../restclient/ApexRestClientConusmerTest.java | 52 +++++++--------------- .../restclient/ApexRestClientProducerTest.java | 50 +++++++-------------- .../RestClientCarrierTechnologyParametersTest.java | 33 ++++---------- 3 files changed, 41 insertions(+), 94 deletions(-) (limited to 'plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient') diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConusmerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConusmerTest.java index ea5e80161..e506513a8 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConusmerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConusmerTest.java @@ -21,6 +21,7 @@ package org.onap.policy.apex.plugins.event.carrier.restclient; +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.assertNotNull; @@ -76,27 +77,19 @@ public class ApexRestClientConusmerTest { EventHandlerParameters consumerParameters = new EventHandlerParameters(); SupportApexEventReceiver incomingEventReceiver = new SupportApexEventReceiver(); - try { - arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); - fail("test should throw an exception here"); - } catch (ApexEventException e) { - assertEquals( - "specified consumer properties are not applicable to REST client consumer (RestClientConsumer)", - e.getMessage()); - } + assertThatThrownBy(() -> arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver)) + .hasMessageContaining("specified consumer properties are not applicable to REST client" + + " consumer (RestClientConsumer)"); RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(rcctp); rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.DELETE); - try { + assertThatThrownBy(() -> { arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); - fail("test should throw an exception here"); - } catch (ApexEventException e) { - assertEquals("specified HTTP method of \"DELETE\" is invalid, only HTTP method \"GET\" is supported " - + "for event reception on REST client consumer (RestClientConsumer)", e.getMessage()); - } + }).hasMessageContaining("specified HTTP method of \"DELETE\" is invalid, only HTTP method \"GET\" is " + + "supported for event reception on REST client consumer (RestClientConsumer)"); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.DELETE, rcctp.getHttpMethod()); rcctp.setHttpMethod(null); rcctp.setHttpCodeFilter("zzz"); @@ -174,7 +167,7 @@ public class ApexRestClientConusmerTest { } @Test - public void testApexRestClientConsumerJsonError() { + public void testApexRestClientConsumerJsonError() throws ApexEventException { MockitoAnnotations.initMocks(this); ApexRestClientConsumer arcc = new ApexRestClientConsumer(); @@ -186,18 +179,13 @@ public class ApexRestClientConusmerTest { consumerParameters.setCarrierTechnologyParameters(rcctp); rcctp.setHttpCodeFilter("[1-5][0][0-5]"); - try { - arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); - - assertEquals("RestClientConsumer", arcc.getName()); + arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); + assertEquals("RestClientConsumer", arcc.getName()); - arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); - assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); - } catch (ApexEventException e) { - fail("test should not throw an exception"); - } + assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); rcctp.setUrl("http://some.place.that.does.not/exist"); Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); @@ -341,14 +329,8 @@ public class ApexRestClientConusmerTest { ByteArrayOutputStream outContent = new ByteArrayOutputStream(); System.setOut(new PrintStream(outContent)); - try { - // We have not set the URL, this test should not receive any events - arcc.start(); - await().atMost(200, TimeUnit.MILLISECONDS).until(() -> incomingEventReceiver.getEventCount() == 0); - arcc.stop(); - } catch (Exception e) { - // test invalid status code - assertEquals("received an invalid status code \"200\"", e.getMessage()); - } + arcc.start(); + await().atMost(200, TimeUnit.MILLISECONDS).until(() -> incomingEventReceiver.getEventCount() == 0); + arcc.stop(); } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java index 0ee8fe6ad..a4ef6e38e 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java @@ -21,9 +21,9 @@ package org.onap.policy.apex.plugins.event.carrier.restclient; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; import java.util.Properties; import javax.ws.rs.client.Client; @@ -64,29 +64,18 @@ public class ApexRestClientProducerTest { assertNotNull(arcp); EventHandlerParameters producerParameters = new EventHandlerParameters(); - try { - arcp.init("RestClientProducer", producerParameters); - fail("test should throw an exception here"); - } catch (ApexEventException e) { - assertEquals( - "specified producer properties are not applicable to REST client producer (RestClientProducer)", - e.getMessage()); - } + assertThatThrownBy(() -> arcp.init("RestClientProducer", producerParameters)) + .hasMessage("specified producer properties are not applicable to REST client producer" + + " (RestClientProducer)"); RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); producerParameters.setCarrierTechnologyParameters(rcctp); rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.DELETE); - try { - arcp.init("RestClientConsumer", producerParameters); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); - fail("test should throw an exception here"); - } catch (ApexEventException e) { - assertEquals( - "specified HTTP method of \"DELETE\" is invalid, only HTTP methods \"POST\" and \"PUT\" " - + "are supported for event sending on REST client producer (RestClientConsumer)", - e.getMessage()); - } + assertThatThrownBy(() -> arcp.init("RestClientConsumer", producerParameters)) + .hasMessage("specified HTTP method of \"DELETE\" is invalid, only HTTP methods \"POST\"" + + " and \"PUT\" are supported for event sending on REST client producer (RestClientConsumer)"); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.DELETE, rcctp.getHttpMethod()); rcctp.setHttpMethod(null); arcp.init("RestClientConsumer", producerParameters); assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); @@ -168,15 +157,12 @@ public class ApexRestClientProducerTest { rcctp.setUrl("http://some.place2.that.{key}.not/{tag}and.again.{tag}"); Properties properties = new Properties(); properties.put("tag", "exist"); - try { + assertThatThrownBy(() -> { arcp.sendEvent(123, properties, "EventName", "This is an Event"); arcp.stop(); - fail("test should throw an exception"); - } catch (Exception e) { - assertEquals("key \"key\" specified on url " - + "\"http://some.place2.that.{key}.not/{tag}and.again.{tag}\" not found " - + "in execution properties passed by the current policy", e.getMessage()); - } + }).hasMessageContaining("key \"key\" specified on url " + + "\"http://some.place2.that.{key}.not/{tag}and.again.{tag}\" not found " + + "in execution properties passed by the current policy"); } @Test @@ -303,14 +289,8 @@ public class ApexRestClientProducerTest { Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); arcp.setClient(httpClientMock); - try { - arcp.sendEvent(123, null, "EventName", "This is an Event"); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals( - "send of event to URL \"http://some.place.that.does.not/exist\" using HTTP \"POST\" " - + "failed with status code 400 and message \"null\", event:\n" + "This is an Event", - e.getMessage()); - } + assertThatThrownBy(() -> arcp.sendEvent(123, null, "EventName", "This is an Event")) + .hasMessageContaining("send of event to URL \"http://some.place.that.does.not/exist\" using HTTP \"POST\" " + + "failed with status code 400 and message \"null\", event:\n" + "This is an Event"); } } \ No newline at end of file diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java index 42a9c0f73..f704ad3ec 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java @@ -22,10 +22,10 @@ package org.onap.policy.apex.plugins.event.carrier.restclient; import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.util.Set; import org.junit.Test; @@ -45,13 +45,8 @@ public class RestClientCarrierTechnologyParametersTest { arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderBadList.json"); arguments.setRelativeFileRoot("."); - try { - new ApexParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (ParameterException pe) { - assertTrue(pe.getMessage().contains("HTTP header array entry is null\n parameter")); - assertTrue(pe.getMessage().trim().endsWith("HTTP header array entry is null")); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessageContaining("HTTP header array entry is null\n parameter"); } @Test @@ -60,15 +55,9 @@ public class RestClientCarrierTechnologyParametersTest { arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNotKvPairs.json"); arguments.setRelativeFileRoot("."); - try { - new ApexParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (ParameterException pe) { - assertTrue(pe.getMessage() - .contains("HTTP header array entries must have one key and one value: [aaa, bbb, ccc]")); - assertTrue(pe.getMessage().trim() - .endsWith("HTTP header array entries must have one key and one value: [aaa]")); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessageContaining("HTTP header array entries must have one key and one value: [aaa, bbb, ccc]") + .hasMessageEndingWith("HTTP header array entries must have one key and one value: [aaa]\n"); } @Test @@ -77,13 +66,9 @@ public class RestClientCarrierTechnologyParametersTest { arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNulls.json"); arguments.setRelativeFileRoot("."); - try { - new ApexParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (ParameterException pe) { - assertTrue(pe.getMessage().contains("HTTP header key is null or blank: [null, bbb]")); - assertTrue(pe.getMessage().trim().endsWith("HTTP header value is null or blank: [ccc, null]")); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessageContaining("HTTP header key is null or blank: [null, bbb]") + .hasMessageEndingWith("HTTP header value is null or blank: [ccc, null]\n"); } @Test -- cgit 1.2.3-korg