@Test public void shouldAddErrorsOnValidationFailure() throws Exception { when(messageBrokerTestService.pingBroker("tcp: .thenReturn(true); BindingResult bindingResult = mock(BindingResult.class); when(bindingResult.hasErrors()).thenReturn(true); when(bindingResult.getAllErrors()).thenReturn(Arrays.asList(new ObjectError("sqlUrl", new String[]{"server.dbUrl.error"}, null, null))); BootstrapConfigForm bootstrapConfigForm = new BootstrapConfigForm(); bootstrapConfigForm.setSqlUrl("http: ModelAndView actualView = bootstrapController.submitForm(bootstrapConfigForm, bindingResult, request); assertThat(actualView.getViewName(), is("bootstrapconfig")); assertThat((String) ((List) actualView.getModel().get("errors")).get(0), is("server.dbUrl.error")); }
@RequestMapping(value = "/", method = RequestMethod.POST) public ModelAndView submitForm(@ModelAttribute(BOOTSTRAP_CONFIG) @Valid BootstrapConfigForm form, BindingResult result, HttpServletRequest request) { if (OsgiListener.isBootstrapPresent()) { return new ModelAndView(REDIRECT_HOME); } if (result.hasErrors()) { ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.addObject("errors", getErrors(result)); addCommonBootstrapViewObjects(bootstrapView); return bootstrapView; } String queueUrl = form.getQueueUrl(); boolean reachable = messageBrokerPingService.pingBroker(queueUrl); if (!reachable) { ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.addObject(ERRORS, singletonList(getMessage("server.bootstrap.verify.amq.warning", new Object[]{queueUrl}, request))); addCommonBootstrapViewObjects(bootstrapView); return bootstrapView; } BootstrapConfig bootstrapConfig; if (form.getOsgiFrameworkStorage() != null) { bootstrapConfig = new BootstrapConfig(new SQLDBConfig(form.getSqlUrl(), form.getSqlDriver(), form.getSqlUsername(), form.getSqlPassword()), ConfigSource.valueOf(form.getConfigSource()), form.getOsgiFrameworkStorage(), form.getMotechDir(), form.getQueueUrl()); } else { bootstrapConfig = new BootstrapConfig(new SQLDBConfig(form.getSqlUrl(), form.getSqlDriver(), form.getSqlUsername(), form.getSqlPassword()), ConfigSource.valueOf(form.getConfigSource()), null, form.getMotechDir(), form.getQueueUrl()); } try { OsgiListener.saveBootstrapConfig(bootstrapConfig); } catch (RuntimeException e) { LOGGER.error("Error while saving bootstrap configuration", e); ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.addObject("errors", singletonList(getMessage("server.error.bootstrap.save", request))); addCommonBootstrapViewObjects(bootstrapView); return bootstrapView; } ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.getModelMap().put("redirect", true); return bootstrapView; }
BootstrapController { @RequestMapping(value = "/", method = RequestMethod.POST) public ModelAndView submitForm(@ModelAttribute(BOOTSTRAP_CONFIG) @Valid BootstrapConfigForm form, BindingResult result, HttpServletRequest request) { if (OsgiListener.isBootstrapPresent()) { return new ModelAndView(REDIRECT_HOME); } if (result.hasErrors()) { ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.addObject("errors", getErrors(result)); addCommonBootstrapViewObjects(bootstrapView); return bootstrapView; } String queueUrl = form.getQueueUrl(); boolean reachable = messageBrokerPingService.pingBroker(queueUrl); if (!reachable) { ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.addObject(ERRORS, singletonList(getMessage("server.bootstrap.verify.amq.warning", new Object[]{queueUrl}, request))); addCommonBootstrapViewObjects(bootstrapView); return bootstrapView; } BootstrapConfig bootstrapConfig; if (form.getOsgiFrameworkStorage() != null) { bootstrapConfig = new BootstrapConfig(new SQLDBConfig(form.getSqlUrl(), form.getSqlDriver(), form.getSqlUsername(), form.getSqlPassword()), ConfigSource.valueOf(form.getConfigSource()), form.getOsgiFrameworkStorage(), form.getMotechDir(), form.getQueueUrl()); } else { bootstrapConfig = new BootstrapConfig(new SQLDBConfig(form.getSqlUrl(), form.getSqlDriver(), form.getSqlUsername(), form.getSqlPassword()), ConfigSource.valueOf(form.getConfigSource()), null, form.getMotechDir(), form.getQueueUrl()); } try { OsgiListener.saveBootstrapConfig(bootstrapConfig); } catch (RuntimeException e) { LOGGER.error("Error while saving bootstrap configuration", e); ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.addObject("errors", singletonList(getMessage("server.error.bootstrap.save", request))); addCommonBootstrapViewObjects(bootstrapView); return bootstrapView; } ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.getModelMap().put("redirect", true); return bootstrapView; } }
BootstrapController { @RequestMapping(value = "/", method = RequestMethod.POST) public ModelAndView submitForm(@ModelAttribute(BOOTSTRAP_CONFIG) @Valid BootstrapConfigForm form, BindingResult result, HttpServletRequest request) { if (OsgiListener.isBootstrapPresent()) { return new ModelAndView(REDIRECT_HOME); } if (result.hasErrors()) { ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.addObject("errors", getErrors(result)); addCommonBootstrapViewObjects(bootstrapView); return bootstrapView; } String queueUrl = form.getQueueUrl(); boolean reachable = messageBrokerPingService.pingBroker(queueUrl); if (!reachable) { ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.addObject(ERRORS, singletonList(getMessage("server.bootstrap.verify.amq.warning", new Object[]{queueUrl}, request))); addCommonBootstrapViewObjects(bootstrapView); return bootstrapView; } BootstrapConfig bootstrapConfig; if (form.getOsgiFrameworkStorage() != null) { bootstrapConfig = new BootstrapConfig(new SQLDBConfig(form.getSqlUrl(), form.getSqlDriver(), form.getSqlUsername(), form.getSqlPassword()), ConfigSource.valueOf(form.getConfigSource()), form.getOsgiFrameworkStorage(), form.getMotechDir(), form.getQueueUrl()); } else { bootstrapConfig = new BootstrapConfig(new SQLDBConfig(form.getSqlUrl(), form.getSqlDriver(), form.getSqlUsername(), form.getSqlPassword()), ConfigSource.valueOf(form.getConfigSource()), null, form.getMotechDir(), form.getQueueUrl()); } try { OsgiListener.saveBootstrapConfig(bootstrapConfig); } catch (RuntimeException e) { LOGGER.error("Error while saving bootstrap configuration", e); ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.addObject("errors", singletonList(getMessage("server.error.bootstrap.save", request))); addCommonBootstrapViewObjects(bootstrapView); return bootstrapView; } ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.getModelMap().put("redirect", true); return bootstrapView; }  }
BootstrapController { @RequestMapping(value = "/", method = RequestMethod.POST) public ModelAndView submitForm(@ModelAttribute(BOOTSTRAP_CONFIG) @Valid BootstrapConfigForm form, BindingResult result, HttpServletRequest request) { if (OsgiListener.isBootstrapPresent()) { return new ModelAndView(REDIRECT_HOME); } if (result.hasErrors()) { ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.addObject("errors", getErrors(result)); addCommonBootstrapViewObjects(bootstrapView); return bootstrapView; } String queueUrl = form.getQueueUrl(); boolean reachable = messageBrokerPingService.pingBroker(queueUrl); if (!reachable) { ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.addObject(ERRORS, singletonList(getMessage("server.bootstrap.verify.amq.warning", new Object[]{queueUrl}, request))); addCommonBootstrapViewObjects(bootstrapView); return bootstrapView; } BootstrapConfig bootstrapConfig; if (form.getOsgiFrameworkStorage() != null) { bootstrapConfig = new BootstrapConfig(new SQLDBConfig(form.getSqlUrl(), form.getSqlDriver(), form.getSqlUsername(), form.getSqlPassword()), ConfigSource.valueOf(form.getConfigSource()), form.getOsgiFrameworkStorage(), form.getMotechDir(), form.getQueueUrl()); } else { bootstrapConfig = new BootstrapConfig(new SQLDBConfig(form.getSqlUrl(), form.getSqlDriver(), form.getSqlUsername(), form.getSqlPassword()), ConfigSource.valueOf(form.getConfigSource()), null, form.getMotechDir(), form.getQueueUrl()); } try { OsgiListener.saveBootstrapConfig(bootstrapConfig); } catch (RuntimeException e) { LOGGER.error("Error while saving bootstrap configuration", e); ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.addObject("errors", singletonList(getMessage("server.error.bootstrap.save", request))); addCommonBootstrapViewObjects(bootstrapView); return bootstrapView; } ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.getModelMap().put("redirect", true); return bootstrapView; }  @RequestMapping(value = "/", method = RequestMethod.GET) ModelAndView bootstrapForm(); @RequestMapping(value = "/", method = RequestMethod.POST) ModelAndView submitForm(@ModelAttribute(BOOTSTRAP_CONFIG) @Valid BootstrapConfigForm form, BindingResult result,
                                   HttpServletRequest request); @RequestMapping(value = "/verifySql", method = RequestMethod.POST) @ResponseBody Map<String, ?> verifySqlConnection(@ModelAttribute(BOOTSTRAP_CONFIG) @Valid BootstrapConfigForm form,
                                              BindingResult result, HttpServletRequest request); @RequestMapping(value = "/verifyAmq", method = RequestMethod.POST) @ResponseBody Map<String, ?> verifyAmqConnection(@ModelAttribute(BOOTSTRAP_CONFIG) @Valid BootstrapConfigForm form,
                                              BindingResult result, HttpServletRequest request); @RequestMapping(value = "/status", method = RequestMethod.GET) @ResponseBody PlatformStatus status(); }
BootstrapController { @RequestMapping(value = "/", method = RequestMethod.POST) public ModelAndView submitForm(@ModelAttribute(BOOTSTRAP_CONFIG) @Valid BootstrapConfigForm form, BindingResult result, HttpServletRequest request) { if (OsgiListener.isBootstrapPresent()) { return new ModelAndView(REDIRECT_HOME); } if (result.hasErrors()) { ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.addObject("errors", getErrors(result)); addCommonBootstrapViewObjects(bootstrapView); return bootstrapView; } String queueUrl = form.getQueueUrl(); boolean reachable = messageBrokerPingService.pingBroker(queueUrl); if (!reachable) { ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.addObject(ERRORS, singletonList(getMessage("server.bootstrap.verify.amq.warning", new Object[]{queueUrl}, request))); addCommonBootstrapViewObjects(bootstrapView); return bootstrapView; } BootstrapConfig bootstrapConfig; if (form.getOsgiFrameworkStorage() != null) { bootstrapConfig = new BootstrapConfig(new SQLDBConfig(form.getSqlUrl(), form.getSqlDriver(), form.getSqlUsername(), form.getSqlPassword()), ConfigSource.valueOf(form.getConfigSource()), form.getOsgiFrameworkStorage(), form.getMotechDir(), form.getQueueUrl()); } else { bootstrapConfig = new BootstrapConfig(new SQLDBConfig(form.getSqlUrl(), form.getSqlDriver(), form.getSqlUsername(), form.getSqlPassword()), ConfigSource.valueOf(form.getConfigSource()), null, form.getMotechDir(), form.getQueueUrl()); } try { OsgiListener.saveBootstrapConfig(bootstrapConfig); } catch (RuntimeException e) { LOGGER.error("Error while saving bootstrap configuration", e); ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.addObject("errors", singletonList(getMessage("server.error.bootstrap.save", request))); addCommonBootstrapViewObjects(bootstrapView); return bootstrapView; } ModelAndView bootstrapView = new ModelAndView(BOOTSTRAP_CONFIG_VIEW); bootstrapView.getModelMap().put("redirect", true); return bootstrapView; }  @RequestMapping(value = "/", method = RequestMethod.GET) ModelAndView bootstrapForm(); @RequestMapping(value = "/", method = RequestMethod.POST) ModelAndView submitForm(@ModelAttribute(BOOTSTRAP_CONFIG) @Valid BootstrapConfigForm form, BindingResult result,
                                   HttpServletRequest request); @RequestMapping(value = "/verifySql", method = RequestMethod.POST) @ResponseBody Map<String, ?> verifySqlConnection(@ModelAttribute(BOOTSTRAP_CONFIG) @Valid BootstrapConfigForm form,
                                              BindingResult result, HttpServletRequest request); @RequestMapping(value = "/verifyAmq", method = RequestMethod.POST) @ResponseBody Map<String, ?> verifyAmqConnection(@ModelAttribute(BOOTSTRAP_CONFIG) @Valid BootstrapConfigForm form,
                                              BindingResult result, HttpServletRequest request); @RequestMapping(value = "/status", method = RequestMethod.GET) @ResponseBody PlatformStatus status(); static final String BOOTSTRAP_CONFIG_VIEW; static final String REDIRECT_HOME; }