Search in sources :

Example 1 with LocalValidatorFactoryBean

use of org.springframework.validation.beanvalidation.LocalValidatorFactoryBean in project head by mifos.

the class LoanAccountFormBeanTest method setUp.

@Before
public void setUp() {
    MifosBeanValidator validator = new MifosBeanValidator();
    LocalValidatorFactoryBean targetValidator = new LocalValidatorFactoryBean();
    targetValidator.afterPropertiesSet();
    validator.setTargetValidator(targetValidator);
    loanAccountFormBean = new LoanAccountFormBean();
    Integer productId = Integer.valueOf(1);
    Integer customerId = Integer.valueOf(1);
    loanAccountFormBean.setProductId(productId);
    loanAccountFormBean.setCustomerId(customerId);
    loanAccountFormBean.setPurposeOfLoanMandatory(false);
    loanAccountFormBean.setSourceOfFundsMandatory(false);
    loanAccountFormBean.setExternalIdMandatory(false);
    loanAccountFormBean.setCollateralTypeAndNotesHidden(false);
    loanAccountFormBean.setAmount(Double.valueOf("1000.0"));
    loanAccountFormBean.setMinAllowedAmount(Integer.valueOf(400));
    loanAccountFormBean.setMaxAllowedAmount(Integer.valueOf(20000));
    loanAccountFormBean.setInterestRate(Double.valueOf(10.0));
    loanAccountFormBean.setMinAllowedInterestRate(Double.valueOf(1.0));
    loanAccountFormBean.setMaxAllowedInterestRate(Double.valueOf(20.0));
    loanAccountFormBean.setDigitsBeforeDecimalForInterest(10);
    loanAccountFormBean.setDigitsAfterDecimalForInterest(5);
    loanAccountFormBean.setDigitsBeforeDecimalForMonetaryAmounts(14);
    loanAccountFormBean.setDigitsAfterDecimalForMonetaryAmounts(1);
    loanAccountFormBean.setAdditionalFees(createAdditionalFeesMocks());
    loanAccountFormBean.setDisbursementDateDD(24);
    loanAccountFormBean.setDisbursementDateMM(02);
    loanAccountFormBean.setDisbursementDateYY(2011);
    loanAccountFormBean.setNumberOfInstallments(12);
    loanAccountFormBean.setMinNumberOfInstallments(1);
    loanAccountFormBean.setMaxNumberOfInstallments(12);
    loanAccountFormBean.setValidator(validator);
    loanAccountFormBean.setLoanDisbursementDateValidationServiceFacade(loanDisbursementDateValidationServiceFacade);
    context = new StubValidationContext();
    when(loanDisbursementDateValidationServiceFacade.validateLoanDisbursementDate((LocalDate) anyObject(), anyInt(), anyInt())).thenReturn(new Errors());
}
Also used : LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) Errors(org.mifos.platform.validations.Errors) MifosBeanValidator(org.mifos.platform.validation.MifosBeanValidator) StubValidationContext(org.mifos.ui.validation.StubValidationContext) Before(org.junit.Before)

Example 2 with LocalValidatorFactoryBean

use of org.springframework.validation.beanvalidation.LocalValidatorFactoryBean in project head by mifos.

the class CreateSavingsAccountFormBeanTest method setUp.

@Before
public void setUp() {
    MifosBeanValidator validator = new MifosBeanValidator();
    LocalValidatorFactoryBean targetValidator = new LocalValidatorFactoryBean();
    targetValidator.afterPropertiesSet();
    validator.setTargetValidator(targetValidator);
    formBean = new CreateSavingsAccountFormBean();
    formBean.setValidator(validator);
    questionnaireServiceFacade = mock(QuestionnaireServiceFacade.class);
    formBean.setQuestionnaireServiceFascade(questionnaireServiceFacade);
    configurationDto = new AccountingConfigurationDto();
    configurationDto.setDigitsBeforeDecimal((short) 14);
    configurationDto.setDigitsAfterDecimal((short) 1);
    configurationServiceFacade = mock(ConfigurationServiceFacade.class);
    when(configurationServiceFacade.getAccountingConfiguration()).thenReturn(configurationDto);
    formBean.setConfigurationServiceFacade(configurationServiceFacade);
    validationContext = new StubValidationContext();
    validationException = new ValidationException("Root");
    validationException.addChildException(new ValidationException("Child"));
}
Also used : LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) ValidationException(org.mifos.platform.validations.ValidationException) MifosBeanValidator(org.mifos.platform.validation.MifosBeanValidator) AccountingConfigurationDto(org.mifos.config.servicefacade.dto.AccountingConfigurationDto) StubValidationContext(org.mifos.ui.validation.StubValidationContext) QuestionnaireServiceFacade(org.mifos.platform.questionnaire.service.QuestionnaireServiceFacade) ConfigurationServiceFacade(org.mifos.config.servicefacade.ConfigurationServiceFacade) Before(org.junit.Before)

