diff options
author | liamfallon <liam.fallon@est.tech> | 2018-12-08 11:27:17 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2018-12-08 11:27:55 +0000 |
commit | 5b97cd4470b9668ed31bf7663808c32087ba696c (patch) | |
tree | d60fb5a8d41f4074ca221fc3c8654e5db9985aaa /plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java | |
parent | e6753352980648bac92aff9a7295639349ea1295 (diff) |
Support HTTP headers in REST Client
Fixed REST client plugin to supprot HTTP headers and add unit
test to plugin.
Change-Id: I6a71ab7f83ed2126b8600bb5e586f971dbdacdc0
Issue-ID: POLICY-1222
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java')
4 files changed, 894 insertions, 0 deletions
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 new file mode 100644 index 000000000..1498678e9 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConusmerTest.java @@ -0,0 +1,338 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.plugins.event.carrier.restclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.Invocation.Builder; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.Response; + +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; + +/** + * This class tests the ApexRestClientConusmer class. + * + */ +public class ApexRestClientConusmerTest { + private final PrintStream stdout = System.out; + + @Mock + private Client httpClientMock; + + @Mock + private WebTarget targetMock; + + @Mock + private Builder builderMock; + + @Mock + private Response responseMock; + + @Test + public void testApexRestClientConusmerErrors() { + MockitoAnnotations.initMocks(this); + + ApexRestClientConsumer arcc = new ApexRestClientConsumer(); + assertNotNull(arcc); + + 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()); + } + + RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); + consumerParameters.setCarrierTechnologyParameters(rcctp); + rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.DELETE); + try { + 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()); + } + + rcctp.setHttpMethod(null); + try { + arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); + + assertEquals("RestClientConsumer", arcc.getName()); + + arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + } catch (ApexEventException e) { + fail("test should not throw an exception"); + } + + rcctp.setUrl("http://some.place.that.does.not/exist"); + Mockito.doReturn(Response.Status.BAD_REQUEST.getStatusCode()).when(responseMock).getStatus(); + Mockito.doReturn(responseMock).when(builderMock).get(); + Mockito.doReturn(builderMock).when(targetMock).request("application/json"); + Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); + arcc.setClient(httpClientMock); + + try { + // We have not set the URL, this test should not receive any events + arcc.start(); + ThreadUtilities.sleep(200); + arcc.stop(); + + assertEquals(0, incomingEventReceiver.getEventCount()); + } catch (Exception e) { + fail("test should not throw an exception"); + } + + Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); + try { + // We have not set the URL, this test should not receive any events + arcc.start(); + ThreadUtilities.sleep(200); + arcc.stop(); + + assertEquals(0, incomingEventReceiver.getEventCount()); + } catch (Exception e) { + fail("test should not throw an exception"); + } + } + + @Test + public void testApexRestClientConusmerHttpError() { + MockitoAnnotations.initMocks(this); + + ApexRestClientConsumer arcc = new ApexRestClientConsumer(); + assertNotNull(arcc); + + EventHandlerParameters consumerParameters = new EventHandlerParameters(); + RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); + consumerParameters.setCarrierTechnologyParameters(rcctp); + rcctp.setUrl("http://some.place.that.does.not/exist"); + SupportApexEventReceiver incomingEventReceiver = new SupportApexEventReceiver(); + + try { + arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); + + assertEquals("RestClientConsumer", arcc.getName()); + + arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + } catch (ApexEventException e) { + fail("test should not throw an exception"); + } + + Mockito.doReturn(Response.Status.BAD_REQUEST.getStatusCode()).when(responseMock).getStatus(); + Mockito.doReturn(responseMock).when(builderMock).get(); + Mockito.doReturn(builderMock).when(targetMock).request("application/json"); + Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); + arcc.setClient(httpClientMock); + + 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(); + ThreadUtilities.sleep(200); + arcc.stop(); + + assertEquals(0, incomingEventReceiver.getEventCount()); + } catch (Exception e) { + fail("test should not throw an exception"); + } + + final String outString = outContent.toString(); + System.setOut(stdout); + + assertTrue(outString.contains( + "reception of event from URL \"http://some.place.that.does.not/exist\" failed with status code 400")); + } + + @Test + public void testApexRestClientConusmerJsonError() { + MockitoAnnotations.initMocks(this); + + ApexRestClientConsumer arcc = new ApexRestClientConsumer(); + assertNotNull(arcc); + + EventHandlerParameters consumerParameters = new EventHandlerParameters(); + SupportApexEventReceiver incomingEventReceiver = new SupportApexEventReceiver(); + RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); + consumerParameters.setCarrierTechnologyParameters(rcctp); + + try { + arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); + + assertEquals("RestClientConsumer", arcc.getName()); + + arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + } catch (ApexEventException e) { + fail("test should not throw an exception"); + } + + rcctp.setUrl("http://some.place.that.does.not/exist"); + Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); + Mockito.doReturn(responseMock).when(builderMock).get(); + Mockito.doReturn(builderMock).when(targetMock).request("application/json"); + Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); + arcc.setClient(httpClientMock); + + 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(); + ThreadUtilities.sleep(200); + arcc.stop(); + + assertEquals(0, incomingEventReceiver.getEventCount()); + } catch (Exception e) { + fail("test should not throw an exception"); + } + + final String outString = outContent.toString(); + System.setOut(stdout); + + assertTrue(outString.contains( + "received an empty event from URL \"http://some.place.that.does.not/exist\"")); + } + + @Test + public void testApexRestClientConusmerJsonEmpty() { + MockitoAnnotations.initMocks(this); + + ApexRestClientConsumer arcc = new ApexRestClientConsumer(); + assertNotNull(arcc); + + EventHandlerParameters consumerParameters = new EventHandlerParameters(); + SupportApexEventReceiver incomingEventReceiver = new SupportApexEventReceiver(); + RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); + consumerParameters.setCarrierTechnologyParameters(rcctp); + + try { + arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); + + assertEquals("RestClientConsumer", arcc.getName()); + + arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + } catch (ApexEventException e) { + fail("test should not throw an exception"); + } + + rcctp.setUrl("http://some.place.that.does.not/exist"); + Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); + Mockito.doReturn("").when(responseMock).readEntity(String.class); + Mockito.doReturn(responseMock).when(builderMock).get(); + Mockito.doReturn(builderMock).when(targetMock).request("application/json"); + Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); + arcc.setClient(httpClientMock); + + 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(); + ThreadUtilities.sleep(200); + arcc.stop(); + + assertEquals(0, incomingEventReceiver.getEventCount()); + } catch (Exception e) { + fail("test should not throw an exception"); + } + + final String outString = outContent.toString(); + System.setOut(stdout); + + assertTrue(outString.contains( + "received an empty event from URL \"http://some.place.that.does.not/exist\"")); + } + + @Test + public void testApexRestClientConusmerJsonOk() { + MockitoAnnotations.initMocks(this); + + ApexRestClientConsumer arcc = new ApexRestClientConsumer(); + assertNotNull(arcc); + + EventHandlerParameters consumerParameters = new EventHandlerParameters(); + SupportApexEventReceiver incomingEventReceiver = new SupportApexEventReceiver(); + RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); + consumerParameters.setCarrierTechnologyParameters(rcctp); + + try { + arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); + + assertEquals("RestClientConsumer", arcc.getName()); + + arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + } catch (ApexEventException e) { + fail("test should not throw an exception"); + } + + rcctp.setUrl("http://some.place.that.does.not/exist"); + Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); + Mockito.doReturn("This is an event").when(responseMock).readEntity(String.class); + Mockito.doReturn(responseMock).when(builderMock).get(); + Mockito.doReturn(builderMock).when(targetMock).request("application/json"); + Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); + arcc.setClient(httpClientMock); + + try { + // We have not set the URL, this test should not receive any events + arcc.start(); + ThreadUtilities.sleep(200); + arcc.stop(); + + assertEquals("This is an event", incomingEventReceiver.getLastEvent()); + } catch (Exception e) { + fail("test should not throw an exception"); + } + } +} 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 new file mode 100644 index 000000000..3ef172427 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java @@ -0,0 +1,337 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.plugins.event.carrier.restclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import ch.qos.logback.classic.Level; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.Invocation.Builder; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.Response; + +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.policy.apex.service.engine.event.ApexEventConsumer; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.SynchronousEventCache; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.ApexFileEventConsumer; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Test the ApexRestClientProducer class. + * + */ +public class ApexRestClientProducerTest { + private static final Logger LOGGER = LoggerFactory.getLogger(ApexRestClientProducer.class); + + @Mock + private Client httpClientMock; + + @Mock + private WebTarget targetMock; + + @Mock + private Builder builderMock; + + @Mock + private Response responseMock; + + @Test + public void testApexRestClientProducerErrors() { + ApexRestClientProducer arcp = new ApexRestClientProducer(); + 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()); + } + + 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 supproted for event sending on REST client producer (RestClientConsumer)", e.getMessage()); + } + + rcctp.setHttpMethod(null); + try { + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); + + assertEquals("RestClientConsumer", arcp.getName()); + + arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + assertEquals(null, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + + arcp.stop(); + } catch (ApexEventException e) { + fail("test should not throw an exception"); + } + + rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.POST); + try { + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); + + assertEquals("RestClientConsumer", arcp.getName()); + + arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + assertEquals(null, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + + arcp.stop(); + } catch (ApexEventException e) { + fail("test should not throw an exception"); + } + + rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.PUT); + try { + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.PUT, rcctp.getHttpMethod()); + + assertEquals("RestClientConsumer", arcp.getName()); + + arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + assertEquals(null, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + + arcp.stop(); + } catch (ApexEventException e) { + fail("test should not throw an exception"); + } + } + + @Test + public void testApexRestClientProducerPutEvent() { + MockitoAnnotations.initMocks(this); + + ApexRestClientProducer arcp = new ApexRestClientProducer(); + assertNotNull(arcp); + + EventHandlerParameters producerParameters = new EventHandlerParameters(); + RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(rcctp); + + rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.PUT); + try { + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.PUT, rcctp.getHttpMethod()); + + assertEquals("RestClientConsumer", arcp.getName()); + } catch (ApexEventException e) { + fail("test should not throw an exception"); + } + + rcctp.setUrl("http://some.place.that.does.not/exist"); + Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); + Mockito.doReturn(responseMock).when(builderMock).put(Mockito.any()); + Mockito.doReturn(builderMock).when(targetMock).request("application/json"); + Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); + arcp.setClient(httpClientMock); + + try { + arcp.sendEvent(123, "EventName", "This is an Event"); + arcp.stop(); + } catch (Exception e) { + fail("test should not throw an exception"); + } + } + + @Test + public void testApexRestClientProducerPostEvent() { + MockitoAnnotations.initMocks(this); + + ApexRestClientProducer arcp = new ApexRestClientProducer(); + assertNotNull(arcp); + + EventHandlerParameters producerParameters = new EventHandlerParameters(); + RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(rcctp); + + rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.POST); + try { + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); + + assertEquals("RestClientConsumer", arcp.getName()); + } catch (ApexEventException e) { + fail("test should not throw an exception"); + } + + rcctp.setUrl("http://some.place.that.does.not/exist"); + Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); + Mockito.doReturn(responseMock).when(builderMock).post(Mockito.any()); + Mockito.doReturn(builderMock).when(targetMock).request("application/json"); + Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); + arcp.setClient(httpClientMock); + + try { + arcp.sendEvent(123, "EventName", "This is an Event"); + arcp.stop(); + } catch (Exception e) { + fail("test should not throw an exception"); + } + } + + @Test + public void testApexRestClientProducerPostEventCache() { + MockitoAnnotations.initMocks(this); + + ApexRestClientProducer arcp = new ApexRestClientProducer(); + assertNotNull(arcp); + + EventHandlerParameters producerParameters = new EventHandlerParameters(); + RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(rcctp); + + rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.POST); + + ApexEventConsumer consumer = new ApexFileEventConsumer(); + SynchronousEventCache cache = new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, arcp, + 1000); + arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, cache); + assertEquals(cache, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + try { + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); + + assertEquals("RestClientConsumer", arcp.getName()); + } catch (ApexEventException e) { + fail("test should not throw an exception"); + } + + rcctp.setUrl("http://some.place.that.does.not/exist"); + Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); + Mockito.doReturn(responseMock).when(builderMock).post(Mockito.any()); + Mockito.doReturn(builderMock).when(targetMock).request("application/json"); + Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); + arcp.setClient(httpClientMock); + + try { + arcp.sendEvent(123, "EventName", "This is an Event"); + arcp.stop(); + } catch (Exception e) { + fail("test should not throw an exception"); + } + } + + + @Test + public void testApexRestClientProducerPostEventCacheTrace() { + MockitoAnnotations.initMocks(this); + + ch.qos.logback.classic.Logger classicLogger = (ch.qos.logback.classic.Logger) LOGGER; + classicLogger.setLevel(Level.TRACE); + + ApexRestClientProducer arcp = new ApexRestClientProducer(); + assertNotNull(arcp); + + EventHandlerParameters producerParameters = new EventHandlerParameters(); + RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(rcctp); + + rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.POST); + + ApexEventConsumer consumer = new ApexFileEventConsumer(); + SynchronousEventCache cache = new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, arcp, + 1000); + arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, cache); + assertEquals(cache, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + try { + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); + + assertEquals("RestClientConsumer", arcp.getName()); + } catch (ApexEventException e) { + fail("test should not throw an exception"); + } + + rcctp.setUrl("http://some.place.that.does.not/exist"); + Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); + Mockito.doReturn(responseMock).when(builderMock).post(Mockito.any()); + Mockito.doReturn(builderMock).when(targetMock).request("application/json"); + Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); + arcp.setClient(httpClientMock); + + try { + arcp.sendEvent(123, "EventName", "This is an Event"); + arcp.stop(); + } catch (Exception e) { + fail("test should not throw an exception"); + } + } + + @Test + public void testApexRestClientProducerHttpError() { + MockitoAnnotations.initMocks(this); + + ApexRestClientProducer arcp = new ApexRestClientProducer(); + assertNotNull(arcp); + + EventHandlerParameters producerParameters = new EventHandlerParameters(); + RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(rcctp); + + rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.POST); + try { + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); + + assertEquals("RestClientConsumer", arcp.getName()); + } catch (ApexEventException e) { + fail("test should not throw an exception"); + } + + rcctp.setUrl("http://some.place.that.does.not/exist"); + Mockito.doReturn(Response.Status.BAD_REQUEST.getStatusCode()).when(responseMock).getStatus(); + Mockito.doReturn(responseMock).when(builderMock).post(Mockito.any()); + Mockito.doReturn(builderMock).when(targetMock).request("application/json"); + Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); + arcp.setClient(httpClientMock); + + try { + arcp.sendEvent(123, "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()); + } + } +} 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 new file mode 100644 index 000000000..3d0c9b73f --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java @@ -0,0 +1,149 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.plugins.event.carrier.restclient; + +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 org.junit.Test; +import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments; +import org.onap.policy.apex.service.parameters.ApexParameterHandler; +import org.onap.policy.apex.service.parameters.ApexParameters; +import org.onap.policy.common.parameters.ParameterException; + +/** + * Test REST Requestor carrier technology parameters. + */ +public class RestClientCarrierTechnologyParametersTest { + + @Test + public void testRestClientCarrierTechnologyParametersBadList() { + ApexCommandLineArguments arguments = new ApexCommandLineArguments(); + 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")); + } + } + + @Test + public void testRestClientCarrierTechnologyParametersNotKvPairs() { + ApexCommandLineArguments arguments = new ApexCommandLineArguments(); + 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]")); + } + } + + @Test + public void testRestClientCarrierTechnologyParametersNulls() { + ApexCommandLineArguments arguments = new ApexCommandLineArguments(); + 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]")); + } + } + + @Test + public void testRestClientCarrierTechnologyParametersOk() { + ApexCommandLineArguments arguments = new ApexCommandLineArguments(); + arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json"); + arguments.setRelativeFileRoot("."); + + try { + ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + + RestClientCarrierTechnologyParameters rrctp0 = (RestClientCarrierTechnologyParameters) parameters + .getEventInputParameters().get("RestClientConsumer0").getCarrierTechnologyParameters(); + assertEquals(0, rrctp0.getHttpHeaders().length); + + RestClientCarrierTechnologyParameters rrctp1 = (RestClientCarrierTechnologyParameters) parameters + .getEventInputParameters().get("RestClientConsumer1").getCarrierTechnologyParameters(); + assertEquals(3, rrctp1.getHttpHeaders().length); + assertEquals("bbb", rrctp1.getHttpHeadersAsMultivaluedMap().get("aaa").get(0)); + assertEquals("ddd", rrctp1.getHttpHeadersAsMultivaluedMap().get("ccc").get(0)); + assertEquals("fff", rrctp1.getHttpHeadersAsMultivaluedMap().get("eee").get(0)); + + rrctp1.setHttpHeaders(null); + assertEquals(null, rrctp1.getHttpHeadersAsMultivaluedMap()); + } catch (ParameterException pe) { + fail("test should not throw an exception"); + } + } + + @Test + public void testGettersAndSetters() { + RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters(); + + rrctp.setUrl("http://some.where"); + assertEquals("http://some.where", rrctp.getUrl()); + + String[][] httpHeaders = new String[2][2]; + httpHeaders[0][0] = "aaa"; + httpHeaders[0][1] = "bbb"; + httpHeaders[1][0] = "ccc"; + httpHeaders[1][1] = "ddd"; + + rrctp.setHttpHeaders(httpHeaders); + assertEquals("aaa", rrctp.getHttpHeaders()[0][0]); + assertEquals("bbb", rrctp.getHttpHeaders()[0][1]); + assertEquals("ccc", rrctp.getHttpHeaders()[1][0]); + assertEquals("ddd", rrctp.getHttpHeaders()[1][1]); + + rrctp.setHttpHeaders(null); + assertFalse(rrctp.checkHttpHeadersSet()); + + String[][] httpHeadersZeroLength = new String[0][0]; + rrctp.setHttpHeaders(httpHeadersZeroLength); + assertFalse(rrctp.checkHttpHeadersSet()); + + rrctp.setHttpHeaders(httpHeaders); + assertTrue(rrctp.checkHttpHeadersSet()); + + rrctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.DELETE); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.DELETE, rrctp.getHttpMethod()); + + assertEquals("RestClientCarrierTechnologyParameters " + + "[url=http://some.where, httpMethod=DELETE, httpHeaders=[[aaa, bbb], [ccc, ddd]]]", + rrctp.toString()); + } +} diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/SupportApexEventReceiver.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/SupportApexEventReceiver.java new file mode 100644 index 000000000..d3f8b9e82 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/SupportApexEventReceiver.java @@ -0,0 +1,70 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.plugins.event.carrier.restclient; + +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.ApexEventReceiver; + +/** + * Support Apex event reveiver for unit test. + * + */ +public class SupportApexEventReceiver implements ApexEventReceiver { + private long lastExecutionId; + private Object lastEvent; + private int eventCount; + + /* (non-Javadoc) + * @see org.onap.policy.apex.service.engine.event.ApexEventReceiver#receiveEvent(long, java.lang.Object) + */ + @Override + public void receiveEvent(long executionId, Object event) throws ApexEventException { + this.lastExecutionId = executionId; + this.lastEvent = event; + this.eventCount++; + } + + /* (non-Javadoc) + * @see org.onap.policy.apex.service.engine.event.ApexEventReceiver#receiveEvent(java.lang.Object) + */ + @Override + public void receiveEvent(Object event) throws ApexEventException { + this.lastEvent = event; + this.eventCount++; + } + + public long getLastExecutionId() { + return lastExecutionId; + } + + public Object getLastEvent() { + return lastEvent; + } + + /** + * Get the number of events received. + * + * @return the number of events received + */ + public int getEventCount() { + return eventCount; + } +} |