usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mru...@apache.org
Subject [2/6] usergrid git commit: Added functionality to delete by name and added a test to verify.
Date Thu, 04 Feb 2016 00:27:08 GMT
Added functionality to delete by name and added a test to verify.


Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/ac0652f3
Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/ac0652f3
Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/ac0652f3

Branch: refs/heads/1.x
Commit: ac0652f3493725fc5ce863db5cff922b17df22b8
Parents: dd5b324
Author: George Reyes <grey@apache.org>
Authored: Fri Jan 29 15:45:53 2016 -0800
Committer: George Reyes <grey@apache.org>
Committed: Fri Jan 29 15:45:53 2016 -0800

----------------------------------------------------------------------
 .../users/CollectionsResourceIT.java            | 90 +++++++++++++++-----
 .../services/AbstractCollectionService.java     | 42 +++++++--
 2 files changed, 104 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/usergrid/blob/ac0652f3/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/CollectionsResourceIT.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/CollectionsResourceIT.java
b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/CollectionsResourceIT.java
index a88334d..82d6d7f 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/CollectionsResourceIT.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/CollectionsResourceIT.java
@@ -39,6 +39,8 @@ import com.sun.jersey.api.client.UniformInterfaceException;
 import me.prettyprint.hector.api.Keyspace;
 import me.prettyprint.hector.api.mutation.MutationResult;
 import me.prettyprint.hector.api.mutation.Mutator;
+import static org.hamcrest.CoreMatchers.*;
+
 
 import static org.apache.usergrid.persistence.cassandra.ApplicationCF.ENTITY_PROPERTIES;
 import static org.apache.usergrid.persistence.cassandra.CassandraPersistenceUtils.addDeleteToMutator;
