Merge of classified_renditions branch which introduces MetadataReferralService and fixes RM-2549.

==============================================================================

Merged  to :
   111287: Common utility classes developed as part of refactor for RM-2549.
       asSet method that works like java.util.Arrays.asList.
       Also variants of java.util.Arrays.asList that take Supplier<T> rather than T.
   111292: This checkin provides the non-RM-specific parts of metadata delegation, which are required for the refactor of classified renditions needed for RM-2549.
   111633: Massive renaming. Delegate/Delegation becomes Referrer, Referent and things *do* make a little more sense.
   111643: This is the RM-specific parts of the refactor for classified renditions - see RM-2549.
   111696: Addressing code review comments.
   111703: Addressing code review comments
   111707: Addressing review comments. Clearer use of lambdas due to default methods in java.util.Collection.
   111768: Addition of tidyup code for when clf:classified aspect is removed. Also added notes on what's to do if this ever becomes a core service.
   111772: Slight refactor. ReferredMetadataService uses the registry to look up Referrals rather than the AdminService. Seems neater.
   111779: Addressing review comment - don't have assoc types in the service API - have aspect names instead.
       I agree with this comment. I think assoc types are an implementation detail of this service.
   111855: Applying code review comment. I added an 'mr' prefix to the spring beans, which we hope will make our lives easier if this Metadata Referral stuff ever makes it into core.




git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@111864 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Neil McErlean
2015-09-10 13:34:40 +00:00
24 changed files with 1887 additions and 207 deletions

View File

@@ -45,6 +45,7 @@ import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationE
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.LevelIdNotFound;
import org.alfresco.module.org_alfresco_module_rm.classification.model.ClassifiedContentModel;
import org.alfresco.module.org_alfresco_module_rm.freeze.FreezeService;
import org.alfresco.module.org_alfresco_module_rm.referredmetadata.ReferredMetadataService;
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.security.permissions.AccessDeniedException;
@@ -80,6 +81,7 @@ public class ContentClassificationServiceImplUnitTest implements ClassifiedConte
@Mock FreezeService mockFreezeService;
@Mock SecurityClearanceService mockSecurityClearanceService;
@Mock AuthenticationUtil mockAuthenticationUtil;
@Mock ReferredMetadataService mockReferredMetadataService;
@Mock ClassificationAspectProperties mockPropertiesDTO;
@Captor ArgumentCaptor<Map<QName, Serializable>> propertiesCaptor;

View File

@@ -1,95 +0,0 @@
/*
* Copyright (C) 2005-2015 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* 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/>.
*/
package org.alfresco.module.org_alfresco_module_rm.model.clf;
import static java.util.Arrays.asList;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import org.alfresco.model.RenditionModel;
import org.alfresco.module.org_alfresco_module_rm.classification.ContentClassificationService;
import org.alfresco.module.org_alfresco_module_rm.classification.model.ClassifiedContentModel;
import org.alfresco.module.org_alfresco_module_rm.model.clf.aspect.ClassifiedAspect;
import org.alfresco.module.org_alfresco_module_rm.test.util.MockAuthenticationUtilHelper;
import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil;
import org.alfresco.module.org_alfresco_module_rm.util.CoreServicesExtras;
import org.alfresco.service.cmr.rendition.RenditionService;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
/**
* Unit tests for {@link ClassifiedRenditions}.
*
* @since 2.4.a
*/
public class ClassifiedRenditionsUnitTest implements ClassifiedContentModel
{
private static final NodeRef SOURCE_NODE = new NodeRef("node://ref/");
private static final NodeRef RENDITION_1 = new NodeRef("node://rendition1/");
private static final NodeRef RENDITION_2 = new NodeRef("node://rendition2/");
@InjectMocks ClassifiedAspect classifiedAspect;
@Mock AuthenticationUtil mockAuthenticationUtil;
@Mock ContentClassificationService mockContentClassificationService;
@Mock CoreServicesExtras mockCoreServicesExtras;
@Mock NodeService mockNodeService;
@Mock RenditionService mockRenditionService;
@Before
public void setUp()
{
initMocks(this);
MockAuthenticationUtilHelper.setup(mockAuthenticationUtil);
}
@Test public void newRenditionOfClassifiedNodeShouldItselfBeClassified()
{
when(mockRenditionService.getRenditions(SOURCE_NODE))
.thenReturn(asList(rendition(SOURCE_NODE, RENDITION_1), rendition(SOURCE_NODE, RENDITION_2)));
when(mockRenditionService.getSourceNode(RENDITION_1)).thenReturn(rendition(SOURCE_NODE, RENDITION_1));
when(mockRenditionService.getSourceNode(RENDITION_2)).thenReturn(rendition(SOURCE_NODE, RENDITION_2));
when(mockContentClassificationService.isClassified(SOURCE_NODE)).thenReturn(true);
final ClassifiedRenditions behaviour = new ClassifiedRenditions();
behaviour.setAuthenticationUtil(mockAuthenticationUtil);
behaviour.setContentClassificationService(mockContentClassificationService);
behaviour.setCoreServicesExtras(mockCoreServicesExtras);
behaviour.setNodeService(mockNodeService);
behaviour.setRenditionService(mockRenditionService);
behaviour.onAddAspect(RENDITION_2, RenditionModel.ASPECT_RENDITION);
verify(mockCoreServicesExtras).copyAspect(SOURCE_NODE, RENDITION_2, ClassifiedContentModel.ASPECT_CLASSIFIED);
}
/** Creates a test Rendition ChildAssociationRef. */
private ChildAssociationRef rendition(NodeRef source, NodeRef rendition)
{
return new ChildAssociationRef(RenditionModel.ASSOC_RENDITION, source, RenditionModel.ASSOC_RENDITION, rendition);
}
}

