From f2b31e9d6c1f4fefb233c73170dd0402231d2fb0 Mon Sep 17 00:00:00 2001 From: Ana Bozianu Date: Thu, 7 Apr 2016 16:06:29 +0300 Subject: [PATCH 1/3] RM-3216 - extended IMAP service and corrected method --- .../default/rm-imap-server-context.xml | 15 +++ .../repo/imap/ExtendedImapServiceImpl.java | 108 ++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 rm-community/rm-community-repo/config/alfresco/extension/subsystems/imap/default/default/rm-imap-server-context.xml create mode 100644 rm-community/rm-community-repo/source/java/org/alfresco/repo/imap/ExtendedImapServiceImpl.java diff --git a/rm-community/rm-community-repo/config/alfresco/extension/subsystems/imap/default/default/rm-imap-server-context.xml b/rm-community/rm-community-repo/config/alfresco/extension/subsystems/imap/default/default/rm-imap-server-context.xml new file mode 100644 index 0000000000..a2520ed653 --- /dev/null +++ b/rm-community/rm-community-repo/config/alfresco/extension/subsystems/imap/default/default/rm-imap-server-context.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/repo/imap/ExtendedImapServiceImpl.java b/rm-community/rm-community-repo/source/java/org/alfresco/repo/imap/ExtendedImapServiceImpl.java new file mode 100644 index 0000000000..26de83afcf --- /dev/null +++ b/rm-community/rm-community-repo/source/java/org/alfresco/repo/imap/ExtendedImapServiceImpl.java @@ -0,0 +1,108 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2016 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.repo.imap; + +import org.alfresco.model.ContentModel; +import org.alfresco.repo.policy.BehaviourFilter; +import org.alfresco.repo.security.authentication.AuthenticationUtil; +import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork; +import org.alfresco.repo.site.SiteModel; +import org.alfresco.service.cmr.dictionary.DictionaryService; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.NodeService; + +/** + * Extends the core service ImapServiceImpl functionality + * + * @author Ana Bozianu + * @since 2.4 + * + */ +public class ExtendedImapServiceImpl extends ImapServiceImpl +{ + private NodeService nodeService; + private BehaviourFilter policyBehaviourFilter; + private DictionaryService dictionaryService; + + public void setDictionaryService(DictionaryService dictionaryService) + { + this.dictionaryService = dictionaryService; + } + + public void setPolicyFilter(BehaviourFilter policyBehaviourFilter) + { + this.policyBehaviourFilter = policyBehaviourFilter; + super.setPolicyFilter(policyBehaviourFilter); + } + + public void setNodeService(NodeService nodeService) + { + this.nodeService = nodeService; + super.setNodeService(nodeService); + } + + @Override + /** + * Overwrites the core functionality so we can list RM files in IMAP + * @see https://issues.alfresco.com/jira/browse/RM-3216 + */ + public String getPathFromSites(final NodeRef ref) + { + return doAsSystem(new RunAsWork() + { + @Override + public String doWork() throws Exception + { + String name = ((String) nodeService.getProperty(ref, ContentModel.PROP_NAME)).toLowerCase(); + if (dictionaryService.isSubClass(nodeService.getType(ref), SiteModel.TYPE_SITE)) + { + return name; + } + else + { + NodeRef parent = nodeService.getPrimaryParent(ref).getParentRef(); + return getPathFromSites(parent) + "/" + name; + } + } + }); + } + + private R doAsSystem(RunAsWork work) + { + policyBehaviourFilter.disableBehaviour(ContentModel.ASPECT_AUDITABLE); + policyBehaviourFilter.disableBehaviour(ContentModel.ASPECT_VERSIONABLE); + try + { + return AuthenticationUtil.runAs(work, AuthenticationUtil.getSystemUserName()); + } + finally + { + policyBehaviourFilter.enableBehaviour(ContentModel.ASPECT_AUDITABLE); + policyBehaviourFilter.enableBehaviour(ContentModel.ASPECT_VERSIONABLE); + } + } +} From 1b359ad0836dd41b6c020fc7f37f0dae4732f569 Mon Sep 17 00:00:00 2001 From: Ana Bozianu Date: Fri, 8 Apr 2016 14:46:14 +0300 Subject: [PATCH 2/3] RM-3216 - added unit test --- .../default/rm-imap-server-context.xml | 1 + .../repo/imap/ExtendedImapServiceImpl.java | 13 ++- .../imap/ExtendedImapServiceImplUnitTest.java | 103 ++++++++++++++++++ 3 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 rm-community/rm-community-repo/unit-test/java/org/alfresco/repo/imap/ExtendedImapServiceImplUnitTest.java diff --git a/rm-community/rm-community-repo/config/alfresco/extension/subsystems/imap/default/default/rm-imap-server-context.xml b/rm-community/rm-community-repo/config/alfresco/extension/subsystems/imap/default/default/rm-imap-server-context.xml index a2520ed653..50c97563ad 100644 --- a/rm-community/rm-community-repo/config/alfresco/extension/subsystems/imap/default/default/rm-imap-server-context.xml +++ b/rm-community/rm-community-repo/config/alfresco/extension/subsystems/imap/default/default/rm-imap-server-context.xml @@ -6,6 +6,7 @@ + diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/repo/imap/ExtendedImapServiceImpl.java b/rm-community/rm-community-repo/source/java/org/alfresco/repo/imap/ExtendedImapServiceImpl.java index 26de83afcf..7f1bcf315b 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/repo/imap/ExtendedImapServiceImpl.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/repo/imap/ExtendedImapServiceImpl.java @@ -28,7 +28,7 @@ package org.alfresco.repo.imap; import org.alfresco.model.ContentModel; import org.alfresco.repo.policy.BehaviourFilter; -import org.alfresco.repo.security.authentication.AuthenticationUtil; +import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork; import org.alfresco.repo.site.SiteModel; import org.alfresco.service.cmr.dictionary.DictionaryService; @@ -46,7 +46,8 @@ public class ExtendedImapServiceImpl extends ImapServiceImpl { private NodeService nodeService; private BehaviourFilter policyBehaviourFilter; - private DictionaryService dictionaryService; + private DictionaryService dictionaryService; + private AuthenticationUtil authenticationUtil; public void setDictionaryService(DictionaryService dictionaryService) { @@ -64,12 +65,12 @@ public class ExtendedImapServiceImpl extends ImapServiceImpl this.nodeService = nodeService; super.setNodeService(nodeService); } - - @Override + /** * Overwrites the core functionality so we can list RM files in IMAP * @see https://issues.alfresco.com/jira/browse/RM-3216 - */ + */ + @Override public String getPathFromSites(final NodeRef ref) { return doAsSystem(new RunAsWork() @@ -97,7 +98,7 @@ public class ExtendedImapServiceImpl extends ImapServiceImpl policyBehaviourFilter.disableBehaviour(ContentModel.ASPECT_VERSIONABLE); try { - return AuthenticationUtil.runAs(work, AuthenticationUtil.getSystemUserName()); + return authenticationUtil.runAsSystem(work); } finally { diff --git a/rm-community/rm-community-repo/unit-test/java/org/alfresco/repo/imap/ExtendedImapServiceImplUnitTest.java b/rm-community/rm-community-repo/unit-test/java/org/alfresco/repo/imap/ExtendedImapServiceImplUnitTest.java new file mode 100644 index 0000000000..868ba27243 --- /dev/null +++ b/rm-community/rm-community-repo/unit-test/java/org/alfresco/repo/imap/ExtendedImapServiceImplUnitTest.java @@ -0,0 +1,103 @@ +package org.alfresco.repo.imap; + +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.alfresco.model.ContentModel; +import org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel; +import org.alfresco.module.org_alfresco_module_rm.test.util.MockAuthenticationUtilHelper; +import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil; +import org.alfresco.repo.policy.BehaviourFilter; +import org.alfresco.repo.site.SiteModel; +import org.alfresco.service.cmr.dictionary.DictionaryService; +import org.alfresco.service.cmr.repository.ChildAssociationRef; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.NodeService; +import org.alfresco.service.cmr.repository.StoreRef; +import org.alfresco.service.namespace.QName; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.extensions.webscripts.GUID; + +/** + * Unit test for ExtendedImapServiceImpl + * @author Ana Bozianu + */ +public class ExtendedImapServiceImplUnitTest +{ + /* service mocks */ + private @Mock NodeService mockedNodeService; + private @Mock BehaviourFilter mockedPolicyBehaviourFilter; + private @Mock DictionaryService mockedDictionaryService; + private @Mock AuthenticationUtil mockedAuthenticationUtil; + + /* test instance of extended IMAP service implementation */ + private @InjectMocks ExtendedImapServiceImpl extendedImapServiceImpl; + + /* test data */ + private NodeRef rmSite = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, GUID.generate()); + private final String RM_SITE_NAME = "RM"; + private NodeRef rmFilePlan = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, GUID.generate()); + private final String RM_FILEPLAN_NAME = "fileplan"; + private NodeRef rmCategory = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, GUID.generate()); + private final String RM_CATEGORY_NAME = "C1"; + + @Before + public void setup() + { + MockitoAnnotations.initMocks(this); + // setup mocked authentication util + MockAuthenticationUtilHelper.setup(mockedAuthenticationUtil); + + // node names + when(mockedNodeService.getProperty(rmSite, ContentModel.PROP_NAME)).thenReturn(RM_SITE_NAME); + when(mockedNodeService.getProperty(rmCategory, ContentModel.PROP_NAME)).thenReturn(RM_CATEGORY_NAME); + when(mockedNodeService.getProperty(rmFilePlan, ContentModel.PROP_NAME)).thenReturn(RM_FILEPLAN_NAME); + + // node types + when(mockedNodeService.getType(rmSite)).thenReturn(RecordsManagementModel.TYPE_RM_SITE); + when(mockedNodeService.getType(rmFilePlan)).thenReturn(RecordsManagementModel.TYPE_FILE_PLAN); + when(mockedNodeService.getType(rmCategory)).thenReturn(RecordsManagementModel.TYPE_RECORD_CATEGORY); + + // type hierarchy + when(mockedDictionaryService.isSubClass(RecordsManagementModel.TYPE_RM_SITE, SiteModel.TYPE_SITE)).thenReturn(true); + when(mockedDictionaryService.isSubClass(RecordsManagementModel.TYPE_FILE_PLAN, SiteModel.TYPE_SITE)).thenReturn(false); + when(mockedDictionaryService.isSubClass(RecordsManagementModel.TYPE_RECORD_CATEGORY, SiteModel.TYPE_SITE)).thenReturn(false); + + // node associations + ChildAssociationRef filePlanParentAssoc = new ChildAssociationRef(ContentModel.ASSOC_CONTAINS, rmSite, QName.createQName(GUID.generate()), rmFilePlan); + when(mockedNodeService.getPrimaryParent(rmFilePlan)).thenReturn(filePlanParentAssoc); + + ChildAssociationRef categoryParentAssoc = new ChildAssociationRef(ContentModel.ASSOC_CONTAINS, rmFilePlan, QName.createQName(GUID.generate()), rmCategory); + when(mockedNodeService.getPrimaryParent(rmCategory)).thenReturn(categoryParentAssoc); + } + + /** + * given the method is called on the rm site node + * check if the result is the site name + */ + @Test + public void testGetPathFromRMSite() + { + String rmSitePath = extendedImapServiceImpl.getPathFromSites(rmSite); + Assert.assertEquals("Incorrect return value", RM_SITE_NAME.toLowerCase(), rmSitePath); + } + + /** + * given the method is called on a rm category + * check if the result is the full path relative to the rm site + */ + @Test + public void testGetPathFromRMCategory() + { + String rmCategoryPath = extendedImapServiceImpl.getPathFromSites(rmCategory); + Assert.assertEquals("Incorrect return value", (RM_SITE_NAME + "/" + RM_FILEPLAN_NAME + "/" + RM_CATEGORY_NAME).toLowerCase(), rmCategoryPath); + + verify(extendedImapServiceImpl).getPathFromSites(rmSite); + verify(extendedImapServiceImpl).getPathFromSites(rmFilePlan); + } +} From 5717cbef6dce4e0c20b607b4bde780c5f4aac1fa Mon Sep 17 00:00:00 2001 From: Ana Bozianu Date: Mon, 11 Apr 2016 12:27:19 +0300 Subject: [PATCH 3/3] RM-3216 - added header to test file --- .../imap/ExtendedImapServiceImplUnitTest.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/rm-community/rm-community-repo/unit-test/java/org/alfresco/repo/imap/ExtendedImapServiceImplUnitTest.java b/rm-community/rm-community-repo/unit-test/java/org/alfresco/repo/imap/ExtendedImapServiceImplUnitTest.java index 868ba27243..0e7b6ad27a 100644 --- a/rm-community/rm-community-repo/unit-test/java/org/alfresco/repo/imap/ExtendedImapServiceImplUnitTest.java +++ b/rm-community/rm-community-repo/unit-test/java/org/alfresco/repo/imap/ExtendedImapServiceImplUnitTest.java @@ -1,3 +1,29 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2016 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.repo.imap; import static org.mockito.Mockito.verify;