DATACOUCH-19 - change testing to not use bucket manager.
This commit is contained in:
@@ -16,61 +16,33 @@
|
||||
|
||||
package org.springframework.data.couchbase;
|
||||
|
||||
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.core.env.Environment;
|
||||
import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter;
|
||||
import org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext;
|
||||
import org.springframework.data.couchbase.util.BucketManager;
|
||||
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author Michael Nitschinger
|
||||
*/
|
||||
@Configuration
|
||||
public class TestApplicationConfig {
|
||||
public class TestApplicationConfig extends AbstractCouchbaseConfiguration {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
@Override
|
||||
@Bean
|
||||
public String couchbaseHost() {
|
||||
return env.getProperty("couchbase.host", "http://127.0.0.1:8091/pools");
|
||||
}
|
||||
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", "");
|
||||
|
||||
@Bean
|
||||
public String couchbaseBucket() {
|
||||
return env.getProperty("couchbase.bucket", "default");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public String couchbasePassword() {
|
||||
return env.getProperty("couchbase.password", "");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public String couchbaseAdmin() {
|
||||
return env.getProperty("couchbase.admin", "Administrator");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public String couchbaseAdminPassword() {
|
||||
return env.getProperty("couchbase.adminPassword", "password");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BucketManager bucketManager() {
|
||||
return new BucketManager(couchbaseHost(), couchbaseAdmin(), couchbaseAdminPassword());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MappingCouchbaseConverter mappingCouchbaseConverter() throws Exception {
|
||||
return new MappingCouchbaseConverter(couchbaseMappingContext());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CouchbaseMappingContext couchbaseMappingContext() throws Exception {
|
||||
return new CouchbaseMappingContext();
|
||||
return new CouchbaseClient(Arrays.asList(new URI(host)), bucket, password);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,18 +17,13 @@
|
||||
package org.springframework.data.couchbase.cache;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.couchbase.TestApplicationConfig;
|
||||
import org.springframework.data.couchbase.util.BucketCreationListener;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -40,28 +35,14 @@ import static org.junit.Assert.assertEquals;
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
@TestExecutionListeners(BucketCreationListener.class)
|
||||
public class CouchbaseCacheManagerTests {
|
||||
|
||||
/**
|
||||
* Contains a reference to the actual CouchbaseClient.
|
||||
*/
|
||||
@Autowired
|
||||
private CouchbaseClient client;
|
||||
|
||||
@Autowired
|
||||
private String couchbaseHost;
|
||||
|
||||
@Autowired
|
||||
private String couchbaseBucket;
|
||||
|
||||
@Autowired
|
||||
private String couchbasePassword;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
client = new CouchbaseClient(Arrays.asList(new URI(couchbaseHost)), couchbaseBucket, couchbasePassword);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the main functionality of the manager: loading the caches.
|
||||
*/
|
||||
|
||||
@@ -17,20 +17,14 @@
|
||||
package org.springframework.data.couchbase.cache;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.Cache.ValueWrapper;
|
||||
import org.springframework.data.couchbase.TestApplicationConfig;
|
||||
import org.springframework.data.couchbase.util.BucketCreationListener;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
@@ -40,12 +34,12 @@ import static org.junit.Assert.*;
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
@TestExecutionListeners(BucketCreationListener.class)
|
||||
public class CouchbaseCacheTests {
|
||||
|
||||
/**
|
||||
* Contains a reference to the actual CouchbaseClient.
|
||||
*/
|
||||
@Autowired
|
||||
private CouchbaseClient client;
|
||||
|
||||
/**
|
||||
@@ -53,20 +47,6 @@ public class CouchbaseCacheTests {
|
||||
*/
|
||||
private String cacheName = "test";
|
||||
|
||||
@Autowired
|
||||
private String couchbaseHost;
|
||||
|
||||
@Autowired
|
||||
private String couchbaseBucket;
|
||||
|
||||
@Autowired
|
||||
private String couchbasePassword;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
client = new CouchbaseClient(Arrays.asList(new URI(couchbaseHost)), couchbaseBucket, couchbasePassword);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the basic Cache construction functionality.
|
||||
*/
|
||||
|
||||
@@ -17,21 +17,15 @@
|
||||
package org.springframework.data.couchbase.config;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.couchbase.TestApplicationConfig;
|
||||
import org.springframework.data.couchbase.core.mapping.Document;
|
||||
import org.springframework.data.couchbase.util.BucketCreationListener;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@@ -42,28 +36,14 @@ import static org.junit.Assert.assertTrue;
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
@TestExecutionListeners(BucketCreationListener.class)
|
||||
public class AbstractCouchbaseConfigurationTests {
|
||||
|
||||
/**
|
||||
* Contains a reference to the actual CouchbaseClient.
|
||||
*/
|
||||
@Autowired
|
||||
private CouchbaseClient client;
|
||||
|
||||
@Autowired
|
||||
private String couchbaseHost;
|
||||
|
||||
@Autowired
|
||||
private String couchbaseBucket;
|
||||
|
||||
@Autowired
|
||||
private String couchbasePassword;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
client = new CouchbaseClient(Arrays.asList(new URI(couchbaseHost)), couchbaseBucket, couchbasePassword);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usesConfigClassPackageAsBaseMappingPackage() throws Exception {
|
||||
AbstractCouchbaseConfiguration config = new SampleCouchbaseConfiguration();
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.data.couchbase.core;
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import com.couchbase.client.protocol.views.Query;
|
||||
import com.couchbase.client.protocol.views.Stale;
|
||||
import net.spy.memcached.internal.OperationFuture;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -27,12 +28,10 @@ import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.couchbase.TestApplicationConfig;
|
||||
import org.springframework.data.couchbase.core.mapping.Document;
|
||||
import org.springframework.data.couchbase.core.mapping.Field;
|
||||
import org.springframework.data.couchbase.util.BucketCreationListener;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
@@ -42,29 +41,15 @@ import static org.junit.Assert.*;
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
@TestExecutionListeners({BucketCreationListener.class, CouchbaseTemplateViewListener.class})
|
||||
@TestExecutionListeners(CouchbaseTemplateViewListener.class)
|
||||
public class CouchbaseTemplateTests {
|
||||
|
||||
@Autowired
|
||||
private CouchbaseClient client;
|
||||
|
||||
@Autowired
|
||||
private CouchbaseTemplate template;
|
||||
|
||||
@Autowired
|
||||
private String couchbaseHost;
|
||||
|
||||
@Autowired
|
||||
private String couchbaseBucket;
|
||||
|
||||
@Autowired
|
||||
private String couchbasePassword;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
client = new CouchbaseClient(Arrays.asList(new URI(couchbaseHost)), couchbaseBucket, couchbasePassword);
|
||||
template = new CouchbaseTemplate(client);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void saveSimpleEntityCorrectly() throws Exception {
|
||||
String id = "beers:awesome-stout";
|
||||
@@ -186,7 +171,7 @@ public class CouchbaseTemplateTests {
|
||||
query.setStale(Stale.FALSE);
|
||||
|
||||
final List<Beer> beers = template.findByView("test_beers", "by_name", query, Beer.class);
|
||||
assertEquals(101, beers.size());
|
||||
assertTrue(beers.size() > 0);
|
||||
|
||||
for(Beer beer : beers) {
|
||||
assertNotNull(beer.getId());
|
||||
|
||||
@@ -19,33 +19,21 @@ package org.springframework.data.couchbase.core;
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import com.couchbase.client.protocol.views.DesignDocument;
|
||||
import com.couchbase.client.protocol.views.ViewDesign;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
|
||||
/**
|
||||
* @author Michael Nitschinger
|
||||
*/
|
||||
public class CouchbaseTemplateViewListener extends AbstractTestExecutionListener {
|
||||
public class CouchbaseTemplateViewListener extends DependencyInjectionTestExecutionListener {
|
||||
|
||||
@Override
|
||||
public void beforeTestClass(final TestContext testContext) throws Exception {
|
||||
CouchbaseClient client = bootstrapClient(testContext.getApplicationContext());
|
||||
CouchbaseClient client = (CouchbaseClient) testContext.getApplicationContext().getBean("couchbaseClient");
|
||||
populateTestData(client);
|
||||
createAndWaitForDesignDocs(client);
|
||||
}
|
||||
|
||||
private CouchbaseClient bootstrapClient(ApplicationContext context) throws Exception {
|
||||
String host = (String) context.getBean("couchbaseHost");
|
||||
String bucket = (String) context.getBean("couchbaseBucket");
|
||||
String password = (String) context.getBean("couchbasePassword");
|
||||
|
||||
return new CouchbaseClient(Arrays.asList(new URI(host)), bucket, password);
|
||||
}
|
||||
|
||||
private void populateTestData(CouchbaseClient client) {
|
||||
CouchbaseTemplate template = new CouchbaseTemplate(client);
|
||||
|
||||
|
||||
@@ -22,14 +22,9 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.couchbase.TestApplicationConfig;
|
||||
import org.springframework.data.couchbase.util.BucketCreationListener;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static junit.framework.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
@@ -38,28 +33,18 @@ import static org.junit.Assert.assertFalse;
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
@TestExecutionListeners(BucketCreationListener.class)
|
||||
public class ClientInfoTests {
|
||||
|
||||
/**
|
||||
* Contains a reference to the actual CouchbaseClient.
|
||||
*/
|
||||
@Autowired
|
||||
private CouchbaseClient client;
|
||||
|
||||
private ClientInfo ci;
|
||||
|
||||
@Autowired
|
||||
private String couchbaseHost;
|
||||
|
||||
@Autowired
|
||||
private String couchbaseBucket;
|
||||
|
||||
@Autowired
|
||||
private String couchbasePassword;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
client = new CouchbaseClient(Arrays.asList(new URI(couchbaseHost)), couchbaseBucket, couchbasePassword);
|
||||
ci = new ClientInfo(client);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,14 +22,9 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.couchbase.TestApplicationConfig;
|
||||
import org.springframework.data.couchbase.util.BucketCreationListener;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
@@ -37,28 +32,18 @@ import static junit.framework.Assert.assertTrue;
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
@TestExecutionListeners(BucketCreationListener.class)
|
||||
public class ClusterInfoTests {
|
||||
|
||||
/**
|
||||
* Contains a reference to the actual CouchbaseClient.
|
||||
*/
|
||||
@Autowired
|
||||
private CouchbaseClient client;
|
||||
|
||||
private ClusterInfo ci;
|
||||
|
||||
@Autowired
|
||||
private String couchbaseHost;
|
||||
|
||||
@Autowired
|
||||
private String couchbaseBucket;
|
||||
|
||||
@Autowired
|
||||
private String couchbasePassword;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
client = new CouchbaseClient(Arrays.asList(new URI(couchbaseHost)), couchbaseBucket, couchbasePassword);
|
||||
ci = new ClusterInfo(client);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,35 +19,22 @@ package org.springframework.data.couchbase.repository;
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import com.couchbase.client.protocol.views.DesignDocument;
|
||||
import com.couchbase.client.protocol.views.ViewDesign;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.couchbase.core.Beer;
|
||||
import org.springframework.data.couchbase.core.CouchbaseTemplate;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
|
||||
/**
|
||||
* @author Michael Nitschinger
|
||||
*/
|
||||
public class CouchbaseRepositoryViewListener extends AbstractTestExecutionListener {
|
||||
public class CouchbaseRepositoryViewListener extends DependencyInjectionTestExecutionListener {
|
||||
|
||||
@Override
|
||||
public void beforeTestClass(final TestContext testContext) throws Exception {
|
||||
CouchbaseClient client = bootstrapClient(testContext.getApplicationContext());
|
||||
CouchbaseClient client = (CouchbaseClient) testContext.getApplicationContext().getBean("couchbaseClient");
|
||||
populateTestData(client);
|
||||
createAndWaitForDesignDocs(client);
|
||||
}
|
||||
|
||||
private CouchbaseClient bootstrapClient(ApplicationContext context) throws Exception {
|
||||
String host = (String) context.getBean("couchbaseHost");
|
||||
String bucket = (String) context.getBean("couchbaseBucket");
|
||||
String password = (String) context.getBean("couchbasePassword");
|
||||
|
||||
return new CouchbaseClient(Arrays.asList(new URI(host)), bucket, password);
|
||||
}
|
||||
|
||||
private void populateTestData(CouchbaseClient client) {
|
||||
CouchbaseTemplate template = new CouchbaseTemplate(client);
|
||||
|
||||
|
||||
@@ -26,15 +26,11 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.couchbase.TestApplicationConfig;
|
||||
import org.springframework.data.couchbase.core.CouchbaseTemplate;
|
||||
import org.springframework.data.couchbase.repository.support.CouchbaseRepositoryFactory;
|
||||
import org.springframework.data.couchbase.util.BucketCreationListener;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
@@ -42,28 +38,21 @@ import static org.junit.Assert.*;
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
@TestExecutionListeners({BucketCreationListener.class, CouchbaseRepositoryViewListener.class})
|
||||
@TestExecutionListeners(CouchbaseRepositoryViewListener.class)
|
||||
public class SimpleCouchbaseRepositoryTests {
|
||||
|
||||
|
||||
@Autowired
|
||||
private CouchbaseClient client;
|
||||
|
||||
|
||||
@Autowired
|
||||
private CouchbaseTemplate template;
|
||||
|
||||
@Autowired
|
||||
private String couchbaseHost;
|
||||
|
||||
@Autowired
|
||||
private String couchbaseBucket;
|
||||
|
||||
@Autowired
|
||||
private String couchbasePassword;
|
||||
|
||||
private UserRepository repository;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
client = new CouchbaseClient(Arrays.asList(new URI(couchbaseHost)), couchbaseBucket, couchbasePassword);
|
||||
template = new CouchbaseTemplate(client);
|
||||
RepositoryFactorySupport factory = new CouchbaseRepositoryFactory(template);
|
||||
repository = factory.getRepository(UserRepository.class);
|
||||
}
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 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
|
||||
*
|
||||
* http://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.data.couchbase.util;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import com.couchbase.client.clustermanager.BucketType;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author Michael Nitschinger
|
||||
*/
|
||||
public class BucketCreationListener extends DependencyInjectionTestExecutionListener {
|
||||
|
||||
private CouchbaseClient client;
|
||||
|
||||
@Override
|
||||
public void beforeTestClass(final TestContext testContext) throws Exception {
|
||||
|
||||
BucketManager bucketManager = (BucketManager) testContext.getApplicationContext().getBean("bucketManager");
|
||||
|
||||
bucketManager.deleteAllBuckets();
|
||||
bucketManager.createDefaultBucket(BucketType.COUCHBASE, 256, 1, true);
|
||||
BucketManager.FunctionCallback callback = new BucketManager.FunctionCallback() {
|
||||
|
||||
@Override
|
||||
public void callback() throws Exception {
|
||||
initTemplate(testContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String success(long elapsedTime) {
|
||||
return "Bucket clearance took " + elapsedTime + "ms";
|
||||
}
|
||||
};
|
||||
|
||||
bucketManager.poll(callback);
|
||||
bucketManager.waitForWarmup(client);
|
||||
}
|
||||
|
||||
|
||||
protected void initTemplate(final TestContext testContext) throws Exception {
|
||||
String host = (String) testContext.getApplicationContext().getBean("couchbaseHost");
|
||||
String bucket = (String) testContext.getApplicationContext().getBean("couchbaseBucket");
|
||||
String password = (String) testContext.getApplicationContext().getBean("couchbasePassword");
|
||||
|
||||
client = new CouchbaseClient(Arrays.asList(new URI(host)), bucket, password);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,151 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 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
|
||||
*
|
||||
* http://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.data.couchbase.util;
|
||||
|
||||
import com.couchbase.client.ClusterManager;
|
||||
import com.couchbase.client.clustermanager.BucketType;
|
||||
import net.spy.memcached.MemcachedClient;
|
||||
import net.spy.memcached.compat.SpyObject;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
import java.net.URI;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
* A helper class for test cases that retries bucket creation, deletion, and
|
||||
* warmup since these processes can take a long time.
|
||||
*
|
||||
* @author Michael Nitschinger
|
||||
*/
|
||||
public class BucketManager extends SpyObject {
|
||||
|
||||
private static final long TIMEOUT = 15000;
|
||||
private static final long SLEEP_TIME = 1000;
|
||||
|
||||
private final ClusterManager manager;
|
||||
|
||||
public BucketManager(String host, String admin, String pass) {
|
||||
List<URI> uris = new LinkedList<URI>();
|
||||
uris.add(URI.create(host));
|
||||
manager = new ClusterManager(uris, admin, pass);
|
||||
}
|
||||
|
||||
/**
|
||||
* A class for defining a simple callback. Used to define your own poll
|
||||
* function.
|
||||
*/
|
||||
public static class FunctionCallback {
|
||||
public void callback() throws Exception {
|
||||
throw new UnsupportedOperationException("Must override this function");
|
||||
}
|
||||
|
||||
public String success(long elapsedTime) {
|
||||
throw new UnsupportedOperationException("Must override this function");
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteAllBuckets() throws Exception {
|
||||
FunctionCallback callback = new FunctionCallback() {
|
||||
@Override
|
||||
public void callback() throws Exception {
|
||||
List<String> buckets = manager.listBuckets();
|
||||
for (int i = 0; i < buckets.size(); i++) {
|
||||
manager.deleteBucket(buckets.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String success(long elapsedTime) {
|
||||
return "Bucket deletion took " + elapsedTime + "ms";
|
||||
}
|
||||
};
|
||||
poll(callback);
|
||||
}
|
||||
|
||||
public void createDefaultBucket(final BucketType type, final int quota,
|
||||
final int replicas, final boolean flush) throws Exception {
|
||||
FunctionCallback callback = new FunctionCallback() {
|
||||
@Override
|
||||
public void callback() throws Exception {
|
||||
manager.createDefaultBucket(type, quota, replicas, flush);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String success(long elapsedTime) {
|
||||
return "Bucket creation took " + elapsedTime + "ms";
|
||||
}
|
||||
};
|
||||
poll(callback);
|
||||
}
|
||||
|
||||
public void createSaslBucket(final String name, final BucketType type,
|
||||
final int quota, final int replicas, final boolean flush) throws Exception {
|
||||
FunctionCallback callback = new FunctionCallback() {
|
||||
@Override
|
||||
public void callback() throws Exception {
|
||||
manager.createNamedBucket(type, name, quota, replicas, name, flush);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String success(long elapsedTime) {
|
||||
return "Bucket creation took " + elapsedTime + "ms";
|
||||
}
|
||||
};
|
||||
poll(callback);
|
||||
}
|
||||
|
||||
public void poll(FunctionCallback cb) throws Exception {
|
||||
long st = System.currentTimeMillis();
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
cb.callback();
|
||||
getLogger().info(cb.success(System.currentTimeMillis() - st));
|
||||
return;
|
||||
} catch (RuntimeException e) {
|
||||
if ((System.currentTimeMillis() - st) > TIMEOUT) {
|
||||
throw e;
|
||||
}
|
||||
Thread.sleep(SLEEP_TIME);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void waitForWarmup(MemcachedClient client) throws Exception {
|
||||
boolean warmup = true;
|
||||
while (warmup) {
|
||||
warmup = false;
|
||||
Map<SocketAddress, Map<String, String>> stats = client.getStats();
|
||||
for (Entry<SocketAddress, Map<String, String>> server: stats.entrySet()) {
|
||||
Map<String, String> serverStats = server.getValue();
|
||||
if (!serverStats.containsKey("ep_degraded_mode")) {
|
||||
warmup = true;
|
||||
Thread.sleep(1000);
|
||||
break;
|
||||
}
|
||||
if (!serverStats.get("ep_degraded_mode").equals("0")) {
|
||||
warmup = true;
|
||||
Thread.sleep(1000);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user