Commit 993d6795 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

overlay controller properaly handles undefined project

parent 20e9b0fb
......@@ -11,9 +11,7 @@ import org.springframework.security.access.prepost.*;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import javassist.tools.rmi.ObjectNotFoundException;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.api.QueryException;
import lcsb.mapviewer.api.*;
import lcsb.mapviewer.model.cache.FileEntry;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.services.interfaces.IUserService;
......@@ -39,7 +37,7 @@ public class OverlayController extends BaseController {
public List<Map<String, Object>> getOverlayList(
@PathVariable(value = "projectId") String projectId,
@RequestParam(value = "creator", defaultValue = "") String creator,
@RequestParam(value = "publicOverlay", defaultValue = "false") boolean publicOverlay) {
@RequestParam(value = "publicOverlay", defaultValue = "false") boolean publicOverlay) throws lcsb.mapviewer.api.ObjectNotFoundException {
return overlayRestImp.getOverlayList(projectId).stream()
.filter(overlay -> !publicOverlay || (Boolean) overlay.get("publicOverlay"))
.filter(
......@@ -141,7 +139,7 @@ public class OverlayController extends BaseController {
throws QueryException, IOException {
Map<String, Object> node = parseBody(body);
Map<String, Object> data = getData(node, "overlay");
return overlayRestImp.updateOverlay(overlayId, data);
return overlayRestImp.updateOverlay(projectId, overlayId, data);
}
@PreAuthorize("hasAuthority('IS_ADMIN')" +
......
......@@ -55,10 +55,10 @@ public class OverlayRestImpl extends BaseRestImpl {
this.layoutDao = layoutDao;
}
public List<Map<String, Object>> getOverlayList(String projectId) {
public List<Map<String, Object>> getOverlayList(String projectId) throws ObjectNotFoundException {
Project project = getProjectService().getProjectByProjectId(projectId);
if (project == null) {
return new ArrayList<>();
throw new ObjectNotFoundException("Project with given id doesn't exist");
}
return overlaysToMap(layoutService.getLayoutsByProject(project));
}
......@@ -125,10 +125,9 @@ public class OverlayRestImpl extends BaseRestImpl {
}
List<Map<String, Object>> result = new ArrayList<>();
getOverlay(projectId, overlayId);
Model model = getModelService().getLastModelByProjectId(projectId);
if (model == null) {
throw new QueryException("Project with given id doesn't exist");
}
List<Pair<Element, ColorSchema>> speciesList = layoutService.getAliasesForLayout(model, overlayId);
for (Pair<Element, ColorSchema> elementDataOverlay : speciesList) {
Map<String, Object> element = new TreeMap<>();
......@@ -149,45 +148,40 @@ public class OverlayRestImpl extends BaseRestImpl {
public Map<String, Object> getOverlayById(String projectId, Integer overlayId)
throws QueryException {
Layout overlay = getOverlay(projectId, overlayId);
return overlayToMap(overlay);
}
private Layout getOverlay(String projectId, Integer overlayId) throws 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");
}
Layout overlay = layoutService.getLayoutById(overlayId);
if (overlay == null) {
throw new QueryException("Overlay with given id doesn't exist");
throw new ObjectNotFoundException("Overlay with given id doesn't exist");
}
return overlayToMap(overlay);
if (overlay.getProject().getId() != model.getProject().getId()) {
throw new ObjectNotFoundException("Overlay with given id doesn't exist");
}
return overlay;
}
public FileEntry getOverlaySource(String projectId, Integer overlayId)
throws QueryException {
Model model = getModelService().getLastModelByProjectId(projectId);
if (model == null) {
throw new QueryException("Project with given id doesn't exist");
}
try {
Layout layout = layoutService.getLayoutById(overlayId);
if (layout == null) {
throw new QueryException("Invalid overlay id");
}
// lazy initialization issue
layout.getInputData().getFileContent();
return layout.getInputData();
} catch (NumberFormatException e) {
throw new QueryException("Invalid overlay id");
}
Layout overlay = getOverlay(projectId, overlayId);
// lazy initialization issue
overlay.getInputData().getFileContent();
return overlay.getInputData();
}
public Map<String, Object> updateOverlay(Integer overlayId, Map<String, Object> overlayData) throws QueryException {
public Map<String, Object> updateOverlay(String projectId, Integer overlayId, Map<String, Object> overlayData)
throws QueryException {
if (overlayData == null) {
throw new QueryException("overlay field cannot be undefined");
}
try {
Layout layout = layoutService.getLayoutById(overlayId);
if (layout == null) {
throw new ObjectNotFoundException("overlay doesn't exist");
}
Layout layout = getOverlay(projectId, overlayId);
for (String key : overlayData.keySet()) {
Object value = overlayData.get(key);
if (key.equalsIgnoreCase("description")) {
......@@ -232,30 +226,18 @@ public class OverlayRestImpl extends BaseRestImpl {
public Map<String, Object> removeOverlay(String projectId, Integer overlayId)
throws QueryException, IOException {
Project project = getProjectService().getProjectByProjectId(projectId);
if (project == null) {
throw new ObjectNotFoundException("Project with given id doesn't exist");
}
try {
Layout layout = layoutService.getLayoutById(overlayId);
if (layout == null) {
throw new ObjectNotFoundException("Overlay doesn't exist");
Layout layout = getOverlay(projectId, overlayId);
layoutService.removeLayout(layout, null);
List<Layout> overlays = layoutService.getLayoutsByProject(layout.getProject());
overlays.sort(Layout.ORDER_COMPARATOR);
for (int i = 0; i < overlays.size(); i++) {
Layout overlay = overlays.get(i);
if (overlay.getOrderIndex() != i + 1) {
overlay.setOrderIndex(i + 1);
layoutService.updateLayout(overlay);
}
layoutService.removeLayout(layout, null);
List<Layout> overlays = layoutService.getLayoutsByProject(project);
overlays.sort(Layout.ORDER_COMPARATOR);
for (int i = 0; i < overlays.size(); i++) {
Layout overlay = overlays.get(i);
if (overlay.getOrderIndex() != i + 1) {
overlay.setOrderIndex(i + 1);
layoutService.updateLayout(overlay);
}
}
return okStatus();
} catch (NumberFormatException e) {
throw new QueryException("Invalid overlay id: " + overlayId);
}
return okStatus();
}
public Map<String, Object> addOverlay(String projectId, String name, String description, String content,
......@@ -263,7 +245,7 @@ public class OverlayRestImpl extends BaseRestImpl {
throws QueryException, IOException, SecurityException {
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");
}
ColorSchemaType colorSchemaType = ColorSchemaType.GENERIC;
if (type != null && !type.equals("")) {
......@@ -327,10 +309,9 @@ public class OverlayRestImpl extends BaseRestImpl {
public Map<String, Object> getOverlayElement(String projectId, Integer modelId, Integer overlayId,
Integer elementId, String elementType, String columns) throws QueryException {
getOverlay(projectId, overlayId);
Model topModel = getModelService().getLastModelByProjectId(projectId);
if (topModel == null) {
throw new QueryException("Project with given id doesn't exist");
}
Model model = topModel.getSubmodelById(modelId);
String[] columnSet;
......
......@@ -69,7 +69,7 @@ public class OverlayRestImplTest extends RestTestFunctions {
Integer id = Integer.valueOf(result.get("idObject").toString());
Map<String, Object> data = new HashMap<>();
data.put("name", "xyz");
result = overlayRest.updateOverlay(id, data);
result = overlayRest.updateOverlay(projectId, id, data);
assertNotNull(result);
} finally {
projectService.removeProject(projectDao.getProjectByProjectId(projectId), null, false);
......@@ -87,7 +87,7 @@ public class OverlayRestImplTest extends RestTestFunctions {
Integer id = Integer.valueOf(result.get("idObject").toString());
Map<String, Object> data = new HashMap<>();
data.put("name", "");
result = overlayRest.updateOverlay(id, data);
result = overlayRest.updateOverlay(projectId, id, data);
assertNotNull(result);
} finally {
projectService.removeProject(projectDao.getProjectByProjectId(projectId), null, false);
......
......@@ -1049,5 +1049,161 @@ public class OverlayControllerIntegrationTest extends ControllerIntegrationTest
return createOverlay(project, user);
}
@Test
public void testListOverlaysWithUnknownProject() throws Exception {
createAdmin(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
RequestBuilder request = get("/projects/*/overlays/")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.session(session);
mockMvc.perform(request)
.andExpect(status().isNotFound());
}
@Test
public void testGetOverlayByIdWithUnknownProject() throws Exception {
User admin = createAdmin(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
Layout overlay = createOverlay(admin);
MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
RequestBuilder request = get("/projects/*/overlays/" + overlay.getId() + "/")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.session(session);
mockMvc.perform(request)
.andExpect(status().isNotFound());
}
@Test
public void testGetBioEntitiesForOverlayWithUndefinedProject() throws Exception {
User admin = createAdmin(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
Layout overlay = createOverlay(admin);
MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
RequestBuilder request = get(
"/projects/*/overlays/" + overlay.getId() + "/models/" + map.getId() + "/bioEntities/")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.session(session);
mockMvc.perform(request)
.andExpect(status().isNotFound());
}
@Test
public void testGetReactionsForOverlayWithUndefinedProject() throws Exception {
User admin = createAdmin(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
Layout overlay = createOverlay(admin);
MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
RequestBuilder request = get("/projects/*/overlays/" + overlay.getId() + "/models/" + map.getId()
+ "/bioEntities/reactions/" + reaction.getId() + "/")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.session(session);
mockMvc.perform(request)
.andExpect(status().isNotFound());
}
@Test
public void testGetElementsForOverlayWithUndefinedProject() throws Exception {
User admin = createAdmin(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
Layout overlay = createOverlay(admin);
MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
RequestBuilder request = get("/projects/*/overlays/" + overlay.getId() + "/models/" + map.getId()
+ "/bioEntities/elements/" + element.getId() + "/")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.session(session);
mockMvc.perform(request)
.andExpect(status().isNotFound());
}
@Test
public void testCreateOverlayWithUndefinedProjectId() throws Exception {
User admin = createAdmin(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
UploadedFileEntry file = createFile("elementIdentifier\tvalue\n\t-1", admin);
MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("fileId", String.valueOf(file.getId())),
new BasicNameValuePair("name", "overlay name"),
new BasicNameValuePair("description", "overlay name"),
new BasicNameValuePair("filename", "overlay name"),
new BasicNameValuePair("googleLicenseConsent", "overlay name"),
new BasicNameValuePair("type", "GENERIC"))));
RequestBuilder request = post("/projects/*/overlays/")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(session);
mockMvc.perform(request)
.andExpect(status().isNotFound());
}
@Test
public void testRemoveOverlayWithUndefinedProject() throws Exception {
User admin = createAdmin(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
Layout overlay = createOverlay(admin);
MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
RequestBuilder request = delete("/projects/*/overlays/" + overlay.getId())
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.session(session);
mockMvc.perform(request)
.andExpect(status().isNotFound());
}
@Test
public void testUpdateOverlayWithUndefinedProject() throws Exception {
User admin = createAdmin(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
Layout overlay = createOverlay(admin);
String body = "{\"overlay\":{}}";
MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
RequestBuilder request = patch("/projects/*/overlays/" + overlay.getId())
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(session);
mockMvc.perform(request)
.andExpect(status().isNotFound());
}
@Test
public void testAccessOverlaySourceWithUndefinedProject() throws Exception {
User admin = createAdmin(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
Layout overlay = createOverlay(admin);
MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
RequestBuilder request = get("/projects/*/overlays/" + overlay.getId() + ":downloadSource")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.session(session);
mockMvc.perform(request)
.andExpect(status().isNotFound());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment