-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added HealthService interface and its implementation, unit test and i…
…ntegration with Server Signed-off-by: Alfredo Gutierrez <[email protected]>
- Loading branch information
1 parent
49b7244
commit 5633e3a
Showing
5 changed files
with
263 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
server/src/main/java/com/hedera/block/server/health/HealthService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.hedera.block.server.health; | ||
|
||
import io.helidon.webserver.http.HttpRules; | ||
import io.helidon.webserver.http.HttpService; | ||
import io.helidon.webserver.http.ServerRequest; | ||
import io.helidon.webserver.http.ServerResponse; | ||
|
||
/** Defines the contract for health http service, needed for implementing an standard */ | ||
public interface HealthService extends HttpService { | ||
/** The path for the health group endpoints. Root path for all health endpoints. | ||
* | ||
* @return the root path for the health group endpoints | ||
* */ | ||
String getHealthRootPath(); | ||
|
||
/** | ||
* Configures the health routes for the server. | ||
* | ||
* @param httpRules is used to configure the health endpoints routes | ||
*/ | ||
@Override | ||
void routing(HttpRules httpRules); | ||
|
||
void handleLiveness(ServerRequest req, ServerResponse res); | ||
|
||
void handleReadiness(ServerRequest req, ServerResponse res); | ||
} |
76 changes: 76 additions & 0 deletions
76
server/src/main/java/com/hedera/block/server/health/HealthServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.hedera.block.server.health; | ||
|
||
import com.hedera.block.server.ServiceStatus; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import io.helidon.webserver.http.HttpRules; | ||
import io.helidon.webserver.http.ServerRequest; | ||
import io.helidon.webserver.http.ServerResponse; | ||
|
||
/** Provides implementation for the health endpoints of the server. */ | ||
public class HealthServiceImpl implements HealthService { | ||
|
||
private static final String LIVENESS_PATH = "/liveness"; | ||
private static final String READINESS_PATH = "/readiness"; | ||
|
||
private final ServiceStatus serviceStatus; | ||
|
||
/** | ||
* It initializes the HealthService with needed dependencies. | ||
* | ||
* @param serviceStatus is used to check the status of the service | ||
*/ | ||
public HealthServiceImpl(@NonNull ServiceStatus serviceStatus) { | ||
this.serviceStatus = serviceStatus; | ||
} | ||
|
||
@Override | ||
public String getHealthRootPath() { | ||
return "/health"; | ||
} | ||
|
||
/** | ||
* Configures the health routes for the server. | ||
* | ||
* @param httpRules is used to configure the health endpoints routes | ||
*/ | ||
@Override | ||
public void routing(HttpRules httpRules) { | ||
httpRules | ||
.get(LIVENESS_PATH, this::handleLiveness) | ||
.get(READINESS_PATH, this::handleReadiness); | ||
} | ||
|
||
@Override | ||
public void handleLiveness(ServerRequest req, ServerResponse res) { | ||
if (serviceStatus.isRunning()) { | ||
res.status(200).send("OK"); | ||
} else { | ||
res.status(503).send("Service is not running"); | ||
} | ||
} | ||
|
||
@Override | ||
public void handleReadiness(ServerRequest req, ServerResponse res) { | ||
if (serviceStatus.isRunning()) { | ||
res.status(200).send("OK"); | ||
} else { | ||
res.status(503).send("Service is not running"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
server/src/test/java/com/hedera/block/server/health/HealthServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.hedera.block.server.health; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.*; | ||
|
||
import com.hedera.block.server.ServiceStatus; | ||
import io.helidon.webserver.http.HttpRules; | ||
import io.helidon.webserver.http.ServerRequest; | ||
import io.helidon.webserver.http.ServerResponse; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class HealthServiceTest { | ||
|
||
private static final String READINESS_PATH = "/readiness"; | ||
private static final String LIVENESS_PATH = "/liveness"; | ||
private static final String HEALTH_PATH = "/health"; | ||
|
||
@Mock private ServiceStatus serviceStatus; | ||
|
||
@Mock ServerRequest serverRequest; | ||
|
||
@Mock ServerResponse serverResponse; | ||
|
||
@Test | ||
public void testHandleLiveness() { | ||
// given | ||
when(serviceStatus.isRunning()).thenReturn(true); | ||
when(serverResponse.status(200)).thenReturn(serverResponse); | ||
doNothing().when(serverResponse).send("OK"); | ||
HealthService healthService = new HealthServiceImpl(serviceStatus); | ||
|
||
// when | ||
healthService.handleLiveness(serverRequest, serverResponse); | ||
|
||
// then | ||
verify(serverResponse, times(1)).status(200); | ||
verify(serverResponse, times(1)).send("OK"); | ||
} | ||
|
||
@Test | ||
public void testHandleLiveness_notRunning() { | ||
// given | ||
when(serviceStatus.isRunning()).thenReturn(false); | ||
when(serverResponse.status(503)).thenReturn(serverResponse); | ||
doNothing().when(serverResponse).send("Service is not running"); | ||
HealthService healthService = new HealthServiceImpl(serviceStatus); | ||
|
||
// when | ||
healthService.handleLiveness(serverRequest, serverResponse); | ||
|
||
// then | ||
verify(serverResponse, times(1)).status(503); | ||
verify(serverResponse, times(1)).send("Service is not running"); | ||
} | ||
|
||
@Test | ||
public void testHandleReadiness() { | ||
// given | ||
when(serviceStatus.isRunning()).thenReturn(true); | ||
when(serverResponse.status(200)).thenReturn(serverResponse); | ||
doNothing().when(serverResponse).send("OK"); | ||
HealthService healthService = new HealthServiceImpl(serviceStatus); | ||
|
||
// when | ||
healthService.handleReadiness(serverRequest, serverResponse); | ||
|
||
// then | ||
verify(serverResponse, times(1)).status(200); | ||
verify(serverResponse, times(1)).send("OK"); | ||
} | ||
|
||
@Test | ||
public void testHandleReadiness_notRunning() { | ||
// given | ||
when(serviceStatus.isRunning()).thenReturn(false); | ||
when(serverResponse.status(503)).thenReturn(serverResponse); | ||
doNothing().when(serverResponse).send("Service is not running"); | ||
HealthService healthService = new HealthServiceImpl(serviceStatus); | ||
|
||
// when | ||
healthService.handleReadiness(serverRequest, serverResponse); | ||
|
||
// then | ||
verify(serverResponse, times(1)).status(503); | ||
verify(serverResponse, times(1)).send("Service is not running"); | ||
} | ||
|
||
@Test | ||
public void testRouting() { | ||
// given | ||
HealthService healthService = new HealthServiceImpl(serviceStatus); | ||
HttpRules httpRules = mock(HttpRules.class); | ||
when(httpRules.get(anyString(), any())).thenReturn(httpRules); | ||
|
||
// when | ||
healthService.routing(httpRules); | ||
|
||
// then | ||
verify(httpRules, times(1)).get(eq(LIVENESS_PATH), any()); | ||
verify(httpRules, times(1)).get(eq(READINESS_PATH), any()); | ||
assertEquals(HEALTH_PATH, healthService.getHealthRootPath()); | ||
} | ||
} |