@Test public void userSignUpWithAnExistingUser() { Employee e = new Employee(); e.setUsername("username"); e.setPassword("password"); EmployeeSignUpRequest request = new EmployeeSignUpRequest(); request.setUsername("username"); request.setPassword("password"); request.setRole("ADMIN"); request.setFirstName("test"); request.setLastName("lastTest"); when(bCryptPasswordEncoder.encode(e.getPassword())).thenReturn(e.getPassword()); when(employeeRepo.findEmployeeByUsername(anyString())).thenReturn(e); GenericResponse response = operations.userSignUp(request); verify(employeeRepo, times(1)).findEmployeeByUsername(e.getUsername()); verify(employeeRepo, never()).save(e); assertFalse(response.isSuccess()); assertEquals(response.getMessage(), "User already exists"); assertEquals(response.getHttpStatus(), HttpStatus.BAD_REQUEST); }
public GenericResponse userSignUp(EmployeeSignUpRequest request) { request.validate(); Employee login = fromEmployeeSignUpRequest(request); GenericResponse response = new GenericResponse(); try { Employee userFound = employeeRepo.findEmployeeByUsername(login.getUsername()); if (userFound != null) { response.setSuccess(false); response.setMessage("User already exists"); response.setHttpStatus(HttpStatus.BAD_REQUEST); return response; } login.setPassword(encoder.encode(login.getPassword())); employeeRepo.save(login); } catch (Exception e) { e.printStackTrace(); response.setSuccess(false); response.setMessage("User Sign Up Failed"); response.setHttpStatus(HttpStatus.BAD_REQUEST); return response; } response.setSuccess(true); response.setMessage("User Sign Up Successful"); response.setHttpStatus(HttpStatus.CREATED); return response; }
UserOperations { public GenericResponse userSignUp(EmployeeSignUpRequest request) { request.validate(); Employee login = fromEmployeeSignUpRequest(request); GenericResponse response = new GenericResponse(); try { Employee userFound = employeeRepo.findEmployeeByUsername(login.getUsername()); if (userFound != null) { response.setSuccess(false); response.setMessage("User already exists"); response.setHttpStatus(HttpStatus.BAD_REQUEST); return response; } login.setPassword(encoder.encode(login.getPassword())); employeeRepo.save(login); } catch (Exception e) { e.printStackTrace(); response.setSuccess(false); response.setMessage("User Sign Up Failed"); response.setHttpStatus(HttpStatus.BAD_REQUEST); return response; } response.setSuccess(true); response.setMessage("User Sign Up Successful"); response.setHttpStatus(HttpStatus.CREATED); return response; } }
UserOperations { public GenericResponse userSignUp(EmployeeSignUpRequest request) { request.validate(); Employee login = fromEmployeeSignUpRequest(request); GenericResponse response = new GenericResponse(); try { Employee userFound = employeeRepo.findEmployeeByUsername(login.getUsername()); if (userFound != null) { response.setSuccess(false); response.setMessage("User already exists"); response.setHttpStatus(HttpStatus.BAD_REQUEST); return response; } login.setPassword(encoder.encode(login.getPassword())); employeeRepo.save(login); } catch (Exception e) { e.printStackTrace(); response.setSuccess(false); response.setMessage("User Sign Up Failed"); response.setHttpStatus(HttpStatus.BAD_REQUEST); return response; } response.setSuccess(true); response.setMessage("User Sign Up Successful"); response.setHttpStatus(HttpStatus.CREATED); return response; } UserOperations(EmployeeRepo employeeRepo, BCryptPasswordEncoder encoder); }
UserOperations { public GenericResponse userSignUp(EmployeeSignUpRequest request) { request.validate(); Employee login = fromEmployeeSignUpRequest(request); GenericResponse response = new GenericResponse(); try { Employee userFound = employeeRepo.findEmployeeByUsername(login.getUsername()); if (userFound != null) { response.setSuccess(false); response.setMessage("User already exists"); response.setHttpStatus(HttpStatus.BAD_REQUEST); return response; } login.setPassword(encoder.encode(login.getPassword())); employeeRepo.save(login); } catch (Exception e) { e.printStackTrace(); response.setSuccess(false); response.setMessage("User Sign Up Failed"); response.setHttpStatus(HttpStatus.BAD_REQUEST); return response; } response.setSuccess(true); response.setMessage("User Sign Up Successful"); response.setHttpStatus(HttpStatus.CREATED); return response; } UserOperations(EmployeeRepo employeeRepo, BCryptPasswordEncoder encoder); DataResponse<BasicEmployee> getAllUsers(); GenericResponse userSignUp(EmployeeSignUpRequest request); GenericResponse deleteUser(Integer id); GenericResponse changePassword(UserChangePasswordRequest request); GenericResponse updateUser(EmployeeUpdateRequest request); }
UserOperations { public GenericResponse userSignUp(EmployeeSignUpRequest request) { request.validate(); Employee login = fromEmployeeSignUpRequest(request); GenericResponse response = new GenericResponse(); try { Employee userFound = employeeRepo.findEmployeeByUsername(login.getUsername()); if (userFound != null) { response.setSuccess(false); response.setMessage("User already exists"); response.setHttpStatus(HttpStatus.BAD_REQUEST); return response; } login.setPassword(encoder.encode(login.getPassword())); employeeRepo.save(login); } catch (Exception e) { e.printStackTrace(); response.setSuccess(false); response.setMessage("User Sign Up Failed"); response.setHttpStatus(HttpStatus.BAD_REQUEST); return response; } response.setSuccess(true); response.setMessage("User Sign Up Successful"); response.setHttpStatus(HttpStatus.CREATED); return response; } UserOperations(EmployeeRepo employeeRepo, BCryptPasswordEncoder encoder); DataResponse<BasicEmployee> getAllUsers(); GenericResponse userSignUp(EmployeeSignUpRequest request); GenericResponse deleteUser(Integer id); GenericResponse changePassword(UserChangePasswordRequest request); GenericResponse updateUser(EmployeeUpdateRequest request);  }