DATACOUCH-16 - Allow View customization through @View annotations.
An initial first attempt at allowing for basic View customization using the @View annotation. For now, it does not support "dynamic" finders, such as findByUsername, but I'm sure it will come shortly aftewards. Also added in hamcrest library to gradually transistion deprecated JUnit methods to a better assertion library :-) -=david=-
This commit is contained in:
committed by
Michael Nitschinger
parent
8ade60937a
commit
48385c965a
@@ -35,20 +35,23 @@ public class CouchbaseRepositoryViewListener extends DependencyInjectionTestExec
|
||||
createAndWaitForDesignDocs(client);
|
||||
}
|
||||
|
||||
private void populateTestData(CouchbaseClient client) {
|
||||
private void populateTestData(final CouchbaseClient client) {
|
||||
CouchbaseTemplate template = new CouchbaseTemplate(client);
|
||||
|
||||
for(int i=0;i < 100; i++) {
|
||||
User u = new User("testuser-" + i, "uname" + i);
|
||||
template.save(u);
|
||||
for (int i = 0; i < 100; i++) {
|
||||
template.save(new User("testuser-" + i, "uname-" + i));
|
||||
}
|
||||
}
|
||||
|
||||
private void createAndWaitForDesignDocs(CouchbaseClient client) {
|
||||
private void createAndWaitForDesignDocs(final CouchbaseClient client) {
|
||||
DesignDocument designDoc = new DesignDocument("user");
|
||||
String mapFunction = "function (doc, meta) { if(doc._class == "
|
||||
+ "\"org.springframework.data.couchbase.repository.User\") { emit(null, null); } }";
|
||||
designDoc.setView(new ViewDesign("all", mapFunction, "_count"));
|
||||
String mapFunction = "function (doc, meta) { if(doc._class == \"org.springframework.data.couchbase.repository.User\") { emit(null, null); } }";
|
||||
designDoc.setView(new ViewDesign("customFindAllView", mapFunction, "_count"));
|
||||
|
||||
client.createDesignDoc(designDoc);
|
||||
|
||||
designDoc = new DesignDocument("userCustom");
|
||||
designDoc.setView(new ViewDesign("customCountView", mapFunction, "_count"));
|
||||
|
||||
client.createDesignDoc(designDoc);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import com.couchbase.client.protocol.views.Query;
|
||||
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.core.CouchbaseTemplate;
|
||||
import org.springframework.data.couchbase.repository.support.CouchbaseRepositoryFactory;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static com.couchbase.client.protocol.views.Stale.FALSE;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
/**
|
||||
* @author David Harrigan
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
@TestExecutionListeners(CouchbaseRepositoryViewListener.class)
|
||||
public class CouchbaseRepositoryViewTests {
|
||||
|
||||
@Autowired
|
||||
private CouchbaseClient client;
|
||||
|
||||
@Autowired
|
||||
private CouchbaseTemplate template;
|
||||
|
||||
private CustomUserRepository repository;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
repository = new CouchbaseRepositoryFactory(template).getRepository(CustomUserRepository.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindAllWithCustomView() {
|
||||
client.query(client.getView("user", "customFindAllView"), new Query().setStale(FALSE));
|
||||
Iterable<User> allUsers = repository.findAll();
|
||||
int i = 0;
|
||||
for (final User allUser : allUsers) {
|
||||
i++;
|
||||
}
|
||||
assertThat(i, is(100));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCountWithCustomView() {
|
||||
client.query(client.getView("userCustom", "customCountView"), new Query().setStale(FALSE));
|
||||
final long value = repository.count();
|
||||
assertThat(value, is(100L));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.springframework.data.couchbase.core.view.View;
|
||||
|
||||
/**
|
||||
* @author David Harrigan
|
||||
*/
|
||||
public interface CustomUserRepository extends CouchbaseRepository<User, String> {
|
||||
|
||||
@Override
|
||||
@View(designDocument = "user", viewName = "customFindAllView")
|
||||
Iterable<User> findAll();
|
||||
|
||||
@Override
|
||||
@View(designDocument = "userCustom", viewName = "customCountView")
|
||||
long count();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import com.couchbase.client.protocol.views.DesignDocument;
|
||||
import com.couchbase.client.protocol.views.ViewDesign;
|
||||
import org.springframework.data.couchbase.core.CouchbaseTemplate;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
|
||||
/**
|
||||
* @author Michael Nitschinger
|
||||
*/
|
||||
public class SimpleCouchbaseRepositoryListener extends DependencyInjectionTestExecutionListener {
|
||||
|
||||
@Override
|
||||
public void beforeTestClass(final TestContext testContext) throws Exception {
|
||||
CouchbaseClient client = (CouchbaseClient) testContext.getApplicationContext().getBean("couchbaseClient");
|
||||
populateTestData(client);
|
||||
createAndWaitForDesignDocs(client);
|
||||
}
|
||||
|
||||
private void populateTestData(CouchbaseClient client) {
|
||||
CouchbaseTemplate template = new CouchbaseTemplate(client);
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
User u = new User("testuser-" + i, "uname-" + i);
|
||||
template.save(u);
|
||||
}
|
||||
}
|
||||
|
||||
private void createAndWaitForDesignDocs(CouchbaseClient client) {
|
||||
DesignDocument designDoc = new DesignDocument("user");
|
||||
String mapFunction = "function (doc, meta) { if(doc._class == \"org.springframework.data.couchbase.repository.User\") { emit(null, null); } }";
|
||||
designDoc.setView(new ViewDesign("all", mapFunction, "_count"));
|
||||
client.createDesignDoc(designDoc);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -38,14 +38,12 @@ import static org.junit.Assert.*;
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
@TestExecutionListeners(CouchbaseRepositoryViewListener.class)
|
||||
@TestExecutionListeners(SimpleCouchbaseRepositoryListener.class)
|
||||
public class SimpleCouchbaseRepositoryTests {
|
||||
|
||||
|
||||
@Autowired
|
||||
private CouchbaseClient client;
|
||||
|
||||
|
||||
@Autowired
|
||||
private CouchbaseTemplate template;
|
||||
|
||||
@@ -75,6 +73,9 @@ public class SimpleCouchbaseRepositoryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
/**
|
||||
* This test uses/assumes a default viewName called "all" that is configured on Couchbase.
|
||||
*/
|
||||
public void shouldFindAll() {
|
||||
// do a non-stale query to populate data for testing.
|
||||
client.query(client.getView("user", "all"), new Query().setStale(Stale.FALSE));
|
||||
|
||||
@@ -19,6 +19,6 @@ package org.springframework.data.couchbase.repository;
|
||||
/**
|
||||
* @author Michael Nitschinger
|
||||
*/
|
||||
public interface UserRepository extends CouchbaseRepository<User, String>{
|
||||
public interface UserRepository extends CouchbaseRepository<User, String> {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user