diff options
author | ramverma <ram.krishna.verma@est.tech> | 2019-02-07 21:52:23 +0000 |
---|---|---|
committer | ramverma <ram.krishna.verma@est.tech> | 2019-02-07 21:52:23 +0000 |
commit | 74ead72904c6c9240dbfcf6d86433e32510416f6 (patch) | |
tree | 5aa421d52d598a476b6f36a0c050f38ba75f1fd4 /main/src | |
parent | 370c227614be2b63c9383d4eb5a6c064d3d49835 (diff) |
Adding statistics endpoint to policy/pap
1) Adding statistics endpoint to policy pap component.
2) Introducing lombok libraray.
3) Adding configurable support for aaf authentication.
4) Adding configurable support for https communication.
5) Adding related test cases.
Change-Id: Ib3131810c42fbd23878b97302da8d54f095da373
Issue-ID: POLICY-1482
Signed-off-by: ramverma <ram.krishna.verma@est.tech>
Diffstat (limited to 'main/src')
15 files changed, 631 insertions, 33 deletions
diff --git a/main/src/main/java/org/onap/policy/pap/main/parameters/RestServerParameters.java b/main/src/main/java/org/onap/policy/pap/main/parameters/RestServerParameters.java index cfc8832b..6b1e3f82 100644 --- a/main/src/main/java/org/onap/policy/pap/main/parameters/RestServerParameters.java +++ b/main/src/main/java/org/onap/policy/pap/main/parameters/RestServerParameters.java @@ -36,6 +36,8 @@ public class RestServerParameters implements ParameterGroup { private int port; private String userName; private String password; + private boolean https; + private boolean aaf; /** * Constructor for instantiating RestServerParameters. @@ -44,13 +46,18 @@ public class RestServerParameters implements ParameterGroup { * @param port the port * @param userName the user name * @param password the password + * @param https the https flag + * @param aaf the aaf flag */ - public RestServerParameters(final String host, final int port, final String userName, final String password) { + public RestServerParameters(final String host, final int port, final String userName, final String password, + final boolean https, final boolean aaf) { super(); this.host = host; this.port = port; this.userName = userName; this.password = password; + this.https = https; + this.aaf = aaf; } /** @@ -100,6 +107,24 @@ public class RestServerParameters implements ParameterGroup { } /** + * Return the https flag of this RestServerParameters instance. + * + * @return the https flag + */ + public boolean isHttps() { + return https; + } + + /** + * Return the aaf flag of this RestServerParameters instance. + * + * @return the aaf flag + */ + public boolean isAaf() { + return aaf; + } + + /** * Set the name of this RestServerParameters instance. * * @param name the name to set diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PapAafFilter.java b/main/src/main/java/org/onap/policy/pap/main/rest/PapAafFilter.java new file mode 100644 index 00000000..990d15e8 --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/rest/PapAafFilter.java @@ -0,0 +1,39 @@ +/*- + * ============LICENSE_START======================================================= + * 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========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import org.onap.policy.common.endpoints.http.server.aaf.AafGranularAuthFilter; + +/** + * Class to manage aaf filters for PAP component. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class PapAafFilter extends AafGranularAuthFilter { + + public static final String AAF_NODETYPE = "policy-pap"; + public static final String AAF_ROOT_PERMISSION = DEFAULT_NAMESPACE + "." + AAF_NODETYPE; + + @Override + public String getPermissionTypeRoot() { + return AAF_ROOT_PERMISSION; + } +} diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PapRestController.java b/main/src/main/java/org/onap/policy/pap/main/rest/PapRestController.java index 226dc356..4e9aa450 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/PapRestController.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/PapRestController.java @@ -56,4 +56,13 @@ public class PapRestController { public Response healthcheck() { return Response.status(Response.Status.OK).entity(new HealthCheckProvider().performHealthCheck()).build(); } + + @GET + @Path("statistics") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation(value = "Fetch current statistics", notes = "Provides current statistics of the Policy PAP component", + response = StatisticsReport.class) + public Response statistics() { + return Response.status(Response.Status.OK).entity(new StatisticsProvider().fetchCurrentStatistics()).build(); + } } diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PapRestServer.java b/main/src/main/java/org/onap/policy/pap/main/rest/PapRestServer.java index 39f65f24..e9064a82 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/PapRestServer.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/PapRestServer.java @@ -63,6 +63,9 @@ public class PapRestServer implements Startable { try { servers = HttpServletServer.factory.build(getServerProperties()); for (final HttpServletServer server : servers) { + if (server.isAaf()) { + server.addFilterClass(null, PapAafFilter.class.getCanonicalName()); + } server.start(); } } catch (final Exception exp) { @@ -92,6 +95,10 @@ public class PapRestServer implements Startable { restServerParameters.getUserName()); props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password", restServerParameters.getPassword()); + props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".https", + String.valueOf(restServerParameters.isHttps())); + props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".aaf", + String.valueOf(restServerParameters.isAaf())); return props; } diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PapStatisticsManager.java b/main/src/main/java/org/onap/policy/pap/main/rest/PapStatisticsManager.java new file mode 100644 index 00000000..98148117 --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/rest/PapStatisticsManager.java @@ -0,0 +1,138 @@ +/*- + * ============LICENSE_START======================================================= + * 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========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import lombok.Getter; + +/** + * Class to hold statistical data for pap component. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class PapStatisticsManager { + + @Getter + private static long totalPdpCount; + @Getter + private static long totalPdpGroupCount; + @Getter + private static long totalPolicyDeployCount; + @Getter + private static long policyDeploySuccessCount; + @Getter + private static long policyDeployFailureCount; + @Getter + private static long totalPolicyDownloadCount; + @Getter + private static long policyDownloadSuccessCount; + @Getter + private static long policyDownloadFailureCount; + + private PapStatisticsManager() { + throw new IllegalStateException("Instantiation of the class is not allowed"); + } + + /** + * Method to update the total pdp count. + * + * @return the updated value of totalPdpCount + */ + public static long updateTotalPdpCount() { + return ++totalPdpCount; + } + + /** + * Method to update the total pdp group count. + * + * @return the updated value of totalPdpGroupCount + */ + public static long updateTotalPdpGroupCount() { + return ++totalPdpGroupCount; + } + + /** + * Method to update the total policy deploy count. + * + * @return the updated value of totalPolicyDeployCount + */ + public static long updateTotalPolicyDeployCount() { + return ++totalPolicyDeployCount; + } + + /** + * Method to update the policy deploy success count. + * + * @return the updated value of policyDeploySuccessCount + */ + public static long updatePolicyDeploySuccessCount() { + return ++policyDeploySuccessCount; + } + + /** + * Method to update the policy deploy failure count. + * + * @return the updated value of policyDeployFailureCount + */ + public static long updatePolicyDeployFailureCount() { + return ++policyDeployFailureCount; + } + + /** + * Method to update the total policy download count. + * + * @return the updated value of totalPolicyDownloadCount + */ + public static long updateTotalPolicyDownloadCount() { + return ++totalPolicyDownloadCount; + } + + /** + * Method to update the policy download success count. + * + * @return the updated value of policyDownloadSuccessCount + */ + public static long updatePolicyDownloadSuccessCount() { + return ++policyDownloadSuccessCount; + } + + /** + * Method to update the policy download failure count. + * + * @return the updated value of policyDownloadFailureCount + */ + public static long updatePolicyDownloadFailureCount() { + return ++policyDownloadFailureCount; + } + + /** + * Reset all the statistics counts to 0. + */ + public static void resetAllStatistics() { + totalPdpCount = 0L; + totalPdpGroupCount = 0L; + totalPolicyDeployCount = 0L; + policyDeploySuccessCount = 0L; + policyDeployFailureCount = 0L; + totalPolicyDownloadCount = 0L; + policyDownloadSuccessCount = 0L; + policyDownloadFailureCount = 0L; + } +} diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/StatisticsProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/StatisticsProvider.java new file mode 100644 index 00000000..38ae5034 --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/rest/StatisticsProvider.java @@ -0,0 +1,50 @@ +/*- + * ============LICENSE_START======================================================= + * 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========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import org.onap.policy.pap.main.startstop.PapActivator; + +/** + * Class to fetch statistics of pap component. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class StatisticsProvider { + + /** + * Returns the current statistics of pap component. + * + * @return Report containing statistics of pap component + */ + public StatisticsReport fetchCurrentStatistics() { + final StatisticsReport report = new StatisticsReport(); + report.setCode(PapActivator.isAlive() ? 200 : 500); + report.setTotalPdpCount(PapStatisticsManager.getTotalPdpCount()); + report.setTotalPdpGroupCount(PapStatisticsManager.getTotalPdpGroupCount()); + report.setTotalPolicyDownloadCount(PapStatisticsManager.getTotalPolicyDownloadCount()); + report.setPolicyDownloadSuccessCount(PapStatisticsManager.getPolicyDownloadSuccessCount()); + report.setPolicyDownloadFailureCount(PapStatisticsManager.getPolicyDownloadFailureCount()); + report.setTotalPolicyDeployCount(PapStatisticsManager.getTotalPolicyDeployCount()); + report.setPolicyDeploySuccessCount(PapStatisticsManager.getPolicyDeploySuccessCount()); + report.setPolicyDeployFailureCount(PapStatisticsManager.getPolicyDeployFailureCount()); + return report; + } +} diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/StatisticsReport.java b/main/src/main/java/org/onap/policy/pap/main/rest/StatisticsReport.java new file mode 100644 index 00000000..013cfab6 --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/rest/StatisticsReport.java @@ -0,0 +1,62 @@ +/*- + * ============LICENSE_START======================================================= + * 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========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +/** + * Class to represent statistics report of pap component. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +@ToString +public class StatisticsReport { + + @Getter + @Setter + private int code; + @Getter + @Setter + private long totalPdpCount; + @Getter + @Setter + private long totalPdpGroupCount; + @Getter + @Setter + private long totalPolicyDeployCount; + @Getter + @Setter + private long policyDeploySuccessCount; + @Getter + @Setter + private long policyDeployFailureCount; + @Getter + @Setter + private long totalPolicyDownloadCount; + @Getter + @Setter + private long policyDownloadSuccessCount; + @Getter + @Setter + private long policyDownloadFailureCount; +} diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java b/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java index b1674efb..86a435ae 100644 --- a/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java +++ b/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java @@ -127,7 +127,7 @@ public class PapActivator { * * @param status the status */ - public static void setAlive(final boolean status) { + private static void setAlive(final boolean status) { alive = status; } diff --git a/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java b/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java index 0e4ae523..0c28de62 100644 --- a/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java +++ b/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java @@ -31,6 +31,8 @@ public class CommonTestData { 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; public static final String PAP_GROUP_NAME = "PapGroup"; /** @@ -43,9 +45,9 @@ public class CommonTestData { final RestServerParameters restServerParameters; if (!isEmpty) { restServerParameters = new RestServerParameters(REST_SERVER_HOST, REST_SERVER_PORT, REST_SERVER_USER, - REST_SERVER_PASSWORD); + REST_SERVER_PASSWORD, REST_SERVER_HTTPS, REST_SERVER_AAF); } else { - restServerParameters = new RestServerParameters(null, 0, null, null); + restServerParameters = new RestServerParameters(null, 0, null, null, REST_SERVER_HTTPS, REST_SERVER_AAF); } return restServerParameters; } diff --git a/main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterGroup.java b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterGroup.java index 7d5355a0..5569b65a 100644 --- a/main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterGroup.java +++ b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterGroup.java @@ -47,6 +47,8 @@ public class TestPapParameterGroup { assertEquals(restServerParameters.getPort(), papParameters.getRestServerParameters().getPort()); assertEquals(restServerParameters.getUserName(), papParameters.getRestServerParameters().getUserName()); assertEquals(restServerParameters.getPassword(), papParameters.getRestServerParameters().getPassword()); + assertFalse(papParameters.getRestServerParameters().isHttps()); + assertFalse(papParameters.getRestServerParameters().isAaf()); } @Test diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPapRestServer.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPapRestServer.java index b604e206..66421b89 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/TestPapRestServer.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPapRestServer.java @@ -25,7 +25,15 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; +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; @@ -34,6 +42,7 @@ import javax.ws.rs.core.MediaType; import org.glassfish.jersey.client.ClientConfig; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; +import org.junit.After; import org.junit.Test; import org.onap.policy.common.endpoints.report.HealthCheckReport; import org.onap.policy.common.utils.network.NetworkUtil; @@ -56,15 +65,37 @@ public class TestPapRestServer { private static final String ALIVE = "alive"; private static final String SELF = "self"; private static final String NAME = "Policy PAP"; + private static final String HEALTHCHECK_ENDPOINT = "healthcheck"; + private static final String STATISTICS_ENDPOINT = "statistics"; + private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore"; + private Main main; + private PapRestServer restServer; + + /** + * Method for cleanup after each test. + */ + @After + public void teardown() { + try { + if (NetworkUtil.isTcpPortOpen("localhost", 6969, 1, 1000L)) { + if (main != null) { + stopPapService(main); + } else if (restServer != null) { + restServer.stop(); + } + } + } catch (InterruptedException | IOException | PolicyPapException exp) { + LOGGER.error("teardown failed", exp); + } + } @Test - public void testHealthCheckSuccess() throws PolicyPapException, InterruptedException { - final String reportString = "Report [name=Policy PAP, url=self, healthy=true, code=200, message=alive]"; + public void testHealthCheckSuccess() { try { - final Main main = startPapService(); - final HealthCheckReport report = performHealthCheck(); - validateReport(NAME, SELF, true, 200, ALIVE, reportString, report); - stopPapService(main); + main = startPapService(true); + final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT); + final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class); + validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report); } catch (final Exception exp) { LOGGER.error("testHealthCheckSuccess failed", exp); fail("Test should not throw an exception"); @@ -73,20 +104,108 @@ public class TestPapRestServer { @Test public void testHealthCheckFailure() throws InterruptedException, IOException { - final String reportString = "Report [name=Policy PAP, url=self, healthy=false, code=500, message=not alive]"; final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); restServerParams.setName(CommonTestData.PAP_GROUP_NAME); - final PapRestServer restServer = new PapRestServer(restServerParams); - restServer.start(); - final HealthCheckReport report = performHealthCheck(); - validateReport(NAME, SELF, false, 500, NOT_ALIVE, reportString, report); - assertTrue(restServer.isAlive()); - assertTrue(restServer.toString().startsWith("PapRestServer [servers=")); - restServer.shutdown(); + restServer = new PapRestServer(restServerParams); + try { + restServer.start(); + final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT); + final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class); + validateHealthCheckReport(NAME, SELF, false, 500, NOT_ALIVE, report); + assertTrue(restServer.isAlive()); + assertTrue(restServer.toString().startsWith("PapRestServer [servers=")); + } catch (final Exception exp) { + LOGGER.error("testHealthCheckFailure failed", exp); + fail("Test should not throw an exception"); + } } - private Main startPapService() { - final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" }; + @Test + public void testHttpsHealthCheckSuccess() { + try { + main = startPapService(false); + final Invocation.Builder invocationBuilder = sendHttpsRequest(HEALTHCHECK_ENDPOINT); + final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class); + validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report); + } catch (final Exception exp) { + LOGGER.error("testHttpsHealthCheckSuccess failed", exp); + fail("Test should not throw an exception"); + } + } + + @Test + public void testPapStatistics_200() { + try { + main = startPapService(true); + Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT); + StatisticsReport report = invocationBuilder.get(StatisticsReport.class); + validateStatisticsReport(report, 0, 200); + updateDistributionStatistics(); + invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT); + report = invocationBuilder.get(StatisticsReport.class); + validateStatisticsReport(report, 1, 200); + PapStatisticsManager.resetAllStatistics(); + } catch (final Exception exp) { + LOGGER.error("testPapStatistics_200 failed", exp); + fail("Test should not throw an exception"); + } + } + + @Test + public void testPapStatistics_500() { + final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); + restServerParams.setName(CommonTestData.PAP_GROUP_NAME); + restServer = new PapRestServer(restServerParams); + try { + restServer.start(); + final Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT); + final StatisticsReport report = invocationBuilder.get(StatisticsReport.class); + validateStatisticsReport(report, 0, 500); + PapStatisticsManager.resetAllStatistics(); + } catch (final Exception exp) { + LOGGER.error("testPapStatistics_500 failed", exp); + fail("Test should not throw an exception"); + } + } + + @Test + public void testHttpsPapStatistic() { + try { + main = startPapService(false); + final Invocation.Builder invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT); + final StatisticsReport report = invocationBuilder.get(StatisticsReport.class); + validateStatisticsReport(report, 0, 200); + } catch (final Exception exp) { + LOGGER.error("testHttpsDistributionStatistic failed", exp); + fail("Test should not throw an exception"); + } + } + + @Test + public void testPapStatisticsConstructorIsPrivate() { + try { + final Constructor<PapStatisticsManager> constructor = PapStatisticsManager.class.getDeclaredConstructor(); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + constructor.newInstance(); + } catch (final Exception exp) { + assertTrue(exp.getCause().toString().contains("Instantiation of the class is not allowed")); + } + } + + private Main startPapService(final boolean http) { + final String[] papConfigParameters = new String[2]; + if (http) { + papConfigParameters[0] = "-c"; + papConfigParameters[1] = "parameters/PapConfigParameters.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); + papConfigParameters[0] = "-c"; + papConfigParameters[1] = "parameters/PapConfigParameters_Https.json"; + } return new Main(papConfigParameters); } @@ -94,32 +213,86 @@ public class TestPapRestServer { main.shutdown(); } - private HealthCheckReport performHealthCheck() throws InterruptedException, IOException { - HealthCheckReport response = null; + private Invocation.Builder sendHttpRequest(final String endpoint) throws Exception { final ClientConfig clientConfig = new ClientConfig(); final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); 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:6969/" + 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"); + } + return invocationBuilder; + } + + private Invocation.Builder sendHttpsRequest(final String endpoint) 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 SSLContext sc = SSLContext.getInstance("TLSv1.2"); + sc.init(null, noopTrustManager, new SecureRandom()); + 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); + + final WebTarget webTarget = client.target("https://localhost:6969/" + 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"); } - response = invocationBuilder.get(HealthCheckReport.class); - return response; + return invocationBuilder; + } + + private void updateDistributionStatistics() { + PapStatisticsManager.updateTotalPdpCount(); + PapStatisticsManager.updateTotalPdpGroupCount(); + PapStatisticsManager.updateTotalPolicyDeployCount(); + PapStatisticsManager.updatePolicyDeploySuccessCount(); + PapStatisticsManager.updatePolicyDeployFailureCount(); + PapStatisticsManager.updateTotalPolicyDownloadCount(); + PapStatisticsManager.updatePolicyDownloadSuccessCount(); + PapStatisticsManager.updatePolicyDownloadFailureCount(); + } + + private void validateStatisticsReport(final StatisticsReport report, final int count, final int code) { + assertEquals(code, report.getCode()); + assertEquals(count, report.getTotalPdpCount()); + assertEquals(count, report.getTotalPdpGroupCount()); + assertEquals(count, report.getTotalPolicyDeployCount()); + assertEquals(count, report.getPolicyDeploySuccessCount()); + assertEquals(count, report.getPolicyDeployFailureCount()); + assertEquals(count, report.getTotalPolicyDownloadCount()); + assertEquals(count, report.getPolicyDeploySuccessCount()); + assertEquals(count, report.getPolicyDeployFailureCount()); } - private void validateReport(final String name, final String url, final boolean healthy, final int code, - final String message, final String reportString, final HealthCheckReport report) { + private void validateHealthCheckReport(final String name, final String url, final boolean healthy, final int code, + final String message, final HealthCheckReport report) { assertEquals(name, report.getName()); assertEquals(url, report.getUrl()); assertEquals(healthy, report.isHealthy()); assertEquals(code, report.getCode()); assertEquals(message, report.getMessage()); - assertEquals(reportString, report.toString()); } } diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestStatisticsReport.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestStatisticsReport.java new file mode 100644 index 00000000..80586fc2 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestStatisticsReport.java @@ -0,0 +1,48 @@ +/*- + * ============LICENSE_START======================================================= + * 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========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import com.openpojo.reflection.filters.FilterClassName; +import com.openpojo.validation.Validator; +import com.openpojo.validation.ValidatorBuilder; +import com.openpojo.validation.rule.impl.GetterMustExistRule; +import com.openpojo.validation.rule.impl.SetterMustExistRule; +import com.openpojo.validation.test.impl.GetterTester; +import com.openpojo.validation.test.impl.SetterTester; + +import org.junit.Test; +import org.onap.policy.common.utils.validation.ToStringTester; + +/** + * Class to perform unit testing of {@link StatisticsReport}. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class TestStatisticsReport { + + @Test + public void testStatisticsReport() { + final Validator validator = ValidatorBuilder.create().with(new ToStringTester()).with(new SetterMustExistRule()) + .with(new GetterMustExistRule()).with(new SetterTester()).with(new GetterTester()).build(); + validator.validate(StatisticsReport.class.getPackage().getName(), + new FilterClassName(StatisticsReport.class.getName())); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java b/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java index 03c3413a..3781be47 100644 --- a/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java +++ b/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java @@ -22,12 +22,16 @@ package org.onap.policy.pap.main.startstop; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import org.junit.After; import org.junit.Test; import org.onap.policy.pap.main.PolicyPapException; import org.onap.policy.pap.main.parameters.CommonTestData; import org.onap.policy.pap.main.parameters.PapParameterGroup; import org.onap.policy.pap.main.parameters.PapParameterHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** @@ -37,18 +41,47 @@ import org.onap.policy.pap.main.parameters.PapParameterHandler; */ public class TestPapActivator { + private static final Logger LOGGER = LoggerFactory.getLogger(TestPapActivator.class); + private PapActivator activator; + + /** + * Method for cleanup after each test. + */ + @After + public void teardown() { + try { + if (activator != null) { + activator.terminate(); + } + } catch (final PolicyPapException exp) { + LOGGER.error("teardown failed", exp); + } + } + @Test public void testPapActivator() throws PolicyPapException { final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" }; - final PapCommandLineArguments arguments = new PapCommandLineArguments(papConfigParameters); - final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments); + activator = new PapActivator(parGroup); + try { + activator.initialize(); + assertTrue(activator.getParameterGroup().isValid()); + assertEquals(CommonTestData.PAP_GROUP_NAME, activator.getParameterGroup().getName()); + } catch (final Exception exp) { + LOGGER.error("testPapActivator failed", exp); + fail("Test should not throw an exception"); + } + } - final PapActivator activator = new PapActivator(parGroup); + @Test(expected = PolicyPapException.class) + public void testPapActivatorError() throws PolicyPapException { + final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" }; + final PapCommandLineArguments arguments = new PapCommandLineArguments(papConfigParameters); + final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments); + activator = new PapActivator(parGroup); activator.initialize(); assertTrue(activator.getParameterGroup().isValid()); - assertEquals(CommonTestData.PAP_GROUP_NAME, activator.getParameterGroup().getName()); - activator.terminate(); + activator.initialize(); } } diff --git a/main/src/test/resources/parameters/PapConfigParameters_Https.json b/main/src/test/resources/parameters/PapConfigParameters_Https.json new file mode 100644 index 00000000..08d4f4a5 --- /dev/null +++ b/main/src/test/resources/parameters/PapConfigParameters_Https.json @@ -0,0 +1,10 @@ +{ + "name":"PapGroup", + "restServerParameters":{ + "host":"0.0.0.0", + "port":6969, + "userName":"healthcheck", + "password":"zb!XztG34", + "https":true + } +} diff --git a/main/src/test/resources/ssl/policy-keystore b/main/src/test/resources/ssl/policy-keystore Binary files differnew file mode 100644 index 00000000..7d2b1ecc --- /dev/null +++ b/main/src/test/resources/ssl/policy-keystore |