From c9049a92cfad0e302f756970e226da73ff9ddf5e Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Wed, 9 Aug 2017 16:54:14 +0200 Subject: [PATCH] when accessing comments no need to fetch model data --- .../projects/comments/CommentRestImpl.java | 57 ++--------- .../java/lcsb/mapviewer/api/AllRestTests.java | 5 +- .../api/comment/CommentRestImplTest.java | 46 --------- .../api/projects/AllProjectTests.java | 2 + .../comments}/AllCommentTests.java | 2 +- .../comments/CommentRestImplTest.java | 95 +++++++++++++++++++ 6 files changed, 107 insertions(+), 100 deletions(-) delete mode 100644 rest-api/src/test/java/lcsb/mapviewer/api/comment/CommentRestImplTest.java rename rest-api/src/test/java/lcsb/mapviewer/api/{comment => projects/comments}/AllCommentTests.java (78%) create mode 100644 rest-api/src/test/java/lcsb/mapviewer/api/projects/comments/CommentRestImplTest.java diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/projects/comments/CommentRestImpl.java b/rest-api/src/main/java/lcsb/mapviewer/api/projects/comments/CommentRestImpl.java index 995ce6afe7..35da482c29 100644 --- a/rest-api/src/main/java/lcsb/mapviewer/api/projects/comments/CommentRestImpl.java +++ b/rest-api/src/main/java/lcsb/mapviewer/api/projects/comments/CommentRestImpl.java @@ -25,8 +25,6 @@ import lcsb.mapviewer.persist.dao.map.ReactionDao; import lcsb.mapviewer.persist.dao.map.species.ElementDao; import lcsb.mapviewer.services.SecurityException; import lcsb.mapviewer.services.interfaces.ICommentService; -import lcsb.mapviewer.services.interfaces.IModelService; -import lcsb.mapviewer.services.interfaces.IUserService; import lcsb.mapviewer.services.overlay.IconManager; import lcsb.mapviewer.services.search.data.ElementIdentifier.ElementIdentifierType; import lcsb.mapviewer.services.view.AuthenticationToken; @@ -39,12 +37,6 @@ public class CommentRestImpl extends BaseRestImpl { */ private Logger logger = Logger.getLogger(CommentRestImpl.class); - @Autowired - private IUserService userService; - - @Autowired - private IModelService modelService; - @Autowired private ICommentService commentService; @@ -56,13 +48,12 @@ public class CommentRestImpl extends BaseRestImpl { public List<Map<String, Object>> getCommentList(String token, String projectId, String columns, String elementId, String elementType, String removed) throws SecurityException, QueryException { - AuthenticationToken authenticationToken = userService.getToken(token); - Model model = modelService.getLastModelByProjectId(projectId, authenticationToken); - if (model == null) { + AuthenticationToken authenticationToken = getUserService().getToken(token); + Project project = getProjectService().getProjectByProjectId(projectId, authenticationToken); + if (project == null) { throw new QueryException("Project with given id doesn't exist"); } - Project project = model.getProject(); - boolean isAdmin = userService.userHasPrivilege(authenticationToken, PrivilegeType.EDIT_COMMENTS_PROJECT, project); + boolean isAdmin = getUserService().userHasPrivilege(authenticationToken, PrivilegeType.EDIT_COMMENTS_PROJECT, project); Set<String> columnsSet = createCommentColumnSet(columns, isAdmin); List<Map<String, Object>> result = new ArrayList<>(); @@ -100,40 +91,6 @@ public class CommentRestImpl extends BaseRestImpl { return result; } - /** - * @return the userService - * @see #userService - */ - public IUserService getUserService() { - return userService; - } - - /** - * @param userService - * the userService to set - * @see #userService - */ - public void setUserService(IUserService userService) { - this.userService = userService; - } - - /** - * @return the modelService - * @see #modelService - */ - public IModelService getModelService() { - return modelService; - } - - /** - * @param modelService - * the modelService to set - * @see #modelService - */ - public void setModelService(IModelService modelService) { - this.modelService = modelService; - } - /** * @return the commentService * @see #commentService @@ -326,8 +283,8 @@ public class CommentRestImpl extends BaseRestImpl { public Map<String, Object> addComment(String token, String projectId, String elementType, String elementId, String name, String email, String content, boolean pinned, Point2D pointCoordinates, String submodelId) throws QueryException, SecurityException { - AuthenticationToken authenticationToken = userService.getToken(token); - Model model = modelService.getLastModelByProjectId(projectId, authenticationToken); + AuthenticationToken authenticationToken = getUserService().getToken(token); + Model model = getModelService().getLastModelByProjectId(projectId, authenticationToken); if (model == null) { throw new ObjectNotFoundException("Project with given id doesn't exist"); } @@ -362,7 +319,7 @@ public class CommentRestImpl extends BaseRestImpl { Comment comment = commentService.addComment(name, email, content, model, pointCoordinates, commentedObject, pinned, submodel); Project project = model.getProject(); - boolean isAdmin = userService.userHasPrivilege(authenticationToken, PrivilegeType.EDIT_COMMENTS_PROJECT, project); + boolean isAdmin = getUserService().userHasPrivilege(authenticationToken, PrivilegeType.EDIT_COMMENTS_PROJECT, project); return preparedComment(comment, createCommentColumnSet("", isAdmin), isAdmin); } diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/AllRestTests.java b/rest-api/src/test/java/lcsb/mapviewer/api/AllRestTests.java index 24879c2e08..71e9be3bb9 100644 --- a/rest-api/src/test/java/lcsb/mapviewer/api/AllRestTests.java +++ b/rest-api/src/test/java/lcsb/mapviewer/api/AllRestTests.java @@ -4,15 +4,14 @@ import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; -import lcsb.mapviewer.api.comment.AllCommentTests; import lcsb.mapviewer.api.configuration.AllConfigurationTests; import lcsb.mapviewer.api.genomics.AllGenomicsTests; import lcsb.mapviewer.api.projects.AllProjectTests; +import lcsb.mapviewer.api.projects.comments.AllCommentTests; import lcsb.mapviewer.api.users.AllUserTests; @RunWith(Suite.class) -@SuiteClasses({ AllCommentTests.class, // - AllConfigurationTests.class, // +@SuiteClasses({ AllConfigurationTests.class, // AllGenomicsTests.class, // AllProjectTests.class, // AllUserTests.class,// diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/comment/CommentRestImplTest.java b/rest-api/src/test/java/lcsb/mapviewer/api/comment/CommentRestImplTest.java deleted file mode 100644 index 9b8e8de34a..0000000000 --- a/rest-api/src/test/java/lcsb/mapviewer/api/comment/CommentRestImplTest.java +++ /dev/null @@ -1,46 +0,0 @@ -package lcsb.mapviewer.api.comment; - -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; - -import lcsb.mapviewer.api.QueryException; -import lcsb.mapviewer.api.RestTestFunctions; -import lcsb.mapviewer.api.projects.comments.CommentRestImpl; - -public class CommentRestImplTest extends RestTestFunctions { - - @Autowired - public CommentRestImpl commentRestImpl; - - @AfterClass - public static void tearDownAfterClass() throws Exception { - } - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testGetForNonExisting() throws Exception { - try { - commentRestImpl.getCommentList(token.getId(), "unk", "","","",""); - fail("Exception expected"); - } catch (QueryException e2) { - assertTrue(e2.getMessage().contains("Project with given id doesn't exist")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - -} diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/projects/AllProjectTests.java b/rest-api/src/test/java/lcsb/mapviewer/api/projects/AllProjectTests.java index d6b904ceb9..297871577a 100644 --- a/rest-api/src/test/java/lcsb/mapviewer/api/projects/AllProjectTests.java +++ b/rest-api/src/test/java/lcsb/mapviewer/api/projects/AllProjectTests.java @@ -5,12 +5,14 @@ import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; import lcsb.mapviewer.api.projects.chemicals.AllChemicalTests; +import lcsb.mapviewer.api.projects.comments.AllCommentTests; import lcsb.mapviewer.api.projects.drugs.AllDrugTests; import lcsb.mapviewer.api.projects.mirnas.AllMiRnaTests; import lcsb.mapviewer.api.projects.models.AllModelsTests; @RunWith(Suite.class) @SuiteClasses({ AllChemicalTests.class, // + AllCommentTests.class, // AllDrugTests.class, // AllMiRnaTests.class, // AllModelsTests.class, // diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/comment/AllCommentTests.java b/rest-api/src/test/java/lcsb/mapviewer/api/projects/comments/AllCommentTests.java similarity index 78% rename from rest-api/src/test/java/lcsb/mapviewer/api/comment/AllCommentTests.java rename to rest-api/src/test/java/lcsb/mapviewer/api/projects/comments/AllCommentTests.java index 7fc7f124fd..de0f5a2fdc 100644 --- a/rest-api/src/test/java/lcsb/mapviewer/api/comment/AllCommentTests.java +++ b/rest-api/src/test/java/lcsb/mapviewer/api/projects/comments/AllCommentTests.java @@ -1,4 +1,4 @@ -package lcsb.mapviewer.api.comment; +package lcsb.mapviewer.api.projects.comments; import org.junit.runner.RunWith; import org.junit.runners.Suite; diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/projects/comments/CommentRestImplTest.java b/rest-api/src/test/java/lcsb/mapviewer/api/projects/comments/CommentRestImplTest.java new file mode 100644 index 0000000000..dd83e30d07 --- /dev/null +++ b/rest-api/src/test/java/lcsb/mapviewer/api/projects/comments/CommentRestImplTest.java @@ -0,0 +1,95 @@ +package lcsb.mapviewer.api.projects.comments; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.argThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.times; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; + +import lcsb.mapviewer.api.QueryException; +import lcsb.mapviewer.api.RestTestFunctions; +import lcsb.mapviewer.model.Project; +import lcsb.mapviewer.model.map.model.Model; +import lcsb.mapviewer.services.interfaces.IModelService; +import lcsb.mapviewer.services.interfaces.IProjectService; + +public class CommentRestImplTest extends RestTestFunctions { + + @Autowired + public CommentRestImpl _commentRestImpl; + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetForNonExisting() throws Exception { + try { + _commentRestImpl.getCommentList(token.getId(), "unk", "","","",""); + fail("Exception expected"); + } catch (QueryException e2) { + assertTrue(e2.getMessage().contains("Project with given id doesn't exist")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testGetCommentsDependencies() throws Exception { + try { + CommentRestImpl commentRestImpl = createMockProjectRest("testFiles/model/sample.xml"); + commentRestImpl.getCommentList(adminToken.getId(), "sample", "","","",""); + Mockito.verify(commentRestImpl.getModelService(), times(0)).getLastModelByProjectId(anyString(), any()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + + private CommentRestImpl createMockProjectRest(String string) throws Exception { + Model model = null; + Project project = null; + String modelName = ""; + if (string != null) { + project = new Project(); + model = super.getModelForFile(string, true); + project.addModel(model); + modelName = model.getName(); + } + IModelService mockModelService = Mockito.mock(IModelService.class); + Mockito.when(mockModelService.getLastModelByProjectId(eq(modelName), any())).thenReturn(model); + _commentRestImpl.setModelService(mockModelService); + + IProjectService projectServiceMock = Mockito.mock(IProjectService.class); + Mockito.when(projectServiceMock.getProjectByProjectId(eq(modelName), any())).thenReturn(project); + List<Project> projects = new ArrayList<>(); + projects.add(project); + Mockito.when(projectServiceMock.getAllProjects(any())).thenReturn(projects); + _commentRestImpl.setProjectService(projectServiceMock); + + return _commentRestImpl; + } + +} -- GitLab