From 46984a705bad860e71aaf58af80d83a42a5e4acb Mon Sep 17 00:00:00 2001 From: Chris Shields Date: Mon, 20 Apr 2020 16:04:11 +0100 Subject: [PATCH] MNT-21305 : ACS - Unable to retrieve comments for node using REST api after user deletion --- .../alfresco/rest/api/impl/CommentsImpl.java | 31 +++- .../org/alfresco/AppContext04TestSuite.java | 1 + .../rest/api/impl/CommentsImplUnitTest.java | 163 ++++++++++++++++++ 3 files changed, 186 insertions(+), 9 deletions(-) create mode 100644 src/test/java/org/alfresco/rest/api/impl/CommentsImplUnitTest.java diff --git a/src/main/java/org/alfresco/rest/api/impl/CommentsImpl.java b/src/main/java/org/alfresco/rest/api/impl/CommentsImpl.java index cd20ff0b7f..b569a84282 100644 --- a/src/main/java/org/alfresco/rest/api/impl/CommentsImpl.java +++ b/src/main/java/org/alfresco/rest/api/impl/CommentsImpl.java @@ -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 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 contentAndFolders = - new HashSet(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 pagingResults = commentService.listComments(nodeRef, pagingRequest); final List page = pagingResults.getPage(); - List comments = new AbstractList() + List comments = new AbstractList<>() { @Override public Comment get(int index) diff --git a/src/test/java/org/alfresco/AppContext04TestSuite.java b/src/test/java/org/alfresco/AppContext04TestSuite.java index 7d4895f3ad..9545280e76 100644 --- a/src/test/java/org/alfresco/AppContext04TestSuite.java +++ b/src/test/java/org/alfresco/AppContext04TestSuite.java @@ -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 { diff --git a/src/test/java/org/alfresco/rest/api/impl/CommentsImplUnitTest.java b/src/test/java/org/alfresco/rest/api/impl/CommentsImplUnitTest.java new file mode 100644 index 0000000000..a7d4817f69 --- /dev/null +++ b/src/test/java/org/alfresco/rest/api/impl/CommentsImplUnitTest.java @@ -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 . + * #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 map = new HashMap<>(); + map.put(CommentService.CAN_EDIT, true); + map.put(CommentService.CAN_DELETE, true); + + Map 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 map = new HashMap<>(); + map.put(CommentService.CAN_EDIT, true); + map.put(CommentService.CAN_DELETE, true); + + Map 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()); + } +}