@Test public final void testIsValid_happyPath() throws Exception { when(_policy.getService()).thenReturn("service-name"); RangerService service = mock(RangerService.class); when(service.getType()).thenReturn("service-type"); when(service.getId()).thenReturn(2L); when(_store.getServiceByName("service-name")).thenReturn(service); _serviceDef = _utils.createServiceDefWithAccessTypes(accessTypes); when(_serviceDef.getName()).thenReturn("service-type"); when(_store.getServiceDefByName("service-type")).thenReturn(_serviceDef); when(_store.getPolicy(7L)).thenReturn(null); RangerPolicy existingPolicy = mock(RangerPolicy.class); when(existingPolicy.getId()).thenReturn(8L); when(existingPolicy.getService()).thenReturn("service-name"); when(_store.getPolicy(8L)).thenReturn(existingPolicy); when(_policy.getResources()).thenReturn(null); for (Action action : cu) { for (Boolean auditEnabled : new Boolean[] { null, true } ) { for (boolean isAdmin : new boolean[] { true, false }) { when(_policy.getIsAuditEnabled()).thenReturn(auditEnabled); if (action == Action.CREATE) { when(_policy.getId()).thenReturn(7L); when(_policy.getName()).thenReturn("policy-name-1"); when(_store.getPolicyId(service.getId(), _policy.getName(), _zoneId)).thenReturn(null); Assert.assertTrue("" + action + ", " + auditEnabled, _validator.isValid(_policy, action, isAdmin, _failures)); Assert.assertTrue(_failures.isEmpty()); } else { when(_policy.getId()).thenReturn(8L); when(_policy.getName()).thenReturn("policy-name-1"); Assert.assertTrue("" + action + ", " + auditEnabled, _validator.isValid(_policy, action, isAdmin, _failures)); Assert.assertTrue(_failures.isEmpty()); when(_policy.getName()).thenReturn("policy-name-2"); when(_store.getPolicyId(service.getId(), _policy.getName(), _zoneId)).thenReturn(null); Assert.assertTrue("" + action + ", " + auditEnabled, _validator.isValid(_policy, action, isAdmin, _failures)); Assert.assertTrue(_failures.isEmpty()); } } } } List<RangerPolicyItem> policyItems = _utils.createPolicyItems(policyItemsData); when(_policy.getPolicyItems()).thenReturn(policyItems); when(_policy.getIsAuditEnabled()).thenReturn(false); for (Action action : cu) { for (boolean isAdmin : new boolean[] { true, false}) { if (action == Action.CREATE) { when(_policy.getId()).thenReturn(7L); when(_policy.getName()).thenReturn("policy-name-1"); } else { when(_policy.getId()).thenReturn(8L); when(_policy.getName()).thenReturn("policy-name-2"); } Assert.assertTrue("" + action , _validator.isValid(_policy, action, isAdmin, _failures)); Assert.assertTrue(_failures.isEmpty()); } } List<RangerResourceDef> resourceDefs = _utils.createResourceDefs(resourceDefData); when(_serviceDef.getResources()).thenReturn(resourceDefs); Map<String, RangerPolicyResource> resourceMap = _utils.createPolicyResourceMap(policyResourceMap_good); when(_policy.getResources()).thenReturn(resourceMap); RangerPolicyResourceSignature policySignature = mock(RangerPolicyResourceSignature.class); when(_factory.createPolicyResourceSignature(_policy)).thenReturn(policySignature); when(policySignature.getSignature()).thenReturn("hash-1"); when(_store.getPoliciesByResourceSignature("service-name", "hash-1", true)).thenReturn(null); for (Action action : cu) { if (action == Action.CREATE) { when(_policy.getId()).thenReturn(7L); when(_policy.getName()).thenReturn("policy-name-1"); } else { when(_policy.getId()).thenReturn(8L); when(_policy.getName()).thenReturn("policy-name-2"); } Assert.assertTrue("" + action , _validator.isValid(_policy, action, true, _failures)); Assert.assertTrue(_failures.isEmpty()); } }
@Override boolean isValid(Long id, Action action, List<ValidationFailureDetails> failures) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerPolicyValidator.isValid(%s, %s, %s)", id, action, failures)); } boolean valid = true; if (action != Action.DELETE) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_UNSUPPORTED_ACTION; failures.add(new ValidationFailureDetailsBuilder() .isAnInternalError() .becauseOf(error.getMessage()) .errorCode(error.getErrorCode()) .build()); valid = false; } else if (id == null) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_MISSING_FIELD; failures.add(new ValidationFailureDetailsBuilder() .becauseOf("policy id was null/missing") .field("id") .isMissing() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage("id")) .build()); valid = false; } else if (policyExists(id)) { if (LOG.isDebugEnabled()) { LOG.debug("No policy found for id[" + id + "]! ok!"); } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerPolicyValidator.isValid(%s, %s, %s): %s", id, action, failures, valid)); } return valid; }
RangerPolicyValidator extends RangerValidator { @Override boolean isValid(Long id, Action action, List<ValidationFailureDetails> failures) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerPolicyValidator.isValid(%s, %s, %s)", id, action, failures)); } boolean valid = true; if (action != Action.DELETE) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_UNSUPPORTED_ACTION; failures.add(new ValidationFailureDetailsBuilder() .isAnInternalError() .becauseOf(error.getMessage()) .errorCode(error.getErrorCode()) .build()); valid = false; } else if (id == null) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_MISSING_FIELD; failures.add(new ValidationFailureDetailsBuilder() .becauseOf("policy id was null/missing") .field("id") .isMissing() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage("id")) .build()); valid = false; } else if (policyExists(id)) { if (LOG.isDebugEnabled()) { LOG.debug("No policy found for id[" + id + "]! ok!"); } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerPolicyValidator.isValid(%s, %s, %s): %s", id, action, failures, valid)); } return valid; } }
RangerPolicyValidator extends RangerValidator { @Override boolean isValid(Long id, Action action, List<ValidationFailureDetails> failures) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerPolicyValidator.isValid(%s, %s, %s)", id, action, failures)); } boolean valid = true; if (action != Action.DELETE) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_UNSUPPORTED_ACTION; failures.add(new ValidationFailureDetailsBuilder() .isAnInternalError() .becauseOf(error.getMessage()) .errorCode(error.getErrorCode()) .build()); valid = false; } else if (id == null) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_MISSING_FIELD; failures.add(new ValidationFailureDetailsBuilder() .becauseOf("policy id was null/missing") .field("id") .isMissing() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage("id")) .build()); valid = false; } else if (policyExists(id)) { if (LOG.isDebugEnabled()) { LOG.debug("No policy found for id[" + id + "]! ok!"); } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerPolicyValidator.isValid(%s, %s, %s): %s", id, action, failures, valid)); } return valid; } RangerPolicyValidator(ServiceStore store); }
RangerPolicyValidator extends RangerValidator { @Override boolean isValid(Long id, Action action, List<ValidationFailureDetails> failures) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerPolicyValidator.isValid(%s, %s, %s)", id, action, failures)); } boolean valid = true; if (action != Action.DELETE) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_UNSUPPORTED_ACTION; failures.add(new ValidationFailureDetailsBuilder() .isAnInternalError() .becauseOf(error.getMessage()) .errorCode(error.getErrorCode()) .build()); valid = false; } else if (id == null) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_MISSING_FIELD; failures.add(new ValidationFailureDetailsBuilder() .becauseOf("policy id was null/missing") .field("id") .isMissing() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage("id")) .build()); valid = false; } else if (policyExists(id)) { if (LOG.isDebugEnabled()) { LOG.debug("No policy found for id[" + id + "]! ok!"); } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerPolicyValidator.isValid(%s, %s, %s): %s", id, action, failures, valid)); } return valid; } RangerPolicyValidator(ServiceStore store); void validate(RangerPolicy policy, Action action, boolean isAdmin); }
RangerPolicyValidator extends RangerValidator { @Override boolean isValid(Long id, Action action, List<ValidationFailureDetails> failures) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerPolicyValidator.isValid(%s, %s, %s)", id, action, failures)); } boolean valid = true; if (action != Action.DELETE) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_UNSUPPORTED_ACTION; failures.add(new ValidationFailureDetailsBuilder() .isAnInternalError() .becauseOf(error.getMessage()) .errorCode(error.getErrorCode()) .build()); valid = false; } else if (id == null) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_MISSING_FIELD; failures.add(new ValidationFailureDetailsBuilder() .becauseOf("policy id was null/missing") .field("id") .isMissing() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage("id")) .build()); valid = false; } else if (policyExists(id)) { if (LOG.isDebugEnabled()) { LOG.debug("No policy found for id[" + id + "]! ok!"); } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerPolicyValidator.isValid(%s, %s, %s): %s", id, action, failures, valid)); } return valid; } RangerPolicyValidator(ServiceStore store); void validate(RangerPolicy policy, Action action, boolean isAdmin);  }