From 2a58e25bee3bf029e1696a443f23495e119c5855 Mon Sep 17 00:00:00 2001 From: "adheli.tavares" Date: Thu, 12 May 2022 13:58:48 +0100 Subject: Security issues from sonar cloud fixes Issue-ID: POLICY-4168 Change-Id: Iedac6c95be452be81e8c73055193b4707735ef56 Signed-off-by: adheli.tavares --- ...AutomationCompositionDecoderFileInCsarTest.java | 2 +- .../file/PolicyDecoderFileInCsarToPolicyTest.java | 4 +- .../file/TestFileSystemReceptionHandler.java | 29 ++++----- .../reception/handling/sdc/DummyDecoder.java | 8 +-- .../handling/sdc/TestSdcReceptionHandler.java | 68 ++++++++++------------ .../reception/util/ReceptionUtilTest.java | 40 ------------- 6 files changed, 49 insertions(+), 102 deletions(-) delete mode 100644 plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/util/ReceptionUtilTest.java (limited to 'plugins/reception-plugins/src/test') diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/AutomationCompositionDecoderFileInCsarTest.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/AutomationCompositionDecoderFileInCsarTest.java index 80c520a4..ac8a75df 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/AutomationCompositionDecoderFileInCsarTest.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/AutomationCompositionDecoderFileInCsarTest.java @@ -92,6 +92,6 @@ public class AutomationCompositionDecoderFileInCsarTest { assertTrue(decoder.canHandle(csar)); assertThatThrownBy(() -> decoder.decode(csar)).isInstanceOf(PolicyDecodingException.class) - .hasMessageContaining("Failed decoding the acm"); + .hasMessageContaining("Couldn't read the zipFile"); } } diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyTest.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyTest.java index e76e9f38..45dd167c 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyTest.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2020, 2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -105,7 +105,7 @@ public class PolicyDecoderFileInCsarToPolicyTest { assertTrue(decoder.canHandle(csar)); assertThatThrownBy(() -> decoder.decode(csar)).isInstanceOf(PolicyDecodingException.class) - .hasMessageContaining("Failed decoding the policy"); + .hasMessageContaining("Couldn't read the zipFile"); } diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/file/TestFileSystemReceptionHandler.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/file/TestFileSystemReceptionHandler.java index c9debdd9..e7a97a24 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/file/TestFileSystemReceptionHandler.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/file/TestFileSystemReceptionHandler.java @@ -1,8 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Intel. All rights reserved. - * Copyright (C) 2019 Nordix Foundation. - * Modifications Copyright (C) 2020 Nordix Foundation + * Copyright (C) 2019-2020, 2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,11 +38,9 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; import org.mockito.runners.MockitoJUnitRunner; import org.mockito.stubbing.Answer; import org.onap.policy.common.parameters.ParameterService; -import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; import org.onap.policy.distribution.reception.statistics.DistributionStatisticsManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -68,13 +65,10 @@ public class TestFileSystemReceptionHandler { * * @throws IOException if it occurs * @throws SecurityException if it occurs - * @throws NoSuchFieldException if it occurs - * @throws IllegalAccessException if it occurs * @throws IllegalArgumentException if it occurs */ @Before - public final void init() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, - IllegalAccessException { + public final void init() throws IOException, SecurityException, IllegalArgumentException { DistributionStatisticsManager.resetAllStatistics(); final Gson gson = new GsonBuilder().create(); @@ -90,7 +84,7 @@ public class TestFileSystemReceptionHandler { } @Test - public final void testInit() throws IOException, InterruptedException { + public final void testInit() throws IOException { final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler); Mockito.doNothing().when(sypHandler).initFileWatcher(Mockito.isA(String.class), Mockito.anyInt()); @@ -110,9 +104,9 @@ public class TestFileSystemReceptionHandler { } @Test - public void testMain() throws IOException, PolicyDecodingException { + public void testMain() throws IOException { final Object lock = new Object(); - final String watchPath = tempFolder.getRoot().getAbsolutePath().toString(); + final String watchPath = tempFolder.getRoot().getAbsolutePath(); class Processed { public boolean processed = false; @@ -121,15 +115,12 @@ public class TestFileSystemReceptionHandler { final Processed cond = new Processed(); final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(final InvocationOnMock invocation) { - synchronized (lock) { - cond.processed = true; - lock.notifyAll(); - } - return null; + Mockito.doAnswer((Answer) invocation -> { + synchronized (lock) { + cond.processed = true; + lock.notifyAll(); } + return null; }).when(sypHandler).createPolicyInputAndCallHandler(Mockito.isA(String.class)); final Thread th = new Thread(() -> { diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyDecoder.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyDecoder.java index f8b87f82..6c9b8bd0 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyDecoder.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyDecoder.java @@ -3,6 +3,7 @@ * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. + * Modifications Copyright (C) 2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,12 +23,11 @@ package org.onap.policy.distribution.reception.handling.sdc; -import java.util.Arrays; import java.util.Collection; +import java.util.List; import org.onap.policy.distribution.model.Csar; import org.onap.policy.distribution.model.PolicyInput; import org.onap.policy.distribution.reception.decoding.PolicyDecoder; -import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; /** * Class to create a dummy decoder for test cases. @@ -50,10 +50,10 @@ public class DummyDecoder implements PolicyDecoder { * {@inheritDoc}. */ @Override - public Collection decode(final Csar input) throws PolicyDecodingException { + public Collection decode(final Csar input) { final DummyPolicy dummyPolicy = new DummyPolicy(input.getCsarFilePath()); decodedPolicy = dummyPolicy; - return Arrays.asList(dummyPolicy); + return List.of(dummyPolicy); } /** diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandler.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandler.java index 58933c1f..3e8f4a63 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandler.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandler.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Intel. All rights reserved. - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019, 2022 Nordix Foundation. * Modifications Copyright (C) 2020 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,6 +24,7 @@ package org.onap.policy.distribution.reception.handling.sdc; import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; @@ -33,9 +34,9 @@ import java.io.FileReader; import java.io.IOException; import java.lang.reflect.Field; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.junit.After; import org.junit.Before; @@ -92,19 +93,16 @@ public class TestSdcReceptionHandler { /** * Setup for the test cases. * - * @throws IOException if it occurs - * @throws SecurityException if it occurs - * @throws NoSuchFieldException if it occurs - * @throws IllegalAccessException if it occurs + * @throws IOException if it occurs + * @throws SecurityException if it occurs * @throws IllegalArgumentException if it occurs */ @Before - public final void init() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, - IllegalAccessException { + public final void init() throws IOException, SecurityException, IllegalArgumentException { DistributionStatisticsManager.resetAllStatistics(); final Gson gson = new GsonBuilder().create(); pssdConfigParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdc.json"), - SdcReceptionHandlerConfigurationParameterGroup.class); + SdcReceptionHandlerConfigurationParameterGroup.class); ParameterService.register(pssdConfigParameters); final SdcReceptionHandler sdcHandler = new SdcReceptionHandler(); sypHandler = Mockito.spy(sdcHandler); @@ -120,15 +118,15 @@ public class TestSdcReceptionHandler { Mockito.when(distributionClient.sendDeploymentStatus(any())).thenReturn(successfulClientInitResult); Mockito.when(distributionClient.sendDeploymentStatus(any(), any())).thenReturn(successfulClientInitResult); Mockito.when(distributionClient.download(any())).thenReturn(successfulClientDownloadResult); - Mockito.when(notificationData.getServiceArtifacts()).thenReturn(Arrays.asList(artifactInfo)); + Mockito.when(notificationData.getServiceArtifacts()).thenReturn(List.of(artifactInfo)); Mockito.when(artifactInfo.getArtifactName()).thenReturn(DUMMY_SERVICE_CSAR); Mockito.when(successfulClientDownloadResult.getArtifactPayload()).thenReturn(new byte[1]); Mockito.when(successfulClientInitResult.getDistributionActionResult()) - .thenReturn(DistributionActionResultEnum.SUCCESS); + .thenReturn(DistributionActionResultEnum.SUCCESS); Mockito.when(successfulClientDownloadResult.getDistributionActionResult()) - .thenReturn(DistributionActionResultEnum.SUCCESS); + .thenReturn(DistributionActionResultEnum.SUCCESS); Mockito.when(failureClientInitResult.getDistributionActionResult()) - .thenReturn(DistributionActionResultEnum.FAIL); + .thenReturn(DistributionActionResultEnum.FAIL); } @@ -139,7 +137,7 @@ public class TestSdcReceptionHandler { @Test public final void testInitializeSdcClient() { - assertThatCode(() -> sypHandler.initializeReception(pssdConfigParameters.getName())) + assertThatCode(() -> sypHandler.initializeReception(pssdConfigParameters.getName())) .doesNotThrowAnyException(); } @@ -147,7 +145,7 @@ public class TestSdcReceptionHandler { public final void testInitializeSdcClient_Failure() { Mockito.when(successfulClientInitResult.getDistributionActionResult()) - .thenReturn(DistributionActionResultEnum.FAIL).thenReturn(DistributionActionResultEnum.SUCCESS); + .thenReturn(DistributionActionResultEnum.FAIL).thenReturn(DistributionActionResultEnum.SUCCESS); assertThatCode(() -> sypHandler.initializeReception(pssdConfigParameters.getName())) .doesNotThrowAnyException(); } @@ -156,7 +154,7 @@ public class TestSdcReceptionHandler { public final void testStartSdcClient_Failure() { assertThatCode(() -> { Mockito.when(distributionClient.start()).thenReturn(failureClientInitResult) - .thenReturn(successfulClientInitResult); + .thenReturn(successfulClientInitResult); sypHandler.initializeReception(pssdConfigParameters.getName()); }).doesNotThrowAnyException(); } @@ -170,10 +168,10 @@ public class TestSdcReceptionHandler { } @Test - public final void testStopSdcClient_Failure() throws PluginInitializationException { + public final void testStopSdcClient_Failure() { sypHandler.initializeReception(pssdConfigParameters.getName()); Mockito.when(distributionClient.stop()).thenReturn(failureClientInitResult) - .thenReturn(successfulClientInitResult); + .thenReturn(successfulClientInitResult); assertThatCode(() -> sypHandler.destroy()).doesNotThrowAnyException(); } @@ -184,7 +182,7 @@ public class TestSdcReceptionHandler { @Test public void testNotificationCallBack() throws NoSuchFieldException, SecurityException, IllegalArgumentException, - IllegalAccessException, PluginInitializationException { + IllegalAccessException, PluginInitializationException { final DummyDecoder policyDecoder = new DummyDecoder(); final Collection> policyDecoders = new ArrayList<>(); @@ -211,10 +209,10 @@ public class TestSdcReceptionHandler { @Test public void testDownloadArtifactFailure() throws NoSuchFieldException, SecurityException, IllegalArgumentException, - IllegalAccessException, PluginInitializationException { + IllegalAccessException, PluginInitializationException { Mockito.when(successfulClientDownloadResult.getDistributionActionResult()) - .thenReturn(DistributionActionResultEnum.FAIL); + .thenReturn(DistributionActionResultEnum.FAIL); final DummyDecoder policyDecoder = new DummyDecoder(); final Collection> policyDecoders = new ArrayList<>(); @@ -228,7 +226,7 @@ public class TestSdcReceptionHandler { sypHandler.initializeReception(pssdConfigParameters.getName()); sypHandler.activateCallback(notificationData); - assertEquals(null, policyDecoder.getDecodedPolicy()); + assertNull(policyDecoder.getDecodedPolicy()); assertEquals(0, policyForwarder.getNumberOfPoliciesReceived()); assertEquals(1, DistributionStatisticsManager.getTotalDistributionCount()); assertEquals(0, DistributionStatisticsManager.getDistributionSuccessCount()); @@ -240,10 +238,10 @@ public class TestSdcReceptionHandler { @Test public void testSendDistributionStatusFailure() throws NoSuchFieldException, SecurityException, - IllegalArgumentException, IllegalAccessException, PluginInitializationException { + IllegalArgumentException, IllegalAccessException, PluginInitializationException { Mockito.when(successfulClientDownloadResult.getDistributionActionResult()) - .thenReturn(DistributionActionResultEnum.FAIL); + .thenReturn(DistributionActionResultEnum.FAIL); Mockito.when(distributionClient.sendDownloadStatus(any(), any())).thenReturn(failureClientInitResult); Mockito.when(distributionClient.sendDeploymentStatus(any(), any())).thenReturn(failureClientInitResult); Mockito.when(distributionClient.sendComponentDoneStatus(any(), any())).thenReturn(failureClientInitResult); @@ -260,14 +258,15 @@ public class TestSdcReceptionHandler { sypHandler.initializeReception(pssdConfigParameters.getName()); sypHandler.activateCallback(notificationData); - assertEquals(null, policyDecoder.getDecodedPolicy()); + assertNull(policyDecoder.getDecodedPolicy()); assertEquals(0, policyForwarder.getNumberOfPoliciesReceived()); } private void setUpPlugins(final AbstractReceptionHandler receptionHandler, - final Collection> decoders, final Collection forwarders) - throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, - PluginInitializationException { + final Collection> decoders, + final Collection forwarders) + throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, + PluginInitializationException { final PluginHandlerParameters pluginParameters = getPluginHandlerParameters(); pluginParameters.setName("DummyDistributionGroup"); ParameterService.register(pluginParameters); @@ -290,24 +289,21 @@ public class TestSdcReceptionHandler { private PluginHandlerParameters getPluginHandlerParameters() { final Map policyDecoders = getPolicyDecoders(); final Map policyForwarders = getPolicyForwarders(); - final PluginHandlerParameters pluginHandlerParameters = - new PluginHandlerParameters(policyDecoders, policyForwarders); - return pluginHandlerParameters; + return new PluginHandlerParameters(policyDecoders, policyForwarders); } private Map getPolicyDecoders() { - final Map policyDecoders = new HashMap(); + final Map policyDecoders = new HashMap<>(); final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters("DummyDecoder", - "org.onap.policy.distribution.reception.handling.sdc.DummyDecoder", "DummyDecoderConfiguration"); + "org.onap.policy.distribution.reception.handling.sdc.DummyDecoder", "DummyDecoderConfiguration"); policyDecoders.put("DummyDecoderKey", pDParameters); return policyDecoders; } private Map getPolicyForwarders() { - final Map policyForwarders = - new HashMap(); + final Map policyForwarders = new HashMap<>(); final PolicyForwarderParameters pFParameters = new PolicyForwarderParameters("DummyForwarder", - "org.onap.policy.distribution.reception.handling.sdc.DummyPolicyForwarder", "DummyConfiguration"); + "org.onap.policy.distribution.reception.handling.sdc.DummyPolicyForwarder", "DummyConfiguration"); policyForwarders.put("DummyForwarderKey", pFParameters); return policyForwarders; } diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/util/ReceptionUtilTest.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/util/ReceptionUtilTest.java deleted file mode 100644 index 6747618e..00000000 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/util/ReceptionUtilTest.java +++ /dev/null @@ -1,40 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2022 Nordix Foundation. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.distribution.reception.util; - -import org.assertj.core.api.Assertions; -import org.junit.Test; - -/** - * Class for testing {@link ReceptionUtil}. - */ - -public class ReceptionUtilTest { - - @Test - public void testValidateZipEntry_InvalidSize() { - long invalidFileSize = 512L * 2048; - - Assertions.assertThatThrownBy(() -> - ReceptionUtil.validateZipEntry("entryName", "csarPath", invalidFileSize)) - .hasMessage("Zip entry for entryName is too large " + invalidFileSize); - } -} -- cgit 1.2.3-korg