ACE-5622: Search api scope now uses "locations"

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@133518 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gethin James
2016-12-08 15:49:24 +00:00
parent ffdb176db2
commit d8c47d60e1
17 changed files with 539 additions and 47 deletions

View File

@@ -36,7 +36,7 @@ import org.junit.runners.Suite.SuiteClasses;
*/
@RunWith(Suite.class)
@SuiteClasses({ SearchMapperTests.class, ResultMapperTests.class,SearchQuerySerializerTests.class,
SearchApiWebscriptTests.class})
SearchApiWebscriptTests.class, StoreMapperTests.class})
public class AllSearchApiTests
{
}

View File

@@ -31,22 +31,30 @@ import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.notNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.alfresco.repo.search.EmptyResultSet;
import org.alfresco.repo.search.impl.lucene.SolrJSONResultSet;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.version.Version2Model;
import org.alfresco.repo.version.common.VersionImpl;
import org.alfresco.rest.api.DeletedNodes;
import org.alfresco.rest.api.impl.NodesImpl;
import org.alfresco.rest.api.lookups.PropertyLookupRegistry;
import org.alfresco.rest.api.model.Node;
import org.alfresco.rest.api.model.UserInfo;
import org.alfresco.rest.api.nodes.NodeVersionsRelation;
import org.alfresco.rest.api.search.context.FacetFieldContext;
import org.alfresco.rest.api.search.context.FacetQueryContext;
import org.alfresco.rest.api.search.context.SearchContext;
import org.alfresco.rest.api.search.context.SpellCheckContext;
import org.alfresco.rest.api.search.impl.ResultMapper;
import org.alfresco.rest.api.search.impl.StoreMapper;
import org.alfresco.rest.api.search.model.HighlightEntry;
import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Params;
import org.alfresco.service.ServiceRegistry;
@@ -58,6 +66,9 @@ import org.alfresco.service.cmr.search.GeneralHighlightParameters;
import org.alfresco.service.cmr.search.LimitBy;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchParameters;
import org.alfresco.service.cmr.version.Version;
import org.alfresco.service.cmr.version.VersionHistory;
import org.alfresco.service.cmr.version.VersionService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.GUID;
import org.json.JSONException;
@@ -95,6 +106,9 @@ public class ResultMapperTests
+ "},"
+ "\"processedDenies\":true, \"lastIndexedTx\":34}";
public static final Params EMPTY_PARAMS = Params.valueOf((String)null,(String)null,(WebScriptRequest) null);
public static final String FROZEN_ID = "frozen";
public static final String FROZEN_VER = "1.1";
private static final long VERSIONED_ID = 521l;
@BeforeClass
public static void setupTests() throws Exception
@@ -105,8 +119,57 @@ public class ResultMapperTests
NodesImpl nodes = mock(NodesImpl.class);
ServiceRegistry sr = mock(ServiceRegistry.class);
DeletedNodes deletedNodes = mock(DeletedNodes.class);
nodes.setServiceRegistry(sr);
VersionService versionService = mock(VersionService.class);
VersionHistory versionHistory = mock(VersionHistory.class);
Map<String, Serializable> versionProperties = new HashMap<>();
versionProperties.put(Version.PROP_DESCRIPTION, "ver desc");
versionProperties.put(Version2Model.PROP_VERSION_TYPE, "v type");
when(versionHistory.getVersion(anyString())).thenAnswer(invocation ->
{
return new VersionImpl(versionProperties,new NodeRef(StoreMapper.STORE_REF_VERSION2_SPACESSTORE, GUID.generate()));
});
NodeService nodeService = mock(NodeService.class);
when(versionService.getVersionHistory(notNull(NodeRef.class))).thenAnswer(invocation ->
{
Object[] args = invocation.getArguments();
NodeRef aNode = (NodeRef)args[0];
return versionHistory;
});
when(nodeService.getProperties(notNull(NodeRef.class))).thenAnswer(invocation ->
{
Object[] args = invocation.getArguments();
NodeRef aNode = (NodeRef)args[0];
if (StoreMapper.STORE_REF_VERSION2_SPACESSTORE.equals(aNode.getStoreRef()))
{
nodeProps.put(Version2Model.PROP_QNAME_FROZEN_NODE_REF, new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, FROZEN_ID+aNode.getId()));
nodeProps.put(Version2Model.PROP_QNAME_VERSION_LABEL, FROZEN_VER);
}
return nodeProps;
});
when(sr.getVersionService()).thenReturn(versionService);
when(sr.getNodeService()).thenReturn(nodeService);
when(nodes.validateOrLookupNode(notNull(String.class), anyString())).thenAnswer(invocation ->
{
Object[] args = invocation.getArguments();
String aNode = (String)args[0];
if (aNode.endsWith(""+VERSIONED_ID))
{
throw new EntityNotFoundException(""+VERSIONED_ID);
}
else
{
return new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, aNode);
}
});
// // NodeRef nodeRef = nodes.validateOrLookupNode(nodeId, null);
when(nodes.getFolderOrDocument(notNull(NodeRef.class), any(), any(), any(), any())).thenAnswer(new Answer<Node>() {
@Override
public Node answer(InvocationOnMock invocation) throws Throwable {
@@ -120,9 +183,28 @@ public class ResultMapperTests
return new Node(aNode, (NodeRef)args[1], nodeProps, mapUserInfo, sr);
}
});
when(deletedNodes.getDeletedNode(notNull(String.class), any(), anyBoolean(), any())).thenAnswer(new Answer<Node>() {
@Override
public Node answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
String nodeId = (String)args[0];
if (FROZEN_ID.equals(nodeId)) throw new EntityNotFoundException(nodeId);
NodeRef aNode = new NodeRef(StoreRef.STORE_REF_ARCHIVE_SPACESSTORE, nodeId);
return new Node(aNode, new NodeRef(StoreRef.STORE_REF_ARCHIVE_SPACESSTORE,"unknown"), nodeProps, mapUserInfo, sr);
}
});
mapper = new ResultMapper();
mapper.setNodes(nodes);
mapper.setStoreMapper(new StoreMapper());
mapper.setPropertyLookup(new PropertyLookupRegistry());
mapper.setDeletedNodes(deletedNodes);
mapper.setServiceRegistry(sr);
NodeVersionsRelation nodeVersionsRelation = new NodeVersionsRelation();
nodeVersionsRelation.setNodes(nodes);
nodeVersionsRelation.setServiceRegistry(sr);
nodeVersionsRelation.afterPropertiesSet();
mapper.setNodeVersions(nodeVersionsRelation);
}
@Test
@@ -138,13 +220,14 @@ public class ResultMapperTests
@Test
public void testToCollectionWithPagingInfo() throws Exception
{
ResultSet results = mockResultset(Arrays.asList(514l));
ResultSet results = mockResultset(Arrays.asList(514l), Arrays.asList(566l, VERSIONED_ID));
CollectionWithPagingInfo<Node> collectionWithPage = mapper.toCollectionWithPagingInfo(EMPTY_PARAMS,results);
assertNotNull(collectionWithPage);
Long found = results.getNumberFound();
assertEquals(found.intValue(), collectionWithPage.getTotalItems().intValue());
Node firstNode = collectionWithPage.getCollection().stream().findFirst().get();
assertNotNull(firstNode.getSearch().getScore());
assertEquals(StoreMapper.LIVE_NODES, firstNode.getLocation());
collectionWithPage.getCollection().stream().forEach(aNode -> {
List<HighlightEntry> high = aNode.getSearch().getHighlight();
if (high != null)
@@ -155,13 +238,18 @@ public class ResultMapperTests
assertNotNull(first.getSnippets());
}
});
//1 deleted node in the test data
assertEquals(1l, collectionWithPage.getCollection().stream().filter(node -> StoreMapper.DELETED.equals(node.getLocation())).count());
//1 version nodes in the test data (and 1 is not shown because it is in the archive store)
assertEquals(1l, collectionWithPage.getCollection().stream().filter(node -> StoreMapper.VERSIONS.equals(node.getLocation())).count());
}
@Test
public void testToSearchContext() throws Exception
{
ResultSet results = mockResultset(Collections.emptyList());
SearchContext searchContext = mapper.toSearchContext((SolrJSONResultSet) results);
ResultSet results = mockResultset(Collections.emptyList(),Collections.emptyList());
SearchContext searchContext = mapper.toSearchContext((SolrJSONResultSet) results, 0);
assertEquals(34l, searchContext.getConsistency().getlastTxId());
assertEquals(6, searchContext.getFacetQueries().size());
// assertEquals("{!afts}creator:admin",searchContext.getFacetQueries().get(0).getLabel());
@@ -209,7 +297,7 @@ public class ResultMapperTests
assertEquals(")",sp.getHighlight().getFields().get(1).getPostfix());
}
private ResultSet mockResultset(List<Long> archivedNodes) throws JSONException
private ResultSet mockResultset(List<Long> archivedNodes, List<Long> versionNodes) throws JSONException
{
NodeService nodeService = mock(NodeService.class);
@@ -218,7 +306,8 @@ public class ResultMapperTests
public NodeRef answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
//If the DBID is in the list archivedNodes, instead of returning a noderef return achivestore noderef
if (archivedNodes.contains(args[0])) return new NodeRef(StoreRef.STORE_REF_ARCHIVE_SPACESSTORE, GUID.generate());;
if (archivedNodes.contains(args[0])) return new NodeRef(StoreRef.STORE_REF_ARCHIVE_SPACESSTORE, GUID.generate());
if (versionNodes.contains(args[0])) return new NodeRef(StoreMapper.STORE_REF_VERSION2_SPACESSTORE, GUID.generate()+args[0]);
return new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, GUID.generate());
}
});

View File

@@ -29,14 +29,13 @@ import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.fail;
import static org.alfresco.service.cmr.repository.StoreRef.PROTOCOL_DELETED;
import static org.alfresco.service.cmr.repository.StoreRef.PROTOCOL_TEST;
import static org.alfresco.service.cmr.search.SearchService.LANGUAGE_CMIS_ALFRESCO;
import static org.alfresco.service.cmr.search.SearchService.LANGUAGE_FTS_ALFRESCO;
import static org.alfresco.service.cmr.search.SearchService.LANGUAGE_LUCENE;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import org.alfresco.rest.api.search.impl.SearchMapper;
import org.alfresco.rest.api.search.impl.StoreMapper;
import org.alfresco.rest.api.search.model.Default;
import org.alfresco.rest.api.search.model.FacetField;
import org.alfresco.rest.api.search.model.FacetFields;
@@ -58,13 +57,11 @@ import org.alfresco.service.cmr.search.LimitBy;
import org.alfresco.service.cmr.search.SearchParameters;
import org.alfresco.service.cmr.search.SearchParameters.FieldFacet;
import org.alfresco.service.cmr.search.SearchService;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Tests the SearchMapper class
@@ -77,6 +74,12 @@ public class SearchMapperTests
static SearchMapper searchMapper = new SearchMapper();
static SerializerTestHelper helper = new SerializerTestHelper();
@BeforeClass
public static void setupTests() throws Exception
{
searchMapper.setStoreMapper(new StoreMapper());
}
@Test(expected = IllegalArgumentException.class)
public void testMandatory() throws Exception
{
@@ -431,12 +434,11 @@ public class SearchMapperTests
assertNotNull(iae);
}
searchMapper.fromScope(searchParameters, new Scope(Arrays.asList(
new StoreRef(PROTOCOL_TEST, "SpacesStore").toString(),
new StoreRef(PROTOCOL_DELETED, "SpacesStore").toString())));
assertEquals(2 ,searchParameters.getStores().size());
assertEquals("test://SpacesStore",searchParameters.getStores().get(0).toString());
assertEquals("deleted://SpacesStore",searchParameters.getStores().get(1).toString());
searchMapper.fromScope(searchParameters, new Scope(Arrays.asList(StoreMapper.DELETED, StoreMapper.LIVE_NODES, StoreMapper.VERSIONS)));
assertEquals(3 ,searchParameters.getStores().size());
assertEquals(StoreRef.STORE_REF_ARCHIVE_SPACESSTORE.toString(),searchParameters.getStores().get(0).toString());
assertEquals(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE.toString(),searchParameters.getStores().get(1).toString());
assertEquals(StoreMapper.STORE_REF_VERSION2_SPACESSTORE.toString(),searchParameters.getStores().get(2).toString());
}
@Test

