changed userserviceimpl to constructor injection. this broke the pathetic excuse of a unit test I had made. no regrets
This commit is contained in:
parent
849a5133d7
commit
2e7f6cced6
@ -2,10 +2,11 @@ package org.ros.chatto.repository;
|
|||||||
|
|
||||||
import org.ros.chatto.model.Role;
|
import org.ros.chatto.model.Role;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface RoleRepository extends JpaRepository<Role, Long>{
|
public interface RoleRepository extends JpaRepository<Role, Long>{
|
||||||
// @Query("select r from RoleRepository where name = ?1")
|
@Query("select r from Role r where r.name = ?1")
|
||||||
public Role findByName(String roleName);
|
public Role findByName(String roleName);
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ import org.ros.chatto.model.ChatUser;
|
|||||||
import org.ros.chatto.model.Role;
|
import org.ros.chatto.model.Role;
|
||||||
import org.ros.chatto.model.UserRole;
|
import org.ros.chatto.model.UserRole;
|
||||||
import org.ros.chatto.model.UserSession;
|
import org.ros.chatto.model.UserSession;
|
||||||
|
import org.ros.chatto.repository.RoleRepository;
|
||||||
import org.ros.chatto.repository.UserRepository;
|
import org.ros.chatto.repository.UserRepository;
|
||||||
import org.ros.chatto.repository.UserRoleRepository;
|
import org.ros.chatto.repository.UserRoleRepository;
|
||||||
import org.ros.chatto.repository.UserSessionRepository;
|
import org.ros.chatto.repository.UserSessionRepository;
|
||||||
@ -24,20 +25,25 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
@Transactional
|
@Transactional
|
||||||
@Service
|
@Service
|
||||||
public class UserServiceImpl implements UserService {
|
public class UserServiceImpl implements UserService {
|
||||||
@Autowired
|
private final UserRepository userRepository;
|
||||||
private UserRepository userRepository;
|
|
||||||
|
|
||||||
@Autowired
|
private final UserRoleRepository userRoleRepository;
|
||||||
private UserRoleRepository userRoleRepository;
|
|
||||||
|
|
||||||
@Autowired
|
private final PasswordEncoder passwordEncoder;
|
||||||
private PasswordEncoder passwordEncoder;
|
|
||||||
|
|
||||||
@Autowired
|
private final RoleRepository roleRepository;
|
||||||
private RoleService roleService;
|
|
||||||
|
|
||||||
@Autowired
|
private final UserSessionRepository userSessionRepository;
|
||||||
private UserSessionRepository userSessionRepository;
|
|
||||||
|
public UserServiceImpl(@Autowired UserRepository userRepository, @Autowired UserRoleRepository userRoleRepository,
|
||||||
|
@Autowired PasswordEncoder passwordEncoder, @Autowired RoleRepository roleRepository,
|
||||||
|
@Autowired UserSessionRepository userSessionRepository) {
|
||||||
|
this.userRepository = userRepository;
|
||||||
|
this.userRoleRepository = userRoleRepository;
|
||||||
|
this.passwordEncoder = passwordEncoder;
|
||||||
|
this.roleRepository = roleRepository;
|
||||||
|
this.userSessionRepository = userSessionRepository;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserRole registerUser(UserRegistrationDTO userRegistrationDTO) {
|
public UserRole registerUser(UserRegistrationDTO userRegistrationDTO) {
|
||||||
@ -46,11 +52,9 @@ public class UserServiceImpl implements UserService {
|
|||||||
user.setPassword(passwordEncoder.encode(userRegistrationDTO.getPassword()));
|
user.setPassword(passwordEncoder.encode(userRegistrationDTO.getPassword()));
|
||||||
ChatUser changedUser = userRepository.save(user);
|
ChatUser changedUser = userRepository.save(user);
|
||||||
UserRole userRole = new UserRole();
|
UserRole userRole = new UserRole();
|
||||||
Role role = roleService.getRole("USER");
|
Role role = roleRepository.findByName("USER");
|
||||||
userRole.setRole(role);
|
userRole.setRole(role);
|
||||||
userRole.setUser(changedUser);
|
userRole.setUser(changedUser);
|
||||||
// System.out.println(role.getRoleID());
|
|
||||||
// System.out.println(changedUser.getUserID());
|
|
||||||
userRoleRepository.save(userRole);
|
userRoleRepository.save(userRole);
|
||||||
return userRole;
|
return userRole;
|
||||||
}
|
}
|
||||||
|
@ -15,11 +15,11 @@ import org.ros.chatto.dto.UserRegistrationDTO;
|
|||||||
import org.ros.chatto.model.ChatUser;
|
import org.ros.chatto.model.ChatUser;
|
||||||
import org.ros.chatto.model.Role;
|
import org.ros.chatto.model.Role;
|
||||||
import org.ros.chatto.model.UserRole;
|
import org.ros.chatto.model.UserRole;
|
||||||
|
import org.ros.chatto.repository.RoleRepository;
|
||||||
import org.ros.chatto.repository.UserRepository;
|
import org.ros.chatto.repository.UserRepository;
|
||||||
import org.ros.chatto.repository.UserRepositoryCustom;
|
import org.ros.chatto.repository.UserRepositoryCustom;
|
||||||
import org.ros.chatto.repository.UserRoleRepository;
|
import org.ros.chatto.repository.UserRoleRepository;
|
||||||
import org.ros.chatto.repository.UserSessionRepository;
|
import org.ros.chatto.repository.UserSessionRepository;
|
||||||
import org.ros.chatto.service.RoleService;
|
|
||||||
import org.ros.chatto.service.UserService;
|
import org.ros.chatto.service.UserService;
|
||||||
import org.ros.chatto.service.UserServiceImpl;
|
import org.ros.chatto.service.UserServiceImpl;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
@ -33,7 +33,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class UnitTest {
|
public class UnitTest {
|
||||||
@InjectMocks
|
@InjectMocks
|
||||||
private UserService userService = new UserServiceImpl();
|
|
||||||
// private RoleService roleService;
|
// private RoleService roleService;
|
||||||
// private UserTokenService userTokenService;
|
// private UserTokenService userTokenService;
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ public class UnitTest {
|
|||||||
private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private RoleService roleService;
|
private RoleRepository roleRepository;
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private UserRepositoryCustom userRepositoryCustom;
|
private UserRepositoryCustom userRepositoryCustom;
|
||||||
@ -54,6 +54,9 @@ public class UnitTest {
|
|||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private UserRepository userRepository;
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
private UserService userService = new UserServiceImpl(userRepository, userRoleRepository, passwordEncoder, roleRepository,
|
||||||
|
userSessionRepository);
|
||||||
// private ChatUser chatUser;
|
// private ChatUser chatUser;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
@ -75,7 +78,7 @@ public class UnitTest {
|
|||||||
Role role = new Role();
|
Role role = new Role();
|
||||||
role.setRoleID(2);
|
role.setRoleID(2);
|
||||||
role.setName("USER");
|
role.setName("USER");
|
||||||
when(roleService.getRole("USER")).thenReturn(role);
|
when(roleRepository.findByName("USER")).thenReturn(role);
|
||||||
when(userRepository.save(chatUser)).thenReturn(chatUser);
|
when(userRepository.save(chatUser)).thenReturn(chatUser);
|
||||||
|
|
||||||
UserRole userRole = userService.registerUser(userRegistrationDTO);
|
UserRole userRole = userService.registerUser(userRegistrationDTO);
|
||||||
|
Loading…
Reference in New Issue
Block a user