update test, remove user repository implementation
This commit is contained in:
parent
03dbd93be2
commit
b3b7ac888c
@ -1,13 +0,0 @@
|
||||
----------------------------------------------------------------
|
||||
Wed Oct 05 21:59:32 EEST 2016:
|
||||
Booting Derby version The Apache Software Foundation - Apache Derby - 10.12.1.1 - (1704137): instance a816c00e-0157-9637-0b63-000000c038f0
|
||||
on database directory memory:C:\Users\lore\Documents\workspace-articles\spring-security-custom-permission\spring_custom_user_service with class loader sun.misc.Launcher$AppClassLoader@6433a2
|
||||
Loaded from file:/C:/Users/lore/.m2/repository/org/apache/derby/derby/10.12.1.1/derby-10.12.1.1.jar
|
||||
java.vendor=Oracle Corporation
|
||||
java.runtime.version=1.8.0_77-b03
|
||||
user.dir=C:\Users\lore\Documents\workspace-articles\spring-security-custom-permission
|
||||
os.name=Windows 7
|
||||
os.arch=x86
|
||||
os.version=6.1
|
||||
derby.system.home=null
|
||||
Database Class Loader started - derby.database.classpath=''
|
@ -5,7 +5,6 @@ import java.util.Properties;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.baeldung.persistence.dao.MyUserRepository;
|
||||
import org.baeldung.persistence.dao.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@ -13,6 +12,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
@ -23,6 +23,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@PropertySource({ "classpath:persistence-derby.properties" })
|
||||
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao")
|
||||
public class PersistenceConfig {
|
||||
|
||||
@Autowired
|
||||
@ -35,7 +36,7 @@ public class PersistenceConfig {
|
||||
// beans
|
||||
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean myEmf() {
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(dataSource());
|
||||
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
||||
@ -79,9 +80,4 @@ public class PersistenceConfig {
|
||||
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
|
||||
return hibernateProperties;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public UserRepository userRepository(){
|
||||
return new MyUserRepository();
|
||||
}
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
package org.baeldung.config;
|
||||
|
||||
import org.baeldung.persistence.dao.MyUserRepository;
|
||||
import org.baeldung.persistence.dao.UserRepository;
|
||||
import org.baeldung.security.MyUserDetailsService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -1,146 +0,0 @@
|
||||
package org.baeldung.persistence.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import org.baeldung.persistence.model.User;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Transactional
|
||||
public class MyUserRepository implements UserRepository {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Override
|
||||
public List<User> findAll() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> findAll(Sort sort) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> findAll(Iterable<Long> ids) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends User> List<S> save(Iterable<S> entities) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends User> S saveAndFlush(S entity) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteInBatch(Iterable<User> entities) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAllInBatch() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getOne(Long id) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<User> findAll(Pageable arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long count() {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Long arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(User arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Iterable<? extends User> arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAll() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists(Long arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User findOne(Long arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends User> S save(S user) {
|
||||
entityManager.persist(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User findByUsername(String username) {
|
||||
Query query = entityManager.createQuery("from User where username=:username", User.class);
|
||||
query.setParameter("username", username);
|
||||
List<User> result = query.getResultList();
|
||||
if (result != null && result.size() > 0) {
|
||||
return result.get(0);
|
||||
} else
|
||||
return null;
|
||||
}
|
||||
|
||||
public void removeUserByUsername(String username) {
|
||||
final Query query = entityManager.createQuery("delete from User where username=:username");
|
||||
query.setParameter("username", username);
|
||||
query.executeUpdate();
|
||||
}
|
||||
|
||||
}
|
@ -2,11 +2,13 @@ package org.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.model.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
|
||||
User findByUsername(final String username);
|
||||
|
||||
@Transactional
|
||||
void removeUserByUsername(String username);
|
||||
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ public class MyUserDetailsService implements UserDetailsService {
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(final String username) {
|
||||
final User user = userRepository.findByUsername(username);
|
||||
System.out.println("load user from repo"+user);
|
||||
if (user == null) {
|
||||
throw new UsernameNotFoundException(username);
|
||||
}
|
||||
|
@ -9,9 +9,11 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.security.authentication.AuthenticationProvider;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
@ -36,13 +38,16 @@ public class CustomUserDetailsServiceTest {
|
||||
|
||||
@Autowired
|
||||
AuthenticationProvider authenticationProvider;
|
||||
|
||||
@Autowired
|
||||
PasswordEncoder passwordEncoder;
|
||||
|
||||
@Test
|
||||
public void givenExistingUser_whenAuthenticate_thenRetrieveFromDb() {
|
||||
try {
|
||||
User user = new User();
|
||||
user.setUsername(USERNAME);
|
||||
user.setPassword(PASSWORD);
|
||||
user.setPassword(passwordEncoder.encode(PASSWORD));
|
||||
|
||||
myUserRepository.save(user);
|
||||
|
||||
@ -51,31 +56,23 @@ public class CustomUserDetailsServiceTest {
|
||||
|
||||
assertEquals(authentication.getName(), USERNAME);
|
||||
|
||||
} catch (Exception exc) {
|
||||
LOG.log(Level.SEVERE, "Error creating account");
|
||||
} finally {
|
||||
myUserRepository.removeUserByUsername(USERNAME);
|
||||
}
|
||||
}
|
||||
|
||||
@Test (expected = BadCredentialsException.class)
|
||||
|
||||
@Test(expected = BadCredentialsException.class)
|
||||
public void givenIncorrectUser_whenAuthenticate_thenBadCredentialsException() {
|
||||
try {
|
||||
User user = new User();
|
||||
user.setUsername(USERNAME);
|
||||
user.setPassword(PASSWORD);
|
||||
user.setPassword(passwordEncoder.encode(PASSWORD));
|
||||
|
||||
myUserRepository.save(user);
|
||||
|
||||
try {
|
||||
myUserRepository.save(user);
|
||||
}
|
||||
catch (Exception exc) {
|
||||
LOG.log(Level.SEVERE, "Error creating account");
|
||||
}
|
||||
|
||||
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(USERNAME2, PASSWORD);
|
||||
Authentication authentication = authenticationProvider.authenticate(auth);
|
||||
}
|
||||
finally {
|
||||
authenticationProvider.authenticate(auth);
|
||||
} finally {
|
||||
myUserRepository.removeUserByUsername(USERNAME);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user