From 5f4fa5204d42b23d4d595eb1c02eec5cf806a756 Mon Sep 17 00:00:00 2001 From: Chris Bono Date: Tue, 24 Sep 2024 10:09:47 -0500 Subject: [PATCH] Add back in `@ConditionalOnBean` to auto-config Also adds auto-configuration tests for server. --- .../server/GrpcServerAutoConfiguration.java | 5 + .../GrpcServerAutoConfigurationTests.java | 102 ++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/server/GrpcServerAutoConfigurationTests.java diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfiguration.java b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfiguration.java index b82046f..9dd260f 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfiguration.java +++ b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfiguration.java @@ -22,6 +22,7 @@ import io.grpc.BindableService; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -34,12 +35,16 @@ import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle; /** * {@link EnableAutoConfiguration Auto-configuration} for gRPC server-side components. + *

+ * gRPC must be on the classpath and at least one {@link BindableService} bean registered + * in the context in order for the auto-configuration to execute. * * @author David Syer * @author Chris Bono */ @AutoConfiguration @ConditionalOnClass(BindableService.class) +@ConditionalOnBean(BindableService.class) @EnableConfigurationProperties(GrpcServerProperties.class) public class GrpcServerAutoConfiguration { diff --git a/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/server/GrpcServerAutoConfigurationTests.java b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/server/GrpcServerAutoConfigurationTests.java new file mode 100644 index 0000000..f52dc62 --- /dev/null +++ b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/server/GrpcServerAutoConfigurationTests.java @@ -0,0 +1,102 @@ +/* + * Copyright 2023-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.grpc.server; + +import java.util.List; + +import io.grpc.BindableService; +import io.grpc.ServerServiceDefinition; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.FilteredClassLoader; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.grpc.autoconfigure.server.GrpcServerAutoConfiguration; +import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link GrpcServerAutoConfiguration}. + * + * @author Chris Bono + */ +class GrpcServerAutoConfigurationTests { + + private ApplicationContextRunner validContextRunner() { + BindableService service = mock(); + ServerServiceDefinition serviceDefinition = ServerServiceDefinition.builder("my-service").build(); + when(service.bindService()).thenReturn(serviceDefinition); + return new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(GrpcServerAutoConfiguration.class)) + .withBean(BindableService.class, () -> service); + } + + @Test + void whenGrpcNotOnClasspathAutoConfigurationIsSkipped() { + this.validContextRunner() + .withClassLoader(new FilteredClassLoader(BindableService.class)) + .run((context) -> assertThat(context).doesNotHaveBean(GrpcServerAutoConfiguration.class)); + } + + @Test + void whenNoBindableServicesRegisteredAutoConfigurationIsSkipped() { + new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GrpcServerAutoConfiguration.class)) + .run((context) -> assertThat(context).doesNotHaveBean(GrpcServerAutoConfiguration.class)); + } + + @Test + void whenHasUserDefinedServerFactoryDoesNotAutoConfigureBean() { + // NOTE: we use noop server lifecycle to avoid startup + GrpcServerFactory customServerFactory = mock(GrpcServerFactory.class); + this.validContextRunner() + .withBean("customServerFactory", GrpcServerFactory.class, () -> customServerFactory) + .withBean("noopServerLifecycle", GrpcServerLifecycle.class, Mockito::mock) + .run((context) -> assertThat(context).getBean(GrpcServerFactory.class).isSameAs(customServerFactory)); + } + + @Test + void whenHasUserDefinedServerLifecycleDoesNotAutoConfigureBean() { + GrpcServerLifecycle customServerLifecycle = mock(GrpcServerLifecycle.class); + this.validContextRunner() + .withBean("customServerLifecycle", GrpcServerLifecycle.class, () -> customServerLifecycle) + .run((context) -> assertThat(context).getBean(GrpcServerLifecycle.class).isSameAs(customServerLifecycle)); + } + + @Test + void serverFactoryAutoConfiguredAsExpected() { + // NOTE: we use noop server lifecycle to avoid startup + this.validContextRunner() + .withBean("noopServerLifecycle", GrpcServerLifecycle.class, Mockito::mock) + .withPropertyValues("spring.grpc.server.address=myhost", "spring.grpc.server.port=6160") + .run((context) -> assertThat(context).getBean(DefaultGrpcServerFactory.class) + .hasFieldOrPropertyWithValue("address", "myhost") + .hasFieldOrPropertyWithValue("port", 6160) + .hasFieldOrPropertyWithValue("serverConfigurers", List.of())); + } + + @Test + void serverLifecycleAutoConfiguredAsExpected() { + this.validContextRunner() + .run((context) -> assertThat(context).getBean(GrpcServerLifecycle.class) + .hasFieldOrPropertyWithValue("factory", context.getBean(DefaultGrpcServerFactory.class))); + } + +}