StartupFormValidator { public List<String> validate(StartupForm target, ConfigSource configSource) { List<String> errors = new ArrayList<>(); for (AbstractValidator validator : validators) { validator.validate(target, errors, configSource); } return errors; } StartupFormValidator(); List<String> validate(StartupForm target, ConfigSource configSource); void add(AbstractValidator validator); List<AbstractValidator> getValidators();  }
@Test public void shouldRejectEmptyFields() { StartupForm startupForm = new StartupForm(); startupForm.setAdminEmail(EMAIL); startupFormValidator = new StartupFormValidatorFactory().getStartupFormValidator(startupForm, userService); List<String> errors = startupFormValidator.validate(startupForm, ConfigSource.UI); assertTrue(errors.contains(String.format("server.error.required.%s", LOGIN_MODE))); assertTrue(errors.contains(String.format("server.error.required.%s", LANGUAGE))); }
@Test public void shouldRejectEmptyUserFieldsWhenLoginModeIsRepository() { StartupForm startupForm = new StartupForm(); when(userService.hasActiveMotechAdmin()).thenReturn(false); startupForm.setLoginMode("repository"); startupForm.setLanguage("en"); startupFormValidator = new StartupFormValidatorFactory().getStartupFormValidator(startupForm, userService); List<String> errors = startupFormValidator.validate(startupForm, ConfigSource.FILE); assertTrue(errors.contains(String.format("server.error.required.%s", ADMIN_LOGIN))); assertTrue(errors.contains(String.format("server.error.required.%s", ADMIN_PASSWORD))); assertTrue(errors.contains("server.error.invalid.email")); }
@Test public void shouldRejectEmptyUserFieldsWhenLoginModeIsOpenId() { StartupForm startupForm = new StartupForm(); startupForm.setLoginMode("openId"); startupForm.setLanguage("en"); startupFormValidator = new StartupFormValidatorFactory().getStartupFormValidator(startupForm, userService); List<String> errors = startupFormValidator.validate(startupForm, ConfigSource.UI); assertTrue(errors.contains(String.format("server.error.required.%s", PROVIDER_NAME))); assertTrue(errors.contains(String.format("server.error.required.%s", PROVIDER_URL))); }
@Test public void shouldAcceptEmptyQueueURLAndLoginModeWhenConfigSourceIsFile() { StartupForm startupForm = new StartupForm(); startupForm.setAdminLogin(ADMIN_LOGIN); startupForm.setAdminPassword(ADMIN_PASSWORD); startupForm.setAdminConfirmPassword(ADMIN_CONFIRM_PASSWORD); startupForm.setAdminEmail(EMAIL); startupFormValidator = new StartupFormValidatorFactory().getStartupFormValidator(startupForm, userService); List<String> errors = startupFormValidator.validate(startupForm, ConfigSource.FILE); assertTrue(errors.isEmpty()); }