From e88ba595a17e2cb66f9c8b2c40249d29bdd5e193 Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Fri, 8 Mar 2019 17:09:57 +0900 Subject: Increase test coverage for plugins-event-carrier Add basic JUnit test cases for plugins-event-carrier-restserver Issue-ID: POLICY-1513 Change-Id: I36e41bd05be71c44eb9e94350f4614437b0d205b Signed-off-by: Parshad Patel --- .../restserver/ApexRestServerConsumerTest.java | 165 +++++++++++++++++++++ .../restserver/ApexRestServerProducerTest.java | 118 +++++++++++++++ .../RestServerCarrierTechnologyParametersTest.java | 86 +++++++++++ .../restserver/SupportApexEventReceiver.java | 70 +++++++++ 4 files changed, 439 insertions(+) create mode 100644 plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/ApexRestServerConsumerTest.java create mode 100644 plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/ApexRestServerProducerTest.java create mode 100644 plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/RestServerCarrierTechnologyParametersTest.java create mode 100644 plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/SupportApexEventReceiver.java (limited to 'plugins/plugins-event') diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/ApexRestServerConsumerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/ApexRestServerConsumerTest.java new file mode 100644 index 000000000..a86ffb18a --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/ApexRestServerConsumerTest.java @@ -0,0 +1,165 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Samsung. 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.restserver; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import java.lang.reflect.Field; +import javax.ws.rs.core.Response; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.ApexEventReceiver; +import org.onap.policy.apex.service.engine.event.PeeredReference; +import org.onap.policy.apex.service.engine.event.SynchronousEventCache; +import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; + +public class ApexRestServerConsumerTest { + + ApexRestServerConsumer apexRestServerConsumer = null; + EventHandlerParameters consumerParameters = null; + ApexEventReceiver incomingEventReceiver = null; + ApexRestServerProducer apexRestServerProducer = null; + RestServerCarrierTechnologyParameters restServerCarrierTechnologyParameters = null; + SynchronousEventCache synchronousEventCache = null; + + /** + * Set up testing. + * + * @throws Exception on test set up errors. + */ + @Before + public void setUp() throws Exception { + apexRestServerConsumer = new ApexRestServerConsumer(); + consumerParameters = new EventHandlerParameters(); + apexRestServerProducer = new ApexRestServerProducer(); + apexRestServerConsumer.start(); + } + + @After + public void tearDown() { + apexRestServerConsumer.stop(); + } + + @Test(expected = ApexEventException.class) + public void testInitWithNonWebSocketCarrierTechnologyParameters() throws ApexEventException { + consumerParameters.setCarrierTechnologyParameters(new CarrierTechnologyParameters() {}); + apexRestServerConsumer.init("TestApexRestServerConsumer", consumerParameters, + incomingEventReceiver); + } + + @Test(expected = ApexEventException.class) + public void testInitWithWebSocketCarrierTechnologyParameters() throws ApexEventException { + restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters(); + consumerParameters.setCarrierTechnologyParameters(restServerCarrierTechnologyParameters); + apexRestServerConsumer.init("TestApexRestServerConsumer", consumerParameters, + incomingEventReceiver); + } + + @Test(expected = ApexEventException.class) + public void testInitWithSynchronousMode() throws ApexEventException, NoSuchFieldException, + SecurityException, IllegalArgumentException, IllegalAccessException { + restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters(); + Field field = RestServerCarrierTechnologyParameters.class.getDeclaredField("standalone"); + field.setAccessible(true); + field.set(restServerCarrierTechnologyParameters, true); + consumerParameters.setCarrierTechnologyParameters(restServerCarrierTechnologyParameters); + consumerParameters.setPeeredMode(EventHandlerPeeredMode.SYNCHRONOUS, true); + apexRestServerConsumer.init("TestApexRestServerConsumer", consumerParameters, + incomingEventReceiver); + } + + @Test(expected = IllegalStateException.class) + public void testInitWithSynchronousModeAndProperValues() + throws ApexEventException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { + + restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters(); + + Field field = RestServerCarrierTechnologyParameters.class.getDeclaredField("standalone"); + field.setAccessible(true); + field.set(restServerCarrierTechnologyParameters, true); + field = RestServerCarrierTechnologyParameters.class.getDeclaredField("host"); + field.setAccessible(true); + field.set(restServerCarrierTechnologyParameters, "1ocalhost"); + field = RestServerCarrierTechnologyParameters.class.getDeclaredField("port"); + field.setAccessible(true); + field.set(restServerCarrierTechnologyParameters, 65535); + + consumerParameters.setCarrierTechnologyParameters(restServerCarrierTechnologyParameters); + consumerParameters.setPeeredMode(EventHandlerPeeredMode.SYNCHRONOUS, true); + apexRestServerConsumer.init("TestApexRestServerConsumer", consumerParameters, + incomingEventReceiver); + } + + @Test + public void testGetName() { + assertNull(apexRestServerConsumer.getName()); + } + + @Test + public void testGetPeeredReference() { + assertNull(apexRestServerConsumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); + } + + @Test + public void testSetPeeredReference() { + PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR, + apexRestServerConsumer, apexRestServerProducer); + apexRestServerConsumer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, + peeredReference); + assertNotNull(apexRestServerConsumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); + } + + @Test + public void testReceiveEvent() throws ApexEventException { + Response response = apexRestServerConsumer.receiveEvent(""); + assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus()); + } + + @Test(expected = NullPointerException.class) + public void testReceiveEventWithNonDefaultValues() + throws ApexEventException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { + + PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR, + apexRestServerConsumer, apexRestServerProducer); + apexRestServerConsumer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, + peeredReference); + + ApexEventReceiver apexEventReceiver = new SupportApexEventReceiver(); + + Field field = ApexRestServerConsumer.class.getDeclaredField("eventReceiver"); + field.setAccessible(true); + field.set(apexRestServerConsumer, apexEventReceiver); + field = ApexRestServerConsumer.class.getDeclaredField("name"); + field.setAccessible(true); + field.set(apexRestServerConsumer, "TestApexRestServerConsumer"); + + apexRestServerConsumer.receiveEvent("TestApexRestServerConsumer"); + + } + + +} diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/ApexRestServerProducerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/ApexRestServerProducerTest.java new file mode 100644 index 000000000..a286336bf --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/ApexRestServerProducerTest.java @@ -0,0 +1,118 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Samsung. 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.restserver; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import java.lang.reflect.Field; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.ApexEventReceiver; +import org.onap.policy.apex.service.engine.event.PeeredReference; +import org.onap.policy.apex.service.engine.event.SynchronousEventCache; +import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; + +public class ApexRestServerProducerTest { + + ApexRestServerProducer apexRestServerProducer = null; + EventHandlerParameters producerParameters = null; + ApexEventReceiver incomingEventReceiver = null; + ApexRestServerConsumer apexRestServerConsumer = null; + RestServerCarrierTechnologyParameters restServerCarrierTechnologyParameters = null; + SynchronousEventCache synchronousEventCache = null; + + /** + * Set up testing. + * + * @throws Exception on test set up errors. + */ + @Before + public void setUp() throws Exception { + apexRestServerConsumer = new ApexRestServerConsumer(); + producerParameters = new EventHandlerParameters(); + apexRestServerProducer = new ApexRestServerProducer(); + } + + @After + public void tearDown() { + apexRestServerProducer.stop(); + } + + @Test(expected = ApexEventException.class) + public void testInitWithNonWebSocketCarrierTechnologyParameters() throws ApexEventException { + producerParameters.setCarrierTechnologyParameters(new CarrierTechnologyParameters() {}); + apexRestServerProducer.init("TestApexRestServerProducer", producerParameters); + } + + @Test(expected = ApexEventException.class) + public void testInitWithWebSocketCarrierTechnologyParameters() throws ApexEventException { + restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(restServerCarrierTechnologyParameters); + apexRestServerProducer.init("TestApexRestServerProducer", producerParameters); + } + + + @Test(expected = ApexEventException.class) + public void testInitWithNonDefaultValue() throws ApexEventException, NoSuchFieldException, + SecurityException, IllegalArgumentException, IllegalAccessException { + restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters(); + Field field = RestServerCarrierTechnologyParameters.class.getDeclaredField("host"); + field.setAccessible(true); + field.set(restServerCarrierTechnologyParameters, "1ocalhost"); + field = RestServerCarrierTechnologyParameters.class.getDeclaredField("port"); + field.setAccessible(true); + field.set(restServerCarrierTechnologyParameters, 65535); + producerParameters.setCarrierTechnologyParameters(restServerCarrierTechnologyParameters); + apexRestServerProducer.init("TestApexRestServerProducer", producerParameters); + } + + @Test + public void testInitWithSynchronousMode() throws ApexEventException { + restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(restServerCarrierTechnologyParameters); + producerParameters.setPeeredMode(EventHandlerPeeredMode.SYNCHRONOUS, true); + apexRestServerProducer.init("TestApexRestServerProducer", producerParameters); + assertEquals("TestApexRestServerProducer", apexRestServerProducer.getName()); + } + + @Test + public void testGetName() { + assertNull(apexRestServerProducer.getName()); + } + + @Test + public void testGetPeeredReference() { + assertNull(apexRestServerProducer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); + } + + @Test + public void testSetPeeredReference() { + PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR, + apexRestServerConsumer, apexRestServerProducer); + apexRestServerProducer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, + peeredReference); + assertNotNull(apexRestServerProducer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); + } +} diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/RestServerCarrierTechnologyParametersTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/RestServerCarrierTechnologyParametersTest.java new file mode 100644 index 000000000..198005386 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/RestServerCarrierTechnologyParametersTest.java @@ -0,0 +1,86 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Samsung. 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.restserver; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import java.lang.reflect.Field; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.parameters.GroupValidationResult; + +public class RestServerCarrierTechnologyParametersTest { + + RestServerCarrierTechnologyParameters restServerCarrierTechnologyParameters = null; + GroupValidationResult result = null; + + /** + * Set up testing. + * + * @throws Exception on test set up errors. + */ + @Before + public void setUp() throws Exception { + restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters(); + } + + @Test + public void testRestServerCarrierTechnologyParameters() { + assertNotNull(restServerCarrierTechnologyParameters); + assertFalse(restServerCarrierTechnologyParameters.isStandalone()); + } + + @Test + public void testValidate() { + result = restServerCarrierTechnologyParameters.validate(); + assertNotNull(result); + assertTrue(result.isValid()); + } + + @Test + public void testValidateWithNonDefaultValues() throws NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { + + Field field = RestServerCarrierTechnologyParameters.class.getDeclaredField("standalone"); + field.setAccessible(true); + field.set(restServerCarrierTechnologyParameters, true); + field = RestServerCarrierTechnologyParameters.class.getDeclaredField("host"); + field.setAccessible(true); + field.set(restServerCarrierTechnologyParameters, ""); + field = RestServerCarrierTechnologyParameters.class.getDeclaredField("port"); + field.setAccessible(true); + field.set(restServerCarrierTechnologyParameters, 1023); + result = restServerCarrierTechnologyParameters.validate(); + assertNotNull(result); + assertFalse(result.isValid()); + + field = RestServerCarrierTechnologyParameters.class.getDeclaredField("host"); + field.setAccessible(true); + field.set(restServerCarrierTechnologyParameters, ""); + field = RestServerCarrierTechnologyParameters.class.getDeclaredField("port"); + field.setAccessible(true); + field.set(restServerCarrierTechnologyParameters, 1023); + result = restServerCarrierTechnologyParameters.validate(); + assertNotNull(result); + assertFalse(result.isValid()); + } + +} diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/SupportApexEventReceiver.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/SupportApexEventReceiver.java new file mode 100644 index 000000000..aaba4c262 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/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.restserver; + +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; + } +} -- cgit 1.2.3-korg