From 6655c53393d6dce9dfd4047996752c105b792c2c Mon Sep 17 00:00:00 2001 From: "arkadiusz.adamski" Date: Tue, 6 Apr 2021 09:56:55 +0100 Subject: Code coverage for jms protocol event plugin - Increase code coverage for jms protocol event plugin Issue-ID: POLICY-3092 Signed-off-by: arkadiusz.adamski Change-Id: I615d56736b548b3d5f552ae1becac771d445b477 --- .../plugins-event-protocol-jms/pom.xml | 15 +- .../protocol/jms/Apex2JmsObjectEventConverter.java | 11 +- .../jms/Apex2JmsObjectEventConverterTest.java | 195 +++++++++++++++++++++ .../jms/Apex2JmsTextEventConverterTest.java | 149 ++++++++++++++++ .../jms/JmsObjectEventProtocolParametersTest.java | 53 ++++++ 5 files changed, 420 insertions(+), 3 deletions(-) create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverterTest.java create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsTextEventConverterTest.java create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/JmsObjectEventProtocolParametersTest.java diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/pom.xml b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/pom.xml index d9f800e12..6e562a812 100644 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/pom.xml +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/pom.xml @@ -1,6 +1,7 @@ - + 4.0.0 + + + org.apache.activemq + activemq-client + 5.16.1 + test + + org.onap.policy.apex-pdp.plugins.plugins-event.plugins-event-protocol plugins-event-protocol @@ -27,5 +37,6 @@ plugins-event-protocol-jms ${project.artifactId} - [${project.parent.artifactId}] Plugins for handling events that are being transported as JMS messages + [${project.parent.artifactId}] Plugins for handling events that are being transported as JMS messages + \ No newline at end of file diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverter.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverter.java index 89e2e8110..924765dc5 100644 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverter.java +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverter.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2021 Nordix Foundation. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -141,4 +141,13 @@ public final class Apex2JmsObjectEventConverter implements ApexEventProtocolConv // Return the single object from the Apex event message return apexEvent.values().iterator().next(); } + + /** + * Returns eventProtocolParameters field. Getter used only for testing + * + * @return eventProtocolParameters + */ + JmsObjectEventProtocolParameters getEventProtocolParameters() { + return eventProtocolParameters; + } } diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverterTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverterTest.java new file mode 100644 index 000000000..70c17c34e --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverterTest.java @@ -0,0 +1,195 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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.plugins.event.protocol.jms; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; + + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.List; +import javax.jms.JMSException; +import javax.jms.ObjectMessage; +import org.apache.activemq.command.ActiveMQObjectMessage; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.ApexEvent; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; +import org.onap.policy.apex.service.engine.event.impl.apexprotocolplugin.ApexEventProtocolParameters; + +public class Apex2JmsObjectEventConverterTest { + private Apex2JmsObjectEventConverter converter; + private final PrintStream orgOutBuffer = System.out; + private ByteArrayOutputStream testOutStream; + + @Before + public void setUp() throws Exception { + converter = new Apex2JmsObjectEventConverter(); + testOutStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(testOutStream)); + } + + @After + public void tearDown() { + System.setOut(orgOutBuffer); + } + + @Test + public void initNull() { + assertThatThrownBy(() -> converter.init(null)) + .isInstanceOf(NullPointerException.class); + } + + @Test + public void initWrongClass() { + converter.init(new ApexEventProtocolParameters()); + final String actual = testOutStream.toString(); + assertThat(actual).contains("specified Event Protocol Parameters properties of typ"); + assertNull(converter.getEventProtocolParameters()); + } + + @Test + public void init() { + final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); + converter.init(parameters); + final JmsObjectEventProtocolParameters actual = converter.getEventProtocolParameters(); + assertSame(parameters, actual); + } + + @Test + public void toApexEventNull() { + final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); + converter.init(parameters); + final String eventName = RandomStringUtils.randomAlphabetic(4); + assertThatThrownBy(() -> converter.toApexEvent(eventName, null)) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void toApexEventObject() { + final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); + converter.init(parameters); + final String eventName = RandomStringUtils.randomAlphabetic(4); + assertThatThrownBy(() -> converter.toApexEvent(eventName, new Object())) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void toApexEventNoParams() { + final String eventName = RandomStringUtils.randomAlphabetic(4); + ObjectMessage object = new ActiveMQObjectMessage(); + assertThatThrownBy(() -> converter.toApexEvent(eventName, object)) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void toApexEventIncomingObjectIsNull() { + final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); + + converter.init(parameters); + final String eventName = RandomStringUtils.randomAlphabetic(4); + ObjectMessage object = new ActiveMQObjectMessage(); + assertThatThrownBy(() -> converter.toApexEvent(eventName, object)) + .isInstanceOf(NullPointerException.class); + } + + @Test + public void toApexEvent() throws ApexEventException, JMSException { + final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); + + converter.init(parameters); + final String eventName = RandomStringUtils.randomAlphabetic(4); + final ObjectMessage object = new ActiveMQObjectMessage(); + final String value = RandomStringUtils.randomAlphabetic(3); + object.setObject(value); + + // Prepare expected object + final ApexEvent expectedEvent = new ApexEvent("String" + parameters.getIncomingEventSuffix(), + parameters.getIncomingEventVersion(), + "java.lang", + parameters.getIncomingEventSource(), + parameters.getIncomingEventTarget()); + // Overwrite executionId to match executionId of actual + expectedEvent.setExecutionId(1); + final Object[] expected = {expectedEvent}; + + // Run tested method + final List actual = converter.toApexEvent(eventName, object); + // Overwrite executionId to match executionId of expected + actual.get(0).setExecutionId(1); + assertArrayEquals(expected, actual.toArray()); + } + + @Test + public void fromApexEventNull() { + assertThatThrownBy(() -> converter.fromApexEvent(null)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void fromApexEventEmptyEvent() throws ApexEventException { + final ApexEvent apexEvent = new ApexEvent( + "a" + RandomStringUtils.randomAlphabetic(3), + "a" + RandomStringUtils.randomAlphabetic(3), + "a" + RandomStringUtils.randomAlphabetic(3), + "", + ""); + assertThatThrownBy(() -> converter.fromApexEvent(apexEvent)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void fromApexEventMultipleEvents() throws ApexEventException { + final ApexEvent apexEvent = new ApexEvent( + "a" + RandomStringUtils.randomAlphabetic(3), + "a" + RandomStringUtils.randomAlphabetic(4), + "a" + RandomStringUtils.randomAlphabetic(5), + "", + ""); + apexEvent.put(RandomStringUtils.randomAlphabetic(2), new Object()); + apexEvent.put(RandomStringUtils.randomAlphabetic(6), new Object()); + assertThatThrownBy(() -> converter.fromApexEvent(apexEvent)).isInstanceOf(ApexEventException.class); + } + + @Test + public void fromApexEventSingleEvent() throws ApexEventException { + final ApexEvent apexEvent = new ApexEvent( + "a" + RandomStringUtils.randomAlphabetic(3), + "a" + RandomStringUtils.randomAlphabetic(3), + "a" + RandomStringUtils.randomAlphabetic(3), + "", + ""); + + final Object expected = new Object(); + apexEvent.put(RandomStringUtils.randomAlphabetic(2), expected); + + final Object actual = converter.fromApexEvent(apexEvent); + + assertSame(expected, actual); + } +} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsTextEventConverterTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsTextEventConverterTest.java new file mode 100644 index 000000000..9f68b49df --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsTextEventConverterTest.java @@ -0,0 +1,149 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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.plugins.event.protocol.jms; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + + +import java.util.List; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvents; +import org.onap.policy.apex.service.engine.event.ApexEvent; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock; +import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.JsonEventProtocolParameters; +import org.onap.policy.common.parameters.ParameterService; + +public class Apex2JmsTextEventConverterTest { + private Apex2JmsTextEventConverter converter; + + @Before + public void setUp() { + converter = new Apex2JmsTextEventConverter(); + ModelService.registerModel(AxContextSchemas.class, new AxContextSchemas()); + ModelService.registerModel(AxEvents.class, new AxEvents()); + ParameterService.register(new SchemaParameters()); + } + + @After + public void tearDown() { + ModelService.deregisterModel(AxContextSchema.class); + ModelService.deregisterModel(AxEvents.class); + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + } + + @Test + public void toApexEventNull() { + final String eventName = RandomStringUtils.randomAlphabetic(4); + assertThatThrownBy(() -> converter.toApexEvent(eventName, null)) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void toApexEventObject() { + final String eventName = RandomStringUtils.randomAlphabetic(4); + assertThatThrownBy(() -> converter.toApexEvent(eventName, new Object())) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void toApexEventJsonString() throws ApexEventException { + final String eventName = RandomStringUtils.randomAlphabetic(4); + final String eventVersion = "0.0.1"; + final String source = RandomStringUtils.randomAlphabetic(5); + final String target = RandomStringUtils.randomAlphabetic(6); + final String nameSpace = "a.name.space"; + + // Prepare Json String to be translated into ApexEvent + final TextBlock object = + new TextBlock(false, "{\"name\": \"" + eventName + "\", \"version\":\"" + eventVersion + "\"}"); + + // Prepare Model service + final AxArtifactKey eventKey = new AxArtifactKey(eventName + ":" + eventVersion); + final AxEvent axEvent = new AxEvent(eventKey, nameSpace, source, target); + ModelService.getModel(AxEvents.class).getEventMap().put(eventKey, axEvent); + + // prepare converter + converter.init(new JsonEventProtocolParameters()); + + // execute test + final List apexEvents = converter.toApexEvent(eventName, object); + + final ApexEvent expectedEvent = new ApexEvent(eventName, eventVersion, nameSpace, source, target); + + // Reset executionId + expectedEvent.setExecutionId(0); + for (ApexEvent event : apexEvents) { + event.setExecutionId(0); + } + Object[] expected = {expectedEvent}; + + assertArrayEquals(expected, apexEvents.toArray()); + } + + @Test + public void fromApexNull() { + assertThatThrownBy(() -> converter.fromApexEvent(null)).isInstanceOf(ApexEventException.class); + } + + @Test + public void fromApex() throws ApexEventException { + final String name = RandomStringUtils.randomAlphabetic(4); + final String version = "0.2.3"; + final String nameSpace = "a.name.space"; + final String source = RandomStringUtils.randomAlphabetic(6); + final String target = RandomStringUtils.randomAlphabetic(7); + + final String expected = "{\n" + + " \"name\": \"" + name + "\",\n" + + " \"version\": \"" + version + "\",\n" + + " \"nameSpace\": \"" + nameSpace + "\",\n" + + " \"source\": \"" + source + "\",\n" + + " \"target\": \"" + target + "\"\n" + + "}"; + + // Prepare Model service + final AxArtifactKey eventKey = new AxArtifactKey(name + ":" + version); + final AxEvent axEvent = new AxEvent(eventKey, nameSpace, source, target); + ModelService.getModel(AxEvents.class).getEventMap().put(eventKey, axEvent); + + converter.init(new JsonEventProtocolParameters()); + + final ApexEvent apexEvent = new ApexEvent(name, version, nameSpace, source, target); + final Object actual = converter.fromApexEvent(apexEvent); + + assertEquals(expected, actual); + } + +} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/JmsObjectEventProtocolParametersTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/JmsObjectEventProtocolParametersTest.java new file mode 100644 index 000000000..8baa9d5b1 --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/JmsObjectEventProtocolParametersTest.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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.plugins.event.protocol.jms; + +import static org.junit.Assert.assertEquals; + + +import org.junit.Test; + +public class JmsObjectEventProtocolParametersTest { + + @Test + public void getIncomingEventVersion() { + final JmsObjectEventProtocolParameters jmsObjectEventProtocolParameters = + new JmsObjectEventProtocolParameters(); + final String actual = jmsObjectEventProtocolParameters.getIncomingEventVersion(); + assertEquals("1.0.0", actual); + } + + @Test + public void getIncomingEventSource() { + final JmsObjectEventProtocolParameters jmsObjectEventProtocolParameters = + new JmsObjectEventProtocolParameters(); + final String actual = jmsObjectEventProtocolParameters.getIncomingEventSource(); + assertEquals("JMS", actual); + } + + @Test + public void getIncomingEventTarget() { + final JmsObjectEventProtocolParameters jmsObjectEventProtocolParameters = + new JmsObjectEventProtocolParameters(); + final String actual = jmsObjectEventProtocolParameters.getIncomingEventTarget(); + assertEquals("Apex", actual); + } +} -- cgit 1.2.3-korg