Skip to content

Commit e2ef1de

Browse files
committed
Add tests
1 parent 73a297b commit e2ef1de

File tree

4 files changed

+251
-16
lines changed

4 files changed

+251
-16
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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.inject;
19+
20+
import io.grpc.CallOptions;
21+
import io.grpc.Channel;
22+
23+
/**
24+
* Fake generated grpc class.
25+
*/
26+
public class CustomGrpc {
27+
28+
public static CustomAccessibleStub custom(final Channel channel) {
29+
return new CustomAccessibleStub(channel);
30+
}
31+
32+
public static FactoryMethodAccessibleStub newFactoryMethodAccessibleStubStub(final Channel channel) {
33+
return new FactoryMethodAccessibleStub(channel);
34+
}
35+
36+
public static class CustomAccessibleStub extends CustomStub<CustomAccessibleStub> {
37+
38+
private CustomAccessibleStub(final Channel channel) {
39+
super(channel);
40+
}
41+
42+
private CustomAccessibleStub(final Channel channel, final CallOptions callOptions) {
43+
super(channel, callOptions);
44+
}
45+
46+
@Override
47+
protected CustomAccessibleStub build(final Channel channel, final CallOptions callOptions) {
48+
return new CustomAccessibleStub(channel, callOptions);
49+
}
50+
51+
}
52+
53+
public static class FactoryMethodAccessibleStub extends OtherStub<FactoryMethodAccessibleStub> {
54+
55+
private FactoryMethodAccessibleStub(final Channel channel) {
56+
super(channel);
57+
}
58+
59+
private FactoryMethodAccessibleStub(final Channel channel, final CallOptions callOptions) {
60+
super(channel, callOptions);
61+
}
62+
63+
@Override
64+
protected FactoryMethodAccessibleStub build(final Channel channel, final CallOptions callOptions) {
65+
return new FactoryMethodAccessibleStub(channel, callOptions);
66+
}
67+
68+
}
69+
70+
public static class ConstructorAccessibleStub extends OtherStub<ConstructorAccessibleStub> {
71+
72+
public ConstructorAccessibleStub(final Channel channel) {
73+
super(channel);
74+
}
75+
76+
public ConstructorAccessibleStub(final Channel channel, final CallOptions callOptions) {
77+
super(channel, callOptions);
78+
}
79+
80+
@Override
81+
protected ConstructorAccessibleStub build(final Channel channel, final CallOptions callOptions) {
82+
return new ConstructorAccessibleStub(channel, callOptions);
83+
}
84+
85+
}
86+
87+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.inject;
19+
20+
import io.grpc.CallOptions;
21+
import io.grpc.Channel;
22+
import io.grpc.stub.AbstractStub;
23+
24+
/**
25+
* Simulates a custom stub type provided by a third party library, that requires a custom
26+
* {@link net.devh.boot.grpc.client.stubfactory.StubFactory StubFactory}.
27+
*
28+
* @param <S> The type of the stub implementation.
29+
*/
30+
public abstract class CustomStub<S extends CustomStub<S>> extends AbstractStub<S> {
31+
32+
protected CustomStub(final Channel channel) {
33+
super(channel);
34+
}
35+
36+
protected CustomStub(final Channel channel, final CallOptions callOptions) {
37+
super(channel, callOptions);
38+
}
39+
40+
}

tests/src/test/java/net/devh/boot/grpc/test/inject/GrpcClientInjectionTest.java

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,23 @@
2323

2424
import org.junit.jupiter.api.Test;
2525
import org.springframework.boot.test.context.SpringBootTest;
26+
import org.springframework.boot.test.context.TestConfiguration;
27+
import org.springframework.context.annotation.Bean;
2628
import org.springframework.test.annotation.DirtiesContext;
2729
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
2830

2931
import io.grpc.Channel;
32+
import io.grpc.stub.AbstractStub;
3033
import net.devh.boot.grpc.client.inject.GrpcClient;
34+
import net.devh.boot.grpc.client.stubfactory.StandardJavaGrpcStubFactory;
35+
import net.devh.boot.grpc.client.stubfactory.StubFactory;
3136
import net.devh.boot.grpc.test.config.BaseAutoConfiguration;
3237
import net.devh.boot.grpc.test.config.InProcessConfiguration;
3338
import net.devh.boot.grpc.test.config.ServiceConfiguration;
39+
import net.devh.boot.grpc.test.inject.CustomGrpc.ConstructorAccessibleStub;
40+
import net.devh.boot.grpc.test.inject.CustomGrpc.CustomAccessibleStub;
41+
import net.devh.boot.grpc.test.inject.CustomGrpc.FactoryMethodAccessibleStub;
42+
import net.devh.boot.grpc.test.inject.GrpcClientInjectionTest.TestConfig;
3443
import net.devh.boot.grpc.test.proto.TestServiceGrpc.TestServiceBlockingStub;
3544
import net.devh.boot.grpc.test.proto.TestServiceGrpc.TestServiceFutureStub;
3645
import net.devh.boot.grpc.test.proto.TestServiceGrpc.TestServiceStub;
@@ -41,24 +50,33 @@
4150
* @author Daniel Theuke ([email protected])
4251
*/
4352
@SpringBootTest
44-
@SpringJUnitConfig(classes = {InProcessConfiguration.class, ServiceConfiguration.class, BaseAutoConfiguration.class})
53+
@SpringJUnitConfig(classes = {TestConfig.class, InProcessConfiguration.class, ServiceConfiguration.class,
54+
BaseAutoConfiguration.class})
4555
@DirtiesContext
46-
public class GrpcClientInjectionTest {
56+
class GrpcClientInjectionTest {
4757

4858
@GrpcClient("test")
49-
protected Channel channel;
59+
Channel channel;
5060
@GrpcClient("test")
51-
protected TestServiceStub stub;
61+
TestServiceStub stub;
5262
@GrpcClient("test")
53-
protected TestServiceBlockingStub blockingStub;
63+
TestServiceBlockingStub blockingStub;
5464
@GrpcClient("test")
55-
protected TestServiceFutureStub futureStub;
56-
57-
protected Channel channelSetted;
58-
protected TestServiceStub stubSetted;
59-
protected TestServiceBlockingStub blockingStubSetted;
60-
protected TestServiceFutureStub futureStubSetted;
65+
TestServiceFutureStub futureStub;
66+
@GrpcClient("test")
67+
ConstructorAccessibleStub constructorStub;
68+
@GrpcClient("test")
69+
FactoryMethodAccessibleStub factoryMethodStub;
70+
@GrpcClient("test")
71+
CustomAccessibleStub customStub;
6172

73+
Channel channelSetted;
74+
TestServiceStub stubSetted;
75+
TestServiceBlockingStub blockingStubSetted;
76+
TestServiceFutureStub futureStubSetted;
77+
ConstructorAccessibleStub constructorStubSetted;
78+
FactoryMethodAccessibleStub factoryMethodStubSetted;
79+
CustomAccessibleStub customStubSetted;
6280

6381
@PostConstruct
6482
public void init() {
@@ -67,44 +85,93 @@ public void init() {
6785
assertNotNull(this.stub, "stub");
6886
assertNotNull(this.blockingStub, "blockingStub");
6987
assertNotNull(this.futureStub, "futureStub");
88+
assertNotNull(this.constructorStub, "constructorStub");
89+
assertNotNull(this.factoryMethodStub, "factoryMethodStub");
90+
assertNotNull(this.customStub, "customStub");
7091
}
7192

7293
@GrpcClient("test")
73-
public void inject(Channel channel) {
94+
void inject(final Channel channel) {
7495
assertNotNull(channel, "channel");
7596
this.channelSetted = channel;
7697
}
7798

7899
@GrpcClient("test")
79-
public void inject(TestServiceStub stub) {
100+
void inject(final TestServiceStub stub) {
80101
assertNotNull(stub, "stub");
81102
this.stubSetted = stub;
82103
}
83104

84105
@GrpcClient("test")
85-
public void inject(TestServiceBlockingStub stub) {
106+
void inject(final TestServiceBlockingStub stub) {
86107
assertNotNull(stub, "stub");
87108
this.blockingStubSetted = stub;
88109
}
89110

90111
@GrpcClient("test")
91-
public void inject(TestServiceFutureStub stub) {
112+
void inject(final TestServiceFutureStub stub) {
92113
assertNotNull(stub, "stub");
93114
this.futureStubSetted = stub;
94115
}
95116

117+
@GrpcClient("test")
118+
void inject(final ConstructorAccessibleStub stub) {
119+
assertNotNull(stub, "stub");
120+
this.constructorStubSetted = stub;
121+
}
122+
123+
@GrpcClient("test")
124+
void inject(final FactoryMethodAccessibleStub stub) {
125+
assertNotNull(stub, "stub");
126+
this.factoryMethodStubSetted = stub;
127+
}
128+
129+
@GrpcClient("test")
130+
void inject(final CustomAccessibleStub stub) {
131+
assertNotNull(stub, "stub");
132+
this.customStubSetted = stub;
133+
}
134+
96135
@Test
97-
public void testAllSet() {
136+
void testAllSet() {
98137
// Field injection
99138
assertNotNull(this.channel, "channel");
100139
assertNotNull(this.stub, "stub");
101140
assertNotNull(this.blockingStub, "blockingStub");
102141
assertNotNull(this.futureStub, "futureStub");
142+
assertNotNull(this.constructorStub, "constructorStub");
143+
assertNotNull(this.factoryMethodStub, "factoryMethodStub");
144+
assertNotNull(this.customStub, "customStub");
103145
// Setter injection
104146
assertNotNull(this.channelSetted, "channelSetted");
105147
assertNotNull(this.stubSetted, "stubSetted");
106148
assertNotNull(this.blockingStubSetted, "blockingStubSetted");
107149
assertNotNull(this.futureStubSetted, "futureStubSetted");
150+
assertNotNull(this.constructorStubSetted, "constructorStubSetted");
151+
assertNotNull(this.factoryMethodStubSetted, "factoryMethodStubSetted");
152+
assertNotNull(this.customStubSetted, "customStubSetted");
153+
}
154+
155+
@TestConfiguration
156+
public static class TestConfig {
157+
158+
@Bean
159+
StubFactory customStubFactory() {
160+
return new StandardJavaGrpcStubFactory() {
161+
162+
@Override
163+
public boolean isApplicable(final Class<? extends AbstractStub<?>> stubType) {
164+
return CustomStub.class.isAssignableFrom(stubType);
165+
}
166+
167+
@Override
168+
protected String getFactoryMethodName() {
169+
return "custom";
170+
}
171+
172+
};
173+
}
174+
108175
}
109176

110177
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.inject;
19+
20+
import io.grpc.CallOptions;
21+
import io.grpc.Channel;
22+
import io.grpc.stub.AbstractStub;
23+
import net.devh.boot.grpc.client.stubfactory.FallbackStubFactory;
24+
25+
/**
26+
* Simulates a custom stub type provided by a third party library, that can be created by the
27+
* {@link FallbackStubFactory}.
28+
*
29+
* @param <S> The type of the stub implementation.
30+
*/
31+
public abstract class OtherStub<S extends OtherStub<S>> extends AbstractStub<S> {
32+
33+
protected OtherStub(final Channel channel) {
34+
super(channel);
35+
}
36+
37+
protected OtherStub(final Channel channel, final CallOptions callOptions) {
38+
super(channel, callOptions);
39+
}
40+
41+
}

0 commit comments

Comments
 (0)