added a unit test for reegistration. Also some cleanup

This commit is contained in:
Rohan Sircar 2019-11-30 10:17:30 +05:30
parent d193ec0f11
commit 3d28adbe68
6 changed files with 112 additions and 77 deletions

View File

@ -69,7 +69,7 @@ public class RegistrationController {
} else { } else {
logger.warn("Registration captcha equal fail"); logger.warn("Registration captcha equal fail");
} }
// userService.registerUser(userRegistrationDTO); userService.registerUser(userRegistrationDTO);
return "user/home"; return "user/home";
} }

View File

@ -25,11 +25,18 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity @Entity
@Table(name = "users") @Table(name = "users")
@EntityListeners(AuditingEntityListener.class) @EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = { "password" }, allowGetters = false) @JsonIgnoreProperties(value = { "password" }, allowGetters = false)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ChatUser { public class ChatUser {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@ -47,56 +54,4 @@ public class ChatUser {
@JsonBackReference @JsonBackReference
// private Set<UserRole> userRoles = new HashSet<UserRole>(); // private Set<UserRole> userRoles = new HashSet<UserRole>();
private Set<UserRole> userRoles; private Set<UserRole> userRoles;
public int getUserID() {
return userID;
}
public void setUserID(int userID) {
this.userID = userID;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getJoinDate() {
return joinDate;
}
public void setJoinDate(Date joinDate) {
this.joinDate = joinDate;
}
public ChatUser(int userID, String userName, String password, Date joinDate) {
super();
this.userID = userID;
this.userName = userName;
this.password = password;
this.joinDate = joinDate;
}
public ChatUser() {
}
public Set<UserRole> getUserRoles() {
return userRoles;
}
public void setUserRoles(Set<UserRole> userRoles) {
this.userRoles = userRoles;
}
} }

View File

@ -12,9 +12,12 @@ import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonBackReference;
import lombok.Data;
@Entity @Entity
@Table(name = "roles") @Table(name = "roles")
@Data
public class Role { public class Role {
@Id @Id
@Column(name = "role_id") @Column(name = "role_id")
@ -25,23 +28,4 @@ public class Role {
@OneToMany(mappedBy = "role", cascade = CascadeType.ALL) @OneToMany(mappedBy = "role", cascade = CascadeType.ALL)
@JsonBackReference @JsonBackReference
private Set<UserRole> userRoles = new HashSet<>(); private Set<UserRole> userRoles = new HashSet<>();
public int getRoleId() {
return roleID;
}
public void setRoleId(int id) {
this.roleID = id;
}
public String getName() {
return name;
}
public void setName(String role) {
this.name = role;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
} }

View File

@ -5,13 +5,14 @@ import java.util.List;
import org.ros.chatto.dto.ActiveUserDTO; import org.ros.chatto.dto.ActiveUserDTO;
import org.ros.chatto.dto.UserRegistrationDTO; import org.ros.chatto.dto.UserRegistrationDTO;
import org.ros.chatto.model.ChatUser; import org.ros.chatto.model.ChatUser;
import org.ros.chatto.model.UserRole;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public interface UserService { public interface UserService {
public void saveChatUser(ChatUser user); public void saveChatUser(ChatUser user);
public List<String> findAllOtherUsers(String userName); public List<String> findAllOtherUsers(String userName);
public void registerUser(UserRegistrationDTO userRegistrationDTO); public UserRole registerUser(UserRegistrationDTO userRegistrationDTO);
public List<String> getAllRegularUsers(); public List<String> getAllRegularUsers();
public ChatUser findByUserName(String userName); public ChatUser findByUserName(String userName);
public List<ActiveUserDTO> getOtherActiveUsers(String userName); public List<ActiveUserDTO> getOtherActiveUsers(String userName);

View File

@ -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.UserRepositoryCustom; import org.ros.chatto.repository.UserRepositoryCustom;
import org.ros.chatto.repository.UserRoleRepository; import org.ros.chatto.repository.UserRoleRepository;
@ -34,6 +35,9 @@ public class UserServiceImpl implements UserService {
@Autowired @Autowired
private PasswordEncoder passwordEncoder; private PasswordEncoder passwordEncoder;
@Autowired
private RoleRepository roleRepository;
@Autowired @Autowired
private RoleService roleService; private RoleService roleService;
@ -54,7 +58,7 @@ public class UserServiceImpl implements UserService {
} }
@Override @Override
public void registerUser(UserRegistrationDTO userRegistrationDTO) { public UserRole registerUser(UserRegistrationDTO userRegistrationDTO) {
ChatUser user = new ChatUser(); ChatUser user = new ChatUser();
user.setUserName(userRegistrationDTO.getUserName()); user.setUserName(userRegistrationDTO.getUserName());
user.setPassword(passwordEncoder.encode(userRegistrationDTO.getPassword())); user.setPassword(passwordEncoder.encode(userRegistrationDTO.getPassword()));
@ -63,9 +67,10 @@ public class UserServiceImpl implements UserService {
Role role = roleService.getRole("USER"); Role role = roleService.getRole("USER");
userRole.setRole(role); userRole.setRole(role);
userRole.setUser(changedUser); userRole.setUser(changedUser);
System.out.println(role.getRoleId()); // System.out.println(role.getRoleID());
System.out.println(changedUser.getUserID()); // System.out.println(changedUser.getUserID());
userRoleRepository.save(userRole); userRoleRepository.save(userRole);
return userRole;
} }
@Override @Override

View File

@ -0,0 +1,90 @@
package org.ros.chatto;
import static org.junit.Assert.assertArrayEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.ros.chatto.dto.UserRegistrationDTO;
import org.ros.chatto.model.ChatUser;
import org.ros.chatto.model.Role;
import org.ros.chatto.model.UserRole;
import org.ros.chatto.repository.UserRepository;
import org.ros.chatto.repository.UserRepositoryCustom;
import org.ros.chatto.repository.UserRoleRepository;
import org.ros.chatto.repository.UserSessionRepository;
import org.ros.chatto.service.RoleService;
import org.ros.chatto.service.UserService;
import org.ros.chatto.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import lombok.extern.slf4j.Slf4j;
@SpringBootTest
@RunWith(MockitoJUnitRunner.class)
@Slf4j
public class UnitTest {
@InjectMocks
private UserService userService = new UserServiceImpl();
// private RoleService roleService;
// private UserTokenService userTokenService;
@Mock
private UserRoleRepository userRoleRepository;
@Mock
private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
@Mock
private RoleService roleService;
@Mock
private UserRepositoryCustom userRepositoryCustom;
@Mock
private UserSessionRepository userSessionRepository;
@Mock
private UserRepository userRepository;
// private ChatUser chatUser;
@Before
public void setupMock() {
// userRepository = mock(UserRepository.class);
// chatUser = mock(ChatUser.class);
}
@Test
public void userRegistrationTest() {
UserRegistrationDTO userRegistrationDTO = new UserRegistrationDTO();
userRegistrationDTO.setUserName("mickey");
userRegistrationDTO.setPassword("mouse");
ChatUser chatUser = new ChatUser();
chatUser.setUserName("mickey");
// chatUser.setPassword("mouse");
Role role = new Role();
role.setRoleID(2);
role.setName("USER");
when(roleService.getRole("USER")).thenReturn(role);
when(userRepository.save(chatUser)).thenReturn(chatUser);
UserRole userRole = userService.registerUser(userRegistrationDTO);
assertArrayEquals(new Object[] { 2, "USER","mickey" },
new Object[] { userRole.getRole().getRoleID(),
userRole.getRole().getName(), userRole.getUser().getUserName() });
verify(userRepository, times(1)).save(chatUser);
verify(userRoleRepository,times(1)).save(userRole);
}
}