diff options
author | ramverma <ram.krishna.verma@est.tech> | 2019-02-05 10:28:22 +0000 |
---|---|---|
committer | ramverma <ram.krishna.verma@est.tech> | 2019-02-05 10:28:22 +0000 |
commit | 370c227614be2b63c9383d4eb5a6c064d3d49835 (patch) | |
tree | 105047c7ec00b42f4793a99ade2f9d3bae89ef10 /main/src/test/java/org | |
parent | 07b7ba00a053a26a5eef39070bff5297c630c5ce (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/src/test/java/org')
3 files changed, 174 insertions, 4 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 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()); + } +} |