summaryrefslogtreecommitdiffstats
path: root/main
diff options
context:
space:
mode:
authorramverma <ram.krishna.verma@est.tech>2019-02-05 10:28:22 +0000
committerramverma <ram.krishna.verma@est.tech>2019-02-05 10:28:22 +0000
commit370c227614be2b63c9383d4eb5a6c064d3d49835 (patch)
tree105047c7ec00b42f4793a99ade2f9d3bae89ef10 /main
parent07b7ba00a053a26a5eef39070bff5297c630c5ce (diff)
Adding healthcheck endpoint to policy/pap
1) Adding healthcheck REST endpoint to policy/pap using the policy-endpoints module in policy/common. 2) Added the related unit test cases. Change-Id: I6a215cceccc9cd42494aef1dfcdd46f0f3fd7d13 Issue-ID: POLICY-1477 Signed-off-by: ramverma <ram.krishna.verma@est.tech>
Diffstat (limited to 'main')
-rw-r--r--main/pom.xml5
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterGroup.java20
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterHandler.java7
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/parameters/RestServerParameters.java138
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/HealthCheckProvider.java52
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/PapRestController.java59
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/PapRestServer.java141
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/startstop/Main.java7
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java28
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java21
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterGroup.java32
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/rest/TestPapRestServer.java125
-rw-r--r--main/src/test/resources/parameters/MinimumParameters.json8
-rw-r--r--main/src/test/resources/parameters/PapConfigParameters.json8
-rw-r--r--main/src/test/resources/parameters/PapConfigParameters_InvalidName.json8
15 files changed, 639 insertions, 20 deletions
diff --git a/main/pom.xml b/main/pom.xml
index e36c042e..f9f4b391 100644
--- a/main/pom.xml
+++ b/main/pom.xml
@@ -51,6 +51,11 @@
<artifactId>utils</artifactId>
<version>${policy.common.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.onap.policy.common</groupId>
+ <artifactId>policy-endpoints</artifactId>
+ <version>${policy.common.version}</version>
+ </dependency>
</dependencies>
<build>
diff --git a/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterGroup.java b/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterGroup.java
index 01032c56..09f87049 100644
--- a/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterGroup.java
+++ b/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterGroup.java
@@ -32,14 +32,17 @@ import org.onap.policy.common.utils.validation.ParameterValidationUtils;
*/
public class PapParameterGroup implements ParameterGroup {
private String name;
+ private RestServerParameters restServerParameters;
/**
* Create the pap parameter group.
*
* @param name the parameter group name
+ * @param restServerParameters the rest server parameters
*/
- public PapParameterGroup(final String name) {
+ public PapParameterGroup(final String name, final RestServerParameters restServerParameters) {
this.name = name;
+ this.restServerParameters = restServerParameters;
}
/**
@@ -63,6 +66,15 @@ public class PapParameterGroup implements ParameterGroup {
}
/**
+ * Return the restServerParameters of this parameter group instance.
+ *
+ * @return the restServerParameters
+ */
+ public RestServerParameters getRestServerParameters() {
+ return restServerParameters;
+ }
+
+ /**
* Validate the parameter group.
*
* @return the result of the validation
@@ -73,6 +85,12 @@ public class PapParameterGroup implements ParameterGroup {
if (!ParameterValidationUtils.validateStringParameter(name)) {
validationResult.setResult("name", ValidationStatus.INVALID, "must be a non-blank string");
}
+ if (restServerParameters == null) {
+ validationResult.setResult("restServerParameters", ValidationStatus.INVALID,
+ "must have restServerParameters to configure pap rest server");
+ } else {
+ validationResult.setResult("restServerParameters", restServerParameters.validate());
+ }
return validationResult;
}
}
diff --git a/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterHandler.java b/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterHandler.java
index f397daed..08124bb3 100644
--- a/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterHandler.java
+++ b/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterHandler.java
@@ -28,8 +28,8 @@ import java.io.FileReader;
import org.onap.policy.common.parameters.GroupValidationResult;
import org.onap.policy.pap.main.PolicyPapException;
import org.onap.policy.pap.main.startstop.PapCommandLineArguments;
-import org.slf4j.ext.XLogger;
-import org.slf4j.ext.XLoggerFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* This class handles reading, parsing and validating of policy pap parameters from JSON files.
@@ -37,7 +37,8 @@ import org.slf4j.ext.XLoggerFactory;
* @author Ram Krishna Verma (ram.krishna.verma@est.tech)
*/
public class PapParameterHandler {
- private static final XLogger LOGGER = XLoggerFactory.getXLogger(PapParameterHandler.class);
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(PapParameterHandler.class);
/**
* Read the parameters from the parameter file.
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
new file mode 100644
index 00000000..cfc8832b
--- /dev/null
+++ b/main/src/main/java/org/onap/policy/pap/main/parameters/RestServerParameters.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.parameters;
+
+import org.onap.policy.common.parameters.GroupValidationResult;
+import org.onap.policy.common.parameters.ParameterGroup;
+import org.onap.policy.common.parameters.ValidationStatus;
+import org.onap.policy.common.utils.validation.ParameterValidationUtils;
+
+/**
+ * Class to hold all parameters needed for pap rest server.
+ *
+ * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
+ */
+public class RestServerParameters implements ParameterGroup {
+ private String name;
+ private String host;
+ private int port;
+ private String userName;
+ private String password;
+
+ /**
+ * Constructor for instantiating RestServerParameters.
+ *
+ * @param host the host name
+ * @param port the port
+ * @param userName the user name
+ * @param password the password
+ */
+ public RestServerParameters(final String host, final int port, final String userName, final String password) {
+ super();
+ this.host = host;
+ this.port = port;
+ this.userName = userName;
+ this.password = password;
+ }
+
+ /**
+ * Return the name of this RestServerParameters instance.
+ *
+ * @return name the name of this RestServerParameters
+ */
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * Return the host of this RestServerParameters instance.
+ *
+ * @return the host
+ */
+ public String getHost() {
+ return host;
+ }
+
+ /**
+ * Return the port of this RestServerParameters instance.
+ *
+ * @return the port
+ */
+ public int getPort() {
+ return port;
+ }
+
+ /**
+ * Return the user name of this RestServerParameters instance.
+ *
+ * @return the userName
+ */
+ public String getUserName() {
+ return userName;
+ }
+
+ /**
+ * Return the password of this RestServerParameters instance.
+ *
+ * @return the password
+ */
+ public String getPassword() {
+ return password;
+ }
+
+ /**
+ * Set the name of this RestServerParameters instance.
+ *
+ * @param name the name to set
+ */
+ @Override
+ public void setName(final String name) {
+ this.name = name;
+ }
+
+ /**
+ * Validate the rest server parameters.
+ *
+ * @return the result of the validation
+ */
+ @Override
+ public GroupValidationResult validate() {
+ final GroupValidationResult validationResult = new GroupValidationResult(this);
+ if (!ParameterValidationUtils.validateStringParameter(host)) {
+ validationResult.setResult("host", ValidationStatus.INVALID,
+ "must be a non-blank string containing hostname/ipaddress of the pap rest server");
+ }
+ if (!ParameterValidationUtils.validateStringParameter(userName)) {
+ validationResult.setResult("userName", ValidationStatus.INVALID,
+ "must be a non-blank string containing userName for pap rest server credentials");
+ }
+ if (!ParameterValidationUtils.validateStringParameter(password)) {
+ validationResult.setResult("password", ValidationStatus.INVALID,
+ "must be a non-blank string containing password for pap rest server credentials");
+ }
+ if (!ParameterValidationUtils.validateIntParameter(port)) {
+ validationResult.setResult("port", ValidationStatus.INVALID,
+ "must be a positive integer containing port of the pap rest server");
+ }
+ return validationResult;
+ }
+}
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/HealthCheckProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/HealthCheckProvider.java
new file mode 100644
index 00000000..d67d0d5b
--- /dev/null
+++ b/main/src/main/java/org/onap/policy/pap/main/rest/HealthCheckProvider.java
@@ -0,0 +1,52 @@
+/*-
+ * ============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.report.HealthCheckReport;
+import org.onap.policy.pap.main.startstop.PapActivator;
+
+/**
+ * Class to fetch health check of PAP service.
+ *
+ * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
+ */
+public class HealthCheckProvider {
+
+ private static final String NOT_ALIVE = "not alive";
+ private static final String ALIVE = "alive";
+ private static final String URL = "self";
+ private static final String NAME = "Policy PAP";
+
+ /**
+ * Performs the health check of PAP service.
+ *
+ * @return Report containing health check status
+ */
+ public HealthCheckReport performHealthCheck() {
+ final HealthCheckReport report = new HealthCheckReport();
+ report.setName(NAME);
+ report.setUrl(URL);
+ report.setHealthy(PapActivator.isAlive());
+ report.setCode(PapActivator.isAlive() ? 200 : 500);
+ report.setMessage(PapActivator.isAlive() ? ALIVE : NOT_ALIVE);
+ return report;
+ }
+}
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
new file mode 100644
index 00000000..226dc356
--- /dev/null
+++ b/main/src/main/java/org/onap/policy/pap/main/rest/PapRestController.java
@@ -0,0 +1,59 @@
+/*-
+ * ============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 io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.Info;
+import io.swagger.annotations.SwaggerDefinition;
+import io.swagger.annotations.Tag;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.onap.policy.common.endpoints.report.HealthCheckReport;
+
+/**
+ * Class to provide REST endpoints for PAP component.
+ *
+ * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
+ */
+@Path("/")
+@Api
+@Produces(MediaType.APPLICATION_JSON)
+@SwaggerDefinition(info = @Info(description = "Policy Pap Service", version = "v1.0", title = "Policy Pap"),
+ consumes = { MediaType.APPLICATION_JSON }, produces = { MediaType.APPLICATION_JSON },
+ schemes = { SwaggerDefinition.Scheme.HTTP },
+ tags = { @Tag(name = "policy-pap", description = "Policy Pap Service Operations") })
+public class PapRestController {
+
+ @GET
+ @Path("healthcheck")
+ @Produces(MediaType.APPLICATION_JSON)
+ @ApiOperation(value = "Perform a system healthcheck", notes = "Provides healthy status of the Policy Pap component",
+ response = HealthCheckReport.class)
+ public Response healthcheck() {
+ return Response.status(Response.Status.OK).entity(new HealthCheckProvider().performHealthCheck()).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
new file mode 100644
index 00000000..39f65f24
--- /dev/null
+++ b/main/src/main/java/org/onap/policy/pap/main/rest/PapRestServer.java
@@ -0,0 +1,141 @@
+/*-
+ * ============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 java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import org.onap.policy.common.capabilities.Startable;
+import org.onap.policy.common.endpoints.http.server.HttpServletServer;
+import org.onap.policy.pap.main.parameters.RestServerParameters;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Class to manage life cycle of PAP rest server.
+ *
+ * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
+ */
+public class PapRestServer implements Startable {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(PapRestServer.class);
+
+ private static final String SEPARATOR = ".";
+ private static final String HTTP_SERVER_SERVICES = "http.server.services";
+
+ private List<HttpServletServer> servers = new ArrayList<>();
+
+ private RestServerParameters restServerParameters;
+
+ /**
+ * Constructor for instantiating PapRestServer.
+ *
+ * @param restServerParameters the rest server parameters
+ */
+ public PapRestServer(final RestServerParameters restServerParameters) {
+ this.restServerParameters = restServerParameters;
+ }
+
+ /**
+ * {@inheritDoc}.
+ */
+ @Override
+ public boolean start() {
+ try {
+ servers = HttpServletServer.factory.build(getServerProperties());
+ for (final HttpServletServer server : servers) {
+ server.start();
+ }
+ } catch (final Exception exp) {
+ LOGGER.error("Failed to start pap http server", exp);
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Creates the server properties object using restServerParameters.
+ *
+ * @return the properties object
+ */
+ private Properties getServerProperties() {
+ final Properties props = new Properties();
+ props.setProperty(HTTP_SERVER_SERVICES, restServerParameters.getName());
+ props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".host",
+ restServerParameters.getHost());
+ props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".port",
+ Integer.toString(restServerParameters.getPort()));
+ props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".restClasses",
+ PapRestController.class.getCanonicalName());
+ props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".managed", "false");
+ props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".swagger", "true");
+ props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".userName",
+ restServerParameters.getUserName());
+ props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password",
+ restServerParameters.getPassword());
+ return props;
+ }
+
+ /**
+ * {@inheritDoc}.
+ */
+ @Override
+ public boolean stop() {
+ for (final HttpServletServer server : servers) {
+ try {
+ server.stop();
+ } catch (final Exception exp) {
+ LOGGER.error("Failed to stop pap http server", exp);
+ }
+ }
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}.
+ */
+ @Override
+ public void shutdown() {
+ stop();
+ }
+
+ /**
+ * {@inheritDoc}.
+ */
+ @Override
+ public boolean isAlive() {
+ return !servers.isEmpty();
+ }
+
+ /**
+ * {@inheritDoc}.
+ */
+ @Override
+ public String toString() {
+ final StringBuilder builder = new StringBuilder();
+ builder.append("PapRestServer [servers=");
+ builder.append(servers);
+ builder.append("]");
+ return builder.toString();
+ }
+
+}
diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java b/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java
index 63d41a02..602e04df 100644
--- a/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java
+++ b/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java
@@ -25,8 +25,8 @@ import java.util.Arrays;
import org.onap.policy.pap.main.PolicyPapException;
import org.onap.policy.pap.main.parameters.PapParameterGroup;
import org.onap.policy.pap.main.parameters.PapParameterHandler;
-import org.slf4j.ext.XLogger;
-import org.slf4j.ext.XLoggerFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* This class initiates ONAP Policy Framework PAP component.
@@ -34,7 +34,8 @@ import org.slf4j.ext.XLoggerFactory;
* @author Ram Krishna Verma (ram.krishna.verma@est.tech)
*/
public class Main {
- private static final XLogger LOGGER = XLoggerFactory.getXLogger(Main.class);
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
private PapActivator activator;
private PapParameterGroup parameterGroup;
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 8aec2adf..b1674efb 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
@@ -23,8 +23,9 @@ package org.onap.policy.pap.main.startstop;
import org.onap.policy.common.parameters.ParameterService;
import org.onap.policy.pap.main.PolicyPapException;
import org.onap.policy.pap.main.parameters.PapParameterGroup;
-import org.slf4j.ext.XLogger;
-import org.slf4j.ext.XLoggerFactory;
+import org.onap.policy.pap.main.rest.PapRestServer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* This class wraps a distributor so that it can be activated as a complete service together with all its pap and
@@ -33,13 +34,12 @@ import org.slf4j.ext.XLoggerFactory;
* @author Ram Krishna Verma (ram.krishna.verma@est.tech)
*/
public class PapActivator {
- // The logger for this class
- private static final XLogger LOGGER = XLoggerFactory.getXLogger(PapActivator.class);
- // The parameters of this policy pap activator
- private final PapParameterGroup papParameterGroup;
+ private static final Logger LOGGER = LoggerFactory.getLogger(PapActivator.class);
+ private final PapParameterGroup papParameterGroup;
private static boolean alive = false;
+ private PapRestServer restServer;
/**
* Instantiate the activator for policy pap as a complete service.
@@ -58,6 +58,7 @@ public class PapActivator {
public void initialize() throws PolicyPapException {
try {
LOGGER.debug("Policy pap starting as a service . . .");
+ startPapRestServer();
registerToParameterService(papParameterGroup);
PapActivator.setAlive(true);
LOGGER.debug("Policy pap started as a service");
@@ -77,6 +78,8 @@ public class PapActivator {
deregisterToParameterService(papParameterGroup);
PapActivator.setAlive(false);
+ // Stop the pap rest server
+ restServer.stop();
} catch (final Exception exp) {
LOGGER.error("Policy pap service termination failed", exp);
throw new PolicyPapException(exp.getMessage(), exp);
@@ -127,4 +130,17 @@ public class PapActivator {
public static void setAlive(final boolean status) {
alive = status;
}
+
+ /**
+ * Starts the pap rest server using configuration parameters.
+ *
+ * @throws PolicyPapException if server start fails
+ */
+ private void startPapRestServer() throws PolicyPapException {
+ papParameterGroup.getRestServerParameters().setName(papParameterGroup.getName());
+ restServer = new PapRestServer(papParameterGroup.getRestServerParameters());
+ if (!restServer.start()) {
+ throw new PolicyPapException("Failed to start pap rest server. Check log for more details...");
+ }
+ }
}
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 5e62ba68..0e4ae523 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
@@ -27,6 +27,27 @@ package org.onap.policy.pap.main.parameters;
*/
public class CommonTestData {
+ 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";
public static final String PAP_GROUP_NAME = "PapGroup";
+ /**
+ * Returns an instance of RestServerParameters for test cases.
+ *
+ * @param isEmpty boolean value to represent that object created should be empty or not
+ * @return the restServerParameters object
+ */
+ public RestServerParameters getRestServerParameters(final boolean isEmpty) {
+ final RestServerParameters restServerParameters;
+ if (!isEmpty) {
+ restServerParameters = new RestServerParameters(REST_SERVER_HOST, REST_SERVER_PORT, REST_SERVER_USER,
+ REST_SERVER_PASSWORD);
+ } else {
+ restServerParameters = new RestServerParameters(null, 0, null, null);
+ }
+ 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 086d7c22..7d5355a0 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
@@ -33,18 +33,26 @@ import org.onap.policy.common.parameters.GroupValidationResult;
* @author Ram Krishna Verma (ram.krishna.verma@est.tech)
*/
public class TestPapParameterGroup {
+ CommonTestData commonTestData = new CommonTestData();
@Test
public void testPapParameterGroup() {
- final PapParameterGroup papParameters = new PapParameterGroup(CommonTestData.PAP_GROUP_NAME);
+ final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false);
+ final PapParameterGroup papParameters =
+ new PapParameterGroup(CommonTestData.PAP_GROUP_NAME, restServerParameters);
final GroupValidationResult validationResult = papParameters.validate();
assertTrue(validationResult.isValid());
assertEquals(CommonTestData.PAP_GROUP_NAME, papParameters.getName());
+ assertEquals(restServerParameters.getHost(), papParameters.getRestServerParameters().getHost());
+ assertEquals(restServerParameters.getPort(), papParameters.getRestServerParameters().getPort());
+ assertEquals(restServerParameters.getUserName(), papParameters.getRestServerParameters().getUserName());
+ assertEquals(restServerParameters.getPassword(), papParameters.getRestServerParameters().getPassword());
}
@Test
public void testPapParameterGroup_NullName() {
- final PapParameterGroup papParameters = new PapParameterGroup(null);
+ final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false);
+ final PapParameterGroup papParameters = new PapParameterGroup(null, restServerParameters);
final GroupValidationResult validationResult = papParameters.validate();
assertFalse(validationResult.isValid());
assertEquals(null, papParameters.getName());
@@ -54,7 +62,8 @@ public class TestPapParameterGroup {
@Test
public void testPapParameterGroup_EmptyName() {
- final PapParameterGroup papParameters = new PapParameterGroup("");
+ final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false);
+ final PapParameterGroup papParameters = new PapParameterGroup("", restServerParameters);
final GroupValidationResult validationResult = papParameters.validate();
assertFalse(validationResult.isValid());
assertEquals("", papParameters.getName());
@@ -64,10 +73,25 @@ public class TestPapParameterGroup {
@Test
public void testPapParameterGroup_SetName() {
- final PapParameterGroup papParameters = new PapParameterGroup(CommonTestData.PAP_GROUP_NAME);
+ final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false);
+ final PapParameterGroup papParameters =
+ new PapParameterGroup(CommonTestData.PAP_GROUP_NAME, restServerParameters);
papParameters.setName("PapNewGroup");
final GroupValidationResult validationResult = papParameters.validate();
assertTrue(validationResult.isValid());
assertEquals("PapNewGroup", papParameters.getName());
}
+
+ @Test
+ public void testApiParameterGroup_EmptyRestServerParameters() {
+ final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(true);
+
+ final PapParameterGroup papParameters =
+ new PapParameterGroup(CommonTestData.PAP_GROUP_NAME, restServerParameters);
+ final GroupValidationResult validationResult = papParameters.validate();
+ assertFalse(validationResult.isValid());
+ assertTrue(validationResult.getResult()
+ .contains("\"org.onap.policy.pap.main.parameters.RestServerParameters\" INVALID, "
+ + "parameter group has status INVALID"));
+ }
}
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
new file mode 100644
index 00000000..b604e206
--- /dev/null
+++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPapRestServer.java
@@ -0,0 +1,125 @@
+/*-
+ * ============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 static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Invocation;
+import javax.ws.rs.client.WebTarget;
+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.onap.policy.common.endpoints.report.HealthCheckReport;
+import org.onap.policy.common.utils.network.NetworkUtil;
+import org.onap.policy.pap.main.PolicyPapException;
+import org.onap.policy.pap.main.parameters.CommonTestData;
+import org.onap.policy.pap.main.parameters.RestServerParameters;
+import org.onap.policy.pap.main.startstop.Main;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Class to perform unit test of {@link PapRestServer}.
+ *
+ * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
+ */
+public class TestPapRestServer {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(TestPapRestServer.class);
+ private static final String NOT_ALIVE = "not alive";
+ private static final String ALIVE = "alive";
+ private static final String SELF = "self";
+ private static final String NAME = "Policy PAP";
+
+ @Test
+ public void testHealthCheckSuccess() throws PolicyPapException, InterruptedException {
+ final String reportString = "Report [name=Policy PAP, url=self, healthy=true, code=200, message=alive]";
+ try {
+ final Main main = startPapService();
+ final HealthCheckReport report = performHealthCheck();
+ validateReport(NAME, SELF, true, 200, ALIVE, reportString, report);
+ stopPapService(main);
+ } catch (final Exception exp) {
+ LOGGER.error("testHealthCheckSuccess failed", exp);
+ fail("Test should not throw an exception");
+ }
+ }
+
+ @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();
+ }
+
+ private Main startPapService() {
+ final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" };
+ return new Main(papConfigParameters);
+ }
+
+ private void stopPapService(final Main main) throws PolicyPapException {
+ main.shutdown();
+ }
+
+ private HealthCheckReport performHealthCheck() throws InterruptedException, IOException {
+ HealthCheckReport response = null;
+ 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 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;
+ }
+
+ private void validateReport(final String name, final String url, final boolean healthy, final int code,
+ final String message, final String reportString, 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/resources/parameters/MinimumParameters.json b/main/src/test/resources/parameters/MinimumParameters.json
index ec79d586..34a8f802 100644
--- a/main/src/test/resources/parameters/MinimumParameters.json
+++ b/main/src/test/resources/parameters/MinimumParameters.json
@@ -1,3 +1,9 @@
{
- "name":"PapGroup"
+ "name":"PapGroup",
+ "restServerParameters":{
+ "host":"0.0.0.0",
+ "port":6969,
+ "userName":"healthcheck",
+ "password":"zb!XztG34"
+ }
}
diff --git a/main/src/test/resources/parameters/PapConfigParameters.json b/main/src/test/resources/parameters/PapConfigParameters.json
index 88d00517..7e415591 100644
--- a/main/src/test/resources/parameters/PapConfigParameters.json
+++ b/main/src/test/resources/parameters/PapConfigParameters.json
@@ -1,3 +1,9 @@
{
- "name":"PapGroup"
+ "name":"PapGroup",
+ "restServerParameters":{
+ "host":"0.0.0.0",
+ "port":6969,
+ "userName":"healthcheck",
+ "password":"zb!XztG34"
+ }
}
diff --git a/main/src/test/resources/parameters/PapConfigParameters_InvalidName.json b/main/src/test/resources/parameters/PapConfigParameters_InvalidName.json
index 1a466d01..80fb8232 100644
--- a/main/src/test/resources/parameters/PapConfigParameters_InvalidName.json
+++ b/main/src/test/resources/parameters/PapConfigParameters_InvalidName.json
@@ -1,3 +1,9 @@
{
- "name":" "
+ "name":" ",
+ "restServerParameters":{
+ "host":"0.0.0.0",
+ "port":6969,
+ "userName":"healthcheck",
+ "password":"zb!XztG34"
+ }
}