From e0159ed6fb27465cd8655d5bb7b2118b13f8947a Mon Sep 17 00:00:00 2001 From: Chris Bono Date: Tue, 1 Oct 2024 22:44:04 -0500 Subject: [PATCH] Add property / mapper tests --- .../server/GrpcServerProperties.java | 2 - .../server/GrpcServerPropertiesTests.java | 75 +++++++++++++++++++ .../ServerFactoryPropertyMappersTests.java | 69 +++++++++++++++++ 3 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerPropertiesTests.java create mode 100644 spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/ServerFactoryPropertyMappersTests.java diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerProperties.java b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerProperties.java index 7341276..d54562f 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerProperties.java +++ b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerProperties.java @@ -28,8 +28,6 @@ public class GrpcServerProperties { /** * Server port to listen on. When the value is 0, a random available port is selected. - * When the value is -1, the inter-process server is disabled (for example if you only - * want to use the in-process server). */ private int port = 9090; diff --git a/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerPropertiesTests.java b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerPropertiesTests.java new file mode 100644 index 0000000..ab54922 --- /dev/null +++ b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerPropertiesTests.java @@ -0,0 +1,75 @@ +/* + * Copyright 2024-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.autoconfigure.server; + +import java.time.Duration; +import java.util.HashMap; +import java.util.Map; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import org.springframework.boot.context.properties.bind.Binder; +import org.springframework.boot.context.properties.source.MapConfigurationPropertySource; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link GrpcServerProperties}. + * + * @author Chris Bono + */ +class GrpcServerPropertiesTests { + + private GrpcServerProperties bindProperties(Map map) { + return new Binder(new MapConfigurationPropertySource(map)) + .bind("spring.grpc.server", GrpcServerProperties.class) + .get(); + } + + @Nested + class BaseProperties { + + @Test + void bind() { + Map map = new HashMap<>(); + map.put("spring.grpc.server.address", "my-server-ip"); + map.put("spring.grpc.server.port", "3130"); + map.put("spring.grpc.server.shutdown-grace-period", "15s"); + GrpcServerProperties properties = bindProperties(map); + assertThat(properties.getAddress()).isEqualTo("my-server-ip"); + assertThat(properties.getPort()).isEqualTo(3130); + } + + } + + @Nested + class KeepAliveProperties { + + @Test + void bind() { + Map map = new HashMap<>(); + map.put("spring.grpc.server.keep-alive.time", "45m"); + map.put("spring.grpc.server.keep-alive.timeout", "40s"); + GrpcServerProperties.KeepAlive properties = bindProperties(map).getKeepAlive(); + assertThat(properties.getTime()).isEqualTo(Duration.ofMinutes(45)); + assertThat(properties.getTimeout()).isEqualTo(Duration.ofSeconds(40)); + } + + } + +} diff --git a/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/ServerFactoryPropertyMappersTests.java b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/ServerFactoryPropertyMappersTests.java new file mode 100644 index 0000000..ad64890 --- /dev/null +++ b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/ServerFactoryPropertyMappersTests.java @@ -0,0 +1,69 @@ +/* + * Copyright 2024-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.autoconfigure.server; + +import java.time.Duration; +import java.util.concurrent.TimeUnit; +import java.util.function.Function; +import java.util.function.Supplier; + +import io.grpc.ServerBuilder; +import org.junit.jupiter.api.Test; + +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.mock; + +/** + * Tests for {@link BaseServerFactoryPropertyMapper}, + * {@link NettyServerFactoryPropertyMapper}, and + * {@link ShadedNettyServerFactoryPropertyMapper}. + * + * @author Chris Bono + */ +class ServerFactoryPropertyMappersTests { + + @Test + void customizeShadedNettyServerBuilder() { + io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder builder = mock(); + customizeServerBuilder(ShadedNettyServerFactoryPropertyMapper::new, () -> builder); + } + + @Test + void customizeNettyServerBuilder() { + io.grpc.netty.NettyServerBuilder builder = mock(); + customizeServerBuilder(NettyServerFactoryPropertyMapper::new, () -> builder); + } + + @Test + > void customizeBaseServerBuilder() { + T builder = mock(); + customizeServerBuilder(BaseServerFactoryPropertyMapper::new, () -> builder); + } + + private , X extends BaseServerFactoryPropertyMapper> void customizeServerBuilder( + Function mapperFactory, Supplier mockBuilderToCustomize) { + GrpcServerProperties properties = new GrpcServerProperties(); + properties.getKeepAlive().setTime(Duration.ofHours(1)); + properties.getKeepAlive().setTimeout(Duration.ofSeconds(10)); + X mapper = mapperFactory.apply(properties); + T builder = mockBuilderToCustomize.get(); + mapper.customizeServerBuilder(builder); + then(builder).should().keepAliveTime(Duration.ofHours(1).toNanos(), TimeUnit.NANOSECONDS); + then(builder).should().keepAliveTimeout(Duration.ofSeconds(10).toNanos(), TimeUnit.NANOSECONDS); + } + +}