diff options
Diffstat (limited to 'main')
8 files changed, 97 insertions, 71 deletions
diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/CommonTestData.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/CommonTestData.java index c8ad7a79..9f6fab5a 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/CommonTestData.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/CommonTestData.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,12 +23,17 @@ package org.onap.policy.distribution.main.parameters; import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.HashMap; import java.util.Map; import org.onap.policy.common.endpoints.parameters.RestServerParameters; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.distribution.forwarding.parameters.PolicyForwarderParameters; import org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup; import org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup; @@ -70,9 +75,46 @@ public class CommonTestData { public static final String POLICY_NAME = "SamplePolicy"; public static final String DECODER_CONFIGURATION_PARAMETERS = "dummyDecoderConfiguration"; + public static final String CONFIG_FILE = "src/test/resources/parameters/TestConfigParams.json"; + private Coder coder = new StandardCoder(); /** + * Makes a parameter configuration file by substituting an available port number within a + * source file. + * + * @param sourceName original configuration file containing 6969 + * @return the port that was substituted into the config file + * @throws IOException if the config file cannot be created + */ + public static int makeConfigFile(String sourceName) throws IOException { + int port = NetworkUtil.allocPort(); + + String json = Files.readString(new File("src/test/resources/" + sourceName).toPath()); + json = json.replace("6969", String.valueOf(port)); + + File file = new File(CONFIG_FILE); + file.deleteOnExit(); + + try (FileOutputStream output = new FileOutputStream(file)) { + output.write(json.getBytes(StandardCharsets.UTF_8)); + } + + return port; + } + + /** + * Waits for the server to connect to a port. + * @param port server's port + * @throws InterruptedException if interrupted + */ + public static void awaitServer(int port) throws InterruptedException { + if (!NetworkUtil.isTcpPortOpen("localhost", port, 50, 200L)) { + throw new IllegalStateException("cannot connect to port " + port); + } + } + + /** * Returns an instance of ReceptionHandlerParameters for test cases. * * @param isEmpty boolean value to represent that object created should be empty or not 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 3c206261..6e2f5377 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 @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2020 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -27,7 +27,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import java.io.IOException; import java.util.Map; import org.junit.Test; import org.onap.policy.common.endpoints.parameters.RestServerParameters; 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 62bd8aad..9d3df3e7 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * 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-2020 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -36,6 +36,7 @@ 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.mockito.internal.util.reflection.Whitebox; import org.onap.policy.common.endpoints.http.server.RestServer; import org.onap.policy.common.endpoints.parameters.RestServerParameters; import org.onap.policy.common.endpoints.report.HealthCheckReport; @@ -56,6 +57,8 @@ public class TestDistributionRestServer { private static final String SELF = NetworkUtil.getHostname(); private static final String NAME = "Policy SSD"; + private int port; + @Test public void testHealthCheckSuccess() { final String reportString = "Report [name=Policy SSD, url=" + SELF + ", healthy=true, code=200, message=alive]"; @@ -68,10 +71,12 @@ public class TestDistributionRestServer { } @Test - public void testHealthCheckFailure() { + public void testHealthCheckFailure() throws IOException { + port = NetworkUtil.allocPort(); final String reportString = "Report [name=Policy SSD, url=" + SELF + ", healthy=false, code=500, message=not alive]"; final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); + Whitebox.setInternalState(restServerParams, "port", port); restServerParams.setName(CommonTestData.DISTRIBUTION_GROUP_NAME); final RestServer restServer = new RestServer(restServerParams, null, DistributionRestController.class); assertThatCode(() -> { @@ -84,8 +89,9 @@ public class TestDistributionRestServer { }).doesNotThrowAnyException(); } - private Main startDistributionService() { - final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters.json" }; + private Main startDistributionService() throws IOException { + port = CommonTestData.makeConfigFile("parameters/DistributionConfigParameters.json"); + final String[] distributionConfigParameters = { "-c", CommonTestData.CONFIG_FILE }; return new Main(distributionConfigParameters); } @@ -100,13 +106,11 @@ public class TestDistributionRestServer { clientConfig.register(feature); final Client client = ClientBuilder.newClient(clientConfig); - final WebTarget webTarget = client.target("http://localhost:6969/healthcheck"); + final WebTarget webTarget = client.target("http://localhost:" + port + "/healthcheck"); final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); - if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) { - throw new IllegalStateException("cannot connect to port 6969"); - } + CommonTestData.awaitServer(port); return invocationBuilder.get(HealthCheckReport.class); } 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 f394e02b..0c732571 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * 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-2020 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2020 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -35,6 +35,7 @@ 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.mockito.internal.util.reflection.Whitebox; import org.onap.policy.common.endpoints.http.server.RestServer; import org.onap.policy.common.endpoints.parameters.RestServerParameters; import org.onap.policy.common.utils.network.NetworkUtil; @@ -50,6 +51,8 @@ import org.onap.policy.distribution.reception.statistics.DistributionStatisticsM */ public class TestDistributionStatistics { + private int port; + @Test public void testDistributionStatistics_200() { assertThatCode(() -> { @@ -65,8 +68,10 @@ public class TestDistributionStatistics { } @Test - public void testDistributionStatistics_500() throws InterruptedException { + public void testDistributionStatistics_500() throws InterruptedException, IOException { + port = NetworkUtil.allocPort(); final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); + Whitebox.setInternalState(restServerParams, "port", port); restServerParams.setName(CommonTestData.DISTRIBUTION_GROUP_NAME); final RestServer restServer = new RestServer(restServerParams, null, DistributionRestController.class); assertThatCode(() -> { @@ -78,8 +83,9 @@ public class TestDistributionStatistics { }).doesNotThrowAnyException(); } - private Main startDistributionService() { - final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters.json" }; + private Main startDistributionService() throws IOException { + port = CommonTestData.makeConfigFile("parameters/DistributionConfigParameters.json"); + final String[] distributionConfigParameters = { "-c", CommonTestData.CONFIG_FILE }; return new Main(distributionConfigParameters); } @@ -94,13 +100,11 @@ public class TestDistributionStatistics { clientConfig.register(feature); final Client client = ClientBuilder.newClient(clientConfig); - final WebTarget webTarget = client.target("http://localhost:6969/statistics"); + final WebTarget webTarget = client.target("http://localhost:" + port + "/statistics"); final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); - if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) { - throw new IllegalStateException("cannot connect to port 6969"); - } + CommonTestData.awaitServer(port); return invocationBuilder.get(StatisticsReport.class); } 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 dd8d4516..4e2fa574 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 @@ -25,12 +25,11 @@ package org.onap.policy.distribution.main.rest; import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; +import java.io.IOException; import java.security.SecureRandom; -import java.security.cert.X509Certificate; 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,6 +40,7 @@ import org.junit.Test; 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.parameters.CommonTestData; import org.onap.policy.distribution.main.startstop.Main; /** @@ -55,6 +55,8 @@ public class TestHttpsDistributionRestServer { private static final String NAME = "Policy SSD"; private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore"; + private int port; + @Test public void testHttpsHealthCheckSuccess() { final String reportString = "Report [name=Policy SSD, url=" + SELF + ", healthy=true, code=200, message=alive]"; @@ -66,13 +68,14 @@ public class TestHttpsDistributionRestServer { }).doesNotThrowAnyException(); } - private Main startDistributionService() { + private Main startDistributionService() throws IOException { final Properties systemProps = System.getProperties(); systemProps.put("javax.net.ssl.keyStore", KEYSTORE); systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap"); System.setProperties(systemProps); - final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters_Https.json" }; + port = CommonTestData.makeConfigFile("parameters/DistributionConfigParameters_Https.json"); + final String[] distributionConfigParameters = { "-c", CommonTestData.CONFIG_FILE }; return new Main(distributionConfigParameters); } @@ -82,21 +85,7 @@ public class TestHttpsDistributionRestServer { private HealthCheckReport performHealthCheck() throws Exception { - final TrustManager[] noopTrustManager = new TrustManager[] { new X509TrustManager() { - - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - - @Override - public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, final String authType) { - } - - @Override - public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, final String authType) { - } - } }; + final TrustManager[] noopTrustManager = NetworkUtil.getAlwaysTrustingManager(); final SSLContext sc = SSLContext.getInstance("TLSv1.2"); sc.init(null, noopTrustManager, new SecureRandom()); @@ -106,13 +95,11 @@ public class TestHttpsDistributionRestServer { final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); client.register(feature); - final WebTarget webTarget = client.target("https://localhost:6969/healthcheck"); + final WebTarget webTarget = client.target("https://localhost:" + port + "/healthcheck"); final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); - if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) { - throw new IllegalStateException("cannot connect to port 6969"); - } + CommonTestData.awaitServer(port); return invocationBuilder.get(HealthCheckReport.class); } 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 76462565..94d82477 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 @@ -26,12 +26,11 @@ package org.onap.policy.distribution.main.rest; import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; +import java.io.IOException; import java.security.SecureRandom; -import java.security.cert.X509Certificate; 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,6 +40,7 @@ import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; 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.parameters.CommonTestData; import org.onap.policy.distribution.main.startstop.Main; /** @@ -52,6 +52,8 @@ public class TestHttpsStatisticDistributionRestServer { private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore"; + private int port; + @Test public void testHttpsDistributionStatistic() { assertThatCode(() -> { @@ -62,13 +64,14 @@ public class TestHttpsStatisticDistributionRestServer { }).doesNotThrowAnyException(); } - private Main startDistributionService() { + private Main startDistributionService() throws IOException { final Properties systemProps = System.getProperties(); systemProps.put("javax.net.ssl.keyStore", KEYSTORE); systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap"); System.setProperties(systemProps); - final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters_Https.json" }; + port = CommonTestData.makeConfigFile("parameters/DistributionConfigParameters_Https.json"); + final String[] distributionConfigParameters = { "-c", CommonTestData.CONFIG_FILE }; return new Main(distributionConfigParameters); } @@ -77,22 +80,7 @@ public class TestHttpsStatisticDistributionRestServer { } private StatisticsReport performStatisticCheck() throws Exception { - - final TrustManager[] noopTrustManager = new TrustManager[] { new X509TrustManager() { - - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - - @Override - public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, final String authType) { - } - - @Override - public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, final String authType) { - } - } }; + final TrustManager[] noopTrustManager = NetworkUtil.getAlwaysTrustingManager(); final SSLContext sc = SSLContext.getInstance("TLSv1.2"); sc.init(null, noopTrustManager, new SecureRandom()); @@ -102,13 +90,11 @@ public class TestHttpsStatisticDistributionRestServer { final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); client.register(feature); - final WebTarget webTarget = client.target("https://localhost:6969/statistics"); + final WebTarget webTarget = client.target("https://localhost:" + port + "/statistics"); final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); - if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) { - throw new IllegalStateException("cannot connect to port 6969"); - } + CommonTestData.awaitServer(port); return invocationBuilder.get(StatisticsReport.class); } diff --git a/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionActivator.java b/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionActivator.java index d877e2a7..b15ef273 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionActivator.java +++ b/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionActivator.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +25,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import java.io.IOException; import org.junit.Test; import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.main.parameters.CommonTestData; @@ -39,8 +41,9 @@ import org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParamet public class TestDistributionActivator { @Test - public void testDistributionActivator() throws PolicyDistributionException { - final String[] distributionConfigParameters = {"-c", "parameters/DistributionConfigParameters.json"}; + public void testDistributionActivator() throws PolicyDistributionException, IOException { + CommonTestData.makeConfigFile("parameters/DistributionConfigParameters.json"); + final String[] distributionConfigParameters = { "-c", CommonTestData.CONFIG_FILE }; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(distributionConfigParameters); diff --git a/main/src/test/java/org/onap/policy/distribution/main/startstop/TestMain.java b/main/src/test/java/org/onap/policy/distribution/main/startstop/TestMain.java index f6ddbe99..d8bdd98b 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/startstop/TestMain.java +++ b/main/src/test/java/org/onap/policy/distribution/main/startstop/TestMain.java @@ -26,6 +26,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import java.io.IOException; import org.junit.Test; import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.main.parameters.CommonTestData; @@ -38,9 +39,9 @@ import org.onap.policy.distribution.main.parameters.CommonTestData; public class TestMain { @Test - public void testMain() throws PolicyDistributionException { - final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters.json" }; + public void testMain() throws PolicyDistributionException, IOException { + CommonTestData.makeConfigFile("parameters/DistributionConfigParameters.json"); + final String[] distributionConfigParameters = { "-c", CommonTestData.CONFIG_FILE }; final Main main = new Main(distributionConfigParameters); assertTrue(main.getParameters().isValid()); assertEquals(CommonTestData.DISTRIBUTION_GROUP_NAME, main.getParameters().getName()); |