From 11d16601a97c7332a3e57792f82114941a83de21 Mon Sep 17 00:00:00 2001 From: Alexander Furer Date: Mon, 4 Sep 2023 09:13:14 +0300 Subject: [PATCH] more tests (ref #373) --- .../GRpcStatusRuntimeExceptionTest.java | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/recovery/GRpcStatusRuntimeExceptionTest.java b/grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/recovery/GRpcStatusRuntimeExceptionTest.java index 5156752..7932518 100644 --- a/grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/recovery/GRpcStatusRuntimeExceptionTest.java +++ b/grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/recovery/GRpcStatusRuntimeExceptionTest.java @@ -49,10 +49,17 @@ static class CustomService extends CustomServiceGrpc.CustomServiceImplBase { @Override public void anotherCustom(Custom.CustomRequest request, StreamObserver responseObserver) { - if("NPE".equals(request.getName())){ + if ("NPE".equals(request.getName())) { ExceptionUtilsKt.throwException(new NullPointerException()); } - ExceptionUtilsKt.throwException(Status.FAILED_PRECONDITION); + if ("RT_ERROR".equals(request.getName())) { + responseObserver.onError(Status.NOT_FOUND.asRuntimeException()); + } + if ("ERROR".equals(request.getName())) { + responseObserver.onError(Status.OUT_OF_RANGE.asException()); + } else { + ExceptionUtilsKt.throwException(Status.FAILED_PRECONDITION); + } } @Override @@ -65,8 +72,9 @@ public void custom(Custom.CustomRequest request, StreamObserver customStream(StreamObserver responseObserver) { throw new StatusRuntimeException(Status.FAILED_PRECONDITION); } + @GRpcExceptionHandler - public Status handle(NullPointerException e, GRpcExceptionScope scope ){ + public Status handle(NullPointerException e, GRpcExceptionScope scope) { return Status.DATA_LOSS; } @@ -129,4 +137,22 @@ public void npeExceptionTest() { assertThat(statusRuntimeException.getStatus(), is(Status.DATA_LOSS)); } + @Test + public void errorExceptionTest() { + final StatusRuntimeException statusRuntimeException = assertThrows(StatusRuntimeException.class, () -> + CustomServiceGrpc.newBlockingStub(getChannel()).anotherCustom( + Custom.CustomRequest.newBuilder().setName("ERROR").build() + ) + ); + assertThat(statusRuntimeException.getStatus(), is(Status.OUT_OF_RANGE)); + } + public void rtErrorExceptionTest() { + final StatusRuntimeException statusRuntimeException = assertThrows(StatusRuntimeException.class, () -> + CustomServiceGrpc.newBlockingStub(getChannel()).anotherCustom( + Custom.CustomRequest.newBuilder().setName("RT_ERROR").build() + ) + ); + assertThat(statusRuntimeException.getStatus(), is(Status.NOT_FOUND)); + } + }