View File

@@ -31,6 +31,7 @@ import static org.junit.Assert.assertTrue;
import org.alfresco.rest.api.search.context.FacetFieldContext;
import org.alfresco.rest.api.search.context.FacetFieldContext.Bucket;
import org.alfresco.rest.api.search.context.SpellCheckContext;
import org.alfresco.rest.api.search.impl.StoreMapper;
import org.alfresco.rest.api.search.model.Default;
import org.alfresco.rest.api.search.model.FacetField;
import org.alfresco.rest.api.search.model.SearchQuery;
@@ -92,8 +93,8 @@ public class SearchQuerySerializerTests
assertEquals("facquery",searchQuery.getFacetQueries().get(0).getQuery());
assertEquals("facnoused",searchQuery.getFacetQueries().get(0).getLabel());
assertEquals("alfrezco", searchQuery.getSpellcheck().getQuery());
assertEquals(1, searchQuery.getScope().getStores().size());
assertEquals("workspace://SpacesStore", searchQuery.getScope().getStores().get(0));
assertEquals(1, searchQuery.getScope().getLocations().size());
assertEquals(StoreMapper.LIVE_NODES, searchQuery.getScope().getLocations().get(0));
assertEquals(2, searchQuery.getFacetFields().getFacets().size());
FacetField ff = searchQuery.getFacetFields().getFacets().get(0);
assertEquals("cm:creator", ff.getField());

