added paging and sorting
This commit is contained in:
@@ -21,7 +21,7 @@ import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.data.graph.neo4j.rest.support.RestTestBase;
|
||||
import org.springframework.data.graph.neo4j.support.GraphRepositoryTest;
|
||||
import org.springframework.data.graph.neo4j.support.FinderTest;
|
||||
import org.springframework.test.context.CleanContextCacheTestExecutionListener;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
@@ -37,7 +37,7 @@ import org.springframework.test.context.transaction.TransactionalTestExecutionLi
|
||||
@ContextConfiguration(locations = {"classpath:org/springframework/data/graph/neo4j/support/Neo4jGraphPersistenceTest-context.xml",
|
||||
"classpath:RestTest-context.xml"})
|
||||
@TestExecutionListeners({CleanContextCacheTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class})
|
||||
public class RestFinderTest extends GraphRepositoryTest {
|
||||
public class RestFinderTest extends FinderTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void startDb() throws Exception {
|
||||
|
||||
@@ -16,6 +16,10 @@
|
||||
|
||||
package org.springframework.data.graph.neo4j.repository;
|
||||
|
||||
import org.neo4j.helpers.collection.IteratorUtil;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.graph.annotation.GraphQuery;
|
||||
import org.springframework.data.graph.annotation.NodeEntity;
|
||||
import org.springframework.data.graph.annotation.RelationshipEntity;
|
||||
@@ -28,13 +32,13 @@ import org.springframework.data.repository.core.EntityInformation;
|
||||
import org.springframework.data.repository.core.NamedQueries;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy;
|
||||
import org.springframework.data.repository.query.QueryMethod;
|
||||
import org.springframework.data.repository.query.RepositoryQuery;
|
||||
import org.springframework.data.repository.query.*;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
|
||||
@@ -144,15 +148,54 @@ public class GraphRepositoryFactory extends RepositoryFactorySupport {
|
||||
return method.getReturnType();
|
||||
}
|
||||
|
||||
private String prepareQuery(Object[] parameters) {
|
||||
Object[] resolvedParameters=resolveParameters(parameters);
|
||||
return String.format(query, (Object[]) resolvedParameters);
|
||||
private String prepareQuery(Object[] args) {
|
||||
final Parameters parameters = getParameters();
|
||||
Object[] resolvedParameters = resolveParameters(args,parameters.getBindableParameters());
|
||||
String baseQuery = String.format(query, (Object[]) resolvedParameters);
|
||||
if (parameters.hasSortParameter()) {
|
||||
baseQuery = addSorting(baseQuery, (Sort) args[parameters.getSortIndex()]);
|
||||
}
|
||||
if (parameters.hasPageableParameter()) {
|
||||
final Pageable pageable = getPageable(args);
|
||||
baseQuery = addSorting(baseQuery, pageable.getSort());
|
||||
baseQuery = addPaging(baseQuery, pageable);
|
||||
}
|
||||
return baseQuery;
|
||||
}
|
||||
|
||||
private Object[] resolveParameters(Object[] parameters) {
|
||||
final Object[] result = new Object[parameters.length];
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
result[i] = resolveParameter(parameters[i]);
|
||||
private Pageable getPageable(Object[] args) {
|
||||
Parameters parameters = getParameters();
|
||||
if (parameters.hasPageableParameter()) return (Pageable) args[parameters.getPageableIndex()];
|
||||
return null;
|
||||
}
|
||||
|
||||
private String addPaging(String baseQuery, Pageable pageable) {
|
||||
if (pageable==null) return baseQuery;
|
||||
return baseQuery + " skip "+pageable.getOffset() + " limit " + pageable.getPageSize();
|
||||
}
|
||||
|
||||
private String addSorting(String baseQuery, Sort sort) {
|
||||
if (sort==null) return baseQuery; // || sort.isEmpty()
|
||||
final String sortOrder = getSortOrder(sort);
|
||||
if (sortOrder.isEmpty()) return baseQuery;
|
||||
return baseQuery + " order by " + sortOrder;
|
||||
}
|
||||
|
||||
private String getSortOrder(Sort sort) {
|
||||
String result = "";
|
||||
for (Sort.Order order : sort) {
|
||||
result += order.getProperty() + " " + order.getDirection();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private Object[] resolveParameters(Object[] parameters, Parameters bindableParameters) {
|
||||
final int paramCount = bindableParameters.getNumberOfParameters();
|
||||
final Object[] result = new Object[paramCount];
|
||||
for (int i = 0; i < paramCount; i++) {
|
||||
final Parameter parameter = bindableParameters.getParameter(i);
|
||||
final Object value = parameters[parameter.getIndex()];
|
||||
result[i] = resolveParameter(value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -207,22 +250,32 @@ public class GraphRepositoryFactory extends RepositoryFactorySupport {
|
||||
@Override
|
||||
public Object execute(Object[] parameters) {
|
||||
final String queryString = queryMethod.prepareQuery(parameters);
|
||||
return dispatchQuery(queryString);
|
||||
return dispatchQuery(queryString,queryMethod.getPageable(parameters));
|
||||
}
|
||||
|
||||
private Object dispatchQuery(String queryString) {
|
||||
private Object dispatchQuery(String queryString, Pageable pageable) {
|
||||
final QueryMethod.Type queryResultType = queryMethod.getType();
|
||||
if (queryResultType== QueryMethod.Type.PAGING) {
|
||||
return queryPaged(queryString,pageable);
|
||||
}
|
||||
if (iterableResult) {
|
||||
if (compoundType.isAssignableFrom(Map.class)) return queryExecutor.query(queryString);
|
||||
return queryExecutor.query(queryString, queryMethod.getCompoundType());
|
||||
}
|
||||
switch (queryMethod.getType()) {
|
||||
case SINGLE_ENTITY: return queryExecutor.queryForObject(queryString, queryMethod.getReturnType());
|
||||
case COLLECTION:
|
||||
case PAGING:
|
||||
return queryExecutor.query(queryString, queryMethod.getCompoundType());
|
||||
default:
|
||||
return queryExecutor.query(queryString);
|
||||
}
|
||||
return queryExecutor.queryForObject(queryString, queryMethod.getReturnType());
|
||||
}
|
||||
|
||||
private Object queryPaged(String queryString, Pageable pageable) {
|
||||
final Iterable<?> result = queryExecutor.query(queryString, queryMethod.getCompoundType());
|
||||
return createPage(result, pageable);
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked"})
|
||||
private Object createPage(Iterable<?> result, Pageable pageable) {
|
||||
final List resultList = IteratorUtil.addToCollection(result, new ArrayList());
|
||||
if (pageable==null) return new PageImpl(resultList);
|
||||
final int currentTotal = pageable.getOffset() + pageable.getPageSize();
|
||||
return new PageImpl(resultList, pageable, currentTotal);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -38,12 +38,15 @@ public class GenericTypeExtractor {
|
||||
|
||||
public static Class<?> resolveConcreteType(Class<?> type, final Type genericType) {
|
||||
if (Iterable.class.isAssignableFrom(type) || Page.class.isAssignableFrom(type)) {
|
||||
if (genericType instanceof ParameterizedType) {
|
||||
ParameterizedType returnType = (ParameterizedType) genericType;
|
||||
Type componentType = returnType.getActualTypeArguments()[0];
|
||||
|
||||
ParameterizedType returnType = (ParameterizedType) genericType;
|
||||
Type componentType = returnType.getActualTypeArguments()[0];
|
||||
|
||||
return componentType instanceof ParameterizedType ? (Class<?>) ((ParameterizedType) componentType).getRawType()
|
||||
: (Class<?>) componentType;
|
||||
return componentType instanceof ParameterizedType ? (Class<?>) ((ParameterizedType) componentType).getRawType()
|
||||
: (Class<?>) componentType;
|
||||
} else {
|
||||
return Object.class;
|
||||
}
|
||||
}
|
||||
|
||||
return type;
|
||||
|
||||
@@ -16,11 +16,13 @@
|
||||
|
||||
package org.springframework.data.graph.neo4j;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.graph.annotation.GraphQuery;
|
||||
import org.springframework.data.graph.neo4j.repository.NamedIndexRepository;
|
||||
import org.springframework.data.graph.neo4j.repository.GraphRepository;
|
||||
import org.springframework.data.graph.neo4j.repository.NamedIndexRepository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -39,4 +41,9 @@ public interface PersonRepository extends GraphRepository<Person>, NamedIndexRep
|
||||
Person findBoss(Person person);
|
||||
|
||||
Group findTeam(Person person);
|
||||
|
||||
@GraphQuery("start team=(%d) match (team)-[:persons]->(member) return member")
|
||||
Page<Person> findAllTeamMembersPaged(Pageable page, Group team);
|
||||
@GraphQuery("start team=(%d) match (team)-[:persons]->(member) return member")
|
||||
Iterable<Person> findAllTeamMembersSorted(Group team, Sort sort);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,205 @@
|
||||
/**
|
||||
* Copyright 2011 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.graph.neo4j.support;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.neo4j.helpers.collection.IteratorUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.graph.neo4j.*;
|
||||
import org.springframework.data.graph.neo4j.support.node.Neo4jHelper;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.transaction.BeforeTransaction;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.internal.matchers.IsCollectionContaining.hasItems;
|
||||
import static org.neo4j.helpers.collection.IteratorUtil.asCollection;
|
||||
import static org.springframework.data.graph.neo4j.Person.persistedPerson;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
//@ContextConfiguration(locations = {"classpath:repository-namespace-config-context.xml"})
|
||||
@ContextConfiguration(locations = {"classpath:org/springframework/data/graph/neo4j/support/Neo4jGraphPersistenceTest-context.xml"})
|
||||
public class FinderTest {
|
||||
|
||||
protected final Log log = LogFactory.getLog(getClass());
|
||||
|
||||
@Autowired
|
||||
private GraphDatabaseContext graphDatabaseContext;
|
||||
|
||||
@Autowired
|
||||
private PersonRepository personRepository;
|
||||
@Autowired
|
||||
private GroupRepository groupRepository;
|
||||
@Autowired
|
||||
private FriendshipRepository friendshipRepository;
|
||||
|
||||
@BeforeTransaction
|
||||
public void cleanDb() {
|
||||
Neo4jHelper.cleanDb(graphDatabaseContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFinderFindAll() {
|
||||
Person p1 = persistedPerson("Michael", 35);
|
||||
Person p2 = persistedPerson("David", 25);
|
||||
Iterable<Person> allPersons = personRepository.findAll();
|
||||
assertThat(asCollection(allPersons), hasItems(p1, p2));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFindIterableOfPersonWithQueryAnnotation() {
|
||||
final TestTeam testTeam = new TestTeam();
|
||||
testTeam.createSDGTeam();
|
||||
Iterable<Person> teamMembers = personRepository.findAllTeamMembers(testTeam.sdg);
|
||||
assertThat(asCollection(teamMembers), hasItems(testTeam.michael,testTeam.david,testTeam.emil));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFindPersonWithQueryAnnotation() {
|
||||
final TestTeam testTeam = new TestTeam();
|
||||
testTeam.createSDGTeam();
|
||||
Person boss = personRepository.findBoss(testTeam.michael);
|
||||
assertThat(boss, is(testTeam.emil));
|
||||
}
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFindIterableMapsWithQueryAnnotation() {
|
||||
final TestTeam testTeam = new TestTeam();
|
||||
testTeam.createSDGTeam();
|
||||
Iterable<Map<String,Object>> teamMembers = personRepository.findAllTeamMemberData(testTeam.sdg);
|
||||
assertThat(asCollection(teamMembers), hasItems(testTeam.simpleRowFor(testTeam.michael,"member"),testTeam.simpleRowFor(testTeam.david,"member"),testTeam.simpleRowFor(testTeam.emil,"member")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFindByNamedQuery() {
|
||||
final TestTeam testTeam = new TestTeam();
|
||||
testTeam.createSDGTeam();
|
||||
Group team = personRepository.findTeam(testTeam.michael);
|
||||
assertThat(team, is(testTeam.sdg));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testSaveManyPeople() {
|
||||
Person p1 = new Person("Michael", 35);
|
||||
Person p2 = new Person("David", 25);
|
||||
personRepository.save(asList(p1,p2));
|
||||
assertEquals("persisted person 1",true,p1.hasPersistentState());
|
||||
assertEquals("persisted person 2",true,p2.hasPersistentState());
|
||||
assertThat(asCollection(personRepository.findAll()), hasItems(p2, p1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testSavePerson() {
|
||||
Person p1 = new Person("Michael", 35);
|
||||
personRepository.save(p1);
|
||||
assertEquals("persisted person",true,p1.hasPersistentState());
|
||||
assertThat(personRepository.findOne(p1.getId()), is(p1));
|
||||
}
|
||||
@Test
|
||||
public void testDeletePerson() {
|
||||
Person p1 = persistedPerson("Michael", 35);
|
||||
personRepository.delete(p1);
|
||||
assertEquals("people deleted", false, personRepository.findAll().iterator().hasNext());
|
||||
}
|
||||
@Test
|
||||
public void testDeletePeople() {
|
||||
Person p1 = persistedPerson("Michael", 35);
|
||||
Person p2 = persistedPerson("David", 26);
|
||||
personRepository.delete(asList(p1,p2));
|
||||
assertEquals("people deleted", false, personRepository.findAll().iterator().hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFindRelationshipEntity() {
|
||||
Person p1 = persistedPerson("Michael", 35);
|
||||
Person p2 = persistedPerson("David", 27);
|
||||
Friendship friendship = p1.knows(p2);
|
||||
assertEquals("Wrong friendship count.", 1L, (long) friendshipRepository.count());
|
||||
assertEquals(friendship, friendshipRepository.findOne(friendship.getRelationshipId()));
|
||||
assertEquals("Did not find friendship.", Collections.singleton(friendship), new HashSet<Friendship>(IteratorUtil.asCollection(friendshipRepository.findAll())));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFinderFindById() {
|
||||
Person p = persistedPerson("Michael", 35);
|
||||
Person pById = personRepository.findOne(p.getNodeId());
|
||||
assertEquals(p, pById);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testExists() {
|
||||
Person p = persistedPerson("Michael", 35);
|
||||
boolean found = personRepository.exists(p.getNodeId());
|
||||
assertTrue("Found persisted entity", found);
|
||||
}
|
||||
@Test
|
||||
@Transactional
|
||||
public void testDoesntExist() {
|
||||
boolean found = personRepository.exists(Long.MAX_VALUE-1);
|
||||
assertFalse("Non existend id isn't foundpo ", found);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFinderFindByIdNonexistent() {
|
||||
Person p = persistedPerson("Michael", 35);
|
||||
Person p2 = personRepository.findOne(589736218L);
|
||||
Assert.assertNull(p2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFinderCount() {
|
||||
assertEquals(0L, personRepository.count());
|
||||
Person p = persistedPerson("Michael", 35);
|
||||
assertEquals(1L, personRepository.count());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFindAllOnGroup() {
|
||||
log.debug("FindAllOnGroup start");
|
||||
Group g = new Group().persist();
|
||||
g.setName("test");
|
||||
Group g2 = new Group().persist();
|
||||
g.setName("test");
|
||||
Collection<Group> groups = IteratorUtil.addToCollection(groupRepository.findAll().iterator(), new HashSet<Group>());
|
||||
Assert.assertEquals(2, groups.size());
|
||||
log.debug("FindAllOnGroup done");
|
||||
}
|
||||
}
|
||||
@@ -18,12 +18,16 @@ package org.springframework.data.graph.neo4j.support;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.neo4j.helpers.collection.IteratorUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.graph.neo4j.*;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.graph.neo4j.Group;
|
||||
import org.springframework.data.graph.neo4j.Person;
|
||||
import org.springframework.data.graph.neo4j.PersonRepository;
|
||||
import org.springframework.data.graph.neo4j.support.node.Neo4jHelper;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@@ -31,20 +35,16 @@ import org.springframework.test.context.transaction.BeforeTransaction;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.internal.matchers.IsCollectionContaining.hasItems;
|
||||
import static org.neo4j.helpers.collection.IteratorUtil.asCollection;
|
||||
import static org.springframework.data.graph.neo4j.Person.persistedPerson;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = {"classpath:repository-namespace-config-context.xml"})
|
||||
//@ContextConfiguration(locations = {"classpath:org/springframework/data/graph/neo4j/support/Neo4jGraphPersistenceTest-context.xml"})
|
||||
public class GraphRepositoryTest {
|
||||
|
||||
protected final Log log = LogFactory.getLog(getClass());
|
||||
@@ -54,152 +54,82 @@ public class GraphRepositoryTest {
|
||||
|
||||
@Autowired
|
||||
private PersonRepository personRepository;
|
||||
@Autowired
|
||||
private GroupRepository groupRepository;
|
||||
@Autowired
|
||||
private FriendshipRepository friendshipRepository;
|
||||
private TestTeam testTeam;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
testTeam = new TestTeam();
|
||||
testTeam.createSDGTeam();
|
||||
}
|
||||
|
||||
@BeforeTransaction
|
||||
public void cleanDb() {
|
||||
Neo4jHelper.cleanDb(graphDatabaseContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFinderFindAll() {
|
||||
Person p1 = persistedPerson("Michael", 35);
|
||||
Person p2 = persistedPerson("David", 25);
|
||||
Iterable<Person> allPersons = personRepository.findAll();
|
||||
assertThat(asCollection(allPersons), hasItems(p1, p2));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFindIterableOfPersonWithQueryAnnotation() {
|
||||
final TestTeam testTeam = new TestTeam();
|
||||
testTeam.createSDGTeam();
|
||||
Iterable<Person> teamMembers = personRepository.findAllTeamMembers(testTeam.sdg);
|
||||
assertThat(asCollection(teamMembers), hasItems(testTeam.michael,testTeam.david,testTeam.emil));
|
||||
assertThat(asCollection(teamMembers), hasItems(testTeam.michael, testTeam.david, testTeam.emil));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFindPersonWithQueryAnnotation() {
|
||||
final TestTeam testTeam = new TestTeam();
|
||||
testTeam.createSDGTeam();
|
||||
Person boss = personRepository.findBoss(testTeam.michael);
|
||||
assertThat(boss, is(testTeam.emil));
|
||||
}
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFindIterableMapsWithQueryAnnotation() {
|
||||
final TestTeam testTeam = new TestTeam();
|
||||
testTeam.createSDGTeam();
|
||||
Iterable<Map<String,Object>> teamMembers = personRepository.findAllTeamMemberData(testTeam.sdg);
|
||||
assertThat(asCollection(teamMembers), hasItems(testTeam.simpleRowFor(testTeam.michael,"member"),testTeam.simpleRowFor(testTeam.david,"member"),testTeam.simpleRowFor(testTeam.emil,"member")));
|
||||
assertThat(asCollection(teamMembers), hasItems(testTeam.simpleRowFor(testTeam.michael, "member"), testTeam.simpleRowFor(testTeam.david, "member"), testTeam.simpleRowFor(testTeam.emil, "member")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFindPaged() {
|
||||
final PageRequest page = new PageRequest(0, 1, Sort.Direction.ASC, "member.name");
|
||||
Page<Person> teamMemberPage1 = personRepository.findAllTeamMembersPaged(page, testTeam.sdg);
|
||||
assertThat(teamMemberPage1, is((Iterable) asList(testTeam.david)));
|
||||
}
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFindPagedDescending() {
|
||||
final PageRequest page = new PageRequest(0, 2, Sort.Direction.DESC, "member.name");
|
||||
Page<Person> teamMemberPage1 = personRepository.findAllTeamMembersPaged(page, testTeam.sdg);
|
||||
assertThat(teamMemberPage1, is((Iterable) asList(testTeam.michael, testTeam.emil)));
|
||||
assertThat(teamMemberPage1.isFirstPage(), is(true));
|
||||
}
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFindPagedNull() {
|
||||
Page<Person> teamMemberPage1 = personRepository.findAllTeamMembersPaged(null, testTeam.sdg);
|
||||
assertThat(teamMemberPage1, is((Iterable) asList(testTeam.michael, testTeam.emil)));
|
||||
assertThat(teamMemberPage1.isFirstPage(), is(true));
|
||||
assertThat(teamMemberPage1.isLastPage(), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFindSortedDescending() {
|
||||
final Sort sort = new Sort(Sort.Direction.DESC, "member.name");
|
||||
Iterable<Person> teamMembers = personRepository.findAllTeamMembersSorted(testTeam.sdg, sort);
|
||||
assertThat(teamMembers, is((Iterable)asList(testTeam.michael, testTeam.emil, testTeam.david)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFindSortedNull() {
|
||||
Iterable<Person> teamMembers = personRepository.findAllTeamMembersSorted(testTeam.sdg, null);
|
||||
assertThat(teamMembers, hasItems(testTeam.michael, testTeam.emil, testTeam.david));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFindByNamedQuery() {
|
||||
final TestTeam testTeam = new TestTeam();
|
||||
testTeam.createSDGTeam();
|
||||
Group team = personRepository.findTeam(testTeam.michael);
|
||||
assertThat(team, is(testTeam.sdg));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testSaveManyPeople() {
|
||||
Person p1 = new Person("Michael", 35);
|
||||
Person p2 = new Person("David", 25);
|
||||
personRepository.save(asList(p1,p2));
|
||||
assertEquals("persisted person 1",true,p1.hasPersistentState());
|
||||
assertEquals("persisted person 2",true,p2.hasPersistentState());
|
||||
assertThat(asCollection(personRepository.findAll()), hasItems(p2, p1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testSavePerson() {
|
||||
Person p1 = new Person("Michael", 35);
|
||||
personRepository.save(p1);
|
||||
assertEquals("persisted person",true,p1.hasPersistentState());
|
||||
assertThat(personRepository.findOne(p1.getId()), is(p1));
|
||||
}
|
||||
@Test
|
||||
public void testDeletePerson() {
|
||||
Person p1 = persistedPerson("Michael", 35);
|
||||
personRepository.delete(p1);
|
||||
assertEquals("people deleted", false, personRepository.findAll().iterator().hasNext());
|
||||
}
|
||||
@Test
|
||||
public void testDeletePeople() {
|
||||
Person p1 = persistedPerson("Michael", 35);
|
||||
Person p2 = persistedPerson("David", 26);
|
||||
personRepository.delete(asList(p1,p2));
|
||||
assertEquals("people deleted", false, personRepository.findAll().iterator().hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFindRelationshipEntity() {
|
||||
Person p1 = persistedPerson("Michael", 35);
|
||||
Person p2 = persistedPerson("David", 27);
|
||||
Friendship friendship = p1.knows(p2);
|
||||
assertEquals("Wrong friendship count.", 1L, (long) friendshipRepository.count());
|
||||
assertEquals(friendship, friendshipRepository.findOne(friendship.getRelationshipId()));
|
||||
assertEquals("Did not find friendship.", Collections.singleton(friendship), new HashSet<Friendship>(IteratorUtil.asCollection(friendshipRepository.findAll())));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFinderFindById() {
|
||||
Person p = persistedPerson("Michael", 35);
|
||||
Person pById = personRepository.findOne(p.getNodeId());
|
||||
assertEquals(p, pById);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testExists() {
|
||||
Person p = persistedPerson("Michael", 35);
|
||||
boolean found = personRepository.exists(p.getNodeId());
|
||||
assertTrue("Found persisted entity", found);
|
||||
}
|
||||
@Test
|
||||
@Transactional
|
||||
public void testDoesntExist() {
|
||||
boolean found = personRepository.exists(Long.MAX_VALUE-1);
|
||||
assertFalse("Non existend id isn't foundpo ", found);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFinderFindByIdNonexistent() {
|
||||
Person p = persistedPerson("Michael", 35);
|
||||
Person p2 = personRepository.findOne(589736218L);
|
||||
Assert.assertNull(p2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFinderCount() {
|
||||
assertEquals(0L, personRepository.count());
|
||||
Person p = persistedPerson("Michael", 35);
|
||||
assertEquals(1L, personRepository.count());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testFindAllOnGroup() {
|
||||
log.debug("FindAllOnGroup start");
|
||||
Group g = new Group().persist();
|
||||
g.setName("test");
|
||||
Group g2 = new Group().persist();
|
||||
g.setName("test");
|
||||
Collection<Group> groups = IteratorUtil.addToCollection(groupRepository.findAll().iterator(), new HashSet<Group>());
|
||||
Assert.assertEquals(2, groups.size());
|
||||
log.debug("FindAllOnGroup done");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,6 +147,13 @@
|
||||
<bean id="personRepository" class="org.springframework.data.graph.neo4j.repository.GraphRepositoryFactoryBean">
|
||||
<property name="repositoryInterface" value="org.springframework.data.graph.neo4j.PersonRepository" />
|
||||
<property name="graphDatabaseContext" ref="graphDatabaseContext"/>
|
||||
<property name="namedQueries">
|
||||
<bean class="org.springframework.data.repository.core.support.PropertiesBasedNamedQueries">
|
||||
<constructor-arg>
|
||||
<props><prop key="Person.findTeam">start p=(%d) match (p)<-[:persons]-(group) return group</prop></props>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
<bean id="groupRepository" class="org.springframework.data.graph.neo4j.repository.GraphRepositoryFactoryBean">
|
||||
<property name="repositoryInterface" value="org.springframework.data.graph.neo4j.GroupRepository" />
|
||||
|
||||
Reference in New Issue
Block a user