|
| 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 | +} |
0 commit comments