ProjectController { @RequestMapping(value="/save", method=RequestMethod.POST) @PreAuthorize("projectExists(#form.name) ? " + "hasProjectPermission(#form.name, EDIT_PROJECT) : " + "hasApplicationPermission(EDIT_PROJECT)") public String saveProject(@ModelAttribute @Valid ProjectForm form, BindingResult bindingResult, Authentication authentication) throws IOException, GitAPIException { String name = form.getName(); String originalName = form.getOriginalName(); List<String> projects = globalRepositoryManager.listProjects(); if (StringUtils.isNotBlank(originalName)) { if (!projects.contains(originalName)) { bindingResult.rejectValue("originalName", "project.name.nonexistent"); } if (!StringUtils.equals(name, originalName) && projects.contains(name)) { bindingResult.rejectValue("name", "project.name.exists"); } } else { if (projects.contains(name)) { bindingResult.rejectValue("name", "project.name.exists"); } } if (bindingResult.hasErrors()) { return "/project/edit"; } User user = userStore.getUser(authentication.getName()); if (StringUtils.isNotBlank(originalName) && !StringUtils.equals(name, originalName)) { globalRepositoryManager.renameProject(originalName, name, user); } else if (StringUtils.isBlank(originalName)) { ILockedRepository repo = null; try { repo = globalRepositoryManager.createProjectCentralRepository(name, user); } finally { Util.closeQuietly(repo); } } return "redirect:/project/" + name; }  @RequestMapping(value="/{name:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("hasProjectPermission(#name, VIEW)") String getProject(@PathVariable String name, Model model); @RequestMapping(value="/create", method=RequestMethod.GET) @PreAuthorize("hasApplicationPermission(EDIT_PROJECT)") String createProject(Model model); @RequestMapping(value="/edit/{name:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("hasProjectPermission(#name, EDIT_PROJECT)") String editProject(@PathVariable String name, Model model); @RequestMapping(value="/save", method=RequestMethod.POST) @PreAuthorize("projectExists(#form.name) ? " + "hasProjectPermission(#form.name, EDIT_PROJECT) : " + "hasApplicationPermission(EDIT_PROJECT)") String saveProject(@ModelAttribute @Valid ProjectForm form, BindingResult bindingResult,
			Authentication authentication); @RequestMapping(value="/importSample/{name:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}/json", method=RequestMethod.GET) @PreAuthorize("hasProjectPermission(#name, ADMIN)") @ResponseBody Map<String, Object> importSampleContents(@PathVariable String name); @RequestMapping(value="/delete/{name:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("hasApplicationPermission(EDIT_PROJECT)") String deleteProject(@PathVariable String name, Authentication authentication); @ModelAttribute ProjectForm createProjectForm(@RequestParam(required=false) String name,
			@RequestParam(required=false) String originalName);  }
@Test public void saveProject() throws IOException, GitAPIException { ProjectForm projectForm = new ProjectForm(PROJECT, null); BindingResult bindingResult = new BeanPropertyBindingResult(projectForm, "projectForm"); String view = projectController.saveProject(projectForm, bindingResult, authentication); assertEquals("/project/" + PROJECT, removeViewPrefix(view)); assertRedirect(view); verify(repoManager).createProjectCentralRepository(PROJECT, USER); }
@Test public void saveProjectButExists() throws IOException, GitAPIException { when(repoManager.listProjects()).thenReturn(Lists.newArrayList(PROJECT)); ProjectForm projectForm = new ProjectForm(PROJECT, PROJECT); BindingResult bindingResult = new BeanPropertyBindingResult(projectForm, "projectForm"); String view = projectController.saveProject(projectForm, bindingResult, authentication); assertEquals("/project/" + PROJECT, removeViewPrefix(view)); assertRedirect(view); verify(repoManager, never()).createProjectCentralRepository(anyString(), any(User.class)); }