### Question:
RangerPolicyValidator extends RangerValidator { boolean isValidPolicyItemAccess(RangerPolicyItemAccess access, List<ValidationFailureDetails> failures, Set<String> accessTypes) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerPolicyValidator.isValidPolicyItemAccess(%s, %s, %s)", access, failures, accessTypes)); } boolean valid = true; if (CollectionUtils.isEmpty(accessTypes)) { LOG.debug("isValidPolicyItemAccess: accessTypes was null!"); } else if (access == null) { LOG.debug("isValidPolicyItemAccess: policy item access was null!"); } else { String accessType = access.getType(); if (StringUtils.isBlank(accessType)) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_MISSING_FIELD; failures.add(new ValidationFailureDetailsBuilder() .field("policy item access type") .isMissing() .becauseOf(error.getMessage("policy item access type")) .errorCode(error.getErrorCode()) .build()); valid = false; } else if (!accessTypes.contains(accessType.toLowerCase())) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_POLICY_ITEM_ACCESS_TYPE_INVALID; failures.add(new ValidationFailureDetailsBuilder() .field("policy item access type") .isSemanticallyIncorrect() .becauseOf(error.getMessage(accessType, accessTypes)) .errorCode(error.getErrorCode()) .build()); valid = false; } Boolean isAllowed = access.getIsAllowed(); if (isAllowed != null && isAllowed == false) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_POLICY_ITEM_ACCESS_TYPE_DENY; failures.add(new ValidationFailureDetailsBuilder() .field("policy item access type allowed") .isSemanticallyIncorrect() .becauseOf(error.getMessage()) .errorCode(error.getErrorCode()) .build()); valid = false; } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerPolicyValidator.isValidPolicyItemAccess(%s, %s, %s): %s", access, failures, accessTypes, valid)); } return valid; } RangerPolicyValidator(ServiceStore store); void validate(RangerPolicy policy, Action action, boolean isAdmin);  }### Answer:
@Test public void test_isValidPolicyItemAccess_happyPath() { RangerPolicyItemAccess access = mock(RangerPolicyItemAccess.class); when(access.getType()).thenReturn("an-Access"); Set<String> validAccesses = Sets.newHashSet(new String[] { "an-access", "another-access" }); for (Boolean allowed : new Boolean[] { null, true } ) { when(access.getIsAllowed()).thenReturn(allowed); assertTrue(_validator.isValidPolicyItemAccess(access, _failures, validAccesses)); assertTrue(_failures.isEmpty()); } }
@Test public void test_isValidPolicyItemAccess_failures() { Set<String> validAccesses = Sets.newHashSet(new String[] { "anAccess", "anotherAccess" }); RangerPolicyItemAccess access = mock(RangerPolicyItemAccess.class); when(access.getIsAllowed()).thenReturn(null); for (String type : new String[] { null, " "}) { when(access.getType()).thenReturn(type); assertTrue(_validator.isValidPolicyItemAccess(access, _failures, null)); assertTrue(_validator.isValidPolicyItemAccess(access, _failures, new HashSet<String>())); _failures.clear(); assertFalse(_validator.isValidPolicyItemAccess(access, _failures, validAccesses)); _utils.checkFailureForMissingValue(_failures, "policy item access type"); } when(access.getType()).thenReturn("anAccess"); when(access.getIsAllowed()).thenReturn(false); _failures.clear();assertFalse(_validator.isValidPolicyItemAccess(access, _failures, validAccesses)); _utils.checkFailureForSemanticError(_failures, "policy item access type allowed"); when(access.getType()).thenReturn("newAccessType"); _failures.clear(); assertFalse(_validator.isValidPolicyItemAccess(access, _failures, validAccesses)); _utils.checkFailureForSemanticError(_failures, "policy item access type"); }