diff --git a/spring-data-neo4j-examples/cineasts/pom.xml b/spring-data-neo4j-examples/cineasts/pom.xml index 604f38d00..21ed3a675 100644 --- a/spring-data-neo4j-examples/cineasts/pom.xml +++ b/spring-data-neo4j-examples/cineasts/pom.xml @@ -17,6 +17,7 @@ 1.5.M02 2.0.0.BUILD-SNAPSHOT 1.6.1 + 1.6.12.M1 @@ -221,11 +222,11 @@ 1.7.2 - + cglib @@ -304,7 +305,6 @@ 1.5 - org.eclipse.ajdt.ui.ajnature org.eclipse.jdt.core.javanature org.springframework.ide.eclipse.core.springnature diff --git a/spring-data-neo4j-examples/cineasts/src/main/java/org/neo4j/cineasts/controller/MovieController.java b/spring-data-neo4j-examples/cineasts/src/main/java/org/neo4j/cineasts/controller/MovieController.java index b61cd3954..59010feba 100644 --- a/spring-data-neo4j-examples/cineasts/src/main/java/org/neo4j/cineasts/controller/MovieController.java +++ b/spring-data-neo4j-examples/cineasts/src/main/java/org/neo4j/cineasts/controller/MovieController.java @@ -80,7 +80,7 @@ public class MovieController { if (user != null && movie != null) { int stars1 = stars==null ? -1 : stars; String comment1 = comment!=null ? comment.trim() : null; - user.rate(template,movie, stars1, comment1); + userDetailsService.rate(movie, user, stars1, comment1); } return singleMovieView(model,movieId); } diff --git a/spring-data-neo4j-examples/cineasts/src/main/java/org/neo4j/cineasts/domain/User.java b/spring-data-neo4j-examples/cineasts/src/main/java/org/neo4j/cineasts/domain/User.java index bb14020fb..3fa5f912b 100644 --- a/spring-data-neo4j-examples/cineasts/src/main/java/org/neo4j/cineasts/domain/User.java +++ b/spring-data-neo4j-examples/cineasts/src/main/java/org/neo4j/cineasts/domain/User.java @@ -127,8 +127,13 @@ public class User { } + public Long getId() { + return nodeId; + } + @Override public int hashCode() { + return nodeId != null ? nodeId.hashCode() : super.hashCode(); } } diff --git a/spring-data-neo4j-examples/cineasts/src/main/java/org/neo4j/cineasts/service/CineastsUserDetailsService.java b/spring-data-neo4j-examples/cineasts/src/main/java/org/neo4j/cineasts/service/CineastsUserDetailsService.java index 3f4e81a67..b4c7c7243 100644 --- a/spring-data-neo4j-examples/cineasts/src/main/java/org/neo4j/cineasts/service/CineastsUserDetailsService.java +++ b/spring-data-neo4j-examples/cineasts/src/main/java/org/neo4j/cineasts/service/CineastsUserDetailsService.java @@ -1,77 +1,32 @@ package org.neo4j.cineasts.service; +import org.neo4j.cineasts.domain.Movie; +import org.neo4j.cineasts.domain.Rating; import org.neo4j.cineasts.domain.User; -import org.neo4j.cineasts.repository.UserRepository; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContext; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; -import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** * @author mh - * @since 06.03.11 + * @since 08.11.11 */ -@Service -public class CineastsUserDetailsService implements UserDetailsService { - - @Autowired - private UserRepository userRepository; - +public interface CineastsUserDetailsService extends UserDetailsService { @Override - public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException, DataAccessException { - final User user = findUser(login); - if (user==null) throw new UsernameNotFoundException("Username not found",login); - return new CineastsUserDetails(user); - } + UserDetails loadUserByUsername(String login) throws UsernameNotFoundException, DataAccessException; - public User findUser(String login) { - return userRepository.findByPropertyValue("login",login); - } + User findUser(String login); - - public User getUserFromSession() { - SecurityContext context = SecurityContextHolder.getContext(); - Authentication authentication = context.getAuthentication(); - Object principal = authentication.getPrincipal(); - if (principal instanceof CineastsUserDetails) { - CineastsUserDetails userDetails = (CineastsUserDetails) principal; - return userDetails.getUser(); - } - return null; - } + User getUserFromSession(); @Transactional - public User register(String login, String name, String password) { - User found = findUser(login); - if (found!=null) throw new RuntimeException("Login already taken: "+login); - if (name==null || name.isEmpty()) throw new RuntimeException("No name provided."); - if (password==null || password.isEmpty()) throw new RuntimeException("No password provided."); - User user=userRepository.save(new User(login,name,password,User.Roles.ROLE_USER)); - setUserInSession(user); - return user; - } - - private void setUserInSession(User user) { - SecurityContext context = SecurityContextHolder.getContext(); - CineastsUserDetails userDetails = new CineastsUserDetails(user); - UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, user.getPassword(),userDetails.getAuthorities()); - context.setAuthentication(authentication); - - } + Rating rate(Movie movie, User user, int stars, String comment); @Transactional - public void addFriend(String login) { - User friend = findUser(login); - User user = getUserFromSession(); - if (!user.equals(friend)) { - user.addFriend(friend); - } - } + User register(String login, String name, String password); + + @Transactional + void addFriend(String login); } diff --git a/spring-data-neo4j-examples/cineasts/src/main/java/org/neo4j/cineasts/service/CineastsUserDetailsServiceImpl.java b/spring-data-neo4j-examples/cineasts/src/main/java/org/neo4j/cineasts/service/CineastsUserDetailsServiceImpl.java new file mode 100644 index 000000000..081994d6e --- /dev/null +++ b/spring-data-neo4j-examples/cineasts/src/main/java/org/neo4j/cineasts/service/CineastsUserDetailsServiceImpl.java @@ -0,0 +1,93 @@ +package org.neo4j.cineasts.service; + +import org.neo4j.cineasts.domain.Movie; +import org.neo4j.cineasts.domain.Rating; +import org.neo4j.cineasts.domain.User; +import org.neo4j.cineasts.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataAccessException; +import org.springframework.data.neo4j.template.Neo4jOperations; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * @author mh + * @since 06.03.11 + */ +@Service +public class CineastsUserDetailsServiceImpl implements CineastsUserDetailsService { + + @Autowired + private UserRepository userRepository; + @Autowired + private Neo4jOperations template; + + @Override + public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException, DataAccessException { + final User user = findUser(login); + if (user==null) throw new UsernameNotFoundException("Username not found",login); + return new CineastsUserDetails(user); + } + + @Override + public User findUser(String login) { + return userRepository.findByPropertyValue("login",login); + } + + + @Override + public User getUserFromSession() { + SecurityContext context = SecurityContextHolder.getContext(); + Authentication authentication = context.getAuthentication(); + Object principal = authentication.getPrincipal(); + if (principal instanceof CineastsUserDetails) { + CineastsUserDetails userDetails = (CineastsUserDetails) principal; + return userDetails.getUser(); + } + return null; + } + + @Override + @Transactional + public Rating rate(Movie movie, User user, int stars, String comment) { + return user.rate(template,movie, stars, comment); + } + + + @Override + @Transactional + public User register(String login, String name, String password) { + User found = findUser(login); + if (found!=null) throw new RuntimeException("Login already taken: "+login); + if (name==null || name.isEmpty()) throw new RuntimeException("No name provided."); + if (password==null || password.isEmpty()) throw new RuntimeException("No password provided."); + User user=userRepository.save(new User(login,name,password,User.Roles.ROLE_USER)); + setUserInSession(user); + return user; + } + + void setUserInSession(User user) { + SecurityContext context = SecurityContextHolder.getContext(); + CineastsUserDetails userDetails = new CineastsUserDetails(user); + UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, user.getPassword(),userDetails.getAuthorities()); + context.setAuthentication(authentication); + + } + + @Override + @Transactional + public void addFriend(String login) { + User friend = findUser(login); + User user = getUserFromSession(); + if (!user.equals(friend)) { + user.addFriend(friend); + template.save(user); + } + } +} diff --git a/spring-data-neo4j-examples/cineasts/src/main/webapp/WEB-INF/applicatioContext-security.xml b/spring-data-neo4j-examples/cineasts/src/main/webapp/WEB-INF/applicatioContext-security.xml index 9f1321d68..eebadb1b1 100644 --- a/spring-data-neo4j-examples/cineasts/src/main/webapp/WEB-INF/applicatioContext-security.xml +++ b/spring-data-neo4j-examples/cineasts/src/main/webapp/WEB-INF/applicatioContext-security.xml @@ -1,11 +1,14 @@ + @@ -22,7 +25,7 @@ - + diff --git a/spring-data-neo4j-examples/cineasts/src/main/webapp/WEB-INF/applicationContext.xml b/spring-data-neo4j-examples/cineasts/src/main/webapp/WEB-INF/applicationContext.xml index bff684ee6..1072405b8 100644 --- a/spring-data-neo4j-examples/cineasts/src/main/webapp/WEB-INF/applicationContext.xml +++ b/spring-data-neo4j-examples/cineasts/src/main/webapp/WEB-INF/applicationContext.xml @@ -9,11 +9,12 @@ http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> - + + @@ -29,7 +30,7 @@ - + \ No newline at end of file diff --git a/spring-data-neo4j-examples/cineasts/src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml b/spring-data-neo4j-examples/cineasts/src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml index 2f428fc98..7e6817554 100644 --- a/spring-data-neo4j-examples/cineasts/src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml +++ b/spring-data-neo4j-examples/cineasts/src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml @@ -16,6 +16,8 @@ http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> + + @@ -23,6 +25,4 @@ - - \ No newline at end of file diff --git a/spring-data-neo4j-examples/cineasts/src/test/java/org/neo4j/cineasts/domain/DomainTest.java b/spring-data-neo4j-examples/cineasts/src/test/java/org/neo4j/cineasts/domain/DomainTest.java index c24c9b71c..1f163e5de 100644 --- a/spring-data-neo4j-examples/cineasts/src/test/java/org/neo4j/cineasts/domain/DomainTest.java +++ b/spring-data-neo4j-examples/cineasts/src/test/java/org/neo4j/cineasts/domain/DomainTest.java @@ -14,9 +14,8 @@ import org.springframework.transaction.annotation.Transactional; import java.util.Iterator; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; /** * @author mh @@ -68,7 +67,7 @@ public class DomainTest { public void userCanRateMovie() { Movie movie = template.save(new Movie("1", "Forrest Gump")); User user = template.save(new User("ich", "Micha", "password")); - Rating awesome = user.rate(template, movie, 5, "Awesome"); + Rating awesome = user.rate(template,movie, 5, "Awesome"); user = userRepository.findByPropertyValue("login", "ich"); movie = movieRepository.findById("1"); @@ -84,4 +83,16 @@ public class DomainTest { User foundUser = userRepository.findByPropertyValue("login", "ich"); assertEquals(user, foundUser); } + @Test + public void testCreateUser() throws Exception { + User user = userRepository.save(new User("me", "me", "me", User.Roles.ROLE_USER)); + assertEquals("user login", "me",user.getLogin()); + assertNotNull("user roles", user.getRole()); + assertEquals("user role", User.Roles.ROLE_USER,user.getRole()[0]); + User user2=userRepository.findOne(user.getId()); + assertEquals("loaded user id", user.getId(),user2.getId()); + assertEquals("loaded user login", "me",user2.getLogin()); + assertNotNull("loaded user roles", user2.getRole()); + assertEquals("loaded user login", User.Roles.ROLE_USER,user2.getRole()[0]); + } } diff --git a/spring-data-neo4j-examples/cineasts/src/test/java/org/neo4j/cineasts/movieimport/MovieDbApiClientTest.java b/spring-data-neo4j-examples/cineasts/src/test/java/org/neo4j/cineasts/movieimport/MovieDbApiClientTest.java index 935c56c29..0f1dc4b8f 100644 --- a/spring-data-neo4j-examples/cineasts/src/test/java/org/neo4j/cineasts/movieimport/MovieDbApiClientTest.java +++ b/spring-data-neo4j-examples/cineasts/src/test/java/org/neo4j/cineasts/movieimport/MovieDbApiClientTest.java @@ -1,5 +1,6 @@ package org.neo4j.cineasts.movieimport; +import org.junit.Ignore; import org.junit.Test; import java.util.Map; @@ -10,6 +11,7 @@ import static org.junit.Assert.assertEquals; * @author mh * @since 13.03.11 */ +@Ignore public class MovieDbApiClientTest { private static final String API_KEY = "926d2a79e82920b62f03b1cb57e532e6"; diff --git a/spring-data-neo4j-examples/cineasts/src/test/java/org/neo4j/cineasts/service/MoviesRepositoryTest.java b/spring-data-neo4j-examples/cineasts/src/test/java/org/neo4j/cineasts/service/MoviesRepositoryTest.java index afa33231c..37eaf5178 100644 --- a/spring-data-neo4j-examples/cineasts/src/test/java/org/neo4j/cineasts/service/MoviesRepositoryTest.java +++ b/spring-data-neo4j-examples/cineasts/src/test/java/org/neo4j/cineasts/service/MoviesRepositoryTest.java @@ -40,8 +40,8 @@ public class MoviesRepositoryTest { Movie movie = movieRepository.save(new Movie("1", "Test-Movie")); Movie found = movieRepository.findById("1"); assertEquals("movie found by id", movie, found); - } + @Test public void testGetMovieRecommendations() throws Exception { Movie movie = movieRepository.save(new Movie("1", "Test-Movie")); diff --git a/spring-data-neo4j-examples/cineasts/src/test/java/org/neo4j/cineasts/service/TransactionTest.java b/spring-data-neo4j-examples/cineasts/src/test/java/org/neo4j/cineasts/service/TransactionTest.java new file mode 100644 index 000000000..588b87307 --- /dev/null +++ b/spring-data-neo4j-examples/cineasts/src/test/java/org/neo4j/cineasts/service/TransactionTest.java @@ -0,0 +1,49 @@ +package org.neo4j.cineasts.service; + +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.neo4j.cineasts.domain.Movie; +import org.neo4j.cineasts.domain.User; +import org.neo4j.cineasts.repository.MovieRepository; +import org.neo4j.cineasts.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.junit.Assert.assertEquals; + +/** + * @author mh + * @since 08.11.11 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration({"/movies-test-context.xml"}) +public class TransactionTest { + + @Autowired + UserRepository userRepository; + @Autowired + MovieRepository movieRepository; + @Autowired + CineastsUserDetailsService userDetailsService; + + @Test + @Ignore + public void testBefriendUsers() { + final User me = userRepository.save(new User("me", "me", "me")); + final User you = userRepository.save(new User("you", "you", "you")); + userDetailsService.addFriend("you"); + final User loaded = userRepository.findOne(me.getId()); + assertEquals(1,loaded.getFriends().size()); + } + + @Test + public void testRateMovie() { + final User me = userRepository.save(new User("me", "me", "me")); + final Movie movie = movieRepository.save(new Movie("1","Movie")); + userDetailsService.rate(movie, me, 5, "cool"); + final User loaded = userRepository.findOne(me.getId()); + assertEquals(1,loaded.getRatings().size()); + } +} diff --git a/spring-data-neo4j-examples/cineasts/src/test/resources/movies-test-context.xml b/spring-data-neo4j-examples/cineasts/src/test/resources/movies-test-context.xml index 51064ec85..ce2821043 100644 --- a/spring-data-neo4j-examples/cineasts/src/test/resources/movies-test-context.xml +++ b/spring-data-neo4j-examples/cineasts/src/test/resources/movies-test-context.xml @@ -21,7 +21,7 @@ - + - + \ No newline at end of file