diff options
author | Pamela Dragosh <pdragosh@research.att.com> | 2019-04-12 11:03:44 -0400 |
---|---|---|
committer | Pamela Dragosh <pdragosh@research.att.com> | 2019-04-12 20:28:17 -0400 |
commit | 70736cfbf6ad1a068f8ee53adddd4faa3b6fa8a8 (patch) | |
tree | 6f883bb4cf79cd18eaac33c46446ac86a42934ce /main/src/test/java | |
parent | a5b035d9bb633cf5d520a62c451250db4b018a13 (diff) |
Add statistics and sonar cleanup and blacklist
* Adding in the statistics for decisions and errors.
* Cleaned up sonar issues and added code coverage.
* Sped up JUnit tests
* Fix JUnit issues with not finding application path
* Fix TestDecision not finding persistence.xml
* Fix for lingering statistics from previous runs. That
needs to be addressed at a later time.
* Changed persistence to use properties for configuration
of database rather than hard coding the persistence.xml
* Fix for Josh's comment to use else-if
* Changed to use apache Pair
* Added blacklist guard policy
Issue-ID: POLICY-1440
Change-Id: I56af8c3dcc82463f7381f1eaea7f1440b76200bd
Signed-off-by: Pamela Dragosh <pdragosh@research.att.com>
Diffstat (limited to 'main/src/test/java')
5 files changed, 132 insertions, 37 deletions
diff --git a/main/src/test/java/org/onap/policy/pdpx/main/PolicyXacmlPdpExceptionTest.java b/main/src/test/java/org/onap/policy/pdpx/main/PolicyXacmlPdpExceptionTest.java new file mode 100644 index 00000000..4dd02c62 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pdpx/main/PolicyXacmlPdpExceptionTest.java @@ -0,0 +1,37 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 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. + * 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.pdpx.main; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.onap.policy.common.utils.test.ExceptionsTester; + +public class PolicyXacmlPdpExceptionTest { + + @Test + public void test() { + assertEquals(2, new ExceptionsTester().test(PolicyXacmlPdpException.class)); + } + +} diff --git a/main/src/test/java/org/onap/policy/pdpx/main/PolicyXacmlPdpRuntimeExceptionTest.java b/main/src/test/java/org/onap/policy/pdpx/main/PolicyXacmlPdpRuntimeExceptionTest.java new file mode 100644 index 00000000..80e69747 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pdpx/main/PolicyXacmlPdpRuntimeExceptionTest.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 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. + * 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.pdpx.main; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.onap.policy.common.utils.test.ExceptionsTester; + +public class PolicyXacmlPdpRuntimeExceptionTest { + + @Test + public void test() { + assertEquals(2, new ExceptionsTester().test(PolicyXacmlPdpRuntimeException.class)); + } + + +} diff --git a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestDecision.java b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestDecision.java index b81336a5..3a1e98b1 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestDecision.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestDecision.java @@ -38,17 +38,11 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; -import javax.ws.rs.client.Invocation; -import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; -import org.glassfish.jersey.client.ClientConfig; -import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.ClassRule; @@ -74,6 +68,7 @@ public class TestDecision { private static final Logger LOGGER = LoggerFactory.getLogger(TestDecision.class); private static Main main; + private static HttpClient client; @ClassRule public static final TemporaryFolder appsFolder = new TemporaryFolder(); @@ -81,15 +76,16 @@ public class TestDecision { /** * BeforeClass setup environment. * @throws IOException Cannot create temp apps folder + * @throws Exception exception if service does not start */ @BeforeClass - public static void beforeClass() throws IOException { + public static void beforeClass() throws Exception { System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog"); System.setProperty("org.eclipse.jetty.LEVEL", "OFF"); // // Copy test directory over of the application directories // - Path src = Paths.get("../packages/policy-xacmlpdp-tarball/src/main/resources/apps"); + Path src = Paths.get("src/test/resources/apps"); File apps = appsFolder.newFolder("apps"); Files.walk(src).forEach(source -> { copy(source, apps.toPath().resolve(src.relativize(source))); @@ -109,6 +105,16 @@ public class TestDecision { // Start the service // main = startXacmlPdpService(fileParams); + // + // Make sure it is running + // + if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 20, 1000L)) { + throw new IllegalStateException("Cannot connect to port 6969"); + } + // + // Create a client + // + client = getNoAuthHttpClient(); } @AfterClass @@ -139,7 +145,9 @@ public class TestDecision { } @Test - public void testDecision_Guard() throws InterruptedException, IOException { + public void testDecision_Guard() throws KeyManagementException, NoSuchAlgorithmException, + ClassNotFoundException { + LOGGER.info("Running test testDecision_Guard"); DecisionRequest request = new DecisionRequest(); @@ -167,28 +175,17 @@ public class TestDecision { main.shutdown(); } - private DecisionResponse getDecision(DecisionRequest request) throws InterruptedException, IOException { - final ClientConfig clientConfig = new ClientConfig(); + private DecisionResponse getDecision(DecisionRequest request) { - 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/policy/pdpx/v1/decision"); + Entity<DecisionRequest> entityRequest = Entity.entity(request, MediaType.APPLICATION_JSON); + Response response = client.post("", entityRequest, Collections.emptyMap()); - final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); + assertEquals(200, response.getStatus()); - if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) { - throw new IllegalStateException("Cannot connect to port 6969"); - } - - return invocationBuilder.post(Entity.json(request), DecisionResponse.class); + return HttpClient.getBody(response, DecisionResponse.class); } - private ErrorResponse getErrorDecision(DecisionRequest request) throws KeyManagementException, - NoSuchAlgorithmException, ClassNotFoundException { - - HttpClient client = getNoAuthHttpClient(); + private ErrorResponse getErrorDecision(DecisionRequest request) { Entity<DecisionRequest> entityRequest = Entity.entity(request, MediaType.APPLICATION_JSON); Response response = client.post("", entityRequest, Collections.emptyMap()); @@ -198,7 +195,7 @@ public class TestDecision { return HttpClient.getBody(response, ErrorResponse.class); } - private HttpClient getNoAuthHttpClient() + private static HttpClient getNoAuthHttpClient() throws KeyManagementException, NoSuchAlgorithmException, ClassNotFoundException { return HttpClient.factory.build(BusTopicParams.builder() .clientName("testDecisionClient") diff --git a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpRestServer.java b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpRestServer.java index 0deab9d0..f9f2abf3 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpRestServer.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpRestServer.java @@ -44,8 +44,10 @@ import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.junit.After; import org.junit.BeforeClass; import org.junit.ClassRule; +import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.runners.MethodSorters; import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClientException; import org.onap.policy.common.endpoints.report.HealthCheckReport; import org.onap.policy.common.utils.network.NetworkUtil; @@ -61,6 +63,7 @@ import org.slf4j.LoggerFactory; * Class to perform unit test of {@link XacmlPdpRestServer}. * */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class TestXacmlPdpRestServer { private static final Logger LOGGER = LoggerFactory.getLogger(TestXacmlPdpRestServer.class); @@ -99,10 +102,12 @@ public class TestXacmlPdpRestServer { if (NetworkUtil.isTcpPortOpen("localhost", 6969, 1, 1000L)) { if (main != null) { stopXacmlPdpService(main); + main = null; } if (restServer != null) { restServer.stop(); + restServer = null; } } } catch (IOException | PolicyXacmlPdpException e) { @@ -114,69 +119,84 @@ public class TestXacmlPdpRestServer { } @Test - public void testHealthCheckSuccess() throws IOException, InterruptedException, TopicSinkClientException { + public void test1HealthCheckSuccess() throws IOException, InterruptedException, TopicSinkClientException { + LOGGER.info("***************************** Running test1HealthCheckSuccess *****************************"); main = startXacmlPdpService(true); final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT); final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class); + LOGGER.info("test1HealthCheckSuccess health report {}", report); validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report); } @Test - public void testHealthCheckFailure() throws InterruptedException, IOException { + public void test7HealthCheckFailure() throws InterruptedException, IOException { + LOGGER.info("***************************** Running test7HealthCheckFailure *****************************"); final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); restServerParams.setName(CommonTestData.PDPX_GROUP_NAME); restServer = new XacmlPdpRestServer(restServerParams, applicationPath.getAbsolutePath()); restServer.start(); final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT); final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class); + LOGGER.info("test7HealthCheckFailure health report {}", report); validateHealthCheckReport(NAME, SELF, false, 500, NOT_ALIVE, report); assertTrue(restServer.isAlive()); assertTrue(restServer.toString().startsWith("XacmlPdpRestServer [servers=")); } @Test - public void testHttpsHealthCheckSuccess() throws Exception { + public void test2HttpsHealthCheckSuccess() throws Exception { + LOGGER.info("**************************** Running test2HttpsHealthCheckSuccess ****************************"); main = startXacmlPdpService(false); final Invocation.Builder invocationBuilder = sendHttpsRequest(HEALTHCHECK_ENDPOINT); final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class); + LOGGER.info("test2HttpsHealthCheckSuccess health report {}", report); validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report); } @Test - public void testStatistics_200() throws IOException, InterruptedException, TopicSinkClientException { + public void test4Statistics_200() throws IOException, InterruptedException, TopicSinkClientException { + LOGGER.info("***************************** Running test4Statistics_200 *****************************"); + XacmlPdpStatisticsManager.resetAllStatistics(); main = startXacmlPdpService(true); Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT); StatisticsReport report = invocationBuilder.get(StatisticsReport.class); + LOGGER.info("test4Statistics_200 health report {}", report); validateStatisticsReport(report, 0, 200); updateXacmlPdpStatistics(); invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT); report = invocationBuilder.get(StatisticsReport.class); + LOGGER.info("test4Statistics_200 health report {}", report); validateStatisticsReport(report, 1, 200); XacmlPdpStatisticsManager.resetAllStatistics(); } @Test - public void testStatistics_500() throws IOException, InterruptedException { + public void test5Statistics_500() throws IOException, InterruptedException { + LOGGER.info("***************************** Running test5Statistics_500 *****************************"); final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); restServerParams.setName(CommonTestData.PDPX_GROUP_NAME); restServer = new XacmlPdpRestServer(restServerParams, applicationPath.getAbsolutePath()); restServer.start(); final Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT); final StatisticsReport report = invocationBuilder.get(StatisticsReport.class); + LOGGER.info("test5Statistics_500 health report {}", report); validateStatisticsReport(report, 0, 500); XacmlPdpStatisticsManager.resetAllStatistics(); } @Test - public void testHttpsStatistic() throws Exception { + public void test6HttpsStatistic() throws Exception { + LOGGER.info("***************************** Running test6HttpsStatistic *****************************"); main = startXacmlPdpService(false); final Invocation.Builder invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT); final StatisticsReport report = invocationBuilder.get(StatisticsReport.class); + LOGGER.info("test6HttpsStatistic health report {}", report); validateStatisticsReport(report, 0, 200); } @Test - public void testStatisticsConstructorIsPrivate() { + public void test3StatisticsConstructorIsPrivate() { + LOGGER.info("************************* Running test3StatisticsConstructorIsPrivate *************************"); try { final Constructor<XacmlPdpStatisticsManager> constructor = XacmlPdpStatisticsManager.class.getDeclaredConstructor(); @@ -224,9 +244,10 @@ public class TestXacmlPdpRestServer { 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", 6969, 20, 1000L)) { + throw new IllegalStateException("Cannot connect to port 6969"); } + return invocationBuilder; } diff --git a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpStatistics.java b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpStatistics.java index 5b08aa80..6a762924 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpStatistics.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpStatistics.java @@ -76,6 +76,7 @@ public class TestXacmlPdpStatistics { @Test public void testXacmlPdpStatistics_200() throws PolicyXacmlPdpException, InterruptedException { try { + LOGGER.info("*************************** Running testXacmlPdpStatistics_200 ***************************"); final Main main = startXacmlPdpService(); StatisticsReport report = getXacmlPdpStatistics(); validateReport(report, 0, 200); @@ -93,6 +94,7 @@ public class TestXacmlPdpStatistics { @Test public void testXacmlPdpStatistics_500() throws InterruptedException { + LOGGER.info("***************************** Running testXacmlPdpStatistics_500 *****************************"); final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); restServerParams.setName(CommonTestData.PDPX_GROUP_NAME); final XacmlPdpRestServer restServer = new XacmlPdpRestServer(restServerParams, @@ -133,7 +135,7 @@ public class TestXacmlPdpStatistics { final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); - if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) { + if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 20, 1000L)) { throw new IllegalStateException("Cannot connect to port 6969"); } |