Skip to content

Commit c994337

Browse files
committed
Add support for @GrpcService annotations in @configuration (Fixes #406)
1 parent a6ab22b commit c994337

File tree

4 files changed

+129
-5
lines changed

4 files changed

+129
-5
lines changed

grpc-server-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/server/service/GrpcService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.lang.annotation.RetentionPolicy;
2424
import java.lang.annotation.Target;
2525

26+
import org.springframework.context.annotation.Bean;
2627
import org.springframework.stereotype.Service;
2728

2829
import io.grpc.BindableService;
@@ -40,12 +41,12 @@
4041
* </p>
4142
*
4243
* @author Michael ([email protected])
43-
* @since 5/17/16
4444
*/
45-
@Target(ElementType.TYPE)
45+
@Target({ElementType.TYPE, ElementType.METHOD})
4646
@Retention(RetentionPolicy.RUNTIME)
4747
@Documented
4848
@Service
49+
@Bean
4950
public @interface GrpcService {
5051

5152
/**
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 java.util.concurrent.atomic.AtomicBoolean;
21+
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
25+
import com.google.protobuf.Empty;
26+
27+
import io.grpc.stub.StreamObserver;
28+
import net.devh.boot.grpc.server.service.GrpcService;
29+
import net.devh.boot.grpc.test.proto.SomeType;
30+
import net.devh.boot.grpc.test.proto.TestServiceGrpc.TestServiceImplBase;
31+
32+
@Configuration
33+
public class BeanAnnotatedServiceConfig {
34+
35+
@Bean
36+
AtomicBoolean invocationCheck() {
37+
return new AtomicBoolean(false);
38+
}
39+
40+
@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+
invocationCheck.set(true);
47+
final SomeType version = SomeType.newBuilder().setVersion("1.2.3").build();
48+
responseObserver.onNext(version);
49+
responseObserver.onCompleted();
50+
}
51+
52+
};
53+
}
54+
55+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public abstract class AbstractSimpleServerClientTest {
6161
protected TestServiceFutureStub testServiceFutureStub;
6262

6363
@PostConstruct
64-
public void init() {
64+
protected void init() {
6565
// Test injection
6666
assertNotNull(this.channel, "channel");
6767
assertNotNull(this.testServiceBlockingStub, "testServiceBlockingStub");
@@ -77,7 +77,7 @@ public void init() {
7777
*/
7878
@Test
7979
@DirtiesContext
80-
public void testSuccessfulCall() throws InterruptedException, ExecutionException {
80+
void testSuccessfulCall() throws InterruptedException, ExecutionException {
8181
log.info("--- Starting tests with successful call ---");
8282
assertEquals("1.2.3",
8383
TestServiceGrpc.newBlockingStub(this.channel).normal(Empty.getDefaultInstance()).getVersion());
@@ -95,7 +95,7 @@ public void testSuccessfulCall() throws InterruptedException, ExecutionException
9595
*/
9696
@Test
9797
@DirtiesContext
98-
public void testFailingCall() {
98+
void testFailingCall() {
9999
log.info("--- Starting tests with failing call ---");
100100
assertThrowsStatus(UNIMPLEMENTED,
101101
() -> TestServiceGrpc.newBlockingStub(this.channel).unimplemented(Empty.getDefaultInstance()));
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.setup;
19+
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
23+
import java.util.concurrent.ExecutionException;
24+
import java.util.concurrent.atomic.AtomicBoolean;
25+
26+
import org.junit.jupiter.api.Test;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.boot.test.context.SpringBootTest;
29+
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.test.annotation.DirtiesContext;
31+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
32+
33+
import lombok.extern.slf4j.Slf4j;
34+
import net.devh.boot.grpc.server.service.GrpcService;
35+
import net.devh.boot.grpc.test.config.BaseAutoConfiguration;
36+
import net.devh.boot.grpc.test.config.BeanAnnotatedServiceConfig;
37+
import net.devh.boot.grpc.test.config.InProcessConfiguration;
38+
39+
/**
40+
* A test checking that the server picks up a {@link GrpcService} annotated bean from a {@link Configuration}.
41+
*
42+
* @author Daniel Theuke ([email protected])
43+
*/
44+
@Slf4j
45+
@SpringBootTest
46+
@SpringJUnitConfig(classes = {
47+
InProcessConfiguration.class,
48+
BeanAnnotatedServiceConfig.class,
49+
BaseAutoConfiguration.class})
50+
@DirtiesContext
51+
class BeanAnnotatedServiceTest extends AbstractSimpleServerClientTest {
52+
53+
public BeanAnnotatedServiceTest() {
54+
log.info("--- BeanAnnotatedServiceTest ---");
55+
}
56+
57+
@Autowired
58+
AtomicBoolean invoked;
59+
60+
@Override
61+
@Test
62+
void testSuccessfulCall() throws InterruptedException, ExecutionException {
63+
assertFalse(this.invoked.get());
64+
super.testSuccessfulCall();
65+
assertTrue(this.invoked.get());
66+
}
67+
68+
}

0 commit comments

Comments
 (0)