DATACOUCH-109 - Provide CDI support for Spring Data Couchbase repositories
Initial integration to enable CDI usage of Spring Data Couchbase repositories along with custom repository implementations.
This commit is contained in:
committed by
Michael Nitschinger
parent
57e7af1f27
commit
2da3840334
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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 org.springframework.data.couchbase.repository.CouchbaseRepository;
|
||||
import org.springframework.data.couchbase.repository.User;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public interface CdiPersonRepository extends CouchbaseRepository<Person, String>, CdiPersonRepositoryCustom {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public interface CdiPersonRepositoryCustom {
|
||||
|
||||
int returnTwo();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class CdiPersonRepositoryImpl implements CdiPersonRepositoryCustom {
|
||||
|
||||
@Override
|
||||
public int returnTwo() {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
@@ -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.inject.Inject;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import org.springframework.data.couchbase.repository.UserRepository;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class CdiRepositoryClient {
|
||||
|
||||
@Inject
|
||||
private CdiPersonRepository cdiPersonRepository;
|
||||
|
||||
@Inject
|
||||
private CouchbaseClient couchbaseClient;
|
||||
|
||||
public CdiPersonRepository getCdiPersonRepository() {
|
||||
return cdiPersonRepository;
|
||||
}
|
||||
|
||||
public CouchbaseClient getCouchbaseClient() {
|
||||
return couchbaseClient;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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 static org.junit.Assert.*;
|
||||
|
||||
import org.apache.webbeans.cditest.CdiTestContainer;
|
||||
import org.apache.webbeans.cditest.CdiTestContainerLoader;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import com.couchbase.client.protocol.views.DesignDocument;
|
||||
import com.couchbase.client.protocol.views.ViewDesign;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class CdiRepositoryTests {
|
||||
|
||||
private static CdiTestContainer cdiContainer;
|
||||
private CdiPersonRepository repository;
|
||||
private CouchbaseClient couchbaseClient;
|
||||
|
||||
@BeforeClass
|
||||
public static void init() throws Exception {
|
||||
cdiContainer = CdiTestContainerLoader.getCdiContainer();
|
||||
cdiContainer.startApplicationScope();
|
||||
cdiContainer.bootContainer();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void shutdown() throws Exception {
|
||||
cdiContainer.stopContexts();
|
||||
cdiContainer.shutdownContainer();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
CdiRepositoryClient repositoryClient = cdiContainer.getInstance(CdiRepositoryClient.class);
|
||||
repository = repositoryClient.getCdiPersonRepository();
|
||||
|
||||
couchbaseClient = repositoryClient.getCouchbaseClient();
|
||||
createAndWaitForDesignDocs(couchbaseClient);
|
||||
|
||||
}
|
||||
|
||||
private void createAndWaitForDesignDocs(CouchbaseClient client) {
|
||||
DesignDocument designDoc = new DesignDocument("person");
|
||||
String mapFunction = "function (doc, meta) { if(doc._class == \"" + Person.class.getName()
|
||||
+ "\") { emit(null, null); } }";
|
||||
designDoc.setView(new ViewDesign("all", mapFunction, "_count"));
|
||||
client.createDesignDoc(designDoc);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACOUCH-109
|
||||
*/
|
||||
@Test
|
||||
public void testCdiRepository() {
|
||||
assertNotNull(repository);
|
||||
repository.deleteAll();
|
||||
|
||||
Person bean = new Person("key", "username");
|
||||
|
||||
repository.save(bean);
|
||||
|
||||
assertTrue(repository.exists(bean.getId()));
|
||||
|
||||
Person retrieved = repository.findOne(bean.getId());
|
||||
assertNotNull(retrieved);
|
||||
assertEquals(bean.getName(), retrieved.getName());
|
||||
assertEquals(bean.getId(), retrieved.getId());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACOUCH-109
|
||||
*/
|
||||
@Test
|
||||
public void testCustomRepository() {
|
||||
|
||||
assertEquals(2, repository.returnTwo());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.Disposes;
|
||||
import javax.enterprise.inject.Produces;
|
||||
|
||||
import org.springframework.data.couchbase.core.CouchbaseFactoryBean;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
|
||||
/**
|
||||
* Producer for {@link CouchbaseClient}. Defaults from {@link CouchbaseFactoryBean} are sufficient for our test.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class CouchbaseClientProducer {
|
||||
|
||||
@Produces
|
||||
public CouchbaseClient createCouchbaseClient() throws Exception {
|
||||
|
||||
CouchbaseFactoryBean couchbaseFactoryBean = new CouchbaseFactoryBean();
|
||||
couchbaseFactoryBean.setBucket("default");
|
||||
couchbaseFactoryBean.afterPropertiesSet();
|
||||
return couchbaseFactoryBean.getObject();
|
||||
}
|
||||
|
||||
public void close(@Disposes CouchbaseClient couchbaseClient) {
|
||||
couchbaseClient.shutdown();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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 org.springframework.data.couchbase.core.CouchbaseOperations;
|
||||
import org.springframework.data.couchbase.core.CouchbaseTemplate;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
|
||||
/**
|
||||
* Produces a {@link CouchbaseOperations} instance for test usage.
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class CouchbaseOperationsProducer {
|
||||
|
||||
@Produces
|
||||
public CouchbaseOperations createCouchbaseOperations(CouchbaseClient couchbaseClient) throws Exception {
|
||||
|
||||
CouchbaseTemplate couchbaseTemplate = new CouchbaseTemplate(couchbaseClient);
|
||||
|
||||
return couchbaseTemplate;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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 org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.couchbase.core.mapping.Field;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class Person {
|
||||
|
||||
@Id private String id;
|
||||
|
||||
@Field private String name;
|
||||
|
||||
public Person() {}
|
||||
|
||||
public Person(String id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
6
src/test/resources/META-INF/beans.xml
Normal file
6
src/test/resources/META-INF/beans.xml
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://java.sun.com/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
|
||||
</beans>
|
||||
|
||||
Reference in New Issue
Block a user