From dcb3bc2934df53239245d90f43ecc8d561f02677 Mon Sep 17 00:00:00 2001 From: Will Abson Date: Wed, 3 Sep 2014 16:30:54 +0000 Subject: [PATCH] Merged HEAD-BUG-FIX (5.0/Cloud) to HEAD (5.0/Cloud) 80698: Merged WAT1 (5.0/Cloud) to HEAD-BUG-FIX (5.0/Cloud) 78457: ACE-1582: - Fixed facet's custom properties. - Made the GET facets web-script to be accessed by all authenticated users rather than just the Search-Admin/Network-Admin users. - Fixed FacetRestApiTests and added more tests. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@82996 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../solr-facet-config-admin.get.desc.xml | 8 + .../solr-facet-config-admin.get.json.ftl | 6 +- .../solr-facet-config-admin.post.desc.xml | 6 + .../solr-facet-config-admin.put.desc.xml | 6 + ...AbstractSolrFacetConfigAdminWebScript.java | 113 +++++- .../solr/facet/SolrFacetConfigAdminGet.java | 7 + .../repo/web/scripts/WebScriptTestSuite.java | 2 + .../scripts/solr/facet/FacetRestApiTest.java | 336 ++++++++++++++++-- 8 files changed, 452 insertions(+), 32 deletions(-) diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/solr/facet/solr-facet-config-admin.get.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/solr/facet/solr-facet-config-admin.get.desc.xml index a8f655f3fb..0b829ab3eb 100644 --- a/config/alfresco/templates/webscripts/org/alfresco/repository/solr/facet/solr-facet-config-admin.get.desc.xml +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/solr/facet/solr-facet-config-admin.get.desc.xml @@ -27,6 +27,14 @@ "site2", "site1" ], + "customProperties" : + { + "blockIncludeFacetRequest": + { + "name" : "{http:\/\/www.alfresco.org\/model\/solrfacetcustomproperty\/1.0}blockIncludeFacetRequest", + "value" : "true" + } + } "isEnabled" : true, "isDefault" : true } diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/solr/facet/solr-facet-config-admin.get.json.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/solr/facet/solr-facet-config-admin.get.json.ftl index 07be33dcb8..5a8d1c104e 100644 --- a/config/alfresco/templates/webscripts/org/alfresco/repository/solr/facet/solr-facet-config-admin.get.json.ftl +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/solr/facet/solr-facet-config-admin.get.json.ftl @@ -28,12 +28,10 @@ <#list propDetails.value as v> "${v?string}"<#if v_has_next>, - ], + ] <#else> - "value" : "${propDetails.value?string}", + "value" : "${propDetails.value?string}" - "type" : <#if propDetails.type??>"${propDetails.type}"<#else>null, - "title" : <#if propDetails.title??>"${propDetails.title}"<#else>null }<#if propDetails_has_next>, }, diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/solr/facet/solr-facet-config-admin.post.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/solr/facet/solr-facet-config-admin.post.desc.xml index 07dff4b647..17aab631e8 100644 --- a/config/alfresco/templates/webscripts/org/alfresco/repository/solr/facet/solr-facet-config-admin.post.desc.xml +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/solr/facet/solr-facet-config-admin.post.desc.xml @@ -23,6 +23,12 @@ "site2", "site1" ], + "customProperties" : { + "blockIncludeFacetRequest": { + "name" : "{http://www.alfresco.org/model/solrfacetcustomproperty/1.0}blockIncludeFacetRequest", + "value" : "true" + } + } "isEnabled" : true // if not provided, default value is false } diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/solr/facet/solr-facet-config-admin.put.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/solr/facet/solr-facet-config-admin.put.desc.xml index 8b9be2fb85..676e5d70ff 100644 --- a/config/alfresco/templates/webscripts/org/alfresco/repository/solr/facet/solr-facet-config-admin.put.desc.xml +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/solr/facet/solr-facet-config-admin.put.desc.xml @@ -25,6 +25,12 @@ "site2", "site1" ], + "customProperties" : { + "blockIncludeFacetRequest": { + "name" : "{http://www.alfresco.org/model/solrfacetcustomproperty/1.0}blockIncludeFacetRequest", + "value" : "true" + } + } "isEnabled" : true // if not provided, default value is false }
diff --git a/source/java/org/alfresco/repo/web/scripts/solr/facet/AbstractSolrFacetConfigAdminWebScript.java b/source/java/org/alfresco/repo/web/scripts/solr/facet/AbstractSolrFacetConfigAdminWebScript.java index ab3a3c3544..17e8e2fa7b 100644 --- a/source/java/org/alfresco/repo/web/scripts/solr/facet/AbstractSolrFacetConfigAdminWebScript.java +++ b/source/java/org/alfresco/repo/web/scripts/solr/facet/AbstractSolrFacetConfigAdminWebScript.java @@ -20,16 +20,22 @@ package org.alfresco.repo.web.scripts.solr.facet; import java.io.IOException; +import java.io.Serializable; +import java.util.ArrayList; import java.util.HashSet; import java.util.Map; import java.util.Set; import javax.servlet.http.HttpServletResponse; +import org.alfresco.repo.search.impl.solr.facet.SolrFacetModel; import org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties; +import org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties.CustomProperties; import org.alfresco.repo.search.impl.solr.facet.SolrFacetService; import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.service.namespace.QName; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -48,6 +54,8 @@ import org.springframework.extensions.webscripts.WebScriptRequest; */ public abstract class AbstractSolrFacetConfigAdminWebScript extends DeclarativeWebScript { + private static final Log logger = LogFactory.getLog(AbstractSolrFacetConfigAdminWebScript.class); + protected static final String PARAM_FILTER_ID = "filterID"; protected static final String PARAM_FACET_QNAME = "facetQName"; protected static final String PARAM_DISPLAY_NAME = "displayName"; @@ -60,6 +68,9 @@ public abstract class AbstractSolrFacetConfigAdminWebScript extends DeclarativeW protected static final String PARAM_SCOPED_SITES = "scopedSites"; protected static final String PARAM_INDEX = "index"; protected static final String PARAM_IS_ENABLED = "isEnabled"; + protected static final String PARAM_CUSTOM_PROPERTIES = "customProperties"; + protected static final String CUSTOM_PARAM_NAME = "name"; + protected static final String CUSTOM_PARAM_VALUE = "value"; protected SolrFacetService facetService; @@ -124,7 +135,8 @@ public abstract class AbstractSolrFacetConfigAdminWebScript extends DeclarativeW scopedSites.add(site); } } - + final JSONObject customPropJsonObj = getValue(JSONObject.class, json.opt(PARAM_CUSTOM_PROPERTIES), null); + Set customProps = getCustomProperties(customPropJsonObj); SolrFacetProperties fp = new SolrFacetProperties.Builder() .filterID(filterID) .facetQName(facetQName) @@ -136,7 +148,8 @@ public abstract class AbstractSolrFacetConfigAdminWebScript extends DeclarativeW .sortBy(sortBy) .scope(scope) .isEnabled(isEnabled) - .scopedSites(scopedSites).build(); + .scopedSites(scopedSites) + .customProperties(customProps).build(); return fp; } catch (IOException e) @@ -151,7 +164,7 @@ public abstract class AbstractSolrFacetConfigAdminWebScript extends DeclarativeW private T getValue(Class clazz, Object value, T defaultValue) throws JSONException { - if (value == null) + if (JSONObject.NULL.equals(value)) { return defaultValue; } @@ -166,5 +179,99 @@ public abstract class AbstractSolrFacetConfigAdminWebScript extends DeclarativeW } } + private Set getCustomProperties(JSONObject customPropsJsonObj) throws JSONException + { + if (customPropsJsonObj == null) + { + return null; + } + JSONArray keys = customPropsJsonObj.names(); + if (keys == null) + { + return null; + } + + Set customProps = new HashSet<>(keys.length()); + for (int i = 0, length = keys.length(); i < length; i++) + { + JSONObject jsonObj = customPropsJsonObj.getJSONObject((String) keys.get(i)); + + QName name = resolveToQName(getValue(String.class, jsonObj.opt(CUSTOM_PARAM_NAME), null)); + validateMandatoryCustomProps(name, CUSTOM_PARAM_NAME); + + Serializable value = null; + Object customPropValue = jsonObj.opt(CUSTOM_PARAM_VALUE); + validateMandatoryCustomProps(customPropValue, CUSTOM_PARAM_VALUE); + + if(customPropValue instanceof JSONArray) + { + JSONArray array = (JSONArray) customPropValue; + ArrayList list = new ArrayList<>(array.length()); + for(int j = 0; j < array.length(); j++) + { + list.add(getSerializableValue(array.get(j))); + } + value = list; + } + else + { + value = getSerializableValue(customPropValue); + } + + customProps.add(new CustomProperties(name, value)); + } + + if (logger.isDebugEnabled() && customProps.size() > 0) + { + logger.debug("Processed custom properties:" + customProps); + } + + return customProps; + } + + private void validateMandatoryCustomProps(Object obj, String paramName) throws JSONException + { + if (obj == null) + { + throw new JSONException("Invalid JSONObject in the Custom Properties JSON. [" + paramName + "] cannot be null."); + } + + } + + private Serializable getSerializableValue(Object object) throws JSONException + { + if (!(object instanceof Serializable)) + { + throw new JSONException("Invalid value in the Custom Properties JSON. [" + object + "] must be an instance of Serializable."); + } + return (Serializable) object; + } + + private QName resolveToQName(String qnameStr) throws JSONException + { + QName typeQName = null; + if (qnameStr == null) + { + return typeQName; + } + if(qnameStr.charAt(0) == QName.NAMESPACE_BEGIN && qnameStr.indexOf("solrfacetcustomproperty") < 0) + { + throw new JSONException("Invalid name in the Custom Properties JSON. Namespace URL must be [" + SolrFacetModel.SOLR_FACET_CUSTOM_PROPERTY_URL + "]"); + } + else if(qnameStr.charAt(0) == QName.NAMESPACE_BEGIN) + { + typeQName = QName.createQName(qnameStr); + } + else + { + typeQName = QName.createQName(SolrFacetModel.SOLR_FACET_CUSTOM_PROPERTY_URL, qnameStr); + } + if (logger.isDebugEnabled()) + { + logger.debug("Resolved facet's custom property name [" + qnameStr + "] into [" + typeQName + "]");System.out.println("QQQQQQQQQQQQQQQQQQQQQQQQQQQQQ:Resolved facet's custom property name [" + qnameStr + "] into [" + typeQName + "]"); + } + return typeQName; + } + abstract protected Map unprotectedExecuteImpl(WebScriptRequest req, Status status, Cache cache); } diff --git a/source/java/org/alfresco/repo/web/scripts/solr/facet/SolrFacetConfigAdminGet.java b/source/java/org/alfresco/repo/web/scripts/solr/facet/SolrFacetConfigAdminGet.java index e0addc6c91..2d1c1abc35 100644 --- a/source/java/org/alfresco/repo/web/scripts/solr/facet/SolrFacetConfigAdminGet.java +++ b/source/java/org/alfresco/repo/web/scripts/solr/facet/SolrFacetConfigAdminGet.java @@ -39,6 +39,13 @@ public class SolrFacetConfigAdminGet extends AbstractSolrFacetConfigAdminWebScri { private static final Log logger = LogFactory.getLog(SolrFacetConfigAdminGet.class); + @Override + protected Map executeImpl(final WebScriptRequest req, final Status status, final Cache cache) + { + // Allow all authenticated users view the filters + return unprotectedExecuteImpl(req, status, cache); + } + @Override protected Map unprotectedExecuteImpl(WebScriptRequest req, Status status, Cache cache) { diff --git a/source/test-java/org/alfresco/repo/web/scripts/WebScriptTestSuite.java b/source/test-java/org/alfresco/repo/web/scripts/WebScriptTestSuite.java index 610a25a76a..15475d9046 100644 --- a/source/test-java/org/alfresco/repo/web/scripts/WebScriptTestSuite.java +++ b/source/test-java/org/alfresco/repo/web/scripts/WebScriptTestSuite.java @@ -43,6 +43,7 @@ import org.alfresco.repo.web.scripts.rule.RuleServiceTest; import org.alfresco.repo.web.scripts.search.PersonSearchTest; import org.alfresco.repo.web.scripts.site.SiteServiceTest; import org.alfresco.repo.web.scripts.solr.SOLRWebScriptTest; +import org.alfresco.repo.web.scripts.solr.facet.FacetRestApiTest; import org.alfresco.repo.web.scripts.subscriptions.SubscriptionServiceRestApiTest; import org.alfresco.repo.web.scripts.tagging.TaggingServiceTest; import org.alfresco.repo.web.scripts.thumbnail.ThumbnailServiceTest; @@ -95,6 +96,7 @@ public class WebScriptTestSuite extends TestSuite suite.addTestSuite( PublishingRestApiTest.class ); suite.addTestSuite( SOLRWebScriptTest.class ); suite.addTestSuite( SubscriptionServiceRestApiTest.class ); + suite.addTestSuite( FacetRestApiTest.class ); // This uses a slightly different context // As such, we can't run it in the same suite as the others, diff --git a/source/test-java/org/alfresco/repo/web/scripts/solr/facet/FacetRestApiTest.java b/source/test-java/org/alfresco/repo/web/scripts/solr/facet/FacetRestApiTest.java index 4694247e30..04cd7c5fae 100644 --- a/source/test-java/org/alfresco/repo/web/scripts/solr/facet/FacetRestApiTest.java +++ b/source/test-java/org/alfresco/repo/web/scripts/solr/facet/FacetRestApiTest.java @@ -18,7 +18,10 @@ */ package org.alfresco.repo.web.scripts.solr.facet; +import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import org.alfresco.model.ContentModel; @@ -32,20 +35,20 @@ import org.alfresco.service.cmr.security.AuthorityService; import org.alfresco.service.cmr.security.AuthorityType; import org.alfresco.service.cmr.security.MutableAuthenticationService; import org.alfresco.service.cmr.security.PersonService; -import org.alfresco.util.Pair; import org.alfresco.util.PropertyMap; import org.alfresco.util.collections.CollectionUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener; -import org.springframework.extensions.webscripts.TestWebScriptServer.*; import org.springframework.extensions.webscripts.TestWebScriptServer.Response; +import org.springframework.extensions.webscripts.TestWebScriptServer.*; /** * This class tests the ReST API of the {@link SolrFacetService}. * * @author Neil Mc Erlean + * @author Jamal Kaabi-Mofrad * @since 5.0 */ public class FacetRestApiTest extends BaseWebScriptTest @@ -57,12 +60,15 @@ public class FacetRestApiTest extends BaseWebScriptTest private final static String GET_FACETS_URL = "/api/solr/facet-config"; private final static String PUT_FACET_URL_FORMAT = "/api/solr/facet-config/{0}?relativePos={1}"; + private final static String POST_FACETS_URL = GET_FACETS_URL; + private final static String PUT_FACETS_URL = GET_FACETS_URL; private MutableAuthenticationService authenticationService; private AuthorityService authorityService; private PersonService personService; private RetryingTransactionHelper transactionHelper; - + private List filters = new ArrayList(); + @Override protected void setUp() throws Exception { super.setUp(); @@ -70,7 +76,7 @@ public class FacetRestApiTest extends BaseWebScriptTest authorityService = getServer().getApplicationContext().getBean("AuthorityService", AuthorityService.class); personService = getServer().getApplicationContext().getBean("PersonService", PersonService.class); transactionHelper = getServer().getApplicationContext().getBean("retryingTransactionHelper", RetryingTransactionHelper.class); - + // Create test users. TODO Create these users @BeforeClass or at a testsuite scope. AuthenticationUtil.runAsSystem(new RunAsWork() { @@ -95,7 +101,17 @@ public class FacetRestApiTest extends BaseWebScriptTest @Override public void tearDown() throws Exception { super.tearDown(); - + + AuthenticationUtil.runAs(new RunAsWork() + { + @Override + public Void doWork() throws Exception + { + deleteFilters(); + return null; + } + }, SEARCH_ADMIN_USER); + AuthenticationUtil.runAsSystem(new RunAsWork() { @Override public Void doWork() throws Exception @@ -114,18 +130,79 @@ public class FacetRestApiTest extends BaseWebScriptTest }); } - public void testNonSearchAdminUserCannotAccessSolrFacets() throws Exception + public void testNonSearchAdminUserCannotCreateUpdateSolrFacets() throws Exception + { + // Create a filter + final JSONObject filter = new JSONObject(); + final String filterName = "filter" + System.currentTimeMillis(); + filters.add(filterName); + filter.put("filterID", filterName); + filter.put("facetQName", "{http://www.alfresco.org/model/content/1.0}test1"); + filter.put("displayName", "facet-menu.facet.test1"); + filter.put("displayControl", "alfresco/search/FacetFilters/test1"); + filter.put("maxFilters", 5); + filter.put("hitThreshold", 1); + filter.put("minFilterValueLength", 4); + filter.put("sortBy", "ALPHABETICALLY"); + + // Non-Search-Admin tries to create a filter + AuthenticationUtil.runAs(new RunAsWork() + { + @Override + public Void doWork() throws Exception + { + // Post the filter + sendRequest(new PostRequest(POST_FACETS_URL, filter.toString(), "application/json"), 403); + return null; + } + }, NON_SEARCH_ADMIN_USER); + + // Search-Admin creates a filter + AuthenticationUtil.runAs(new RunAsWork() + { + @Override + public Void doWork() throws Exception + { + // Post the filter + sendRequest(new PostRequest(POST_FACETS_URL, filter.toString(), "application/json"), 200); + return null; + } + }, SEARCH_ADMIN_USER); + + // Non-Search-Admin tries to modify the filter + AuthenticationUtil.runAs(new RunAsWork() + { + @Override + public Void doWork() throws Exception + { + Response response = sendRequest(new GetRequest(GET_FACETS_URL + "/" + filterName), 200); + JSONObject jsonRsp = new JSONObject(new JSONTokener(response.getContentAsString())); + assertEquals(filterName, jsonRsp.getString("filterID")); + assertEquals(5, jsonRsp.getInt("maxFilters")); + // Now change the maxFilters value and try to update + jsonRsp.put("maxFilters", 10); + sendRequest(new PutRequest(PUT_FACETS_URL, jsonRsp.toString(), "application/json"), 403); + + return null; + } + }, NON_SEARCH_ADMIN_USER); + } + + public void testNonSearchAdminUserCanGetFacets() throws Exception { AuthenticationUtil.runAs(new RunAsWork() { @Override public Void doWork() throws Exception { - sendRequest(new GetRequest(GET_FACETS_URL), 403); + Response response = sendRequest(new GetRequest(GET_FACETS_URL), 200); + JSONObject jsonRsp = new JSONObject(new JSONTokener(response.getContentAsString())); + List filters = getListFromJsonArray(jsonRsp.getJSONArray(FACETS)); + assertTrue(filters.size() > 0); return null; } }, NON_SEARCH_ADMIN_USER); } - + public void testSearchAdminCanGetFacets() throws Exception { AuthenticationUtil.runAs(new RunAsWork() @@ -147,7 +224,7 @@ public class FacetRestApiTest extends BaseWebScriptTest } }, SEARCH_ADMIN_USER); } - + public void testSearchAdminReordersFacets() throws Exception { AuthenticationUtil.runAs(new RunAsWork() @@ -164,15 +241,15 @@ public class FacetRestApiTest extends BaseWebScriptTest System.out.println("Received " + facetsArray.length() + " facets"); - final List> idsIndexes = getIdsIndexes(facetsArray); + final List idsIndexes = getListFromJsonArray(facetsArray); System.out.println(" IDs, indexes = " + idsIndexes); // Reorder them such that the last facet is moved left one place. assertTrue("There should be more than 1 built-in facet", facetsArray.length() > 1); - final Pair lastIndexIdPair = idsIndexes.get(idsIndexes.size() - 1); - final String url = PUT_FACET_URL_FORMAT.replace("{0}", lastIndexIdPair.getSecond()) + final String lastIndexId = idsIndexes.get(idsIndexes.size() - 1); + final String url = PUT_FACET_URL_FORMAT.replace("{0}", lastIndexId) .replace("{1}", "-1"); rsp = sendRequest(new PutRequest(url, "", "application/json"), 200); @@ -187,30 +264,231 @@ public class FacetRestApiTest extends BaseWebScriptTest System.out.println("Received " + newfacetsArray.length() + " facets"); - final List> newIdsIndexes = getIdsIndexes(newfacetsArray); + final List newIdsIndexes = getListFromJsonArray(newfacetsArray); System.out.println(" IDs, indexes = " + newIdsIndexes); - // Note here that the last Facet JSON object *is* moved one place up the list. - // But its index value does not change, which I think is as it should be. - assertEquals(CollectionUtils.moveLeft(1, lastIndexIdPair, idsIndexes), - newIdsIndexes); + assertEquals(CollectionUtils.moveLeft(1, lastIndexId, idsIndexes), newIdsIndexes); return null; } }, SEARCH_ADMIN_USER); } - - private List> getIdsIndexes(JSONArray facetsArray) throws JSONException + + public void testDefaultValues() throws Exception { - List> result = new ArrayList<>(); - + AuthenticationUtil.runAs(new RunAsWork() + { + @Override + public Void doWork() throws Exception + { + // Build the Filter object - ignore the optional values + JSONObject filter_one = new JSONObject(); + String filterNameOne = "filterOne" + System.currentTimeMillis(); + filters.add(filterNameOne); + filter_one.put("filterID", filterNameOne); + filter_one.put("facetQName", "{http://www.alfresco.org/model/content/1.0}test1"); + filter_one.put("displayName", "facet-menu.facet.test1"); + filter_one.put("displayControl", "alfresco/search/FacetFilters/test1"); + filter_one.put("maxFilters", 5); + filter_one.put("hitThreshold", 1); + filter_one.put("minFilterValueLength", 4); + filter_one.put("sortBy", "ALPHABETICALLY"); + + // Post the filter + Response response = sendRequest(new PostRequest(POST_FACETS_URL, filter_one.toString(),"application/json"), 200); + + // Retrieve the created filter + response = sendRequest(new GetRequest(GET_FACETS_URL + "/" + filterNameOne), 200); + JSONObject jsonRsp = new JSONObject(new JSONTokener(response.getContentAsString())); + assertEquals(filterNameOne, jsonRsp.getString("filterID")); + assertEquals("{http://www.alfresco.org/model/content/1.0}test1", jsonRsp.getString("facetQName")); + assertEquals("facet-menu.facet.test1", jsonRsp.getString("displayName")); + assertEquals("alfresco/search/FacetFilters/test1", jsonRsp.getString("displayControl")); + assertEquals(5, jsonRsp.getInt("maxFilters")); + assertEquals(1, jsonRsp.getInt("hitThreshold")); + assertEquals(4, jsonRsp.getInt("minFilterValueLength")); + assertEquals("ALPHABETICALLY", jsonRsp.getString("sortBy")); + // Check the Default values + assertEquals("ALL", jsonRsp.getString("scope")); + assertFalse(jsonRsp.getBoolean("isEnabled")); + assertFalse(jsonRsp.getBoolean("isDefault")); + + // Build the Filter object with all the values + JSONObject filter_two = new JSONObject(); + String filterNameTwo = "filterTwo" + System.currentTimeMillis(); + filters.add(filterNameTwo); + filter_two.put("filterID", filterNameTwo); + filter_two.put("facetQName", "{http://www.alfresco.org/model/content/1.0}test2"); + filter_two.put("displayName", "facet-menu.facet.test2"); + filter_two.put("displayControl", "alfresco/search/FacetFilters/test2"); + filter_two.put("maxFilters", 5); + filter_two.put("hitThreshold", 1); + filter_two.put("minFilterValueLength", 4); + filter_two.put("sortBy", "ALPHABETICALLY"); + filter_two.put("scope", "SCOPED_SITES"); + List expectedValues = Arrays.asList(new String[] { "sit1", "site2", "site3" }); + filter_two.put("scopedSites", expectedValues); + filter_two.put("isEnabled", true); + + // Post the filter + response = sendRequest(new PostRequest(POST_FACETS_URL, filter_two.toString(), "application/json"), 200); + + // Retrieve the created filter + response = sendRequest(new GetRequest(GET_FACETS_URL + "/" + filterNameTwo), 200); + jsonRsp = new JSONObject(new JSONTokener(response.getContentAsString())); + + assertEquals(filterNameTwo, jsonRsp.getString("filterID")); + assertEquals("SCOPED_SITES", jsonRsp.getString("scope")); + assertTrue(jsonRsp.getBoolean("isEnabled")); + JSONArray jsonArray = jsonRsp.getJSONArray("scopedSites"); + List retrievedValues = getListFromJsonArray(jsonArray); + // Sort the list + Collections.sort(retrievedValues); + assertEquals(expectedValues, retrievedValues); + + return null; + } + }, SEARCH_ADMIN_USER); + } + + public void testFacetCustomProperties() throws Exception + { + AuthenticationUtil.runAs(new RunAsWork() + { + @Override + public Void doWork() throws Exception + { + // Build the Filter object + JSONObject filter = new JSONObject(); + String filterName = "filter" + System.currentTimeMillis(); + filters.add(filterName); + filter.put("filterID", filterName); + filter.put("facetQName", "{http://www.alfresco.org/model/content/1.0}content.size.test"); + filter.put("displayName", "facet-menu.facet.size.test"); + filter.put("displayControl", "alfresco/search/FacetFilters/test"); + filter.put("maxFilters", 5); + filter.put("hitThreshold", 1); + filter.put("minFilterValueLength", 4); + filter.put("sortBy", "ALPHABETICALLY"); + + JSONObject customProp = new JSONObject(); + // 1st custom prop + JSONObject blockIncludeRequest = new JSONObject(); + blockIncludeRequest.put("name", "blockIncludeFacetRequest"); + blockIncludeRequest.put("value", "true"); + customProp.put("blockIncludeFacetRequest", blockIncludeRequest); + + // 2nd custom prop + JSONObject multipleValue = new JSONObject(); + multipleValue.put("name", "multipleValueTest"); + List expectedValues = Arrays.asList(new String[] { "sit1", "site2", "site3" }); + multipleValue.put("value", expectedValues); + customProp.put("multipleValueTest", multipleValue); + + filter.put("customProperties", customProp); + + // Post the filter + Response response = sendRequest(new PostRequest(POST_FACETS_URL, filter.toString(),"application/json"), 200); + // Retrieve the created filter + response = sendRequest(new GetRequest(GET_FACETS_URL + "/" + filterName), 200); + JSONObject jsonRsp = new JSONObject(new JSONTokener(response.getContentAsString())); + customProp = jsonRsp.getJSONObject("customProperties"); + + blockIncludeRequest = customProp.getJSONObject("blockIncludeFacetRequest"); + assertEquals("{http://www.alfresco.org/model/solrfacetcustomproperty/1.0}blockIncludeFacetRequest", blockIncludeRequest.get("name")); + assertEquals("true", blockIncludeRequest.get("value")); + + multipleValue = customProp.getJSONObject("multipleValueTest"); + assertEquals("{http://www.alfresco.org/model/solrfacetcustomproperty/1.0}multipleValueTest", multipleValue.get("name")); + + JSONArray jsonArray = (JSONArray) multipleValue.get("value"); + List retrievedValues = getListFromJsonArray(jsonArray); + // Sort the list + Collections.sort(retrievedValues); + assertEquals(expectedValues, retrievedValues); + + return null; + } + }, SEARCH_ADMIN_USER); + } + + public void testCreateUpdateFacetWithInvalidFilterId() throws Exception + { + // Build the Filter object + final JSONObject filter = new JSONObject(); + final String filterName = "filter" + System.currentTimeMillis(); + filters.add(filterName); + filter.put("filterID", filterName); + filter.put("facetQName", "{http://www.alfresco.org/model/content/1.0}test1"); + filter.put("displayName", "facet-menu.facet.test1"); + filter.put("displayControl", "alfresco/search/FacetFilters/test1"); + filter.put("maxFilters", 5); + filter.put("hitThreshold", 1); + filter.put("minFilterValueLength", 4); + filter.put("sortBy", "ALPHABETICALLY"); + + AuthenticationUtil.runAs(new RunAsWork() + { + @Override + public Void doWork() throws Exception + { + // Post the filter + sendRequest(new PostRequest(POST_FACETS_URL, filter.toString(), "application/json"), 200); + return null; + } + }, SEARCH_ADMIN_USER); + + // Admin tries to change the FilterID value + AuthenticationUtil.runAs(new RunAsWork() + { + @Override + public Void doWork() throws Exception + { + // Retrieve the created filter + Response response = sendRequest(new GetRequest(GET_FACETS_URL + "/" + filterName), 200); + JSONObject jsonRsp = new JSONObject(new JSONTokener(response.getContentAsString())); + assertEquals(filterName, jsonRsp.getString("filterID")); + // Now change the filterID value and try to update + jsonRsp.put("filterID", filterName + "Modified"); + sendRequest(new PutRequest(PUT_FACETS_URL, jsonRsp.toString(), "application/json"), 400); + + return null; + } + }, SEARCH_ADMIN_USER); + + // Admin tries to create a filter with a duplicate FilterID + AuthenticationUtil.runAs(new RunAsWork() + { + @Override + public Void doWork() throws Exception + { + // Post the filter + sendRequest(new PostRequest(POST_FACETS_URL, filter.toString(), "application/json"), 400); + + return null; + } + }, SEARCH_ADMIN_USER); + + } + + private List getListFromJsonArray(JSONArray facetsArray) throws JSONException + { + List result = new ArrayList<>(); + for (int i = 0; i < facetsArray.length(); i++) { - final JSONObject nextFacet = facetsArray.getJSONObject(i); - final int nextIndex = nextFacet.getInt("index"); - final String nextId = nextFacet.getString("filterID"); - result.add(new Pair<>(nextIndex, nextId)); + Object object = facetsArray.get(i); + if (object instanceof JSONObject) + { + final JSONObject nextFacet = (JSONObject) object; + final String nextId = nextFacet.getString("filterID"); + result.add(nextId); + } + else + { + result.add((String) object); + } } return result; } @@ -242,4 +520,12 @@ public class FacetRestApiTest extends BaseWebScriptTest personService.deletePerson(userName); } } + + private void deleteFilters() throws IOException + { + for (String filter : filters) + { + sendRequest(new DeleteRequest(GET_FACETS_URL + "/" + filter), 200); + } + } }