|
| 1 | +/* |
| 2 | + * Copyright 2023-2024 the original author or 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 | + * https://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 org.springframework.grpc.autoconfigure.server; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.mockito.ArgumentMatchers.any; |
| 21 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 22 | +import static org.mockito.Mockito.inOrder; |
| 23 | +import static org.mockito.Mockito.mock; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +import java.time.Duration; |
| 27 | +import java.util.List; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | + |
| 30 | +import org.assertj.core.api.InstanceOfAssertFactories; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.mockito.InOrder; |
| 33 | +import org.mockito.MockedStatic; |
| 34 | +import org.mockito.Mockito; |
| 35 | +import org.mockito.stubbing.Answer; |
| 36 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 37 | +import org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration; |
| 38 | +import org.springframework.boot.test.context.FilteredClassLoader; |
| 39 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 40 | +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; |
| 41 | +import org.springframework.boot.web.servlet.ServletRegistrationBean; |
| 42 | +import org.springframework.context.annotation.Bean; |
| 43 | +import org.springframework.context.annotation.Configuration; |
| 44 | +import org.springframework.core.annotation.Order; |
| 45 | +import org.springframework.grpc.server.GrpcServerFactory; |
| 46 | +import org.springframework.grpc.server.NettyGrpcServerFactory; |
| 47 | +import org.springframework.grpc.server.ServerBuilderCustomizer; |
| 48 | +import org.springframework.grpc.server.ShadedNettyGrpcServerFactory; |
| 49 | +import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle; |
| 50 | + |
| 51 | +import io.grpc.BindableService; |
| 52 | +import io.grpc.Grpc; |
| 53 | +import io.grpc.ServerBuilder; |
| 54 | +import io.grpc.ServerServiceDefinition; |
| 55 | +import io.grpc.ServiceDescriptor; |
| 56 | +import io.grpc.netty.NettyServerBuilder; |
| 57 | + |
| 58 | +/** |
| 59 | + * Tests for {@link GrpcServerAutoConfiguration}. |
| 60 | + * |
| 61 | + * @author Chris Bono |
| 62 | + */ |
| 63 | +class GrpcServletAutoConfigurationTests { |
| 64 | + |
| 65 | + private WebApplicationContextRunner contextRunner() { |
| 66 | + BindableService service = mock(); |
| 67 | + ServerServiceDefinition serviceDefinition = ServerServiceDefinition.builder("my-service").build(); |
| 68 | + when(service.bindService()).thenReturn(serviceDefinition); |
| 69 | + // NOTE: we use noop server lifecycle to avoid startup |
| 70 | + return new WebApplicationContextRunner() |
| 71 | + .withConfiguration( |
| 72 | + AutoConfigurations.of(GrpcServerAutoConfiguration.class, GrpcServerFactoryAutoConfiguration.class)) |
| 73 | + .withBean(BindableService.class, () -> service); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void whenGrpcNotOnClasspathAutoConfigurationIsSkipped() { |
| 78 | + this.contextRunner() |
| 79 | + .withClassLoader(new FilteredClassLoader(BindableService.class)) |
| 80 | + .run((context) -> assertThat(context).doesNotHaveBean(GrpcServerAutoConfiguration.class) |
| 81 | + .doesNotHaveBean(ServletRegistrationBean.class)); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void whenNoBindableServicesRegisteredAutoConfigurationIsSkipped() { |
| 86 | + new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GrpcServerAutoConfiguration.class)) |
| 87 | + .run((context) -> assertThat(context).doesNotHaveBean(GrpcServerAutoConfiguration.class) |
| 88 | + .doesNotHaveBean(ServletRegistrationBean.class)); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void whenWebApplicationServletIsAutoConfigured() { |
| 93 | + this.contextRunner().run((context) -> assertThat(context).getBean(ServletRegistrationBean.class).isNotNull()); |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments