ProjectController { @Secured({Roles.ROLE_USER}) @RequestMapping(value = "/project/{projectId}", method = RequestMethod.GET) public Project getProject(@PathVariable String projectId, Authentication auth) { UserEntity userEntity = userFromAuthentication(auth); final Project project = projectService.getProject(projectId, userEntity); if (!mayViewProjectFilter(project, auth)) { throw new ForbiddenException(); } return project; }  @Secured({Roles.ROLE_USER}) @RequestMapping(value = "/projects", method = RequestMethod.GET) @JsonView(Project.ProjectSummaryView.class) List<Project> getProjects(Authentication auth); @Secured({Roles.ROLE_USER}) @RequestMapping(value = "/project/{projectId}", method = RequestMethod.GET) Project getProject(@PathVariable String projectId, Authentication auth); @Secured(Roles.ROLE_USER) @ResponseStatus(HttpStatus.CREATED) @RequestMapping(value = "/project", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) Project addProject(@RequestBody @Valid Project project, Principal principal); @Secured(Roles.ROLE_USER) @ResponseStatus(HttpStatus.CREATED) @RequestMapping(value = "/project/{projectId}/pledges", method = RequestMethod.POST) void pledgeProject(@PathVariable String projectId, @RequestBody @Valid Pledge pledge, Principal principal); @Secured(Roles.ROLE_ADMIN) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/project/{projectId}/status", method = RequestMethod.PATCH) Project modifyProjectStatus(@PathVariable("projectId") String projectId, @RequestBody @Valid @NotNull ProjectStatusUpdate newStatus, Principal principal); @Secured(Roles.ROLE_USER) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/project/{projectId}", method = RequestMethod.PUT) Project modifyProjectMasterdata(@PathVariable("projectId") String projectId, @RequestBody @Valid @NotNull Project modifiedProject, Principal principal); @Secured(Roles.ROLE_USER) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/projects/{projectId}/likes", method = RequestMethod.POST) void likeProject(@PathVariable("projectId") String projectId, Principal principal); @Secured(Roles.ROLE_USER) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/projects/{projectId}/likes", method = RequestMethod.DELETE) void unlikeProject(@PathVariable("projectId") String projectId, Principal principal); @Secured(Roles.ROLE_USER) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/projects/{projectId}/attachments", method = RequestMethod.POST) Attachment addProjectAttachment(@PathVariable("projectId") String projectId, @RequestParam("file") MultipartFile file, Principal principal); @Secured({Roles.ROLE_TRUSTED_ANONYMOUS, Roles.ROLE_USER}) @RequestMapping(value = "/projects/{projectId}/attachments/{fileReference}", method = RequestMethod.GET) ResponseEntity<InputStreamResource> serveProjectAttachment(@PathVariable("projectId") String projectId, @PathVariable("fileReference") String fileReference); @Secured(Roles.ROLE_USER) @ResponseStatus(HttpStatus.NO_CONTENT) @RequestMapping(value = "/projects/{projectId}/attachments/{fileReference}", method = RequestMethod.DELETE) void deleteProjectAttachment(@PathVariable("projectId") String projectId, @PathVariable("fileReference") String fileReference, Principal principal);  }
@Test public void getProject_shouldRespondWith404OnInvalidProjectId() throws Exception { when(projectService.getProject(anyString(), any(UserEntity.class))).thenThrow(new ResourceNotFoundException()); mockMvc.perform(get("/project/{projectId}", "foo bah bah")) .andExpect(status().isNotFound()); }
@Test public void getProject_shouldRespondWith403IfTheUserMayNotSeeThisProject() throws Exception { final UserEntity userEntity = userEntity(USER_EMAIL, USER_FIRST_NAME, USER_LAST_NAME, Roles.ROLE_USER); final UserEntity creator = userEntity(ADMIN1_EMAIL, ADMIN1_FIRST_NAME, ADMIN1_LAST_NAME, Roles.ROLE_USER); final String projectId = "existingProjectId"; when(projectService.getProject(eq(projectId), eq(userEntity))) .thenReturn(toCreatedProject(project("title", "descr", "shortDescr", 44, ProjectStatus.PROPOSED), creator)); mockMvc.perform(get("/project/{projectId}", projectId) .principal(authentication(userEntity)) ).andExpect(status().isForbidden()); }
@Test public void getProject_shouldReturnSingleProjectSuccessfullyWhenProjectIsPublished() throws Exception { final UserEntity userEntity = userEntity(USER_EMAIL, USER_FIRST_NAME, USER_LAST_NAME, Roles.ROLE_USER); final UserEntity creator = userEntity(ADMIN1_EMAIL, ADMIN1_FIRST_NAME, ADMIN1_LAST_NAME, Roles.ROLE_USER); final String projectId = "existingProjectId"; final Project expProjcet = toCreatedProject(project("title", "descr", "shortDescr", 44, ProjectStatus.PUBLISHED), creator); when(projectService.getProject(eq(projectId), eq(userEntity))).thenReturn(expProjcet); MvcResult mvcResult = mockMvc.perform(get("/project/{projectId}", projectId) .principal(authentication(userEntity))) .andExpect(status().isOk()) .andReturn(); assertThat(mapper.readValue(mvcResult.getResponse().getContentAsString(), Project.class), is(equalTo(expProjcet))); }
@Test public void getProject_shouldReturnSingleProjectSuccessfullyWhenProjectIsPublishedForAnonymousToo() throws Exception { final UserEntity creator = userEntity(ADMIN1_EMAIL, ADMIN1_FIRST_NAME, ADMIN1_LAST_NAME, Roles.ROLE_USER); final String projectId = "existingProjectId"; final Project expProjcet = toCreatedProject(project("title", "descr", "shortDescr", 44, ProjectStatus.PUBLISHED), creator); when(projectService.getProject(eq(projectId), eq(null))).thenReturn(expProjcet); MvcResult mvcResult = mockMvc.perform(get("/project/{projectId}", projectId) .principal(anonymousAuthentication())) .andExpect(status().isOk()) .andReturn(); assertThat(mapper.readValue(mvcResult.getResponse().getContentAsString(), Project.class), is(equalTo(expProjcet))); }
@Test public void getProject_shouldReturnProjectLikeCountAndUserLikeStatus() throws Exception { final UserEntity creator = userEntity(ADMIN1_EMAIL, ADMIN1_FIRST_NAME, ADMIN1_LAST_NAME, Roles.ROLE_USER); final String projectId = "existingProjectId"; final Project expProjcet = toCreatedProject(project("title", "descr", "shortDescr", 44, ProjectStatus.PUBLISHED), creator); when(projectService.getProject(eq(projectId), eq(null))).thenReturn(expProjcet); mockMvc.perform(get("/project/{projectId}", projectId) .principal(anonymousAuthentication())) .andExpect(jsonPath("$.likeStatus", nullValue())) .andExpect(jsonPath("$.likeCount", is(0))) .andExpect(status().isOk()); }