UserCreationServiceImpl implements UserCreationService { @Override public UserActivationResponseDto activateUser(UserActivationRequestDto userActivationRequest, String xForwardedProto, String xForwardedHost, int xForwardedPort) { assertPasswordAndConfirmPassword(userActivationRequest); final UserCreation userCreation = userCreationRepository.findOneByEmailTokenAndVerificationCode( userActivationRequest.getEmailToken(), userActivationRequest.getVerificationCode()) .orElseThrow(UserActivationCannotBeVerifiedException::new); assertNotAlreadyVerified(userCreation); assertEmailTokenNotExpired(userCreation); final PatientDto patientProfile = phrService.findPatientProfileById(userCreation.getPatientId(), true); assertUsernameAndPatientEmailMatch(userActivationRequest, patientProfile); assertBirthDateVerification(userActivationRequest, patientProfile); userCreation.setVerified(true); userCreationRepository.save(userCreation); final UserActivationResponseDto response = modelMapper.map(patientProfile, UserActivationResponseDto.class); response.setBirthDate(patientProfile.getBirthDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()); response.setVerified(userCreation.isVerified()); ScimUser scimUser = new ScimUser(null, patientProfile.getEmail(), patientProfile.getFirstName(), patientProfile.getLastName()); scimUser.setPassword(userActivationRequest.getPassword()); ScimUser.Email email = new ScimUser.Email(); email.setValue(patientProfile.getEmail()); scimUser.setEmails(Collections.singletonList(email)); scimUser.setVerified(true); final ScimUser savedScimUser = scimService.save(scimUser); final String userId = savedScimUser.getId(); Assert.hasText(userId, "SCIM userId must have text"); userCreation.setUserId(userId); userCreationRepository.save(userCreation); scimService.addUserToGroups(userCreation); emailSender.sendEmailToConfirmVerification( xForwardedProto, xForwardedHost, xForwardedPort, patientProfile.getEmail(), getRecipientFullName(patientProfile)); return response; }  @Override @Transactional UserCreationResponseDto initiateUserCreation(UserCreationRequestDto userCreationRequest, String xForwardedProto, String xForwardedHost, int xForwardedPort); @Override @Transactional(readOnly = true) UserCreationResponseDto findUserCreationInfoByPatientId(Long patientId); @Override UserActivationResponseDto activateUser(UserActivationRequestDto userActivationRequest, String xForwardedProto, String xForwardedHost, int xForwardedPort); @Override @Transactional(readOnly = true) VerificationResponseDto verify(String emailToken, Optional<String> verificationCode, Optional<LocalDate> birthDate); ScopeAssignmentResponseDto assignScopeToUser(ScopeAssignmentRequestDto scopeAssignmentRequestDto);  }
@Test public void testActivateUser() throws Exception { final Long patientId = 10L; final Boolean initialVerified = false; final Boolean verified = true; final Instant emailTokenExpiration = Instant.now().plus(Period.ofDays(7)); final String verificationCode = "verificationCode"; final String emailToken = "emailToken"; final String firstName = "firstName"; final String lastName = "lastName"; final String email = "email"; final String username = "email"; final String password = "password"; final String confirmPassword = "password"; final String userId = "userId"; final LocalDate birthDateInRequest = LocalDate.of(1980, 1, 1); final Date birthDateInPhr = Date.from(birthDateInRequest.atStartOfDay(ZoneId.systemDefault()).toInstant()); UserActivationRequestDto userActivationRequest = mock(UserActivationRequestDto.class); when(userActivationRequest.getVerificationCode()).thenReturn(verificationCode); when(userActivationRequest.getEmailToken()).thenReturn(emailToken); when(userActivationRequest.getBirthDate()).thenReturn(birthDateInRequest); when(userActivationRequest.getPassword()).thenReturn(password); when(userActivationRequest.getConfirmPassword()).thenReturn(confirmPassword); when(userActivationRequest.getUsername()).thenReturn(username); final UserCreation userCreation = mock(UserCreation.class); when(userCreation.isVerified()).thenReturn(initialVerified).thenReturn(verified); when(userCreation.getEmailTokenExpirationAsInstant()).thenReturn(emailTokenExpiration); when(userCreation.getPatientId()).thenReturn(patientId); when(userCreationRepository.findOneByEmailTokenAndVerificationCode(emailToken, verificationCode)).thenReturn(Optional.of(userCreation)); final PatientDto patientDto = mock(PatientDto.class); when(patientDto.getBirthDate()).thenReturn(birthDateInPhr); when(patientDto.getFirstName()).thenReturn(firstName); when(patientDto.getLastName()).thenReturn(lastName); when(patientDto.getEmail()).thenReturn(email); when(phrService.findPatientProfileById(patientId, true)).thenReturn(patientDto); final UserActivationResponseDto userActivationResponse = mock(UserActivationResponseDto.class); when(modelMapper.map(patientDto, UserActivationResponseDto.class)).thenReturn(userActivationResponse); final ScimUser savedScimUser = new ScimUser(); savedScimUser.setId(userId); when(scimService.save(argThat(matching( user -> user.getEmails().get(0).getValue().equals(email) && user.getUserName().equals(email) && user.isVerified() == verified && user.getFamilyName().equals(lastName) && user.getGivenName().equals(firstName) )))).thenReturn(savedScimUser); final UserActivationResponseDto response = sut.activateUser(userActivationRequest, xForwardedProto, xForwardedHost, xForwardedPort); assertEquals(userActivationResponse, response); verify(userCreation, times(1)).setVerified(verified); verify(userCreation, times(1)).setUserId(userId); verify(userActivationResponse, times(1)).setVerified(verified); verify(userCreationRepository, times(2)).save(userCreation); verify(scimService, times(1)).save(argThat(matching( user -> user.getEmails().get(0).getValue().equals(email) && user.getUserName().equals(email) && user.isVerified() == true && user.getFamilyName().equals(lastName) && user.getGivenName().equals(firstName) ))); verify(scimService, times(1)).addUserToGroups(userCreation); verify(emailSender, times(1)).sendEmailToConfirmVerification( eq(xForwardedProto), eq(xForwardedHost), eq(xForwardedPort), eq(email), argThat(matching(fn -> fn.contains(firstName) && fn.contains(lastName)))); }