From 0ca3ecd5f8bbcac6152ee4b819d48910148c8c49 Mon Sep 17 00:00:00 2001 From: "Magnusen, Drew (dm741q)" Date: Tue, 12 Sep 2017 10:45:58 -0500 Subject: Added junit for feature-healthcheck Added a junit test increase coverage of the healthcheck feature. Issue-ID: POLICY-227 Change-Id: I3f37e7fab15f2f3072b442b43b79b8241c61e306 Signed-off-by: Magnusen, Drew (dm741q) --- feature-healthcheck/pom.xml | 47 ++--- .../drools/healthcheck/HealthCheckFeatureTest.java | 198 +++++++++++++++++++++ .../drools/healthcheck/RestMockHealthCheck.java | 41 +++++ 3 files changed, 265 insertions(+), 21 deletions(-) create mode 100644 feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/HealthCheckFeatureTest.java create mode 100644 feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/RestMockHealthCheck.java diff --git a/feature-healthcheck/pom.xml b/feature-healthcheck/pom.xml index 32aa4d1c..6fd81f84 100644 --- a/feature-healthcheck/pom.xml +++ b/feature-healthcheck/pom.xml @@ -93,28 +93,33 @@ - io.swagger - swagger-jersey2-jaxrs - provided + io.swagger + swagger-jersey2-jaxrs + provided + + + org.onap.policy.drools-pdp + policy-core + ${project.version} + provided + + + org.onap.policy.drools-pdp + policy-endpoints + ${project.version} + provided + + + org.onap.policy.drools-pdp + policy-management + ${project.version} + provided + + + junit + junit + test - - org.onap.policy.drools-pdp - policy-core - ${project.version} - provided - - - org.onap.policy.drools-pdp - policy-endpoints - ${project.version} - provided - - - org.onap.policy.drools-pdp - policy-management - ${project.version} - provided - diff --git a/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/HealthCheckFeatureTest.java b/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/HealthCheckFeatureTest.java new file mode 100644 index 00000000..a618f033 --- /dev/null +++ b/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/HealthCheckFeatureTest.java @@ -0,0 +1,198 @@ +/*- + * ============LICENSE_START======================================================= + * feature-healthcheck + * ================================================================================ + * Copyright (C) 2017 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.healthcheck; + + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.util.Properties; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; +import org.onap.policy.drools.healthcheck.HealthCheck.Report; +import org.onap.policy.drools.healthcheck.HealthCheck.Reports; +import org.onap.policy.drools.http.client.HttpClient; +import org.onap.policy.drools.http.server.HttpServletServer; +import org.onap.policy.drools.persistence.SystemPersistence; +import org.onap.policy.drools.properties.PolicyProperties; +import org.onap.policy.drools.system.PolicyEngine; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HealthCheckFeatureTest { + + /** + * Healthcheck Configuration File + */ + private static final String HEALTH_CHECK_PROPERTIES_FILE = "feature-healthcheck.properties"; + + private static final Path healthCheckPropsPath = Paths.get(SystemPersistence.manager.getConfigurationPath().toString(), + HEALTH_CHECK_PROPERTIES_FILE); + + private static final Path healthCheckPropsBackupPath = Paths.get(SystemPersistence.manager.getConfigurationPath().toString(), + HEALTH_CHECK_PROPERTIES_FILE + ".bak"); + + + /** + * logger + */ + private static Logger logger = LoggerFactory.getLogger(HealthCheckFeatureTest.class); + + private static Properties httpProperties = new Properties(); + + + @BeforeClass + public static void setup(){ + + httpProperties.setProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES, "HEALTHCHECK"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_HOST_SUFFIX, + "localhost"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_PORT_SUFFIX, + "7777"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, + "username"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, + "password"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, + org.onap.policy.drools.healthcheck.RestMockHealthCheck.class.getName()); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_MANAGED_SUFFIX, + "true"); + + + httpProperties.setProperty(PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES, "HEALTHCHECK"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_HOST_SUFFIX, + "localhost"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_PORT_SUFFIX, + "7777"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_URL_SUFFIX, + "healthcheck/test"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_HTTPS_SUFFIX, + "false"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, + "username"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, + "password"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_MANAGED_SUFFIX, + "true"); + + + configDirSetup(); + + } + + @AfterClass + public static void tearDown() { + logger.info("-- tearDown() --"); + + configDirCleanup(); + } + + @Test + public void test() { + + HealthCheckFeature feature = new HealthCheckFeature(); + feature.afterStart(PolicyEngine.manager); + + Reports reports = HealthCheck.monitor.healthCheck(); + + for (Report rpt : reports.details) { + if (rpt.name == "HEALTHCHECK") { + assertTrue(rpt.healthy); + assertEquals(200,rpt.code); + assertEquals("All Alive", rpt.message); + break; + } + } + + feature.afterShutdown(PolicyEngine.manager); + + } + + + /** + * setup up config directory + */ + protected static void configDirSetup() { + + File origPropsFile = new File(healthCheckPropsPath.toString()); + File backupPropsFile = new File(healthCheckPropsBackupPath.toString()); + Path configDir = Paths.get(SystemPersistence.DEFAULT_CONFIGURATION_DIR); + + try { + + if (Files.notExists(configDir)) { + Files.createDirectories(configDir); + } + + Files.deleteIfExists(healthCheckPropsBackupPath); + origPropsFile.renameTo(backupPropsFile); + + FileWriter writer = new FileWriter(origPropsFile); + httpProperties.store(writer,"Machine created healthcheck-feature Properties"); + + } catch (final Exception e) { + logger.info("Problem cleaning {}", healthCheckPropsPath, e); + } + } + + /** + * cleanup up config directory + */ + protected static void configDirCleanup() { + + File origPropsFile = new File(healthCheckPropsBackupPath.toString()); + File backupPropsFile = new File(healthCheckPropsPath.toString()); + + try { + Files.deleteIfExists(healthCheckPropsPath); + origPropsFile.renameTo(backupPropsFile); + } catch (final Exception e) { + logger.info("Problem cleaning {}", healthCheckPropsPath, e); + } + } + +} diff --git a/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/RestMockHealthCheck.java b/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/RestMockHealthCheck.java new file mode 100644 index 00000000..5b3b82b3 --- /dev/null +++ b/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/RestMockHealthCheck.java @@ -0,0 +1,41 @@ +/*- + * ============LICENSE_START======================================================= + * feature-healthcheck + * ================================================================================ + * Copyright (C) 2017 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.healthcheck; + + +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 javax.ws.rs.core.Response.Status; + +@Path("/") +public class RestMockHealthCheck { + + @GET + @Path("healthcheck/test") + @Produces(MediaType.APPLICATION_JSON) + public Response papHealthCheck() { + return Response.status(Status.OK).entity("All Alive").build(); + } + +} -- cgit 1.2.3-korg