testing work
This commit is contained in:
parent
e5b7f52b5c
commit
796c71a24a
|
@ -5,5 +5,6 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
|||
|
||||
public interface PrivilegeRepository extends JpaRepository<Privilege, Long> {
|
||||
public Privilege findByName(String name);
|
||||
|
||||
public void delete(Privilege privilege);
|
||||
}
|
||||
|
|
|
@ -5,5 +5,6 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
|||
|
||||
public interface RoleRepository extends JpaRepository<Role, Long> {
|
||||
public Role findByName(String name);
|
||||
|
||||
public void delete(Role role);
|
||||
}
|
||||
|
|
|
@ -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<Role> roles;
|
||||
|
||||
public User() {
|
||||
|
|
|
@ -28,7 +28,7 @@ public class UserService implements IUserService {
|
|||
|
||||
@Autowired
|
||||
private RoleRepository roleRepository;
|
||||
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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>());
|
||||
role.setPrivileges(new ArrayList<Privilege>());
|
||||
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<Privilege>());
|
||||
privilegeRepository.delete(privilege);
|
||||
|
||||
|
||||
assertNull(privilegeRepository.findByName(privilege.getName()));
|
||||
assertNotNull(roleRepository.findByName(role.getName()));
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue