summaryrefslogtreecommitdiffstats
path: root/main/src/test/java
diff options
context:
space:
mode:
authorramverma <ram.krishna.verma@est.tech>2019-02-07 21:52:23 +0000
committerramverma <ram.krishna.verma@est.tech>2019-02-07 21:52:23 +0000
commit74ead72904c6c9240dbfcf6d86433e32510416f6 (patch)
tree5aa421d52d598a476b6f36a0c050f38ba75f1fd4 /main/src/test/java
parent370c227614be2b63c9383d4eb5a6c064d3d49835 (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/test/java')
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java6
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterGroup.java2
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/rest/TestPapRestServer.java221
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/rest/TestStatisticsReport.java48
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java43
5 files changed, 289 insertions, 31 deletions
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();
}
}