PlanValidation extends Validation { public ValidationResult isUpdateNameValid(Long planId, String name) { ValidationResult validationResult = isNameValid(name); if (validationResult.getValidationStatus() == ValidationStatus.INVALID) { return validationResult; } if (!nameExistsWithSameIdOrNotAtAll(planId, name)) { return new ValidationResult(ValidationStatus.INVALID, stringProvider.getString(R.string.name_exist_msg)); } return new ValidationResult(ValidationStatus.VALID); } PlanValidation(StringsProvider stringsProvider,
                          PlanTemplateRepository planTemplateRepository); ValidationResult isNewNameValid(String name); ValidationResult isUpdateNameValid(Long planId, String name);  }
@Test public void whenPlanNameIsValidExpectUpdateNameValidationPasses() { when(planTemplateRepository.get(VALID_PLAN_NAME)).thenReturn(Collections.EMPTY_LIST); ValidationResult validationResult = planValidation.isUpdateNameValid( PLAN_ID, VALID_PLAN_NAME); assertThat(validationResult.getValidationStatus(), is(ValidationStatus.VALID)); }
@Test public void whenPlanNameIsEmptyExpectUpdateNameValidationFails() { ValidationResult validationResult = planValidation.isUpdateNameValid( PLAN_ID, EMPTY_PLAN_NAME); assertThat(validationResult.getValidationStatus(), is(ValidationStatus.INVALID)); assertThat(validationResult.getValidationInfo(), is(ERROR_MESSAGE_EMPTY)); }
@Test public void whenPlanNameIsInvalidExpectUpdateNameValidationFails() { ValidationResult validationResult = planValidation.isUpdateNameValid( PLAN_ID, INVALID_PLAN_NAME); assertThat(validationResult.getValidationStatus(), is(ValidationStatus.INVALID)); assertThat(validationResult.getValidationInfo(), is(ERROR_MESSAGE_ONLY_LETTERS)); }
@Test public void whenPlanNameIsAlreadyInDbWithSameIdExpectUpdateNameValidationPasses() { duplicatedPlanTemplate.setId(PLAN_ID); when(planTemplateRepository.get(VALID_PLAN_NAME)) .thenReturn(Collections.singletonList(duplicatedPlanTemplate)); ValidationResult validationResult = planValidation.isUpdateNameValid( PLAN_ID, VALID_PLAN_NAME); assertThat(validationResult.getValidationStatus(), is(ValidationStatus.VALID)); }
@Test public void whenPlanNameIsAlreadyInDbWithDifferentIdExpectUpdateNameValidationPasses() { duplicatedPlanTemplate.setId(PLAN_ID + 1); when(planTemplateRepository.get(VALID_PLAN_NAME)) .thenReturn(Collections.singletonList(duplicatedPlanTemplate)); ValidationResult validationResult = planValidation.isUpdateNameValid( PLAN_ID, VALID_PLAN_NAME); assertThat(validationResult.getValidationStatus(), is(ValidationStatus.INVALID)); assertThat(validationResult.getValidationInfo(), is(ERROR_MESSAGE_NAME_EXIST)); }