diff options
Diffstat (limited to 'feature-healthcheck/src')
3 files changed, 174 insertions, 28 deletions
diff --git a/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheckFeature.java b/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheckFeature.java index 3a79a55c..8e7a66ef 100644 --- a/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheckFeature.java +++ b/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheckFeature.java @@ -48,7 +48,7 @@ public class HealthCheckFeature implements PolicyEngineFeatureAPI { @Override public boolean afterStart(PolicyEngine engine) { try { - HealthCheck.monitor.start(); + getMonitor().start(); } catch (IllegalStateException e) { logger.error("Healthcheck Monitor cannot be started", e); } @@ -59,7 +59,7 @@ public class HealthCheckFeature implements PolicyEngineFeatureAPI { @Override public boolean afterShutdown(PolicyEngine engine) { try { - HealthCheck.monitor.stop(); + getMonitor().stop(); } catch (IllegalStateException e) { logger.error("Healthcheck Monitor cannot be stopped", e); } 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 index e0312f1f..42d77f26 100644 --- 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 @@ -7,9 +7,9 @@ * 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. @@ -21,7 +21,12 @@ 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 static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import java.io.File; import java.io.FileWriter; @@ -30,7 +35,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Properties; - import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -50,11 +54,13 @@ public class HealthCheckFeatureTest { */ 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 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"); - private static final Path healthCheckPropsBackupPath = Paths - .get(SystemPersistence.manager.getConfigurationPath().toString(), HEALTH_CHECK_PROPERTIES_FILE + ".bak"); + private static final String EXPECTED = "expected exception"; /** @@ -65,47 +71,47 @@ public class HealthCheckFeatureTest { private static Properties httpProperties = new Properties(); /** - * Set up. + * Set up. */ @BeforeClass public static void setup() { httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, "HEALTHCHECK"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" - + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, "localhost"); + + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, "localhost"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" - + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7777"); + + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7777"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" - + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "username"); + + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "username"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" - + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, "password"); + + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, "password"); httpProperties.setProperty( - PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" - + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, - org.onap.policy.drools.healthcheck.RestMockHealthCheck.class.getName()); + PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" + + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, + org.onap.policy.drools.healthcheck.RestMockHealthCheck.class.getName()); httpProperties.setProperty( - PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" - + PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX, - org.onap.policy.drools.healthcheck.TestAafHealthCheckFilter.class.getName()); + PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" + + PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX, + org.onap.policy.drools.healthcheck.TestAafHealthCheckFilter.class.getName()); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" - + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true"); + + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES, "HEALTHCHECK"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" - + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, "localhost"); + + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, "localhost"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" - + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7777"); + + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7777"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" - + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, "healthcheck/test"); + + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, "healthcheck/test"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" - + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, "false"); + + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, "false"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" - + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "username"); + + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "username"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" - + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, "password"); + + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, "password"); httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" - + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true"); + + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true"); configDirSetup(); @@ -148,6 +154,41 @@ public class HealthCheckFeatureTest { } + @Test + public void testGetSequenceNumber() { + assertEquals(1000, new HealthCheckFeature().getSequenceNumber()); + } + + @Test + public void testAfterStart() { + HealthCheck checker = mock(HealthCheck.class); + HealthCheckFeature feature = new HealthCheckFeatureImpl(checker); + + // without exception + assertFalse(feature.afterStart(null)); + verify(checker).start(); + verify(checker, never()).stop(); + + // with exception + doThrow(new IllegalStateException(EXPECTED)).when(checker).start(); + assertFalse(feature.afterStart(null)); + } + + @Test + public void testAfterShutdown() { + HealthCheck checker = mock(HealthCheck.class); + HealthCheckFeature feature = new HealthCheckFeatureImpl(checker); + + // without exception + assertFalse(feature.afterShutdown(null)); + verify(checker).stop(); + verify(checker, never()).start(); + + // with exception + doThrow(new IllegalStateException(EXPECTED)).when(checker).stop(); + assertFalse(feature.afterShutdown(null)); + } + /** * setup up config directory. @@ -191,4 +232,20 @@ public class HealthCheckFeatureTest { } } + /** + * Feature that returns a particular monitor. + */ + private static class HealthCheckFeatureImpl extends HealthCheckFeature { + private final HealthCheck checker; + + public HealthCheckFeatureImpl(HealthCheck checker) { + this.checker = checker; + } + + @Override + public HealthCheck getMonitor() { + return checker; + } + + } } diff --git a/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/HealthCheckTest.java b/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/HealthCheckTest.java new file mode 100644 index 00000000..850b1edf --- /dev/null +++ b/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/HealthCheckTest.java @@ -0,0 +1,89 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2018 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.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.Collections; +import java.util.List; +import org.junit.Test; +import org.onap.policy.drools.healthcheck.HealthCheck.Report; +import org.onap.policy.drools.healthcheck.HealthCheck.Reports; + +public class HealthCheckTest { + + private static final int RPT_CODE = 100; + private static final String RPT_MSG = "report-message"; + private static final String RPT_NAME = "report-name"; + private static final String RPT_URL = "report-url"; + + @Test + public void testHealthCheck_Report() { + Report rpt = new Report(); + + // toString should work with un-populated data + assertNotNull(rpt.toString()); + + rpt.setCode(RPT_CODE); + rpt.setHealthy(true); + rpt.setMessage(RPT_MSG); + rpt.setName(RPT_NAME); + rpt.setUrl(RPT_URL); + + assertEquals(RPT_CODE, rpt.getCode()); + assertEquals(true, rpt.isHealthy()); + assertEquals(RPT_MSG, rpt.getMessage()); + assertEquals(RPT_NAME, rpt.getName()); + assertEquals(RPT_URL, rpt.getUrl()); + + // flip the flag + rpt.setHealthy(false); + assertEquals(false, rpt.isHealthy()); + + // toString should work with populated data + assertNotNull(rpt.toString()); + } + + @Test + public void testHealthCheck_Reports() { + Reports reports = new Reports(); + + // toString should work with un-populated data + assertNotNull(reports.toString()); + + List<Report> lst = Collections.emptyList(); + reports.setDetails(lst); + reports.setHealthy(true); + + assertTrue(lst == reports.getDetails()); + assertEquals(true, reports.isHealthy()); + + // flip the flag + reports.setHealthy(false); + assertEquals(false, reports.isHealthy()); + + // toString should work with populated data + assertNotNull(reports.toString()); + } + +} |