### Question:
RangerServiceDefValidator extends RangerValidator { boolean isValidServiceDefName(String name, Long id, final Action action, final List<ValidationFailureDetails> failures) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerServiceDefValidator.isValidServiceDefName(%s, %s, %s, %s)", name, id, action, failures)); } boolean valid = true; if (StringUtils.isBlank(name)) { ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_INVALID_SERVICE_DEF_NAME; failures.add(new ValidationFailureDetailsBuilder() .field("name") .isMissing() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage(name)) .build()); valid = false; } else { RangerServiceDef otherServiceDef = getServiceDef(name); if (otherServiceDef != null && action == Action.CREATE) { ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_SERVICE_DEF_NAME_CONFICT; failures.add(new ValidationFailureDetailsBuilder() .field("name") .isSemanticallyIncorrect() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage(name)) .build()); valid = false; } else if (otherServiceDef != null && !Objects.equals(id, otherServiceDef.getId())) { ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_ID_NAME_CONFLICT; failures.add(new ValidationFailureDetailsBuilder() .field("id/name") .isSemanticallyIncorrect() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage(name, otherServiceDef.getId())) .build()); valid = false; } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerServiceDefValidator.isValidServiceDefName(%s, %s, %s, %s): %s", name, id, action, failures, valid)); } return valid; } RangerServiceDefValidator(ServiceStore store); void validate(final RangerServiceDef serviceDef, final Action action);  }### Answer:
@Test public final void test_isValidName() throws Exception { Long id = 7L; for (Action action: cu) { for (String name : new String[] { null, "", " " }) { when(_serviceDef.getName()).thenReturn(name); _failures.clear(); assertFalse(_validator.isValidServiceDefName(name, id, action, _failures)); _utils.checkFailureForMissingValue(_failures, "name"); } } }
@Test public final void test_isValidName_create() throws Exception { Long id = null; String name = "existing-service"; when(_serviceDef.getName()).thenReturn(name); when(_store.getServiceDefByName(name)).thenReturn(null); assertTrue(_validator.isValidServiceDefName(name, id, Action.CREATE, _failures)); assertTrue(_failures.isEmpty()); RangerServiceDef existingServiceDef = mock(RangerServiceDef.class); when(_store.getServiceDefByName(name)).thenReturn(existingServiceDef); _failures.clear(); assertFalse(_validator.isValidServiceDefName(name, id, Action.CREATE, _failures)); _utils.checkFailureForSemanticError(_failures, "name"); }
@Test public final void test_isValidName_update() throws Exception { long id = 7; when(_serviceDef.getId()).thenReturn(id); String name = "aServiceDef"; when(_serviceDef.getName()).thenReturn(name); when(_store.getServiceDefByName(name)).thenReturn(null); assertTrue(_validator.isValidServiceDefName(name, id, Action.UPDATE, _failures)); assertTrue(_failures.isEmpty()); RangerServiceDef existingServiceDef = mock(RangerServiceDef.class); when(existingServiceDef.getId()).thenReturn(id); when(existingServiceDef.getName()).thenReturn(name); when(_store.getServiceDefByName(name)).thenReturn(existingServiceDef); assertTrue(_validator.isValidServiceDefName(name, id, Action.UPDATE, _failures)); assertTrue(_failures.isEmpty()); long anotherId = 49; when(existingServiceDef.getId()).thenReturn(anotherId); assertFalse(_validator.isValidServiceDefName(name, id, Action.UPDATE, _failures)); _utils.checkFailureForSemanticError(_failures, "id/name"); }