### Question:
RangerPolicyValidator extends RangerValidator { boolean isValidPolicyItem(RangerPolicyItem policyItem, List<ValidationFailureDetails> failures, RangerServiceDef serviceDef) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerPolicyValidator.isValid(%s, %s, %s)", policyItem, failures, serviceDef)); } boolean valid = true; if (policyItem == null) { LOG.debug("policy item was null!"); } else { if (CollectionUtils.isEmpty(policyItem.getAccesses())) { if (!Boolean.TRUE.equals(policyItem.getDelegateAdmin())) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_MISSING_FIELD; failures.add(new ValidationFailureDetailsBuilder() .field("policy item accesses") .isMissing() .becauseOf(error.getMessage("policy item accesses")) .errorCode(error.getErrorCode()) .build()); valid = false; } else { LOG.debug("policy item collection was null but delegated admin is true. Ok"); } } else { valid = isValidItemAccesses(policyItem.getAccesses(), failures, serviceDef) && valid; } if (CollectionUtils.isEmpty(policyItem.getUsers()) && CollectionUtils.isEmpty(policyItem.getGroups())) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_MISSING_USER_AND_GROUPS; failures.add(new ValidationFailureDetailsBuilder() .field("policy item users/user-groups") .isMissing() .becauseOf(error.getMessage()) .errorCode(error.getErrorCode()) .build()); valid = false; } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerPolicyValidator.isValid(%s, %s, %s): %s", policyItem, failures, serviceDef, valid)); } return valid; } RangerPolicyValidator(ServiceStore store); void validate(RangerPolicy policy, Action action, boolean isAdmin);  }### Answer:
@Test public void test_isValidPolicyItem_failures() { RangerPolicyItem policyItem = mock(RangerPolicyItem.class); when(policyItem.getAccesses()).thenReturn(null); _failures.clear(); assertFalse(_validator.isValidPolicyItem(policyItem, _failures, _serviceDef)); _utils.checkFailureForMissingValue(_failures, "policy item accesses"); List<RangerPolicyItemAccess> accesses = new ArrayList<RangerPolicy.RangerPolicyItemAccess>(); when(policyItem.getAccesses()).thenReturn(accesses); _failures.clear(); assertFalse(_validator.isValidPolicyItem(policyItem, _failures, _serviceDef)); _utils.checkFailureForMissingValue(_failures, "policy item accesses"); RangerPolicyItemAccess access = mock(RangerPolicyItemAccess.class); accesses.add(access); when(policyItem.getUsers()).thenReturn(null); when(policyItem.getGroups()).thenReturn(new ArrayList<String>()); _failures.clear(); assertFalse(_validator.isValidPolicyItem(policyItem, _failures, _serviceDef)); _utils.checkFailureForMissingValue(_failures, "policy item users/user-groups"); }
@Test public void test_isValidPolicyItem_happPath() { RangerPolicyItem policyItem = mock(RangerPolicyItem.class); when(policyItem.getAccesses()).thenReturn(null); when(policyItem.getDelegateAdmin()).thenReturn(true); List<String> users = Arrays.asList("user1"); when(policyItem.getUsers()).thenReturn(users); _failures.clear(); assertTrue(_validator.isValidPolicyItem(policyItem, _failures, _serviceDef)); assertTrue(_failures.isEmpty()); }