Replaced try/catch blocks in policy/distribution test cases with assertj assertions
Issue-ID: POLICY-2451
Change-Id: Ib7ecd13e39abd471bf1424d8a3db22c72ad1e3b8
Signed-off-by: waynedunican <wayne.dunican@est.tech>
* ============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.
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;
commonTestData.getPolicyForwarderConfigurationParameters(false);
final Map<String, PolicyDecoderConfigurationParameterGroup> 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
/*-
* ============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.
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;
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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");
}
}
/*-
* ============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.
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;
@Test
public void testPluginHandlerParameters_NullPolicyDecoders() {
- try {
- final Map<String, PolicyForwarderParameters> 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<String, PolicyForwarderParameters> 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<String, PolicyDecoderParameters> 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<String, PolicyDecoderParameters> policyDecoders = commonTestData.getPolicyDecoders(false);
+ final PluginHandlerParameters pHParameters = new PluginHandlerParameters(policyDecoders, null);
+ assertThatThrownBy(pHParameters::validate).isInstanceOf(ParameterRuntimeException.class)
+ .hasMessage("map parameter \"policyForwarders\" is null");
}
@Test
* 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.
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;
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.
*/
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();
@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
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() {
* 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.
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;
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}.
*/
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);
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
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() {
/*-
* ============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");
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;
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.
*/
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";
@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() {
* 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.
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;
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.
*/
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() {
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Intel Corp. All rights reserved.
* Modifications Copyright (C) 2019-2020 AT&T Intellectual Property.
- * 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.
package org.onap.policy.distribution.forwarding.file;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
import java.io.IOException;
import java.nio.file.Files;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.policy.common.parameters.ParameterService;
+import org.onap.policy.distribution.forwarding.PolicyForwardingException;
import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
}
@Test
- public void testForwardPolicy() {
+ public void testForwardPolicy() throws PolicyForwardingException {
final Collection<ToscaEntity> policies = new ArrayList<>();
final ToscaPolicy policy = createPolicy(policies, "test", "test");
final FilePolicyForwarder forwarder = new FilePolicyForwarder();
forwarder.configure(GROUP_NAME);
- try {
- forwarder.forward(policies);
- final Path path = Paths.get(tempFolder.getRoot().getAbsolutePath().toString(), policy.getName());
- assertTrue(Files.exists(path));
- } catch (final Exception exp) {
- fail("Test must not throw an exception");
- }
+ forwarder.forward(policies);
+ final Path path = Paths.get(tempFolder.getRoot().getAbsolutePath().toString(), policy.getName());
+ assertTrue(Files.exists(path));
}
@Test
final FilePolicyForwarder forwarder = new FilePolicyForwarder();
forwarder.configure(GROUP_NAME);
- try {
- forwarder.forward(policies);
- fail("Test must throw an exception");
- } catch (final Exception exp) {
- assertTrue(exp.getMessage().contains("Error sending policy"));
- }
+ assertThatThrownBy(() -> forwarder.forward(policies)).isInstanceOf(PolicyForwardingException.class)
+ .hasMessageContaining("Error sending policy");
}
@Test
final ToscaEntity policy = new UnsupportedPolicy();
policies.add(policy);
- try {
- forwarder.forward(policies);
- fail("Test must throw an exception");
- } catch (final Exception exp) {
- assertTrue(exp.getMessage().contains("Cannot forward policy"));
- }
+ assertThatThrownBy(() -> forwarder.forward(policies)).isInstanceOf(PolicyForwardingException.class)
+ .hasMessageContaining("Cannot forward policy");
}
class UnsupportedPolicy extends ToscaEntity {
/*-
* ============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.
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;
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;
}
@Test
- public void testDecodePolicy() {
+ public void testDecodePolicy() throws PolicyDecodingException {
final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy();
decoder.configure(PolicyDecoderFileInCsarToPolicyParameterGroup.class.getSimpleName());
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<ToscaEntity> 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<ToscaEntity> policyHolders = decoder.decode(csar);
+ assertEquals(2, policyHolders.size());
}
@Test
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");
}
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");
}
}
* ============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.
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;
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
* ============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.
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;
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());
@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
* ============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.
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;
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}.
@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
@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
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
/*-
* ============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.
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;
@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);
@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