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 | |
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')
7 files changed, 66 insertions, 112 deletions
diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/consumer/ApexFileEventConsumer.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/consumer/ApexFileEventConsumer.java index 076c50b40..e12e4588e 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/consumer/ApexFileEventConsumer.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/consumer/ApexFileEventConsumer.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2020 Nordix Foundation. + * 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. @@ -129,7 +130,6 @@ public class ApexFileEventConsumer implements ApexEventConsumer, Runnable { final String errorMessage = APEX_FILE_CONSUMER_PREAMBLE + consumerName + "\" failed to open file for reading: \"" + fileCarrierTechnologyParameters.getFileName() + "\""; - LOGGER.warn(errorMessage, e); throw new ApexEventException(errorMessage, e); } diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/carriertechnology/CarrierTechnologyParameters.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/carriertechnology/CarrierTechnologyParameters.java index 5a90f847d..e688aad17 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/carriertechnology/CarrierTechnologyParameters.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/carriertechnology/CarrierTechnologyParameters.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========================================================= */ @@ -28,7 +29,7 @@ import org.onap.policy.common.parameters.ValidationStatus; /** * The default carrier technology parameter class that may be specialized by carrier technology plugins that require * plugin specific parameters. - * + * * <p>The following parameters are defined: <ol> <li>label: The label of the carrier technology. * <li>eventProducerPluginClass: The name of the plugin class that will be used by Apex to produce and emit output * events for this carrier technology <li>eventConsumerPluginClass: The name of the plugin class that will be used by @@ -50,7 +51,7 @@ public abstract class CarrierTechnologyParameters implements ParameterGroup { * Constructor to create a carrier technology parameters instance with the name of a sub class of this class and * register the instance with the parameter service. */ - public CarrierTechnologyParameters() { + protected CarrierTechnologyParameters() { super(); } diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolParameters.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolParameters.java index b3a264481..68d5f8a56 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolParameters.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolParameters.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========================================================= */ @@ -49,7 +50,7 @@ public abstract class EventProtocolParameters implements ParameterGroup { * Constructor to create an event protocol parameters instance with the name of a sub class of this class and * register the instance with the parameter service. */ - public EventProtocolParameters() { + protected EventProtocolParameters() { super(); } diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolTextCharDelimitedParameters.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolTextCharDelimitedParameters.java index 989d3f266..d15d7ed47 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolTextCharDelimitedParameters.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolTextCharDelimitedParameters.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========================================================= */ @@ -43,7 +44,7 @@ public abstract class EventProtocolTextCharDelimitedParameters extends EventProt /** * Constructor to create an event protocol parameters instance with the name of a sub class of this class. */ - public EventProtocolTextCharDelimitedParameters() { + protected EventProtocolTextCharDelimitedParameters() { super(); } diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolTextTokenDelimitedParameters.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolTextTokenDelimitedParameters.java index f9328f418..ff363aff6 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolTextTokenDelimitedParameters.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/eventprotocol/EventProtocolTextTokenDelimitedParameters.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========================================================= */ @@ -48,7 +49,7 @@ public abstract class EventProtocolTextTokenDelimitedParameters extends EventPro /** * Constructor to create an event protocol parameters instance with the name of a sub class of this class. */ - public EventProtocolTextTokenDelimitedParameters() { + protected EventProtocolTextTokenDelimitedParameters() { super(); } @@ -92,7 +93,7 @@ public abstract class EventProtocolTextTokenDelimitedParameters extends EventPro /** * Check if there must be a delimiter at the start of the first text block. - * + * * @return true if there must be a delimiter at the start of the text block */ public boolean isDelimiterAtStart() { @@ -101,7 +102,7 @@ public abstract class EventProtocolTextTokenDelimitedParameters extends EventPro /** * Sets if there has to be a delimiter at the start of the first text block. - * + * * @param delimiterAtStart * true if there must be a delimiter at the start of the text block */ 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 |