From 3c72bbc0f9d9f8a53e6a101fb898ee3fde019f4b Mon Sep 17 00:00:00 2001 From: Ana Bozianu Date: Fri, 8 Dec 2017 07:53:22 +0000 Subject: [PATCH] MNT-18502 - filtered out dod properties if the node is not a dod record --- .../email/RFC822MetadataExtracter.java | 23 ++- .../RFC822MetadataExtracterUnitTest.java | 169 ++++++++++++++++++ 2 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/email/RFC822MetadataExtracterUnitTest.java diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/email/RFC822MetadataExtracter.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/email/RFC822MetadataExtracter.java index 4ef27fbd86..b601d84d22 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/email/RFC822MetadataExtracter.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/email/RFC822MetadataExtracter.java @@ -33,6 +33,7 @@ import java.util.Map; import java.util.Set; import org.alfresco.model.ContentModel; +import org.alfresco.module.org_alfresco_module_rm.dod5015.DOD5015Model; import org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; @@ -69,7 +70,13 @@ public class RFC822MetadataExtracter extends org.alfresco.repo.content.metadata. protected void filterSystemProperties(Map systemProperties, Map targetProperties) { NodeRef nodeRef = getNodeRef(targetProperties); - if (nodeRef == null || !nodeService.hasAspect(nodeRef, RecordsManagementModel.ASPECT_RECORD)) + if(nodeRef == null) + { + return; + } + + // Remove record properties from non-record nodes + if (!nodeService.hasAspect(nodeRef, RecordsManagementModel.ASPECT_RECORD)) { // Remove all rm namespace properties from the system map Map clone = new HashMap(systemProperties); @@ -81,6 +88,20 @@ public class RFC822MetadataExtracter extends org.alfresco.repo.content.metadata. } } } + + // Remove dod5015 properties from non-dod5015 nodes + if (!nodeService.hasAspect(nodeRef, DOD5015Model.ASPECT_DOD_5015_RECORD)) + { + // Remove all dod5015 namespace properties from the system map + Map clone = new HashMap<>(systemProperties); + for (QName propName : clone.keySet()) + { + if (DOD5015Model.DOD_URI.equals(propName.getNamespaceURI())) + { + systemProperties.remove(propName); + } + } + } } /** diff --git a/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/email/RFC822MetadataExtracterUnitTest.java b/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/email/RFC822MetadataExtracterUnitTest.java new file mode 100644 index 0000000000..3c37670754 --- /dev/null +++ b/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/email/RFC822MetadataExtracterUnitTest.java @@ -0,0 +1,169 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2017 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.module.org_alfresco_module_rm.email; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import java.io.Serializable; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import com.google.common.collect.ImmutableMap; + +import org.alfresco.model.ContentModel; +import org.alfresco.module.org_alfresco_module_rm.dod5015.DOD5015Model; +import org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel; +import org.alfresco.module.org_alfresco_module_rm.test.util.BaseUnitTest; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.namespace.QName; +import org.junit.Test; +import org.mockito.InjectMocks; + +/** + * Unit test for RFC822MetadataExtracter + * + * @author Ana Manolache + * @since 2.7 + */ +public class RFC822MetadataExtracterUnitTest extends BaseUnitTest +{ + @InjectMocks + private RFC822MetadataExtracter metadataExtracter; + + private static final Map COMMON_PROPERTIES = ImmutableMap.of( + ContentModel.PROP_NAME, "Name", + ContentModel.PROP_TITLE, "Title"); + private static final Map RECORD_PROPERTIES = ImmutableMap.of( + RecordsManagementModel.PROP_DECLARED_BY, "DeclaredBy", + RecordsManagementModel.PROP_DECLARED_AT, new Date()); + private static final Map DOD_PROPERTIES = ImmutableMap.of( + DOD5015Model.PROP_ORIGINATOR, "DODOriginator", + DOD5015Model.PROP_ADDRESS, "Title"); + + /** + * Given a node that is not a record nor a dod record + * and has record and dod record properties + * When the method is called + * Then the record properties and dod properties are filtered out + */ + @Test + public void testRemoveSensitivePropertiesFromCommonNodes() + { + // Given + NodeRef node = generateNodeRef(); + when(mockedNodeService.hasAspect(node, RecordsManagementModel.ASPECT_RECORD)).thenReturn(false); + when(mockedNodeService.hasAspect(node, DOD5015Model.ASPECT_DOD_5015_RECORD)).thenReturn(false); + + // When + Map systemProperties = new HashMap<>(COMMON_PROPERTIES); + systemProperties.putAll(RECORD_PROPERTIES); + systemProperties.putAll(DOD_PROPERTIES); + metadataExtracter.filterSystemProperties(systemProperties, generateTargetProperties(node)); + + // Then + assertTrue("Sensitive properties were not properly filtered out.", + systemProperties.keySet().equals(COMMON_PROPERTIES.keySet())); + } + + /** + * Given a node that is a record + * and has record properties and dod properties + * When the method is called + * Then the DOD properties are filtered out + * and common and record properties are preserved + */ + @Test + public void testRemoveDodPropertiesFromRecordNodes() + { + // Given + NodeRef node = generateNodeRef(); + when(mockedNodeService.hasAspect(node, RecordsManagementModel.ASPECT_RECORD)).thenReturn(true); + when(mockedNodeService.hasAspect(node, DOD5015Model.ASPECT_DOD_5015_RECORD)).thenReturn(false); + + // When + Map systemProperties = new HashMap<>(COMMON_PROPERTIES); + systemProperties.putAll(RECORD_PROPERTIES); + systemProperties.putAll(DOD_PROPERTIES); + metadataExtracter.filterSystemProperties(systemProperties, generateTargetProperties(node)); + + // Then + assertTrue("Common properties should not be filtered out from record nodes.", + systemProperties.keySet().containsAll(COMMON_PROPERTIES.keySet())); + assertTrue("Record properties should not be filtered out from record nodes.", + systemProperties.keySet().containsAll(RECORD_PROPERTIES.keySet())); + assertFalse("Sensitive DOD properties were not properly filtered out from record nodes.", + systemProperties.keySet().removeAll(DOD_PROPERTIES.keySet())); + } + + /** + * Given a node that is a dod record + * and has record properties and dod properties + * When the method is called + * Then the record properties are filtered out + * and common and DOD properties are preserved + */ + @Test + public void testRemoveRecordPropertiesFromDodNodes() + { + // Given + NodeRef node = generateNodeRef(); + when(mockedNodeService.hasAspect(node, RecordsManagementModel.ASPECT_RECORD)).thenReturn(false); + when(mockedNodeService.hasAspect(node, DOD5015Model.ASPECT_DOD_5015_RECORD)).thenReturn(true); + + // When + Map systemProperties = new HashMap<>(COMMON_PROPERTIES); + systemProperties.putAll(RECORD_PROPERTIES); + systemProperties.putAll(DOD_PROPERTIES); + metadataExtracter.filterSystemProperties(systemProperties, generateTargetProperties(node)); + + // Then + assertTrue("Common properties should not be filtered out from DOD nodes.", + systemProperties.keySet().containsAll(COMMON_PROPERTIES.keySet())); + assertTrue("DOD properties should not be filtered out from DOD nodes.", + systemProperties.keySet().containsAll(DOD_PROPERTIES.keySet())); + assertFalse("Sensitive record properties were not properly filtered out from DOD nodes.", + systemProperties.keySet().removeAll(RECORD_PROPERTIES.keySet())); + } + + /** + * Helper method that generates target properties such as the given node is retrieved from them + * + * @param node the node to represent in the properties + * @return the list of properties containing the node's information + */ + private Map generateTargetProperties(NodeRef node) + { + Map targetProperties = new HashMap<>(); + targetProperties.put(ContentModel.PROP_STORE_PROTOCOL, node.getStoreRef().getProtocol()); + targetProperties.put(ContentModel.PROP_STORE_IDENTIFIER, node.getStoreRef().getIdentifier()); + targetProperties.put(ContentModel.PROP_NODE_UUID, node.getId()); + return targetProperties; + } +}