diff options
author | Jim Hahn <jrh3@att.com> | 2019-07-08 11:15:07 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-07-08 11:53:19 -0400 |
commit | 112549df98ef75d071f40632c4f905a83eaacdb0 (patch) | |
tree | 5eecbef02b80865988684904b9373d46cc9ae640 /main/src/test/java/org | |
parent | 7baf805c53151aecd65a19cf44deaa35aadf577a (diff) |
Fix api due to sonar changes in common
Fixed breakages due to changes made in policy/common to satisfy
sonar.
Also modified the code to allocate ports for junit tests, writing
them into the config files.
Change-Id: Id31d19e0691f7450c3e4158090dac6f3c3849631
Issue-ID: POLICY-1791
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'main/src/test/java/org')
5 files changed, 74 insertions, 34 deletions
diff --git a/main/src/test/java/org/onap/policy/api/main/parameters/CommonTestData.java b/main/src/test/java/org/onap/policy/api/main/parameters/CommonTestData.java index 50e484eb..f6266de5 100644 --- a/main/src/test/java/org/onap/policy/api/main/parameters/CommonTestData.java +++ b/main/src/test/java/org/onap/policy/api/main/parameters/CommonTestData.java @@ -23,6 +23,9 @@ package org.onap.policy.api.main.parameters; +import java.io.File; +import java.io.IOException; +import org.onap.policy.common.utils.resources.TextFileUtils; import org.onap.policy.models.provider.PolicyModelsProviderParameters; /** @@ -33,9 +36,13 @@ public class CommonTestData { public static final String API_GROUP_NAME = "ApiGroup"; + /** + * Server port, as it appears within the config files. + */ + private static final String REST_SERVER_PORT = "6969"; + private static final String REST_SERVER_PASSWORD = "zb!XztG34"; private static final String REST_SERVER_USER = "healthcheck"; - private static final int REST_SERVER_PORT = 6969; private static final String REST_SERVER_HOST = "0.0.0.0"; private static final boolean REST_SERVER_HTTPS = false; private static final boolean REST_SERVER_AAF = false; @@ -52,12 +59,13 @@ public class CommonTestData { * Returns an instance of RestServerParameters for test cases. * * @param isEmpty boolean value to represent that object created should be empty or not + * @param port server port * @return the RestServerParameters object */ - public RestServerParameters getRestServerParameters(final boolean isEmpty) { + public RestServerParameters getRestServerParameters(final boolean isEmpty, int port) { final RestServerParameters restServerParameters; if (!isEmpty) { - restServerParameters = new RestServerParameters(REST_SERVER_HOST, REST_SERVER_PORT, REST_SERVER_USER, + restServerParameters = new RestServerParameters(REST_SERVER_HOST, port, REST_SERVER_USER, REST_SERVER_PASSWORD, REST_SERVER_HTTPS, REST_SERVER_AAF); } else { restServerParameters = new RestServerParameters(null, 0, null, null, false, false); @@ -87,4 +95,22 @@ public class CommonTestData { } return databaseProviderParameters; } + + /** + * Copies a source file to a target file, replacing occurrances of + * {@link #REST_SERVER_PORT} with the given port number. + * + * @param source source file name + * @param target target file name + * @param port port to be substituted + * @throws IOException if an error occurs + */ + public void makeParameters(String source, String target, int port) throws IOException { + String text = TextFileUtils.getTextFileAsString(source); + text = text.replace(REST_SERVER_PORT, String.valueOf(port)); + + File file = new File(target); + file.deleteOnExit(); + TextFileUtils.putStringAsFile(text, file); + } } diff --git a/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterGroup.java b/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterGroup.java index 8be52455..6af84b97 100644 --- a/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterGroup.java +++ b/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterGroup.java @@ -36,11 +36,12 @@ import org.onap.policy.models.provider.PolicyModelsProviderParameters; * */ public class TestApiParameterGroup { - CommonTestData commonTestData = new CommonTestData(); + private static final int PORT = 6969; + private CommonTestData commonTestData = new CommonTestData(); @Test public void testApiParameterGroup() { - final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); + final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false, PORT); final PolicyModelsProviderParameters databaseProviderParameters = commonTestData.getDatabaseProviderParameters(false); final ApiParameterGroup apiParameters = new ApiParameterGroup( @@ -60,7 +61,7 @@ public class TestApiParameterGroup { @Test public void testApiParameterGroup_NullName() { - final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); + final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false, PORT); final PolicyModelsProviderParameters databaseProviderParameters = commonTestData.getDatabaseProviderParameters(false); final ApiParameterGroup apiParameters = new ApiParameterGroup(null, @@ -75,7 +76,7 @@ public class TestApiParameterGroup { @Test public void testApiParameterGroup_EmptyName() { - final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); + final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false, PORT); final PolicyModelsProviderParameters databaseProviderParameters = commonTestData.getDatabaseProviderParameters(false); final ApiParameterGroup apiParameters = new ApiParameterGroup("", @@ -89,7 +90,7 @@ public class TestApiParameterGroup { @Test public void testApiParameterGroup_EmptyRestServerParameters() { - final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(true); + final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(true, PORT); final PolicyModelsProviderParameters databaseProviderParameters = commonTestData.getDatabaseProviderParameters(false); final ApiParameterGroup apiParameters = new ApiParameterGroup( @@ -103,7 +104,7 @@ public class TestApiParameterGroup { @Test public void testApiParameterGroup_EmptyDatabaseProviderParameters() { - final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); + final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false, PORT); final PolicyModelsProviderParameters databaseProviderParameters = commonTestData.getDatabaseProviderParameters(true); final ApiParameterGroup apiParameters = new ApiParameterGroup( diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java index 2f286f0c..6d49efd2 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java @@ -136,10 +136,12 @@ public class TestApiRestServer { private static final String OPS_POLICIES_VFIREWALL_VERSION = "policytypes/" + "onap.policies.controlloop.Guard/versions/1.0.0/policies/operational.modifyconfig/versions/1"; - private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore"; + private static final String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore"; + private static final CommonTestData COMMON_TEST_DATA = new CommonTestData(); private Main main; private ApiRestServer restServer; private StandardCoder standardCoder = new StandardCoder(); + private int port; // @formatter:off private String[] toscaPolicyResourceNames = { @@ -170,12 +172,10 @@ public class TestApiRestServer { */ @After public void teardown() throws Exception { - if (NetworkUtil.isTcpPortOpen("localhost", 6969, 1, 1000L)) { - if (main != null) { - stopApiService(main); - } else if (restServer != null) { - restServer.stop(); - } + if (main != null) { + stopApiService(main); + } else if (restServer != null) { + restServer.stop(); } } @@ -196,7 +196,8 @@ public class TestApiRestServer { @Test public void testHealthCheckFailure() throws InterruptedException, IOException { - final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); + port = NetworkUtil.allocPort(); + final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false, port); restServerParams.setName(CommonTestData.API_GROUP_NAME); restServer = new ApiRestServer(restServerParams); try { @@ -246,9 +247,10 @@ public class TestApiRestServer { } @Test - public void testApiStatistics_500() { + public void testApiStatistics_500() throws Exception { - final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); + port = NetworkUtil.allocPort(); + final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false, port); restServerParams.setName(CommonTestData.API_GROUP_NAME); restServer = new ApiRestServer(restServerParams); try { @@ -693,19 +695,24 @@ public class TestApiRestServer { return invocationBuilder.delete(); } - private Main startApiService(final boolean http) { + private Main startApiService(final boolean http) throws Exception { + port = NetworkUtil.allocPort(); final String[] apiConfigParameters = new String[2]; if (http) { + COMMON_TEST_DATA.makeParameters("src/test/resources/parameters/ApiConfigParameters.json", + "src/test/resources/parameters/ApiConfigParametersXXX.json", port); apiConfigParameters[0] = "-c"; - apiConfigParameters[1] = "parameters/ApiConfigParameters.json"; + apiConfigParameters[1] = "src/test/resources/parameters/ApiConfigParametersXXX.json"; } else { final Properties systemProps = System.getProperties(); systemProps.put("javax.net.ssl.keyStore", KEYSTORE); systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap"); System.setProperties(systemProps); + COMMON_TEST_DATA.makeParameters("src/test/resources/parameters/ApiConfigParameters_Https.json", + "src/test/resources/parameters/ApiConfigParameters_HttpsXXX.json", port); apiConfigParameters[0] = "-c"; - apiConfigParameters[1] = "parameters/ApiConfigParameters_Https.json"; + apiConfigParameters[1] = "src/test/resources/parameters/ApiConfigParameters_HttpsXXX.json"; } return new Main(apiConfigParameters); } @@ -727,12 +734,12 @@ public class TestApiRestServer { client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); client.register(GsonMessageBodyHandler.class); - final WebTarget webTarget = client.target("http://localhost:6969/policy/api/v1/" + endpoint); + final WebTarget webTarget = client.target("http://localhost:" + port + "/policy/api/v1/" + endpoint); final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); - if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) { - throw new IllegalStateException("cannot connect to port 6969"); + if (!NetworkUtil.isTcpPortOpen("localhost", port, 60, 1000L)) { + throw new IllegalStateException("cannot connect to port " + port); } return invocationBuilder; } @@ -764,12 +771,12 @@ public class TestApiRestServer { client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); client.register(GsonMessageBodyHandler.class); - final WebTarget webTarget = client.target("https://localhost:6969/policy/api/v1/" + endpoint); + final WebTarget webTarget = client.target("https://localhost:" + port + "/policy/api/v1/" + endpoint); final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); - if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) { - throw new IllegalStateException("cannot connect to port 6969"); + if (!NetworkUtil.isTcpPortOpen("localhost", port, 60, 1000L)) { + throw new IllegalStateException("cannot connect to port " + port); } return invocationBuilder; } diff --git a/main/src/test/java/org/onap/policy/api/main/startstop/TestApiActivator.java b/main/src/test/java/org/onap/policy/api/main/startstop/TestApiActivator.java index 01cd77c6..c34a4aa3 100644 --- a/main/src/test/java/org/onap/policy/api/main/startstop/TestApiActivator.java +++ b/main/src/test/java/org/onap/policy/api/main/startstop/TestApiActivator.java @@ -25,10 +25,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import org.junit.Test; -import org.onap.policy.api.main.exception.PolicyApiException; import org.onap.policy.api.main.parameters.ApiParameterGroup; import org.onap.policy.api.main.parameters.ApiParameterHandler; import org.onap.policy.api.main.parameters.CommonTestData; +import org.onap.policy.common.utils.network.NetworkUtil; /** @@ -36,10 +36,13 @@ import org.onap.policy.api.main.parameters.CommonTestData; * */ public class TestApiActivator { + private static final CommonTestData COMMON_TEST_DATA = new CommonTestData(); @Test - public void testApiActivator() throws PolicyApiException { - final String[] apiConfigParameters = { "-c", "parameters/ApiConfigParameters.json" }; + public void testApiActivator() throws Exception { + COMMON_TEST_DATA.makeParameters("src/test/resources/parameters/ApiConfigParameters.json", + "src/test/resources/parameters/ApiConfigParametersXXX.json", NetworkUtil.allocPort()); + final String[] apiConfigParameters = { "-c", "src/test/resources/parameters/ApiConfigParametersXXX.json" }; final ApiCommandLineArguments arguments = new ApiCommandLineArguments(apiConfigParameters); final ApiParameterGroup parGroup = new ApiParameterHandler().getParameters(arguments); final ApiActivator activator = new ApiActivator(parGroup); diff --git a/main/src/test/java/org/onap/policy/api/main/startstop/TestMain.java b/main/src/test/java/org/onap/policy/api/main/startstop/TestMain.java index 2f68e852..d770b56c 100644 --- a/main/src/test/java/org/onap/policy/api/main/startstop/TestMain.java +++ b/main/src/test/java/org/onap/policy/api/main/startstop/TestMain.java @@ -25,18 +25,21 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import org.junit.Test; -import org.onap.policy.api.main.exception.PolicyApiException; import org.onap.policy.api.main.parameters.CommonTestData; +import org.onap.policy.common.utils.network.NetworkUtil; /** * Class to perform unit test of Main. * */ public class TestMain { + private static final CommonTestData COMMON_TEST_DATA = new CommonTestData(); @Test - public void testMain() throws PolicyApiException { - final String[] apiConfigParameters = { "-c", "parameters/ApiConfigParameters.json" }; + public void testMain() throws Exception { + COMMON_TEST_DATA.makeParameters("src/test/resources/parameters/ApiConfigParameters.json", + "src/test/resources/parameters/ApiConfigParametersXXX.json", NetworkUtil.allocPort()); + final String[] apiConfigParameters = { "-c", "src/test/resources/parameters/ApiConfigParametersXXX.json" }; final Main main = new Main(apiConfigParameters); assertTrue(main.getParameters().isValid()); assertEquals(CommonTestData.API_GROUP_NAME, main.getParameters().getName()); |