DATACOUCH-189 - Test and document how to change repo implementation

Added an integration test for custom method customization.
Moved the integration test for base class customization.
Put the samples in the documentation in a new "Changing Repository Behaviour" section.
This commit is contained in:
Simon Baslé
2016-01-15 18:16:00 +01:00
parent c589a13699
commit 15d0d27d71
9 changed files with 334 additions and 8 deletions

View File

@@ -14,9 +14,10 @@
* limitations under the License.
*/
package org.springframework.data.couchbase.repository.base;
package org.springframework.data.couchbase.repository.extending.base;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -36,9 +37,11 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.data.annotation.Id;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.core.CouchbaseOperations;
import org.springframework.data.couchbase.repository.base.impl.MyRepository;
import org.springframework.data.couchbase.repository.base.impl.MyRepositoryImpl;
import org.springframework.data.couchbase.repository.User;
import org.springframework.data.couchbase.repository.UserRepository;
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
import org.springframework.data.couchbase.repository.extending.base.impl.MyRepository;
import org.springframework.data.couchbase.repository.extending.base.impl.MyRepositoryImpl;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -67,10 +70,17 @@ public class RepositoryBaseTest {
@Autowired
ItemRepository repositoryA;
@Autowired
UserRepository repositoryB;
public interface ItemRepository extends MyRepository<Item, String> {
//
}
public interface UserRepository extends MyRepository<User, String> {
//
}
@Configuration
@EnableCouchbaseRepositories(considerNestedRepositories = true, repositoryBaseClass = MyRepositoryImpl.class)
static class Config extends AbstractCouchbaseConfiguration {
@@ -99,9 +109,13 @@ public class RepositoryBaseTest {
@Test
public void testRepositoryBaseIsChanged() {
assertNotNull(repositoryA);
assertNotNull(repositoryB);
assertEquals(4, repositoryA.sharedCustomMethod("toto"));
assertEquals(4000, repositoryA.sharedCustomMethod("anna"));
assertEquals(repositoryA.sharedCustomMethod("sameInput"), repositoryB.sharedCustomMethod("sameInput"));
assertEquals(repositoryA.sharedCustomMethod("anna"), repositoryB.sharedCustomMethod("anna"));
}
private static class Item {

View File

@@ -1,4 +1,4 @@
package org.springframework.data.couchbase.repository.base.impl;
package org.springframework.data.couchbase.repository.extending.base.impl;
import java.io.Serializable;

View File

@@ -1,8 +1,9 @@
package org.springframework.data.couchbase.repository.base.impl;
package org.springframework.data.couchbase.repository.extending.base.impl;
import java.io.Serializable;
import org.springframework.data.couchbase.core.CouchbaseOperations;
import org.springframework.data.couchbase.repository.extending.base.impl.MyRepository;
import org.springframework.data.couchbase.repository.query.CouchbaseEntityInformation;
import org.springframework.data.couchbase.repository.support.N1qlCouchbaseRepository;

View File

@@ -0,0 +1,15 @@
package org.springframework.data.couchbase.repository.extending.method;
import org.springframework.data.annotation.Id;
class MyItem {
@Id
public final String id;
public final String value;
public MyItem(String id, String value) {
this.id = id;
this.value = value;
}
}

View File

@@ -0,0 +1,10 @@
package org.springframework.data.couchbase.repository.extending.method;
import org.springframework.data.couchbase.core.query.N1qlPrimaryIndexed;
import org.springframework.data.couchbase.core.query.ViewIndexed;
import org.springframework.data.repository.CrudRepository;
@N1qlPrimaryIndexed
@ViewIndexed(designDoc = "myItem", viewName = "all")
public interface MyRepository extends CrudRepository<MyItem, String>, MyRepositoryCustom {
}

View File

@@ -0,0 +1,7 @@
package org.springframework.data.couchbase.repository.extending.method;
public interface MyRepositoryCustom {
long customCountItems();
}

View File

@@ -0,0 +1,58 @@
package org.springframework.data.couchbase.repository.extending.method;
import java.util.List;
import com.couchbase.client.java.query.N1qlParams;
import com.couchbase.client.java.query.N1qlQuery;
import com.couchbase.client.java.query.Statement;
import com.couchbase.client.java.query.consistency.ScanConsistency;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.couchbase.core.CouchbaseOperations;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
import org.springframework.data.couchbase.repository.Item;
import org.springframework.data.couchbase.repository.config.RepositoryOperationsMapping;
import org.springframework.data.couchbase.repository.query.CouchbaseEntityInformation;
import org.springframework.data.couchbase.repository.query.CountFragment;
import org.springframework.data.couchbase.repository.query.support.N1qlUtils;
import org.springframework.data.couchbase.repository.support.MappingCouchbaseEntityInformation;
public class MyRepositoryImpl implements MyRepositoryCustom {
@Autowired
RepositoryOperationsMapping templateProvider;
@Override
public long customCountItems() {
CouchbaseOperations template = templateProvider.resolve(MyRepository.class, Item.class);
CouchbasePersistentEntity<Object> itemPersistenceEntity = (CouchbasePersistentEntity<Object>)
template.getConverter()
.getMappingContext()
.getPersistentEntity(MyItem.class);
CouchbaseEntityInformation<? extends Object, String> itemEntityInformation =
new MappingCouchbaseEntityInformation<Object, String>(itemPersistenceEntity);
Statement countStatement = N1qlUtils.createCountQueryForEntity(
template.getCouchbaseBucket().name(),
template.getConverter(),
itemEntityInformation);
ScanConsistency consistency = template.getDefaultConsistency().n1qlConsistency();
N1qlParams queryParams = N1qlParams.build().consistency(consistency);
N1qlQuery query = N1qlQuery.simple(countStatement, queryParams);
List<CountFragment> countFragments = template.findByN1QLProjection(query, CountFragment.class);
if (countFragments == null || countFragments.isEmpty()) {
return 0L;
} else {
return countFragments.get(0).count * -1L;
}
}
public long count() {
return 100;
}
}

View File

@@ -0,0 +1,102 @@
/*
* 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.extending.method;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import java.util.Arrays;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.core.query.Consistency;
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* This tests custom repository methods.
*
* @author Simon Baslé
*/
@SuppressWarnings("SpringJavaAutowiringInspection")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class RepositoryCustomMethodTest {
@Autowired
MyRepository repository;
@Configuration
@EnableCouchbaseRepositories
static class Config extends AbstractCouchbaseConfiguration {
@Override
protected List<String> getBootstrapHosts() {
return Arrays.asList("127.0.0.1");
}
@Override
protected String getBucketName() {
return "default";
}
@Override
protected String getBucketPassword() {
return "";
}
@Override
protected Consistency getDefaultConsistency() {
return Consistency.STRONGLY_CONSISTENT;
}
}
private static final String KEY = "customMethodTestItem";
@Before
public void initData() {
try { repository.delete(KEY); } catch (Exception e) { }
repository.save(new MyItem(KEY, "new item for custom count"));
}
@After
public void clearData() {
repository.delete(KEY);
}
@Test
public void testRepositoryCustomMethodIsWeavedIn() {
long customCount = repository.customCountItems();
assertEquals(-1L, customCount);
}
@Test
public void testRepositoryCrudMethodIsReplaced() {
long count = repository.count();
assertEquals(100L, count);
}
}