### Question:
ChangedPasswordValidator implements ConstraintValidator<ChangedPassword, UserSecurityDto> { @Override public boolean isValid(UserSecurityDto dto, ConstraintValidatorContext context) { JCUser currentUser = userService.getCurrentUser(); String editedUserName = getUsername(dto.getUserId()); String currentUserName = currentUser.getUsername(); boolean isWillBeChangedByOwner = ObjectUtils.equals(editedUserName, currentUserName); if (isWillBeChangedByOwner) { boolean result = dto.getNewUserPassword() == null; String enteredCurrentPassword = encryptionService.encryptPassword(dto.getCurrentUserPassword()); result |= currentUser.getPassword().equals(enteredCurrentPassword); if (!result) { context.buildConstraintViolationWithTemplate(message) .addNode("currentUserPassword") .addConstraintViolation(); } return result; } return true; } @Autowired  ChangedPasswordValidator(UserService userService, EncryptionService encryptionService); @Override void initialize(ChangedPassword constraintAnnotation); @Override boolean isValid(UserSecurityDto dto, ConstraintValidatorContext context);  }### Answer:
@Test public void editedByModeratorShouldNotCheckCurrentPassword() throws NotFoundException { userSecurityDto.setUserId(2L); when(userService.get(2L)).thenReturn(new JCUser("moderator", "email", userCurrentPassword)); boolean isValid = validator.isValid(userSecurityDto, validatorContext); assertTrue(isValid, "If moderator edits user's profile, we mustn't check current password."); }
@Test public void editedNotExistingUserShouldNotCheckPassword() throws NotFoundException { when(userService.get(1L)).thenThrow(new NotFoundException()); boolean isValid = validator.isValid(userSecurityDto, validatorContext); assertTrue(isValid, "If someone try to edit not existing user's profile, we shouldn't check current password."); }
@Test public void editedByOwnerWithNewPasswordAsNullShouldBeValid() { userSecurityDto.setNewUserPassword(null); boolean isValid = validator.isValid(userSecurityDto, validatorContext); assertTrue(isValid, "The null password is not valid."); }
@Test public void editedByOwnerWithCorrectCurrentPasswordShouldBeValid() { String currentUserPassword = userSecurityDto.getCurrentUserPassword(); when(encryptionService.encryptPassword(currentUserPassword)). thenReturn(currentUserPassword); boolean isValid = validator.isValid(userSecurityDto, validatorContext); assertTrue(isValid, "The old password is correct, but the check fails."); }
@Test public void editedByOwnerWithIncorrectCurrentPasswordShouldNotBeValid() { String incorrectCurrentPassword = "other_password"; userSecurityDto.setCurrentUserPassword(incorrectCurrentPassword); when(encryptionService.encryptPassword(incorrectCurrentPassword)). thenReturn(incorrectCurrentPassword); when(validatorContext.buildConstraintViolationWithTemplate(null)). thenReturn(violationBuilder); when(violationBuilder.addNode(anyString())). thenReturn(nodeBuilderDefinedContext); boolean isValid = validator.isValid(userSecurityDto, validatorContext); assertFalse(isValid, "The old password isn't correct, but the check passed."); verify(validatorContext).buildConstraintViolationWithTemplate(null); }