View File

@@ -19,37 +19,32 @@
package org.alfresco.module.org_alfresco_module_rm.model.clf.aspect;
import static java.util.Arrays.asList;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.model.RenditionModel;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingDowngradeInstructions;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationSchemeService;
import org.alfresco.module.org_alfresco_module_rm.classification.model.ClassifiedContentModel;
import org.alfresco.module.org_alfresco_module_rm.referredmetadata.ReferralAdminService;
import org.alfresco.module.org_alfresco_module_rm.util.CoreServicesExtras;
import org.alfresco.service.cmr.rendition.RenditionService;
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.namespace.QName;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import java.util.Date;
/**
* Unit tests for the {@link ClassifiedAspect}.
*
* @author Tom Page
* @author Neil Mc Erlean
* @since 2.4.a
*/
public class ClassifiedAspectUnitTest implements ClassifiedContentModel
@@ -57,14 +52,13 @@ public class ClassifiedAspectUnitTest implements ClassifiedContentModel
private static final NodeRef NODE_REF = new NodeRef("node://Ref/");
private static final NodeRef RENDITION_1 = new NodeRef("node://rendition1/");
private static final NodeRef RENDITION_2 = new NodeRef("node://rendition2/");
private static final ClassificationLevel TOP_SECRET = new ClassificationLevel("Top Secret", "Top Secret");
private static final ClassificationLevel SECRET = new ClassificationLevel("Secret", "Secret");
@InjectMocks ClassifiedAspect classifiedAspect;
@Mock ClassificationSchemeService mockClassificationSchemeService;
@Mock CoreServicesExtras mockCoreServicesExtras;
@Mock NodeService mockNodeService;
@Mock RenditionService mockRenditionService;
@Mock ReferralAdminService mockReferralAdminService;
@Before
public void setUp()
@@ -135,50 +129,20 @@ public class ClassifiedAspectUnitTest implements ClassifiedContentModel
classifiedAspect.checkConsistencyOfProperties(NODE_REF);
}
/** Check that when a node is classified, its renditions are also classified. */
@Test public void classificationOfNodeShouldClassifyRenditions()
@Test public void newlyClassifiedNodeShouldLinkItsMetadataToAllRenditions()
{
for (NodeRef n : asList(NODE_REF, RENDITION_1, RENDITION_2))
{
when(mockNodeService.hasAspect(n, ASPECT_CLASSIFIED)).thenReturn(true);
}
when(mockClassificationSchemeService.getClassificationLevelById(eq("Top Secret"))).thenReturn(TOP_SECRET);
when(mockClassificationSchemeService.getClassificationLevelById(eq("Secret"))).thenReturn(SECRET);
when(mockClassificationSchemeService.getReclassification(any(), any())).thenReturn(ClassificationSchemeService.Reclassification.DOWNGRADE);
when(mockRenditionService.getRenditions(eq(NODE_REF)))
when(mockRenditionService.getRenditions(NODE_REF))
.thenReturn(asList(rendition(NODE_REF, RENDITION_1), rendition(NODE_REF, RENDITION_2)));
for (final NodeRef rendition : asList(RENDITION_1, RENDITION_2))
{
when(mockRenditionService.getSourceNode(rendition)).thenReturn(rendition(NODE_REF, rendition));
}
classifiedAspect.onAddAspect(NODE_REF, ASPECT_CLASSIFIED);
for (NodeRef rendition : asList(RENDITION_1, RENDITION_2))
{
verify(mockCoreServicesExtras).copyAspect(NODE_REF, rendition, ClassifiedContentModel.ASPECT_CLASSIFIED);
}
}
@Test public void reclassificationOfNodeShouldReclassifyRenditions()
{
for (NodeRef n : asList(NODE_REF, RENDITION_1, RENDITION_2))
{
when(mockNodeService.hasAspect(n, ASPECT_CLASSIFIED)).thenReturn(true);
}
when(mockClassificationSchemeService.getClassificationLevelById("Top Secret")).thenReturn(TOP_SECRET);
when(mockClassificationSchemeService.getClassificationLevelById("Secret")).thenReturn(SECRET);
when(mockClassificationSchemeService.getReclassification(any(), any())).thenReturn(ClassificationSchemeService.Reclassification.DOWNGRADE);
when(mockRenditionService.getRenditions(eq(NODE_REF)))
.thenReturn(asList(rendition(NODE_REF, RENDITION_1), rendition(NODE_REF, RENDITION_2)));
Map<QName, Serializable> oldProps = new HashMap<>();
oldProps.put(PROP_CLASSIFIED_BY, "userone");
oldProps.put(PROP_CURRENT_CLASSIFICATION, "Top Secret");
Map<QName, Serializable> newProps = new HashMap<>(oldProps);
newProps.put(PROP_CURRENT_CLASSIFICATION, "Secret");
classifiedAspect.onUpdateProperties(NODE_REF, oldProps, newProps);
for (NodeRef rendition : asList(RENDITION_1, RENDITION_2))
{
verify(mockCoreServicesExtras).copyAspect(NODE_REF, rendition, ClassifiedContentModel.ASPECT_CLASSIFIED);
verify(mockReferralAdminService).attachReferrer(rendition, NODE_REF, ASPECT_CLASSIFIED);
}
}

