usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From toddn...@apache.org
Subject [28/50] incubator-usergrid git commit: Lookup applications using EntityCollectionManager.getIdField() instead of using query.
Date Mon, 13 Apr 2015 17:45:53 GMT
Lookup applications using EntityCollectionManager.getIdField() instead of using query.


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

Branch: refs/heads/USERGRID-536
Commit: 06bcad625382f64166d43b77e234f2835ba422d0
Parents: 3be95f9
Author: Dave Johnson <dmjohnson@apigee.com>
Authored: Tue Apr 7 09:37:14 2015 -0400
Committer: Dave Johnson <dmjohnson@apigee.com>
Committed: Tue Apr 7 09:37:14 2015 -0400

----------------------------------------------------------------------
 .../corepersistence/ApplicationIdCacheImpl.java |  59 ++++----
 .../cassandra/EntityManagerFactoryImplIT.java   |  17 +++
 .../applications/ApplicationCreateTest.java     | 135 +++++++++++++++++++
 .../applications/ApplicationDeleteTest.java     |  34 ++---
 4 files changed, 200 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/06bcad62/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheImpl.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheImpl.java
b/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheImpl.java
index 379f306..5f96b82 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheImpl.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheImpl.java
@@ -20,20 +20,27 @@
 package org.apache.usergrid.corepersistence;
 
 
-import java.util.UUID;
-import java.util.concurrent.ExecutionException;
-
-import org.apache.usergrid.corepersistence.util.CpNamingUtils;
-import org.apache.usergrid.persistence.*;
-
-import com.google.common.base.Optional;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
-import org.apache.usergrid.persistence.index.query.Query;
+import org.apache.usergrid.corepersistence.util.CpNamingUtils;
+import org.apache.usergrid.persistence.EntityManager;
+import org.apache.usergrid.persistence.EntityManagerFactory;
+import org.apache.usergrid.persistence.Schema;
+import org.apache.usergrid.persistence.collection.EntityCollectionManager;
+import org.apache.usergrid.persistence.core.scope.ApplicationScopeImpl;
+import org.apache.usergrid.persistence.model.entity.Entity;
+import org.apache.usergrid.persistence.model.entity.Id;
+import org.apache.usergrid.persistence.model.entity.SimpleId;
+import org.apache.usergrid.persistence.model.field.StringField;
 import org.apache.usergrid.utils.UUIDUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import rx.Observable;
+
+import java.util.UUID;
+
+import static org.apache.usergrid.persistence.Schema.PROPERTY_APPLICATION_ID;
 
 
 /**
@@ -48,6 +55,7 @@ public class ApplicationIdCacheImpl implements ApplicationIdCache {
      * Cache the pointer to our root entity manager for reference
      */
     private final EntityManager rootEm;
