From b9c02b5f65216d2e0a4093b9df4a5fee414b164b Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Fri, 26 Jul 2019 15:42:42 +0200 Subject: [PATCH] creating project was crashing --- .../mapviewer/services/impl/UserService.java | 4 + .../mapviewer/web/AllIntegrationTests.java | 1 + .../web/ControllerIntegrationTest.java | 30 ++++++++ ...ontrollerIntegrationTestForAsyncCalls.java | 28 ------- ...llerIntegrationTestWithoutTransaction.java | 77 +++++++++++++++++++ 5 files changed, 112 insertions(+), 28 deletions(-) create mode 100644 web/src/test/java/lcsb/mapviewer/web/ProjectControllerIntegrationTestWithoutTransaction.java diff --git a/service/src/main/java/lcsb/mapviewer/services/impl/UserService.java b/service/src/main/java/lcsb/mapviewer/services/impl/UserService.java index 6d060e5578..37f5050da1 100644 --- a/service/src/main/java/lcsb/mapviewer/services/impl/UserService.java +++ b/service/src/main/java/lcsb/mapviewer/services/impl/UserService.java @@ -75,24 +75,28 @@ public class UserService implements IUserService { @Override public void grantUserPrivilege(User user, PrivilegeType type) { + user = getUserByLogin(user.getLogin()); user.addPrivilege(privilegeService.getPrivilege(type)); userDao.update(user); } @Override public void grantUserPrivilege(User user, PrivilegeType type, String objectId) { + user = getUserByLogin(user.getLogin()); user.addPrivilege(privilegeService.getPrivilege(type, objectId)); userDao.update(user); } @Override public void revokeUserPrivilege(User user, PrivilegeType type) { + user = getUserByLogin(user.getLogin()); user.removePrivilege(privilegeService.getPrivilege(type)); userDao.update(user); } @Override public void revokeUserPrivilege(User user, PrivilegeType type, String objectId) { + user = getUserByLogin(user.getLogin()); user.removePrivilege(privilegeService.getPrivilege(type, objectId)); userDao.update(user); } diff --git a/web/src/test/java/lcsb/mapviewer/web/AllIntegrationTests.java b/web/src/test/java/lcsb/mapviewer/web/AllIntegrationTests.java index 432add8ee7..a7e43f5302 100644 --- a/web/src/test/java/lcsb/mapviewer/web/AllIntegrationTests.java +++ b/web/src/test/java/lcsb/mapviewer/web/AllIntegrationTests.java @@ -20,6 +20,7 @@ import org.junit.runners.Suite.SuiteClasses; PluginControllerIntegrationTest.class, ProjectControllerIntegrationTest.class, ProjectControllerIntegrationTestForAsyncCalls.class, + ProjectControllerIntegrationTestWithoutTransaction.class, PublicationsControllerIntegrationTest.class, ReactionControllerIntegrationTest.class, SpringSecurityGeneralIntegrationTest.class, diff --git a/web/src/test/java/lcsb/mapviewer/web/ControllerIntegrationTest.java b/web/src/test/java/lcsb/mapviewer/web/ControllerIntegrationTest.java index df0d467a51..769bfcf888 100644 --- a/web/src/test/java/lcsb/mapviewer/web/ControllerIntegrationTest.java +++ b/web/src/test/java/lcsb/mapviewer/web/ControllerIntegrationTest.java @@ -27,6 +27,7 @@ import org.springframework.web.context.WebApplicationContext; import lcsb.mapviewer.common.MinervaLoggerAppender; import lcsb.mapviewer.common.UnitTestFailedWatcher; import lcsb.mapviewer.model.Project; +import lcsb.mapviewer.model.ProjectStatus; import lcsb.mapviewer.model.cache.UploadedFileEntry; import lcsb.mapviewer.model.graphics.PolylineData; import lcsb.mapviewer.model.map.model.ModelData; @@ -232,4 +233,33 @@ abstract public class ControllerIntegrationTest { }); } + protected UploadedFileEntry createFileInSeparateThread(String content, User user) throws Exception { + return callInSeparateThread(() -> { + return createFile(content, user); + }); + } + + protected void waitForProjectToFinishLoading(String projectId) throws InterruptedException { + Project project; + do { + project = projectDao.getProjectByProjectId(projectId); + projectDao.evict(project); + Thread.sleep(100); + } while (project.getStatus() != ProjectStatus.DONE); + } + + public void removeProjectInSeparateThread(Project project) throws Exception { + removeProjectInSeparateThread(project.getProjectId()); + } + + public void removeProjectInSeparateThread(String projectId) throws Exception { + callInSeparateThread(() -> { + Project project = projectDao.getProjectByProjectId(projectId); + if (project != null) { + projectDao.delete(project); + } + return null; + }); + } + } diff --git a/web/src/test/java/lcsb/mapviewer/web/ProjectControllerIntegrationTestForAsyncCalls.java b/web/src/test/java/lcsb/mapviewer/web/ProjectControllerIntegrationTestForAsyncCalls.java index 607a27e11b..c779a724e2 100644 --- a/web/src/test/java/lcsb/mapviewer/web/ProjectControllerIntegrationTestForAsyncCalls.java +++ b/web/src/test/java/lcsb/mapviewer/web/ProjectControllerIntegrationTestForAsyncCalls.java @@ -101,15 +101,6 @@ public class ProjectControllerIntegrationTestForAsyncCalls extends ControllerInt } } - private void waitForProjectToFinishLoading(String projectId) throws InterruptedException { - Project project; - do { - project = projectDao.getProjectByProjectId(projectId); - projectDao.evict(project); - Thread.sleep(100); - } while (project.getStatus() != ProjectStatus.DONE); - } - private void waitForProjectToFinishRemoving(String projectId) throws InterruptedException { Project project; do { @@ -377,11 +368,6 @@ public class ProjectControllerIntegrationTestForAsyncCalls extends ControllerInt } } - private UploadedFileEntry createFileInSeparateThread(String content, User user) throws Exception { - return callInSeparateThread(() -> { - return createFile(content, user); - }); - } private User createCuratorInSeparateThread(String login, String password) throws Exception { return callInSeparateThread(() -> { @@ -401,18 +387,4 @@ public class ProjectControllerIntegrationTestForAsyncCalls extends ControllerInt }); } - private void removeProjectInSeparateThread(Project project) throws Exception { - removeProjectInSeparateThread(project.getProjectId()); - } - - private void removeProjectInSeparateThread(String projectId) throws Exception { - callInSeparateThread(() -> { - Project project = projectDao.getProjectByProjectId(projectId); - if (project != null) { - projectDao.delete(project); - } - return null; - }); - } - } diff --git a/web/src/test/java/lcsb/mapviewer/web/ProjectControllerIntegrationTestWithoutTransaction.java b/web/src/test/java/lcsb/mapviewer/web/ProjectControllerIntegrationTestWithoutTransaction.java new file mode 100644 index 0000000000..a1f33cbaa9 --- /dev/null +++ b/web/src/test/java/lcsb/mapviewer/web/ProjectControllerIntegrationTestWithoutTransaction.java @@ -0,0 +1,77 @@ +package lcsb.mapviewer.web; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Arrays; + +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.web.servlet.RequestBuilder; + +import lcsb.mapviewer.model.cache.UploadedFileEntry; +import lcsb.mapviewer.model.user.User; +import lcsb.mapviewer.services.interfaces.IUserService; + +@RunWith(SpringJUnit4ClassRunner.class) +public class ProjectControllerIntegrationTestWithoutTransaction extends ControllerIntegrationTest { + + private static final String BUILT_IN_ADMIN_PASSWORD = "admin"; + private static final String BUILT_IN_ADMIN_LOGIN = "admin"; + + private static final String TEST_PROJECT = "test_project"; + + Logger logger = LogManager.getLogger(); + @Autowired + private IUserService userService; + + @Before + public void setup() { + } + + @Test + public void addProjectAsCurator() throws Exception { + User admin = userService.getUserByLogin(BUILT_IN_ADMIN_LOGIN); + try { + UploadedFileEntry fileEntry = createFileInSeparateThread( + new String(Files.readAllBytes(Paths.get("./src/test/resources/generic.xml")), "UTF-8"), + admin); + + 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")))); + + 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); + } + } + +} -- GitLab