diff options
author | Jim Hahn <jrh3@att.com> | 2021-02-17 15:23:38 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2021-02-18 17:47:55 +0000 |
commit | b6977d2f7ce64ece732ac1a1a0525dac972d7ccf (patch) | |
tree | 4966f5aa4737b0fef8241989eb216cce7fbe9294 /services/services-engine/src/test/java | |
parent | 4e05982e36aa66b83fa0ee44b9631412442c3969 (diff) |
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 <jrh3@att.com>
Diffstat (limited to 'services/services-engine/src/test/java')
2 files changed, 42 insertions, 92 deletions
diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonTaggedEventConsumerTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonTaggedEventConsumerTest.java index 52d8981c3..3bdcf53d9 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonTaggedEventConsumerTest.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonTaggedEventConsumerTest.java @@ -1,19 +1,20 @@ /*- * ============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. * 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========================================================= */ @@ -40,91 +41,60 @@ public class JsonTaggedEventConsumerTest { @Test public void testGarbageText() throws IOException { - final InputStream jsonInputStream = new ByteArrayInputStream("hello there".getBytes()); - - final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}'); - taggedReader.init(jsonInputStream); - - final TextBlock textBlock = taggedReader.readTextBlock(); - assertNull(textBlock.getText()); - assertTrue(textBlock.isEndOfText()); + verifyNoEvent("testGarbageText", "hello there"); } @Test public void testPartialEvent() throws IOException { - final InputStream jsonInputStream = new ByteArrayInputStream("\"TestTimestamp\": 1469781869268}".getBytes()); - - final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}'); - taggedReader.init(jsonInputStream); - - final TextBlock textBlock = taggedReader.readTextBlock(); - assertNull(textBlock.getText()); - assertTrue(textBlock.isEndOfText()); + verifyNoEvent("testGarbageText", "\"TestTimestamp\": 1469781869268}"); } @Test public void testFullEvent() throws IOException { - final InputStream jsonInputStream = new ByteArrayInputStream("{TestTimestamp\": 1469781869268}".getBytes()); - - final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}'); - taggedReader.init(jsonInputStream); - - TextBlock textBlock = taggedReader.readTextBlock(); - assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText()); - - textBlock = taggedReader.readTextBlock(); - assertNull(textBlock.getText()); - assertTrue(textBlock.isEndOfText()); + verifyMulti("testFullEvent", "{TestTimestamp\": 1469781869268}", "{TestTimestamp\": 1469781869268}"); } @Test public void testFullEventGarbageBefore() throws IOException { - final InputStream jsonInputStream = - new ByteArrayInputStream("Garbage{TestTimestamp\": 1469781869268}".getBytes()); - - final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}'); - taggedReader.init(jsonInputStream); - - TextBlock textBlock = taggedReader.readTextBlock(); - assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText()); - assertFalse(textBlock.isEndOfText()); - - textBlock = taggedReader.readTextBlock(); - assertNull(textBlock.getText()); - assertTrue(textBlock.isEndOfText()); + verifyMulti("testFullEventGarbageBefore", "Garbage{TestTimestamp\": 1469781869268}", + "{TestTimestamp\": 1469781869268}"); } @Test public void testFullEventGarbageBeforeAfter() throws IOException { - final InputStream jsonInputStream = - new ByteArrayInputStream("Garbage{TestTimestamp\": 1469781869268}Rubbish".getBytes()); + verifyMulti("testFullEventGarbageBeforeAfter", "Garbage{TestTimestamp\": 1469781869268}Rubbish", + "{TestTimestamp\": 1469781869268}"); + } + + @Test + public void testFullEventGarbageAfter() throws IOException { + verifyMulti("testFullEventGarbageAfter", "{TestTimestamp\": 1469781869268}Rubbish", + "{TestTimestamp\": 1469781869268}"); + } + + private void verifyNoEvent(String testName, String input) throws IOException { + final InputStream jsonInputStream = new ByteArrayInputStream(input.getBytes()); final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}'); taggedReader.init(jsonInputStream); - TextBlock textBlock = taggedReader.readTextBlock(); - assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText()); - assertFalse(textBlock.isEndOfText()); - - textBlock = taggedReader.readTextBlock(); - assertNull(textBlock.getText()); - assertTrue(textBlock.isEndOfText()); + final TextBlock textBlock = taggedReader.readTextBlock(); + assertNull(testName, textBlock.getText()); + assertTrue(testName, textBlock.isEndOfText()); } - @Test - public void testFullEventGarbageAfter() throws IOException { - final InputStream jsonInputStream = - new ByteArrayInputStream("{TestTimestamp\": 1469781869268}Rubbish".getBytes()); + private void verifyMulti(String testName, String input, String expected) throws IOException { + final InputStream jsonInputStream = new ByteArrayInputStream(input.getBytes()); final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}'); taggedReader.init(jsonInputStream); TextBlock textBlock = taggedReader.readTextBlock(); - assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText()); - assertFalse(textBlock.isEndOfText()); + assertEquals(testName, expected, textBlock.getText()); + assertFalse(testName, textBlock.isEndOfText()); textBlock = taggedReader.readTextBlock(); - assertNull(textBlock.getText()); - assertTrue(textBlock.isEndOfText()); + assertNull(testName, textBlock.getText()); + assertTrue(testName, textBlock.isEndOfText()); } } diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/SyncParameterTests.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/SyncParameterTests.java index 9f420d8fb..71ff28877 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/SyncParameterTests.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/SyncParameterTests.java @@ -3,6 +3,7 @@ * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2020 Bell Canada. 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. @@ -191,53 +192,32 @@ public class SyncParameterTests { @Test public void testSyncGoodSyncGoodTimeoutProducer() throws ParameterException { - final String[] args = {"-p", "src/test/resources/parameters/syncGoodParamsProducerTimeout.json"}; - final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - - final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - assertEquals(12345, parameters.getEventInputParameters().get("SyncConsumer0") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - assertEquals(1, parameters.getEventInputParameters().get("SyncConsumer1") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - assertEquals(12345, parameters.getEventOutputParameters().get("SyncProducer0") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - assertEquals(1, parameters.getEventOutputParameters().get("SyncProducer1") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - + verifySyncGoodSyncGoodTimeout("src/test/resources/parameters/syncGoodParamsProducerTimeout.json"); } @Test public void testSyncGoodSyncGoodTimeoutConsumer() throws ParameterException { - final String[] args = {"-p", "src/test/resources/parameters/syncGoodParamsConsumerTimeout.json"}; - final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); - - final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - assertEquals(12345, parameters.getEventInputParameters().get("SyncConsumer0") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - assertEquals(1, parameters.getEventInputParameters().get("SyncConsumer1") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - assertEquals(12345, parameters.getEventOutputParameters().get("SyncProducer0") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - assertEquals(1, parameters.getEventOutputParameters().get("SyncProducer1") - .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - + verifySyncGoodSyncGoodTimeout("src/test/resources/parameters/syncGoodParamsConsumerTimeout.json"); } @Test public void testSyncGoodSyncGoodTimeoutBoth() throws ParameterException { - final String[] args = {"-p", "src/test/resources/parameters/syncGoodParamsBothTimeout.json"}; + verifySyncGoodSyncGoodTimeout("src/test/resources/parameters/syncGoodParamsBothTimeout.json"); + } + + private void verifySyncGoodSyncGoodTimeout(String fileName) throws ParameterException { + final String[] args = {"-p", fileName}; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - assertEquals(12345, parameters.getEventInputParameters().get("SyncConsumer0") + assertEquals(fileName, 12345, parameters.getEventInputParameters().get("SyncConsumer0") .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - assertEquals(1, parameters.getEventInputParameters().get("SyncConsumer1") + assertEquals(fileName, 1, parameters.getEventInputParameters().get("SyncConsumer1") .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - assertEquals(12345, parameters.getEventOutputParameters().get("SyncProducer0") + assertEquals(fileName, 12345, parameters.getEventOutputParameters().get("SyncProducer0") .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - assertEquals(1, parameters.getEventOutputParameters().get("SyncProducer1") + assertEquals(fileName, 1, parameters.getEventOutputParameters().get("SyncProducer1") .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); - } @Test |