View File

@@ -64,7 +64,7 @@ public class SerializerTestHelper implements RequestReader
+ "\"facetQueries\": [{\"query\": \"facquery\",\"label\": \"facnoused\"}],"
+ "\"spellcheck\": {\"query\": \"alfrezco\"},"
+ "\"limits\": {\"permissionEvaluationCount\": \"2000\",\"permissionEvaluationTime\": \"5000\"},"
+ "\"scope\": { \"stores\": [\"workspace://SpacesStore\"]},"
+ "\"scope\": { \"locations\": [\"nodes\"]},"
+ "\"fields\": [\"id\", \"name\"],"
+ "\"highlight\": {\"prefix\": \"[\",\"postfix\": \"]\",\"snippetCount\": \"20\","
+ "\"fragmentSize\": \"10\",\"mergeContiguous\": \"true\",\"maxAnalyzedChars\": \"40\", \"usePhraseHighlighter\": \"true\","

View File

@@ -0,0 +1,88 @@
/*-
* #%L
* Alfresco Remote API
* %%
* 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 <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.api.search;
import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertNull;
import org.alfresco.rest.api.search.impl.StoreMapper;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.junit.Test;
/**
* Tests the StoreMapper class
*
* @author Gethin James
*/
public class StoreMapperTests
{
static StoreMapper storeMapper = new StoreMapper();
@Test(expected = InvalidArgumentException.class)
public void testGetStoreErrors() throws Exception
{
storeMapper.getStoreRef(null);
}
@Test(expected = InvalidArgumentException.class)
public void testGetStoreWithEmpty() throws Exception
{
storeMapper.getStoreRef("");
}
@Test(expected = InvalidArgumentException.class)
public void testInvalidStoreName() throws Exception
{
storeMapper.getStoreRef("bob");
}
@Test
public void testGetStoreRef() throws Exception
{
assertEquals(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, storeMapper.getStoreRef("nodes"));
assertEquals(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, storeMapper.getStoreRef("Nodes"));
assertEquals(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, storeMapper.getStoreRef("NODES"));
assertEquals(StoreMapper.STORE_REF_VERSION2_SPACESSTORE, storeMapper.getStoreRef("Versions"));
assertEquals(StoreMapper.STORE_REF_VERSION2_SPACESSTORE, storeMapper.getStoreRef("versions"));
assertEquals(StoreMapper.STORE_REF_VERSION2_SPACESSTORE, storeMapper.getStoreRef("VERSIONS"));
assertEquals(StoreRef.STORE_REF_ARCHIVE_SPACESSTORE, storeMapper.getStoreRef("Deleted-nodes"));
assertEquals(StoreRef.STORE_REF_ARCHIVE_SPACESSTORE, storeMapper.getStoreRef("deleted-nodes"));
assertEquals(StoreRef.STORE_REF_ARCHIVE_SPACESSTORE, storeMapper.getStoreRef("DELETED-NODES"));
}
@Test
public void testGetStore() throws Exception
{
assertEquals(storeMapper.LIVE_NODES, storeMapper.getStore(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "not interested")));
assertEquals(storeMapper.VERSIONS, storeMapper.getStore(new NodeRef(StoreMapper.STORE_REF_VERSION2_SPACESSTORE, "not interested")));
assertEquals(storeMapper.DELETED, storeMapper.getStore(new NodeRef(StoreRef.STORE_REF_ARCHIVE_SPACESSTORE, "not interested")));
assertNull(storeMapper.getStore(null));
}
}