diff options
Diffstat (limited to 'feature-healthcheck')
2 files changed, 44 insertions, 37 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 index 7cf7ed5f..90413041 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 @@ -23,6 +23,7 @@ package org.onap.policy.drools.healthcheck; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doAnswer; @@ -115,58 +116,31 @@ public class HealthCheckFeatureTest { var reports = healthcheck(manager); serverChecks(reports); checkReports(reports, List.of("STUCK"), - HttpStatus.OK_200, HttpStatus.getMessage(200)); + HttpStatus.OK_200, HttpStatus.getMessage(200)); checkReports(reports, List.of("echo"), 1, "[echo:{java.lang.String=1}]"); /* mock controller and clients stuck */ RestMockHealthCheck.stuck = true; // make the server named STUCK unresponsive doAnswer(AdditionalAnswers - .answersWithDelay((manager.getTimeoutSeconds() + 2) * 1000L, - invocationOnMock -> new HashMap<String, Integer>())) - .when(manager).getFactTypes(any(), any()); + .answersWithDelay((manager.getTimeoutSeconds() + 2) * 1000L, + invocationOnMock -> new HashMap<String, Integer>())) + .when(manager).getFactTypes(any(), any()); reports = healthcheck(manager); RestMockHealthCheck.stuck = false; // unstuck the server named STUCK serverChecks(reports); checkReports(reports, List.of("STUCK"), - HealthCheckManager.TIMEOUT_CODE, HealthCheckManager.TIMEOUT_MESSAGE); + HealthCheckManager.TIMEOUT_CODE, HealthCheckManager.TIMEOUT_MESSAGE); - assertTrue(RestMockHealthCheck.WAIT * 1000 > HealthCheckManagerTest.select(reports, "STUCK", - HealthCheckManager.TIMEOUT_CODE, HealthCheckManager.TIMEOUT_MESSAGE) - .get(0).getElapsedTime()); + assertTrue(RestMockHealthCheck.wait * 1000 > HealthCheckManagerTest.select(reports, "STUCK", + HealthCheckManager.TIMEOUT_CODE, HealthCheckManager.TIMEOUT_MESSAGE) + .get(0).getElapsedTime()); feature.afterShutdown(PolicyEngineConstants.getManager()); } - private void checkReports(Reports reports, List<String> reportNames, int code, String message) { - reportNames - .forEach(name -> assertEquals(1, - HealthCheckManagerTest.select(reports, name, code, message).size())); - } - - private Reports healthcheck(HealthCheck manager) { - var reports = manager.healthCheck(); - logger.info("{}", reports); - return reports; - } - - private void checkOpen(int port) throws InterruptedException { - if (!NetworkUtil.isTcpPortOpen("localhost", port, 5, 10000L)) { - throw new IllegalStateException("cannot connect to port " + port); - } - } - - private void serverChecks(Reports reports) { - checkReports(reports, List.of("HEALTHCHECK", "LIVENESS"), - HttpStatus.OK_200, HttpStatus.getMessage(200)); - checkReports(reports, List.of("UNAUTH"), - HttpStatus.UNAUTHORIZED_401, HttpStatus.getMessage(401)); - checkReports(reports, List.of(HealthCheckManager.ENGINE_NAME), - HealthCheckManager.SUCCESS_CODE, HealthCheckManager.ENABLED_MESSAGE); - } - @Test void testGetSequenceNumber() { assertEquals(1000, new HealthCheckFeature().getSequenceNumber()); @@ -218,6 +192,38 @@ public class HealthCheckFeatureTest { assertFalse(feature.afterShutdown(null)); } + @Test + void testGetManager() { + assertNotNull(new HealthCheckFeature().getManager()); + } + + private void checkReports(Reports reports, List<String> reportNames, int code, String message) { + reportNames + .forEach(name -> assertEquals(1, + HealthCheckManagerTest.select(reports, name, code, message).size())); + } + + private Reports healthcheck(HealthCheck manager) { + var reports = manager.healthCheck(); + logger.info("{}", reports); + return reports; + } + + private void checkOpen(int port) throws InterruptedException { + if (!NetworkUtil.isTcpPortOpen("localhost", port, 5, 10000L)) { + throw new IllegalStateException("cannot connect to port " + port); + } + } + + private void serverChecks(Reports reports) { + checkReports(reports, List.of("HEALTHCHECK", "LIVENESS"), + HttpStatus.OK_200, HttpStatus.getMessage(200)); + checkReports(reports, List.of("UNAUTH"), + HttpStatus.UNAUTHORIZED_401, HttpStatus.getMessage(401)); + checkReports(reports, List.of(HealthCheckManager.ENGINE_NAME), + HealthCheckManager.SUCCESS_CODE, HealthCheckManager.ENABLED_MESSAGE); + } + /** * Feature that returns a particular monitor. */ 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 index d2376d9d..ace32dc4 100644 --- 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 @@ -3,6 +3,7 @@ * ONAP * ================================================================================ * Copyright (C) 2017-2018,2022 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,7 +37,7 @@ public class RestMockHealthCheck { protected static final String OK_MESSAGE = "All Alive"; protected static volatile boolean stuck = false; - protected static volatile long WAIT = 15; + protected static volatile long wait = 15; @GET @Path("healthcheck/test") @@ -49,7 +50,7 @@ public class RestMockHealthCheck { @Path("healthcheck/stuck") @Produces({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML}) public Response stuck() { - await().atMost(WAIT, TimeUnit.SECONDS).until(() -> !stuck); + await().atMost(wait, TimeUnit.SECONDS).until(() -> !stuck); return Response.status(Status.OK).entity("I may be stuck: " + stuck).build(); } } |