Skip to content

Commit

Permalink
replace usage of var and status imports
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesmst committed May 10, 2024
1 parent 134e004 commit 183c22b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

package net.devh.boot.grpc.server.health;

import java.util.Objects;

import static io.grpc.Status.NOT_FOUND;

import org.springframework.boot.actuate.health.HealthComponent;
import org.springframework.boot.actuate.health.HealthEndpoint;
import org.springframework.boot.actuate.health.Status;

import io.grpc.Status;
import io.grpc.StatusException;
import io.grpc.health.v1.HealthCheckRequest;
import io.grpc.health.v1.HealthCheckResponse;
Expand All @@ -37,20 +39,20 @@ public ActuatorGrpcHealth(HealthEndpoint healthEndpoint) {
public void check(HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) {

if (!request.getService().isEmpty()) {
var health = healthEndpoint.healthForPath(request.getService());
HealthComponent health = healthEndpoint.healthForPath(request.getService());
if (health == null) {
responseObserver.onError(new StatusException(
Status.NOT_FOUND.withDescription("unknown service " + request.getService())));
responseObserver.onError(
new StatusException(NOT_FOUND.withDescription("unknown service " + request.getService())));
return;
}
var status = health.getStatus();
Status status = health.getStatus();
HealthCheckResponse.ServingStatus result = resolveStatus(status);
HealthCheckResponse response = HealthCheckResponse.newBuilder().setStatus(result).build();
responseObserver.onNext(response);
responseObserver.onCompleted();
} else {

var status = healthEndpoint.health().getStatus();
Status status = healthEndpoint.health().getStatus();
HealthCheckResponse.ServingStatus result = resolveStatus(status);
HealthCheckResponse response = HealthCheckResponse.newBuilder().setStatus(result).build();
responseObserver.onNext(response);
Expand All @@ -59,12 +61,11 @@ public void check(HealthCheckRequest request, StreamObserver<HealthCheckResponse

}

private HealthCheckResponse.ServingStatus resolveStatus(org.springframework.boot.actuate.health.Status status) {
if (Objects.equals(org.springframework.boot.actuate.health.Status.UP.getCode(), status.getCode())) {
private HealthCheckResponse.ServingStatus resolveStatus(Status status) {
if (Status.UP.equals(status)) {
return HealthCheckResponse.ServingStatus.SERVING;
}
if (Objects.equals(org.springframework.boot.actuate.health.Status.DOWN.getCode(), status.getCode()) || Objects
.equals(org.springframework.boot.actuate.health.Status.OUT_OF_SERVICE.getCode(), status.getCode())) {
if (Status.DOWN.equals(status) || Status.OUT_OF_SERVICE.equals(status)) {
return HealthCheckResponse.ServingStatus.NOT_SERVING;
}
return HealthCheckResponse.ServingStatus.UNKNOWN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void testNotFoundService() throws InterruptedException {
.setService("someservice")
.build(), resultObserver);

var error = resultObserver.getError();
Throwable error = resultObserver.getError();
assertInstanceOf(StatusRuntimeException.class, error);
assertEquals(Status.NOT_FOUND.getCode(), ((StatusRuntimeException) error).getStatus().getCode());
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ void testSpecificServiceNotFound() {

assertEquals(0, response.getValues().size());

var error = response.getError();
Throwable error = response.getError();
assertNotNull(error);
assertInstanceOf(StatusException.class, error);

var statusException = (StatusException) error;
StatusException statusException = (StatusException) error;
assertEquals(io.grpc.Status.NOT_FOUND.getCode(), statusException.getStatus().getCode());
}

Expand Down

0 comments on commit 183c22b

Please sign in to comment.