From 796c71a24af63e6c87e5aee080e60ee3aa23292c Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 11 Jan 2015 23:43:46 +0200 Subject: [PATCH] testing work --- .../persistence/dao/PrivilegeRepository.java | 1 + .../persistence/dao/RoleRepository.java | 1 + .../org/baeldung/persistence/model/User.java | 6 +- .../persistence/service/UserService.java | 2 +- .../baeldung/spring/PersistenceJPAConfig.java | 4 +- .../test/SpringSecurityRolesTest.java | 20 ++--- .../java/org/baeldung/test/TestConfig.java | 73 ------------------- 7 files changed, 18 insertions(+), 89 deletions(-) delete mode 100644 spring-security-login-and-registration/src/main/java/org/baeldung/test/TestConfig.java diff --git a/spring-security-login-and-registration/src/main/java/org/baeldung/persistence/dao/PrivilegeRepository.java b/spring-security-login-and-registration/src/main/java/org/baeldung/persistence/dao/PrivilegeRepository.java index 4b878b5230..3f8016f314 100644 --- a/spring-security-login-and-registration/src/main/java/org/baeldung/persistence/dao/PrivilegeRepository.java +++ b/spring-security-login-and-registration/src/main/java/org/baeldung/persistence/dao/PrivilegeRepository.java @@ -5,5 +5,6 @@ import org.springframework.data.jpa.repository.JpaRepository; public interface PrivilegeRepository extends JpaRepository { public Privilege findByName(String name); + public void delete(Privilege privilege); } diff --git a/spring-security-login-and-registration/src/main/java/org/baeldung/persistence/dao/RoleRepository.java b/spring-security-login-and-registration/src/main/java/org/baeldung/persistence/dao/RoleRepository.java index f4d6d10526..90d6de60f2 100644 --- a/spring-security-login-and-registration/src/main/java/org/baeldung/persistence/dao/RoleRepository.java +++ b/spring-security-login-and-registration/src/main/java/org/baeldung/persistence/dao/RoleRepository.java @@ -5,5 +5,6 @@ import org.springframework.data.jpa.repository.JpaRepository; public interface RoleRepository extends JpaRepository { public Role findByName(String name); + public void delete(Role role); } diff --git a/spring-security-login-and-registration/src/main/java/org/baeldung/persistence/model/User.java b/spring-security-login-and-registration/src/main/java/org/baeldung/persistence/model/User.java index a78f7d06f9..5aac7f50a9 100644 --- a/spring-security-login-and-registration/src/main/java/org/baeldung/persistence/model/User.java +++ b/spring-security-login-and-registration/src/main/java/org/baeldung/persistence/model/User.java @@ -1,6 +1,5 @@ package org.baeldung.persistence.model; - import java.util.Collection; import javax.persistence.Entity; @@ -31,10 +30,7 @@ public class User { private boolean tokenExpired; @ManyToMany - @JoinTable( - name = "users_roles", - joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), - inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id")) + @JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id")) private Collection roles; public User() { diff --git a/spring-security-login-and-registration/src/main/java/org/baeldung/persistence/service/UserService.java b/spring-security-login-and-registration/src/main/java/org/baeldung/persistence/service/UserService.java index 15989f9e8b..00e3a4a46f 100644 --- a/spring-security-login-and-registration/src/main/java/org/baeldung/persistence/service/UserService.java +++ b/spring-security-login-and-registration/src/main/java/org/baeldung/persistence/service/UserService.java @@ -28,7 +28,7 @@ public class UserService implements IUserService { @Autowired private RoleRepository roleRepository; - + // API @Override diff --git a/spring-security-login-and-registration/src/main/java/org/baeldung/spring/PersistenceJPAConfig.java b/spring-security-login-and-registration/src/main/java/org/baeldung/spring/PersistenceJPAConfig.java index e16dc16f3f..d2465af345 100644 --- a/spring-security-login-and-registration/src/main/java/org/baeldung/spring/PersistenceJPAConfig.java +++ b/spring-security-login-and-registration/src/main/java/org/baeldung/spring/PersistenceJPAConfig.java @@ -1,7 +1,9 @@ package org.baeldung.spring; import java.util.Properties; + import javax.sql.DataSource; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -53,7 +55,7 @@ public class PersistenceJPAConfig { @Bean public JpaTransactionManager transactionManager() { - JpaTransactionManager transactionManager = new JpaTransactionManager(); + final JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); return transactionManager; } diff --git a/spring-security-login-and-registration/src/main/java/org/baeldung/test/SpringSecurityRolesTest.java b/spring-security-login-and-registration/src/main/java/org/baeldung/test/SpringSecurityRolesTest.java index 2cf3fc453f..77a67d17d7 100644 --- a/spring-security-login-and-registration/src/main/java/org/baeldung/test/SpringSecurityRolesTest.java +++ b/spring-security-login-and-registration/src/main/java/org/baeldung/test/SpringSecurityRolesTest.java @@ -15,13 +15,12 @@ import org.baeldung.persistence.model.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; -import org.springframework.test.context.support.AnnotationConfigContextLoader; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class) @@ -38,10 +37,15 @@ public class SpringSecurityRolesTest { @Autowired private PrivilegeRepository privilegeRepository; + @Autowired + private PasswordEncoder passwordEncoder; + private User user; private Role role; private Privilege privilege; + // tests + @Test public void testDeleteUser() { role = new Role("TEST_ROLE"); @@ -50,8 +54,7 @@ public class SpringSecurityRolesTest { user = new User(); user.setFirstName("John"); user.setLastName("Doe"); - PasswordEncoder encoder = new BCryptPasswordEncoder(); - user.setPassword(encoder.encode("123")); + user.setPassword(passwordEncoder.encode("123")); user.setEmail("john@doe.com"); user.setRoles(Arrays.asList(role)); user.setEnabled(true); @@ -78,8 +81,7 @@ public class SpringSecurityRolesTest { user = new User(); user.setFirstName("John"); user.setLastName("Doe"); - PasswordEncoder encoder = new BCryptPasswordEncoder(); - user.setPassword(encoder.encode("123")); + user.setPassword(passwordEncoder.encode("123")); user.setEmail("john@doe.com"); user.setRoles(Arrays.asList(role)); user.setEnabled(true); @@ -92,7 +94,7 @@ public class SpringSecurityRolesTest { user.setRoles(new ArrayList()); role.setPrivileges(new ArrayList()); roleRepository.delete(role); - + assertNull(roleRepository.findByName(role.getName())); assertNotNull(privilegeRepository.findByName(privilege.getName())); assertNotNull(userRepository.findByEmail(user.getEmail())); @@ -106,13 +108,13 @@ public class SpringSecurityRolesTest { role = new Role("TEST_ROLE"); role.setPrivileges(Arrays.asList(privilege)); roleRepository.save(role); - + assertNotNull(roleRepository.findByName(role.getName())); assertNotNull(privilegeRepository.findByName(privilege.getName())); role.setPrivileges(new ArrayList()); privilegeRepository.delete(privilege); - + assertNull(privilegeRepository.findByName(privilege.getName())); assertNotNull(roleRepository.findByName(role.getName())); } diff --git a/spring-security-login-and-registration/src/main/java/org/baeldung/test/TestConfig.java b/spring-security-login-and-registration/src/main/java/org/baeldung/test/TestConfig.java deleted file mode 100644 index 9168179b8a..0000000000 --- a/spring-security-login-and-registration/src/main/java/org/baeldung/test/TestConfig.java +++ /dev/null @@ -1,73 +0,0 @@ -package org.baeldung.test; - -import java.util.Properties; - -import javax.sql.DataSource; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -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; -import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -@Configuration -@EnableTransactionManagement -@PropertySource({ "classpath:persistence.properties" }) -@ComponentScan({ "org.baeldung.persistence.model","org.baeldung.persistence.dao" }) -@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao") -public class TestConfig { - @Autowired - private Environment env; - - public TestConfig() { - super(); - } - - @Bean - public LocalContainerEntityManagerFactoryBean entityManagerFactory() { - final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); - em.setDataSource(dataSource()); - em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" }); - final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); - em.setJpaVendorAdapter(vendorAdapter); - em.setJpaProperties(additionalProperties()); - return em; - } - - @Bean - public DataSource dataSource() { - final DriverManagerDataSource dataSource = new DriverManagerDataSource(); - dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); - dataSource.setUrl(env.getProperty("jdbc.url")); - dataSource.setUsername(env.getProperty("jdbc.user")); - dataSource.setPassword(env.getProperty("jdbc.pass")); - return dataSource; - } - - @Bean - public JpaTransactionManager transactionManager() { - JpaTransactionManager transactionManager = new JpaTransactionManager(); - transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); - return transactionManager; - } - - @Bean - public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { - return new PersistenceExceptionTranslationPostProcessor(); - } - - final Properties additionalProperties() { - final Properties hibernateProperties = new Properties(); - hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); - hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); - return hibernateProperties; - } -}