+    private final CpEntityManagerFactory emf;
 
     private final LoadingCache<String, UUID> appCache =
         CacheBuilder.newBuilder().maximumSize( 10000 ).build( new CacheLoader<String,
UUID>() {
@@ -59,6 +67,7 @@ public class ApplicationIdCacheImpl implements ApplicationIdCache {
 
 
     public ApplicationIdCacheImpl(final EntityManagerFactory emf) {
+        this.emf = (CpEntityManagerFactory)emf;
         this.rootEm = emf.getEntityManager( emf.getManagementAppId());
     }
 
@@ -68,8 +77,7 @@ public class ApplicationIdCacheImpl implements ApplicationIdCache {
             UUID optionalUuid = appCache.get( applicationName.toLowerCase() );
             logger.debug("Returning for key {} value {}", applicationName, optionalUuid );
             return optionalUuid;
-        }
-        catch ( Exception e ) {
+        } catch (Exception e) {
             logger.debug("Returning for key {} value null", applicationName );
             return null;
         }
@@ -81,37 +89,30 @@ public class ApplicationIdCacheImpl implements ApplicationIdCache {
      */
     private UUID fetchApplicationId( final String applicationName ) {
 
-        UUID value = null;
+        final UUID value;
+
+        EntityCollectionManager ecm = emf.getManagerCache().getEntityCollectionManager(
+            new ApplicationScopeImpl(
+                new SimpleId( CpNamingUtils.MANAGEMENT_APPLICATION_ID, Schema.TYPE_APPLICATION
) ) );
 
         try {
             if ( rootEm.getApplication() == null ) {
                 return null;
             }
         } catch ( Exception e ) {
-            logger.error("Error looking up app", e);
+            logger.error("Error looking up management app", e);
         }
 
         try {
-            Query q = Query.fromQL( Schema.PROPERTY_NAME + " = '" + applicationName.toLowerCase()
+ "'" );
-
-            Results results = rootEm.searchCollection(
-                rootEm.getApplicationRef(), CpNamingUtils.APPLICATION_INFOS, q);
 
-            if ( !results.isEmpty() ) {
+            // look up application_info ID for application using unique "name" field
+            final Observable<Id> idObs = ecm.getIdField(
+                CpNamingUtils.APPLICATION_INFO, new StringField(Schema.PROPERTY_NAME, applicationName));
 
-                Entity entity = results.iterator().next();
-                Object uuidObject = entity.getProperty(Schema.PROPERTY_APPLICATION_ID);
-
-                if (uuidObject instanceof UUID) {
-                    value = (UUID) uuidObject;
-                } else {
-                    value = UUIDUtils.tryExtractUUID(
-                        entity.getProperty(Schema.PROPERTY_APPLICATION_ID).toString());
-                }
-
-            }
+            Id id = idObs.toBlocking().lastOrDefault(null);
+            value = id.getUuid();
 
-            logger.debug("Loaded    for key {} value {}", applicationName, value );
+            logger.debug("Loaded for key {} value {}", applicationName, value );
             return value;
         }
         catch ( Exception e ) {

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/06bcad62/stack/core/src/test/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImplIT.java
----------------------------------------------------------------------
diff --git a/stack/core/src/test/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImplIT.java
b/stack/core/src/test/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImplIT.java
index 33a59e1..d3f2333 100644
--- a/stack/core/src/test/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImplIT.java
+++ b/stack/core/src/test/java/org/apache/usergrid/persistence/cassandra/EntityManagerFactoryImplIT.java
@@ -320,4 +320,21 @@ public class EntityManagerFactoryImplIT extends AbstractCoreIT {
 		 */
         traceTagReporter.report( traceTagManager.detach() );
     }
+
+
+    @Test
+    public void testCreateAndImmediateGet() throws Exception {
+
+        String random = RandomStringUtils.randomAlphabetic(10);
+        String orgName = "org_" + random;
+        String appName = "app_" + random;
+        String orgAppName = orgName + "/" + appName;
+
+        UUID appId = setup.createApplication(orgName, appName);
+
+        UUID lookedUpId = setup.getEmf().lookupApplication( orgAppName );
+
+        Assert.assertEquals(appId, lookedUpId);
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/06bcad62/stack/rest/src/test/java/org/apache/usergrid/rest/applications/ApplicationCreateTest.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/ApplicationCreateTest.java
b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/ApplicationCreateTest.java
new file mode 100644
index 0000000..ff1b7a8
--- /dev/null
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/ApplicationCreateTest.java
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.usergrid.rest.applications;
+
+
+import org.apache.commons.lang3.RandomStringUtils;
+import org.apache.usergrid.rest.test.resource2point0.AbstractRestIT;
+import org.apache.usergrid.rest.test.resource2point0.endpoints.mgmt.ManagementResponse;
+import org.apache.usergrid.rest.test.resource2point0.model.ApiResponse;
+import org.apache.usergrid.rest.test.resource2point0.model.Application;
+import org.apache.usergrid.rest.test.resource2point0.model.Entity;
+import org.apache.usergrid.rest.test.resource2point0.model.Token;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.core.MediaType;
+import java.util.*;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+
+public class ApplicationCreateTest extends AbstractRestIT {
+    private static final Logger logger = LoggerFactory.getLogger(ApplicationCreateTest.class);
+
+
+    /**
+     * Test that we can create and then immediately retrieve an app by name.
+     * https://issues.apache.org/jira/browse/USERGRID-491
+     */
+    @Test
+    public void testCreateAndImmediateGet() throws Exception {
+
+        // create app
+
+        String orgName = clientSetup.getOrganization().getName();
+        String appName = clientSetup.getAppName() + "_new_app";
+        Token orgAdminToken = getAdminToken(clientSetup.getUsername(), clientSetup.getUsername());
+
+        ApiResponse appCreateResponse = clientSetup.getRestClient()
+            .management().orgs().organization(orgName).app().getResource()
+            .queryParam("access_token", orgAdminToken.getAccessToken())
+            .type( MediaType.APPLICATION_JSON )
+            .post(ApiResponse.class, new Application(appName));
+        appCreateResponse.getEntities().get(0).getUuid();
+
+        // should be able to immediately get the application's roles collection
+
+        ApiResponse response = clientSetup.getRestClient().getResource()
+            .path("/" + clientSetup.getOrganizationName() + "/" + appName + "/roles" )
+            .queryParam( "access_token", orgAdminToken.getAccessToken() )
+            .get(ApiResponse.class);
+        assertTrue( !response.getEntities().isEmpty() );
+    }
+
+
+    /**
+     * Test that we can create applications and the immediately retrieve them all.
+     */
+    @Test
+    public void testCreateAndImmediateList() throws Exception {
+
+        int appCount = 40;
+
+        String random = RandomStringUtils.randomAlphabetic(10);
+        String orgName = "org_" + random;
+        String appName = "app_" + random;
+        Token orgAdminToken = getAdminToken(clientSetup.getUsername(), clientSetup.getUsername());
+
+        for ( int i=0; i<appCount; i++ ) {
+           createAppWithCollection( orgName, appName + i, orgAdminToken, new ArrayList<>()
);
+        }
+
+        // test that we get all applications back from the management end-point
+
+        ManagementResponse orgAppResponse = clientSetup.getRestClient()
+            .management().orgs().organization( orgName ).apps().getOrganizationApplications();
+
+        int count = 0;
+        for ( String name : orgAppResponse.getData().keySet() ) {
+            count++;
+        }
+        assertEquals( appCount, count );
+    }
+
+
+    private UUID createAppWithCollection(
+        String orgName, String appName, Token orgAdminToken, List<Entity> entities)
{
+
+        ApiResponse appCreateResponse = clientSetup.getRestClient()
+            .management().orgs().organization( orgName ).app().getResource()
+            .queryParam( "access_token", orgAdminToken.getAccessToken() )
+            .type( MediaType.APPLICATION_JSON )
+            .post( ApiResponse.class, new Application( appName ) );
+        UUID appId = appCreateResponse.getEntities().get(0).getUuid();
+
+        try { Thread.sleep(1000); } catch (InterruptedException ignored ) { }
+
+        for ( int i=0; i<5; i++ ) {
+
+            final String entityName = "entity" + i;
+            Entity entity = new Entity();
+            entity.setProperties(new HashMap<String, Object>() {{
+                put("name", entityName );
+            }});
+
+            ApiResponse createResponse = clientSetup.getRestClient()
+                .org(orgName).app( appName ).collection("things").getResource()
+                .queryParam("access_token", orgAdminToken.getAccessToken())
+                .type(MediaType.APPLICATION_JSON)
+                .post( ApiResponse.class, entity );
+
+            entities.add( createResponse.getEntities().get(0) );
+        }
+        return appId;
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/06bcad62/stack/rest/src/test/java/org/apache/usergrid/rest/applications/ApplicationDeleteTest.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/ApplicationDeleteTest.java
b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/ApplicationDeleteTest.java
index 0eaf566..6971fe4 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/ApplicationDeleteTest.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/ApplicationDeleteTest.java
@@ -20,9 +20,6 @@ package org.apache.usergrid.rest.applications;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.sun.jersey.api.client.UniformInterfaceException;
-import org.apache.usergrid.corepersistence.ApplicationIdCacheImpl;
-import org.apache.usergrid.corepersistence.util.CpNamingUtils;
-import org.apache.usergrid.persistence.EntityManagerFactory;
 import org.apache.usergrid.rest.test.resource2point0.AbstractRestIT;
 import org.apache.usergrid.rest.test.resource2point0.endpoints.mgmt.ManagementResponse;
 import org.apache.usergrid.rest.test.resource2point0.model.*;
@@ -31,16 +28,17 @@ import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import javax.ws.rs.core.MediaType;
-import java.io.StringReader;
 import java.util.*;
-import java.util.regex.Pattern;
 
 import static org.junit.Assert.fail;
 
 
-public class ApplicationDeleteTest  extends AbstractRestIT {
+public class ApplicationDeleteTest extends AbstractRestIT {
+
     private static final Logger logger = LoggerFactory.getLogger(ApplicationDeleteTest.class);
 
+    public static final int INDEXING_WAIT = 3000;
+
 
     /**
      * Test most common use cases.
@@ -180,15 +178,15 @@ public class ApplicationDeleteTest  extends AbstractRestIT {
     }
 
 
-   /**
-    * Test restore of deleted app.
-    * <pre>
-    *  - create app with collection of things
-    *  - delete the app
-    *  - restore the app
-    *  - test that we can get the app, its collections and an entity
-    * </pre>
-    */
+    /**
+     * Test restore of deleted app.
+     * <pre>
+     *  - create app with collection of things
+     *  - delete the app
+     *  - restore the app
+     *  - test that we can get the app, its collections and an entity
+     * </pre>
+     */
     @Test
     public void testAppRestore() throws Exception {
 
@@ -212,7 +210,7 @@ public class ApplicationDeleteTest  extends AbstractRestIT {
             .queryParam("app_delete_confirm", "confirm_delete_of_application_and_data")
             .delete();
 
-        Thread.sleep(1000);
+        Thread.sleep(INDEXING_WAIT);
 
         // restore the app
 
@@ -223,6 +221,8 @@ public class ApplicationDeleteTest  extends AbstractRestIT {
             .queryParam("access_token", orgAdminToken.getAccessToken() )
             .put();
 
+        Thread.sleep(INDEXING_WAIT);
+
         // test that we can see the application in the list of applications
 
         logger.debug("\n\nGetting app list from management end-point\n");
@@ -365,6 +365,8 @@ public class ApplicationDeleteTest  extends AbstractRestIT {
             .post( ApiResponse.class, new Application( appName ) );
         UUID appId = appCreateResponse.getEntities().get(0).getUuid();
 
+        try { Thread.sleep(INDEXING_WAIT); } catch (InterruptedException ignored ) { }
+
         for ( int i=0; i<10; i++ ) {
 
             final String entityName = "entity" + i;


Mime
View raw message