@@ -50,6 +52,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.apache.usergrid.utils.MapUtils.hashMap;
+import static org.junit.Assert.assertThat;
 
 
 /**
@@ -210,21 +213,23 @@ public class CollectionsResourceIT extends AbstractRestIT {
         }
     }
 
+
     @Test
-    public void testUpdatingRandomEntity() throws Exception {
+    public void testUpdatingMissingEntityByName() throws Exception {
 
         UUID entityId = null;
 
-            // create an "app_user" object with name fred
-            Map<String, String> payload = hashMap( "type", "app_user" ).map( "name",
"fred" );
+        // create an "app_user" object with name fred
+        Map<String, String> payload = hashMap( "type", "app_user" ).map( "name", "mokey"
);
 
-            JsonNode node = resource().path( "/test-organization/test-app/app_users" )
-                                      .queryParam( "access_token", access_token ).accept(
MediaType.APPLICATION_JSON )
-                                      .type( MediaType.APPLICATION_JSON_TYPE ).post( JsonNode.class,
payload );
+        JsonNode node =
+                resource().path( "/test-organization/test-app/app_users" ).queryParam( "access_token",
access_token )
+                          .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE
)
+                          .post( JsonNode.class, payload );
 
-            String uuidString = node.get( "entities" ).get( 0 ).get( "uuid" ).asText();
-            entityId = UUIDUtils.tryGetUUID( uuidString );
-        UUID applicationId = UUIDUtils.tryGetUUID( node.get( "application").asText());
+        String uuidString = node.get( "entities" ).get( 0 ).get( "uuid" ).asText();
+        entityId = UUIDUtils.tryGetUUID( uuidString );
+        UUID applicationId = UUIDUtils.tryGetUUID( node.get( "application" ).asText() );
 
 
         UUID timestampUuid = newTimeUUID();
@@ -234,27 +239,74 @@ public class CollectionsResourceIT extends AbstractRestIT {
         Keyspace ko = setup.getCassSvc().getApplicationKeyspace( applicationId );
 
         Mutator<ByteBuffer> m = CountingMutator.createFlushingMutator( ko, be );
-        addDeleteToMutator( m, ENTITY_PROPERTIES, key( entityId ), timestamp);
+        addDeleteToMutator( m, ENTITY_PROPERTIES, key( entityId ), timestamp );
 
         m.execute();
 
-        payload = hashMap( "name", "ted" );
+        payload = hashMap( "name", "mokey" );
 
 
         // check REST API response for duplicate name property
-            // have to look at raw response data, Jackson will remove dups
-            node = resource().path( "/test-organization/test-app/app_users/fred") //+entityId
)
-                                 .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON
)
-                                 .type( MediaType.APPLICATION_JSON_TYPE ).put(JsonNode.class,
payload );
+        // have to look at raw response data, Jackson will remove dups
+        node = resource().path( "/test-organization/test-app/app_users/mokey" ) //+entityId
)
+                         .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON
)
+                         .type( MediaType.APPLICATION_JSON_TYPE ).put( JsonNode.class, payload
);
 
-            assertNotNull(node);
+        assertNotNull( node );
 
 
-        node = resource().path( "/test-organization/test-app/app_users/fred")//+entityId
)
+        node = resource().path( "/test-organization/test-app/app_users/mokey" )//+entityId
)
                          .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON
)
-                         .type( MediaType.APPLICATION_JSON_TYPE ).get(JsonNode.class );
+                         .type( MediaType.APPLICATION_JSON_TYPE ).get( JsonNode.class );
+
+        assertNotNull( node );
+    }
+
+
+    @Test
+    public void testDeleteByNameOfMissingEntityAndRecreation() throws Exception {
+
+        UUID entityId = null;
+
+        // create an "app_user" object with name fred
+        Map<String, String> payload = hashMap( "type", "app_user" ).map( "name", "rock"
);
+
+        JsonNode node =
+                resource().path( "/test-organization/test-app/app_users" ).queryParam( "access_token",
access_token )
+                          .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE
)
+                          .post( JsonNode.class, payload );
+
+        String uuidString = node.get( "entities" ).get( 0 ).get( "uuid" ).asText();
+        entityId = UUIDUtils.tryGetUUID( uuidString );
+        UUID applicationId = UUIDUtils.tryGetUUID( node.get( "application" ).asText() );
+
+
+        UUID timestampUuid = newTimeUUID();
+        long timestamp = getTimestampInMicros( timestampUuid );
+
+        //Delete only the entity properties out of the filesystem.
+        Keyspace ko = setup.getCassSvc().getApplicationKeyspace( applicationId );
+
+        Mutator<ByteBuffer> m = CountingMutator.createFlushingMutator( ko, be );
+        addDeleteToMutator( m, ENTITY_PROPERTIES, key( entityId ), timestamp );
+
+        m.execute();
+
+        //delete the incomplete entity
+        node = resource().path( "/test-organization/test-app/app_users/rock" )
+                         .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON
)
+                         .type( MediaType.APPLICATION_JSON_TYPE ).delete( JsonNode.class
);
+
+        assertNotNull( node );
 
-        assertNotNull(node);
+        //try recreating an entity with the exact same payload and see if it can be recreated
with a different uuid.
+        node = resource().path( "/test-organization/test-app/app_users" ).queryParam( "access_token",
access_token )
+                         .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE
)
+                         .post( JsonNode.class, payload );
 
+        assertNotNull( node );
+        uuidString = node.get( "entities" ).get( 0 ).get( "uuid" ).asText();
+        assertThat( entityId, not( UUIDUtils.tryGetUUID( uuidString ) ) );
+        assertThat( "rock", equalTo( node.get( "entities" ).get( 0 ).get( "name" ).asText()
) );
     }
 }

http://git-wip-us.apache.org/repos/asf/usergrid/blob/ac0652f3/stack/services/src/main/java/org/apache/usergrid/services/AbstractCollectionService.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/services/AbstractCollectionService.java
b/stack/services/src/main/java/org/apache/usergrid/services/AbstractCollectionService.java
index b45b528..38295c8 100644
--- a/stack/services/src/main/java/org/apache/usergrid/services/AbstractCollectionService.java
+++ b/stack/services/src/main/java/org/apache/usergrid/services/AbstractCollectionService.java
@@ -17,7 +17,9 @@
 package org.apache.usergrid.services;
 
 
+import java.nio.ByteBuffer;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -35,7 +37,19 @@ import org.apache.usergrid.persistence.exceptions.UnexpectedEntityTypeException;
 import org.apache.usergrid.services.ServiceResults.Type;
 import org.apache.usergrid.services.exceptions.ForbiddenServiceOperationException;
 import org.apache.usergrid.services.exceptions.ServiceResourceNotFoundException;
+import org.apache.usergrid.utils.MapUtils;
+import org.apache.usergrid.utils.UUIDUtils;
 
+import com.fasterxml.uuid.impl.UUIDUtil;
+
+import me.prettyprint.cassandra.service.RangeSlicesIterator;
+import me.prettyprint.hector.api.beans.ColumnSlice;
+import me.prettyprint.hector.api.factory.HFactory;
+import me.prettyprint.hector.api.query.ColumnQuery;
+import me.prettyprint.hector.api.query.RangeSlicesQuery;
+
+import static org.apache.usergrid.persistence.cassandra.ApplicationCF.ENTITY_UNIQUE;
+import static org.apache.usergrid.persistence.cassandra.Serializers.be;
 import static org.apache.usergrid.utils.ClassUtils.cast;
 
 
@@ -289,7 +303,7 @@ public class AbstractCollectionService extends AbstractService {
     public ServiceResults putItemByName( ServiceContext context, String name ) throws Exception
{
 
         EntityRef ref = em.getAlias( getEntityType(), name );
-        return putItemById(context ,ref.getUuid() );
+        return putItemById( context, ref.getUuid() );
     }
 
 
@@ -447,6 +461,9 @@ public class AbstractCollectionService extends AbstractService {
 
         Entity item = em.get( id );
         if ( item == null ) {
+            logger.error(
+                    "Found entity reference but not entity properties when trying to delete
uuid: {}.Please delete by"
+                            + " Name.", id );
             throw new ServiceResourceNotFoundException( context );
         }
 
@@ -454,8 +471,6 @@ public class AbstractCollectionService extends AbstractService {
 
         item = importEntity( context, item );
 
-        prepareToDelete( context, item );
-
         em.removeFromCollection( context.getOwner(), context.getCollectionName(), item );
 
         return new ServiceResults( this, context, Type.COLLECTION, Results.fromEntity( item
), null, null );
@@ -468,20 +483,29 @@ public class AbstractCollectionService extends AbstractService {
         if ( context.moreParameters() ) {
             return getItemByName( context, name );
         }
-
         EntityRef ref = em.getAlias( getEntityType(), name );
+
         if ( ref == null ) {
             throw new ServiceResourceNotFoundException( context );
         }
+
+        checkPermissionsForEntity( context, ref );
+
         Entity entity = em.get( ref );
         if ( entity == null ) {
-            throw new ServiceResourceNotFoundException( context );
-        }
-        entity = importEntity( context, entity );
+            logger.error(
+                    "Found entity reference but not entity properties when trying to delete.
Cleaning up uuid: {}",
+                    ref.getUuid() );
+            String entityType = getEntityType();
+            Map<String, Object> properties = new HashMap<String, Object>();
+            properties.put( "name", name );
+            em.create( ref.getUuid(), entityType, properties );
 
-        checkPermissionsForEntity( context, entity );
+            em.removeFromCollection( context.getOwner(), context.getCollectionName(), ref
);
+            return new ServiceResults( this, context, Type.COLLECTION, Results.fromRef( ref
), null, null );
+        }
 
-        prepareToDelete( context, entity );
+        entity = importEntity( context, entity );
 
         em.removeFromCollection( context.getOwner(), context.getCollectionName(), entity
);
 


Mime
View raw message