@Test public final void testIsValid_failures() throws Exception { for (Action action : cu) { _policy = null; checkFailure_isValid(action, "missing", "policy"); _policy = mock(RangerPolicy.class); for (String name : new String[] { null, " " }) { when(_policy.getName()).thenReturn(name); when(_policy.getResources()).thenReturn(null); checkFailure_isValid(action, "missing", "name"); } if (action == Action.UPDATE) { when(_policy.getId()).thenReturn(null); checkFailure_isValid(action, "missing", "id"); } } RangerService service = mock(RangerService.class); when(_policy.getName()).thenReturn("policy-name"); when(_policy.getService()).thenReturn("service-name"); when(_store.getServiceByName("service-name")).thenReturn(service); when(service.getId()).thenReturn(2L); RangerPolicy existingPolicy = mock(RangerPolicy.class); when(existingPolicy.getId()).thenReturn(7L); when(existingPolicy.getService()).thenReturn("service-name"); List<RangerPolicy> existingPolicies = new ArrayList<>(); when(_store.getPolicyId(service.getId(), "policy-name", _zoneId)).thenReturn(7L); checkFailure_isValid(Action.CREATE, "semantic", "policy name"); when(_policy.getId()).thenReturn(7L); when(_store.getPolicy(7L)).thenReturn(null); checkFailure_isValid(Action.UPDATE, "semantic", "id"); when(_store.getPolicy(7L)).thenReturn(existingPolicy); RangerPolicy anotherExistingPolicy = mock(RangerPolicy.class); when(anotherExistingPolicy.getId()).thenReturn(8L); when(anotherExistingPolicy.getService()).thenReturn("service-name"); existingPolicies.add(anotherExistingPolicy); when(_store.getPolicyId(service.getId(), "policy-name", _zoneId)).thenReturn(8L); checkFailure_isValid(Action.UPDATE, "semantic", "id/name"); when(_policy.getName()).thenReturn("policy-name"); for (Action action : cu) { for (boolean isAdmin : new boolean[] { true, false }) { when(_policy.getService()).thenReturn(null); _failures.clear(); Assert.assertFalse(_validator.isValid(_policy, action, isAdmin, _failures)); _utils.checkFailureForMissingValue(_failures, "service name"); when(_policy.getService()).thenReturn(""); _failures.clear(); Assert.assertFalse(_validator.isValid(_policy, action, isAdmin, _failures)); _utils.checkFailureForMissingValue(_failures, "service name"); } } when(_store.getServiceByName("service-name")).thenReturn(null); when(_store.getServiceByName("another-service-name")).thenThrow(new Exception()); for (Action action : cu) { for (boolean isAdmin : new boolean[] { true, false }) { when(_policy.getService()).thenReturn(null); _failures.clear(); Assert.assertFalse(_validator.isValid(_policy, action, isAdmin, _failures)); _utils.checkFailureForMissingValue(_failures, "service name"); when(_policy.getService()).thenReturn(null); _failures.clear(); Assert.assertFalse(_validator.isValid(_policy, action, isAdmin, _failures)); _utils.checkFailureForMissingValue(_failures, "service name"); when(_policy.getService()).thenReturn("service-name"); _failures.clear(); Assert.assertFalse(_validator.isValid(_policy, action, isAdmin, _failures)); _utils.checkFailureForSemanticError(_failures, "service name"); when(_policy.getService()).thenReturn("another-service-name"); _failures.clear(); Assert.assertFalse(_validator.isValid(_policy, action, isAdmin, _failures)); _utils.checkFailureForSemanticError(_failures, "service name"); } } List<RangerPolicyItem> policyItems = new ArrayList<>(); for (Action action : cu) { for (boolean isAdmin : new boolean[] { true, false }) { when(_policy.getPolicyItems()).thenReturn(null); _failures.clear(); Assert.assertFalse(_validator.isValid(_policy, action, isAdmin, _failures)); _utils.checkFailureForMissingValue(_failures, "policy items"); when(_policy.getPolicyItems()).thenReturn(policyItems); _failures.clear(); Assert.assertFalse(_validator.isValid(_policy, action, isAdmin, _failures)); _utils.checkFailureForMissingValue(_failures, "policy items"); } } policyItems = _utils.createPolicyItems(policyItemsData); when(_policy.getPolicyItems()).thenReturn(policyItems); when(service.getType()).thenReturn("service-type"); when(_store.getServiceDefByName("service-type")).thenReturn(null); for (Action action : cu) { for (boolean isAdmin : new boolean[] { true, false }) { when(_policy.getService()).thenReturn("service-name"); when(_store.getServiceByName("service-name")).thenReturn(service); _failures.clear(); Assert.assertFalse(_validator.isValid(_policy, action, isAdmin, _failures)); _utils.checkFailureForInternalError(_failures, "policy service def"); } } _serviceDef = _utils.createServiceDefWithAccessTypes(accessTypes_bad, "service-type"); when(_store.getServiceDefByName("service-type")).thenReturn(_serviceDef); for (Action action : cu) { for (boolean isAdmin : new boolean[] { true, false }) { _failures.clear(); Assert.assertFalse(_validator.isValid(_policy, action, isAdmin, _failures)); _utils.checkFailureForSemanticError(_failures, "policy item access type"); } } _serviceDef = _utils.createServiceDefWithAccessTypes(accessTypes, "service-type"); when(_store.getPolicyId(service.getId(), "policy-name", _zoneId)).thenReturn(null); List<RangerResourceDef> resourceDefs = _utils.createResourceDefs(resourceDefData); when(_serviceDef.getResources()).thenReturn(resourceDefs); when(_store.getServiceDefByName("service-type")).thenReturn(_serviceDef); Map<String, RangerPolicyResource> policyResources = _utils.createPolicyResourceMap(policyResourceMap_bad); when(_policy.getResources()).thenReturn(policyResources); RangerPolicyResourceSignature signature = mock(RangerPolicyResourceSignature.class); when(_factory.createPolicyResourceSignature(_policy)).thenReturn(signature); when(signature.getSignature()).thenReturn("hash-1"); when(_store.getPoliciesByResourceSignature("service-name", "hash-1", true)).thenReturn(null); for (Action action : cu) { for (boolean isAdmin : new boolean[] { true, false }) { _failures.clear(); Assert.assertFalse(_validator.isValid(_policy, action, isAdmin, _failures)); _utils.checkFailureForSemanticError(_failures, "resource-values", "col"); _utils.checkFailureForSemanticError(_failures, "isRecursive", "db"); _utils.checkFailureForSemanticError(_failures, "isExcludes", "col"); } } when(_store.getPoliciesByResourceSignature("service-name", "hash-1", true)).thenReturn(existingPolicies); for (Action action : cu) { for (boolean isAdmin : new boolean[] { true, false }) { _failures.clear(); Assert.assertFalse(_validator.isValid(_policy, action, isAdmin, _failures)); _utils.checkFailureForSemanticError(_failures, "policy resources"); } } }
@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);  }