diff --git a/chatto/src/main/java/org/ros/chatto/controller/RegistrationController.java b/chatto/src/main/java/org/ros/chatto/controller/RegistrationController.java index c01761f..2ae00df 100644 --- a/chatto/src/main/java/org/ros/chatto/controller/RegistrationController.java +++ b/chatto/src/main/java/org/ros/chatto/controller/RegistrationController.java @@ -69,7 +69,7 @@ public class RegistrationController { } else { logger.warn("Registration captcha equal fail"); } -// userService.registerUser(userRegistrationDTO); + userService.registerUser(userRegistrationDTO); return "user/home"; } diff --git a/chatto/src/main/java/org/ros/chatto/model/ChatUser.java b/chatto/src/main/java/org/ros/chatto/model/ChatUser.java index 6bc48e0..a547fc9 100644 --- a/chatto/src/main/java/org/ros/chatto/model/ChatUser.java +++ b/chatto/src/main/java/org/ros/chatto/model/ChatUser.java @@ -25,11 +25,18 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener; import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + @Entity @Table(name = "users") @EntityListeners(AuditingEntityListener.class) @JsonIgnoreProperties(value = { "password" }, allowGetters = false) +@Data +@AllArgsConstructor +@NoArgsConstructor public class ChatUser { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -47,56 +54,4 @@ public class ChatUser { @JsonBackReference // private Set userRoles = new HashSet(); private Set 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 getUserRoles() { - return userRoles; - } - - public void setUserRoles(Set userRoles) { - this.userRoles = userRoles; - } - } diff --git a/chatto/src/main/java/org/ros/chatto/model/Role.java b/chatto/src/main/java/org/ros/chatto/model/Role.java index ce2d831..d18e285 100644 --- a/chatto/src/main/java/org/ros/chatto/model/Role.java +++ b/chatto/src/main/java/org/ros/chatto/model/Role.java @@ -12,9 +12,12 @@ import javax.persistence.Table; import com.fasterxml.jackson.annotation.JsonBackReference; +import lombok.Data; + @Entity @Table(name = "roles") +@Data public class Role { @Id @Column(name = "role_id") @@ -25,23 +28,4 @@ public class Role { @OneToMany(mappedBy = "role", cascade = CascadeType.ALL) @JsonBackReference private Set 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; - } - } diff --git a/chatto/src/main/java/org/ros/chatto/service/UserService.java b/chatto/src/main/java/org/ros/chatto/service/UserService.java index e218ab8..6785fc2 100644 --- a/chatto/src/main/java/org/ros/chatto/service/UserService.java +++ b/chatto/src/main/java/org/ros/chatto/service/UserService.java @@ -5,13 +5,14 @@ import java.util.List; import org.ros.chatto.dto.ActiveUserDTO; import org.ros.chatto.dto.UserRegistrationDTO; import org.ros.chatto.model.ChatUser; +import org.ros.chatto.model.UserRole; import org.springframework.stereotype.Service; @Service public interface UserService { public void saveChatUser(ChatUser user); public List findAllOtherUsers(String userName); - public void registerUser(UserRegistrationDTO userRegistrationDTO); + public UserRole registerUser(UserRegistrationDTO userRegistrationDTO); public List getAllRegularUsers(); public ChatUser findByUserName(String userName); public List getOtherActiveUsers(String userName); diff --git a/chatto/src/main/java/org/ros/chatto/service/UserServiceImpl.java b/chatto/src/main/java/org/ros/chatto/service/UserServiceImpl.java index a01de76..afd1898 100644 --- a/chatto/src/main/java/org/ros/chatto/service/UserServiceImpl.java +++ b/chatto/src/main/java/org/ros/chatto/service/UserServiceImpl.java @@ -13,6 +13,7 @@ import org.ros.chatto.model.ChatUser; import org.ros.chatto.model.Role; import org.ros.chatto.model.UserRole; import org.ros.chatto.model.UserSession; +import org.ros.chatto.repository.RoleRepository; import org.ros.chatto.repository.UserRepository; import org.ros.chatto.repository.UserRepositoryCustom; import org.ros.chatto.repository.UserRoleRepository; @@ -33,7 +34,10 @@ public class UserServiceImpl implements UserService { @Autowired private PasswordEncoder passwordEncoder; - + + @Autowired + private RoleRepository roleRepository; + @Autowired private RoleService roleService; @@ -54,7 +58,7 @@ public class UserServiceImpl implements UserService { } @Override - public void registerUser(UserRegistrationDTO userRegistrationDTO) { + public UserRole registerUser(UserRegistrationDTO userRegistrationDTO) { ChatUser user = new ChatUser(); user.setUserName(userRegistrationDTO.getUserName()); user.setPassword(passwordEncoder.encode(userRegistrationDTO.getPassword())); @@ -63,9 +67,10 @@ public class UserServiceImpl implements UserService { Role role = roleService.getRole("USER"); userRole.setRole(role); userRole.setUser(changedUser); - System.out.println(role.getRoleId()); - System.out.println(changedUser.getUserID()); +// System.out.println(role.getRoleID()); +// System.out.println(changedUser.getUserID()); userRoleRepository.save(userRole); + return userRole; } @Override diff --git a/chatto/src/test/java/org/ros/chatto/UnitTest.java b/chatto/src/test/java/org/ros/chatto/UnitTest.java new file mode 100644 index 0000000..fb8d443 --- /dev/null +++ b/chatto/src/test/java/org/ros/chatto/UnitTest.java @@ -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); + } + +}