From e8b197315437cac84872752e2ea090d8fb233941 Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Tue, 20 Nov 2018 14:46:45 +0900 Subject: Rename test classes in apex-pdp Make test classes name consistence under auth, model, plugins, testsuits, tools projects Issue-ID: POLICY-1263 Change-Id: I49ec9a9f5b457d6381e693de2c04ec0268ad1b02 Signed-off-by: Parshad Patel --- .../event/protocol/xml/TestXmlEventHandler.java | 147 ------- .../protocol/xml/TestXmlTaggedEventConsumer.java | 446 --------------------- .../event/protocol/xml/XmlEventHandlerTest.java | 147 +++++++ .../protocol/xml/XmlTaggedEventConsumerTest.java | 446 +++++++++++++++++++++ .../event/protocol/yaml/TestYamlEventProtocol.java | 287 ------------- .../protocol/yaml/TestYamlPluginStability.java | 278 ------------- .../event/protocol/yaml/YamlEventProtocolTest.java | 287 +++++++++++++ .../protocol/yaml/YamlPluginStabilityTest.java | 278 +++++++++++++ 8 files changed, 1158 insertions(+), 1158 deletions(-) delete mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/TestXmlEventHandler.java delete mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/TestXmlTaggedEventConsumer.java create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlEventHandlerTest.java create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlTaggedEventConsumerTest.java delete mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlEventProtocol.java delete mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlPluginStability.java create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolTest.java create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlPluginStabilityTest.java (limited to 'plugins/plugins-event/plugins-event-protocol') diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/TestXmlEventHandler.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/TestXmlEventHandler.java deleted file mode 100644 index 1e42bef30..000000000 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/TestXmlEventHandler.java +++ /dev/null @@ -1,147 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.protocol.xml; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.ApexException; -import org.onap.policy.apex.service.engine.event.ApexEvent; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; - -/** - * The Class TestApexXMLEventHandlerURL. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestXmlEventHandler { - private static final XLogger logger = XLoggerFactory.getXLogger(TestXmlEventHandler.class); - - /** - * Test XML to apex event. - * - * @throws ApexException on Apex event handling errors - */ - @Test - public void testXmltoApexEvent() throws ApexException { - try { - final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter(); - assertNotNull(xmlEventConverter); - - final String apexEventXmlStringIn = XmlEventGenerator.xmlEvent(); - - logger.debug("input event\n" + apexEventXmlStringIn); - - for (final ApexEvent apexEvent : xmlEventConverter.toApexEvent("XMLEventName", apexEventXmlStringIn)) { - assertNotNull(apexEvent); - - logger.debug(apexEvent.toString()); - - assertTrue(apexEvent.getName().equals("Event0000") || apexEvent.getName().equals("Event0100")); - assertTrue(apexEvent.getVersion().equals("0.0.1")); - assertTrue(apexEvent.getNameSpace().equals("org.onap.policy.apex.sample.events")); - assertTrue(apexEvent.getSource().equals("test")); - assertTrue(apexEvent.getTarget().equals("apex")); - assertTrue(apexEvent.get("TestSlogan").toString().startsWith("Test slogan for External Event")); - - final Object testMatchCaseSelected = apexEvent.get("TestMatchCaseSelected"); - assertTrue(testMatchCaseSelected == null); - } - } catch (final Exception e) { - e.printStackTrace(); - throw new ApexException("Exception reading Apex event xml file", e); - } - } - - /** - * Test apex event to xml. - * - * @throws ApexException on Apex event handling errors - */ - @Test - public void testApexEventToXml() throws ApexException { - try { - final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter(); - assertNotNull(xmlEventConverter); - - final Date event0000StartTime = new Date(); - final Map event0000DataMap = new HashMap(); - event0000DataMap.put("TestSlogan", "This is a test slogan"); - event0000DataMap.put("TestMatchCase", 12345); - event0000DataMap.put("TestTimestamp", event0000StartTime.getTime()); - event0000DataMap.put("TestTemperature", 34.5445667); - - final ApexEvent apexEvent0000 = - new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.sample.events", "test", "apex"); - apexEvent0000.putAll(event0000DataMap); - - final String apexEvent0000XmlString = xmlEventConverter.fromApexEvent(apexEvent0000); - - logger.debug(apexEvent0000XmlString); - - assertTrue(apexEvent0000XmlString.contains("Event0000")); - assertTrue(apexEvent0000XmlString.contains("0.0.1")); - assertTrue(apexEvent0000XmlString.contains("This is a test slogan")); - assertTrue(apexEvent0000XmlString.contains("12345")); - assertTrue(apexEvent0000XmlString.contains("" + event0000StartTime.getTime() + "")); - assertTrue(apexEvent0000XmlString.contains("34.5445667")); - - final Date event0004StartTime = new Date(1434363272000L); - final Map event0004DataMap = new HashMap(); - event0004DataMap.put("TestSlogan", "Test slogan for External Event"); - event0004DataMap.put("TestMatchCase", new Integer(2)); - event0004DataMap.put("TestTimestamp", new Long(event0004StartTime.getTime())); - event0004DataMap.put("TestTemperature", new Double(1064.43)); - event0004DataMap.put("TestMatchCaseSelected", new Integer(2)); - event0004DataMap.put("TestMatchStateTime", new Long(1434370506078L)); - event0004DataMap.put("TestEstablishCaseSelected", new Integer(0)); - event0004DataMap.put("TestEstablishStateTime", new Long(1434370506085L)); - event0004DataMap.put("TestDecideCaseSelected", new Integer(3)); - event0004DataMap.put("TestDecideStateTime", new Long(1434370506092L)); - event0004DataMap.put("TestActCaseSelected", new Integer(2)); - event0004DataMap.put("TestActStateTime", new Long(1434370506095L)); - - final ApexEvent apexEvent0004 = - new ApexEvent("Event0004", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex"); - apexEvent0004.putAll(event0004DataMap); - - final String apexEvent0004XmlString = xmlEventConverter.fromApexEvent(apexEvent0004); - - logger.debug(apexEvent0004XmlString); - - assertTrue(apexEvent0004XmlString.contains("Event0004")); - assertTrue(apexEvent0004XmlString.contains("0.0.1")); - assertTrue(apexEvent0004XmlString.contains("Test slogan for External Event")); - assertTrue(apexEvent0004XmlString.contains("1434370506078")); - assertTrue(apexEvent0004XmlString.contains("" + event0004StartTime.getTime() + "")); - assertTrue(apexEvent0004XmlString.contains("1064.43")); - } catch (final Exception e) { - e.printStackTrace(); - throw new ApexException("Exception reading Apex event xml file", e); - } - } -} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/TestXmlTaggedEventConsumer.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/TestXmlTaggedEventConsumer.java deleted file mode 100644 index 475373a7c..000000000 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/TestXmlTaggedEventConsumer.java +++ /dev/null @@ -1,446 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.protocol.xml; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; - -import org.junit.Test; -import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.HeaderDelimitedTextBlockReader; -import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock; - -/** - * The Class TestXmlTaggedEventConsumer. - */ -public class TestXmlTaggedEventConsumer { - - /** - * Test garbage text line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testGarbageTextLine() throws IOException { - final InputStream xmlInputStream = new ByteArrayInputStream("hello there".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268", textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test full event garbage before line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testFullEventGarbageBeforeLine() throws IOException { - final InputStream xmlInputStream = new ByteArrayInputStream( - "Garbage1469781869268".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268Rubbish" - .getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268Rubbish".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268Rubbish", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test garbage text multi line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testGarbageTextMultiLine() throws IOException { - final InputStream xmlInputStream = new ByteArrayInputStream("hello\nthere".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\n\n".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test full event garbage before multi line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testFullEventGarbageBeforeMultiLine() throws IOException { - final InputStream xmlInputStream = new ByteArrayInputStream( - "Garbage\n\n\n1469781869268\n\n\n" - .getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test full event garbage before after multi line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testFullEventGarbageBeforeAfterMultiLine() throws IOException { - String garbageString = "Garbage\n\n\n1469781869268" - + "\n\nRubbish\n\n"; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\nRubbish", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test full event garbage after multi line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testFullEventGarbageAfterMultiLine() throws IOException { - final InputStream xmlInputStream = new ByteArrayInputStream( - "\n\n1469781869268\n\nRubbish" - .getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\nRubbish", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test partial events line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testPartialEventsLine() throws IOException { - String garbageString = "1469781869268" - + "1469781869268"; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268" - + ""; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268" - + "Rubbish\nRefuse"; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268" - + "RubbishRefuse"; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n\n\n".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n", textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test full events multi line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testFullEventsMultiLine() throws IOException { - String garbageString = "\n\n1469781869268\n" - + "\n\n\n1469781869268\n\n"; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n", - textBlock.getText()); - assertFalse(textBlock.isEndOfText()); - - textBlock = xmlTaggedReader.readTextBlock(); - assertEquals("\n\n1469781869268\n", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test full events garbage before multi line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testFullEventsGarbageBeforeMultiLine() throws IOException { - String garbageString = "Garbage\n\n\n1469781869268\n" - + "\n\n\n\n1469781869268\n\n"; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n", - textBlock.getText()); - assertFalse(textBlock.isEndOfText()); - - textBlock = xmlTaggedReader.readTextBlock(); - assertEquals("\n\n1469781869268\n", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test full events garbage before after multi line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testFullEventsGarbageBeforeAfterMultiLine() throws IOException { - String garbageString = "Garbage\n\n\n1469781869268\n" - + "\nRubbish\n\n\n1469781869268\n" - + "\nRefuse\n"; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\nRubbish", - textBlock.getText()); - assertFalse(textBlock.isEndOfText()); - - textBlock = xmlTaggedReader.readTextBlock(); - assertEquals("\n\n1469781869268\n\nRefuse", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test full events garbage after multi line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testFullEventsGarbageAfterMultiLine() throws IOException { - final InputStream xmlInputStream = new ByteArrayInputStream( - "\n\n1469781869268\n\nRubbish" - .getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\nRubbish", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } -} 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 new file mode 100644 index 000000000..685a1f236 --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlEventHandlerTest.java @@ -0,0 +1,147 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.protocol.xml; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.service.engine.event.ApexEvent; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class TestApexXMLEventHandlerURL. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class XmlEventHandlerTest { + private static final XLogger logger = XLoggerFactory.getXLogger(XmlEventHandlerTest.class); + + /** + * Test XML to apex event. + * + * @throws ApexException on Apex event handling errors + */ + @Test + public void testXmltoApexEvent() throws ApexException { + try { + final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter(); + assertNotNull(xmlEventConverter); + + final String apexEventXmlStringIn = XmlEventGenerator.xmlEvent(); + + logger.debug("input event\n" + apexEventXmlStringIn); + + for (final ApexEvent apexEvent : xmlEventConverter.toApexEvent("XMLEventName", apexEventXmlStringIn)) { + assertNotNull(apexEvent); + + logger.debug(apexEvent.toString()); + + assertTrue(apexEvent.getName().equals("Event0000") || apexEvent.getName().equals("Event0100")); + assertTrue(apexEvent.getVersion().equals("0.0.1")); + assertTrue(apexEvent.getNameSpace().equals("org.onap.policy.apex.sample.events")); + assertTrue(apexEvent.getSource().equals("test")); + assertTrue(apexEvent.getTarget().equals("apex")); + assertTrue(apexEvent.get("TestSlogan").toString().startsWith("Test slogan for External Event")); + + final Object testMatchCaseSelected = apexEvent.get("TestMatchCaseSelected"); + assertTrue(testMatchCaseSelected == null); + } + } catch (final Exception e) { + e.printStackTrace(); + throw new ApexException("Exception reading Apex event xml file", e); + } + } + + /** + * Test apex event to xml. + * + * @throws ApexException on Apex event handling errors + */ + @Test + public void testApexEventToXml() throws ApexException { + try { + final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter(); + assertNotNull(xmlEventConverter); + + final Date event0000StartTime = new Date(); + final Map event0000DataMap = new HashMap(); + event0000DataMap.put("TestSlogan", "This is a test slogan"); + event0000DataMap.put("TestMatchCase", 12345); + event0000DataMap.put("TestTimestamp", event0000StartTime.getTime()); + event0000DataMap.put("TestTemperature", 34.5445667); + + final ApexEvent apexEvent0000 = + new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.sample.events", "test", "apex"); + apexEvent0000.putAll(event0000DataMap); + + final String apexEvent0000XmlString = xmlEventConverter.fromApexEvent(apexEvent0000); + + logger.debug(apexEvent0000XmlString); + + assertTrue(apexEvent0000XmlString.contains("Event0000")); + assertTrue(apexEvent0000XmlString.contains("0.0.1")); + assertTrue(apexEvent0000XmlString.contains("This is a test slogan")); + assertTrue(apexEvent0000XmlString.contains("12345")); + assertTrue(apexEvent0000XmlString.contains("" + event0000StartTime.getTime() + "")); + assertTrue(apexEvent0000XmlString.contains("34.5445667")); + + final Date event0004StartTime = new Date(1434363272000L); + final Map event0004DataMap = new HashMap(); + event0004DataMap.put("TestSlogan", "Test slogan for External Event"); + event0004DataMap.put("TestMatchCase", new Integer(2)); + event0004DataMap.put("TestTimestamp", new Long(event0004StartTime.getTime())); + event0004DataMap.put("TestTemperature", new Double(1064.43)); + event0004DataMap.put("TestMatchCaseSelected", new Integer(2)); + event0004DataMap.put("TestMatchStateTime", new Long(1434370506078L)); + event0004DataMap.put("TestEstablishCaseSelected", new Integer(0)); + event0004DataMap.put("TestEstablishStateTime", new Long(1434370506085L)); + event0004DataMap.put("TestDecideCaseSelected", new Integer(3)); + event0004DataMap.put("TestDecideStateTime", new Long(1434370506092L)); + event0004DataMap.put("TestActCaseSelected", new Integer(2)); + event0004DataMap.put("TestActStateTime", new Long(1434370506095L)); + + final ApexEvent apexEvent0004 = + new ApexEvent("Event0004", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex"); + apexEvent0004.putAll(event0004DataMap); + + final String apexEvent0004XmlString = xmlEventConverter.fromApexEvent(apexEvent0004); + + logger.debug(apexEvent0004XmlString); + + assertTrue(apexEvent0004XmlString.contains("Event0004")); + assertTrue(apexEvent0004XmlString.contains("0.0.1")); + assertTrue(apexEvent0004XmlString.contains("Test slogan for External Event")); + assertTrue(apexEvent0004XmlString.contains("1434370506078")); + assertTrue(apexEvent0004XmlString.contains("" + event0004StartTime.getTime() + "")); + assertTrue(apexEvent0004XmlString.contains("1064.43")); + } catch (final Exception e) { + e.printStackTrace(); + throw new ApexException("Exception reading Apex event xml file", e); + } + } +} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlTaggedEventConsumerTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlTaggedEventConsumerTest.java new file mode 100644 index 000000000..aeb37331a --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlTaggedEventConsumerTest.java @@ -0,0 +1,446 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.protocol.xml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.HeaderDelimitedTextBlockReader; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock; + +/** + * The Class TestXmlTaggedEventConsumer. + */ +public class XmlTaggedEventConsumerTest { + + /** + * Test garbage text line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testGarbageTextLine() throws IOException { + final InputStream xmlInputStream = new ByteArrayInputStream("hello there".getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("".getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268".getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268", textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test full event garbage before line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testFullEventGarbageBeforeLine() throws IOException { + final InputStream xmlInputStream = new ByteArrayInputStream( + "Garbage1469781869268".getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268Rubbish" + .getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268Rubbish".getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268Rubbish", + textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test garbage text multi line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testGarbageTextMultiLine() throws IOException { + final InputStream xmlInputStream = new ByteArrayInputStream("hello\nthere".getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n".getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\n\n".getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n", + textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test full event garbage before multi line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testFullEventGarbageBeforeMultiLine() throws IOException { + final InputStream xmlInputStream = new ByteArrayInputStream( + "Garbage\n\n\n1469781869268\n\n\n" + .getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n", + textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test full event garbage before after multi line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testFullEventGarbageBeforeAfterMultiLine() throws IOException { + String garbageString = "Garbage\n\n\n1469781869268" + + "\n\nRubbish\n\n"; + final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\nRubbish", + textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test full event garbage after multi line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testFullEventGarbageAfterMultiLine() throws IOException { + final InputStream xmlInputStream = new ByteArrayInputStream( + "\n\n1469781869268\n\nRubbish" + .getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\nRubbish", + textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test partial events line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testPartialEventsLine() throws IOException { + String garbageString = "1469781869268" + + "1469781869268"; + final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268" + + ""; + final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268" + + "Rubbish\nRefuse"; + final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268" + + "RubbishRefuse"; + final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n\n\n".getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n", textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test full events multi line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testFullEventsMultiLine() throws IOException { + String garbageString = "\n\n1469781869268\n" + + "\n\n\n1469781869268\n\n"; + final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n", + textBlock.getText()); + assertFalse(textBlock.isEndOfText()); + + textBlock = xmlTaggedReader.readTextBlock(); + assertEquals("\n\n1469781869268\n", + textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test full events garbage before multi line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testFullEventsGarbageBeforeMultiLine() throws IOException { + String garbageString = "Garbage\n\n\n1469781869268\n" + + "\n\n\n\n1469781869268\n\n"; + final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n", + textBlock.getText()); + assertFalse(textBlock.isEndOfText()); + + textBlock = xmlTaggedReader.readTextBlock(); + assertEquals("\n\n1469781869268\n", + textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test full events garbage before after multi line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testFullEventsGarbageBeforeAfterMultiLine() throws IOException { + String garbageString = "Garbage\n\n\n1469781869268\n" + + "\nRubbish\n\n\n1469781869268\n" + + "\nRefuse\n"; + final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\nRubbish", + textBlock.getText()); + assertFalse(textBlock.isEndOfText()); + + textBlock = xmlTaggedReader.readTextBlock(); + assertEquals("\n\n1469781869268\n\nRefuse", + textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test full events garbage after multi line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testFullEventsGarbageAfterMultiLine() throws IOException { + final InputStream xmlInputStream = new ByteArrayInputStream( + "\n\n1469781869268\n\nRubbish" + .getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\nRubbish", + textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } +} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlEventProtocol.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlEventProtocol.java deleted file mode 100644 index 1d2a1d9f7..000000000 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlEventProtocol.java +++ /dev/null @@ -1,287 +0,0 @@ -/*- - * ============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.protocol.yaml; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; -import org.onap.policy.apex.context.parameters.SchemaParameters; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; -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.model.eventmodel.concepts.AxField; -import org.onap.policy.apex.model.utilities.TextFileUtils; -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.impl.filecarrierplugin.consumer.HeaderDelimitedTextBlockReader; -import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock; -import org.onap.policy.common.parameters.ParameterService; - -/** - * The Class TestYamlEventProtocol. - */ -public class TestYamlEventProtocol { - - /** - * Register test events and schemas. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @BeforeClass - public static void registerTestEventsAndSchemas() throws IOException { - SchemaParameters schemaParameters = new SchemaParameters(); - schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters()); - ParameterService.register(schemaParameters, true); - - AxContextSchemas schemas = new AxContextSchemas(); - - AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA", - "java.lang.Integer"); - schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema); - - AxContextSchema simpleDoubleSchema = new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"), - "JAVA", "java.lang.Double"); - schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema); - - AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), - "JAVA", "java.lang.String"); - schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema); - - AxContextSchema arrayListSchema = new AxContextSchema(new AxArtifactKey("ArrayListSchema", "0.0.1"), "JAVA", - "java.util.ArrayList"); - schemas.getSchemasMap().put(arrayListSchema.getKey(), arrayListSchema); - - AxContextSchema linkedHashMapSchema = new AxContextSchema(new AxArtifactKey("LinkedHashMapSchema", "0.0.1"), - "JAVA", "java.util.LinkedHashMap"); - schemas.getSchemasMap().put(linkedHashMapSchema.getKey(), linkedHashMapSchema); - - ModelService.registerModel(AxContextSchemas.class, schemas); - - AxEvents events = new AxEvents(); - - AxEvent testEvent0 = new AxEvent(new AxArtifactKey("TestEvent0", "0.0.1")); - testEvent0.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - events.getEventMap().put(testEvent0.getKey(), testEvent0); - - AxEvent testEvent1 = new AxEvent(new AxArtifactKey("TestEvent1", "0.0.1")); - testEvent1.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te1Field0 = new AxField(new AxReferenceKey(testEvent1.getKey(), "yaml_field"), - arrayListSchema.getKey()); - testEvent1.getParameterMap().put("yaml_field", te1Field0); - events.getEventMap().put(testEvent1.getKey(), testEvent1); - - AxEvent testEvent2 = new AxEvent(new AxArtifactKey("TestEvent2", "0.0.1")); - testEvent2.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te2Field0 = new AxField(new AxReferenceKey(testEvent2.getKey(), "hr"), simpleIntSchema.getKey()); - testEvent2.getParameterMap().put("hr", te2Field0); - AxField te2Field1 = new AxField(new AxReferenceKey(testEvent2.getKey(), "avg"), simpleDoubleSchema.getKey()); - testEvent2.getParameterMap().put("avg", te2Field1); - AxField te2Field2 = new AxField(new AxReferenceKey(testEvent2.getKey(), "rbi"), simpleIntSchema.getKey()); - testEvent2.getParameterMap().put("rbi", te2Field2); - events.getEventMap().put(testEvent2.getKey(), testEvent2); - - AxEvent testEvent3 = new AxEvent(new AxArtifactKey("TestEvent3", "0.0.1")); - testEvent3.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te3Field0 = new AxField(new AxReferenceKey(testEvent3.getKey(), "american"), arrayListSchema.getKey()); - testEvent3.getParameterMap().put("american", te3Field0); - AxField te3Field1 = new AxField(new AxReferenceKey(testEvent3.getKey(), "national"), arrayListSchema.getKey()); - testEvent3.getParameterMap().put("national", te3Field1); - events.getEventMap().put(testEvent3.getKey(), testEvent3); - - AxEvent testEvent4 = new AxEvent(new AxArtifactKey("TestEvent4", "0.0.1")); - testEvent4.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te4Field0 = new AxField(new AxReferenceKey(testEvent4.getKey(), "yaml_field"), - arrayListSchema.getKey()); - testEvent4.getParameterMap().put("yaml_field", te4Field0); - events.getEventMap().put(testEvent4.getKey(), testEvent4); - - AxEvent testEvent5 = new AxEvent(new AxArtifactKey("TestEvent5", "0.0.1")); - testEvent5.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te5Field0 = new AxField(new AxReferenceKey(testEvent5.getKey(), "yaml_field"), - arrayListSchema.getKey()); - testEvent5.getParameterMap().put("yaml_field", te5Field0); - events.getEventMap().put(testEvent5.getKey(), testEvent5); - - AxEvent testEvent6 = new AxEvent(new AxArtifactKey("TestEvent6", "0.0.1")); - testEvent6.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te6Field0 = new AxField(new AxReferenceKey(testEvent6.getKey(), "MarkMcGwire"), - linkedHashMapSchema.getKey()); - testEvent6.getParameterMap().put("Mark McGwire", te6Field0); - AxField te6Field1 = new AxField(new AxReferenceKey(testEvent6.getKey(), "SammySosa"), - linkedHashMapSchema.getKey()); - testEvent6.getParameterMap().put("Sammy Sosa", te6Field1); - events.getEventMap().put(testEvent6.getKey(), testEvent6); - - AxEvent testEvent7 = new AxEvent(new AxArtifactKey("TestEvent7", "0.0.1")); - testEvent7.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te7Field0 = new AxField(new AxReferenceKey(testEvent7.getKey(), "time"), simpleIntSchema.getKey()); - testEvent7.getParameterMap().put("time", te7Field0); - AxField te7Field1 = new AxField(new AxReferenceKey(testEvent7.getKey(), "player"), simpleStringSchema.getKey()); - testEvent7.getParameterMap().put("player", te7Field1); - AxField te7Field2 = new AxField(new AxReferenceKey(testEvent7.getKey(), "action"), simpleStringSchema.getKey()); - testEvent7.getParameterMap().put("action", te7Field2); - events.getEventMap().put(testEvent7.getKey(), testEvent7); - - AxEvent testEvent8 = new AxEvent(new AxArtifactKey("TestEvent8", "0.0.1")); - testEvent8.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te8Field0 = new AxField(new AxReferenceKey(testEvent8.getKey(), "hr"), arrayListSchema.getKey()); - testEvent8.getParameterMap().put("hr", te8Field0); - AxField te8Field1 = new AxField(new AxReferenceKey(testEvent8.getKey(), "rbi"), arrayListSchema.getKey()); - testEvent8.getParameterMap().put("rbi", te8Field1); - events.getEventMap().put(testEvent8.getKey(), testEvent8); - - AxEvent testEvent9 = new AxEvent(new AxArtifactKey("TestEvent9", "0.0.1")); - testEvent9.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te9Field0 = new AxField(new AxReferenceKey(testEvent9.getKey(), "ChicagoCubs"), - arrayListSchema.getKey()); - testEvent9.getParameterMap().put("ChicagoCubs", te9Field0); - AxField te9Field1 = new AxField(new AxReferenceKey(testEvent9.getKey(), "AtlantaBraves"), - arrayListSchema.getKey()); - testEvent9.getParameterMap().put("AtlantaBraves", te9Field1); - events.getEventMap().put(testEvent9.getKey(), testEvent9); - - AxEvent testEvent10 = new AxEvent(new AxArtifactKey("TestEvent10", "0.0.1")); - testEvent10.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te10Field0 = new AxField(new AxReferenceKey(testEvent10.getKey(), "yaml_field"), - arrayListSchema.getKey()); - testEvent10.getParameterMap().put("yaml_field", te10Field0); - events.getEventMap().put(testEvent10.getKey(), testEvent10); - - AxEvent testEvent11 = new AxEvent(new AxArtifactKey("TestEvent11", "0.0.1")); - testEvent11.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te11Field0 = new AxField(new AxReferenceKey(testEvent11.getKey(), "tosca_definitions_version"), - simpleStringSchema.getKey()); - testEvent11.getParameterMap().put("tosca_definitions_version", te11Field0); - AxField te11Field1 = new AxField(new AxReferenceKey(testEvent11.getKey(), "description"), - simpleStringSchema.getKey(), true); - testEvent11.getParameterMap().put("description", te11Field1); - AxField te11Field2 = new AxField(new AxReferenceKey(testEvent11.getKey(), "node_types"), - linkedHashMapSchema.getKey(), true); - testEvent11.getParameterMap().put("node_types", te11Field2); - AxField te11Field3 = new AxField(new AxReferenceKey(testEvent11.getKey(), "topology_template"), - linkedHashMapSchema.getKey()); - testEvent11.getParameterMap().put("topology_template", te11Field3); - events.getEventMap().put(testEvent11.getKey(), testEvent11); - - ModelService.registerModel(AxEvents.class, events); - } - - /** - * Unregister test events and schemas. - */ - @AfterClass - public static void unregisterTestEventsAndSchemas() { - ModelService.clear(); - ParameterService.clear(); - } - - /** - * Test yaml processing. - * - * @throws ApexEventException the apex event exception - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testYamlProcessing() throws ApexEventException, IOException { - try { - testYamlDecodeEncode("TestEvent0", 1, 0, "Empty0"); - fail("test should fail here"); - } catch (ApexEventException e) { - assertEquals("event processing failed, event is null", e.getMessage()); - } - - testYamlDecodeEncode("TestEvent0", 1, 0, "Empty1"); - testYamlDecodeEncode("TestEvent1", 1, 1, "Collection0"); - testYamlDecodeEncode("TestEvent2", 1, 3, "Collection1"); - testYamlDecodeEncode("TestEvent3", 1, 2, "Collection2"); - testYamlDecodeEncode("TestEvent4", 1, 1, "Collection3"); - testYamlDecodeEncode("TestEvent5", 1, 1, "Collection4"); - testYamlDecodeEncode("TestEvent6", 1, 2, "Collection5"); - testYamlDecodeEncode("TestEvent1", 2, 1, "Structure0"); - testYamlDecodeEncode("TestEvent7", 2, 3, "Structure1"); - testYamlDecodeEncode("TestEvent8", 1, 2, "Structure2"); - testYamlDecodeEncode("TestEvent8", 1, 2, "Structure3"); - testYamlDecodeEncode("TestEvent9", 1, 2, "Structure4"); - testYamlDecodeEncode("TestEvent10", 1, 1, "Structure5"); - testYamlDecodeEncode("TestEvent11", 1, 4, "TOSCA0"); - } - - /** - * Test yaml decode encode. - * - * @param eventName the event name - * @param eventCount the event count - * @param parCount the par count - * @param fileName the file name - * @throws ApexEventException the apex event exception - * @throws IOException Signals that an I/O exception has occurred. - */ - private void testYamlDecodeEncode(final String eventName, final int eventCount, final int parCount, - final String fileName) throws ApexEventException, IOException { - YamlEventProtocolParameters parameters = new YamlEventProtocolParameters(); - parameters.setDelimiterAtStart(false); - - Apex2YamlEventConverter converter = new Apex2YamlEventConverter(); - converter.init(parameters); - - String filePath = "src/test/resources/yaml_in/" + fileName + ".yaml"; - FileInputStream fileInputStream = new FileInputStream(new File(filePath)); - HeaderDelimitedTextBlockReader reader = new HeaderDelimitedTextBlockReader(parameters); - reader.init(fileInputStream); - - List eventList = new ArrayList<>(); - - TextBlock textBlock; - do { - textBlock = reader.readTextBlock(); - - eventList.addAll(converter.toApexEvent(eventName, textBlock.getText())); - } - while (!textBlock.isEndOfText()); - - fileInputStream.close(); - - assertEquals(eventCount, eventList.size()); - - for (int eventNo = 0; eventNo < eventCount; eventNo++) { - assertEquals(parCount, eventList.get(0).size()); - - String eventYaml = (String) converter.fromApexEvent(eventList.get(eventNo)); - String expectedYaml = TextFileUtils - .getTextFileAsString("src/test/resources/yaml_out/" + fileName + '_' + eventNo + ".yaml"); - assertEquals(expectedYaml.replaceAll("\\s*", ""), eventYaml.replaceAll("\\s*", "")); - } - } -} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlPluginStability.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlPluginStability.java deleted file mode 100644 index d033de56d..000000000 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlPluginStability.java +++ /dev/null @@ -1,278 +0,0 @@ -/*- - * ============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.protocol.yaml; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.io.IOException; -import java.util.List; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; -import org.onap.policy.apex.context.parameters.SchemaParameters; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; -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.model.eventmodel.concepts.AxField; -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.common.parameters.ParameterService; - -/** - * The Class TestYamlPluginStability. - */ -public class TestYamlPluginStability { - static AxEvent testEvent; - - /** - * Register test events and schemas. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @BeforeClass - public static void registerTestEventsAndSchemas() throws IOException { - SchemaParameters schemaParameters = new SchemaParameters(); - schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters()); - ParameterService.register(schemaParameters); - - AxContextSchemas schemas = new AxContextSchemas(); - - AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA", - "java.lang.Integer"); - schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema); - - AxContextSchema simpleDoubleSchema = new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"), - "JAVA", "java.lang.Double"); - schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema); - - AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), - "JAVA", "java.lang.String"); - schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema); - - ModelService.registerModel(AxContextSchemas.class, schemas); - - testEvent = new AxEvent(new AxArtifactKey("TestEvent", "0.0.1")); - testEvent.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField teField0 = new AxField(new AxReferenceKey(testEvent.getKey(), "intValue"), simpleIntSchema.getKey()); - testEvent.getParameterMap().put("intValue", teField0); - AxField teField1 = new AxField(new AxReferenceKey(testEvent.getKey(), "doubleValue"), - simpleDoubleSchema.getKey()); - testEvent.getParameterMap().put("doubleValue", teField1); - AxField teField2 = new AxField(new AxReferenceKey(testEvent.getKey(), "stringValue"), - simpleStringSchema.getKey(), true); - testEvent.getParameterMap().put("stringValue", teField2); - - AxEvents events = new AxEvents(); - events.getEventMap().put(testEvent.getKey(), testEvent); - - ModelService.registerModel(AxEvents.class, events); - } - - /** - * Unregister test events and schemas. - */ - @AfterClass - public static void unregisterTestEventsAndSchemas() { - ModelService.clear(); - ParameterService.clear(); - } - - /** - * Test stability. - * - * @throws ApexEventException the apex event exception - */ - @Test - public void testStability() throws ApexEventException { - Apex2YamlEventConverter converter = new Apex2YamlEventConverter(); - - try { - converter.init(null); - fail("this test should throw an exception"); - } catch (ApexEventRuntimeException e) { - assertEquals("specified consumer properties are not applicable to the YAML event protocol", e.getMessage()); - } - - YamlEventProtocolParameters pars = new YamlEventProtocolParameters(); - converter.init(pars); - - try { - converter.toApexEvent("NonExistantEvent", ""); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("Failed to unmarshal YAML event: an event definition for an event named \"NonExistantEvent\"", - e.getMessage().substring(0, 89)); - } - - try { - converter.toApexEvent("TestEvent", null); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("event processing failed, event is null", e.getMessage()); - } - - try { - converter.toApexEvent("TestEvent", 1); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("error converting event \"1\" to a string", e.getMessage()); - } - - try { - converter.toApexEvent("TestEvent", ""); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertTrue(e.getMessage().contains("Field \"doubleValue\" is missing")); - } - - try { - converter.fromApexEvent(null); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("event processing failed, Apex event is null", e.getMessage()); - } - - ApexEvent apexEvent = new ApexEvent(testEvent.getKey().getName(), testEvent.getKey().getVersion(), - testEvent.getNameSpace(), testEvent.getSource(), testEvent.getTarget()); - apexEvent.put("doubleValue", 123.45); - apexEvent.put("intValue", 123); - apexEvent.put("stringValue", "123.45"); - - apexEvent.setExceptionMessage("my wonderful exception message"); - String yamlString = (String) converter.fromApexEvent(apexEvent); - assertTrue(yamlString.contains("my wonderful exception message")); - - apexEvent.remove("intValue"); - try { - yamlString = (String) converter.fromApexEvent(apexEvent); - fail("this test should throw an exception"); - } catch (ApexEventRuntimeException e) { - assertEquals("error parsing TestEvent:0.0.1 event to Json. Field \"intValue\" is missing", - e.getMessage().substring(0, 72)); - } - - try { - converter.toApexEvent(null, ""); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"", - e.getMessage().substring(0, 81)); - } - - pars.setNameAlias("TheNameField"); - try { - converter.toApexEvent(null, ""); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"", - e.getMessage().substring(0, 81)); - } - - apexEvent.put("intValue", 123); - - apexEvent.remove("stringValue"); - yamlString = (String) converter.fromApexEvent(apexEvent); - apexEvent.put("stringValue", "123.45"); - - String yamlInputString = "doubleValue: 123.45\n" + "intValue: 123"; - - List eventList = converter.toApexEvent("TestEvent", yamlInputString); - assertEquals(123.45, eventList.get(0).get("doubleValue")); - - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: null"; - - eventList = converter.toApexEvent("TestEvent", yamlInputString); - assertEquals(null, eventList.get(0).get("stringValue")); - - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: TestEvent"; - pars.setNameAlias("stringValue"); - eventList = converter.toApexEvent(null, yamlInputString); - assertEquals("TestEvent", eventList.get(0).get("stringValue")); - - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: SomeOtherEvent"; - eventList = converter.toApexEvent("TestEvent", yamlInputString); - assertEquals("SomeOtherEvent", eventList.get(0).get("stringValue")); - - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: 0.0.1"; - pars.setNameAlias(null); - pars.setVersionAlias("stringValue"); - eventList = converter.toApexEvent("TestEvent", yamlInputString); - assertEquals("0.0.1", eventList.get(0).get("stringValue")); - - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: org.some.other.namespace"; - pars.setVersionAlias(null); - pars.setNameSpaceAlias("stringValue"); - try { - converter.toApexEvent("TestEvent", yamlInputString); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("Failed to unmarshal YAML event: namespace \"org.some.other.namespace\" on event", - e.getMessage().substring(0, 77)); - } - - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" - + "stringValue: org.onap.policy.apex.plugins.event.protocol.yaml"; - eventList = converter.toApexEvent("TestEvent", yamlInputString); - assertEquals("org.onap.policy.apex.plugins.event.protocol.yaml", eventList.get(0).getNameSpace()); - - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MySource"; - pars.setNameSpaceAlias(null); - pars.setSourceAlias("stringValue"); - eventList = converter.toApexEvent("TestEvent", yamlInputString); - assertEquals("MySource", eventList.get(0).getSource()); - - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyTarget"; - pars.setSourceAlias(null); - pars.setTargetAlias("stringValue"); - eventList = converter.toApexEvent("TestEvent", yamlInputString); - assertEquals("MyTarget", eventList.get(0).getTarget()); - pars.setTargetAlias(null); - - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyString"; - pars.setSourceAlias(null); - pars.setTargetAlias("intValue"); - try { - converter.toApexEvent("TestEvent", yamlInputString); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("Failed to unmarshal YAML event: field \"target\" with type \"java.lang.Integer\"", - e.getMessage().substring(0, 76)); - } - pars.setTargetAlias(null); - - yamlInputString = "doubleValue: 123.45\n" + "intValue: ~\n" + "stringValue: MyString"; - try { - converter.toApexEvent("TestEvent", yamlInputString); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertTrue(e.getMessage().contains("mandatory field \"intValue\" is missing")); - } - } -} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolTest.java new file mode 100644 index 000000000..2b6a13f89 --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolTest.java @@ -0,0 +1,287 @@ +/*- + * ============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.protocol.yaml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +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.model.eventmodel.concepts.AxField; +import org.onap.policy.apex.model.utilities.TextFileUtils; +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.impl.filecarrierplugin.consumer.HeaderDelimitedTextBlockReader; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock; +import org.onap.policy.common.parameters.ParameterService; + +/** + * The Class TestYamlEventProtocol. + */ +public class YamlEventProtocolTest { + + /** + * Register test events and schemas. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @BeforeClass + public static void registerTestEventsAndSchemas() throws IOException { + SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters()); + ParameterService.register(schemaParameters, true); + + AxContextSchemas schemas = new AxContextSchemas(); + + AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA", + "java.lang.Integer"); + schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema); + + AxContextSchema simpleDoubleSchema = new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"), + "JAVA", "java.lang.Double"); + schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema); + + AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), + "JAVA", "java.lang.String"); + schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema); + + AxContextSchema arrayListSchema = new AxContextSchema(new AxArtifactKey("ArrayListSchema", "0.0.1"), "JAVA", + "java.util.ArrayList"); + schemas.getSchemasMap().put(arrayListSchema.getKey(), arrayListSchema); + + AxContextSchema linkedHashMapSchema = new AxContextSchema(new AxArtifactKey("LinkedHashMapSchema", "0.0.1"), + "JAVA", "java.util.LinkedHashMap"); + schemas.getSchemasMap().put(linkedHashMapSchema.getKey(), linkedHashMapSchema); + + ModelService.registerModel(AxContextSchemas.class, schemas); + + AxEvents events = new AxEvents(); + + AxEvent testEvent0 = new AxEvent(new AxArtifactKey("TestEvent0", "0.0.1")); + testEvent0.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + events.getEventMap().put(testEvent0.getKey(), testEvent0); + + AxEvent testEvent1 = new AxEvent(new AxArtifactKey("TestEvent1", "0.0.1")); + testEvent1.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te1Field0 = new AxField(new AxReferenceKey(testEvent1.getKey(), "yaml_field"), + arrayListSchema.getKey()); + testEvent1.getParameterMap().put("yaml_field", te1Field0); + events.getEventMap().put(testEvent1.getKey(), testEvent1); + + AxEvent testEvent2 = new AxEvent(new AxArtifactKey("TestEvent2", "0.0.1")); + testEvent2.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te2Field0 = new AxField(new AxReferenceKey(testEvent2.getKey(), "hr"), simpleIntSchema.getKey()); + testEvent2.getParameterMap().put("hr", te2Field0); + AxField te2Field1 = new AxField(new AxReferenceKey(testEvent2.getKey(), "avg"), simpleDoubleSchema.getKey()); + testEvent2.getParameterMap().put("avg", te2Field1); + AxField te2Field2 = new AxField(new AxReferenceKey(testEvent2.getKey(), "rbi"), simpleIntSchema.getKey()); + testEvent2.getParameterMap().put("rbi", te2Field2); + events.getEventMap().put(testEvent2.getKey(), testEvent2); + + AxEvent testEvent3 = new AxEvent(new AxArtifactKey("TestEvent3", "0.0.1")); + testEvent3.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te3Field0 = new AxField(new AxReferenceKey(testEvent3.getKey(), "american"), arrayListSchema.getKey()); + testEvent3.getParameterMap().put("american", te3Field0); + AxField te3Field1 = new AxField(new AxReferenceKey(testEvent3.getKey(), "national"), arrayListSchema.getKey()); + testEvent3.getParameterMap().put("national", te3Field1); + events.getEventMap().put(testEvent3.getKey(), testEvent3); + + AxEvent testEvent4 = new AxEvent(new AxArtifactKey("TestEvent4", "0.0.1")); + testEvent4.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te4Field0 = new AxField(new AxReferenceKey(testEvent4.getKey(), "yaml_field"), + arrayListSchema.getKey()); + testEvent4.getParameterMap().put("yaml_field", te4Field0); + events.getEventMap().put(testEvent4.getKey(), testEvent4); + + AxEvent testEvent5 = new AxEvent(new AxArtifactKey("TestEvent5", "0.0.1")); + testEvent5.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te5Field0 = new AxField(new AxReferenceKey(testEvent5.getKey(), "yaml_field"), + arrayListSchema.getKey()); + testEvent5.getParameterMap().put("yaml_field", te5Field0); + events.getEventMap().put(testEvent5.getKey(), testEvent5); + + AxEvent testEvent6 = new AxEvent(new AxArtifactKey("TestEvent6", "0.0.1")); + testEvent6.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te6Field0 = new AxField(new AxReferenceKey(testEvent6.getKey(), "MarkMcGwire"), + linkedHashMapSchema.getKey()); + testEvent6.getParameterMap().put("Mark McGwire", te6Field0); + AxField te6Field1 = new AxField(new AxReferenceKey(testEvent6.getKey(), "SammySosa"), + linkedHashMapSchema.getKey()); + testEvent6.getParameterMap().put("Sammy Sosa", te6Field1); + events.getEventMap().put(testEvent6.getKey(), testEvent6); + + AxEvent testEvent7 = new AxEvent(new AxArtifactKey("TestEvent7", "0.0.1")); + testEvent7.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te7Field0 = new AxField(new AxReferenceKey(testEvent7.getKey(), "time"), simpleIntSchema.getKey()); + testEvent7.getParameterMap().put("time", te7Field0); + AxField te7Field1 = new AxField(new AxReferenceKey(testEvent7.getKey(), "player"), simpleStringSchema.getKey()); + testEvent7.getParameterMap().put("player", te7Field1); + AxField te7Field2 = new AxField(new AxReferenceKey(testEvent7.getKey(), "action"), simpleStringSchema.getKey()); + testEvent7.getParameterMap().put("action", te7Field2); + events.getEventMap().put(testEvent7.getKey(), testEvent7); + + AxEvent testEvent8 = new AxEvent(new AxArtifactKey("TestEvent8", "0.0.1")); + testEvent8.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te8Field0 = new AxField(new AxReferenceKey(testEvent8.getKey(), "hr"), arrayListSchema.getKey()); + testEvent8.getParameterMap().put("hr", te8Field0); + AxField te8Field1 = new AxField(new AxReferenceKey(testEvent8.getKey(), "rbi"), arrayListSchema.getKey()); + testEvent8.getParameterMap().put("rbi", te8Field1); + events.getEventMap().put(testEvent8.getKey(), testEvent8); + + AxEvent testEvent9 = new AxEvent(new AxArtifactKey("TestEvent9", "0.0.1")); + testEvent9.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te9Field0 = new AxField(new AxReferenceKey(testEvent9.getKey(), "ChicagoCubs"), + arrayListSchema.getKey()); + testEvent9.getParameterMap().put("ChicagoCubs", te9Field0); + AxField te9Field1 = new AxField(new AxReferenceKey(testEvent9.getKey(), "AtlantaBraves"), + arrayListSchema.getKey()); + testEvent9.getParameterMap().put("AtlantaBraves", te9Field1); + events.getEventMap().put(testEvent9.getKey(), testEvent9); + + AxEvent testEvent10 = new AxEvent(new AxArtifactKey("TestEvent10", "0.0.1")); + testEvent10.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te10Field0 = new AxField(new AxReferenceKey(testEvent10.getKey(), "yaml_field"), + arrayListSchema.getKey()); + testEvent10.getParameterMap().put("yaml_field", te10Field0); + events.getEventMap().put(testEvent10.getKey(), testEvent10); + + AxEvent testEvent11 = new AxEvent(new AxArtifactKey("TestEvent11", "0.0.1")); + testEvent11.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te11Field0 = new AxField(new AxReferenceKey(testEvent11.getKey(), "tosca_definitions_version"), + simpleStringSchema.getKey()); + testEvent11.getParameterMap().put("tosca_definitions_version", te11Field0); + AxField te11Field1 = new AxField(new AxReferenceKey(testEvent11.getKey(), "description"), + simpleStringSchema.getKey(), true); + testEvent11.getParameterMap().put("description", te11Field1); + AxField te11Field2 = new AxField(new AxReferenceKey(testEvent11.getKey(), "node_types"), + linkedHashMapSchema.getKey(), true); + testEvent11.getParameterMap().put("node_types", te11Field2); + AxField te11Field3 = new AxField(new AxReferenceKey(testEvent11.getKey(), "topology_template"), + linkedHashMapSchema.getKey()); + testEvent11.getParameterMap().put("topology_template", te11Field3); + events.getEventMap().put(testEvent11.getKey(), testEvent11); + + ModelService.registerModel(AxEvents.class, events); + } + + /** + * Unregister test events and schemas. + */ + @AfterClass + public static void unregisterTestEventsAndSchemas() { + ModelService.clear(); + ParameterService.clear(); + } + + /** + * Test yaml processing. + * + * @throws ApexEventException the apex event exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testYamlProcessing() throws ApexEventException, IOException { + try { + testYamlDecodeEncode("TestEvent0", 1, 0, "Empty0"); + fail("test should fail here"); + } catch (ApexEventException e) { + assertEquals("event processing failed, event is null", e.getMessage()); + } + + testYamlDecodeEncode("TestEvent0", 1, 0, "Empty1"); + testYamlDecodeEncode("TestEvent1", 1, 1, "Collection0"); + testYamlDecodeEncode("TestEvent2", 1, 3, "Collection1"); + testYamlDecodeEncode("TestEvent3", 1, 2, "Collection2"); + testYamlDecodeEncode("TestEvent4", 1, 1, "Collection3"); + testYamlDecodeEncode("TestEvent5", 1, 1, "Collection4"); + testYamlDecodeEncode("TestEvent6", 1, 2, "Collection5"); + testYamlDecodeEncode("TestEvent1", 2, 1, "Structure0"); + testYamlDecodeEncode("TestEvent7", 2, 3, "Structure1"); + testYamlDecodeEncode("TestEvent8", 1, 2, "Structure2"); + testYamlDecodeEncode("TestEvent8", 1, 2, "Structure3"); + testYamlDecodeEncode("TestEvent9", 1, 2, "Structure4"); + testYamlDecodeEncode("TestEvent10", 1, 1, "Structure5"); + testYamlDecodeEncode("TestEvent11", 1, 4, "TOSCA0"); + } + + /** + * Test yaml decode encode. + * + * @param eventName the event name + * @param eventCount the event count + * @param parCount the par count + * @param fileName the file name + * @throws ApexEventException the apex event exception + * @throws IOException Signals that an I/O exception has occurred. + */ + private void testYamlDecodeEncode(final String eventName, final int eventCount, final int parCount, + final String fileName) throws ApexEventException, IOException { + YamlEventProtocolParameters parameters = new YamlEventProtocolParameters(); + parameters.setDelimiterAtStart(false); + + Apex2YamlEventConverter converter = new Apex2YamlEventConverter(); + converter.init(parameters); + + String filePath = "src/test/resources/yaml_in/" + fileName + ".yaml"; + FileInputStream fileInputStream = new FileInputStream(new File(filePath)); + HeaderDelimitedTextBlockReader reader = new HeaderDelimitedTextBlockReader(parameters); + reader.init(fileInputStream); + + List eventList = new ArrayList<>(); + + TextBlock textBlock; + do { + textBlock = reader.readTextBlock(); + + eventList.addAll(converter.toApexEvent(eventName, textBlock.getText())); + } + while (!textBlock.isEndOfText()); + + fileInputStream.close(); + + assertEquals(eventCount, eventList.size()); + + for (int eventNo = 0; eventNo < eventCount; eventNo++) { + assertEquals(parCount, eventList.get(0).size()); + + String eventYaml = (String) converter.fromApexEvent(eventList.get(eventNo)); + String expectedYaml = TextFileUtils + .getTextFileAsString("src/test/resources/yaml_out/" + fileName + '_' + eventNo + ".yaml"); + assertEquals(expectedYaml.replaceAll("\\s*", ""), eventYaml.replaceAll("\\s*", "")); + } + } +} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlPluginStabilityTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlPluginStabilityTest.java new file mode 100644 index 000000000..7f62961be --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlPluginStabilityTest.java @@ -0,0 +1,278 @@ +/*- + * ============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.protocol.yaml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.util.List; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +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.model.eventmodel.concepts.AxField; +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.common.parameters.ParameterService; + +/** + * The Class TestYamlPluginStability. + */ +public class YamlPluginStabilityTest { + static AxEvent testEvent; + + /** + * Register test events and schemas. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @BeforeClass + public static void registerTestEventsAndSchemas() throws IOException { + SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters()); + ParameterService.register(schemaParameters); + + AxContextSchemas schemas = new AxContextSchemas(); + + AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA", + "java.lang.Integer"); + schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema); + + AxContextSchema simpleDoubleSchema = new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"), + "JAVA", "java.lang.Double"); + schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema); + + AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), + "JAVA", "java.lang.String"); + schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema); + + ModelService.registerModel(AxContextSchemas.class, schemas); + + testEvent = new AxEvent(new AxArtifactKey("TestEvent", "0.0.1")); + testEvent.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField teField0 = new AxField(new AxReferenceKey(testEvent.getKey(), "intValue"), simpleIntSchema.getKey()); + testEvent.getParameterMap().put("intValue", teField0); + AxField teField1 = new AxField(new AxReferenceKey(testEvent.getKey(), "doubleValue"), + simpleDoubleSchema.getKey()); + testEvent.getParameterMap().put("doubleValue", teField1); + AxField teField2 = new AxField(new AxReferenceKey(testEvent.getKey(), "stringValue"), + simpleStringSchema.getKey(), true); + testEvent.getParameterMap().put("stringValue", teField2); + + AxEvents events = new AxEvents(); + events.getEventMap().put(testEvent.getKey(), testEvent); + + ModelService.registerModel(AxEvents.class, events); + } + + /** + * Unregister test events and schemas. + */ + @AfterClass + public static void unregisterTestEventsAndSchemas() { + ModelService.clear(); + ParameterService.clear(); + } + + /** + * Test stability. + * + * @throws ApexEventException the apex event exception + */ + @Test + public void testStability() throws ApexEventException { + Apex2YamlEventConverter converter = new Apex2YamlEventConverter(); + + try { + converter.init(null); + fail("this test should throw an exception"); + } catch (ApexEventRuntimeException e) { + assertEquals("specified consumer properties are not applicable to the YAML event protocol", e.getMessage()); + } + + YamlEventProtocolParameters pars = new YamlEventProtocolParameters(); + converter.init(pars); + + try { + converter.toApexEvent("NonExistantEvent", ""); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertEquals("Failed to unmarshal YAML event: an event definition for an event named \"NonExistantEvent\"", + e.getMessage().substring(0, 89)); + } + + try { + converter.toApexEvent("TestEvent", null); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertEquals("event processing failed, event is null", e.getMessage()); + } + + try { + converter.toApexEvent("TestEvent", 1); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertEquals("error converting event \"1\" to a string", e.getMessage()); + } + + try { + converter.toApexEvent("TestEvent", ""); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertTrue(e.getMessage().contains("Field \"doubleValue\" is missing")); + } + + try { + converter.fromApexEvent(null); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertEquals("event processing failed, Apex event is null", e.getMessage()); + } + + ApexEvent apexEvent = new ApexEvent(testEvent.getKey().getName(), testEvent.getKey().getVersion(), + testEvent.getNameSpace(), testEvent.getSource(), testEvent.getTarget()); + apexEvent.put("doubleValue", 123.45); + apexEvent.put("intValue", 123); + apexEvent.put("stringValue", "123.45"); + + apexEvent.setExceptionMessage("my wonderful exception message"); + String yamlString = (String) converter.fromApexEvent(apexEvent); + assertTrue(yamlString.contains("my wonderful exception message")); + + apexEvent.remove("intValue"); + try { + yamlString = (String) converter.fromApexEvent(apexEvent); + fail("this test should throw an exception"); + } catch (ApexEventRuntimeException e) { + assertEquals("error parsing TestEvent:0.0.1 event to Json. Field \"intValue\" is missing", + e.getMessage().substring(0, 72)); + } + + try { + converter.toApexEvent(null, ""); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"", + e.getMessage().substring(0, 81)); + } + + pars.setNameAlias("TheNameField"); + try { + converter.toApexEvent(null, ""); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"", + e.getMessage().substring(0, 81)); + } + + apexEvent.put("intValue", 123); + + apexEvent.remove("stringValue"); + yamlString = (String) converter.fromApexEvent(apexEvent); + apexEvent.put("stringValue", "123.45"); + + String yamlInputString = "doubleValue: 123.45\n" + "intValue: 123"; + + List eventList = converter.toApexEvent("TestEvent", yamlInputString); + assertEquals(123.45, eventList.get(0).get("doubleValue")); + + yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: null"; + + eventList = converter.toApexEvent("TestEvent", yamlInputString); + assertEquals(null, eventList.get(0).get("stringValue")); + + yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: TestEvent"; + pars.setNameAlias("stringValue"); + eventList = converter.toApexEvent(null, yamlInputString); + assertEquals("TestEvent", eventList.get(0).get("stringValue")); + + yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: SomeOtherEvent"; + eventList = converter.toApexEvent("TestEvent", yamlInputString); + assertEquals("SomeOtherEvent", eventList.get(0).get("stringValue")); + + yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: 0.0.1"; + pars.setNameAlias(null); + pars.setVersionAlias("stringValue"); + eventList = converter.toApexEvent("TestEvent", yamlInputString); + assertEquals("0.0.1", eventList.get(0).get("stringValue")); + + yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: org.some.other.namespace"; + pars.setVersionAlias(null); + pars.setNameSpaceAlias("stringValue"); + try { + converter.toApexEvent("TestEvent", yamlInputString); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertEquals("Failed to unmarshal YAML event: namespace \"org.some.other.namespace\" on event", + e.getMessage().substring(0, 77)); + } + + yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + + "stringValue: org.onap.policy.apex.plugins.event.protocol.yaml"; + eventList = converter.toApexEvent("TestEvent", yamlInputString); + assertEquals("org.onap.policy.apex.plugins.event.protocol.yaml", eventList.get(0).getNameSpace()); + + yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MySource"; + pars.setNameSpaceAlias(null); + pars.setSourceAlias("stringValue"); + eventList = converter.toApexEvent("TestEvent", yamlInputString); + assertEquals("MySource", eventList.get(0).getSource()); + + yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyTarget"; + pars.setSourceAlias(null); + pars.setTargetAlias("stringValue"); + eventList = converter.toApexEvent("TestEvent", yamlInputString); + assertEquals("MyTarget", eventList.get(0).getTarget()); + pars.setTargetAlias(null); + + yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyString"; + pars.setSourceAlias(null); + pars.setTargetAlias("intValue"); + try { + converter.toApexEvent("TestEvent", yamlInputString); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertEquals("Failed to unmarshal YAML event: field \"target\" with type \"java.lang.Integer\"", + e.getMessage().substring(0, 76)); + } + pars.setTargetAlias(null); + + yamlInputString = "doubleValue: 123.45\n" + "intValue: ~\n" + "stringValue: MyString"; + try { + converter.toApexEvent("TestEvent", yamlInputString); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertTrue(e.getMessage().contains("mandatory field \"intValue\" is missing")); + } + } +} -- cgit 1.2.3-korg