void validateCompanies(List<VacancyTemplate> vacancyTemplates) {
    List<Company> companies = new ArrayList<>();
    Set<String> companyCaptions = new HashSet<String>();
    vacancyTemplates.forEach(
        vacancyTemplate -> companyCaptions.add(vacancyTemplate.getCompanyCaption()));

    companyCaptions.forEach(
        s -> {
          Company company = companyService.findByCaption(s);
          if (company == null) {
            company = new Company();
            company.setCaption(s);

            BeanPropertyBindingResult result = new BeanPropertyBindingResult(company, s);

            ValidationUtils.invokeValidator(validator, company, result);
            if (result.hasErrors()) {
              List<ObjectError> errors = result.getAllErrors();
              StringBuilder builder = new StringBuilder();
              builder.append(
                  String.format(
                      "Validate company '%s' . No of validation errors: %s", s, errors.size()));
              errors.forEach(
                  objectError -> {
                    builder.append("\n");
                    builder.append(objectError.getDefaultMessage());
                  });
              log.error(builder.toString());
            } else {
              companies.add(company);
            }
          }
        });

    saveAllCompanies(companies);
  }
  List<Vacancy> validateVacancies(List<VacancyTemplate> vacancyTemplates) {
    Set<String> vacancyURLs = new HashSet<>();
    List<Vacancy> vacancies = new ArrayList<>();
    for (VacancyTemplate vt : vacancyTemplates) {
      Company company = companyService.findByCaption(vt.getCompanyCaption());
      if (company == null)
        log.error(
            "Job missing: '{}'. Reason: company '{}' not found",
            vt.getVacancyCaption(),
            vt.getCompanyCaption());
      else if (!vacancyURLs.contains(vt.getVacancyUrl())) {

        String pathSegment;

        try {
          pathSegment =
              UriUtils.encodePathSegment(vt.getVacancyUrl(), WebUtils.DEFAULT_CHARACTER_ENCODING);
        } catch (UnsupportedEncodingException uee) {
          log.error(uee.toString());
          continue;
        }

        vacancyURLs.add(pathSegment);

        Vacancy vacancy = new Vacancy();
        vacancy.setId(pathSegment);
        vacancy.setCaption(vt.getVacancyCaption());
        vacancy.setUrl(vt.getVacancyUrl());
        vacancy.setPriceMax(vt.getPriceMax());
        vacancy.setPriceMin(vt.getPriceMin());
        vacancy.setRegistrationDate(vt.getRegistrationDate());
        vacancy.setCurrency(currencyService.findByCaption(vt.getCurrencyCaption()));
        vacancy.setSite(vt.getSite());
        vacancy.setCity(vt.getCity());
        vacancy.setCompany(company);

        BeanPropertyBindingResult result =
            new BeanPropertyBindingResult(vacancy, vacancy.getCaption());

        ValidationUtils.invokeValidator(validator, vacancy, result);
        if (result.hasErrors()) {
          List<ObjectError> errors = result.getAllErrors();

          StringBuilder builder = new StringBuilder();
          builder.append(
              String.format(
                  "Validate vacancy '%s' . No of validation errors: %s",
                  vacancy.getCaption(), errors.size()));
          errors.forEach(
              objectError -> {
                builder.append("\n");
                builder.append(objectError.getDefaultMessage());
              });
          log.error(builder.toString());
        } else {
          vacancies.add(vacancy);
        }
      }
    }

    return vacancies;
  }
Beispiel #3
0
  @Test
  public void webMvcConfigurerExtensionHooks() throws Exception {

    StaticWebApplicationContext appCxt = new StaticWebApplicationContext();
    appCxt.setServletContext(new MockServletContext(new FileSystemResourceLoader()));
    appCxt.registerSingleton("controller", TestController.class);

    WebConfig webConfig = new WebConfig();
    webConfig.setApplicationContext(appCxt);
    webConfig.setServletContext(appCxt.getServletContext());

    String actual = webConfig.mvcConversionService().convert(new TestBean(), String.class);
    assertEquals("converted", actual);

    RequestMappingHandlerAdapter adapter = webConfig.requestMappingHandlerAdapter();
    assertEquals(1, adapter.getMessageConverters().size());

    ConfigurableWebBindingInitializer initializer =
        (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
    assertNotNull(initializer);

    BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(null, "");
    initializer.getValidator().validate(null, bindingResult);
    assertEquals("invalid", bindingResult.getAllErrors().get(0).getCode());

    @SuppressWarnings("unchecked")
    List<HandlerMethodArgumentResolver> argResolvers =
        (List<HandlerMethodArgumentResolver>)
            new DirectFieldAccessor(adapter).getPropertyValue("customArgumentResolvers");
    assertEquals(1, argResolvers.size());

    @SuppressWarnings("unchecked")
    List<HandlerMethodReturnValueHandler> handlers =
        (List<HandlerMethodReturnValueHandler>)
            new DirectFieldAccessor(adapter).getPropertyValue("customReturnValueHandlers");
    assertEquals(1, handlers.size());

    HandlerExceptionResolverComposite composite =
        (HandlerExceptionResolverComposite) webConfig.handlerExceptionResolver();
    assertEquals(1, composite.getExceptionResolvers().size());

    RequestMappingHandlerMapping rmHandlerMapping = webConfig.requestMappingHandlerMapping();
    rmHandlerMapping.setApplicationContext(appCxt);
    HandlerExecutionChain chain =
        rmHandlerMapping.getHandler(new MockHttpServletRequest("GET", "/"));
    assertNotNull(chain.getInterceptors());
    assertEquals(2, chain.getInterceptors().length);
    assertEquals(LocaleChangeInterceptor.class, chain.getInterceptors()[0].getClass());
    assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[1].getClass());

    AbstractHandlerMapping handlerMapping =
        (AbstractHandlerMapping) webConfig.viewControllerHandlerMapping();
    handlerMapping.setApplicationContext(appCxt);
    assertNotNull(handlerMapping);
    assertEquals(1, handlerMapping.getOrder());
    HandlerExecutionChain handler =
        handlerMapping.getHandler(new MockHttpServletRequest("GET", "/path"));
    assertNotNull(handler.getHandler());

    handlerMapping = (AbstractHandlerMapping) webConfig.resourceHandlerMapping();
    handlerMapping.setApplicationContext(appCxt);
    assertNotNull(handlerMapping);
    assertEquals(Integer.MAX_VALUE - 1, handlerMapping.getOrder());
    handler = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/resources/foo.gif"));
    assertNotNull(handler.getHandler());

    handlerMapping = (AbstractHandlerMapping) webConfig.defaultServletHandlerMapping();
    handlerMapping.setApplicationContext(appCxt);
    assertNotNull(handlerMapping);
    assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());
    handler = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/anyPath"));
    assertNotNull(handler.getHandler());
  }