MNT-21305 : ACS - Unable to retrieve comments for node using REST api after user deletion

This commit is contained in:
Chris Shields
2020-04-20 16:04:11 +01:00
parent 312807d92b
commit 46984a705b
3 changed files with 186 additions and 9 deletions

View File

@@ -35,6 +35,7 @@ import org.alfresco.rest.api.People;
import org.alfresco.rest.api.model.Comment;
import org.alfresco.rest.api.model.Person;
import org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException;
import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
@@ -49,7 +50,6 @@ import org.alfresco.util.TypeConstraint;
import java.io.Serializable;
import java.util.AbstractList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@@ -68,7 +68,7 @@ public class CommentsImpl implements Comments
{
private static final List<String> INCLUDE_FULL_PERSON = Arrays.asList(
PARAM_INCLUDE_ASPECTNAMES,
PARAM_INCLUDE_PROPERTIES);;
PARAM_INCLUDE_PROPERTIES);
private Nodes nodes;
private NodeService nodeService;
private CommentService commentService;
@@ -123,13 +123,26 @@ public class CommentsImpl implements Comments
boolean canEdit = map.get(CommentService.CAN_EDIT);
boolean canDelete = map.get(CommentService.CAN_DELETE);
Person createdBy = people.getPerson((String) nodeProps.get(ContentModel.PROP_CREATOR), include);
Person createdBy;
try {
createdBy = people.getPerson((String) nodeProps.get(ContentModel.PROP_CREATOR), include);
} catch (EntityNotFoundException enfe){
createdBy = new Person();
createdBy.setUserName((String) nodeProps.get(ContentModel.PROP_CREATOR));
}
nodeProps.put(Comment.PROP_COMMENT_CREATED_BY, createdBy);
Person modifiedBy = people.getPerson((String) nodeProps.get(ContentModel.PROP_MODIFIER), include);
Person modifiedBy;
try {
modifiedBy = people.getPerson((String) nodeProps.get(ContentModel.PROP_MODIFIER), include);
} catch (EntityNotFoundException enfe)
{
modifiedBy = new Person();
modifiedBy.setUserName((String) nodeProps.get(ContentModel.PROP_MODIFIER));
}
nodeProps.put(Comment.PROP_COMMENT_MODIFIED_BY, modifiedBy);
Comment comment = new Comment(commentNodeRef.getId(), nodeProps, canEdit, canDelete);
Comment comment = new Comment(commentNodeRef.getId(), nodeProps, canEdit, canDelete);
return comment;
}
@@ -184,7 +197,7 @@ public class CommentsImpl implements Comments
/* MNT-10536 : fix */
final Set<QName> contentAndFolders =
new HashSet<QName>(Arrays.asList(ContentModel.TYPE_FOLDER, ContentModel.TYPE_CONTENT));
new HashSet<>(Arrays.asList(ContentModel.TYPE_FOLDER, ContentModel.TYPE_CONTENT));
if (!nodes.nodeMatches(nodeRef, contentAndFolders, null))
{
throw new InvalidArgumentException("NodeId of folder or content is expected");
@@ -194,7 +207,7 @@ public class CommentsImpl implements Comments
final PagingResults<NodeRef> pagingResults = commentService.listComments(nodeRef, pagingRequest);
final List<NodeRef> page = pagingResults.getPage();
List<Comment> comments = new AbstractList<Comment>()
List<Comment> comments = new AbstractList<>()
{
@Override
public Comment get(int index)

View File

@@ -75,6 +75,7 @@ import org.junit.runners.Suite;
org.alfresco.repo.web.scripts.custommodel.CustomModelImportTest.class,
org.alfresco.repo.web.scripts.site.SurfConfigTest.class,
org.alfresco.repo.web.scripts.node.NodeWebScripTest.class,
org.alfresco.rest.api.impl.CommentsImplUnitTest.class,
})
public class AppContext04TestSuite
{

View File

@@ -0,0 +1,163 @@
/*
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2020 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.api.impl;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.forum.CommentService;
import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.People;
import org.alfresco.rest.api.model.Comment;
import org.alfresco.rest.api.model.Person;
import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.TypeConstraint;
import org.junit.Before;
import org.junit.Test;
/**
* Unit tests for {@link CommentsImpl} class.
*
* @author Chris Shields
*/
public class CommentsImplUnitTest
{
private CommentsImpl commentsImpl;
private Nodes nodes;
private TypeConstraint typeConstraint;
private CommentService commentService;
private NodeService nodeService;
private ContentService contentService;
private People people;
@Before
public void setUp(){
commentsImpl = new CommentsImpl();
nodes = mock(Nodes.class);
typeConstraint = mock(TypeConstraint.class);
commentService = mock(CommentService.class);
nodeService = mock(NodeService.class);
contentService = mock(ContentService.class);
people = mock(People.class);
commentsImpl.setNodes(nodes);
commentsImpl.setTypeConstraint(typeConstraint);
commentsImpl.setCommentService(commentService);
commentsImpl.setNodeService(nodeService);
commentsImpl.setContentService(contentService);
commentsImpl.setPeople(people);
}
@Test
public void createComment()
{
String nodeId = "node-id";
Comment comment = new Comment();
NodeRef nodeRef = new NodeRef("protocol", "identifier", "id");
NodeRef commentNode = new NodeRef("protocol", "identifier", "comment-id");
Map<String, Boolean> map = new HashMap<>();
map.put(CommentService.CAN_EDIT, true);
map.put(CommentService.CAN_DELETE, true);
Map<QName, Serializable> nodeProps = new HashMap<>();
nodeProps.put(ContentModel.PROP_CREATOR, "user1");
nodeProps.put(ContentModel.PROP_MODIFIER, "user2");
Person person1 = new Person();
person1.setUserName("user1");
person1.setEmail("user1@alfresco.com");
Person person2 = new Person();
person2.setUserName("user2");
person2.setEmail("user2@alfresco.com");
when(nodes.validateNode(nodeId)).thenReturn(nodeRef);
when(typeConstraint.matches(nodeRef)).thenReturn(true);
when(commentService.createComment(nodeRef, comment.getTitle(), comment.getContent(), false)).thenReturn(commentNode);
when(nodeService.getProperties(commentNode)).thenReturn(nodeProps);
when(commentService.getCommentPermissions(any(NodeRef.class), any(NodeRef.class))).thenReturn(map);
when(people.getPerson(eq("user1"), any(List.class))).thenReturn(person1);
when(people.getPerson(eq("user2"), any(List.class))).thenReturn(person2);
Comment resultComment = commentsImpl.createComment(nodeId, comment);
assertNotNull(resultComment);
assertNotNull(resultComment.getCreatedBy());
assertEquals("user1", resultComment.getCreatedBy().getUserName());
assertEquals("user1@alfresco.com", resultComment.getCreatedBy().getEmail());
assertNotNull(resultComment.getModifiedBy());
assertEquals("user2", resultComment.getModifiedBy().getUserName());
assertEquals("user2@alfresco.com", resultComment.getModifiedBy().getEmail());
}
@Test
public void testCreateCommentForDeletedUser(){
String nodeId = "node-id";
Comment comment = new Comment();
NodeRef nodeRef = new NodeRef("protocol", "identifier", "id");
NodeRef commentNode = new NodeRef("protocol", "identifier", "comment-id");
Map<String, Boolean> map = new HashMap<>();
map.put(CommentService.CAN_EDIT, true);
map.put(CommentService.CAN_DELETE, true);
Map<QName, Serializable> nodeProps = new HashMap<>();
nodeProps.put(ContentModel.PROP_CREATOR, "user1");
nodeProps.put(ContentModel.PROP_MODIFIER, "user2");
when(nodes.validateNode(nodeId)).thenReturn(nodeRef);
when(typeConstraint.matches(nodeRef)).thenReturn(true);
when(commentService.createComment(nodeRef, comment.getTitle(), comment.getContent(), false)).thenReturn(commentNode);
when(nodeService.getProperties(commentNode)).thenReturn(nodeProps);
when(commentService.getCommentPermissions(any(NodeRef.class), any(NodeRef.class))).thenReturn(map);
when(people.getPerson(eq("user1"), any(List.class))).thenThrow(EntityNotFoundException.class);
when(people.getPerson(eq("user2"), any(List.class))).thenThrow(EntityNotFoundException.class);
Comment resultComment = commentsImpl.createComment(nodeId, comment);
assertNotNull(resultComment);
assertNotNull(resultComment.getCreatedBy());
assertEquals("user1", resultComment.getCreatedBy().getUserName());
assertNull(resultComment.getCreatedBy().getEmail());
assertNotNull(resultComment.getModifiedBy());
assertEquals("user2", resultComment.getModifiedBy().getUserName());
assertNull(resultComment.getCreatedBy().getEmail());
}
}