View File

@@ -0,0 +1,120 @@
/*
* Copyright (C) 2005-2015 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* 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/>.
*/
package org.alfresco.module.org_alfresco_module_rm.referredmetadata;
import static java.util.Collections.emptySet;
import static org.alfresco.module.org_alfresco_module_rm.referredmetadata.ReferredMetadataException.InvalidMetadataReferral;
import static org.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtils.expectedException;
import static org.alfresco.module.org_alfresco_module_rm.test.util.FPUtils.asListFrom;
import static org.alfresco.module.org_alfresco_module_rm.test.util.FPUtils.asSet;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.alfresco.service.cmr.dictionary.AspectDefinition;
import org.alfresco.service.cmr.dictionary.AssociationDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
/**
* Unit tests for {@link MetadataReferral}.
*
* @author Neil Mc Erlean
* @since 3.0.a
*/
public class MetadataReferralUnitTest
{
@Mock DictionaryService mockDictionaryService;
@Mock NodeService mockNodeService;
private final ReferralAdminServiceImpl referralAdminService = new ReferralAdminServiceImpl();
private final QName aspect1 = QName.createQName("test", "aspect1");
private final QName aspect2 = QName.createQName("test", "aspect2");
private final QName assoc1 = QName.createQName("test", "assoc1");
@Before public void setUp()
{
MockitoAnnotations.initMocks(this);
referralAdminService.setNodeService(mockNodeService);
}
@Test public void nullOrEmptyReferralsAreForbidden()
{
asListFrom(() -> new MetadataReferral(),
() -> {
MetadataReferral mr = new MetadataReferral();
mr.setAssocType(assoc1);
mr.setAspects(null);
mr.setDictionaryService(mockDictionaryService);
return mr;
},
() -> {
MetadataReferral mr = new MetadataReferral();
mr.setAssocType(assoc1);
mr.setAspects(emptySet());
mr.setDictionaryService(mockDictionaryService);
return mr;
},
() -> {
MetadataReferral mr = new MetadataReferral();
mr.setAssocType(null);
mr.setAspects(asSet(aspect1, aspect2));
mr.setDictionaryService(mockDictionaryService);
return mr;
})
.forEach(mr -> expectedException(InvalidMetadataReferral.class, () -> {
mr.validateAndRegister();
return null;
})
);
}
@Test(expected=InvalidMetadataReferral.class)
public void referralMustHaveAssocThatExists()
{
when(mockDictionaryService.getAssociation(assoc1)).thenReturn(null);
when(mockDictionaryService.getAspect(aspect1)).thenReturn(mock(AspectDefinition.class));
MetadataReferral mr = new MetadataReferral();
mr.setAssocType(assoc1);
mr.setAspects(asSet(aspect1));
mr.setDictionaryService(mockDictionaryService);
mr.validateAndRegister();
}
@Test(expected=InvalidMetadataReferral.class)
public void referralMustHaveAspectsAllOfWhichExist()
{
when(mockDictionaryService.getAssociation(assoc1)).thenReturn(mock(AssociationDefinition.class));
when(mockDictionaryService.getAspect(aspect1)).thenReturn(mock(AspectDefinition.class));
when(mockDictionaryService.getAspect(aspect2)).thenReturn(null);
MetadataReferral mr = new MetadataReferral();
mr.setAssocType(assoc1);
mr.setAspects(asSet(aspect1, aspect2));
mr.setDictionaryService(mockDictionaryService);
mr.validateAndRegister();
}
}

View File

@@ -0,0 +1,142 @@
/*
* Copyright (C) 2005-2015 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* 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/>.
*/
package org.alfresco.module.org_alfresco_module_rm.referredmetadata;
import static java.util.Arrays.asList;
import static org.alfresco.module.org_alfresco_module_rm.referredmetadata.ReferredMetadataException.ChainedMetadataReferralUnsupported;
import static org.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtils.expectedException;
import static org.alfresco.module.org_alfresco_module_rm.test.util.FPUtils.asSet;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.AssociationRef;
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.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
/**
* Unit tests for {@link ReferralAdminServiceImpl}.
*
* @author Neil Mc Erlean
* @since 2.4.a
*/
public class ReferralAdminServiceImplUnitTest
{
@InjectMocks private final ReferralAdminServiceImpl referralAdminService = new ReferralAdminServiceImpl();
@Mock DictionaryService mockDictionaryService;
@Mock NodeService mockNodeService;
@Mock ReferralRegistry mockRegistry;
@Mock ReferredMetadataServiceImpl mockReferredMetadataService;
private final NodeRef node1 = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "node1");
private final NodeRef node2 = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "node2");
private final NodeRef node3 = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "node3");
private final QName assoc1 = QName.createQName("test", "assoc1");
private final QName aspect1 = QName.createQName("test", "aspect1");
private final QName aspect2 = QName.createQName("test", "aspect2");
private final QName assoc2 = QName.createQName("test", "assoc2");
private final QName aspect3 = QName.createQName("test", "aspect3");
private final MetadataReferral referral1 = new MetadataReferral()
{{
this.setAssocType(assoc1);
this.setAspects(asSet(aspect1, aspect2));
}};
private final MetadataReferral referral2 = new MetadataReferral()
{{
this.setAssocType(assoc2);
this.setAspects(asSet(aspect3));
}};
@Before public void setUp()
{
MockitoAnnotations.initMocks(this);
when(mockRegistry.getMetadataReferrals()).thenReturn(asSet(referral1, referral2));
}
@Test(expected=IllegalArgumentException.class)
public void attachingReferrerWithNoAspectConfiguredShouldFail()
{
referralAdminService.attachReferrer(node2, node1, aspect1);
}
@Test public void attachDetach()
{
when(mockRegistry.getReferralForAspect(aspect1)).thenReturn(referral1);
// attach
MetadataReferral d = attachReferrer(node1, node2, aspect1);
// validate
assertEquals(assoc1, d.getAssocType());
assertEquals(asSet(aspect1, aspect2), d.getAspects());
assertTrue(mockReferredMetadataService.isReferringMetadata(node1, aspect1));
assertFalse(mockReferredMetadataService.isReferringMetadata(node1, aspect3));
// detach
assertEquals(d, referralAdminService.detachReferrer(node1, aspect1));
}
private MetadataReferral attachReferrer(NodeRef referrer, NodeRef referent, QName aspectName)
{
MetadataReferral mr = referralAdminService.attachReferrer(referrer, referent, aspectName);
final QName assocType = mr.getAssocType();
when(mockNodeService.getSourceAssocs(referent, assocType)).thenReturn(asList(new AssociationRef(referrer, assocType, referent)));
when(mockNodeService.getTargetAssocs(referrer, assocType)).thenReturn(asList(new AssociationRef(referrer, assocType, referent)));
for (QName aspect : mr.getAspects())
{
when(mockReferredMetadataService.isReferringMetadata(referrer, aspect)).thenReturn(true);
}
return mr;
}
@Test public void chainsOfDelegationShouldBePrevented()
{
when(mockRegistry.getReferralForAspect(aspect1)).thenReturn(referral1);
// The node already has a delegation in place: node1 -> node2. We're trying to add to the
// end of the chain: node2 -> node3
when(mockNodeService.getSourceAssocs(node2, assoc1)).thenReturn(asList(new AssociationRef(node1, assoc1, node2)));
when(mockNodeService.getTargetAssocs(node1, assoc1)).thenReturn(asList(new AssociationRef(node1, assoc1, node2)));
expectedException(ChainedMetadataReferralUnsupported.class, () -> {
referralAdminService.attachReferrer(node2, node3, aspect1);
return null;
});
// Now try to add to the start of the chain: node3 -> node1
expectedException(ChainedMetadataReferralUnsupported.class, () -> {
referralAdminService.attachReferrer(node3, node1, aspect1);
return null;
});
}
}

View File

@@ -0,0 +1,196 @@
/*
* Copyright (C) 2005-2015 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* 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/>.
*/
package org.alfresco.module.org_alfresco_module_rm.referredmetadata;
import static java.util.Collections.emptyMap;
import static java.util.Arrays.asList;
import static org.alfresco.module.org_alfresco_module_rm.referredmetadata.ReferredMetadataException.ReferentNodeNotFound;
import static org.alfresco.module.org_alfresco_module_rm.referredmetadata.ReferredMetadataException.MetadataReferralNotFound;
import static org.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtils.expectedException;
import static org.alfresco.module.org_alfresco_module_rm.test.util.FPUtils.asSet;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.when;
import org.alfresco.service.cmr.dictionary.ClassDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
import org.alfresco.service.cmr.repository.AssociationRef;
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.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* Unit tests for {@link ReferredMetadataServiceImpl}.
*
* @author Neil Mc Erlean
* @since 3.0.a
*/
public class ReferredMetadataServiceImplUnitTest
{
@InjectMocks private final ReferredMetadataServiceImpl referredMetadataService = new ReferredMetadataServiceImpl();
@Mock DictionaryService mockDictionaryService;
@Mock NodeService mockNodeService;
@Mock ReferralAdminServiceImpl mockReferralAdminService;
@Mock ReferralRegistry mockReferralRegistry;
/** This node has a referent node. */
private final NodeRef referringNode = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "referringNode");
/** This is the referent for {@link #referringNode}. */
private final NodeRef referentNode = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "referentNode");
/** This node has no referent node. */
private final NodeRef nodeWithoutReferent = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "nodeWithoutReferent");
/** The type of the peer association that links the referringNode to its source. */
private final QName referralAssocType = QName.createQName("test", "referralAssocType");
/** The instance of the association between {@link #referringNode} and {@link #referentNode}. */
private final AssociationRef attachedReferralAssocRef = new AssociationRef(referringNode, referralAssocType, referentNode);
/** Name of an aspect that has been referred. */
private final QName referredAspect1 = QName.createQName("test", "referredAspect1");
/** Name of an aspect that has been referred. */
private final QName referredAspect2 = QName.createQName("test", "referredAspect2");
/** Name of a content class (a type in this case) that has not been referred.
* N.B. Types can't be referred currently. */
private final QName unreferredType = QName.createQName("test", "unreferredType");
private final QName referredProp = QName.createQName("test", "referredProp");
private final Serializable referredPropValue = "hello";
private final QName unreferredProp = QName.createQName("test", "unreferredProp");
private final MetadataReferral referral = new MetadataReferral()
{{
this.setAssocType(referralAssocType);
this.setAspects(asSet(referredAspect1, referredAspect2));
}};
@Before public void setUp()
{
MockitoAnnotations.initMocks(this);
final PropertyDefinition aspectProp = mock(PropertyDefinition.class);
final ClassDefinition aspectDefn = mock(ClassDefinition.class);
when(aspectDefn.getName()).thenReturn(referredAspect1);
when(aspectProp.getContainerClass()).thenReturn(aspectDefn);
when(aspectDefn.isAspect()).thenReturn(true);
final PropertyDefinition typeProp = mock(PropertyDefinition.class);
final ClassDefinition typeDefn = mock(ClassDefinition.class);
when(typeDefn.getName()).thenReturn(unreferredType);
when(typeProp.getContainerClass()).thenReturn(typeDefn);
when(typeDefn.isAspect()).thenReturn(false);
when(mockDictionaryService.getProperty(referredProp)).thenReturn(aspectProp);
when(mockReferralAdminService.getAttachedReferralsFrom(referringNode)).thenReturn(asSet(referral));
for (QName referredAspect : asSet(referredAspect1, referredAspect2))
{
when(mockReferralRegistry.getReferralForAspect(referredAspect)).thenReturn(referral);
when(mockNodeService.hasAspect(referentNode, referredAspect)).thenReturn(true);
}
when(mockNodeService.getSourceAssocs(referentNode, referralAssocType)).thenReturn(asList(attachedReferralAssocRef));
when(mockNodeService.getTargetAssocs(referringNode, referralAssocType)).thenReturn(asList(attachedReferralAssocRef));
when(mockNodeService.exists(any(NodeRef.class))).thenReturn(true);
when(mockNodeService.getProperties(referentNode))
.thenReturn(new HashMap<QName, Serializable>()
{{
this.put(referredProp, referredPropValue);
}});
}
@Test public void isReferringMetadata()
{
assertTrue(referredMetadataService.isReferringMetadata(referringNode, referredAspect1));
expectedException(MetadataReferralNotFound.class,
() -> referredMetadataService.isReferringMetadata(nodeWithoutReferent, unreferredType));
assertFalse(referredMetadataService.isReferringMetadata(nodeWithoutReferent, referredAspect1));
}
@Test public void getReferentNode()
{
assertEquals(referentNode, referredMetadataService.getReferentNode(referringNode, referredAspect1));
expectedException(MetadataReferralNotFound.class,
() -> {
referredMetadataService.getReferentNode(referringNode, unreferredType);
return null;
});
assertNull(referredMetadataService.getReferentNode(nodeWithoutReferent, referredAspect1));
}
@Test public void getReferredProperties()
{
final Map<QName, Serializable> expectedProps = new HashMap<>();
expectedProps.put(referredProp, referredPropValue);
assertEquals(expectedProps, referredMetadataService.getReferredProperties(referringNode, referredAspect1));
expectedException(MetadataReferralNotFound.class,
() -> referredMetadataService.getReferredProperties(referringNode, unreferredType));
expectedException(ReferentNodeNotFound.class,
() -> referredMetadataService.getReferredProperties(nodeWithoutReferent, referredAspect1));
}
@Test public void getReferredProperty()
{
assertEquals(referredPropValue, referredMetadataService.getReferredProperty(referringNode, referredProp));
expectedException(IllegalArgumentException.class,
() -> referredMetadataService.getReferredProperty(referringNode, unreferredProp));
expectedException(MetadataReferralNotFound.class,
() -> referredMetadataService.getReferredProperties(nodeWithoutReferent, referredProp));
}
@Test public void hasReferredAspect()
{
assertTrue(referredMetadataService.hasReferredAspect(referringNode, referredAspect1));
expectedException(MetadataReferralNotFound.class,
() -> referredMetadataService.hasReferredAspect(referringNode, unreferredType));
expectedException(ReferentNodeNotFound.class,
() -> referredMetadataService.hasReferredAspect(nodeWithoutReferent, referredAspect1));
}
@Test public void getAttachedReferrals()
{
final Map<MetadataReferral, NodeRef> expectedReferrals = new HashMap<>();
expectedReferrals.put(referral, referentNode);
assertEquals(expectedReferrals, referredMetadataService.getAttachedReferrals(referringNode));
assertEquals(emptyMap(), referredMetadataService.getAttachedReferrals(nodeWithoutReferent));
}
}

View File

@@ -0,0 +1,85 @@
/*
* Copyright (C) 2005-2015 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* 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/>.
*/
package org.alfresco.module.org_alfresco_module_rm.test.util;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Stream;
/**
* Utility class to help with Java 8 FP stuff.
*
* @author Neil Mc Erlean
* @since 2.4.a
*/
public class FPUtils
{
/**
* This method is intended to work exactly like {@code java.util.Arrays.asList()} but it takes
* a vararg of {@code Supplier}s instead of actual objects.
*
* @param suppliers a vararg of {@link Supplier}s giving a sequence of values for the list.
* @param <T> the type of elements in the list.
* @return the list with each element being the first retrieved from a {@code Supplier}.
*/
public static <T> List<T> asListFrom(Supplier<T>... suppliers)
{
if (suppliers == null || suppliers.length == 0)
{
return Collections.emptyList();
}
else
{
return Stream.of(suppliers)
.map(s -> s.get())
.collect(toList());
}
}
/**
* This method is intended to work exactly like {@link #asSet(Object[])}} but it takes
* a vararg of {@code Supplier}s instead of actual objects.
*
* @param suppliers a vararg of {@link Supplier}s giving a sequence of values for the set.
* @param <T> the type of elements in the set.
* @return the set with each element being the first retrieved from a {@code Supplier} (duplicates removed).
*/
public static <T> Set<T> asSetFrom(Supplier<T>... suppliers)
{
List<T> l = asListFrom(suppliers);
return new HashSet<>(l);
}
/**
* This utility method converts a vararg of objects into a Set<T>.
*
* @param objects the objects to be added to the set
* @return a Set of objects (any equal objects will of course not be duplicated)
*/
public static <T> Set<T> asSet(T... objects)
{
return new HashSet<>(asList(objects));
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright (C) 2005-2015 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* 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/>.
*/
package org.alfresco.module.org_alfresco_module_rm.test.util;
import static org.alfresco.module.org_alfresco_module_rm.test.util.FPUtils.asListFrom;
import static org.alfresco.module.org_alfresco_module_rm.test.util.FPUtils.asSet;
import static org.alfresco.module.org_alfresco_module_rm.test.util.FPUtils.asSetFrom;
import static org.junit.Assert.assertEquals;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.Test;
/**
* Unit tests for {@link FPUtils}.
*
* @author Neil Mc Erlean
* @since 2.4.a
*/
public class FPUtilsUnitTest
{
@Test public void asListShouldProduceList()
{
List<String> l = asListFrom(() -> "hello",
() -> "world",
() -> {
String s1 = "abc";
String s2 = "xyz";
return s1 + s2;
});
assertEquals(asList("hello", "world", "abcxyz"), l);
}
@Test public void asListShouldWorkForEmptyVarArgs()
{
assertEquals(emptyList(), FPUtils.<String>asListFrom());
}
@Test public void asSetShouldProduceSet()
{
assertEquals(new HashSet<>(asList("hello", "world")),
asSet("hello", "hello", "world"));
}
@Test public void asSetFromShouldWork()
{
Set<String> s = asSetFrom(() -> "hello",
() -> "hello",
() -> "world",
() -> {
String s1 = "wo";
String s2 = "rld";
return s1 + s2;
});
assertEquals(new HashSet<>(asList("hello", "world")), s);
}
}