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 --- .../parameters/TestDistributionParameterGroup.java | 20 +- .../TestDistributionParameterHandler.java | 248 +++++++++------------ .../parameters/TestPluginHandlerParameters.java | 28 +-- .../main/rest/TestDistributionRestServer.java | 21 +- .../main/rest/TestDistributionStatistics.java | 22 +- .../main/rest/TestHttpsDistributionRestServer.java | 14 +- .../TestHttpsStatisticDistributionRestServer.java | 18 +- 7 files changed, 141 insertions(+), 230 deletions(-) (limited to 'main') diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterGroup.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterGroup.java index 317b1a13..3c206261 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterGroup.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterGroup.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * 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,15 +22,17 @@ package org.onap.policy.distribution.main.parameters; +import static org.assertj.core.api.Assertions.assertThatThrownBy; 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 java.io.IOException; import java.util.Map; import org.junit.Test; import org.onap.policy.common.endpoints.parameters.RestServerParameters; import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ParameterRuntimeException; import org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup; import org.onap.policy.distribution.main.testclasses.DummyReceptionHandlerParameterGroup; import org.onap.policy.distribution.reception.parameters.PolicyDecoderConfigurationParameterGroup; @@ -177,16 +180,11 @@ public class TestDistributionParameterGroup { commonTestData.getPolicyForwarderConfigurationParameters(false); final Map decoderConfigurations = commonTestData.getPolicyDecoderConfigurationParameters(false); - try { - final DistributionParameterGroup distributionParameters = - new DistributionParameterGroup(CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, null, - receptionHandlerConfigurations, forwarderConfigurations, decoderConfigurations); - distributionParameters.validate(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().contains("map parameter \"receptionHandlerParameters\" is null")); - } - + final DistributionParameterGroup distributionParameters = + new DistributionParameterGroup(CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, null, + receptionHandlerConfigurations, forwarderConfigurations, decoderConfigurations); + assertThatThrownBy(distributionParameters::validate).isInstanceOf(ParameterRuntimeException.class) + .hasMessageContaining("map parameter \"receptionHandlerParameters\" is null"); } @Test diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterHandler.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterHandler.java index f97f2713..a789569b 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterHandler.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterHandler.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * 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. @@ -20,14 +21,15 @@ package org.onap.policy.distribution.main.parameters; +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.IOException; import java.nio.file.Files; import java.nio.file.Paths; import org.junit.Test; +import org.onap.policy.common.parameters.ParameterRuntimeException; import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.main.startstop.DistributionCommandLineArguments; import org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup; @@ -46,13 +48,9 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments noArguments = new DistributionCommandLineArguments(); noArguments.parse(noArgumentString); - - try { - new DistributionParameterHandler().getParameters(noArguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().contains("FileNotFoundException")); - } + assertThatThrownBy(() -> + new DistributionParameterHandler().getParameters(noArguments) + ).isInstanceOf(PolicyDistributionException.class).hasMessageContaining("FileNotFoundException"); } @Test @@ -63,12 +61,10 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments emptyArguments = new DistributionCommandLineArguments(); emptyArguments.parse(emptyArgumentString); - try { - new DistributionParameterHandler().getParameters(emptyArguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("no parameters found in \"parameters/EmptyParameters.json\"", e.getMessage()); - } + assertThatThrownBy(() -> + new DistributionParameterHandler().getParameters(emptyArguments) + ).isInstanceOf(PolicyDistributionException.class) + .hasMessageContaining("no parameters found in \"parameters/EmptyParameters.json\""); } @Test @@ -79,14 +75,12 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments badArguments = new DistributionCommandLineArguments(); badArguments.parse(badArgumentString); - try { - new DistributionParameterHandler().getParameters(badArguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("error reading parameters from \"parameters/BadParameters.json\"\n" - + "(JsonSyntaxException):java.lang.IllegalStateException: " - + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name", e.getMessage()); - } + assertThatThrownBy(() -> + new DistributionParameterHandler().getParameters(badArguments) + ).isInstanceOf(PolicyDistributionException.class) + .hasMessageContaining("error reading parameters from \"parameters/BadParameters.json\"\n" + + "(JsonSyntaxException):java.lang.IllegalStateException: " + + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name"); } @Test @@ -97,14 +91,12 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments invalidArguments = new DistributionCommandLineArguments(); invalidArguments.parse(invalidArgumentString); - try { - new DistributionParameterHandler().getParameters(invalidArguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("error reading parameters from \"parameters/InvalidParameters.json\"\n" - + "(JsonSyntaxException):java.lang.IllegalStateException: " - + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name", e.getMessage()); - } + assertThatThrownBy(() -> + new DistributionParameterHandler().getParameters(invalidArguments) + ).isInstanceOf(PolicyDistributionException.class) + .hasMessageContaining("error reading parameters from \"parameters/InvalidParameters.json\"\n" + + "(JsonSyntaxException):java.lang.IllegalStateException: " + + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name"); } @Test @@ -115,12 +107,10 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments noArguments = new DistributionCommandLineArguments(); noArguments.parse(noArgumentString); - try { - new DistributionParameterHandler().getParameters(noArguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("map parameter \"receptionHandlerParameters\" is null", e.getMessage()); - } + assertThatThrownBy(() -> + new DistributionParameterHandler().getParameters(noArguments) + ).isInstanceOf(ParameterRuntimeException.class) + .hasMessageContaining("map parameter \"receptionHandlerParameters\" is null"); } @Test @@ -176,12 +166,10 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); - try { - new DistributionParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().contains("parameter \"parameterClassName\" value \"\" invalid in JSON file")); - } + assertThatThrownBy(() -> + new DistributionParameterHandler().getParameters(arguments) + ).isInstanceOf(PolicyDistributionException.class) + .hasMessageContaining("parameter \"parameterClassName\" value \"\" invalid in JSON file"); } @Test @@ -193,13 +181,11 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); - try { - new DistributionParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().contains( - "parameter \"parameterClassName\" value \"org.onap.policy.Unknown\", could not find class")); - } + assertThatThrownBy(() -> + new DistributionParameterHandler().getParameters(arguments) + ).isInstanceOf(PolicyDistributionException.class) + .hasMessageContaining("parameter \"parameterClassName\" value \"org.onap.policy.Unknown\", " + + "could not find class"); } @Test @@ -210,13 +196,11 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); - try { - new DistributionParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().contains( - "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string")); - } + assertThatThrownBy(() -> + new DistributionParameterHandler().getParameters(arguments) + ).isInstanceOf(PolicyDistributionException.class) + .hasMessageContaining("field \"name\" type \"java.lang.String\" value \" " + + "\" INVALID, must be a non-blank string"); } @Test @@ -227,12 +211,10 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); - try { - new DistributionParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().contains("map parameter \"receptionHandlerParameters\" is null")); - } + assertThatThrownBy(() -> + new DistributionParameterHandler().getParameters(arguments) + ).isInstanceOf(ParameterRuntimeException.class) + .hasMessageContaining("map parameter \"receptionHandlerParameters\" is null"); } @Test @@ -243,12 +225,10 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); - try { - new DistributionParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().contains("must have at least one reception handler\n")); - } + assertThatThrownBy(() -> + new DistributionParameterHandler().getParameters(arguments) + ).isInstanceOf(PolicyDistributionException.class) + .hasMessageContaining("must have at least one reception handler\n"); } @Test @@ -259,12 +239,10 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); - try { - new DistributionParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().contains("map parameter \"policyDecoders\" is null")); - } + assertThatThrownBy(() -> + new DistributionParameterHandler().getParameters(arguments) + ).isInstanceOf(ParameterRuntimeException.class) + .hasMessageContaining("map parameter \"policyDecoders\" is null"); } @Test @@ -275,12 +253,10 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); - try { - new DistributionParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().contains("map parameter \"policyForwarders\" is null")); - } + assertThatThrownBy(() -> + new DistributionParameterHandler().getParameters(arguments) + ).isInstanceOf(ParameterRuntimeException.class) + .hasMessageContaining("map parameter \"policyForwarders\" is null"); } @Test @@ -291,12 +267,10 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); - try { - new DistributionParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().contains("must have at least one policy decoder\n")); - } + assertThatThrownBy(() -> + new DistributionParameterHandler().getParameters(arguments) + ).isInstanceOf(PolicyDistributionException.class) + .hasMessageContaining("must have at least one policy decoder\n"); } @Test @@ -307,12 +281,10 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); - try { - new DistributionParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().endsWith("must have at least one policy forwarder\n")); - } + assertThatThrownBy(() -> + new DistributionParameterHandler().getParameters(arguments) + ).isInstanceOf(PolicyDistributionException.class) + .hasMessageContaining("must have at least one policy forwarder\n"); } @Test @@ -324,15 +296,13 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); - try { - new DistributionParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - final String expectedResult = new String(Files.readAllBytes( - Paths.get("src/test/resources/expectedValidationResults/InvalidReceptionHandlerParameters.txt"))) - .replaceAll("\\s+", ""); - assertEquals(expectedResult, e.getMessage().replaceAll("\\s+", "")); - } + String resultString = Files.readString(Paths.get( + "src/test/resources/expectedValidationResults/InvalidReceptionHandlerParameters.txt")) + .trim().replaceAll("\\r\\n", "\\\n"); + assertThatThrownBy(() -> + new DistributionParameterHandler().getParameters(arguments) + ).isInstanceOf(PolicyDistributionException.class) + .hasMessageContaining(resultString); } @Test @@ -343,16 +313,12 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); - - try { - new DistributionParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - final String expectedResult = new String(Files.readAllBytes( - Paths.get("src/test/resources/expectedValidationResults/InvalidDecoderAndForwarderParameters.txt"))) - .replaceAll("\\s+", ""); - assertEquals(expectedResult, e.getMessage().replaceAll("\\s+", "")); - } + String resultString = new String(Files.readString(Paths.get( + "src/test/resources/expectedValidationResults/InvalidDecoderAndForwarderParameters.txt")) + .trim().replaceAll("\\r\\n", "\\\n")); + assertThatThrownBy(() -> new DistributionParameterHandler().getParameters(arguments)) + .isInstanceOf(PolicyDistributionException.class) + .hasMessageContaining((resultString)); } @Test @@ -364,15 +330,12 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); - try { - new DistributionParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - final String expectedResult = new String(Files.readAllBytes( - Paths.get("src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt"))) - .replaceAll("\\s+", ""); - assertEquals(expectedResult, e.getMessage().replaceAll("\\s+", "")); - } + String resultString = new String(Files.readString(Paths.get( + "src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt")) + .trim().replaceAll("\\r\\n", "\\\n")); + assertThatThrownBy(() -> new DistributionParameterHandler().getParameters(arguments)) + .isInstanceOf(PolicyDistributionException.class) + .hasMessageContaining(resultString); } @Test @@ -398,11 +361,10 @@ public class TestDistributionParameterHandler { final String[] distributionConfigParameters = { "-d" }; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); - try { - arguments.parse(distributionConfigParameters); - } catch (final Exception exp) { - assertTrue(exp.getMessage().startsWith("invalid command line arguments specified")); - } + assertThatThrownBy(() -> + arguments.parse(distributionConfigParameters) + ).isInstanceOf(PolicyDistributionException.class) + .hasMessageContaining("invalid command line arguments specified"); } @Test @@ -413,11 +375,9 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); - try { - new DistributionParameterHandler().getParameters(arguments); - } catch (final Exception e) { - assertTrue(e.getMessage().contains("could not find class")); - } + assertThatThrownBy(() -> new DistributionParameterHandler().getParameters(arguments)) + .isInstanceOf(PolicyDistributionException.class) + .hasMessageContaining("could not find class"); } @Test @@ -428,11 +388,9 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); - try { - new DistributionParameterHandler().getParameters(arguments); - } catch (final Exception e) { - assertTrue(e.getMessage().contains("invalid in JSON file")); - } + assertThatThrownBy(() -> new DistributionParameterHandler().getParameters(arguments)) + .isInstanceOf(PolicyDistributionException.class) + .hasMessageContaining("invalid in JSON file"); } @Test @@ -444,12 +402,9 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); - try { - new DistributionParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().contains("parameter \"parameterClassName\" value \"\" invalid in JSON file")); - } + assertThatThrownBy(() -> new DistributionParameterHandler().getParameters(arguments)) + .isInstanceOf(PolicyDistributionException.class) + .hasMessageContaining("parameter \"parameterClassName\" value \"\" invalid in JSON file"); } @Test @@ -461,12 +416,9 @@ public class TestDistributionParameterHandler { final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); - try { - new DistributionParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().contains( - "parameter \"parameterClassName\" value \"org.onap.policy.Unknown\", could not find class")); - } + assertThatThrownBy(() -> new DistributionParameterHandler().getParameters(arguments)) + .isInstanceOf(PolicyDistributionException.class) + .hasMessageContaining("parameter \"parameterClassName\" value" + + " \"org.onap.policy.Unknown\", could not find class"); } } diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPluginHandlerParameters.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPluginHandlerParameters.java index 9eeae354..73ec0ba3 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPluginHandlerParameters.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPluginHandlerParameters.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * 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. @@ -20,14 +21,15 @@ package org.onap.policy.distribution.main.parameters; +import static org.assertj.core.api.Assertions.assertThatThrownBy; 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 java.util.Map; import org.junit.Test; import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ParameterRuntimeException; import org.onap.policy.distribution.forwarding.parameters.PolicyForwarderParameters; import org.onap.policy.distribution.reception.parameters.PluginHandlerParameters; import org.onap.policy.distribution.reception.parameters.PolicyDecoderParameters; @@ -55,26 +57,18 @@ public class TestPluginHandlerParameters { @Test public void testPluginHandlerParameters_NullPolicyDecoders() { - try { - final Map policyForwarders = commonTestData.getPolicyForwarders(false); - final PluginHandlerParameters pHParameters = new PluginHandlerParameters(null, policyForwarders); - pHParameters.validate(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().contains("map parameter \"policyDecoders\" is null")); - } + final Map policyForwarders = commonTestData.getPolicyForwarders(false); + final PluginHandlerParameters pHParameters = new PluginHandlerParameters(null, policyForwarders); + assertThatThrownBy(pHParameters::validate).isInstanceOf(ParameterRuntimeException.class) + .hasMessage("map parameter \"policyDecoders\" is null"); } @Test public void testPluginHandlerParameters_NullPolicyForwarders() { - try { - final Map policyDecoders = commonTestData.getPolicyDecoders(false); - final PluginHandlerParameters pHParameters = new PluginHandlerParameters(policyDecoders, null); - pHParameters.validate(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().contains("map parameter \"policyForwarders\" is null")); - } + final Map policyDecoders = commonTestData.getPolicyDecoders(false); + final PluginHandlerParameters pHParameters = new PluginHandlerParameters(policyDecoders, null); + assertThatThrownBy(pHParameters::validate).isInstanceOf(ParameterRuntimeException.class) + .hasMessage("map parameter \"policyForwarders\" is null"); } @Test diff --git a/main/src/test/java/org/onap/policy/distribution/main/rest/TestDistributionRestServer.java b/main/src/test/java/org/onap/policy/distribution/main/rest/TestDistributionRestServer.java index 5adc0bdf..62bd8aad 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/rest/TestDistributionRestServer.java +++ b/main/src/test/java/org/onap/policy/distribution/main/rest/TestDistributionRestServer.java @@ -3,7 +3,7 @@ * Copyright (C) 2018 Ericsson. All rights reserved. * Copyright (C) 2019 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. 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. @@ -23,9 +23,9 @@ package org.onap.policy.distribution.main.rest; +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 java.io.IOException; import javax.ws.rs.client.Client; @@ -43,8 +43,6 @@ import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.main.parameters.CommonTestData; import org.onap.policy.distribution.main.startstop.Main; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Class to perform unit test of HealthCheckMonitor. @@ -53,7 +51,6 @@ import org.slf4j.LoggerFactory; */ public class TestDistributionRestServer { - private static final Logger LOGGER = LoggerFactory.getLogger(TestDistributionRestServer.class); private static final String NOT_ALIVE = "not alive"; private static final String ALIVE = "alive"; private static final String SELF = NetworkUtil.getHostname(); @@ -62,15 +59,12 @@ public class TestDistributionRestServer { @Test public void testHealthCheckSuccess() { final String reportString = "Report [name=Policy SSD, url=" + SELF + ", healthy=true, code=200, message=alive]"; - try { + assertThatCode(() -> { final Main main = startDistributionService(); final HealthCheckReport report = performHealthCheck(); validateReport(NAME, SELF, true, 200, ALIVE, reportString, report); stopDistributionService(main); - } catch (final Exception exp) { - LOGGER.error("testHealthCheckSuccess failed", exp); - fail("Test should not throw an exception"); - } + }).doesNotThrowAnyException(); } @Test @@ -80,17 +74,14 @@ public class TestDistributionRestServer { final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); restServerParams.setName(CommonTestData.DISTRIBUTION_GROUP_NAME); final RestServer restServer = new RestServer(restServerParams, null, DistributionRestController.class); - try { + assertThatCode(() -> { restServer.start(); final HealthCheckReport report = performHealthCheck(); validateReport(NAME, SELF, false, 500, NOT_ALIVE, reportString, report); assertTrue(restServer.isAlive()); assertTrue(restServer.toString().startsWith("RestServer [servers=")); restServer.shutdown(); - } catch (final Exception exp) { - LOGGER.error("testHealthCheckFailure failed", exp); - fail("Test should not throw an exception"); - } + }).doesNotThrowAnyException(); } private Main startDistributionService() { diff --git a/main/src/test/java/org/onap/policy/distribution/main/rest/TestDistributionStatistics.java b/main/src/test/java/org/onap/policy/distribution/main/rest/TestDistributionStatistics.java index ebaa18b9..f394e02b 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/rest/TestDistributionStatistics.java +++ b/main/src/test/java/org/onap/policy/distribution/main/rest/TestDistributionStatistics.java @@ -3,6 +3,7 @@ * Copyright (C) 2018 Ericsson. All rights reserved. * Copyright (C) 2019 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * 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. @@ -22,8 +23,8 @@ package org.onap.policy.distribution.main.rest; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; import java.io.IOException; import javax.ws.rs.client.Client; @@ -41,8 +42,6 @@ import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.main.parameters.CommonTestData; import org.onap.policy.distribution.main.startstop.Main; import org.onap.policy.distribution.reception.statistics.DistributionStatisticsManager; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Class to perform unit test of {@link DistributionRestController}. @@ -51,12 +50,9 @@ import org.slf4j.LoggerFactory; */ public class TestDistributionStatistics { - private static final Logger LOGGER = LoggerFactory.getLogger(TestDistributionStatistics.class); - - @Test public void testDistributionStatistics_200() { - try { + assertThatCode(() -> { final Main main = startDistributionService(); StatisticsReport report = getDistributionStatistics(); validateReport(report, 0, 200); @@ -65,10 +61,7 @@ public class TestDistributionStatistics { validateReport(report, 1, 200); stopDistributionService(main); DistributionStatisticsManager.resetAllStatistics(); - } catch (final Exception exp) { - LOGGER.error("testDistributionStatistics_200 failed", exp); - fail("Test should not throw an exception"); - } + }).doesNotThrowAnyException(); } @Test @@ -76,16 +69,13 @@ public class TestDistributionStatistics { final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); restServerParams.setName(CommonTestData.DISTRIBUTION_GROUP_NAME); final RestServer restServer = new RestServer(restServerParams, null, DistributionRestController.class); - try { + assertThatCode(() -> { restServer.start(); final StatisticsReport report = getDistributionStatistics(); validateReport(report, 0, 500); restServer.shutdown(); DistributionStatisticsManager.resetAllStatistics(); - } catch (final Exception exp) { - LOGGER.error("testDistributionStatistics_500 failed", exp); - fail("Test should not throw an exception"); - } + }).doesNotThrowAnyException(); } private Main startDistributionService() { diff --git a/main/src/test/java/org/onap/policy/distribution/main/rest/TestHttpsDistributionRestServer.java b/main/src/test/java/org/onap/policy/distribution/main/rest/TestHttpsDistributionRestServer.java index 0d511088..dd8d4516 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/rest/TestHttpsDistributionRestServer.java +++ b/main/src/test/java/org/onap/policy/distribution/main/rest/TestHttpsDistributionRestServer.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. * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,8 +22,8 @@ package org.onap.policy.distribution.main.rest; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; import java.security.SecureRandom; import java.security.cert.X509Certificate; @@ -42,8 +42,6 @@ import org.onap.policy.common.endpoints.report.HealthCheckReport; import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.main.startstop.Main; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Class to perform unit test of HealthCheckMonitor. @@ -52,7 +50,6 @@ import org.slf4j.LoggerFactory; */ public class TestHttpsDistributionRestServer { - private static final Logger LOGGER = LoggerFactory.getLogger(TestHttpsDistributionRestServer.class); private static final String ALIVE = "alive"; private static final String SELF = NetworkUtil.getHostname(); private static final String NAME = "Policy SSD"; @@ -61,15 +58,12 @@ public class TestHttpsDistributionRestServer { @Test public void testHttpsHealthCheckSuccess() { final String reportString = "Report [name=Policy SSD, url=" + SELF + ", healthy=true, code=200, message=alive]"; - try { + assertThatCode(() -> { final Main main = startDistributionService(); final HealthCheckReport report = performHealthCheck(); validateReport(NAME, SELF, true, 200, ALIVE, reportString, report); stopDistributionService(main); - } catch (final Exception exp) { - LOGGER.error("testHttpsHealthCheckSuccess failed", exp); - fail("Test should not throw an exception"); - } + }).doesNotThrowAnyException(); } private Main startDistributionService() { diff --git a/main/src/test/java/org/onap/policy/distribution/main/rest/TestHttpsStatisticDistributionRestServer.java b/main/src/test/java/org/onap/policy/distribution/main/rest/TestHttpsStatisticDistributionRestServer.java index 8d29c8ef..76462565 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/rest/TestHttpsStatisticDistributionRestServer.java +++ b/main/src/test/java/org/onap/policy/distribution/main/rest/TestHttpsStatisticDistributionRestServer.java @@ -3,6 +3,7 @@ * Copyright (C) 2018 Intel. All rights reserved. * Copyright (C) 2019 Nordix Foundation. * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * 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. @@ -22,11 +23,9 @@ package org.onap.policy.distribution.main.rest; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.cert.X509Certificate; import java.util.Properties; @@ -43,8 +42,6 @@ import org.junit.Test; import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.main.startstop.Main; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Class to perform unit test of HealthCheckMonitor. @@ -53,21 +50,16 @@ import org.slf4j.LoggerFactory; */ public class TestHttpsStatisticDistributionRestServer { - private static final Logger LOGGER = LoggerFactory.getLogger(TestHttpsStatisticDistributionRestServer.class); private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore"; @Test - public void testHttpsDistributionStatistic() - throws PolicyDistributionException, InterruptedException, KeyManagementException, NoSuchAlgorithmException { - try { + public void testHttpsDistributionStatistic() { + assertThatCode(() -> { final Main main = startDistributionService(); final StatisticsReport report = performStatisticCheck(); validateReport(200, 0, 0, 0, 0, 0, 0, report); stopDistributionService(main); - } catch (final Exception exp) { - LOGGER.error("testHttpsDistributionStatistic failed", exp); - fail("Test should not throw an exception"); - } + }).doesNotThrowAnyException(); } private Main startDistributionService() { -- cgit 1.2.3-korg