Skip to content

Commit

Permalink
fix: after adding a document version, allow to create a new activity …
Browse files Browse the repository at this point in the history
…when the original is deleted - EXO-65154 (#2169)



When a new document version is created, an activity is created too and the text associated to the version is added as a comment.
When this activity is removed and a new version is created, nothing happens.
this fix allows to create a new activity when the original one is deleted, it also removes the usage of PowerMockito in Tests and replaces it with Mockito framework.
  • Loading branch information
ahamdi committed Sep 7, 2023
1 parent 8e52aba commit de80132
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 191 deletions.
14 changes: 2 additions & 12 deletions ecms-social-integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<name>eXo PLF:: ECMS - Social Activities</name>
<description>ECMS with Social activity</description>
<properties>
<exo.test.coverage.ratio>0.05</exo.test.coverage.ratio>
<exo.test.coverage.ratio>0.12</exo.test.coverage.ratio>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -172,17 +172,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ private static void refineNode(Node currentNode) throws Exception {
*
* @return activity owner
*/
private static String getActivityOwnerId(Node node) {
public static String getActivityOwnerId(Node node) {
String activityOwnerId = "";
ConversationState conversationState = ConversationState.getCurrent();
if (conversationState != null) {
Expand All @@ -706,9 +706,8 @@ private static String getActivityOwnerId(Node node) {
* @return the group name
* @throws Exception
*/
private static String getSpaceName(Node node) throws Exception {
NodeHierarchyCreator nodeHierarchyCreator = (NodeHierarchyCreator) ExoContainerContext.getCurrentContainer()
.getComponentInstanceOfType(NodeHierarchyCreator.class);
public static String getSpaceName(Node node) throws Exception {
NodeHierarchyCreator nodeHierarchyCreator = CommonsUtils.getService(NodeHierarchyCreator.class);
String groupPath = nodeHierarchyCreator.getJcrPath(BasePath.CMS_GROUPS_PATH);
String spacesFolder = groupPath + "/spaces/";
String spaceName = "";
Expand All @@ -721,7 +720,7 @@ private static String getSpaceName(Node node) throws Exception {
return spaceName;
}

private static boolean isPublic(Node node) {
public static boolean isPublic(Node node) {
if (node instanceof ExtendedNode) {
ExtendedNode n = (ExtendedNode)node;
try {
Expand Down Expand Up @@ -1047,23 +1046,28 @@ public static void setAvatarUrl(Node commentNode) throws RepositoryException {
}

public static String addVersionComment(Node node, String commentText, String userId) throws Exception {
String nodeActivityID;
String nodeActivityID = null;
IdentityManager identityManager = CommonsUtils.getService(IdentityManager.class);
SpaceService spaceService = CommonsUtils.getService(SpaceService.class);
ActivityManager activityManager = CommonsUtils.getService(ActivityManager.class);
String activityOwnerId = getActivityOwnerId(node);
boolean deletedActivity = false;
if(node.hasProperty(ActivityTypeUtils.EXO_ACTIVITY_ID)) {
nodeActivityID = node.getProperty(ActivityTypeUtils.EXO_ACTIVITY_ID).getString();
deletedActivity = activityManager.getActivity(nodeActivityID) == null;
}

ExoSocialActivity activity = createActivity(identityManager, activityOwnerId, node, commentText, activityType, true, "", "");
setActivityType(null);

if (!node.isNodeType(ActivityTypeUtils.EXO_ACTIVITY_INFO)) {
if (!node.isNodeType(ActivityTypeUtils.EXO_ACTIVITY_INFO) || deletedActivity) {
String spaceGroupName = getSpaceName(node);
Space space = spaceService.getSpaceByGroupId(SpaceUtils.SPACE_GROUP + "/" + spaceGroupName);
if (spaceGroupName != null && spaceGroupName.length() > 0 && space != null) {
if (spaceGroupName != null && !spaceGroupName.isEmpty() && space != null) {
// post activity to space stream
Identity spaceIdentity = identityManager.getOrCreateIdentity(SpaceIdentityProvider.NAME, space.getPrettyName());
activityManager.saveActivityNoReturn(spaceIdentity, activity);
} else if (activityOwnerId != null && activityOwnerId.length() > 0) {
} else if (activityOwnerId != null && !activityOwnerId.isEmpty()) {
if (!isPublic(node)) {
// only post activity to user status stream if that upload is public
return null;
Expand All @@ -1075,10 +1079,10 @@ public static String addVersionComment(Node node, String commentText, String use
return null;
}
if (!StringUtils.isEmpty(activity.getId())) {
nodeActivityID = activity.getId();
ActivityTypeUtils.attachActivityId(node, activity.getId());
}
}
nodeActivityID = node.getProperty(ActivityTypeUtils.EXO_ACTIVITY_ID).getString();

if (nodeActivityID != null && !nodeActivityID.isEmpty() && commentText != null && !commentText.trim().isEmpty()) {
org.exoplatform.social.core.identity.model.Identity identity =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.mockito.MockedStatic;
import org.mockito.runners.MockitoJUnitRunner;

import javax.jcr.Node;
import java.util.HashMap;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

@RunWith(PowerMockRunner.class)
@PrepareForTest(NodeLocation.class)
@PowerMockIgnore({ "javax.management.*", "javax.xml.*", "org.apache.xerces.*", "org.xml.*" })
@RunWith(MockitoJUnitRunner.class)
public class ActivityAttachmentProcessorTest {

@Mock
Expand All @@ -47,8 +43,8 @@ public void processActivity() {
templateParams.get("nodePath"), templateParams.get("nodeUUID"), true);
Node currentNode = mock(Node.class);
doCallRealMethod().when(activityAttachmentProcessor).processActivity(activity);
PowerMockito.mockStatic(NodeLocation.class);
when(NodeLocation.getNodeByLocation(ArgumentMatchers.refEq(nodeLocation))).thenReturn(currentNode);
MockedStatic<NodeLocation> NODE_LOCATION = mockStatic(NodeLocation.class);
NODE_LOCATION.when(() -> NodeLocation.getNodeByLocation(ArgumentMatchers.refEq(nodeLocation))).thenReturn(currentNode);
activityAttachmentProcessor.processActivity(activity);
assertEquals(1, activity.getFiles().size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.exoplatform.task.dto.TaskDto;
import org.exoplatform.task.service.ProjectService;
import org.exoplatform.task.service.TaskService;
import org.powermock.api.mockito.PowerMockito;

import javax.jcr.nodetype.NodeType;
import java.util.Arrays;
Expand All @@ -49,14 +48,14 @@ public void testGetAttachmentOrLinkId() throws Exception {
long entityId = 1;

// Mock services
TaskService taskService = PowerMockito.mock(TaskService.class);
ProjectService projectService = PowerMockito.mock(ProjectService.class);
NodeHierarchyCreator nodeHierarchyCreator = PowerMockito.mock(NodeHierarchyCreator.class);
RepositoryService repositoryService = PowerMockito.mock(RepositoryService.class);
SessionProviderService sessionProviderService = PowerMockito.mock(SessionProviderService.class);
TaskService taskService = mock(TaskService.class);
ProjectService projectService = mock(ProjectService.class);
NodeHierarchyCreator nodeHierarchyCreator = mock(NodeHierarchyCreator.class);
RepositoryService repositoryService = mock(RepositoryService.class);
SessionProviderService sessionProviderService = mock(SessionProviderService.class);

// Mock SessionProvider
SessionProvider sessionProvider = PowerMockito.mock(SessionProvider.class);
SessionProvider sessionProvider = mock(SessionProvider.class);
when(sessionProviderService.getSessionProvider(null)).thenReturn(sessionProvider);

// Mock Repository service and JCR session
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import static org.powermock.api.mockito.PowerMockito.whenNew;

import javax.jcr.Node;
import javax.jcr.Property;
Expand Down Expand Up @@ -253,4 +252,4 @@ public void shouldReturnUpdatedContentForExport() throws Exception {
// Then
assertTrue(processedContent.matches("<p>content with image: <img src=\"//-image.png-//\" /></p>"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.mockito.junit.MockitoJUnitRunner;

import javax.jcr.Node;
import javax.jcr.Property;
Expand All @@ -36,9 +34,7 @@
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

@RunWith(PowerMockRunner.class)
@PrepareForTest(NodeLocation.class)
@PowerMockIgnore({ "javax.management.*", "javax.xml.*", "org.apache.xerces.*", "org.xml.*" })
@RunWith(MockitoJUnitRunner.class)
public class ECMSActivityFileStoragePluginTest {

static private String TEXT_PLAIN = "text/plain";
Expand Down Expand Up @@ -101,10 +97,10 @@ public void storeAttachments() throws Exception {
uploadResource.setStoreLocation(file.getPath());
uploadResource.setMimeType(TEXT_PLAIN);
when(sessionProviderService.getSystemSessionProvider(any())).thenReturn(sessionProvider);
when(sessionProviderService.getSessionProvider(any())).thenReturn(sessionProvider);
lenient().when(sessionProviderService.getSessionProvider(any())).thenReturn(sessionProvider);
when(repositoryService.getCurrentRepository()).thenReturn(repository);
when(repository.getConfiguration()).thenReturn(repositoryEntry);
when(repositoryEntry.getName()).thenReturn("workspace");
lenient().when(repositoryEntry.getName()).thenReturn("workspace");
when(repositoryEntry.getDefaultWorkspaceName()).thenReturn("collaboration");
when(sessionProvider.getSession(any(), any())).thenReturn(session);
Node userNode = mock(Node.class);
Expand Down
Loading

0 comments on commit de80132

Please sign in to comment.