Skip to content

Commit 05b3e13

Browse files
committed
WIP - unit tests for exception advice
1 parent 6db52ef commit 05b3e13

File tree

4 files changed

+78
-44
lines changed

4 files changed

+78
-44
lines changed

grpc-server-spring-boot-autoconfigure/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ dependencies {
1616
api project(':grpc-common-spring-boot')
1717
api 'org.springframework.boot:spring-boot-starter'
1818
optionalSupportImplementation 'org.springframework.boot:spring-boot-starter-actuator'
19+
optionalSupportImplementation 'org.springframework.boot:spring-boot-starter-aop'
1920
optionalSupportImplementation 'org.springframework.security:spring-security-core'
2021
optionalSupportImplementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
2122
optionalSupportImplementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery'
2223
optionalSupportImplementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
2324
optionalSupportImplementation "com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery:${springCloudAlibabaNacosVersion}"
24-
optionalSupportImplementation 'com.google.inject:guice:4.2.3' // Only needed to avoid some warnings during compilation (for eureka)
25+
// Only needed to avoid some warnings during compilation (for eureka)
26+
optionalSupportImplementation 'com.google.inject:guice:4.2.3'
2527
optionalSupportImplementation 'io.zipkin.brave:brave-instrumentation-grpc'
2628
optionalSupportApi 'io.grpc:grpc-netty'
2729
api 'io.grpc:grpc-netty-shaded'
@@ -37,5 +39,6 @@ dependencies {
3739
exclude module: 'junit'
3840
}
3941

42+
runtimeOnly 'org.aspectj:aspectjweaver'
4043
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
4144
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2016-2020 Michael Zhang <[email protected]>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package net.devh.boot.grpc.test.config;
19+
20+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
21+
import org.springframework.context.annotation.Configuration;
22+
23+
import net.devh.boot.grpc.server.service.exceptionhandling.GrpcExceptionHandlerMethodResolver;
24+
import net.devh.boot.grpc.server.service.exceptionhandling.GrpcServiceAdviceDiscoverer;
25+
import net.devh.boot.grpc.server.service.exceptionhandling.GrpcServiceAdviceExceptionHandler;
26+
import net.devh.boot.grpc.server.service.exceptionhandling.GrpcServiceAdviceIsPresent;
27+
28+
@Configuration
29+
@ImportAutoConfiguration({
30+
GrpcServiceAdviceIsPresent.class,
31+
GrpcServiceAdviceDiscoverer.class,
32+
GrpcExceptionHandlerMethodResolver.class,
33+
GrpcServiceAdviceExceptionHandler.class})
34+
public class BaseExceptionAdviceAutoConfiguration {
35+
36+
}

tests/src/test/java/net/devh/boot/grpc/test/config/GrpcServiceAdviceConfig.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,38 @@
1717

1818
package net.devh.boot.grpc.test.config;
1919

20-
import java.util.concurrent.atomic.AtomicBoolean;
21-
22-
import org.springframework.context.annotation.Bean;
2320
import org.springframework.context.annotation.Configuration;
2421

2522
import com.google.protobuf.Empty;
2623

24+
import io.grpc.Status;
2725
import io.grpc.stub.StreamObserver;
2826
import net.devh.boot.grpc.server.service.GrpcService;
27+
import net.devh.boot.grpc.server.service.exceptionhandling.GrpcExceptionHandler;
28+
import net.devh.boot.grpc.server.service.exceptionhandling.GrpcServiceAdvice;
2929
import net.devh.boot.grpc.test.proto.SomeType;
30-
import net.devh.boot.grpc.test.proto.TestServiceGrpc.TestServiceImplBase;
30+
import net.devh.boot.grpc.test.proto.TestServiceGrpc;
3131

3232
@Configuration
3333
public class GrpcServiceAdviceConfig {
3434

35-
@Bean
36-
AtomicBoolean invocationCheck() {
37-
return new AtomicBoolean(false);
38-
}
3935

4036
@GrpcService
41-
TestServiceImplBase testServiceImplBase(final AtomicBoolean invocationCheck) {
42-
return new TestServiceImplBase() {
43-
44-
@Override
45-
public void normal(final Empty request, final StreamObserver<SomeType> responseObserver) {
46-
throw new IllegalArgumentException("Trigger GrpcServiceAdvice");
47-
// invocationCheck.set(true);
48-
// final SomeType version = SomeType.newBuilder().setVersion("1.2.3").build();
49-
// responseObserver.onNext(version);
50-
// responseObserver.onCompleted();
51-
}
52-
53-
};
37+
public class TestGrpcService extends TestServiceGrpc.TestServiceImplBase {
38+
39+
@Override
40+
public void normal(final Empty request, final StreamObserver<SomeType> responseObserver) {
41+
throw new IllegalArgumentException("Trigger GrpcServiceAdvice");
42+
}
43+
}
44+
45+
@GrpcServiceAdvice
46+
public class TestAdvice {
47+
48+
@GrpcExceptionHandler
49+
public Status handleIllegalArgumentException(IllegalArgumentException e) {
50+
return Status.NOT_FOUND.withCause(e).withDescription(e.getMessage());
51+
}
5452
}
5553

5654
}

tests/src/test/java/net/devh/boot/grpc/test/setup/GrpcServiceAdviceTest.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,25 @@
1717

1818
package net.devh.boot.grpc.test.setup;
1919

20-
import static org.junit.jupiter.api.Assertions.assertFalse;
21-
import static org.junit.jupiter.api.Assertions.assertTrue;
22-
2320
import java.util.concurrent.ExecutionException;
24-
import java.util.concurrent.atomic.AtomicBoolean;
2521

22+
import org.assertj.core.api.Assertions;
23+
import org.junit.jupiter.api.Disabled;
2624
import org.junit.jupiter.api.Test;
2725
import org.springframework.beans.factory.annotation.Autowired;
2826
import org.springframework.boot.test.context.SpringBootTest;
2927
import org.springframework.context.annotation.Configuration;
3028
import org.springframework.test.annotation.DirtiesContext;
3129
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3230

31+
import io.grpc.Status;
32+
import io.grpc.StatusRuntimeException;
3333
import lombok.extern.slf4j.Slf4j;
3434
import net.devh.boot.grpc.server.service.exceptionhandling.GrpcServiceAdvice;
35-
import net.devh.boot.grpc.server.service.exceptionhandling.GrpcServiceAdviceExceptionHandler;
3635
import net.devh.boot.grpc.test.config.BaseAutoConfiguration;
36+
import net.devh.boot.grpc.test.config.BaseExceptionAdviceAutoConfiguration;
3737
import net.devh.boot.grpc.test.config.GrpcServiceAdviceConfig;
38+
import net.devh.boot.grpc.test.config.GrpcServiceAdviceConfig.TestGrpcService;
3839
import net.devh.boot.grpc.test.config.InProcessConfiguration;
3940

4041
/**
@@ -48,37 +49,33 @@
4849
InProcessConfiguration.class,
4950
GrpcServiceAdviceConfig.class,
5051
BaseAutoConfiguration.class,
51-
GrpcServiceAdviceExceptionHandler.class})
52+
BaseExceptionAdviceAutoConfiguration.class})
5253
@DirtiesContext
5354
class GrpcServiceAdviceTest extends AbstractSimpleServerClientTest {
5455

5556
public GrpcServiceAdviceTest() {
5657
log.info("--- GrpcServiceAdviceTest ---");
5758
}
5859

59-
60-
// @GrpcServiceAdvice
61-
// class TestAdvice {
62-
//
63-
// @GrpcExceptionHandler
64-
// public StatusRuntimeException throwSomeError(IllegalArgumentException e) {
65-
// return Status.NOT_FOUND.withDescription("Something not found").withCause(e).asRuntimeException();
66-
// }
67-
// }
68-
6960
@Autowired
70-
GrpcServiceAdviceExceptionHandler grpcServiceAdviceExceptionHandler;
61+
private GrpcServiceAdviceConfig.TestAdvice testAdvice;
7162

7263
@Autowired
73-
AtomicBoolean invoked;
64+
private TestGrpcService testGrpcService;
65+
7466

75-
// @Override
67+
@Disabled
7668
@Test
69+
@Override
7770
void testSuccessfulCall() throws InterruptedException, ExecutionException {
7871

79-
assertFalse(this.invoked.get());
80-
super.testSuccessfulCall();
81-
assertTrue(this.invoked.get());
72+
// TODO - @Aspect in GrpcServiceAdviceExceptionHandler not triggered
73+
Assertions.assertThatThrownBy(super::testSuccessfulCall)
74+
.isInstanceOf(StatusRuntimeException.class)
75+
.hasMessage(Status.NOT_FOUND.toString())
76+
.getCause()
77+
.isInstanceOf(IllegalArgumentException.class)
78+
.hasMessage("Trigger GrpcServiceAdvice");
8279
}
8380

8481
}

0 commit comments

Comments
 (0)