Example 3 with LocalValidatorFactoryBean

use of org.springframework.validation.beanvalidation.LocalValidatorFactoryBean in project irida by phac-nml.

the class IridaApiServicesConfig method validator.

@Bean
public Validator validator() {
    ResourceBundleMessageSource validatorMessageSource = new ResourceBundleMessageSource();
    validatorMessageSource.setBasename("ValidationMessages");
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.setValidationMessageSource(validatorMessageSource);
    return validator;
}
Also used : LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) ReloadableResourceBundleMessageSource(org.springframework.context.support.ReloadableResourceBundleMessageSource) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean)

Example 4 with LocalValidatorFactoryBean

use of org.springframework.validation.beanvalidation.LocalValidatorFactoryBean in project spring-framework by spring-projects.

the class WebMvcConfigurationSupportTests method requestMappingHandlerAdapter.

@Test
public void requestMappingHandlerAdapter() {
    ApplicationContext context = initContext(WebConfig.class);
    RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
    List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
    assertThat(converters.size()).isEqualTo(12);
    converters.stream().filter(converter -> converter instanceof AbstractJackson2HttpMessageConverter).forEach(converter -> {
        ObjectMapper mapper = ((AbstractJackson2HttpMessageConverter) converter).getObjectMapper();
        assertThat(mapper.getDeserializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION)).isFalse();
        assertThat(mapper.getSerializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION)).isFalse();
        assertThat(mapper.getDeserializationConfig().isEnabled(FAIL_ON_UNKNOWN_PROPERTIES)).isFalse();
        if (converter instanceof MappingJackson2XmlHttpMessageConverter) {
            assertThat(mapper.getClass()).isEqualTo(XmlMapper.class);
        }
    });
    ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
    assertThat(initializer).isNotNull();
    ConversionService conversionService = initializer.getConversionService();
    assertThat(conversionService).isNotNull();
    boolean condition1 = conversionService instanceof FormattingConversionService;
    assertThat(condition1).isTrue();
    Validator validator = initializer.getValidator();
    assertThat(validator).isNotNull();
    boolean condition = validator instanceof LocalValidatorFactoryBean;
    assertThat(condition).isTrue();
    DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter);
    @SuppressWarnings("unchecked") List<Object> bodyAdvice = (List<Object>) fieldAccessor.getPropertyValue("requestResponseBodyAdvice");
    assertThat(bodyAdvice.size()).isEqualTo(2);
    assertThat(bodyAdvice.get(0).getClass()).isEqualTo(JsonViewRequestBodyAdvice.class);
    assertThat(bodyAdvice.get(1).getClass()).isEqualTo(JsonViewResponseBodyAdvice.class);
}
Also used : LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) PathVariable(org.springframework.web.bind.annotation.PathVariable) DefaultRequestToViewNameTranslator(org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator) DEFAULT_VIEW_INCLUSION(com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION) Validator(org.springframework.validation.Validator) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) LocaleResolver(org.springframework.web.servlet.LocaleResolver) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) DateTimeFormat(org.springframework.format.annotation.DateTimeFormat) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) SessionFlashMapManager(org.springframework.web.servlet.support.SessionFlashMapManager) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) FAIL_ON_UNKNOWN_PROPERTIES(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) HandlerExceptionResolverComposite(org.springframework.web.servlet.handler.HandlerExceptionResolverComposite) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) Locale(java.util.Locale) Map(java.util.Map) MethodParameter(org.springframework.core.MethodParameter) AntPathMatcher(org.springframework.util.AntPathMatcher) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) JsonViewRequestBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewRequestBodyAdvice) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) HandlerMethodArgumentResolver(org.springframework.web.method.support.HandlerMethodArgumentResolver) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) ISO(org.springframework.format.annotation.DateTimeFormat.ISO) StaticMessageSource(org.springframework.context.support.StaticMessageSource) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) HandlerMapping(org.springframework.web.servlet.HandlerMapping) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) FlashMapManager(org.springframework.web.servlet.FlashMapManager) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) ExceptionHandlerExceptionResolver(org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) FLASH_MAP_MANAGER_BEAN_NAME(org.springframework.web.servlet.DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME) ResponseStatusExceptionResolver(org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) HandlerMethodReturnValueHandler(org.springframework.web.method.support.HandlerMethodReturnValueHandler) THEME_RESOLVER_BEAN_NAME(org.springframework.web.servlet.DispatcherServlet.THEME_RESOLVER_BEAN_NAME) REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME(org.springframework.web.servlet.DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME) Ordered(org.springframework.core.Ordered) ResourceUrlProviderExposingInterceptor(org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ThemeResolver(org.springframework.web.servlet.ThemeResolver) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) JsonViewResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Controller(org.springframework.stereotype.Controller) Scope(org.springframework.context.annotation.Scope) RequestToViewNameTranslator(org.springframework.web.servlet.RequestToViewNameTranslator) LOCALE_RESOLVER_BEAN_NAME(org.springframework.web.servlet.DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME) CompositeUriComponentsContributor(org.springframework.web.method.support.CompositeUriComponentsContributor) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ConversionService(org.springframework.core.convert.ConversionService) MessageSource(org.springframework.context.MessageSource) ViewResolver(org.springframework.web.servlet.ViewResolver) PathMatcher(org.springframework.util.PathMatcher) ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ScopedProxyMode(org.springframework.context.annotation.ScopedProxyMode) FormattingConversionService(org.springframework.format.support.FormattingConversionService) ApplicationContext(org.springframework.context.ApplicationContext) BeanNameViewResolver(org.springframework.web.servlet.view.BeanNameViewResolver) HttpStatus(org.springframework.http.HttpStatus) BeanNameUrlHandlerMapping(org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping) UrlPathHelper(org.springframework.web.util.UrlPathHelper) Bean(org.springframework.context.annotation.Bean) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) FormattingConversionService(org.springframework.format.support.FormattingConversionService) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) ConversionService(org.springframework.core.convert.ConversionService) FormattingConversionService(org.springframework.format.support.FormattingConversionService) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) List(java.util.List) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Validator(org.springframework.validation.Validator) Test(org.junit.jupiter.api.Test)

