RangerSecurityZoneValidator extends RangerValidator { @Override boolean isValid(String name, Action action, List<ValidationFailureDetails> failures) { if (LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerPolicyValidator.isValid(%s, %s, %s)", name, action, failures)); } boolean ret = true; if (action != Action.DELETE) { ValidationErrorCode error = ValidationErrorCode.SECURITY_ZONE_VALIDATION_ERR_UNSUPPORTED_ACTION; failures.add(new ValidationFailureDetailsBuilder().isAnInternalError().becauseOf(error.getMessage()).errorCode(error.getErrorCode()).build()); ret = false; } else { if (StringUtils.isEmpty(name)) { ValidationErrorCode error = ValidationErrorCode.SECURITY_ZONE_VALIDATION_ERR_MISSING_FIELD; failures.add(new ValidationFailureDetailsBuilder().becauseOf("security zone name was null/missing").field("name").isMissing().errorCode(error.getErrorCode()).becauseOf(error.getMessage("name")).build()); ret = false; } else { if (getSecurityZone(name) == null) { ValidationErrorCode error = ValidationErrorCode.SECURITY_ZONE_VALIDATION_ERR_INVALID_ZONE_ID; failures.add(new ValidationFailureDetailsBuilder().becauseOf("security zone does not exist").field("name").errorCode(error.getErrorCode()).becauseOf(error.getMessage(name)).build()); ret = false; } } } if (LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerPolicyValidator.isValid(%s, %s, %s) : %s", name, action, failures, ret)); } return ret; } RangerSecurityZoneValidator(ServiceStore store, SecurityZoneStore securityZoneStore); void validate(RangerSecurityZone securityZone, Action action);  }
@Test public void testValidateSecurityZoneForDelete() throws Exception{ List<ValidationFailureDetails> failures = new ArrayList<ValidationFailureDetails>(); RangerSecurityZone suppliedSecurityZone = getRangerSecurityZone(); Mockito.when(_store.getSecurityZone(1L)).thenReturn(suppliedSecurityZone); rangerSecurityZoneValidator.isValid(1L, RangerValidator.Action.DELETE, failures); Mockito.verify(_store).getSecurityZone(1L); }
@Test public void testIsValidSecurityZoneForDeleteWithWrongActionTypeReturnFalse() throws Exception{ RangerSecurityZone suppliedSecurityZone = getRangerSecurityZone(); List<ValidationFailureDetails> failures = new ArrayList<ValidationFailureDetails>(); boolean isValid = rangerSecurityZoneValidator.isValid(suppliedSecurityZone.getName(), RangerValidator.Action.UPDATE, failures); Assert.assertFalse(isValid); }
@Test public void testIsValidSecurityZoneForDeleteWithoutNameReturnFalse() throws Exception{ RangerSecurityZone suppliedSecurityZone = new RangerSecurityZone(); suppliedSecurityZone.setName(null); List<ValidationFailureDetails> failures = new ArrayList<ValidationFailureDetails>(); boolean isValid = rangerSecurityZoneValidator.isValid(suppliedSecurityZone.getName(), RangerValidator.Action.DELETE, failures); Assert.assertFalse(isValid); }
@Test public void testIsValidSecurityZoneForDeleteWithWrongNameReturnFalse() throws Exception{ RangerSecurityZone suppliedSecurityZone = getRangerSecurityZone(); Mockito.when(_store.getSecurityZone(suppliedSecurityZone.getName())).thenReturn(null); List<ValidationFailureDetails> failures = new ArrayList<ValidationFailureDetails>(); boolean isValid = rangerSecurityZoneValidator.isValid(suppliedSecurityZone.getName(), RangerValidator.Action.DELETE, failures); Assert.assertFalse(isValid); }
@Test public void testIsValidSecurityZoneIdForDeleteWithWrongActionTypeReturnFalse() throws Exception{ RangerSecurityZone suppliedSecurityZone = getRangerSecurityZone(); List<ValidationFailureDetails> failures = new ArrayList<ValidationFailureDetails>(); boolean isValid = rangerSecurityZoneValidator.isValid(suppliedSecurityZone.getId(), RangerValidator.Action.UPDATE, failures); Assert.assertFalse(isValid); }
@Test public void testIsValidSecurityZoneForDeleteWithWrongIdReturnFalse() throws Exception{ RangerSecurityZone suppliedSecurityZone = getRangerSecurityZone(); Mockito.when(_store.getSecurityZone(suppliedSecurityZone.getId())).thenReturn(null); List<ValidationFailureDetails> failures = new ArrayList<ValidationFailureDetails>(); boolean isValid = rangerSecurityZoneValidator.isValid(suppliedSecurityZone.getId(), RangerValidator.Action.DELETE, failures); Assert.assertFalse(isValid); }