Skip to content
Snippets Groups Projects
Commit b8f493ed authored by Piotr Gawron's avatar Piotr Gawron
Browse files

when accessing comments no need to fetch model data

parent c30300a6
No related branches found
No related tags found
2 merge requests!115Resolve "admin panel should use API",!114Resolve "admin panel should use API"
......@@ -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);
}
......
......@@ -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,//
......
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;
}
}
}
......@@ -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, //
......
package lcsb.mapviewer.api.comment;
package lcsb.mapviewer.api.projects.comments;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
......
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;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment