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
This commit is contained in:
Will Abson
2014-09-03 16:30:54 +00:00
parent 9f2d601915
commit dcb3bc2934
8 changed files with 452 additions and 32 deletions

View File

@@ -27,6 +27,14 @@
"site2", "site2",
"site1" "site1"
], ],
"customProperties" :
{
"blockIncludeFacetRequest":
{
"name" : "{http:\/\/www.alfresco.org\/model\/solrfacetcustomproperty\/1.0}blockIncludeFacetRequest",
"value" : "true"
}
}
"isEnabled" : true, "isEnabled" : true,
"isDefault" : true "isDefault" : true
} }

View File

@@ -28,12 +28,10 @@
<#list propDetails.value as v> <#list propDetails.value as v>
"${v?string}"<#if v_has_next>,</#if> "${v?string}"<#if v_has_next>,</#if>
</#list> </#list>
], ]
<#else> <#else>
"value" : "${propDetails.value?string}", "value" : "${propDetails.value?string}"
</#if> </#if>
"type" : <#if propDetails.type??>"${propDetails.type}"<#else>null</#if>,
"title" : <#if propDetails.title??>"${propDetails.title}"<#else>null</#if>
}<#if propDetails_has_next>,</#if> }<#if propDetails_has_next>,</#if>
</#list> </#list>
}, },

View File

@@ -23,6 +23,12 @@
"site2", "site2",
"site1" "site1"
], ],
"customProperties" : {
"blockIncludeFacetRequest": {
"name" : "{http://www.alfresco.org/model/solrfacetcustomproperty/1.0}blockIncludeFacetRequest",
"value" : "true"
}
}
"isEnabled" : true // if not provided, default value is false "isEnabled" : true // if not provided, default value is false
} }

View File

@@ -25,6 +25,12 @@
"site2", "site2",
"site1" "site1"
], ],
"customProperties" : {
"blockIncludeFacetRequest": {
"name" : "{http://www.alfresco.org/model/solrfacetcustomproperty/1.0}blockIncludeFacetRequest",
"value" : "true"
}
}
"isEnabled" : true // if not provided, default value is false "isEnabled" : true // if not provided, default value is false
}</pre><br/> }</pre><br/>

View File

@@ -20,16 +20,22 @@
package org.alfresco.repo.web.scripts.solr.facet; package org.alfresco.repo.web.scripts.solr.facet;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.servlet.http.HttpServletResponse; 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;
import org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties.CustomProperties;
import org.alfresco.repo.search.impl.solr.facet.SolrFacetService; import org.alfresco.repo.search.impl.solr.facet.SolrFacetService;
import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.service.namespace.QName; 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.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@@ -48,6 +54,8 @@ import org.springframework.extensions.webscripts.WebScriptRequest;
*/ */
public abstract class AbstractSolrFacetConfigAdminWebScript extends DeclarativeWebScript 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_FILTER_ID = "filterID";
protected static final String PARAM_FACET_QNAME = "facetQName"; protected static final String PARAM_FACET_QNAME = "facetQName";
protected static final String PARAM_DISPLAY_NAME = "displayName"; 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_SCOPED_SITES = "scopedSites";
protected static final String PARAM_INDEX = "index"; protected static final String PARAM_INDEX = "index";
protected static final String PARAM_IS_ENABLED = "isEnabled"; 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; protected SolrFacetService facetService;
@@ -124,7 +135,8 @@ public abstract class AbstractSolrFacetConfigAdminWebScript extends DeclarativeW
scopedSites.add(site); scopedSites.add(site);
} }
} }
final JSONObject customPropJsonObj = getValue(JSONObject.class, json.opt(PARAM_CUSTOM_PROPERTIES), null);
Set<CustomProperties> customProps = getCustomProperties(customPropJsonObj);
SolrFacetProperties fp = new SolrFacetProperties.Builder() SolrFacetProperties fp = new SolrFacetProperties.Builder()
.filterID(filterID) .filterID(filterID)
.facetQName(facetQName) .facetQName(facetQName)
@@ -136,7 +148,8 @@ public abstract class AbstractSolrFacetConfigAdminWebScript extends DeclarativeW
.sortBy(sortBy) .sortBy(sortBy)
.scope(scope) .scope(scope)
.isEnabled(isEnabled) .isEnabled(isEnabled)
.scopedSites(scopedSites).build(); .scopedSites(scopedSites)
.customProperties(customProps).build();
return fp; return fp;
} }
catch (IOException e) catch (IOException e)
@@ -151,7 +164,7 @@ public abstract class AbstractSolrFacetConfigAdminWebScript extends DeclarativeW
private <T> T getValue(Class<T> clazz, Object value, T defaultValue) throws JSONException private <T> T getValue(Class<T> clazz, Object value, T defaultValue) throws JSONException
{ {
if (value == null) if (JSONObject.NULL.equals(value))
{ {
return defaultValue; return defaultValue;
} }
@@ -166,5 +179,99 @@ public abstract class AbstractSolrFacetConfigAdminWebScript extends DeclarativeW
} }
} }
private Set<CustomProperties> getCustomProperties(JSONObject customPropsJsonObj) throws JSONException
{
if (customPropsJsonObj == null)
{
return null;
}
JSONArray keys = customPropsJsonObj.names();
if (keys == null)
{
return null;
}
Set<CustomProperties> 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<Serializable> 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<String, Object> unprotectedExecuteImpl(WebScriptRequest req, Status status, Cache cache); abstract protected Map<String, Object> unprotectedExecuteImpl(WebScriptRequest req, Status status, Cache cache);
} }

View File

@@ -39,6 +39,13 @@ public class SolrFacetConfigAdminGet extends AbstractSolrFacetConfigAdminWebScri
{ {
private static final Log logger = LogFactory.getLog(SolrFacetConfigAdminGet.class); private static final Log logger = LogFactory.getLog(SolrFacetConfigAdminGet.class);
@Override
protected Map<String, Object> executeImpl(final WebScriptRequest req, final Status status, final Cache cache)
{
// Allow all authenticated users view the filters
return unprotectedExecuteImpl(req, status, cache);
}
@Override @Override
protected Map<String, Object> unprotectedExecuteImpl(WebScriptRequest req, Status status, Cache cache) protected Map<String, Object> unprotectedExecuteImpl(WebScriptRequest req, Status status, Cache cache)
{ {

View File

@@ -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.search.PersonSearchTest;
import org.alfresco.repo.web.scripts.site.SiteServiceTest; import org.alfresco.repo.web.scripts.site.SiteServiceTest;
import org.alfresco.repo.web.scripts.solr.SOLRWebScriptTest; 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.subscriptions.SubscriptionServiceRestApiTest;
import org.alfresco.repo.web.scripts.tagging.TaggingServiceTest; import org.alfresco.repo.web.scripts.tagging.TaggingServiceTest;
import org.alfresco.repo.web.scripts.thumbnail.ThumbnailServiceTest; import org.alfresco.repo.web.scripts.thumbnail.ThumbnailServiceTest;
@@ -95,6 +96,7 @@ public class WebScriptTestSuite extends TestSuite
suite.addTestSuite( PublishingRestApiTest.class ); suite.addTestSuite( PublishingRestApiTest.class );
suite.addTestSuite( SOLRWebScriptTest.class ); suite.addTestSuite( SOLRWebScriptTest.class );
suite.addTestSuite( SubscriptionServiceRestApiTest.class ); suite.addTestSuite( SubscriptionServiceRestApiTest.class );
suite.addTestSuite( FacetRestApiTest.class );
// This uses a slightly different context // This uses a slightly different context
// As such, we can't run it in the same suite as the others, // As such, we can't run it in the same suite as the others,

View File

@@ -18,7 +18,10 @@
*/ */
package org.alfresco.repo.web.scripts.solr.facet; package org.alfresco.repo.web.scripts.solr.facet;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import org.alfresco.model.ContentModel; 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.AuthorityType;
import org.alfresco.service.cmr.security.MutableAuthenticationService; import org.alfresco.service.cmr.security.MutableAuthenticationService;
import org.alfresco.service.cmr.security.PersonService; import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.util.Pair;
import org.alfresco.util.PropertyMap; import org.alfresco.util.PropertyMap;
import org.alfresco.util.collections.CollectionUtils; import org.alfresco.util.collections.CollectionUtils;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONTokener; import org.json.JSONTokener;
import org.springframework.extensions.webscripts.TestWebScriptServer.*;
import org.springframework.extensions.webscripts.TestWebScriptServer.Response; import org.springframework.extensions.webscripts.TestWebScriptServer.Response;
import org.springframework.extensions.webscripts.TestWebScriptServer.*;
/** /**
* This class tests the ReST API of the {@link SolrFacetService}. * This class tests the ReST API of the {@link SolrFacetService}.
* *
* @author Neil Mc Erlean * @author Neil Mc Erlean
* @author Jamal Kaabi-Mofrad
* @since 5.0 * @since 5.0
*/ */
public class FacetRestApiTest extends BaseWebScriptTest public class FacetRestApiTest extends BaseWebScriptTest
@@ -57,11 +60,14 @@ public class FacetRestApiTest extends BaseWebScriptTest
private final static String GET_FACETS_URL = "/api/solr/facet-config"; 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 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 MutableAuthenticationService authenticationService;
private AuthorityService authorityService; private AuthorityService authorityService;
private PersonService personService; private PersonService personService;
private RetryingTransactionHelper transactionHelper; private RetryingTransactionHelper transactionHelper;
private List<String> filters = new ArrayList<String>();
@Override protected void setUp() throws Exception @Override protected void setUp() throws Exception
{ {
@@ -96,6 +102,16 @@ public class FacetRestApiTest extends BaseWebScriptTest
{ {
super.tearDown(); super.tearDown();
AuthenticationUtil.runAs(new RunAsWork<Void>()
{
@Override
public Void doWork() throws Exception
{
deleteFilters();
return null;
}
}, SEARCH_ADMIN_USER);
AuthenticationUtil.runAsSystem(new RunAsWork<Void>() AuthenticationUtil.runAsSystem(new RunAsWork<Void>()
{ {
@Override public Void doWork() throws Exception @Override public Void doWork() throws Exception
@@ -114,13 +130,74 @@ 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<Void>()
{
@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<Void>()
{
@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<Void>()
{
@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<Void>() AuthenticationUtil.runAs(new RunAsWork<Void>()
{ {
@Override public Void doWork() throws Exception @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<String> filters = getListFromJsonArray(jsonRsp.getJSONArray(FACETS));
assertTrue(filters.size() > 0);
return null; return null;
} }
}, NON_SEARCH_ADMIN_USER); }, NON_SEARCH_ADMIN_USER);
@@ -164,15 +241,15 @@ public class FacetRestApiTest extends BaseWebScriptTest
System.out.println("Received " + facetsArray.length() + " facets"); System.out.println("Received " + facetsArray.length() + " facets");
final List<Pair<Integer, String>> idsIndexes = getIdsIndexes(facetsArray); final List<String> idsIndexes = getListFromJsonArray(facetsArray);
System.out.println(" IDs, indexes = " + idsIndexes); System.out.println(" IDs, indexes = " + idsIndexes);
// Reorder them such that the last facet is moved left one place. // 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); assertTrue("There should be more than 1 built-in facet", facetsArray.length() > 1);
final Pair<Integer, String> lastIndexIdPair = idsIndexes.get(idsIndexes.size() - 1); final String lastIndexId = idsIndexes.get(idsIndexes.size() - 1);
final String url = PUT_FACET_URL_FORMAT.replace("{0}", lastIndexIdPair.getSecond()) final String url = PUT_FACET_URL_FORMAT.replace("{0}", lastIndexId)
.replace("{1}", "-1"); .replace("{1}", "-1");
rsp = sendRequest(new PutRequest(url, "", "application/json"), 200); rsp = sendRequest(new PutRequest(url, "", "application/json"), 200);
@@ -187,30 +264,231 @@ public class FacetRestApiTest extends BaseWebScriptTest
System.out.println("Received " + newfacetsArray.length() + " facets"); System.out.println("Received " + newfacetsArray.length() + " facets");
final List<Pair<Integer, String>> newIdsIndexes = getIdsIndexes(newfacetsArray); final List<String> newIdsIndexes = getListFromJsonArray(newfacetsArray);
System.out.println(" IDs, indexes = " + newIdsIndexes); System.out.println(" IDs, indexes = " + newIdsIndexes);
// Note here that the last Facet JSON object *is* moved one place up the list. // 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, lastIndexId, idsIndexes), newIdsIndexes);
assertEquals(CollectionUtils.moveLeft(1, lastIndexIdPair, idsIndexes),
newIdsIndexes);
return null; return null;
} }
}, SEARCH_ADMIN_USER); }, SEARCH_ADMIN_USER);
} }
private List<Pair<Integer, String>> getIdsIndexes(JSONArray facetsArray) throws JSONException public void testDefaultValues() throws Exception
{ {
List<Pair<Integer, String>> result = new ArrayList<>(); AuthenticationUtil.runAs(new RunAsWork<Void>()
{
@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<String> 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<String> 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<Void>()
{
@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<String> 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<String> 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<Void>()
{
@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<Void>()
{
@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<Void>()
{
@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<String> getListFromJsonArray(JSONArray facetsArray) throws JSONException
{
List<String> result = new ArrayList<>();
for (int i = 0; i < facetsArray.length(); i++) for (int i = 0; i < facetsArray.length(); i++)
{ {
final JSONObject nextFacet = facetsArray.getJSONObject(i); Object object = facetsArray.get(i);
final int nextIndex = nextFacet.getInt("index"); if (object instanceof JSONObject)
{
final JSONObject nextFacet = (JSONObject) object;
final String nextId = nextFacet.getString("filterID"); final String nextId = nextFacet.getString("filterID");
result.add(new Pair<>(nextIndex, nextId)); result.add(nextId);
}
else
{
result.add((String) object);
}
} }
return result; return result;
} }
@@ -242,4 +520,12 @@ public class FacetRestApiTest extends BaseWebScriptTest
personService.deletePerson(userName); personService.deletePerson(userName);
} }
} }
private void deleteFilters() throws IOException
{
for (String filter : filters)
{
sendRequest(new DeleteRequest(GET_FACETS_URL + "/" + filter), 200);
}
}
} }