Skip to content

Commit c5346c6

Browse files
committed
Make isEnabled anemic (move logic to determineEnabled)
Signed-off-by: onobc <chris.bono@gmail.com>
1 parent a60daf6 commit c5346c6

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

module/spring-boot-grpc-client/src/main/java/org/springframework/boot/grpc/client/autoconfigure/GrpcClientProperties.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,15 +418,18 @@ public static class Ssl {
418418
*/
419419
private @Nullable String bundle;
420420

421-
// TODO bono
422-
public boolean isEnabled() {
423-
return (this.enabled != null) ? this.enabled : this.bundle != null;
421+
public @Nullable Boolean isEnabled() {
422+
return this.enabled;
424423
}
425424

426-
public void setEnabled(boolean enabled) {
425+
public void setEnabled(@Nullable Boolean enabled) {
427426
this.enabled = enabled;
428427
}
429428

429+
public boolean determineEnabled() {
430+
return (this.enabled != null) ? this.enabled : this.bundle != null;
431+
}
432+
430433
public @Nullable String getBundle() {
431434
return this.bundle;
432435
}

module/spring-boot-grpc-client/src/main/java/org/springframework/boot/grpc/client/autoconfigure/NamedChannelCredentialsProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ public NamedChannelCredentialsProvider(SslBundles bundles, GrpcClientProperties
5050
@Override
5151
public ChannelCredentials getChannelCredentials(String path) {
5252
ChannelConfig channel = this.properties.getChannel(path);
53-
if (!channel.getSsl().isEnabled() && channel.getNegotiationType() == NegotiationType.PLAINTEXT) {
53+
boolean sslEnabled = channel.getSsl().determineEnabled();
54+
if (!sslEnabled && channel.getNegotiationType() == NegotiationType.PLAINTEXT) {
5455
return InsecureChannelCredentials.create();
5556
}
56-
if (channel.getSsl().isEnabled()) {
57+
if (sslEnabled) {
5758
String bundleName = channel.getSsl().getBundle();
5859
Assert.notNull(bundleName, "Bundle name must not be null when SSL is enabled");
5960
SslBundle bundle = this.bundles.getBundle(bundleName);

module/spring-boot-grpc-client/src/test/java/org/springframework/boot/grpc/client/autoconfigure/GrpcClientPropertiesTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ private void withDefaultValues(String channelName,
9292
assertThat(channel.getMaxInboundMetadataSize()).isEqualTo(DataSize.ofBytes(8192));
9393
assertThat(channel.getUserAgent()).isNull();
9494
assertThat(channel.isSecure()).isTrue();
95-
assertThat(channel.getSsl().isEnabled()).isFalse();
95+
assertThat(channel.getSsl().isEnabled()).isNull();
96+
assertThat(channel.getSsl().determineEnabled()).isFalse();
9697
assertThat(channel.getSsl().getBundle()).isNull();
9798
}
9899

@@ -144,6 +145,7 @@ private void withSpecifiedValues(String channelName,
144145
assertThat(channel.getUserAgent()).isEqualTo("me");
145146
assertThat(channel.isSecure()).isFalse();
146147
assertThat(channel.getSsl().isEnabled()).isTrue();
148+
assertThat(channel.getSsl().determineEnabled()).isTrue();
147149
assertThat(channel.getSsl().getBundle()).isEqualTo("my-bundle");
148150
}
149151

@@ -182,6 +184,16 @@ void withServiceConfig() {
182184
assertThat(channel.getServiceConfig().get("something")).isInstanceOf(Map.class);
183185
}
184186

187+
@Test
188+
void whenBundleNameSetThenDetermineEnabledReturnsTrue() {
189+
Map<String, String> map = new HashMap<>();
190+
map.put("spring.grpc.client.default-channel.ssl.bundle", "my-bundle");
191+
GrpcClientProperties properties = bindProperties(map);
192+
var channel = properties.getDefaultChannel();
193+
assertThat(channel.getSsl().isEnabled()).isNull();
194+
assertThat(channel.getSsl().determineEnabled()).isTrue();
195+
}
196+
185197
}
186198

187199
@Nested

0 commit comments

Comments
 (0)