spring-data-commons-mapping based cineasts running with tx-mode proxy

This commit is contained in:
Michael Hunger
2011-11-08 13:14:11 +01:00
parent d320acb35b
commit 96f44c31c9
13 changed files with 192 additions and 73 deletions

View File

@@ -17,6 +17,7 @@
<neo4j.version>1.5.M02</neo4j.version>
<spring-data-graph.version>2.0.0.BUILD-SNAPSHOT</spring-data-graph.version>
<org.slf4j-version>1.6.1</org.slf4j-version>
<aspectj.version>1.6.12.M1</aspectj.version>
</properties>
<repositories>
@@ -221,11 +222,11 @@
<version>1.7.2</version>
</dependency>
<!--dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency-->
</dependency>
<dependency>
<groupId>cglib</groupId>
@@ -304,7 +305,6 @@
<!-- The ajdtVersion configuration parameter is optional. The valid values are none, 1.4, and 1.5. none indicates that AJDT should not be enabled even though Aspectj is enabled in maven. 1.4 generates the org.eclipse.ajdt.ui.prefs file in the .settings directory. 1.5 (or later) includes the configuration into the .classpath file and is the default value. -->
<ajdtVersion>1.5</ajdtVersion>
<additionalProjectnatures>
<projectnature>org.eclipse.ajdt.ui.ajnature</projectnature>
<projectnature>org.eclipse.jdt.core.javanature</projectnature>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>

View File

@@ -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);
}

View File

@@ -127,8 +127,13 @@ public class User {
}
public Long getId() {
return nodeId;
}
@Override
public int hashCode() {
return nodeId != null ? nodeId.hashCode() : super.hashCode();
}
}

View File

@@ -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);
}

View File

@@ -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);
}
}
}

View File

@@ -1,11 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<context:annotation-config/>
<security:global-method-security secured-annotations="enabled">
</security:global-method-security>
@@ -22,7 +25,7 @@
</security:http>
<security:authentication-manager>
<security:authentication-provider user-service-ref="cineastsUserDetailsService">
<security:authentication-provider user-service-ref="cineastsUserDetailsServiceImpl">
<security:password-encoder hash="md5">
<security:salt-source system-wide="cewuiqwzie"/>
</security:password-encoder>

View File

@@ -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">
<context:annotation-config/>
<context:spring-configured/>
<context:component-scan base-package="org.neo4j.cineasts">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:spring-configured/>
<neo4j:config storeDirectory="data/graph.db"/>
<neo4j:repositories base-package="org.neo4j.cineasts.repository"/>
@@ -29,7 +30,7 @@
<constructor-arg value="data/json"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<tx:annotation-driven mode="proxy" transaction-manager="transactionManager"/>
<!--import resource="applicatioContext-security.xml"/-->
</beans>

View File

@@ -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">
<context:annotation-config/>
<mvc:annotation-driven/>
<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
@@ -23,6 +25,4 @@
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/views/" p:suffix=".jsp"/>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
</beans>

View File

@@ -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]);
}
}

View File

@@ -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";

View File

@@ -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"));

View File

@@ -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());
}
}

View File

@@ -21,7 +21,7 @@
<constructor-arg value="926d2a79e82920b62f03b1cb57e532e6"/>
</bean>
<bean class="org.neo4j.cineasts.movieimport.MovieDbLocalStorage">
<constructor-arg value="target/json-data"/>
<constructor-arg value="data/json"/>
</bean>
<tx:annotation-driven mode="aspectj"/>
<tx:annotation-driven mode="proxy"/>
</beans>