Skip to content

Commit

Permalink
Add conditionals on PubSubBinderConfig, including a property to expli… (
Browse files Browse the repository at this point in the history
#393)

* Add conditionals on PubSubBinderConfig, including a property to explicitly disable it.
  • Loading branch information
Travis Tomsu authored Mar 17, 2021
1 parent b79122e commit 7442572
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/src/main/asciidoc/_configprops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
|spring.cloud.gcp.metrics.enabled | true | Auto-configure Google Cloud Monitoring for Micrometer.
|spring.cloud.gcp.metrics.project-id | | Overrides the GCP project ID specified in the Core module.
|spring.cloud.gcp.project-id | | GCP project ID where services are running.
|spring.cloud.gcp.pubsub.credentials.encoded-key | |
|spring.cloud.gcp.pubsub.binder.enabled | true | Auto-configure Google Cloud Pub/Sub Stream Binder components.
|spring.cloud.gcp.pubsub.credentials.encoded-key | |
|spring.cloud.gcp.pubsub.credentials.location | |
|spring.cloud.gcp.pubsub.credentials.scopes | |
|spring.cloud.gcp.pubsub.emulator-host | | The host and port of the local running emulator. If provided, this will setup the client to connect against a running pub/sub emulator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
"description": "Auto-configure Google Cloud Pub/Sub components.",
"defaultValue": true
},
{
"name": "spring.cloud.gcp.pubsub.binder.enabled",
"type": "java.lang.Boolean",
"description": "Auto-configure Google Cloud Pub/Sub Stream Binder components.",
"defaultValue": true
},
{
"name": "spring.cloud.gcp.pubsub.reactive.enabled",
"type": "java.lang.Boolean",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import com.google.cloud.spring.stream.binder.pubsub.properties.PubSubExtendedBindingProperties;
import com.google.cloud.spring.stream.binder.pubsub.provisioning.PubSubChannelProvisioner;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
import org.springframework.cloud.stream.binder.Binder;
Expand All @@ -46,6 +48,8 @@
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(Binder.class)
@ConditionalOnBean({PubSubAdmin.class, PubSubTemplate.class})
@ConditionalOnProperty(value = "spring.cloud.gcp.pubsub.binder.enabled", matchIfMissing = true)
@EnableConfigurationProperties(PubSubExtendedBindingProperties.class)
public class PubSubBinderConfiguration {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright 2021-2021 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 com.google.cloud.spring.stream.binder.pubsub.config;

import com.google.api.gax.core.CredentialsProvider;
import com.google.auth.Credentials;
import com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration;
import com.google.cloud.spring.autoconfigure.pubsub.GcpPubSubAutoConfiguration;
import com.google.cloud.spring.core.GcpProjectIdProvider;
import com.google.cloud.spring.pubsub.PubSubAdmin;
import com.google.cloud.spring.pubsub.core.PubSubTemplate;
import com.google.cloud.spring.stream.binder.pubsub.provisioning.PubSubChannelProvisioner;
import org.junit.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.test.annotation.DirtiesContext;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

/**
* Tests for PubSubBinderConfiguration and its interaction with other autoconfiguration classes.
*/
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
public class PubSubBinderConfigurationTests {

@Test
public void testEverythingEnabled_implicit() {
ApplicationContextRunner baseContext = new ApplicationContextRunner()
// no property values specified
.withConfiguration(AutoConfigurations.of(
GcpContextAutoConfiguration.class,
GcpPubSubAutoConfiguration.class,
PubSubBinderConfiguration.class))
.withUserConfiguration(TestConfiguration.class);
baseContext.run(ctx -> assertThat(ctx).hasSingleBean(PubSubChannelProvisioner.class));
}

@Test
public void testEverythingEnabled_explicit() {
ApplicationContextRunner baseContext = new ApplicationContextRunner()
.withPropertyValues(
// including 'core' for completeness, but it's not super relevant to the test.
"spring.cloud.gcp.core.enabled=true",
"spring.cloud.gcp.pubsub.enabled=true",
"spring.cloud.gcp.pubsub.binder.enabled=true")
.withConfiguration(AutoConfigurations.of(
GcpContextAutoConfiguration.class,
GcpPubSubAutoConfiguration.class,
PubSubBinderConfiguration.class))
.withUserConfiguration(TestConfiguration.class);
baseContext.run(ctx -> assertThat(ctx).hasSingleBean(PubSubChannelProvisioner.class));
}

@Test
public void testBinderDisabled() {
ApplicationContextRunner baseContext = new ApplicationContextRunner()
.withPropertyValues(
"spring.cloud.gcp.pubsub.enabled=true",
"spring.cloud.gcp.pubsub.binder.enabled=false")
.withConfiguration(AutoConfigurations.of(
GcpContextAutoConfiguration.class,
GcpPubSubAutoConfiguration.class,
PubSubBinderConfiguration.class))
.withUserConfiguration(TestConfiguration.class);
baseContext.run(ctx -> assertThat(ctx).doesNotHaveBean(PubSubChannelProvisioner.class));
}

@Test
public void testPubSubDisabled_noReplacements() {
ApplicationContextRunner baseContext = new ApplicationContextRunner()
.withPropertyValues(
"spring.cloud.gcp.pubsub.enabled=false",
"spring.cloud.gcp.pubsub.binder.enabled=true")
.withConfiguration(AutoConfigurations.of(
GcpContextAutoConfiguration.class,
GcpPubSubAutoConfiguration.class,
PubSubBinderConfiguration.class))
.withUserConfiguration(TestConfiguration.class);
baseContext.run(ctx -> assertThat(ctx).doesNotHaveBean(PubSubChannelProvisioner.class));
}
@Test
public void testPubSubDisabled_withReplacements() {
ApplicationContextRunner baseContext = new ApplicationContextRunner()
.withPropertyValues(
"spring.cloud.gcp.pubsub.enabled=false",
"spring.cloud.gcp.pubsub.binder.enabled=true")
.withBean(PubSubAdmin.class, () -> mock(PubSubAdmin.class))
.withBean(PubSubTemplate.class, () -> mock(PubSubTemplate.class))
.withConfiguration(AutoConfigurations.of(
GcpContextAutoConfiguration.class,
GcpPubSubAutoConfiguration.class,
PubSubBinderConfiguration.class))
.withUserConfiguration(TestConfiguration.class);
baseContext.run(ctx -> assertThat(ctx).hasSingleBean(PubSubChannelProvisioner.class));
}

@Test
public void testBothDisabled() {
ApplicationContextRunner baseContext = new ApplicationContextRunner()
.withPropertyValues(
"spring.cloud.gcp.pubsub.enabled=false",
"spring.cloud.gcp.pubsub.binder.enabled=false")
.withConfiguration(AutoConfigurations.of(
GcpContextAutoConfiguration.class,
GcpPubSubAutoConfiguration.class,
PubSubBinderConfiguration.class))
.withUserConfiguration(TestConfiguration.class);
baseContext.run(ctx -> assertThat(ctx).doesNotHaveBean(PubSubChannelProvisioner.class));
}

private static class TestConfiguration {
@Bean
public CredentialsProvider googleCredentials() {
return () -> mock(Credentials.class);
}


@Bean
public GcpProjectIdProvider gcpProjectIdProvider() {
return () -> "test-project";
}
}
}

0 comments on commit 7442572

Please sign in to comment.