DATACOUCH-144 - Detect N1QL dependency and fail fast if not available.
Detect N1QL is not available at configuration time for the repositories and at runtime for the template. This throws a UnsupportedCouchbaseFeatureException. The ClusterInfo is captured at bootstrap and used to determine if the feature is available. The xml configuration will need the ClusterInfo-related credentials to be provided explicitly.
This commit is contained in:
@@ -51,7 +51,7 @@ public class CouchbaseTemplateParserIntegrationTests {
|
||||
reader.loadBeanDefinitions(new ClassPathResource("configurations/couchbase-template-bean.xml"));
|
||||
|
||||
BeanDefinition definition = factory.getBeanDefinition("couchbaseTemplate");
|
||||
assertEquals(1, definition.getConstructorArgumentValues().getArgumentCount());
|
||||
assertEquals(2, definition.getConstructorArgumentValues().getArgumentCount());
|
||||
|
||||
factory.getBean("couchbaseTemplate");
|
||||
}
|
||||
@@ -61,7 +61,7 @@ public class CouchbaseTemplateParserIntegrationTests {
|
||||
reader.loadBeanDefinitions(new ClassPathResource("configurations/couchbase-template-with-translation-service-bean.xml"));
|
||||
|
||||
BeanDefinition definition = factory.getBeanDefinition("couchbaseTemplate");
|
||||
assertEquals(2, definition.getConstructorArgumentValues().getArgumentCount());
|
||||
assertEquals(3, definition.getConstructorArgumentValues().getArgumentCount());
|
||||
|
||||
factory.getBean("couchbaseTemplate");
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.data.couchbase.core;
|
||||
import java.util.Collections;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.cluster.ClusterInfo;
|
||||
import com.couchbase.client.java.view.DefaultView;
|
||||
import com.couchbase.client.java.view.DesignDocument;
|
||||
import com.couchbase.client.java.view.View;
|
||||
@@ -31,28 +32,29 @@ import org.springframework.test.context.support.DependencyInjectionTestExecution
|
||||
*/
|
||||
public class CouchbaseTemplateViewListener extends DependencyInjectionTestExecutionListener {
|
||||
|
||||
@Override
|
||||
public void beforeTestClass(final TestContext testContext) throws Exception {
|
||||
Bucket client = (Bucket) testContext.getApplicationContext().getBean("couchbaseBucket");
|
||||
populateTestData(client);
|
||||
createAndWaitForDesignDocs(client);
|
||||
}
|
||||
@Override
|
||||
public void beforeTestClass(final TestContext testContext) throws Exception {
|
||||
Bucket client = (Bucket) testContext.getApplicationContext().getBean("couchbaseBucket");
|
||||
ClusterInfo clusterInfo = (ClusterInfo) testContext.getApplicationContext().getBean("couchbaseClusterInfo");
|
||||
populateTestData(client, clusterInfo);
|
||||
createAndWaitForDesignDocs(client);
|
||||
}
|
||||
|
||||
private void populateTestData(Bucket client) {
|
||||
CouchbaseTemplate template = new CouchbaseTemplate(client);
|
||||
private void populateTestData(Bucket client, ClusterInfo clusterInfo) {
|
||||
CouchbaseTemplate template = new CouchbaseTemplate(clusterInfo, client);
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
Beer b = new Beer("testbeer-" + i).setName("MyBeer " + i).setActive(true);
|
||||
template.save(b);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 100; i++) {
|
||||
Beer b = new Beer("testbeer-" + i).setName("MyBeer " + i).setActive(true);
|
||||
template.save(b);
|
||||
}
|
||||
}
|
||||
|
||||
private void createAndWaitForDesignDocs(Bucket client) {
|
||||
String mapFunction = "function (doc, meta) { if(doc._class == "
|
||||
+ "\"org.springframework.data.couchbase.core.Beer\") { emit(doc.name, null); } }";
|
||||
View view = DefaultView.create("by_name", mapFunction);
|
||||
DesignDocument designDoc = DesignDocument.create("test_beers", Collections.singletonList(view));
|
||||
client.bucketManager().upsertDesignDocument(designDoc);
|
||||
}
|
||||
private void createAndWaitForDesignDocs(Bucket client) {
|
||||
String mapFunction = "function (doc, meta) { if(doc._class == "
|
||||
+ "\"org.springframework.data.couchbase.core.Beer\") { emit(doc.name, null); } }";
|
||||
View view = DefaultView.create("by_name", mapFunction);
|
||||
DesignDocument designDoc = DesignDocument.create("test_beers", Collections.singletonList(view));
|
||||
client.bucketManager().upsertDesignDocument(designDoc);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.List;
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.PersistTo;
|
||||
import com.couchbase.client.java.ReplicateTo;
|
||||
import com.couchbase.client.java.cluster.ClusterInfo;
|
||||
import com.couchbase.client.java.view.DefaultView;
|
||||
import com.couchbase.client.java.view.DesignDocument;
|
||||
import com.couchbase.client.java.view.View;
|
||||
@@ -40,12 +41,13 @@ public class CouchbaseRepositoryViewListener extends DependencyInjectionTestExec
|
||||
@Override
|
||||
public void beforeTestClass(final TestContext testContext) throws Exception {
|
||||
Bucket client = (Bucket) testContext.getApplicationContext().getBean("couchbaseBucket");
|
||||
populateTestData(client);
|
||||
ClusterInfo clusterInfo = (ClusterInfo) testContext.getApplicationContext().getBean("couchbaseClusterInfo");
|
||||
populateTestData(client, clusterInfo);
|
||||
createAndWaitForDesignDocs(client);
|
||||
}
|
||||
|
||||
private void populateTestData(final Bucket client) {
|
||||
CouchbaseTemplate template = new CouchbaseTemplate(client);
|
||||
private void populateTestData(final Bucket client, ClusterInfo clusterInfo) {
|
||||
CouchbaseTemplate template = new CouchbaseTemplate(clusterInfo, client);
|
||||
for (int i = 0; i < 100; i++) {
|
||||
template.save(new User("testuser-" + i, "uname-" + i), PersistTo.MASTER, ReplicateTo.NONE);
|
||||
}
|
||||
|
||||
@@ -2,12 +2,12 @@ package org.springframework.data.couchbase.repository;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.PersistTo;
|
||||
import com.couchbase.client.java.ReplicateTo;
|
||||
import com.couchbase.client.java.cluster.ClusterInfo;
|
||||
import com.couchbase.client.java.view.DefaultView;
|
||||
import com.couchbase.client.java.view.DesignDocument;
|
||||
import com.couchbase.client.java.view.View;
|
||||
@@ -24,12 +24,13 @@ public class QueryDerivationConversionListener extends DependencyInjectionTestEx
|
||||
@Override
|
||||
public void beforeTestClass(final TestContext testContext) throws Exception {
|
||||
Bucket client = (Bucket) testContext.getApplicationContext().getBean("couchbaseBucket");
|
||||
populateTestData(client);
|
||||
ClusterInfo clusterInfo = (ClusterInfo) testContext.getApplicationContext().getBean("couchbaseClusterInfo");
|
||||
populateTestData(client, clusterInfo);
|
||||
createAndWaitForDesignDocs(client);
|
||||
}
|
||||
|
||||
private void populateTestData(Bucket client) {
|
||||
CouchbaseTemplate template = new CouchbaseTemplate(client);
|
||||
private void populateTestData(Bucket client, ClusterInfo clusterInfo) {
|
||||
CouchbaseTemplate template = new CouchbaseTemplate(clusterInfo, client);
|
||||
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.clear();
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.util.List;
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.PersistTo;
|
||||
import com.couchbase.client.java.ReplicateTo;
|
||||
import com.couchbase.client.java.cluster.ClusterInfo;
|
||||
import com.couchbase.client.java.view.DefaultView;
|
||||
import com.couchbase.client.java.view.DesignDocument;
|
||||
import com.couchbase.client.java.view.View;
|
||||
@@ -38,12 +39,13 @@ public class SimpleCouchbaseRepositoryListener extends DependencyInjectionTestEx
|
||||
@Override
|
||||
public void beforeTestClass(final TestContext testContext) throws Exception {
|
||||
Bucket client = (Bucket) testContext.getApplicationContext().getBean("couchbaseBucket");
|
||||
populateTestData(client);
|
||||
ClusterInfo clusterInfo = (ClusterInfo) testContext.getApplicationContext().getBean("couchbaseClusterInfo");
|
||||
populateTestData(client, clusterInfo);
|
||||
createAndWaitForDesignDocs(client);
|
||||
}
|
||||
|
||||
private void populateTestData(Bucket client) {
|
||||
CouchbaseTemplate template = new CouchbaseTemplate(client);
|
||||
private void populateTestData(Bucket client, ClusterInfo clusterInfo) {
|
||||
CouchbaseTemplate template = new CouchbaseTemplate(clusterInfo, client);
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
User u = new User("testuser-" + i, "uname-" + i);
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2014 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.repository.cdi;
|
||||
|
||||
import javax.enterprise.inject.Produces;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.Cluster;
|
||||
import com.couchbase.client.java.CouchbaseCluster;
|
||||
import com.couchbase.client.java.cluster.ClusterInfo;
|
||||
|
||||
import org.springframework.data.couchbase.core.CouchbaseOperations;
|
||||
import org.springframework.data.couchbase.core.CouchbaseTemplate;
|
||||
|
||||
/**
|
||||
* Produces a {@link ClusterInfo} instance for test usage.
|
||||
*
|
||||
* @author Simon Baslé
|
||||
*/
|
||||
class CouchbaseClusterInfoProducer {
|
||||
|
||||
@Produces
|
||||
public ClusterInfo createClusterInfo() throws Exception {
|
||||
Cluster cluster = CouchbaseCluster.create();
|
||||
return cluster.clusterManager("default", "").info();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,22 +19,24 @@ package org.springframework.data.couchbase.repository.cdi;
|
||||
import javax.enterprise.inject.Produces;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.cluster.ClusterInfo;
|
||||
|
||||
import org.springframework.data.couchbase.core.CouchbaseOperations;
|
||||
import org.springframework.data.couchbase.core.CouchbaseTemplate;
|
||||
|
||||
/**
|
||||
* Produces a {@link CouchbaseOperations} instance for test usage.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class CouchbaseOperationsProducer {
|
||||
|
||||
@Produces
|
||||
public CouchbaseOperations createCouchbaseOperations(Bucket couchbaseClient) throws Exception {
|
||||
@Produces
|
||||
public CouchbaseOperations createCouchbaseOperations(Bucket couchbaseClient, ClusterInfo clusterInfo) throws Exception {
|
||||
|
||||
CouchbaseTemplate couchbaseTemplate = new CouchbaseTemplate(couchbaseClient);
|
||||
CouchbaseTemplate couchbaseTemplate = new CouchbaseTemplate(clusterInfo, couchbaseClient);
|
||||
|
||||
return couchbaseTemplate;
|
||||
}
|
||||
return couchbaseTemplate;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* 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.repository.feature;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.couchbase.client.java.cluster.ClusterInfo;
|
||||
import com.couchbase.client.java.query.Query;
|
||||
import com.couchbase.client.java.util.features.CouchbaseFeature;
|
||||
import com.couchbase.client.java.util.features.Version;
|
||||
import org.junit.Assume;
|
||||
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.core.CouchbaseTemplate;
|
||||
import org.springframework.data.couchbase.core.UnsupportedCouchbaseFeatureException;
|
||||
import org.springframework.data.couchbase.repository.User;
|
||||
import org.springframework.data.couchbase.repository.UserRepository;
|
||||
import org.springframework.data.couchbase.repository.support.CouchbaseRepositoryFactory;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* An integration test that validates feature checking with Java Config.
|
||||
*
|
||||
* @author Simon Baslé
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = FeatureDetectionTestApplicationConfig.class)
|
||||
public class FeatureDetectionRepositoryTests {
|
||||
|
||||
@Autowired
|
||||
private CouchbaseTemplate template;
|
||||
|
||||
@Autowired
|
||||
private ClusterInfo clusterInfo;
|
||||
|
||||
@Before
|
||||
public void checkClusterInfo() {
|
||||
Assume.assumeTrue(clusterInfo.getMinVersion() == Version.NO_VERSION);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testN1qlIncompatibleClusterFailsFastForN1qlBasedRepository() throws Exception {
|
||||
RepositoryFactorySupport factory = new CouchbaseRepositoryFactory(template);
|
||||
try {
|
||||
factory.getRepository(UserRepository.class);
|
||||
fail("expected UnsupportedCouchbaseFeatureException");
|
||||
} catch (UnsupportedCouchbaseFeatureException e) {
|
||||
assertEquals(CouchbaseFeature.N1QL, e.getFeature());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testN1qlIncompatibleClusterDoesntFailForViewBasedRepository() throws Exception {
|
||||
RepositoryFactorySupport factory = new CouchbaseRepositoryFactory(template);
|
||||
ViewOnlyUserRepository repository = factory.getRepository(ViewOnlyUserRepository.class);
|
||||
assertNotNull(repository);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testN1qlIncompatibleClusterTemplateFails() {
|
||||
Query query = Query.simple("SELECT * FROM `" + template.getCouchbaseBucket().name() + "`");
|
||||
try {
|
||||
template.findByN1QL(query, User.class);
|
||||
fail("expected findByN1QL to fail with UnsupportedCouchbaseFeatureException");
|
||||
} catch (UnsupportedCouchbaseFeatureException e) {
|
||||
assertEquals(CouchbaseFeature.N1QL, e.getFeature());
|
||||
}
|
||||
|
||||
try {
|
||||
template.findByN1QLProjection(query, User.class);
|
||||
fail("expected findByN1QLProjection to fail with UnsupportedCouchbaseFeatureException");
|
||||
} catch (UnsupportedCouchbaseFeatureException e) {
|
||||
assertEquals(CouchbaseFeature.N1QL, e.getFeature());
|
||||
}
|
||||
|
||||
try {
|
||||
template.queryN1QL(query);
|
||||
fail("expected queryN1QL to fail with UnsupportedCouchbaseFeatureException");
|
||||
} catch (UnsupportedCouchbaseFeatureException e) {
|
||||
assertEquals(CouchbaseFeature.N1QL, e.getFeature());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package org.springframework.data.couchbase.repository.feature;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import com.couchbase.client.java.cluster.ClusterInfo;
|
||||
import com.couchbase.client.java.cluster.DefaultClusterInfo;
|
||||
import com.couchbase.client.java.document.json.JsonObject;
|
||||
import com.couchbase.client.java.env.CouchbaseEnvironment;
|
||||
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
|
||||
|
||||
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.config.AbstractCouchbaseConfiguration;
|
||||
import org.springframework.data.couchbase.core.CouchbaseTemplate;
|
||||
import org.springframework.data.couchbase.core.WriteResultChecking;
|
||||
|
||||
@Configuration
|
||||
public class FeatureDetectionTestApplicationConfig extends AbstractCouchbaseConfiguration {
|
||||
|
||||
@Autowired
|
||||
private Environment springEnv;
|
||||
|
||||
@Bean
|
||||
public String couchbaseAdminUser() {
|
||||
return springEnv.getProperty("couchbase.adminUser", "Administrator");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public String couchbaseAdminPassword() {
|
||||
return springEnv.getProperty("couchbase.adminUser", "password");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<String> getBootstrapHosts() {
|
||||
return Collections.singletonList(springEnv.getProperty("couchbase.host", "127.0.0.1"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBucketName() {
|
||||
return springEnv.getProperty("couchbase.bucket", "default");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBucketPassword() {
|
||||
return springEnv.getProperty("couchbase.password", "");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected CouchbaseEnvironment getEnvironment() {
|
||||
return DefaultCouchbaseEnvironment.builder()
|
||||
.connectTimeout(10000)
|
||||
.kvTimeout(10000)
|
||||
.queryTimeout(10000)
|
||||
.viewTimeout(10000)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CouchbaseTemplate couchbaseTemplate() throws Exception {
|
||||
CouchbaseTemplate template = super.couchbaseTemplate();
|
||||
template.setWriteResultChecking(WriteResultChecking.LOG);
|
||||
return template;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClusterInfo couchbaseClusterInfo() throws Exception {
|
||||
return new DefaultClusterInfo(JsonObject.empty());
|
||||
}
|
||||
|
||||
//change the name of the field that will hold type information
|
||||
@Override
|
||||
public String typeKey() {
|
||||
return "javaClass";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.springframework.data.couchbase.repository.feature;
|
||||
|
||||
import org.springframework.data.couchbase.repository.User;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface ViewOnlyUserRepository extends CrudRepository<User, String> {
|
||||
}
|
||||
Reference in New Issue
Block a user