Added tests and code to cover the following cases: Somebody doesn't have a field array in the payload submitted to the index schema. Somebody submits something other than a JSON array to the "fields" field.
Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/fc312bd8 Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/fc312bd8 Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/fc312bd8 Branch: refs/heads/release-2.1.1 Commit: fc312bd8c168f395fe7a2982ef4c42c334fcfd13 Parents: 8beb3a5 Author: George Reyes <[email protected]> Authored: Fri Apr 1 12:28:31 2016 -0700 Committer: George Reyes <[email protected]> Committed: Fri Apr 1 12:28:31 2016 -0700 ---------------------------------------------------------------------- .../index/impl/EntityToMapConverter.java | 5 ++- .../rest/applications/CollectionResource.java | 16 ++++++++ .../collection/CollectionsResourceIT.java | 40 +++++++++++++++++++- 3 files changed, 58 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/usergrid/blob/fc312bd8/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EntityToMapConverter.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EntityToMapConverter.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EntityToMapConverter.java index a331c38..98a8ed5 100644 --- a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EntityToMapConverter.java +++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EntityToMapConverter.java @@ -123,8 +123,9 @@ public class EntityToMapConverter { EntityField testedField = ( EntityField ) outputEntityField; String fieldName = ( String ) ( testedField ).get( "name" ); + //could move this down into the method below if ( !defaultProperties.contains( fieldName ) ) { - iterateThroughMapForFieldsToBeIndexed( defaultProperties, collectionIterator, fieldName ); + iterateThroughMapForFieldsToBeIndexed( defaultProperties, collectionIterator, fieldName ); } }); @@ -142,7 +143,7 @@ public class EntityToMapConverter { * the map. * * @param fieldsToKeep - contains a list of fields that the user defined in their schema. - * @param collectionIterator - contains the iterator with the reference to the map where we want to remove the field. + * @param collectionIterator - contains the iterator with the reference to the map where we want to remove the field. Once removed here it is removed from the entity so it won't be indexed. * @param fieldName - contains the name of the field that we want to keep. */ private static void iterateThroughMapForFieldsToBeIndexed( final Set<String> fieldsToKeep, http://git-wip-us.apache.org/repos/asf/usergrid/blob/fc312bd8/stack/rest/src/main/java/org/apache/usergrid/rest/applications/CollectionResource.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/applications/CollectionResource.java b/stack/rest/src/main/java/org/apache/usergrid/rest/applications/CollectionResource.java index c124f9d..5bd895c 100644 --- a/stack/rest/src/main/java/org/apache/usergrid/rest/applications/CollectionResource.java +++ b/stack/rest/src/main/java/org/apache/usergrid/rest/applications/CollectionResource.java @@ -18,6 +18,8 @@ package org.apache.usergrid.rest.applications; +import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.UUID; import java.util.concurrent.TimeUnit; @@ -33,21 +35,25 @@ import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.PathSegment; +import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; +import org.apache.commons.lang.NullArgumentException; import org.apache.commons.lang.StringUtils; import org.apache.usergrid.corepersistence.index.ReIndexRequestBuilder; import org.apache.usergrid.corepersistence.index.ReIndexRequestBuilderImpl; import org.apache.usergrid.corepersistence.index.ReIndexService; import org.apache.usergrid.persistence.Query; +import org.apache.usergrid.persistence.exceptions.RequiredPropertyNotFoundException; import org.apache.usergrid.persistence.index.utils.UUIDUtils; import org.apache.usergrid.rest.AbstractContextResource; import org.apache.usergrid.rest.ApiResponse; import org.apache.usergrid.rest.RootResource; +import org.apache.usergrid.rest.exceptions.RequiredPropertyNotFoundExceptionMapper; import org.apache.usergrid.rest.security.annotations.RequireApplicationAccess; import org.apache.usergrid.rest.security.annotations.RequireSystemAccess; import org.apache.usergrid.rest.system.IndexResource; @@ -56,6 +62,7 @@ import org.apache.usergrid.services.ServiceAction; import org.apache.usergrid.services.ServiceParameter; import org.apache.usergrid.services.ServicePayload; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.jaxrs.json.annotation.JSONP; @@ -114,6 +121,15 @@ public class CollectionResource extends ServiceResource { ServicePayload payload = getPayload( json ); + if(payload.getProperty( "fields" )==null){ + throw new NullArgumentException( "fields" ); + } + + if(! (payload.getProperty( "fields" ) instanceof ArrayList)){ + throw new NullArgumentException( "fields must be of json array type" ); + } + + executeServicePostRequestForSchema( ui,response, ServiceAction.POST,payload ); return response; http://git-wip-us.apache.org/repos/asf/usergrid/blob/fc312bd8/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/CollectionsResourceIT.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/CollectionsResourceIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/CollectionsResourceIT.java index d814f55..e0b75ba 100644 --- a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/CollectionsResourceIT.java +++ b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/CollectionsResourceIT.java @@ -35,6 +35,7 @@ import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.ws.rs.BadRequestException; import javax.ws.rs.ClientErrorException; import java.io.IOException; import java.util.HashMap; @@ -43,6 +44,8 @@ import java.util.Map; import java.util.Set; import java.util.ArrayList; +import org.apache.commons.lang.NullArgumentException; + import static org.junit.Assert.*; @@ -195,8 +198,43 @@ public class CollectionsResourceIT extends AbstractRestIT { } @Test - public void putCollectionSchema() throws Exception { + public void verifyThatFieldsIsRequiredForCollectionSchema() throws Exception { + ArrayList<String> indexingArray = new ArrayList<>( ); + + //field "fields" is required. + Entity payload = new Entity(); + payload.put( "fieldWeirdnessNotFields", indexingArray); + + //Post index to the collection metadata + try { + this.app().collection( "testCollections" ).collection( "_indexes" ).post( payload ); + fail(); + }catch(BadRequestException bre){ + //this is expected. + } + + //ensure that it has to be an arraylist passed in. + Map indexingMap = new HashMap<>( ); + indexingMap.put( "exludeStuff","randomtext" ); + payload = new Entity(); + payload.put( "fields", indexingMap); + + try { + this.app().collection( "testCollections" ).collection( "_indexes" ).post( payload ); + fail(); + }catch(BadRequestException bre){ + //this is expected. + } + + payload = new Entity(); + payload.put( "fields", indexingArray); + + try { + this.app().collection( "testCollections" ).collection( "_indexes" ).post( payload ); + }catch(BadRequestException bre){ + fail( "This shouldn't fail" ); + } }
