Skip to content

Commit e6cb044

Browse files
authored
Merge pull request #993 from 313hemant313/target_annotation_grpcclient
feat: Allow annotation GrpcClient for target type 'annotation'
2 parents 95a2c03 + e321464 commit e6cb044

File tree

3 files changed

+252
-1
lines changed

3 files changed

+252
-1
lines changed

grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/inject/GrpcClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
*
6666
* @see GrpcClientBean Add as bean to the {@link ApplicationContext}.
6767
*/
68-
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
68+
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
6969
@Retention(RetentionPolicy.RUNTIME)
7070
@Documented
7171
@Inherited
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/*
2+
* Copyright (c) 2016-2021 The gRPC-Spring Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.devh.boot.grpc.test.inject;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertNotNull;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
22+
23+
import java.lang.reflect.Field;
24+
25+
import javax.annotation.PostConstruct;
26+
27+
import org.junit.jupiter.api.Test;
28+
import org.opentest4j.AssertionFailedError;
29+
import org.springframework.boot.test.context.SpringBootTest;
30+
import org.springframework.boot.test.context.TestConfiguration;
31+
import org.springframework.context.annotation.Bean;
32+
import org.springframework.test.annotation.DirtiesContext;
33+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
34+
35+
import io.grpc.stub.AbstractStub;
36+
import net.devh.boot.grpc.client.stubfactory.StandardJavaGrpcStubFactory;
37+
import net.devh.boot.grpc.client.stubfactory.StubFactory;
38+
import net.devh.boot.grpc.test.config.BaseAutoConfiguration;
39+
import net.devh.boot.grpc.test.config.InProcessConfiguration;
40+
import net.devh.boot.grpc.test.config.ServiceConfiguration;
41+
import net.devh.boot.grpc.test.inject.CustomGrpc.ConstructorAccessibleStub;
42+
import net.devh.boot.grpc.test.inject.CustomGrpc.CustomAccessibleStub;
43+
import net.devh.boot.grpc.test.inject.CustomGrpc.FactoryMethodAccessibleStub;
44+
import net.devh.boot.grpc.test.inject.GrpcClientInjectionTest.TestConfig;
45+
import net.devh.boot.grpc.test.inject.metaannotation.GrpcClientWrapper;
46+
import net.devh.boot.grpc.test.proto.TestServiceGrpc.TestServiceBlockingStub;
47+
import net.devh.boot.grpc.test.proto.TestServiceGrpc.TestServiceFutureStub;
48+
import net.devh.boot.grpc.test.proto.TestServiceGrpc.TestServiceStub;
49+
50+
/**
51+
* A test checking that the client injection works.
52+
*
53+
* @author Hemant Vyas ([email protected])
54+
*/
55+
@SpringBootTest
56+
@SpringJUnitConfig(classes = {TestConfig.class, InProcessConfiguration.class,
57+
ServiceConfiguration.class,
58+
BaseAutoConfiguration.class})
59+
@DirtiesContext
60+
class GrpcClientMetaAnnotationTest {
61+
62+
@GrpcClientWrapper(value = "test", extraParamString = "testExtraParamString", extraParamBoolean = true)
63+
TestServiceStub stub;
64+
@GrpcClientWrapper(value = "test", extraParamString = "testExtraParamString", extraParamBoolean = true)
65+
TestServiceBlockingStub blockingStub;
66+
@GrpcClientWrapper(value = "test", extraParamString = "testExtraParamString", extraParamBoolean = true)
67+
TestServiceFutureStub futureStub;
68+
@GrpcClientWrapper(value = "test", extraParamString = "testExtraParamString", extraParamBoolean = true)
69+
ConstructorAccessibleStub constructorStub;
70+
@GrpcClientWrapper(value = "test", extraParamString = "testExtraParamString", extraParamBoolean = true)
71+
FactoryMethodAccessibleStub factoryMethodStub;
72+
@GrpcClientWrapper(value = "test", extraParamString = "testExtraParamString", extraParamBoolean = true)
73+
CustomAccessibleStub customStub;
74+
75+
TestServiceStub stubSetted;
76+
TestServiceBlockingStub blockingStubSetted;
77+
TestServiceFutureStub futureStubSetted;
78+
ConstructorAccessibleStub constructorStubSetted;
79+
FactoryMethodAccessibleStub factoryMethodStubSetted;
80+
CustomAccessibleStub customStubSetted;
81+
82+
@PostConstruct
83+
public void init() {
84+
// Test injection
85+
assertNotNull(this.stub, "stub");
86+
assertNotNull(this.blockingStub, "blockingStub");
87+
assertNotNull(this.futureStub, "futureStub");
88+
assertNotNull(this.constructorStub, "constructorStub");
89+
assertNotNull(this.factoryMethodStub, "factoryMethodStub");
90+
assertNotNull(this.customStub, "customStub");
91+
92+
assertAnnotationExtraParams("testExtraParamString", true);
93+
}
94+
95+
@GrpcClientWrapper(value = "test", extraParamString = "testExtraParamString", extraParamBoolean = true)
96+
void inject(final TestServiceStub stub) {
97+
assertNotNull(stub, "stub");
98+
this.stubSetted = stub;
99+
}
100+
101+
@GrpcClientWrapper(value = "test", extraParamString = "testExtraParamString", extraParamBoolean = true)
102+
void inject(final TestServiceBlockingStub stub) {
103+
assertNotNull(stub, "stub");
104+
this.blockingStubSetted = stub;
105+
}
106+
107+
@GrpcClientWrapper(value = "test", extraParamString = "testExtraParamString", extraParamBoolean = true)
108+
void inject(final TestServiceFutureStub stub) {
109+
assertNotNull(stub, "stub");
110+
this.futureStubSetted = stub;
111+
}
112+
113+
@GrpcClientWrapper(value = "test", extraParamString = "testExtraParamString", extraParamBoolean = true)
114+
void inject(final ConstructorAccessibleStub stub) {
115+
assertNotNull(stub, "stub");
116+
this.constructorStubSetted = stub;
117+
}
118+
119+
@GrpcClientWrapper(value = "test", extraParamString = "testExtraParamString", extraParamBoolean = true)
120+
void inject(final FactoryMethodAccessibleStub stub) {
121+
assertNotNull(stub, "stub");
122+
this.factoryMethodStubSetted = stub;
123+
}
124+
125+
@GrpcClientWrapper(value = "test", extraParamString = "testExtraParamString", extraParamBoolean = true)
126+
void inject(final CustomAccessibleStub stub) {
127+
assertNotNull(stub, "stub");
128+
this.customStubSetted = stub;
129+
}
130+
131+
@Test
132+
void testAllSet() {
133+
// Field injection
134+
assertNotNull(this.stub, "stub");
135+
assertNotNull(this.blockingStub, "blockingStub");
136+
assertNotNull(this.futureStub, "futureStub");
137+
assertNotNull(this.constructorStub, "constructorStub");
138+
assertNotNull(this.factoryMethodStub, "factoryMethodStub");
139+
assertNotNull(this.customStub, "customStub");
140+
// Setter injection
141+
assertNotNull(this.stubSetted, "stubSetted");
142+
assertNotNull(this.blockingStubSetted, "blockingStubSetted");
143+
assertNotNull(this.futureStubSetted, "futureStubSetted");
144+
assertNotNull(this.constructorStubSetted, "constructorStubSetted");
145+
assertNotNull(this.factoryMethodStubSetted, "factoryMethodStubSetted");
146+
assertNotNull(this.customStubSetted, "customStubSetted");
147+
}
148+
149+
@Test
150+
void AnnotationExtraParamsNegativeTest() {
151+
152+
AssertionFailedError exceptionA = assertThrows(
153+
AssertionFailedError.class,
154+
() -> assertAnnotationExtraParams("INCORRECT_VALUE", true),
155+
"failed");
156+
157+
AssertionFailedError exceptionB = assertThrows(
158+
AssertionFailedError.class,
159+
() -> assertAnnotationExtraParams("testExtraParamString", false),
160+
"failed");
161+
162+
assertNotNull(exceptionA);
163+
assertNotNull(exceptionB);
164+
}
165+
166+
private void assertAnnotationExtraParams(String extraParamStringValue,
167+
boolean extraParamBooleanValue) {
168+
final Field[] fields = this.getClass().getDeclaredFields();
169+
for (final Field field : fields) {
170+
if (field.isAnnotationPresent(GrpcClientWrapper.class)) {
171+
final GrpcClientWrapper annotation = field.getAnnotation(GrpcClientWrapper.class);
172+
assertEquals(extraParamStringValue, annotation.extraParamString());
173+
assertEquals(extraParamBooleanValue, annotation.extraParamBoolean());
174+
}
175+
}
176+
}
177+
178+
@TestConfiguration
179+
public static class TestConfig {
180+
181+
@Bean
182+
StubFactory customStubFactory() {
183+
return new StandardJavaGrpcStubFactory() {
184+
185+
@Override
186+
public boolean isApplicable(final Class<? extends AbstractStub<?>> stubType) {
187+
return CustomStub.class.isAssignableFrom(stubType);
188+
}
189+
190+
@Override
191+
protected String getFactoryMethodName() {
192+
return "custom";
193+
}
194+
195+
};
196+
}
197+
198+
}
199+
200+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2016-2023 The gRPC-Spring Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.devh.boot.grpc.test.inject.metaannotation;
18+
19+
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.springframework.core.annotation.AliasFor;
26+
27+
import io.grpc.ClientInterceptor;
28+
import net.devh.boot.grpc.client.inject.GrpcClient;
29+
30+
@GrpcClient(value = "")
31+
@Retention(RetentionPolicy.RUNTIME)
32+
@Target({ElementType.FIELD, ElementType.METHOD})
33+
public @interface GrpcClientWrapper {
34+
35+
@AliasFor(annotation = GrpcClient.class, attribute = "value")
36+
String value();
37+
38+
@AliasFor(annotation = GrpcClient.class, attribute = "interceptors")
39+
Class<? extends ClientInterceptor>[] interceptors() default {};
40+
41+
@AliasFor(annotation = GrpcClient.class, attribute = "interceptorNames")
42+
String[] interceptorNames() default {};
43+
44+
@AliasFor(annotation = GrpcClient.class, attribute = "sortInterceptors")
45+
boolean sortInterceptors() default false;
46+
47+
String extraParamString();
48+
49+
boolean extraParamBoolean() default false;
50+
51+
}

0 commit comments

Comments
 (0)