add test config
This commit is contained in:
parent
c8513d0d80
commit
f225eba3d6
|
@ -4,5 +4,6 @@ import org.baeldung.persistence.model.Privilege;
|
|||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface PrivilegeRepository extends JpaRepository<Privilege, Long> {
|
||||
Privilege findByName(String name);
|
||||
public Privilege findByName(String name);
|
||||
public void delete(Privilege privilege);
|
||||
}
|
||||
|
|
|
@ -5,4 +5,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
|||
|
||||
public interface RoleRepository extends JpaRepository<Role, Long> {
|
||||
public Role findByName(String name);
|
||||
public void delete(Role role);
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package org.baeldung.persistence.model;
|
|||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
|
@ -21,7 +20,7 @@ public class Role {
|
|||
@ManyToMany(mappedBy = "roles")
|
||||
private Collection<User> users;
|
||||
|
||||
@ManyToMany(cascade = CascadeType.ALL)
|
||||
@ManyToMany
|
||||
@JoinTable(name = "roles_privileges", joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "privilege_id", referencedColumnName = "id"))
|
||||
private Collection<Privilege> privileges;
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@ package org.baeldung.persistence.model;
|
|||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
|
@ -31,7 +30,7 @@ public class User {
|
|||
|
||||
private boolean tokenExpired;
|
||||
|
||||
@ManyToMany(cascade = CascadeType.ALL)
|
||||
@ManyToMany
|
||||
@JoinTable(
|
||||
name = "users_roles",
|
||||
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
package org.baeldung.test;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.baeldung.persistence.dao.PrivilegeRepository;
|
||||
|
@ -8,12 +12,6 @@ import org.baeldung.persistence.dao.UserRepository;
|
|||
import org.baeldung.persistence.model.Privilege;
|
||||
import org.baeldung.persistence.model.Role;
|
||||
import org.baeldung.persistence.model.User;
|
||||
import org.baeldung.spring.AppConfig;
|
||||
import org.baeldung.spring.MvcConfig;
|
||||
import org.baeldung.spring.PersistenceJPAConfig;
|
||||
import org.baeldung.spring.SecSecurityConfig;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -22,13 +20,13 @@ 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.transaction.TransactionConfiguration;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { AppConfig.class, MvcConfig.class, PersistenceJPAConfig.class, SecSecurityConfig.class})
|
||||
@ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@Transactional
|
||||
@TransactionConfiguration
|
||||
public class SpringSecurityRolesTest {
|
||||
|
||||
@Autowired
|
||||
|
@ -39,19 +37,16 @@ public class SpringSecurityRolesTest {
|
|||
|
||||
@Autowired
|
||||
private PrivilegeRepository privilegeRepository;
|
||||
|
||||
|
||||
private User user;
|
||||
private Role role;
|
||||
private Privilege privilege;
|
||||
|
||||
@Before
|
||||
public void init(){
|
||||
privilege = new Privilege("TEST_PRIVILEGE");
|
||||
privilegeRepository.save(privilege);
|
||||
|
||||
|
||||
@Test
|
||||
public void testDeleteUser() {
|
||||
role = new Role("TEST_ROLE");
|
||||
roleRepository.save(role);
|
||||
|
||||
|
||||
user = new User();
|
||||
user.setFirstName("John");
|
||||
user.setLastName("Doe");
|
||||
|
@ -61,31 +56,64 @@ public class SpringSecurityRolesTest {
|
|||
user.setRoles(Arrays.asList(role));
|
||||
user.setEnabled(true);
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp(){
|
||||
privilegeRepository.delete(privilege);
|
||||
roleRepository.delete(role);
|
||||
|
||||
assertNotNull(userRepository.findByEmail(user.getEmail()));
|
||||
assertNotNull(roleRepository.findByName(role.getName()));
|
||||
user.setRoles(null);
|
||||
userRepository.delete(user);
|
||||
|
||||
assertNull(userRepository.findByEmail(user.getEmail()));
|
||||
assertNotNull(roleRepository.findByName(role.getName()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDeleteUser(){
|
||||
userRepository.delete(user);
|
||||
System.out.println(roleRepository.findByName(role.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteRole(){
|
||||
public void testDeleteRole() {
|
||||
privilege = new Privilege("TEST_PRIVILEGE");
|
||||
privilegeRepository.save(privilege);
|
||||
|
||||
role = new Role("TEST_ROLE");
|
||||
role.setPrivileges(Arrays.asList(privilege));
|
||||
roleRepository.save(role);
|
||||
|
||||
user = new User();
|
||||
user.setFirstName("John");
|
||||
user.setLastName("Doe");
|
||||
PasswordEncoder encoder = new BCryptPasswordEncoder();
|
||||
user.setPassword(encoder.encode("123"));
|
||||
user.setEmail("john@doe.com");
|
||||
user.setRoles(Arrays.asList(role));
|
||||
user.setEnabled(true);
|
||||
userRepository.save(user);
|
||||
|
||||
assertNotNull(privilegeRepository.findByName(privilege.getName()));
|
||||
assertNotNull(userRepository.findByEmail(user.getEmail()));
|
||||
assertNotNull(roleRepository.findByName(role.getName()));
|
||||
|
||||
user.setRoles(new ArrayList<Role>());
|
||||
role.setPrivileges(new ArrayList<Privilege>());
|
||||
roleRepository.delete(role);
|
||||
System.out.println(privilegeRepository.findByName(privilege.getName()));
|
||||
System.out.println(userRepository.findByEmail(user.getEmail()));
|
||||
|
||||
assertNull(roleRepository.findByName(role.getName()));
|
||||
assertNotNull(privilegeRepository.findByName(privilege.getName()));
|
||||
assertNotNull(userRepository.findByEmail(user.getEmail()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDeletePrivilege(){
|
||||
public void testDeletePrivilege() {
|
||||
privilege = new Privilege("TEST_PRIVILEGE");
|
||||
privilegeRepository.save(privilege);
|
||||
|
||||
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<Privilege>());
|
||||
privilegeRepository.delete(privilege);
|
||||
System.out.println(roleRepository.findByName(role.getName()));
|
||||
|
||||
assertNull(privilegeRepository.findByName(privilege.getName()));
|
||||
assertNotNull(roleRepository.findByName(role.getName()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue