Add property / mapper tests

This commit is contained in:
Chris Bono
2024-10-01 22:44:04 -05:00
committed by Dave Syer
parent 11c658bfba
commit e0159ed6fb
3 changed files with 144 additions and 2 deletions

View File

@@ -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;

View File

@@ -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<String, String> map) {
return new Binder(new MapConfigurationPropertySource(map))
.bind("spring.grpc.server", GrpcServerProperties.class)
.get();
}
@Nested
class BaseProperties {
@Test
void bind() {
Map<String, String> 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<String, String> 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));
}
}
}

View File

@@ -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
<T extends ServerBuilder<T>> void customizeBaseServerBuilder() {
T builder = mock();
customizeServerBuilder(BaseServerFactoryPropertyMapper::new, () -> builder);
}
private <T extends ServerBuilder<T>, X extends BaseServerFactoryPropertyMapper<T>> void customizeServerBuilder(
Function<GrpcServerProperties, X> mapperFactory, Supplier<T> 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);
}
}