From 02f7c4d533790ed75ed928e88f986c2b60f73b2b Mon Sep 17 00:00:00 2001 From: Michael Hunger Date: Tue, 8 Nov 2011 13:15:11 +0100 Subject: [PATCH] added test for custom implementation of repository methods --- .../neo4j/repository/GraphRepositoryTest.java | 14 ++++++-- .../neo4j/repository/PersonRepository.java | 2 +- .../PersonRepositoryFriendship.java | 27 +++++++++++++++ .../repository/PersonRepositoryImpl.java | 34 +++++++++++++++++++ 4 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/PersonRepositoryFriendship.java create mode 100644 spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/PersonRepositoryImpl.java diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/GraphRepositoryTest.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/GraphRepositoryTest.java index 35fd67098..0599ddfda 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/GraphRepositoryTest.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/GraphRepositoryTest.java @@ -18,15 +18,16 @@ package org.springframework.data.neo4j.repository; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.hibernate.ejb.criteria.expression.function.AggregationFunction; import org.junit.Before; import org.junit.Ignore; 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.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; +import org.springframework.data.neo4j.model.Friendship; import org.springframework.data.neo4j.model.Group; import org.springframework.data.neo4j.model.Person; import org.springframework.data.neo4j.support.Neo4jTemplate; @@ -51,8 +52,8 @@ import java.util.Map; import static java.util.Arrays.asList; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; import static org.junit.internal.matchers.IsCollectionContaining.hasItem; import static org.junit.internal.matchers.IsCollectionContaining.hasItems; import static org.neo4j.helpers.collection.IteratorUtil.addToCollection; @@ -256,4 +257,13 @@ public class GraphRepositoryTest { final Iterable groups = groupRepository.findByName(testTeam.sdg.getName(), new PageRequest(0, 1)); assertThat(groups, hasItem(testTeam.sdg)); } + + @Test @Transactional + public void testCustomImplementation() { + final Friendship friendship = personRepository.befriend(testTeam.michael, testTeam.emil); + assertNotNull(friendship.getId()); + final Person loaded = personRepository.findOne(testTeam.michael.getId()); + assertEquals(2, IteratorUtil.count(loaded.getFriendships())); + assertThat(loaded.getFriendships(),hasItem(friendship)); + } } diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/PersonRepository.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/PersonRepository.java index e73c9d052..9cc5abd3b 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/PersonRepository.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/PersonRepository.java @@ -34,7 +34,7 @@ import java.util.Map; * @author Oliver Gierke * @since 29.03.11 */ -public interface PersonRepository extends GraphRepository, NamedIndexRepository, SpatialRepository { +public interface PersonRepository extends GraphRepository, NamedIndexRepository, SpatialRepository, PersonRepositoryFriendship { @Query("start team=node({p_team}) match (team)-[:persons]->(member) return member") Iterable findAllTeamMembers(@Param("p_team") Group team); diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/PersonRepositoryFriendship.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/PersonRepositoryFriendship.java new file mode 100644 index 000000000..4c9101484 --- /dev/null +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/PersonRepositoryFriendship.java @@ -0,0 +1,27 @@ +/** + * 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.neo4j.repository; + +import org.springframework.data.neo4j.model.Friendship; +import org.springframework.data.neo4j.model.Person; + +/** + * @author mh + * @since 08.11.11 + */ +public interface PersonRepositoryFriendship { + Friendship befriend(Person p1, Person p2); +} diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/PersonRepositoryImpl.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/PersonRepositoryImpl.java new file mode 100644 index 000000000..1d8fcbaa2 --- /dev/null +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/PersonRepositoryImpl.java @@ -0,0 +1,34 @@ +/** + * 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.neo4j.repository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.neo4j.model.Friendship; +import org.springframework.data.neo4j.model.Person; +import org.springframework.data.neo4j.template.Neo4jOperations; + +/** + * @author mh + * @since 08.11.11 + */ +public class PersonRepositoryImpl implements PersonRepositoryFriendship { + @Autowired + Neo4jOperations template; + @Override + public Friendship befriend(Person p1, Person p2) { + return template.createRelationshipBetween(p1, p2, Friendship.class, "knows", false); + } +}