mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-31 17:39:05 +00:00
Search changes, adding support for search by reason key and 'other source' names
This commit is contained in:
@@ -467,12 +467,23 @@
|
||||
<property name="permissionService" ref="PermissionService" />
|
||||
<property name="personService" ref="PersonService" />
|
||||
<property name="recordCategoryUtil" ref="RecordCategoryUtil" />
|
||||
<property name="classificationReasonsUtil" ref="ClassificationReasonsUtil" />
|
||||
<property name="classificationSourcesUtil" ref="ClassificationSourcesUtil" />
|
||||
</bean>
|
||||
|
||||
<bean id="RecordCategoryUtil" class="org.alfresco.module.org_alfresco_module_rm.script.slingshot.RecordCategoryUtil">
|
||||
<property name="nodeService" ref="NodeService"/>
|
||||
</bean>
|
||||
|
||||
<bean id="ClassificationReasonsUtil" class="org.alfresco.module.org_alfresco_module_rm.script.slingshot.ClassificationReasonsUtil">
|
||||
<property name="nodeService" ref="NodeService" />
|
||||
</bean>
|
||||
|
||||
<bean id="ClassificationSourcesUtil"
|
||||
class="org.alfresco.module.org_alfresco_module_rm.script.slingshot.ClassificationSourcesUtil">
|
||||
<property name="nodeService" ref="NodeService" />
|
||||
</bean>
|
||||
|
||||
<bean
|
||||
id="webscript.org.alfresco.slingshot.rmsearch.rmsearchproperties.get"
|
||||
class="org.alfresco.module.org_alfresco_module_rm.script.slingshot.RMSearchPropertiesGet"
|
||||
|
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* #%L
|
||||
* Alfresco Records Management Module
|
||||
* %%
|
||||
* Copyright (C) 2005 - 2018 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 <http://www.gnu.org/licenses/>.
|
||||
* #L%
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.script.slingshot;
|
||||
|
||||
import static org.alfresco.model.ContentModel.PROP_NAME;
|
||||
import static org.alfresco.service.namespace.QName.createQName;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
|
||||
/**
|
||||
* Method to replace the plain text classification reason id with the correct nodeRef during record search
|
||||
* @author Ross Gale
|
||||
* @since 2.7
|
||||
*/
|
||||
public class ClassificationReasonsUtil extends SearchUtil
|
||||
{
|
||||
|
||||
public static final String CR_URI = "http://www.alfresco.org/model/securitymarks/1.0";
|
||||
public static final QName CLASSIFICATION_REASONS_CONTAINER = createQName(CR_URI,"classificationReasonsContainer");
|
||||
public static final QName PROP_CLASSIFICATION_REASON_CODE = createQName(CR_URI, "classificationReasonCode");
|
||||
public static final String REASONS_KEY = "clf:classificationReasons:";
|
||||
|
||||
/**
|
||||
* Replace plain text reason id with nodeRef
|
||||
* @param searchQuery String e.g. clf:classificationReasons:1.4(a)
|
||||
* @return String e.g. clf:classificationReasons:5cc6d344-fa94-4370-9c81-d947b7e8f2ac
|
||||
*/
|
||||
public String replaceReasonWithNodeRef(String searchQuery)
|
||||
{
|
||||
List<String> queries = new ArrayList<>(Arrays.asList(searchQuery.split(" ")));
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (String queryToEdit : queries)
|
||||
{
|
||||
if(queryToEdit.contains(REASONS_KEY))
|
||||
{
|
||||
for (String reasonId : retrieveAllNodeIds(getRootContainer(CLASSIFICATION_REASONS_CONTAINER)))
|
||||
{
|
||||
NodeRef reasonNodeRef = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, reasonId);
|
||||
Map<QName, Serializable> properties = nodeService.getProperties(reasonNodeRef);
|
||||
if (queryToEdit.equals(REASONS_KEY + properties.get(PROP_CLASSIFICATION_REASON_CODE).toString()) ||
|
||||
queryToEdit.equals(REASONS_KEY +"\""+ properties.get(PROP_CLASSIFICATION_REASON_CODE).toString() + "\""))
|
||||
{
|
||||
queryToEdit = REASONS_KEY + properties.get(PROP_NAME).toString();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
stringBuilder.append(queryToEdit).append(" ");
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* #%L
|
||||
* Alfresco Records Management Module
|
||||
* %%
|
||||
* Copyright (C) 2005 - 2018 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 <http://www.gnu.org/licenses/>.
|
||||
* #L%
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.script.slingshot;
|
||||
|
||||
|
||||
import static org.alfresco.model.ContentModel.PROP_NODE_UUID;
|
||||
import static org.alfresco.service.namespace.QName.createQName;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
|
||||
/**
|
||||
* Method to replace the plain text classification source name with all possible nodeRefs during record search
|
||||
* @author Ross Gale
|
||||
* @since 2.7
|
||||
*/
|
||||
public class ClassificationSourcesUtil extends SearchUtil
|
||||
{
|
||||
|
||||
|
||||
public static final String CS_URI = "http://www.alfresco.org/model/classificationsources/1.0";
|
||||
public static final QName CLASSIFICATION_SOURCES_CONTAINER = createQName(CS_URI, "classificationSourcesContainer");
|
||||
public static final QName PROP_CLASSIFICATION_SOURCE_NAME = createQName(CS_URI, "classificationSourceName");
|
||||
public static final String SOURCES_KEY = "cs:appliedSources:";
|
||||
public static final String START = "start";
|
||||
public static final String END = "end";
|
||||
|
||||
/**
|
||||
* Replace plain text source name with all matching nodeRefs
|
||||
*
|
||||
* @param searchQuery String e.g. clf:classificationReasons:"Other source"
|
||||
* @return String e.g. (cs:appliedSources:5cc6d344-fa94-4370-9c81-d947b7e8f2ac OR cs:appliedSources:47afd476-358f-4007-a35e-8f83adb06523)
|
||||
*/
|
||||
public String replaceSourceNameWithNodeRef(String searchQuery)
|
||||
{
|
||||
Pattern pattern = Pattern.compile("cs:appliedSources:\"[^\"]*\"");
|
||||
Matcher matcher = pattern.matcher(searchQuery);
|
||||
StringBuilder builder = new StringBuilder(searchQuery);
|
||||
Map<Integer,Map<String, Integer>> index = new HashMap<>();
|
||||
int count = 0;
|
||||
//create a map of where the strings to replace are
|
||||
while(matcher.find())
|
||||
{
|
||||
index.put(count, new HashMap<>());
|
||||
index.get(count).put(START,matcher.start());
|
||||
index.get(count).put(END, matcher.end());
|
||||
count++;
|
||||
}
|
||||
//Go through the string in reverse and replace the plain text reference with nodeIds
|
||||
for(int i = index.size(); i > 0; i--)
|
||||
{
|
||||
Map<String, Integer> element = index.get(i-1);
|
||||
int start = element.get(START);
|
||||
int end = element.get(END);
|
||||
builder.replace(start, end, replaceSingleInstance(searchQuery.substring(start, end)));
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private String replaceSingleInstance(String str)
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
if (str.contains(SOURCES_KEY))
|
||||
{
|
||||
boolean multipleResults = false;
|
||||
stringBuilder.append('(');
|
||||
for (String sourceId : retrieveAllNodeIds(getRootContainer(CLASSIFICATION_SOURCES_CONTAINER)))
|
||||
{
|
||||
NodeRef reasonNodeRef = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, sourceId);
|
||||
Map<QName, Serializable> properties = nodeService.getProperties(reasonNodeRef);
|
||||
if (str.equals(SOURCES_KEY + "\"" + properties.get(PROP_CLASSIFICATION_SOURCE_NAME).toString() + "\""))
|
||||
{
|
||||
if (multipleResults)
|
||||
{
|
||||
stringBuilder.append(" OR ");
|
||||
}
|
||||
stringBuilder.append(SOURCES_KEY + "\"" + properties.get(PROP_NODE_UUID).toString() + "\"");
|
||||
//Sources create a node each time even if all the details are the same this will allow multiple nodeIds to be added for a single string
|
||||
multipleResults = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
stringBuilder.append(')');
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -27,6 +27,9 @@
|
||||
|
||||
package org.alfresco.module.org_alfresco_module_rm.script.slingshot;
|
||||
|
||||
import static org.alfresco.module.org_alfresco_module_rm.script.slingshot.ClassificationReasonsUtil.REASONS_KEY;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.script.slingshot.ClassificationSourcesUtil.SOURCES_KEY;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
@@ -103,6 +106,12 @@ public class RMSearchGet extends DeclarativeWebScript
|
||||
/** Utility class for record categories */
|
||||
private RecordCategoryUtil recordCategoryUtil;
|
||||
|
||||
/** Utility class for classification reasons (enterprise only) */
|
||||
private ClassificationReasonsUtil classificationReasonsUtil;
|
||||
|
||||
/** Utility class for classification sources (enterprise only) */
|
||||
private ClassificationSourcesUtil classificationSourcesUtil;
|
||||
|
||||
/**
|
||||
* @param recordsManagementSearchService records management search service
|
||||
*/
|
||||
@@ -159,6 +168,16 @@ public class RMSearchGet extends DeclarativeWebScript
|
||||
this.recordCategoryUtil = recordCategoryUtil;
|
||||
}
|
||||
|
||||
public void setClassificationReasonsUtil(ClassificationReasonsUtil classificationReasonsUtil)
|
||||
{
|
||||
this.classificationReasonsUtil = classificationReasonsUtil;
|
||||
}
|
||||
|
||||
public void setClassificationSourcesUtil(ClassificationSourcesUtil classificationSourcesUtil)
|
||||
{
|
||||
this.classificationSourcesUtil = classificationSourcesUtil;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param personService person service
|
||||
*/
|
||||
@@ -198,6 +217,19 @@ public class RMSearchGet extends DeclarativeWebScript
|
||||
String filters = req.getParameter(PARAM_FILTERS);
|
||||
// TODO this is optional
|
||||
|
||||
//Replace any plain text reason ids with the appropriate node reference
|
||||
if(query.contains(REASONS_KEY))
|
||||
{
|
||||
query = classificationReasonsUtil.replaceReasonWithNodeRef(query);
|
||||
}
|
||||
|
||||
//replace any plain test other source titles with appropriate node ref
|
||||
if(query.contains(SOURCES_KEY))
|
||||
{
|
||||
query = classificationSourcesUtil.replaceSourceNameWithNodeRef(query);
|
||||
}
|
||||
|
||||
|
||||
// Convert into a rm search parameter object
|
||||
RecordsManagementSearchParameters searchParameters =
|
||||
SavedSearchDetailsCompatibility.createSearchParameters(filters, new String[]{",", "/"}, sortby, namespaceService);
|
||||
|
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* #%L
|
||||
* Alfresco Records Management Module
|
||||
* %%
|
||||
* Copyright (C) 2005 - 2018 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 <http://www.gnu.org/licenses/>.
|
||||
* #L%
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.script.slingshot;
|
||||
|
||||
import static org.alfresco.model.ContentModel.ASSOC_CHILDREN;
|
||||
import static org.alfresco.model.ContentModel.TYPE_CONTAINER;
|
||||
import static org.alfresco.service.cmr.repository.StoreRef.STORE_REF_WORKSPACE_SPACESSTORE;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.alfresco.error.AlfrescoRuntimeException;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Parent class for records search utilities
|
||||
* @author Ross Gale
|
||||
* @since 2.7
|
||||
*/
|
||||
public class SearchUtil
|
||||
{
|
||||
/**
|
||||
* Node service
|
||||
*/
|
||||
protected NodeService nodeService;
|
||||
|
||||
/**
|
||||
* Setter for node service
|
||||
* @param nodeService Node service
|
||||
*/
|
||||
public void setNodeService(NodeService nodeService)
|
||||
{
|
||||
this.nodeService = nodeService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use a container node ref and return the nodeIds of the contents
|
||||
* @param nodeRef container
|
||||
* @return list of nodeIds
|
||||
*/
|
||||
protected Set<String> retrieveAllNodeIds(NodeRef nodeRef)
|
||||
{
|
||||
List<ChildAssociationRef> childAssocRefs = nodeService.getChildAssocs(nodeRef);
|
||||
return childAssocRefs.stream().map(assoc -> assoc.getChildRef().getId()).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to get the classification reason root container.
|
||||
* The method creates the container if it doesn't already exist.
|
||||
*
|
||||
* @return reference to the classification reason root container
|
||||
*/
|
||||
protected NodeRef getRootContainer(QName container)
|
||||
{
|
||||
NodeRef rootNodeRef = nodeService.getRootNode(STORE_REF_WORKSPACE_SPACESSTORE);
|
||||
List<ChildAssociationRef> assocRefs = nodeService.getChildAssocs(rootNodeRef, ASSOC_CHILDREN, container);
|
||||
|
||||
if (assocRefs.size() == 0)
|
||||
{
|
||||
return nodeService.createNode(rootNodeRef, ASSOC_CHILDREN, container, TYPE_CONTAINER).getChildRef();
|
||||
} else if (assocRefs.size() != 1)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Only one container is allowed.");
|
||||
} else
|
||||
{
|
||||
return assocRefs.iterator().next().getChildRef();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* #%L
|
||||
* Alfresco Records Management Module
|
||||
* %%
|
||||
* Copyright (C) 2005 - 2018 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 <http://www.gnu.org/licenses/>.
|
||||
* #L%
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.script.slingshot;
|
||||
|
||||
import static org.alfresco.model.ContentModel.ASSOC_CHILDREN;
|
||||
import static org.alfresco.model.ContentModel.PROP_NAME;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.script.slingshot.ClassificationReasonsUtil.CLASSIFICATION_REASONS_CONTAINER;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.script.slingshot.ClassificationReasonsUtil.PROP_CLASSIFICATION_REASON_CODE;
|
||||
import static org.alfresco.service.cmr.repository.StoreRef.STORE_REF_WORKSPACE_SPACESSTORE;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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 org.mockito.MockitoAnnotations;
|
||||
|
||||
/**
|
||||
* @author Ross Gale
|
||||
* @since 2.7
|
||||
*/
|
||||
public class ClassificationReasonsUtilUnitTest
|
||||
{
|
||||
|
||||
@Mock
|
||||
private NodeService nodeService;
|
||||
|
||||
@Mock
|
||||
private ChildAssociationRef childAssociationRef;
|
||||
|
||||
@Mock
|
||||
private ChildAssociationRef reason;
|
||||
|
||||
@Mock
|
||||
private Map<QName, Serializable> properties;
|
||||
|
||||
@InjectMocks
|
||||
private ClassificationReasonsUtil classificationReasonsUtil;
|
||||
|
||||
private NodeRef childNodeRef;
|
||||
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
MockitoAnnotations.initMocks(this);
|
||||
NodeRef rootNodeRef = new NodeRef("workspace://SpacesStore/rootNodeRef");
|
||||
NodeRef containerNodeRef = new NodeRef("workspace://SpacesStore/containerNodeRef");
|
||||
childNodeRef = new NodeRef("workspace://SpacesStore/childNodeRef");
|
||||
List<ChildAssociationRef> assocRefs = new ArrayList<>();
|
||||
List<ChildAssociationRef> childAssocRefs = new ArrayList<>();
|
||||
assocRefs.add(childAssociationRef);
|
||||
childAssocRefs.add(reason);
|
||||
when(reason.getChildRef()).thenReturn(childNodeRef);
|
||||
when(nodeService.getRootNode(STORE_REF_WORKSPACE_SPACESSTORE)).thenReturn(rootNodeRef);
|
||||
when(nodeService.getChildAssocs(rootNodeRef, ASSOC_CHILDREN, CLASSIFICATION_REASONS_CONTAINER)).thenReturn(assocRefs);
|
||||
when(childAssociationRef.getChildRef()).thenReturn(containerNodeRef);
|
||||
when(nodeService.getChildAssocs(containerNodeRef)).thenReturn(childAssocRefs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check no modifications are made to non matching parts of the query string
|
||||
*/
|
||||
@Test
|
||||
public void testNoChangeMadeToStringIfKeyNotFound()
|
||||
{
|
||||
String stringToTest = "noChangeMadeToString";
|
||||
assertEquals("Change made to string",stringToTest, classificationReasonsUtil.replaceReasonWithNodeRef(stringToTest).trim());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check no modifications made if the plain text parameter doesn't have a stored match
|
||||
*/
|
||||
@Test
|
||||
public void testNoChangeMadeToStringIfMatchNotFound()
|
||||
{
|
||||
when(nodeService.getProperties(childNodeRef)).thenReturn(properties);
|
||||
when(properties.get(PROP_CLASSIFICATION_REASON_CODE)).thenReturn("not a match!");
|
||||
String stringToTest = "clf:classificationReasons:noChangeMadeToString";
|
||||
assertEquals("Change made to string", stringToTest, classificationReasonsUtil.replaceReasonWithNodeRef(stringToTest).trim());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the query is updated correctly when a match is found
|
||||
*/
|
||||
@Test
|
||||
public void testChangeMadeToStringIfMatchFound()
|
||||
{
|
||||
when(nodeService.getProperties(childNodeRef)).thenReturn(properties);
|
||||
when(properties.get(PROP_CLASSIFICATION_REASON_CODE)).thenReturn("stringToChange");
|
||||
when(properties.get(PROP_NAME)).thenReturn("newString");
|
||||
String stringToTest = "clf:classificationReasons:stringToChange";
|
||||
assertEquals("No change made to string", "clf:classificationReasons:newString", classificationReasonsUtil.replaceReasonWithNodeRef(stringToTest).trim());
|
||||
}
|
||||
}
|
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* #%L
|
||||
* Alfresco Records Management Module
|
||||
* %%
|
||||
* Copyright (C) 2005 - 2018 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 <http://www.gnu.org/licenses/>.
|
||||
* #L%
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.script.slingshot;
|
||||
|
||||
import static org.alfresco.model.ContentModel.ASSOC_CHILDREN;
|
||||
import static org.alfresco.model.ContentModel.PROP_NODE_UUID;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.script.slingshot.ClassificationReasonsUtil.PROP_CLASSIFICATION_REASON_CODE;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.script.slingshot.ClassificationSourcesUtil.CLASSIFICATION_SOURCES_CONTAINER;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.script.slingshot.ClassificationSourcesUtil.PROP_CLASSIFICATION_SOURCE_NAME;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.script.slingshot.ClassificationSourcesUtil.SOURCES_KEY;
|
||||
import static org.alfresco.service.cmr.repository.StoreRef.STORE_REF_WORKSPACE_SPACESSTORE;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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 org.mockito.MockitoAnnotations;
|
||||
|
||||
/**
|
||||
* @author Ross Gale
|
||||
* @since 2.7
|
||||
*/
|
||||
public class ClassificationSourcesUtilUnitTest
|
||||
{
|
||||
|
||||
@Mock
|
||||
private NodeService nodeService;
|
||||
|
||||
@Mock
|
||||
private ChildAssociationRef childAssociationRef;
|
||||
|
||||
@Mock
|
||||
private ChildAssociationRef source, secondSource;
|
||||
|
||||
@Mock
|
||||
private Map<QName, Serializable> properties;
|
||||
|
||||
@Mock
|
||||
private Map<QName, Serializable> secondSetOfProperties;
|
||||
|
||||
@InjectMocks
|
||||
private ClassificationSourcesUtil classificationSourcesUtil;
|
||||
|
||||
private List<ChildAssociationRef> childAssocRefs;
|
||||
|
||||
private NodeRef childNodeRef;
|
||||
|
||||
private NodeRef childNodeRef2;
|
||||
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
MockitoAnnotations.initMocks(this);
|
||||
NodeRef rootNodeRef = new NodeRef("workspace://SpacesStore/rootNodeRef");
|
||||
NodeRef containerNodeRef = new NodeRef("workspace://SpacesStore/containerNodeRef");
|
||||
childNodeRef = new NodeRef("workspace://SpacesStore/childNodeRef");
|
||||
childNodeRef2 = new NodeRef("workspace://SpacesStore/childNodeRef2");
|
||||
List<ChildAssociationRef> assocRefs = new ArrayList<>();
|
||||
childAssocRefs = new ArrayList<>();
|
||||
assocRefs.add(childAssociationRef);
|
||||
childAssocRefs.add(source);
|
||||
when(source.getChildRef()).thenReturn(childNodeRef);
|
||||
when(nodeService.getRootNode(STORE_REF_WORKSPACE_SPACESSTORE)).thenReturn(rootNodeRef);
|
||||
when(nodeService.getChildAssocs(rootNodeRef, ASSOC_CHILDREN, CLASSIFICATION_SOURCES_CONTAINER)).thenReturn(assocRefs);
|
||||
when(childAssociationRef.getChildRef()).thenReturn(containerNodeRef);
|
||||
when(nodeService.getChildAssocs(containerNodeRef)).thenReturn(childAssocRefs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check no modifications are made to non matching parts of the query string
|
||||
*/
|
||||
@Test
|
||||
public void testNoChangeMadeToStringIfKeyNotFound()
|
||||
{
|
||||
String stringToTest = "noChangeMadeToString";
|
||||
assertEquals("Change made to string",stringToTest, classificationSourcesUtil.replaceSourceNameWithNodeRef(stringToTest));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check no modifications made if the plain text parameter doesn't have a stored match
|
||||
*/
|
||||
@Test
|
||||
public void testNoChangeMadeToStringIfMatchNotFound()
|
||||
{
|
||||
when(nodeService.getProperties(childNodeRef)).thenReturn(properties);
|
||||
when(properties.get(PROP_CLASSIFICATION_REASON_CODE)).thenReturn("not a match!");
|
||||
String stringToTest = SOURCES_KEY + "noChangeMadeToString";
|
||||
assertEquals("Change made to string", stringToTest, classificationSourcesUtil.replaceSourceNameWithNodeRef(stringToTest));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the query is updated correctly when a match is found
|
||||
*/
|
||||
@Test
|
||||
public void testChangeMadeToStringIfMatchFound()
|
||||
{
|
||||
when(nodeService.getProperties(childNodeRef)).thenReturn(properties);
|
||||
when(properties.get(PROP_CLASSIFICATION_SOURCE_NAME)).thenReturn("stringToChange");
|
||||
when(properties.get(PROP_NODE_UUID)).thenReturn("newString");
|
||||
String stringToTest = SOURCES_KEY + "\"stringToChange\"";
|
||||
assertEquals("No change made to string", "(cs:appliedSources:\"newString\")", classificationSourcesUtil.replaceSourceNameWithNodeRef(stringToTest));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the query is updated correctly when multiple matches are found
|
||||
*
|
||||
* This is required as the source name isn't unique to the container.
|
||||
*/
|
||||
@Test
|
||||
public void testChangeMadeToStringIfMultipleMatchesFound()
|
||||
{
|
||||
childAssocRefs.add(secondSource);
|
||||
when(secondSource.getChildRef()).thenReturn(childNodeRef2);
|
||||
when(nodeService.getProperties(childNodeRef)).thenReturn(properties);
|
||||
when(nodeService.getProperties(childNodeRef2)).thenReturn(secondSetOfProperties);
|
||||
when(properties.get(PROP_CLASSIFICATION_SOURCE_NAME)).thenReturn("stringToChange");
|
||||
when(properties.get(PROP_NODE_UUID)).thenReturn("newString");
|
||||
when(secondSetOfProperties.get(PROP_CLASSIFICATION_SOURCE_NAME)).thenReturn("stringToChange");
|
||||
when(secondSetOfProperties.get(PROP_NODE_UUID)).thenReturn("secondNewString");
|
||||
String stringToTest = SOURCES_KEY + "\"stringToChange\"";
|
||||
String actual = classificationSourcesUtil.replaceSourceNameWithNodeRef(stringToTest);
|
||||
assertTrue(actual.contains("cs:appliedSources:\"newString\""));
|
||||
assertTrue(actual.contains("cs:appliedSources:\"secondNewString\""));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user