|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2021 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 static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.springframework.beans.factory.BeanCreationException; |
| 25 | +import org.springframework.boot.SpringApplication; |
| 26 | +import org.springframework.boot.test.context.TestConfiguration; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.test.annotation.DirtiesContext; |
| 29 | + |
| 30 | +import lombok.extern.slf4j.Slf4j; |
| 31 | +import net.devh.boot.grpc.client.inject.GrpcClient; |
| 32 | +import net.devh.boot.grpc.client.inject.GrpcClientBean; |
| 33 | +import net.devh.boot.grpc.client.inject.GrpcClientBeans; |
| 34 | +import net.devh.boot.grpc.test.config.BaseAutoConfiguration; |
| 35 | +import net.devh.boot.grpc.test.config.InProcessConfiguration; |
| 36 | +import net.devh.boot.grpc.test.config.ServiceConfiguration; |
| 37 | +import net.devh.boot.grpc.test.proto.TestServiceGrpc; |
| 38 | + |
| 39 | +/** |
| 40 | + * Test case should cover probable negative situations with @GrpcBeanClient and @GrpcClient usage |
| 41 | + */ |
| 42 | +@Slf4j |
| 43 | +@DirtiesContext |
| 44 | +public class GrpcClientBeanInjectionNegativeTest { |
| 45 | + |
| 46 | + @Test |
| 47 | + void twoSameGrpcClientBeansTest() { |
| 48 | + final SpringApplication app = new SpringApplication( |
| 49 | + TwoSameGrpcClientBeans.class, |
| 50 | + InProcessConfiguration.class, |
| 51 | + ServiceConfiguration.class, |
| 52 | + BaseAutoConfiguration.class); |
| 53 | + assertThrows(BeanCreationException.class, app::run); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void grpcClientBeanAndCustomGrpcClientFieldTest() { |
| 58 | + final SpringApplication app = new SpringApplication( |
| 59 | + GrpcClientBeanWithCustomGrpcClientFieldConfig.class, |
| 60 | + InProcessConfiguration.class, |
| 61 | + ServiceConfiguration.class, |
| 62 | + BaseAutoConfiguration.class); |
| 63 | + assertDoesNotThrow(() -> SpringApplication.exit(app.run())); |
| 64 | + } |
| 65 | + |
| 66 | + @TestConfiguration |
| 67 | + @GrpcClientBeans(value = { |
| 68 | + @GrpcClientBean( |
| 69 | + clazz = TestServiceGrpc.TestServiceBlockingStub.class, |
| 70 | + beanName = "blockingStub", |
| 71 | + client = @GrpcClient("test")), |
| 72 | + @GrpcClientBean( |
| 73 | + clazz = TestServiceGrpc.TestServiceBlockingStub.class, |
| 74 | + beanName = "blockingStub", |
| 75 | + client = @GrpcClient("test")), |
| 76 | + }) |
| 77 | + public static class TwoSameGrpcClientBeans { |
| 78 | + } |
| 79 | + |
| 80 | + @TestConfiguration |
| 81 | + @GrpcClientBean( |
| 82 | + clazz = TestServiceGrpc.TestServiceBlockingStub.class, |
| 83 | + beanName = "blockingStub", |
| 84 | + client = @GrpcClient("testGrpcBeanClient")) |
| 85 | + public static class GrpcClientBeanWithCustomGrpcClientFieldConfig { |
| 86 | + |
| 87 | + @GrpcClient("testGrpcBeanClient") |
| 88 | + TestServiceGrpc.TestServiceBlockingStub stub; |
| 89 | + |
| 90 | + @Bean |
| 91 | + public TestServiceGrpc.TestServiceBlockingStub blockingStub() { |
| 92 | + return stub; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments