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 6d060e5578398e83dc55d225111990676a5acd1d..37f5050da1ccba7a7dafe49278fc3d518bd52777 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 432add8ee7a07f2b3cb912dcc9ba3ffbb8b6727c..a7e43f53026a8724d59ac18b4d12f88e60a7a0b6 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 df0d467a514896a88b06580bb23adf5e97f8df7c..769bfcf888e2a142499d003977ee61c13db8fa77 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 607a27e11b64605c7fe5d89ddb0815f0d18219cb..c779a724e2f144891caba5b6aac1747e5c60cfc0 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 0000000000000000000000000000000000000000..a1f33cbaa925c2109e3bba25c974a688a5d8518f --- /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); + } + } + +}