Example 5 with LocalValidatorFactoryBean

use of org.springframework.validation.beanvalidation.LocalValidatorFactoryBean in project spring-framework by spring-projects.

the class DelegatingWebFluxConfigurationTests method requestMappingHandlerAdapter.

@Test
public void requestMappingHandlerAdapter() {
    delegatingConfig.setConfigurers(Collections.singletonList(webFluxConfigurer));
    ReactiveAdapterRegistry reactiveAdapterRegistry = delegatingConfig.webFluxAdapterRegistry();
    ServerCodecConfigurer serverCodecConfigurer = delegatingConfig.serverCodecConfigurer();
    FormattingConversionService formattingConversionService = delegatingConfig.webFluxConversionService();
    Validator validator = delegatingConfig.webFluxValidator();
    ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) this.delegatingConfig.requestMappingHandlerAdapter(reactiveAdapterRegistry, serverCodecConfigurer, formattingConversionService, validator).getWebBindingInitializer();
    verify(webFluxConfigurer).configureHttpMessageCodecs(codecsConfigurer.capture());
    verify(webFluxConfigurer).getValidator();
    verify(webFluxConfigurer).getMessageCodesResolver();
    verify(webFluxConfigurer).addFormatters(formatterRegistry.capture());
    verify(webFluxConfigurer).configureArgumentResolvers(any());
    assertThat(initializer).isNotNull();
    boolean condition = initializer.getValidator() instanceof LocalValidatorFactoryBean;
    assertThat(condition).isTrue();
    assertThat(initializer.getConversionService()).isSameAs(formatterRegistry.getValue());
    assertThat(codecsConfigurer.getValue().getReaders().size()).isEqualTo(14);
}
Also used : LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) FormattingConversionService(org.springframework.format.support.FormattingConversionService) Validator(org.springframework.validation.Validator) Test(org.junit.jupiter.api.Test)

Aggregations

LocalValidatorFactoryBean (org.springframework.validation.beanvalidation.LocalValidatorFactoryBean)32 Bean (org.springframework.context.annotation.Bean)12 Test (org.junit.jupiter.api.Test)6 ApplicationContext (org.springframework.context.ApplicationContext)5 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)4 Locale (java.util.Locale)2 Before (org.junit.Before)2 Test (org.junit.Test)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 MifosBeanValidator (org.mifos.platform.validation.MifosBeanValidator)2 StubValidationContext (org.mifos.ui.validation.StubValidationContext)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 MessageInterpolatorFactory (org.springframework.boot.validation.MessageInterpolatorFactory)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 Role (org.springframework.context.annotation.Role)2 StaticMessageSource (org.springframework.context.support.StaticMessageSource)2 FormattingConversionService (org.springframework.format.support.FormattingConversionService)2 Validator (org.springframework.validation.Validator)2 RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter)2 FAIL_ON_UNKNOWN_PROPERTIES (com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)1