From b6977d2f7ce64ece732ac1a1a0525dac972d7ccf Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 17 Feb 2021 15:23:38 -0500 Subject: Fix sonars in apex-pdp Addressed the following issues: - initialize mocks before use - use parameterized queries - Random() is not secure - provide parameterized type for generics - unused imports - constructor visibility - use compute() instead of containsKey()/put() - make final fields static - rename constants to all upper case - no assert() in Thread.run() methods - nested try - nested if/else - too many break/continue - use try-with-resources - repeatable annotations - overlapping characters in reg ex - hashcode is not sufficient in compareTo() - need equals() with compareTo() - make class an interface - use parameterized test - multiple calls in assert() - log or re-throw - use different type of lambda - use parameterized logging - use StringBuilder instead of concatenation - use StandardCharsets.UTF_8 Issue-ID: POLICY-2906 Change-Id: I2cf8c885e3e22c2c6cbe6403a34906928afad022 Signed-off-by: Jim Hahn --- .../protocol/xml/XmlTaggedEventConsumerTest.java | 286 +++++++-------------- 1 file changed, 90 insertions(+), 196 deletions(-) (limited to 'plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test') 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 index 52b84bc5b..320d7ba22 100644 --- 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 @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. 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. @@ -44,14 +45,7 @@ public class XmlTaggedEventConsumerTest { */ @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(""); } /** @@ -79,15 +65,8 @@ public class XmlTaggedEventConsumerTest { */ @Test public void testFullEventLine() throws IOException { - final InputStream xmlInputStream = new ByteArrayInputStream( - "1469781869268".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268", textBlock.getText()); - assertTrue(textBlock.isEndOfText()); + verifyLine("testFullEventLine", "1469781869268", + "1469781869268"); } /** @@ -97,14 +76,8 @@ public class XmlTaggedEventConsumerTest { */ @Test public void testFullEventGarbageBeforeLine() throws IOException { - final InputStream xmlInputStream = new ByteArrayInputStream( - "Garbage1469781869268".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268"); } /** @@ -114,15 +87,8 @@ public class XmlTaggedEventConsumerTest { */ @Test public void testFullEventGarbageBeforeAfterLine() throws IOException { - final InputStream xmlInputStream = new ByteArrayInputStream( - "Garbage1469781869268Rubbish" - .getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268Rubbish"); } /** @@ -132,16 +98,9 @@ public class XmlTaggedEventConsumerTest { */ @Test public void testFullEventGarbageAfterLine() throws IOException { - final InputStream xmlInputStream = new ByteArrayInputStream( - "1469781869268Rubbish".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268Rubbish", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); + verifyLine("testFullEventGarbageAfterLine", + "1469781869268Rubbish", + "1469781869268Rubbish"); } /** @@ -151,13 +110,7 @@ public class XmlTaggedEventConsumerTest { */ @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"); } /** @@ -184,16 +130,9 @@ public class XmlTaggedEventConsumerTest { */ @Test public void testFullEventMultiLine() throws IOException { - final InputStream xmlInputStream = new ByteArrayInputStream( - "\n\n1469781869268\n\n\n".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); + verifyLine("testFullEventMultiLine", + "\n\n1469781869268\n\n\n", + "\n\n1469781869268\n"); } /** @@ -203,17 +142,9 @@ public class XmlTaggedEventConsumerTest { */ @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()); + verifyLine("testFullEventGarbageBeforeMultiLine", + "Garbage\n\n\n1469781869268\n\n\n", + "\n\n1469781869268\n"); } /** @@ -225,15 +156,9 @@ public class XmlTaggedEventConsumerTest { 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()); + verifyLine("testFullEventsGarbageAfterLine", garbageString, + "\n\n1469781869268\n\nRubbish"); } /** @@ -243,17 +168,9 @@ public class XmlTaggedEventConsumerTest { */ @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()); + verifyLine("testFullEventGarbageAfterMultiLine", + "\n\n1469781869268\n\nRubbish", + "\n\n1469781869268\n\nRubbish"); } /** @@ -263,15 +180,8 @@ public class XmlTaggedEventConsumerTest { */ @Test public void testPartialEventsLine() throws IOException { - String garbageString = "1469781869268" - + "1469781869268"; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("" + + "1469781869268"); } /** @@ -281,15 +191,9 @@ public class XmlTaggedEventConsumerTest { */ @Test public void testFullEventsGarbageBeforeLine() throws IOException { - String garbageString = "Garbage1469781869268" - + ""; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268" + + ""); } /** @@ -299,15 +203,8 @@ public class XmlTaggedEventConsumerTest { */ @Test public void testFullEventsGarbageBeforeAfterLine() throws IOException { - String garbageString = "Garbage1469781869268" - + "Rubbish\nRefuse"; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268" + + "Rubbish\nRefuse"); } /** @@ -319,14 +216,8 @@ public class XmlTaggedEventConsumerTest { public void testFullEventsGarbageAfterLine() throws IOException { String garbageString = "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()); + verifyLine("testPartialEventsMultiLine", + "1469781869268\n\n\n\n\n", + "\n\n"); } /** @@ -356,20 +241,10 @@ public class XmlTaggedEventConsumerTest { 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()); + verifyLines("testFullEventsMultiLine", garbageString, + "\n\n1469781869268\n", + "\n\n1469781869268\n"); } /** @@ -381,20 +256,10 @@ public class XmlTaggedEventConsumerTest { 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()); + verifyLines("testFullEventsGarbageBeforeMultiLine", garbageString, + "\n\n1469781869268\n", + "\n\n1469781869268\n"); } /** @@ -407,20 +272,10 @@ public class XmlTaggedEventConsumerTest { 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()); + verifyLines("testFullEventsGarbageBeforeAfterMultiLine", garbageString, + "\n\n1469781869268\n\nRubbish", + "\n\n1469781869268\n\nRefuse"); } /** @@ -430,16 +285,55 @@ public class XmlTaggedEventConsumerTest { */ @Test public void testFullEventsGarbageAfterMultiLine() throws IOException { - final InputStream xmlInputStream = new ByteArrayInputStream( - "\n\n1469781869268\n\nRubbish" - .getBytes()); + verifyLine("testFullEventsGarbageAfterMultiLine", + "\n\n1469781869268\n\nRubbish", + "\n\n1469781869268\n\nRubbish"); + } + + private void verifyNull(String testName, String xml) throws IOException { + final InputStream xmlInputStream = new ByteArrayInputStream(xml.getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\nRubbish", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); + assertTrue(testName, textBlock.isEndOfText()); } } -- cgit 1.2.3-korg