diff options
author | Jorge Hernandez <jh1730@att.com> | 2017-09-15 20:36:06 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2017-09-15 20:36:06 +0000 |
commit | fbed3c9c7b816b9fa4dd96dc218a0603b1d1c544 (patch) | |
tree | 3a0f7cac02d328f4f6604beab9282f6f8c6f3b53 /feature-healthcheck/src/test | |
parent | 2d578b67cc872b37f2a709a82be6c83939cdf54a (diff) | |
parent | 0ca3ecd5f8bbcac6152ee4b819d48910148c8c49 (diff) |
Merge "Added junit for feature-healthcheck"
Diffstat (limited to 'feature-healthcheck/src/test')
2 files changed, 239 insertions, 0 deletions
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(); + } + +} |