diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/projects/drugs/DrugController.java b/rest-api/src/main/java/lcsb/mapviewer/api/projects/drugs/DrugController.java index fe9519beb2825c671eb861238ac872365c4c3e37..544e49100178119f2733f28952e3a0f3918003fa 100644 --- a/rest-api/src/main/java/lcsb/mapviewer/api/projects/drugs/DrugController.java +++ b/rest-api/src/main/java/lcsb/mapviewer/api/projects/drugs/DrugController.java @@ -8,8 +8,7 @@ import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import lcsb.mapviewer.annotation.services.DrugSearchException; -import lcsb.mapviewer.api.BaseController; -import lcsb.mapviewer.api.QueryException; +import lcsb.mapviewer.api.*; @RestController @RequestMapping(value = "/projects/{projectId}/", produces = MediaType.APPLICATION_JSON_VALUE) @@ -28,7 +27,7 @@ public class DrugController extends BaseController { @PathVariable(value = "projectId") String projectId, @RequestParam(value = "columns", defaultValue = "") String columns, @RequestParam(value = "query", defaultValue = "") String query, - @RequestParam(value = "target", defaultValue = "") String target) throws QueryException { + @RequestParam(value = "target", defaultValue = "") String target) throws QueryException, ObjectNotFoundException { if (!query.equals("")) { return drugController.getDrugsByQuery(projectId, columns, query); } else if (target.contains(":")) { @@ -43,7 +42,7 @@ public class DrugController extends BaseController { @PreAuthorize("hasAnyAuthority('IS_ADMIN', 'READ_PROJECT:' + #projectId)") @GetMapping(value = "drugs/suggestedQueryList") public List<String> getSuggestedQueryList(@PathVariable(value = "projectId") String projectId) - throws DrugSearchException { + throws DrugSearchException, ObjectNotFoundException { return drugController.getSuggestedQueryList(projectId); } diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/projects/drugs/DrugRestImpl.java b/rest-api/src/main/java/lcsb/mapviewer/api/projects/drugs/DrugRestImpl.java index 420e788f385824bc2a240c0466e8e27e1d56e4c1..0966b332183f52e94a99fffa68e0aee8c6f18ab6 100644 --- a/rest-api/src/main/java/lcsb/mapviewer/api/projects/drugs/DrugRestImpl.java +++ b/rest-api/src/main/java/lcsb/mapviewer/api/projects/drugs/DrugRestImpl.java @@ -41,10 +41,10 @@ public class DrugRestImpl extends BaseRestImpl { } public List<Map<String, Object>> getDrugsByQuery(String projectId, String columns, String query) - throws QueryException { + throws QueryException, ObjectNotFoundException { Model model = getModelService().getLastModelByProjectId(projectId); if (model == null) { - throw new QueryException("Project with given id doesn't exist"); + throw new ObjectNotFoundException("Project with given id doesn't exist"); } Project project = getProjectService().getProjectByProjectId(projectId); @@ -128,10 +128,10 @@ public class DrugRestImpl extends BaseRestImpl { } public List<Map<String, Object>> getDrugsByTarget(String projectId, String targetType, String targetId, - String columns) throws QueryException { + String columns) throws QueryException, ObjectNotFoundException { Model model = getModelService().getLastModelByProjectId(projectId); if (model == null) { - throw new QueryException("Project with given id doesn't exist"); + throw new ObjectNotFoundException("Project with given id doesn't exist"); } Project project = getProjectService().getProjectByProjectId(projectId); @@ -176,8 +176,11 @@ public class DrugRestImpl extends BaseRestImpl { return targets; } - public List<String> getSuggestedQueryList(String projectId) throws DrugSearchException { + public List<String> getSuggestedQueryList(String projectId) throws DrugSearchException, ObjectNotFoundException { Project project = getProjectService().getProjectByProjectId(projectId); + if (project == null) { + throw new ObjectNotFoundException("Project with given id doesn't exist"); + } return drugService.getSuggestedQueryList(project, project.getOrganism()); } diff --git a/web/src/test/java/lcsb/mapviewer/web/DrugControllerIntegrationTest.java b/web/src/test/java/lcsb/mapviewer/web/DrugControllerIntegrationTest.java index c4ed2e324c0a123796417e226580d68ca98f7dcf..63d46b98004fe305855a2ddf6cebfa6865b9e1f1 100644 --- a/web/src/test/java/lcsb/mapviewer/web/DrugControllerIntegrationTest.java +++ b/web/src/test/java/lcsb/mapviewer/web/DrugControllerIntegrationTest.java @@ -50,6 +50,30 @@ public class DrugControllerIntegrationTest extends ControllerIntegrationTest { .andExpect(status().is2xxSuccessful()); } + @Test + public void testSearchDrugsInUndefinedProject() throws Exception { + MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD); + + RequestBuilder request = get("/projects/*/drugs:search?query=xyz") + .contentType(MediaType.APPLICATION_FORM_URLENCODED) + .session(session); + + mockMvc.perform(request) + .andExpect(status().isNotFound()); + } + + @Test + public void testSearchDrugsByTargetInUndefinedProject() throws Exception { + MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD); + + RequestBuilder request = get("/projects/*/drugs:search?target=ALIAS:123") + .contentType(MediaType.APPLICATION_FORM_URLENCODED) + .session(session); + + mockMvc.perform(request) + .andExpect(status().isNotFound()); + } + @Test public void testGetSuggestedList() throws Exception { MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD); @@ -62,4 +86,16 @@ public class DrugControllerIntegrationTest extends ControllerIntegrationTest { .andExpect(status().is2xxSuccessful()); } + @Test + public void testGetSuggestedListWithUndefinedProject() throws Exception { + MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD); + + RequestBuilder request = get("/projects/*/drugs/suggestedQueryList") + .contentType(MediaType.APPLICATION_FORM_URLENCODED) + .session(session); + + mockMvc.perform(request) + .andExpect(status().isNotFound()); + } + }