-
Notifications
You must be signed in to change notification settings - Fork 433
/
DemoAppTest.java
96 lines (66 loc) · 3.08 KB
/
DemoAppTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package org.lognet.springboot.grpc;
import io.grpc.ServerInterceptor;
import io.grpc.examples.CalculatorGrpc;
import io.grpc.examples.CalculatorOuterClass;
import io.grpc.examples.GreeterGrpc;
import io.grpc.examples.GreeterOuterClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.lognet.springboot.grpc.demo.DemoApp;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.system.OutputCaptureRule;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.concurrent.ExecutionException;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.isA;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
/**
* Created by alexf on 28-Jan-16.
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {DemoApp.class, TestConfig.class}, webEnvironment = RANDOM_PORT
, properties = {
"grpc.shutdownGrace=-1",
"spring.main.web-application-type=servlet"
})
@ActiveProfiles({"disable-security"})
public class DemoAppTest extends GrpcServerTestBase {
@Rule
public OutputCaptureRule outputCapture = new OutputCaptureRule();
@Autowired
@Qualifier("globalInterceptor")
private ServerInterceptor globalInterceptor;
@Test
public void disabledServerTest() {
assertNotNull(grpcServerRunner);
assertNull(grpcInprocessServerRunner);
}
@Test
public void interceptorsTest() throws ExecutionException, InterruptedException {
GreeterGrpc.newFutureStub(channel)
.sayHello(GreeterOuterClass.HelloRequest.newBuilder().setName("name").build())
.get().getMessage();
CalculatorGrpc.newFutureStub(channel)
.calculate(CalculatorOuterClass.CalculatorRequest.newBuilder().setNumber1(1).setNumber2(1).build())
.get().getResult();
// global interceptor should be invoked once on each service
Mockito.verify(globalInterceptor, Mockito.times(2)).interceptCall(Mockito.any(), Mockito.any(), Mockito.any());
// log interceptor should be invoked only on GreeterService and not CalculatorService
outputCapture.expect(containsString(GreeterGrpc.getSayHelloMethod().getFullMethodName()));
outputCapture.expect(not(containsString(CalculatorGrpc.getCalculateMethod().getFullMethodName())));
outputCapture.expect(containsString("I'm not Spring bean interceptor and still being invoked..."));
}
@Test
public void testDefaultConfigurer() {
assertThat(context.getBean(GRpcServerBuilderConfigurer.class),isA(GRpcServerBuilderConfigurer.class));
}
}