From ffd7bb269fd65df1b976f416a0502d665bd3aa61 Mon Sep 17 00:00:00 2001 From: waynedunican Date: Wed, 1 Jul 2020 08:39:47 +0100 Subject: Replace try/catch blocks with assertj - dist Replaced try/catch blocks in policy/distribution test cases with assertj assertions Issue-ID: POLICY-2451 Change-Id: Ib7ecd13e39abd471bf1424d8a3db22c72ad1e3b8 Signed-off-by: waynedunican --- .../file/PolicyDecoderFileInCsarToPolicyTest.java | 41 +++++----------- .../file/TestFileSystemReceptionHandler.java | 25 ++++------ ...eceptionHandlerConfigurationParameterGroup.java | 27 +++++------ .../handling/sdc/TestSdcReceptionHandler.java | 55 +++++----------------- ...eceptionHandlerConfigurationParameterGroup.java | 27 +++++------ 5 files changed, 57 insertions(+), 118 deletions(-) (limited to 'plugins/reception-plugins/src/test') 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 8e1585a0..05bd0780 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 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,12 +21,11 @@ package org.onap.policy.distribution.reception.decoding.policy.file; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.io.File; -import java.io.IOException; import java.util.Collection; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -34,8 +33,8 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.runners.MockitoJUnitRunner; import org.onap.policy.common.parameters.ParameterService; -import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.distribution.model.Csar; +import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; import org.onap.policy.distribution.reception.decoding.hpa.CommonTestData; import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; @@ -68,7 +67,7 @@ public class PolicyDecoderFileInCsarToPolicyTest { } @Test - public void testDecodePolicy() { + public void testDecodePolicy() throws PolicyDecodingException { final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy(); decoder.configure(PolicyDecoderFileInCsarToPolicyParameterGroup.class.getSimpleName()); @@ -76,13 +75,9 @@ public class PolicyDecoderFileInCsarToPolicyTest { final File file = new File("src/test/resources/service-Sampleservice.csar"); final Csar csar = new Csar(file.getAbsolutePath()); - try { - assertTrue(decoder.canHandle(csar)); - final Collection policyHolders = decoder.decode(csar); - assertEquals(2, policyHolders.size()); - } catch (final Exception exp) { - fail("Test must not throw an exception"); - } + assertTrue(decoder.canHandle(csar)); + final Collection policyHolders = decoder.decode(csar); + assertEquals(2, policyHolders.size()); } @Test @@ -94,14 +89,9 @@ public class PolicyDecoderFileInCsarToPolicyTest { final File file = new File("unknown.csar"); final Csar csar = new Csar(file.getAbsolutePath()); - try { - assertTrue(decoder.canHandle(csar)); - decoder.decode(csar); - fail("Test must throw an exception"); - } catch (final Exception exp) { - assertTrue(exp.getCause() instanceof IOException); - assertTrue(exp.getMessage().contains("Failed decoding the policy")); - } + assertTrue(decoder.canHandle(csar)); + assertThatThrownBy(() -> decoder.decode(csar)).isInstanceOf(PolicyDecodingException.class) + .hasMessageContaining("Failed decoding the policy"); } @@ -114,13 +104,8 @@ public class PolicyDecoderFileInCsarToPolicyTest { final File file = new File("src/test/resources/service-Sampleservice-test.csar"); final Csar csar = new Csar(file.getAbsolutePath()); - try { - assertTrue(decoder.canHandle(csar)); - decoder.decode(csar); - fail("Test must throw an exception"); - } catch (final Exception exp) { - assertTrue(exp.getCause() instanceof CoderException); - assertTrue(exp.getMessage().contains("Failed decoding the policy")); - } + assertTrue(decoder.canHandle(csar)); + assertThatThrownBy(() -> decoder.decode(csar)).isInstanceOf(PolicyDecodingException.class) + .hasMessageContaining("Failed decoding the policy"); } } 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 7f97c5cf..c9debdd9 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 @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Intel. All rights reserved. * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2020 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +22,7 @@ package org.onap.policy.distribution.reception.handling.file; -import static org.junit.Assert.fail; +import static org.assertj.core.api.Assertions.assertThatCode; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -93,27 +94,19 @@ public class TestFileSystemReceptionHandler { final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler); Mockito.doNothing().when(sypHandler).initFileWatcher(Mockito.isA(String.class), Mockito.anyInt()); - try { - sypHandler.initializeReception(pssdConfigParameters.getName()); - } catch (final Exception exp) { - LOGGER.error("testInit failed", exp); - fail("Test should not throw any exception"); - } + assertThatCode(() -> sypHandler.initializeReception(pssdConfigParameters.getName())) + .doesNotThrowAnyException(); } @Test public final void testDestroy() throws IOException { - try { - final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler); - Mockito.doNothing().when(sypHandler).initFileWatcher(Mockito.isA(String.class), - Mockito.anyInt()); + final FileSystemReceptionHandler sypHandler = Mockito.spy(fileSystemHandler); + Mockito.doNothing().when(sypHandler).initFileWatcher(Mockito.isA(String.class), + Mockito.anyInt()); + assertThatCode(() -> { sypHandler.initializeReception(pssdConfigParameters.getName()); sypHandler.destroy(); - } catch (final Exception exp) { - LOGGER.error("testDestroy failed", exp); - fail("Test should not throw any exception"); - } - + }).doesNotThrowAnyException(); } @Test diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/file/TestFileSystemReceptionHandlerConfigurationParameterGroup.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/file/TestFileSystemReceptionHandlerConfigurationParameterGroup.java index 6c57a132..03455b84 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/file/TestFileSystemReceptionHandlerConfigurationParameterGroup.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/file/TestFileSystemReceptionHandlerConfigurationParameterGroup.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Intel. All rights reserved. * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2020 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,7 +25,6 @@ package org.onap.policy.distribution.reception.handling.file; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -47,15 +47,13 @@ public class TestFileSystemReceptionHandlerConfigurationParameterGroup { public void testFileSystemConfiguration() throws IOException { FileSystemReceptionHandlerConfigurationParameterGroup configParameters = null; String validPath = null; - try { - validPath = tempFolder.getRoot().getAbsolutePath(); - configParameters = new FileSystemReceptionHandlerConfigurationParameterGroup(); - configParameters.setWatchPath(validPath); - configParameters.setMaxThread(2); - } catch (final Exception e) { - fail("test should not thrown an exception here: " + e.getMessage()); - } + validPath = tempFolder.getRoot().getAbsolutePath(); + + configParameters = new FileSystemReceptionHandlerConfigurationParameterGroup(); + configParameters.setWatchPath(validPath); + configParameters.setMaxThread(2); + final GroupValidationResult validationResult = configParameters.validate(); assertTrue(validationResult.isValid()); assertEquals(validPath, configParameters.getWatchPath()); @@ -65,15 +63,12 @@ public class TestFileSystemReceptionHandlerConfigurationParameterGroup { @Test public void testInvalidFileSystemConfiguration() throws IOException { FileSystemReceptionHandlerConfigurationParameterGroup configParameters = null; - try { - final Gson gson = new GsonBuilder().create(); - configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdcInvalid.json"), - FileSystemReceptionHandlerConfigurationParameterGroup.class); - } catch (final Exception e) { - fail("test should not thrown an exception here: " + e.getMessage()); - } + final Gson gson = new GsonBuilder().create(); + configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdcInvalid.json"), + FileSystemReceptionHandlerConfigurationParameterGroup.class); final GroupValidationResult validationResult = configParameters.validate(); assertFalse(validationResult.isValid()); + } @Test 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 f51b6da0..58933c1f 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 @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Intel. All rights reserved. * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2020 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,9 +22,9 @@ 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.assertTrue; -import static org.junit.Assert.fail; import static org.mockito.Matchers.any; import com.google.gson.Gson; @@ -60,8 +61,6 @@ import org.onap.sdc.api.notification.INotificationData; import org.onap.sdc.api.results.IDistributionClientDownloadResult; import org.onap.sdc.api.results.IDistributionClientResult; import org.onap.sdc.utils.DistributionActionResultEnum; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Class to perform unit test of {@link SdcReceptionHandler}. @@ -71,7 +70,6 @@ import org.slf4j.LoggerFactory; @RunWith(MockitoJUnitRunner.class) public class TestSdcReceptionHandler { - private static final Logger LOGGER = LoggerFactory.getLogger(TestSdcReceptionHandler.class); private static final String DUMMY_SERVICE_CSAR = "dummyService.csar"; @Mock @@ -141,12 +139,8 @@ public class TestSdcReceptionHandler { @Test public final void testInitializeSdcClient() { - try { - sypHandler.initializeReception(pssdConfigParameters.getName()); - } catch (final Exception exp) { - LOGGER.error("testInitializeSdcClient failed", exp); - fail("Test should not throw any exception"); - } + assertThatCode(() -> sypHandler.initializeReception(pssdConfigParameters.getName())) + .doesNotThrowAnyException(); } @Test @@ -154,61 +148,38 @@ public class TestSdcReceptionHandler { Mockito.when(successfulClientInitResult.getDistributionActionResult()) .thenReturn(DistributionActionResultEnum.FAIL).thenReturn(DistributionActionResultEnum.SUCCESS); - try { - sypHandler.initializeReception(pssdConfigParameters.getName()); - } catch (final Exception exp) { - LOGGER.error("testInitializeSdcClient_Failure failed", exp); - fail("Test should not throw any exception"); - } + assertThatCode(() -> sypHandler.initializeReception(pssdConfigParameters.getName())) + .doesNotThrowAnyException(); } @Test public final void testStartSdcClient_Failure() { - try { + assertThatCode(() -> { Mockito.when(distributionClient.start()).thenReturn(failureClientInitResult) - .thenReturn(successfulClientInitResult); + .thenReturn(successfulClientInitResult); sypHandler.initializeReception(pssdConfigParameters.getName()); - } catch (final Exception exp) { - LOGGER.error("testStartSdcClient_Failure failed", exp); - fail("Test should not throw any exception"); - } + }).doesNotThrowAnyException(); } @Test public final void testStopSdcClient() { - try { + assertThatCode(() -> { sypHandler.initializeReception(pssdConfigParameters.getName()); sypHandler.destroy(); - } catch (final Exception exp) { - LOGGER.error("testStopSdcClient failed", exp); - fail("Test should not throw any exception"); - } - + }).doesNotThrowAnyException(); } @Test public final void testStopSdcClient_Failure() throws PluginInitializationException { - sypHandler.initializeReception(pssdConfigParameters.getName()); Mockito.when(distributionClient.stop()).thenReturn(failureClientInitResult) .thenReturn(successfulClientInitResult); - try { - sypHandler.destroy(); - } catch (final Exception exp) { - LOGGER.error("testStopSdcClient_Failure failed", exp); - fail("Test should not throw any exception"); - } + assertThatCode(() -> sypHandler.destroy()).doesNotThrowAnyException(); } @Test public final void testStopSdcClientWithoutStart() { - try { - sypHandler.destroy(); - } catch (final Exception exp) { - LOGGER.error("testStopSdcClientWithoutStart", exp); - fail("Test should not throw any exception"); - } - + assertThatCode(() -> sypHandler.destroy()).doesNotThrowAnyException(); } @Test diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandlerConfigurationParameterGroup.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandlerConfigurationParameterGroup.java index 62a61eb5..d981369c 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandlerConfigurationParameterGroup.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandlerConfigurationParameterGroup.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Intel. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,7 +24,6 @@ package org.onap.policy.distribution.reception.handling.sdc; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -45,13 +44,10 @@ public class TestSdcReceptionHandlerConfigurationParameterGroup { @Test public void testSdcConfiguration() throws IOException { SdcReceptionHandlerConfigurationParameterGroup configParameters = null; - try { - final Gson gson = new GsonBuilder().create(); - configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdc.json"), - SdcReceptionHandlerConfigurationParameterGroup.class); - } catch (final Exception e) { - fail("test should not thrown an exception here: " + e.getMessage()); - } + final Gson gson = new GsonBuilder().create(); + configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdc.json"), + SdcReceptionHandlerConfigurationParameterGroup.class); + final GroupValidationResult validationResult = configParameters.validate(); assertTrue(validationResult.isValid()); final SdcConfiguration config = new SdcConfiguration(configParameters); @@ -75,15 +71,14 @@ public class TestSdcReceptionHandlerConfigurationParameterGroup { @Test public void testInvalidSdcConfiguration() throws IOException { SdcReceptionHandlerConfigurationParameterGroup configParameters = null; - try { - final Gson gson = new GsonBuilder().create(); - configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdcInvalid.json"), - SdcReceptionHandlerConfigurationParameterGroup.class); - } catch (final Exception e) { - fail("test should not thrown an exception here: " + e.getMessage()); - } + + final Gson gson = new GsonBuilder().create(); + configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdcInvalid.json"), + SdcReceptionHandlerConfigurationParameterGroup.class); + final GroupValidationResult validationResult = configParameters.validate(); assertFalse(validationResult.isValid()); + } @Test -- cgit 1.2.3-korg