From b5f4934afdc2601c925e83458d0e3d136c36cf1c Mon Sep 17 00:00:00 2001 From: ramverma Date: Tue, 5 Feb 2019 15:34:35 +0000 Subject: Change policy/distribution logging to slf4j 1) Changed logging from FlexLogger to SLF4J with logback 2) Fixed test cases of distribution REST endpoints having infinite while loop Change-Id: I1551873b3d348f9a7d2b4f8dfc3ae5dd6462bdb8 Issue-ID: POLICY-1346 Signed-off-by: ramverma --- main/pom.xml | 5 -- .../parameters/DistributionParameterHandler.java | 7 ++- ...orwarderConfigurationParametersJsonAdapter.java | 19 +++--- .../main/rest/DistributionRestServer.java | 7 ++- .../main/startstop/DistributionActivator.java | 9 +-- .../policy/distribution/main/startstop/Main.java | 9 +-- .../main/rest/TestDistributionRestServer.java | 59 ++++++++++-------- .../main/rest/TestDistributionStatistics.java | 70 ++++++++++++---------- .../main/rest/TestHttpsDistributionRestServer.java | 61 +++++++++---------- .../TestHttpsStatisticDistributionRestServer.java | 64 ++++++++++---------- 10 files changed, 161 insertions(+), 149 deletions(-) (limited to 'main') diff --git a/main/pom.xml b/main/pom.xml index 34467aea..cc471bd4 100644 --- a/main/pom.xml +++ b/main/pom.xml @@ -41,11 +41,6 @@ capabilities ${policy.common.version} - - org.onap.policy.common - policy-endpoints - ${policy.common.version} - commons-cli commons-cli diff --git a/main/src/main/java/org/onap/policy/distribution/main/parameters/DistributionParameterHandler.java b/main/src/main/java/org/onap/policy/distribution/main/parameters/DistributionParameterHandler.java index 603c9869..7ef5fd75 100644 --- a/main/src/main/java/org/onap/policy/distribution/main/parameters/DistributionParameterHandler.java +++ b/main/src/main/java/org/onap/policy/distribution/main/parameters/DistributionParameterHandler.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,8 +26,6 @@ import com.google.gson.GsonBuilder; import java.io.FileReader; -import org.onap.policy.common.logging.flexlogger.FlexLogger; -import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.common.parameters.GroupValidationResult; import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.main.startstop.DistributionCommandLineArguments; @@ -34,12 +33,14 @@ import org.onap.policy.distribution.reception.parameters.PolicyDecoderConfigurat import org.onap.policy.distribution.reception.parameters.PolicyDecoderConfigurationParametersJsonAdapter; import org.onap.policy.distribution.reception.parameters.ReceptionHandlerConfigurationParameterGroup; import org.onap.policy.distribution.reception.parameters.ReceptionHandlerConfigurationParametersJsonAdapter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class handles reading, parsing and validating of policy distribution parameters from JSON files. */ public class DistributionParameterHandler { - private static final Logger LOGGER = FlexLogger.getLogger(DistributionParameterHandler.class); + private static final Logger LOGGER = LoggerFactory.getLogger(DistributionParameterHandler.class); /** * Read the parameters from the parameter file. diff --git a/main/src/main/java/org/onap/policy/distribution/main/parameters/PolicyForwarderConfigurationParametersJsonAdapter.java b/main/src/main/java/org/onap/policy/distribution/main/parameters/PolicyForwarderConfigurationParametersJsonAdapter.java index 5ce15a28..687970dc 100644 --- a/main/src/main/java/org/onap/policy/distribution/main/parameters/PolicyForwarderConfigurationParametersJsonAdapter.java +++ b/main/src/main/java/org/onap/policy/distribution/main/parameters/PolicyForwarderConfigurationParametersJsonAdapter.java @@ -1,19 +1,20 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * + * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ @@ -25,17 +26,19 @@ import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; + import java.lang.reflect.Type; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class deserialises policy forwarder parameters from JSON. */ public class PolicyForwarderConfigurationParametersJsonAdapter implements JsonDeserializer { - private static final XLogger LOGGER = - XLoggerFactory.getXLogger(PolicyForwarderConfigurationParametersJsonAdapter.class); + private static final Logger LOGGER = + LoggerFactory.getLogger(PolicyForwarderConfigurationParametersJsonAdapter.class); private static final String PARAMETER_CLASS_NAME = "parameterClassName"; private static final String POLICY_FORWARDER_PARAMETERS = "parameters"; @@ -46,7 +49,7 @@ public class PolicyForwarderConfigurationParametersJsonAdapter final JsonObject jsonObject = json.getAsJsonObject(); final String policyForwarderParameterClassName = getParameterGroupClassName(jsonObject); - Class policyForwarderParameterClass = getParameterGroupClass(policyForwarderParameterClassName); + final Class policyForwarderParameterClass = getParameterGroupClass(policyForwarderParameterClassName); return context.deserialize(jsonObject.get(POLICY_FORWARDER_PARAMETERS), policyForwarderParameterClass); } diff --git a/main/src/main/java/org/onap/policy/distribution/main/rest/DistributionRestServer.java b/main/src/main/java/org/onap/policy/distribution/main/rest/DistributionRestServer.java index 748d516d..8f7652b8 100644 --- a/main/src/main/java/org/onap/policy/distribution/main/rest/DistributionRestServer.java +++ b/main/src/main/java/org/onap/policy/distribution/main/rest/DistributionRestServer.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,9 +27,9 @@ import java.util.Properties; import org.onap.policy.common.capabilities.Startable; import org.onap.policy.common.endpoints.http.server.HttpServletServer; -import org.onap.policy.common.logging.flexlogger.FlexLogger; -import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.distribution.main.parameters.RestServerParameters; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Class to manage life cycle of distribution rest server. @@ -37,9 +38,9 @@ import org.onap.policy.distribution.main.parameters.RestServerParameters; */ public class DistributionRestServer implements Startable { + private static final Logger LOGGER = LoggerFactory.getLogger(DistributionRestServer.class); private static final String SEPARATOR = "."; private static final String HTTP_SERVER_SERVICES = "http.server.services"; - private static final Logger LOGGER = FlexLogger.getLogger(DistributionRestServer.class); private List servers = new ArrayList<>(); diff --git a/main/src/main/java/org/onap/policy/distribution/main/startstop/DistributionActivator.java b/main/src/main/java/org/onap/policy/distribution/main/startstop/DistributionActivator.java index 1395b283..8db118a8 100644 --- a/main/src/main/java/org/onap/policy/distribution/main/startstop/DistributionActivator.java +++ b/main/src/main/java/org/onap/policy/distribution/main/startstop/DistributionActivator.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Copyright (C) 2019 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,8 +25,6 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; -import org.onap.policy.common.logging.flexlogger.FlexLogger; -import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.main.parameters.DistributionParameterGroup; @@ -36,14 +35,16 @@ import org.onap.policy.distribution.reception.handling.AbstractReceptionHandler; import org.onap.policy.distribution.reception.parameters.PolicyDecoderConfigurationParameterGroup; import org.onap.policy.distribution.reception.parameters.ReceptionHandlerConfigurationParameterGroup; import org.onap.policy.distribution.reception.parameters.ReceptionHandlerParameters; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class wraps a distributor so that it can be activated as a complete service together with all its distribution * and forwarding handlers. */ public class DistributionActivator { - // The logger for this class - private static final Logger LOGGER = FlexLogger.getLogger(DistributionActivator.class); + + private static final Logger LOGGER = LoggerFactory.getLogger(DistributionActivator.class); // The parameters of this policy distribution activator private final DistributionParameterGroup distributionParameterGroup; diff --git a/main/src/main/java/org/onap/policy/distribution/main/startstop/Main.java b/main/src/main/java/org/onap/policy/distribution/main/startstop/Main.java index 3ff85b8e..13da2169 100644 --- a/main/src/main/java/org/onap/policy/distribution/main/startstop/Main.java +++ b/main/src/main/java/org/onap/policy/distribution/main/startstop/Main.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Copyright (C) 2019 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,11 @@ package org.onap.policy.distribution.main.startstop; import java.util.Arrays; -import org.onap.policy.common.logging.flexlogger.FlexLogger; -import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.main.parameters.DistributionParameterGroup; import org.onap.policy.distribution.main.parameters.DistributionParameterHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class initiates ONAP Policy Framework policy distribution. @@ -34,7 +35,7 @@ import org.onap.policy.distribution.main.parameters.DistributionParameterHandler * @author Liam Fallon (liam.fallon@ericsson.com) */ public class Main { - private static final Logger LOGGER = FlexLogger.getLogger(Main.class); + private static final Logger LOGGER = LoggerFactory.getLogger(Main.class); // The policy distribution Activator that activates the policy distribution service private DistributionActivator activator; @@ -49,7 +50,7 @@ public class Main { */ public Main(final String[] args) { final String argumentString = Arrays.toString(args); - LOGGER.info("Starting policy distribution service with arguments - " + argumentString); + LOGGER.info("Starting policy distribution service with arguments - {}", argumentString); // Check the arguments final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); 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 d02139f2..356a5861 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 @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Copyright (C) 2019 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,6 +23,9 @@ package org.onap.policy.distribution.main.rest; 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 javax.ws.rs.client.ClientBuilder; @@ -33,12 +37,13 @@ import org.glassfish.jersey.client.ClientConfig; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.junit.Test; import org.onap.policy.common.endpoints.report.HealthCheckReport; -import org.onap.policy.common.logging.flexlogger.FlexLogger; -import org.onap.policy.common.logging.flexlogger.Logger; +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.parameters.RestServerParameters; import org.onap.policy.distribution.main.startstop.Main; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Class to perform unit test of HealthCheckMonitor. @@ -47,33 +52,43 @@ import org.onap.policy.distribution.main.startstop.Main; */ public class TestDistributionRestServer { - private static final Logger LOGGER = FlexLogger.getLogger(TestDistributionRestServer.class); + 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 = "self"; private static final String NAME = "Policy SSD"; @Test - public void testHealthCheckSuccess() throws PolicyDistributionException, InterruptedException { + public void testHealthCheckSuccess() { final String reportString = "Report [name=Policy SSD, url=self, healthy=true, code=200, message=alive]"; - final Main main = startDistributionService(); - final HealthCheckReport report = performHealthCheck(); - validateReport(NAME, SELF, true, 200, ALIVE, reportString, report); - stopDistributionService(main); + try { + 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"); + } } @Test - public void testHealthCheckFailure() throws InterruptedException { + public void testHealthCheckFailure() { final String reportString = "Report [name=Policy SSD, url=self, healthy=false, code=500, message=not alive]"; final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); restServerParams.setName(CommonTestData.DISTRIBUTION_GROUP_NAME); final DistributionRestServer restServer = new DistributionRestServer(restServerParams); - restServer.start(); - final HealthCheckReport report = performHealthCheck(); - validateReport(NAME, SELF, false, 500, NOT_ALIVE, reportString, report); - assertTrue(restServer.isAlive()); - assertTrue(restServer.toString().startsWith("DistributionRestServer [servers=")); - restServer.shutdown(); + try { + restServer.start(); + final HealthCheckReport report = performHealthCheck(); + validateReport(NAME, SELF, false, 500, NOT_ALIVE, reportString, report); + assertTrue(restServer.isAlive()); + assertTrue(restServer.toString().startsWith("DistributionRestServer [servers=")); + restServer.shutdown(); + } catch (final Exception exp) { + LOGGER.error("testHealthCheckFailure failed", exp); + fail("Test should not throw an exception"); + } } private Main startDistributionService() { @@ -85,8 +100,7 @@ public class TestDistributionRestServer { main.shutdown(); } - private HealthCheckReport performHealthCheck() throws InterruptedException { - HealthCheckReport response = null; + private HealthCheckReport performHealthCheck() throws InterruptedException, IOException { final ClientConfig clientConfig = new ClientConfig(); final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); @@ -96,14 +110,11 @@ public class TestDistributionRestServer { final WebTarget webTarget = client.target("http://localhost:6969/healthcheck"); final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); - while (response == null) { - try { - response = invocationBuilder.get(HealthCheckReport.class); - } catch (final Exception exp) { - LOGGER.info("the server is not started yet. We will retry again"); - } + + if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) { + throw new IllegalStateException("cannot connect to port 6969"); } - return response; + return invocationBuilder.get(HealthCheckReport.class); } private void validateReport(final String name, final String url, final boolean healthy, final int code, 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 92ae14a8..6c95a2a8 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 @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Copyright (C) 2019 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,6 +22,9 @@ package org.onap.policy.distribution.main.rest; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.io.IOException; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; @@ -31,13 +35,14 @@ import javax.ws.rs.core.MediaType; import org.glassfish.jersey.client.ClientConfig; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.junit.Test; -import org.onap.policy.common.logging.flexlogger.FlexLogger; -import org.onap.policy.common.logging.flexlogger.Logger; +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.parameters.RestServerParameters; 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}. @@ -46,40 +51,45 @@ import org.onap.policy.distribution.reception.statistics.DistributionStatisticsM */ public class TestDistributionStatistics { - private static final Logger LOGGER = FlexLogger.getLogger(TestDistributionStatistics.class); + private static final Logger LOGGER = LoggerFactory.getLogger(TestDistributionStatistics.class); @Test - public void testDistributionStatistics_200() throws PolicyDistributionException, InterruptedException { - final Main main = startDistributionService(); - StatisticsReport report = getDistributionStatistics(); - - validateReport(report, 0, 200); - updateDistributionStatistics(); - report = getDistributionStatistics(); - validateReport(report, 1, 200); - stopDistributionService(main); - DistributionStatisticsManager.resetAllStatistics(); + public void testDistributionStatistics_200() { + try { + final Main main = startDistributionService(); + StatisticsReport report = getDistributionStatistics(); + validateReport(report, 0, 200); + updateDistributionStatistics(); + report = getDistributionStatistics(); + 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"); + } } @Test public void testDistributionStatistics_500() throws InterruptedException { final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); restServerParams.setName(CommonTestData.DISTRIBUTION_GROUP_NAME); - final DistributionRestServer restServer = new DistributionRestServer(restServerParams); - restServer.start(); - final StatisticsReport report = getDistributionStatistics(); - - validateReport(report, 0, 500); - restServer.shutdown(); - DistributionStatisticsManager.resetAllStatistics(); + try { + 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"); + } } - private Main startDistributionService() { - final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters.json" }; + final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters.json" }; return new Main(distributionConfigParameters); } @@ -87,8 +97,7 @@ public class TestDistributionStatistics { main.shutdown(); } - private StatisticsReport getDistributionStatistics() throws InterruptedException { - StatisticsReport response = null; + private StatisticsReport getDistributionStatistics() throws InterruptedException, IOException { final ClientConfig clientConfig = new ClientConfig(); final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); @@ -98,14 +107,11 @@ public class TestDistributionStatistics { final WebTarget webTarget = client.target("http://localhost:6969/statistics"); final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); - while (response == null) { - try { - response = invocationBuilder.get(StatisticsReport.class); - } catch (final Exception exp) { - LOGGER.info("the server is not started yet. We will retry again"); - } + + if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) { + throw new IllegalStateException("cannot connect to port 6969"); } - return response; + return invocationBuilder.get(StatisticsReport.class); } private void updateDistributionStatistics() { 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 6667d7fd..f3046c29 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,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Intel. All rights reserved. + * Copyright (C) 2019 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,8 @@ package org.onap.policy.distribution.main.rest; 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; @@ -31,7 +31,6 @@ import java.util.Properties; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; - import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Invocation; @@ -41,10 +40,11 @@ import javax.ws.rs.core.MediaType; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.junit.Test; import org.onap.policy.common.endpoints.report.HealthCheckReport; -import org.onap.policy.common.logging.flexlogger.FlexLogger; -import org.onap.policy.common.logging.flexlogger.Logger; +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,24 +53,28 @@ import org.onap.policy.distribution.main.startstop.Main; */ public class TestHttpsDistributionRestServer { - private static final Logger LOGGER = FlexLogger.getLogger(TestDistributionRestServer.class); + private static final Logger LOGGER = LoggerFactory.getLogger(TestHttpsDistributionRestServer.class); private static final String ALIVE = "alive"; private static final String SELF = "self"; private static final String NAME = "Policy SSD"; private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore"; @Test - public void testHealthCheckSuccess() - throws PolicyDistributionException, InterruptedException, KeyManagementException, NoSuchAlgorithmException { + public void testHttpsHealthCheckSuccess() { final String reportString = "Report [name=Policy SSD, url=self, healthy=true, code=200, message=alive]"; - final Main main = startDistributionService(); - final HealthCheckReport report = performHealthCheck(); - validateReport(NAME, SELF, true, 200, ALIVE, reportString, report); - stopDistributionService(main); + try { + 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"); + } } private Main startDistributionService() { - Properties systemProps = System.getProperties(); + final Properties systemProps = System.getProperties(); systemProps.put("javax.net.ssl.keyStore", KEYSTORE); systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap"); System.setProperties(systemProps); @@ -83,11 +87,9 @@ public class TestHttpsDistributionRestServer { main.shutdown(); } - private HealthCheckReport performHealthCheck() - throws InterruptedException, KeyManagementException, NoSuchAlgorithmException { - HealthCheckReport response = null; + private HealthCheckReport performHealthCheck() throws Exception { - TrustManager[] noopTrustManager = new TrustManager[] { new X509TrustManager() { + final TrustManager[] noopTrustManager = new TrustManager[] { new X509TrustManager() { @Override public X509Certificate[] getAcceptedIssuers() { @@ -95,18 +97,16 @@ public class TestHttpsDistributionRestServer { } @Override - public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) { - } + public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {} @Override - public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) { - } + public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {} } }; - SSLContext sc = SSLContext.getInstance("TLSv1.2"); + final SSLContext sc = SSLContext.getInstance("TLSv1.2"); sc.init(null, noopTrustManager, new SecureRandom()); - final ClientBuilder clientBuilder = ClientBuilder.newBuilder().sslContext(sc) - .hostnameVerifier((host, session) -> true); + final ClientBuilder clientBuilder = + ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true); final Client client = clientBuilder.build(); final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); client.register(feature); @@ -114,18 +114,15 @@ public class TestHttpsDistributionRestServer { final WebTarget webTarget = client.target("https://localhost:6969/healthcheck"); final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); - while (response == null) { - try { - response = invocationBuilder.get(HealthCheckReport.class); - } catch (final Exception exp) { - LOGGER.error("the server is not started yet. We will retry again", exp); - } + + if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) { + throw new IllegalStateException("cannot connect to port 6969"); } - return response; + return invocationBuilder.get(HealthCheckReport.class); } private void validateReport(final String name, final String url, final boolean healthy, final int code, - final String message, final String reportString, final HealthCheckReport report) { + final String message, final String reportString, final HealthCheckReport report) { assertEquals(name, report.getName()); assertEquals(url, report.getUrl()); assertEquals(healthy, report.isHealthy()); 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 5aadb6ed..ba64e550 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 @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Intel. All rights reserved. + * Copyright (C) 2019 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,6 +22,7 @@ package org.onap.policy.distribution.main.rest; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; @@ -31,7 +33,6 @@ import java.util.Properties; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; - import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Invocation; @@ -40,10 +41,11 @@ import javax.ws.rs.core.MediaType; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.junit.Test; -import org.onap.policy.common.logging.flexlogger.FlexLogger; -import org.onap.policy.common.logging.flexlogger.Logger; +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,22 +54,25 @@ import org.onap.policy.distribution.main.startstop.Main; */ public class TestHttpsStatisticDistributionRestServer { - private static final Logger LOGGER = FlexLogger.getLogger(TestHttpsStatisticDistributionRestServer.class); + 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 testDistributionStatistic() - throws PolicyDistributionException, InterruptedException, KeyManagementException, NoSuchAlgorithmException { - final String reportString = "StatisticsReport [code=200, totalDistributionCount=0, distributionSuccessCount=0, " - + "distributionFailureCount=0, totalDownloadCount=0, " + "downloadSuccessCount=0, downloadFailureCount=0]"; - final Main main = startDistributionService(); - final StatisticsReport report = performStatisticCheck(); - validateReport(200, 0, 0, 0, 0, 0, 0, reportString, report); - stopDistributionService(main); + public void testHttpsDistributionStatistic() + throws PolicyDistributionException, InterruptedException, KeyManagementException, NoSuchAlgorithmException { + try { + 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"); + } } private Main startDistributionService() { - Properties systemProps = System.getProperties(); + final Properties systemProps = System.getProperties(); systemProps.put("javax.net.ssl.keyStore", KEYSTORE); systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap"); System.setProperties(systemProps); @@ -80,11 +85,9 @@ public class TestHttpsStatisticDistributionRestServer { main.shutdown(); } - private StatisticsReport performStatisticCheck() - throws InterruptedException, KeyManagementException, NoSuchAlgorithmException { - StatisticsReport response = null; + private StatisticsReport performStatisticCheck() throws Exception { - TrustManager[] noopTrustManager = new TrustManager[] { new X509TrustManager() { + final TrustManager[] noopTrustManager = new TrustManager[] { new X509TrustManager() { @Override public X509Certificate[] getAcceptedIssuers() { @@ -92,18 +95,16 @@ public class TestHttpsStatisticDistributionRestServer { } @Override - public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) { - } + public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {} @Override - public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) { - } + public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {} } }; - SSLContext sc = SSLContext.getInstance("TLSv1.2"); + final SSLContext sc = SSLContext.getInstance("TLSv1.2"); sc.init(null, noopTrustManager, new SecureRandom()); - final ClientBuilder clientBuilder = ClientBuilder.newBuilder().sslContext(sc) - .hostnameVerifier((host, session) -> true); + final ClientBuilder clientBuilder = + ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true); final Client client = clientBuilder.build(); final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); client.register(feature); @@ -111,19 +112,15 @@ public class TestHttpsStatisticDistributionRestServer { final WebTarget webTarget = client.target("https://localhost:6969/statistics"); final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); - while (response == null) { - try { - response = invocationBuilder.get(StatisticsReport.class); - } catch (final Exception exp) { - LOGGER.error("the server is not started yet. We will retry again", exp); - } + + if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) { + throw new IllegalStateException("cannot connect to port 6969"); } - return response; + return invocationBuilder.get(StatisticsReport.class); } private void validateReport(final int code, final int total, final int successCount, final int failureCount, - final int download, final int downloadSuccess, final int downloadFailure, final String reportString, - final StatisticsReport report) { + final int download, final int downloadSuccess, final int downloadFailure, final StatisticsReport report) { assertEquals(code, report.getCode()); assertEquals(total, report.getTotalDistributionCount()); assertEquals(successCount, report.getDistributionSuccessCount()); @@ -131,6 +128,5 @@ public class TestHttpsStatisticDistributionRestServer { assertEquals(download, report.getTotalDownloadCount()); assertEquals(downloadSuccess, report.getDownloadSuccessCount()); assertEquals(downloadFailure, report.getDownloadFailureCount()); - assertEquals(reportString, report.toString()); } } -- cgit 1.2.3-korg