Skip to content

Commit 11a05b1

Browse files
author
Alexander Furer
committed
1. fixes #80
2. added spring boot 2 tests and samples
1 parent 872ab14 commit 11a05b1

File tree

11 files changed

+114
-31
lines changed

11 files changed

+114
-31
lines changed

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
buildscript {
33
ext {
4-
springBootVersion = '1.5.9.RELEASE'
4+
springBoot_1_X_Version = '1.5.11.RELEASE'
5+
springBoot_2_X_Version = '2.0.1.RELEASE'
56
grpcVersion = '1.10.0'
67
}
78
repositories {
@@ -12,8 +13,7 @@ buildscript {
1213

1314
}
1415
dependencies {
15-
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
16-
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.5"
16+
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.5"
1717
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.5'
1818
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.0.0"
1919
classpath 'com.palantir:jacoco-coverage:0.4.0'

grpc-spring-boot-starter-demo/build.gradle

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
2+
buildscript {
3+
repositories {
4+
mavenCentral()
5+
}
6+
dependencies {
7+
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBoot_1_X_Version}")
8+
}
9+
}
10+
111
apply plugin: 'com.google.protobuf'
212
apply plugin: 'org.springframework.boot'
313

@@ -53,10 +63,11 @@ task cleanProtoGen{
5363
}
5464
}
5565
clean.dependsOn cleanProtoGen
66+
bootRepackage.enabled =false
5667

5768
dependencyManagement {
5869
imports {
59-
mavenBom "org.springframework.boot:spring-boot-starter-parent:${springBootVersion}"
70+
mavenBom "org.springframework.boot:spring-boot-starter-parent:${springBoot_1_X_Version}"
6071
}
6172
}
6273

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/DemoAppTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616
import org.mockito.Mockito;
1717
import org.springframework.beans.factory.annotation.Autowired;
1818
import org.springframework.beans.factory.annotation.Qualifier;
19-
import org.springframework.boot.context.embedded.LocalServerPort;
19+
import org.springframework.beans.factory.annotation.Value;
20+
2021
import org.springframework.boot.test.context.SpringBootTest;
2122
import org.springframework.boot.test.rule.OutputCapture;
2223
import org.springframework.boot.test.web.client.TestRestTemplate;
2324
import org.springframework.http.HttpStatus;
2425
import org.springframework.http.ResponseEntity;
2526
import org.springframework.test.context.junit4.SpringRunner;
2627

28+
import java.net.URI;
2729
import java.util.concurrent.ExecutionException;
2830

2931
import static org.hamcrest.CoreMatchers.containsString;
@@ -38,8 +40,8 @@
3840
@SpringBootTest(classes = {DemoApp.class,TestConfig.class}, webEnvironment = RANDOM_PORT)
3941
public class DemoAppTest extends GrpcServerTestBase{
4042

41-
@LocalServerPort
42-
private int localServerPort;
43+
@Autowired
44+
private TestRestTemplate restTemplate;
4345

4446
@Rule
4547
public OutputCapture outputCapture = new OutputCapture();
@@ -71,18 +73,16 @@ public void interceptorsTest() throws ExecutionException, InterruptedException {
7173

7274

7375
// log interceptor should be invoked only on GreeterService and not CalculatorService
74-
outputCapture.expect(containsString(GreeterGrpc.METHOD_SAY_HELLO.getFullMethodName()));
75-
outputCapture.expect(not(containsString(CalculatorGrpc.METHOD_CALCULATE.getFullMethodName())));
76+
outputCapture.expect(containsString(GreeterGrpc.getSayHelloMethod().getFullMethodName()));
77+
outputCapture.expect(not(containsString(CalculatorGrpc.getCalculateMethod().getFullMethodName())));
7678

7779

7880
outputCapture.expect(containsString("I'm not Spring bean interceptor and still being invoked..."));
7981
}
8082

8183
@Test
8284
public void actuatorTest() throws ExecutionException, InterruptedException {
83-
final TestRestTemplate template = new TestRestTemplate();
84-
85-
ResponseEntity<String> response = template.getForEntity("http://localhost:" + localServerPort + "/env", String.class);
85+
ResponseEntity<String> response = restTemplate.getForEntity("/env", String.class);
8686
assertEquals(HttpStatus.OK, response.getStatusCode());
8787
}
8888

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/OrderedInterceptorsTest.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import org.lognet.springboot.grpc.demo.DemoApp;
1414
import org.springframework.boot.test.context.SpringBootTest;
1515
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
16+
import org.springframework.context.annotation.Bean;
1617
import org.springframework.context.annotation.Configuration;
18+
import org.springframework.core.Ordered;
1719
import org.springframework.core.annotation.Order;
1820
import org.springframework.test.context.junit4.SpringRunner;
1921

@@ -44,7 +46,7 @@ public void setup() {
4446

4547
@Override
4648
protected void afterGreeting() {
47-
assertThat(calledInterceptors).containsExactly(1, 2, 3, 4, 10, 100);
49+
assertThat(calledInterceptors).containsExactly(1, 2, 3, 4,5, 10, 100);
4850
}
4951

5052

@@ -101,7 +103,7 @@ public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
101103
}
102104

103105
@GRpcGlobalInterceptor
104-
@Order // no value means lowest priority amongst all @Ordered, but higher priority than iceptors without the annot
106+
@Order // no value means lowest priority amongst all @Ordered, but higher priority than interceptors without the annotation
105107
static class DefaultOrderedInterceptor implements ServerInterceptor {
106108

107109
@Override
@@ -123,5 +125,25 @@ public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
123125
return next.startCall(call, headers);
124126
}
125127
}
128+
129+
@Bean
130+
@GRpcGlobalInterceptor
131+
public ServerInterceptor myInterceptor(){
132+
return new MyInterceptor();
133+
}
134+
135+
class MyInterceptor implements ServerInterceptor,Ordered {
136+
@Override
137+
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
138+
calledInterceptors.add(5);
139+
return next.startCall(call, headers);
140+
}
141+
142+
@Override
143+
public int getOrder() {
144+
return 5;
145+
}
146+
}
147+
126148
}
127149
}

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/TestConfig.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ public ServerInterceptor globalInterceptor(){
2727
when(mock.interceptCall(notNull(ServerCall.class),notNull(Metadata.class),notNull(ServerCallHandler.class))).thenAnswer(new Answer<ServerCall.Listener>() {
2828
@Override
2929
public ServerCall.Listener answer(InvocationOnMock invocation) throws Throwable {
30-
return invocation.getArgumentAt(2,ServerCallHandler.class).startCall(invocation.getArgumentAt(0,ServerCall.class),
31-
invocation.getArgumentAt(1,Metadata.class)
30+
31+
return ServerCallHandler.class.cast(invocation.getArguments()[2]).startCall(
32+
ServerCall.class.cast(invocation.getArguments()[0]),
33+
Metadata.class.cast(invocation.getArguments()[1])
3234
);
3335
}
3436
});
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
//apply from: "${rootDir}/publishSonatype.gradle"
2+
buildscript {
3+
repositories {
4+
mavenCentral()
5+
}
6+
dependencies {
7+
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBoot_1_X_Version}")
8+
}
9+
}
210
apply from: "${rootDir}/publish.gradle"
311
dependencies {
412
compile "io.grpc:grpc-netty:${grpcVersion}"
513
compile "io.grpc:grpc-services:${grpcVersion}"
6-
compile (group: 'org.springframework.boot', name: 'spring-boot-starter', version: springBootVersion )
14+
compile (group: 'org.springframework.boot', name: 'spring-boot-starter', version: springBoot_1_X_Version )
715

8-
compileOnly("org.springframework.boot:spring-boot-configuration-processor:${springBootVersion}")
16+
compileOnly("org.springframework.boot:spring-boot-configuration-processor:${springBoot_1_X_Version}")
917

1018
}
1119
compileJava.dependsOn(processResources)

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/GRpcServerRunner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ private <T> Stream<String> getBeanNamesByTypeWithAnnotation(Class<? extends Anno
156156
final BeanDefinition beanDefinition = applicationContext.getBeanFactory().getBeanDefinition(name);
157157
final Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(annotationType);
158158

159-
if (!beansWithAnnotation.isEmpty()) {
160-
return beansWithAnnotation.containsKey(name);
159+
if (beansWithAnnotation.containsKey(name)) {
160+
return true;
161161
} else if (beanDefinition.getSource() instanceof StandardMethodMetadata) {
162162
StandardMethodMetadata metadata = (StandardMethodMetadata) beanDefinition.getSource();
163163
return metadata.isAnnotated(annotationType.getName());
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
buildscript {
2+
3+
repositories {
4+
mavenCentral()
5+
}
6+
dependencies {
7+
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBoot_2_X_Version}")
8+
}
9+
}
10+
11+
apply plugin: 'java'
12+
apply plugin: 'org.springframework.boot'
13+
apply plugin: 'io.spring.dependency-management'
14+
15+
16+
sourceSets.test{
17+
java{
18+
srcDir "${project(':grpc-spring-boot-starter-demo').getProjectDir()}/src/test/java"
19+
}
20+
}
21+
dependencies {
22+
23+
testCompile project(':grpc-spring-boot-starter-demo')
24+
25+
26+
27+
testCompile('org.springframework.boot:spring-boot-starter-test')
28+
testCompile 'org.springframework.boot:spring-boot-starter-aop'
29+
30+
testRuntime('org.springframework.boot:spring-boot-starter-actuator')
31+
testRuntime('org.springframework.boot:spring-boot-starter-web')
32+
33+
34+
}
35+
bootJar.enabled =false
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
management.endpoints.web.base-path=/
2+
management.endpoints.web.exposure.include=*
3+

publish.gradle

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@ artifacts {
2424
project[it] = System.env[it]
2525
}
2626

27-
install {
28-
repositories.mavenInstaller {
29-
pom.project {
30-
parent {
31-
groupId 'org.springframework.boot'
32-
artifactId 'spring-boot-starter-parent'
33-
version "${project.springBootVersion}"
34-
}
35-
}
36-
}
37-
}
27+
//install {
28+
// repositories.mavenInstaller {
29+
// pom.project {
30+
// parent {
31+
// groupId 'org.springframework.boot'
32+
// artifactId 'spring-boot-starter-parent'
33+
// version "${project.springBoot_1_X_Version}"
34+
// }
35+
// }
36+
// }
37+
//}
38+
3839
//This is only needed if bintrayUpload is done as part of releasing
3940
task updateBintrayVersion << {
4041
bintray.pkg.version.name = project.version

0 commit comments

Comments
 (0)