diff --git a/pom.xml b/pom.xml
index ba278d48..1120e0ce 100644
--- a/pom.xml
+++ b/pom.xml
@@ -74,6 +74,13 @@
test
+
+ org.apache.httpcomponents
+ httpclient
+ 4.1
+ test
+
+
com.fasterxml.jackson.core
jackson-databind
diff --git a/src/test/java/org/springframework/data/couchbase/BucketCreator.java b/src/test/java/org/springframework/data/couchbase/BucketCreator.java
new file mode 100644
index 00000000..e944518f
--- /dev/null
+++ b/src/test/java/org/springframework/data/couchbase/BucketCreator.java
@@ -0,0 +1,62 @@
+package org.springframework.data.couchbase;
+
+import com.couchbase.client.ClusterManager;
+import com.couchbase.client.clustermanager.BucketType;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.http.ResponseEntity;
+import org.springframework.http.client.ClientHttpRequestFactory;
+import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
+import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.RestTemplate;
+
+import java.net.URI;
+import java.util.Arrays;
+
+public class BucketCreator implements InitializingBean {
+
+ private final String hostUri;
+ private final String adminUser;
+ private final String adminPass;
+
+ public BucketCreator(String host, String user, String pass) {
+ hostUri = host;
+ adminUser = user;
+ adminPass = pass;
+ }
+
+ @Override
+ public void afterPropertiesSet() throws Exception {
+ DefaultHttpClient client = new DefaultHttpClient();
+ BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
+ credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(adminUser, adminPass));
+ client.setCredentialsProvider(credentialsProvider);
+ ClientHttpRequestFactory rf = new HttpComponentsClientHttpRequestFactory(client);
+
+ RestTemplate template = new RestTemplate(rf);
+
+ String fullUri = hostUri + "/default/buckets/default";
+
+ try {
+ template.getForEntity(fullUri, String.class);
+ } catch (HttpClientErrorException ex) {
+ if (ex.getMessage().equals("404 Object Not Found")) {
+ createBucket();
+ } else {
+ throw new RuntimeException("Could not see if bucket is already created.", ex);
+ }
+ }
+ }
+
+ private void createBucket() throws Exception {
+ ClusterManager bucketManager =
+ new ClusterManager(Arrays.asList(new URI(hostUri)), adminUser, adminPass);
+ bucketManager.createDefaultBucket(BucketType.COUCHBASE, 128, 0, true);
+ Thread.sleep(5000);
+ bucketManager.shutdown();
+ }
+
+}
diff --git a/src/test/java/org/springframework/data/couchbase/TestApplicationConfig.java b/src/test/java/org/springframework/data/couchbase/TestApplicationConfig.java
index 8f27491b..5de2b2e5 100644
--- a/src/test/java/org/springframework/data/couchbase/TestApplicationConfig.java
+++ b/src/test/java/org/springframework/data/couchbase/TestApplicationConfig.java
@@ -20,6 +20,7 @@ import com.couchbase.client.CouchbaseClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.DependsOn;
import org.springframework.core.env.Environment;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
@@ -35,14 +36,44 @@ public class TestApplicationConfig extends AbstractCouchbaseConfiguration {
@Autowired
private Environment env;
- @Override
@Bean
- public CouchbaseClient couchbaseClient() throws Exception {
- String host = env.getProperty("couchbase.host", "http://127.0.0.1:8091/pools");
- String bucket = env.getProperty("couchbase.bucket", "default");
- String password = env.getProperty("couchbase.password", "");
-
- return new CouchbaseClient(Arrays.asList(new URI(host)), bucket, password);
+ public String couchbaseHost() {
+ return env.getProperty("couchbase.host", "http://127.0.0.1:8091/pools");
}
+ @Bean
+ public String couchbaseBucket() {
+ return env.getProperty("couchbase.bucket", "default");
+ }
+
+ @Bean
+ public String couchbasePassword() {
+ return env.getProperty("couchbase.password", "");
+ }
+
+ @Bean
+ public String couchbaseAdminUser() {
+ return env.getProperty("couchbase.adminUser", "Administrator");
+ }
+
+ @Bean
+ public String couchbaseAdminPassword() {
+ return env.getProperty("couchbase.adminUser", "password");
+ }
+
+ @Bean
+ public BucketCreator bucketCreator() throws Exception {
+ return new BucketCreator(couchbaseHost(), couchbaseAdminUser(), couchbaseAdminPassword());
+ }
+
+ @Override
+ @Bean
+ @DependsOn("bucketCreator")
+ public CouchbaseClient couchbaseClient() throws Exception {
+ return new CouchbaseClient(
+ Arrays.asList(new URI(couchbaseHost())), couchbaseBucket(), couchbasePassword());
+ }
+
+
+
}