Polish Couchbase tests

This commit is contained in:
Brian Clozel 2018-03-15 15:49:21 +01:00
parent ac64441611
commit a0b9974796
3 changed files with 107 additions and 68 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,6 +24,9 @@ import com.couchbase.client.java.env.CouchbaseEnvironment;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -35,28 +38,38 @@ import static org.mockito.Mockito.mock;
* *
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public class CouchbaseAutoConfigurationIntegrationTests public class CouchbaseAutoConfigurationIntegrationTests {
extends AbstractCouchbaseAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
CouchbaseAutoConfiguration.class));
@Rule @Rule
public final CouchbaseTestServer couchbase = new CouchbaseTestServer(); public final CouchbaseTestServer couchbase = new CouchbaseTestServer();
@Test @Test
public void defaultConfiguration() { public void defaultConfiguration() {
load(null, "spring.couchbase.bootstrapHosts=localhost"); this.contextRunner
assertThat(this.context.getBeansOfType(Cluster.class)).hasSize(1); .withPropertyValues("spring.couchbase.bootstrapHosts=localhost")
assertThat(this.context.getBeansOfType(ClusterInfo.class)).hasSize(1); .run((context) -> {
assertThat(this.context.getBeansOfType(CouchbaseEnvironment.class)).hasSize(1); assertThat(context).hasSingleBean(Cluster.class)
assertThat(this.context.getBeansOfType(Bucket.class)).hasSize(1); .hasSingleBean(ClusterInfo.class)
.hasSingleBean(CouchbaseEnvironment.class)
.hasSingleBean(Bucket.class);
});
} }
@Test @Test
public void customConfiguration() { public void customConfiguration() {
load(CustomConfiguration.class, "spring.couchbase.bootstrapHosts=localhost"); this.contextRunner
assertThat(this.context.getBeansOfType(Cluster.class)).hasSize(2); .withUserConfiguration(CustomConfiguration.class)
assertThat(this.context.getBeansOfType(ClusterInfo.class)).hasSize(1); .withPropertyValues("spring.couchbase.bootstrapHosts=localhost")
assertThat(this.context.getBeansOfType(CouchbaseEnvironment.class)).hasSize(1); .run((context) -> {
assertThat(this.context.getBeansOfType(Bucket.class)).hasSize(2); assertThat(context.getBeansOfType(Cluster.class)).hasSize(2);
assertThat(context.getBeansOfType(ClusterInfo.class)).hasSize(1);
assertThat(context.getBeansOfType(CouchbaseEnvironment.class)).hasSize(1);
assertThat(context.getBeansOfType(Bucket.class)).hasSize(2);
});
} }
@Configuration @Configuration

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
package org.springframework.boot.autoconfigure.couchbase; package org.springframework.boot.autoconfigure.couchbase;
import java.util.function.Consumer;
import com.couchbase.client.java.Bucket; import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster; import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseBucket; import com.couchbase.client.java.CouchbaseBucket;
@ -24,8 +26,12 @@ import com.couchbase.client.java.env.CouchbaseEnvironment;
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment; import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration.CouchbaseConfiguration; import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration.CouchbaseConfiguration;
import org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration; import org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
@ -38,102 +44,122 @@ import static org.mockito.Mockito.mock;
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public class CouchbaseAutoConfigurationTests public class CouchbaseAutoConfigurationTests {
extends AbstractCouchbaseAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
CouchbaseAutoConfiguration.class));
@Test @Test
public void bootstrapHostsIsRequired() { public void bootstrapHostsIsRequired() {
load(null); this.contextRunner.run((context) -> {
assertNoCouchbaseBeans(); assertNoCouchbaseBeans(context);
});
} }
@Test @Test
public void bootstrapHostsNotRequiredIfCouchbaseConfigurerIsSet() { public void bootstrapHostsNotRequiredIfCouchbaseConfigurerIsSet() {
load(CouchbaseTestConfigurer.class); this.contextRunner.withUserConfiguration(CouchbaseTestConfigurer.class)
assertThat(this.context.getBeansOfType(CouchbaseTestConfigurer.class)).hasSize(1); .run((context) -> {
// No beans are going to be created assertThat(context).hasSingleBean(CouchbaseTestConfigurer.class);
assertNoCouchbaseBeans(); // No beans are going to be created
assertNoCouchbaseBeans(context);
});
} }
@Test @Test
public void bootstrapHostsIgnoredIfCouchbaseConfigurerIsSet() { public void bootstrapHostsIgnoredIfCouchbaseConfigurerIsSet() {
load(CouchbaseTestConfigurer.class, "spring.couchbase.bootstrapHosts=localhost"); this.contextRunner.withUserConfiguration(CouchbaseTestConfigurer.class)
assertThat(this.context.getBeansOfType(CouchbaseTestConfigurer.class)).hasSize(1); .withPropertyValues("spring.couchbase.bootstrapHosts=localhost")
assertNoCouchbaseBeans(); .run((context) -> {
assertThat(context).hasSingleBean(CouchbaseTestConfigurer.class);
assertNoCouchbaseBeans(context);
});
} }
private void assertNoCouchbaseBeans() { private void assertNoCouchbaseBeans(AssertableApplicationContext context) {
// No beans are going to be created // No beans are going to be created
assertThat(this.context.getBeansOfType(CouchbaseEnvironment.class)).isEmpty(); assertThat(context).doesNotHaveBean(CouchbaseEnvironment.class)
assertThat(this.context.getBeansOfType(ClusterInfo.class)).isEmpty(); .doesNotHaveBean(ClusterInfo.class)
assertThat(this.context.getBeansOfType(Cluster.class)).isEmpty(); .doesNotHaveBean(Cluster.class)
assertThat(this.context.getBeansOfType(Bucket.class)).isEmpty(); .doesNotHaveBean(Bucket.class);
} }
@Test @Test
public void customizeEnvEndpoints() throws Exception { public void customizeEnvEndpoints() {
DefaultCouchbaseEnvironment env = customizeEnv( testCouchbaseEnv(env -> {
assertThat(env.kvEndpoints()).isEqualTo(4);
assertThat(env.queryEndpoints()).isEqualTo(5);
assertThat(env.viewEndpoints()).isEqualTo(6);
},
"spring.couchbase.env.endpoints.keyValue=4", "spring.couchbase.env.endpoints.keyValue=4",
"spring.couchbase.env.endpoints.query=5", "spring.couchbase.env.endpoints.query=5",
"spring.couchbase.env.endpoints.view=6"); "spring.couchbase.env.endpoints.view=6");
assertThat(env.kvEndpoints()).isEqualTo(4);
assertThat(env.queryEndpoints()).isEqualTo(5);
assertThat(env.viewEndpoints()).isEqualTo(6);
} }
@Test @Test
public void customizeEnvTimeouts() throws Exception { public void customizeEnvTimeouts() {
DefaultCouchbaseEnvironment env = customizeEnv( testCouchbaseEnv(env -> {
assertThat(env.connectTimeout()).isEqualTo(100);
assertThat(env.kvTimeout()).isEqualTo(200);
assertThat(env.queryTimeout()).isEqualTo(300);
assertThat(env.socketConnectTimeout()).isEqualTo(400);
assertThat(env.viewTimeout()).isEqualTo(500);
},
"spring.couchbase.env.timeouts.connect=100", "spring.couchbase.env.timeouts.connect=100",
"spring.couchbase.env.timeouts.keyValue=200", "spring.couchbase.env.timeouts.keyValue=200",
"spring.couchbase.env.timeouts.query=300", "spring.couchbase.env.timeouts.query=300",
"spring.couchbase.env.timeouts.socket-connect=400", "spring.couchbase.env.timeouts.socket-connect=400",
"spring.couchbase.env.timeouts.view=500"); "spring.couchbase.env.timeouts.view=500");
assertThat(env.connectTimeout()).isEqualTo(100);
assertThat(env.kvTimeout()).isEqualTo(200);
assertThat(env.queryTimeout()).isEqualTo(300);
assertThat(env.socketConnectTimeout()).isEqualTo(400);
assertThat(env.viewTimeout()).isEqualTo(500);
} }
@Test @Test
public void enableSslNoEnabledFlag() throws Exception { public void enableSslNoEnabledFlag() {
DefaultCouchbaseEnvironment env = customizeEnv( testCouchbaseEnv(env -> {
assertThat(env.sslEnabled()).isTrue();
assertThat(env.sslKeystoreFile()).isEqualTo("foo");
assertThat(env.sslKeystorePassword()).isEqualTo("secret");
},
"spring.couchbase.env.ssl.keyStore=foo", "spring.couchbase.env.ssl.keyStore=foo",
"spring.couchbase.env.ssl.keyStorePassword=secret"); "spring.couchbase.env.ssl.keyStorePassword=secret");
assertThat(env.sslEnabled()).isTrue();
assertThat(env.sslKeystoreFile()).isEqualTo("foo");
assertThat(env.sslKeystorePassword()).isEqualTo("secret");
} }
@Test @Test
public void disableSslEvenWithKeyStore() throws Exception { public void disableSslEvenWithKeyStore() {
DefaultCouchbaseEnvironment env = customizeEnv( testCouchbaseEnv(env -> {
assertThat(env.sslEnabled()).isFalse();
assertThat(env.sslKeystoreFile()).isNull();
assertThat(env.sslKeystorePassword()).isNull();
},
"spring.couchbase.env.ssl.enabled=false", "spring.couchbase.env.ssl.enabled=false",
"spring.couchbase.env.ssl.keyStore=foo", "spring.couchbase.env.ssl.keyStore=foo",
"spring.couchbase.env.ssl.keyStorePassword=secret"); "spring.couchbase.env.ssl.keyStorePassword=secret");
assertThat(env.sslEnabled()).isFalse(); }
assertThat(env.sslKeystoreFile()).isNull();
assertThat(env.sslKeystorePassword()).isNull(); private void testCouchbaseEnv(Consumer<DefaultCouchbaseEnvironment> environmentConsumer,
String... environment) {
this.contextRunner.withUserConfiguration(CouchbaseTestConfigurer.class)
.withPropertyValues(environment)
.run((context) -> {
CouchbaseProperties properties = context.getBean(CouchbaseProperties.class);
DefaultCouchbaseEnvironment env = new CouchbaseConfiguration(properties)
.couchbaseEnvironment();
environmentConsumer.accept(env);
});
} }
@Test @Test
public void customizeEnvWithCustomCouchbaseConfiguration() { public void customizeEnvWithCustomCouchbaseConfiguration() {
load(CustomCouchbaseConfiguration.class, this.contextRunner.withUserConfiguration(CustomCouchbaseConfiguration.class)
"spring.couchbase.bootstrap-hosts=localhost", .withPropertyValues(
"spring.couchbase.env.timeouts.connect=100"); "spring.couchbase.bootstrap-hosts=localhost",
assertThat(this.context.getBeansOfType(CouchbaseConfiguration.class)).hasSize(1); "spring.couchbase.env.timeouts.connect=100")
DefaultCouchbaseEnvironment env = this.context .run((context) -> {
.getBean(DefaultCouchbaseEnvironment.class); assertThat(context).hasSingleBean(CouchbaseConfiguration.class);
assertThat(env.socketConnectTimeout()).isEqualTo(5000); DefaultCouchbaseEnvironment env = context.getBean(DefaultCouchbaseEnvironment.class);
assertThat(env.connectTimeout()).isEqualTo(2000); assertThat(env.socketConnectTimeout()).isEqualTo(5000);
} assertThat(env.connectTimeout()).isEqualTo(2000);
});
private DefaultCouchbaseEnvironment customizeEnv(String... environment)
throws Exception {
load(CouchbaseTestConfigurer.class, environment);
CouchbaseProperties properties = this.context.getBean(CouchbaseProperties.class);
return new CouchbaseConfiguration(properties).couchbaseEnvironment();
} }
@Configuration @Configuration