Commit cfe111ca authored by Piotr Gawron's avatar Piotr Gawron
Browse files

forbid to create overlay with empty name

parent 94ce9fcd
......@@ -438,8 +438,11 @@ public class ProjectRestImpl extends BaseRestImpl {
entry = new ModelZipEntryFile(filename, name, root, mapping, mapType);
} else if ("OVERLAY".equalsIgnoreCase(entryType)) {
String name = (String) data.get("zip-entries[" + fileIndex + "][_data][name]").get(0);
String description = (String) data.get("zip-entries[" + fileIndex + "][_data][description]").get(0);
String name = getFirstEntry(data, "zip-entries[" + fileIndex + "][_data][name]");
if (name == null || name.trim().isEmpty()) {
throw new QueryException("zip-entries[" + fileIndex + "][_data][name] cannot be empty");
}
String description = getFirstEntry(data, "zip-entries[" + fileIndex + "][_data][description]");
entry = new LayoutZipEntryFile(filename, name, description);
} else if ("IMAGE".equalsIgnoreCase(entryType)) {
entry = new ImageZipEntryFile(filename);
......@@ -455,6 +458,15 @@ public class ProjectRestImpl extends BaseRestImpl {
return result;
}
@SuppressWarnings("unchecked")
private <T> T getFirstEntry(MultiValueMap<String, Object> data, String string) {
if (data.get(string) != null) {
return (T) data.get(string).get(0);
} else {
return null;
}
}
private Boolean getBoolValue(List<Object> list, boolean defaultValue) {
if (list == null) {
return defaultValue;
......
......@@ -223,10 +223,14 @@ abstract public class ControllerIntegrationTest {
}
protected UploadedFileEntry createFile(String content, User user) {
return createFile(content.getBytes(), user);
}
protected UploadedFileEntry createFile(byte[] content, User user) {
UploadedFileEntry file = new UploadedFileEntry();
file.setFileContent(content.getBytes());
file.setFileContent(content);
file.setOriginalFileName("test_file");
file.setLength(content.getBytes().length);
file.setLength(content.length);
file.setOwner(user);
fileDao.add(file);
return file;
......@@ -271,6 +275,10 @@ abstract public class ControllerIntegrationTest {
}
protected UploadedFileEntry createFileInSeparateThread(String content, User user) throws Exception {
return createFileInSeparateThread(content.getBytes(), user);
}
protected UploadedFileEntry createFileInSeparateThread(byte[] content, User user) throws Exception {
return callInSeparateThread(() -> {
return createFile(content, user);
});
......@@ -282,7 +290,7 @@ abstract public class ControllerIntegrationTest {
project = projectDao.getProjectByProjectId(projectId);
projectDao.evict(project);
Thread.sleep(100);
} while (project.getStatus() != ProjectStatus.DONE);
} while (project.getStatus() != ProjectStatus.DONE && project.getStatus() != ProjectStatus.FAIL);
}
public void removeProjectInSeparateThread(Project project) throws Exception {
......
package lcsb.mapviewer.web;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.nio.file.Files;
......@@ -234,7 +232,7 @@ public class ProjectControllerIntegrationTestWithoutTransaction extends Controll
request = delete("/projects/" + testProject2)
.session(createSession(BUILT_IN_ADMIN_LOGIN, BUILT_IN_ADMIN_PASSWORD));
mockMvc.perform(request).andExpect(status().is2xxSuccessful());
} finally {
......@@ -251,4 +249,82 @@ public class ProjectControllerIntegrationTestWithoutTransaction extends Controll
}
}
@Test
public void addComplexProjectWithInvalidOverlayName() throws Exception {
User admin = userService.getUserByLogin(BUILT_IN_ADMIN_LOGIN);
UploadedFileEntry fileEntry = createFileInSeparateThread(
Files.readAllBytes(Paths.get("./src/test/resources/complex_model_with_layouts.zip")), admin);
try {
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("file-id", String.valueOf(fileEntry.getId())),
new BasicNameValuePair("mapCanvasType", "OPEN_LAYERS"),
new BasicNameValuePair("parser", "lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser"),
new BasicNameValuePair("zip-entries[0][_type]", "MAP"),
new BasicNameValuePair("zip-entries[0][_filename]", "main.xml"),
new BasicNameValuePair("zip-entries[0][_data][root]", "true"),
new BasicNameValuePair("zip-entries[0][_data][name]", "s1"),
new BasicNameValuePair("zip-entries[0][_data][type][id]", "UNKNOWN"),
new BasicNameValuePair("zip-entries[0][_data][type][name]", "Unknown"),
new BasicNameValuePair("zip-entries[1][_type]", "OVERLAY"),
new BasicNameValuePair("zip-entries[1][_filename]", "layouts/goodSchema.txt"),
new BasicNameValuePair("zip-entries[1][_data][name]", "")
)));
RequestBuilder request = post("/projects/" + TEST_PROJECT)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(createSession(BUILT_IN_ADMIN_LOGIN, BUILT_IN_ADMIN_PASSWORD));
mockMvc.perform(request).andExpect(status().isBadRequest());
} finally {
removeFileInSeparateThread(fileEntry);
}
}
@Test
public void addComplexProject() throws Exception {
User admin = userService.getUserByLogin(BUILT_IN_ADMIN_LOGIN);
UploadedFileEntry fileEntry = createFileInSeparateThread(
Files.readAllBytes(Paths.get("./src/test/resources/complex_model_with_layouts.zip")), admin);
try {
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("file-id", String.valueOf(fileEntry.getId())),
new BasicNameValuePair("mapCanvasType", "OPEN_LAYERS"),
new BasicNameValuePair("parser", "lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser"),
new BasicNameValuePair("zip-entries[0][_type]", "MAP"),
new BasicNameValuePair("zip-entries[0][_filename]", "main.xml"),
new BasicNameValuePair("zip-entries[0][_data][root]", "true"),
new BasicNameValuePair("zip-entries[0][_data][name]", "s1"),
new BasicNameValuePair("zip-entries[0][_data][type][id]", "UNKNOWN"),
new BasicNameValuePair("zip-entries[0][_data][type][name]", "Unknown"),
new BasicNameValuePair("zip-entries[1][_type]", "OVERLAY"),
new BasicNameValuePair("zip-entries[1][_filename]", "layouts/goodSchema.txt"),
new BasicNameValuePair("zip-entries[1][_data][name]", "test-o")
)));
RequestBuilder request = post("/projects/" + TEST_PROJECT)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(createSession(BUILT_IN_ADMIN_LOGIN, BUILT_IN_ADMIN_PASSWORD));
mockMvc.perform(request).andExpect(status().is2xxSuccessful());
} finally {
callInSeparateThread(() -> {
try {
waitForProjectToFinishLoading(TEST_PROJECT);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
});
removeProjectInSeparateThread(TEST_PROJECT);
}
}
}
Supports Markdown
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