From 583337cbe603f3548295a1929b7bc7b01f98e405 Mon Sep 17 00:00:00 2001 From: "arkadiusz.adamski" Date: Thu, 11 Mar 2021 09:55:54 +0000 Subject: Increase code coverage - increased test coverage in the event.protocol.xml package Issue-ID: POLICY-3092 Change-Id: Ie68f8acf49d258536ddc23a14d05302273687634 Signed-off-by: arkadiusz.adamski --- .../event/protocol/xml/XmlEventHandlerTest.java | 57 +++++++++++++++++++++- .../xml/XmlEventProtocolParametersTest.java | 40 +++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlEventProtocolParametersTest.java diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlEventHandlerTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlEventHandlerTest.java index caa3bd06f..609b0471b 100644 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlEventHandlerTest.java +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlEventHandlerTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. + * Modifications Copyright (C) 2020-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. @@ -29,9 +29,13 @@ import static org.junit.Assert.assertTrue; import java.util.Date; import java.util.HashMap; import java.util.Map; +import java.util.Random; +import org.apache.commons.lang3.RandomStringUtils; import org.junit.Test; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; 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.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; @@ -43,6 +47,44 @@ import org.slf4j.ext.XLoggerFactory; public class XmlEventHandlerTest { private static final XLogger logger = XLoggerFactory.getXLogger(XmlEventHandlerTest.class); + /** + * Test XML to apex event. Null value is passed as parameter. + * + * @throws ApexException on Apex event handling errors + */ + @Test(expected = ApexException.class) + public void testApexEventToApexNullObject() throws ApexException { + final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter(); + // This is only for code coverage stats. This method does nothing + xmlEventConverter.init(null); + + xmlEventConverter.toApexEvent("XMLEventName", null); + } + + /** + * Test XML to apex event. There is no string passed as parameter. + * + * @throws ApexException on Apex event handling errors + */ + @Test(expected = ApexEventRuntimeException.class) + public void testApexEventToApexNotString() throws ApexException { + final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter(); + + xmlEventConverter.toApexEvent("XMLEventName", new Random().nextInt()); + } + + /** + * Test not valid XML to apex event. + * + * @throws ApexException on Apex event handling errors + */ + @Test(expected = ApexException.class) + public void testApexEventToApexNotXml() throws ApexException { + final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter(); + + xmlEventConverter.toApexEvent("XMLEventName", RandomStringUtils.randomAlphabetic(25)); + } + /** * Test XML to apex event. * @@ -79,6 +121,17 @@ public class XmlEventHandlerTest { } } + /** + * Test null as apex event to xml. + * + * @throws ApexEventException on Apex event handling errors + */ + @Test(expected = ApexEventException.class) + public void testApexEventToXmlNullEvent() throws ApexEventException { + final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter(); + xmlEventConverter.fromApexEvent(null); + } + /** * Test apex event to xml. * @@ -96,6 +149,7 @@ public class XmlEventHandlerTest { event0000DataMap.put("TestMatchCase", 12345); event0000DataMap.put("TestTimestamp", event0000StartTime.getTime()); event0000DataMap.put("TestTemperature", 34.5445667); + event0000DataMap.put("NullValue", null); final ApexEvent apexEvent0000 = new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.sample.events", "test", "apex"); @@ -109,6 +163,7 @@ public class XmlEventHandlerTest { assertTrue(apexEvent0000XmlString.contains("0.0.1")); assertTrue(apexEvent0000XmlString.contains("This is a test slogan")); assertTrue(apexEvent0000XmlString.contains("12345")); + assertTrue(apexEvent0000XmlString.contains("")); assertTrue(apexEvent0000XmlString.contains("" + event0000StartTime.getTime() + "")); assertTrue(apexEvent0000XmlString.contains("34.5445667")); diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlEventProtocolParametersTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlEventProtocolParametersTest.java new file mode 100644 index 000000000..27fd22095 --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlEventProtocolParametersTest.java @@ -0,0 +1,40 @@ +/*- + * ============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.xml; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class XmlEventProtocolParametersTest { + + /** + * Test object creation. + */ + @Test + public void instantiationTest() { + final XmlEventProtocolParameters parameters = new XmlEventProtocolParameters(); + + assertEquals(XmlEventProtocolParameters.XML_EVENT_PROTOCOL_LABEL, parameters.getLabel()); + assertEquals("