usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sfeld...@apache.org
Subject incubator-usergrid git commit: isolating app info from old app infos, so we can do in place migration
Date Wed, 01 Jul 2015 00:19:48 GMT
Repository: incubator-usergrid
Updated Branches:
  refs/heads/isolate-app-infos [created] d1c19434c


isolating app info from old app infos, so we can do in place migration


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

Branch: refs/heads/isolate-app-infos
Commit: d1c19434cf821c67b8fef7d0d205e78574f8e6ac
Parents: 6b829ca
Author: Shawn Feldman <sfeldman@apache.org>
Authored: Tue Jun 30 18:19:33 2015 -0600
Committer: Shawn Feldman <sfeldman@apache.org>
Committed: Tue Jun 30 18:19:33 2015 -0600

----------------------------------------------------------------------
 .../usergrid/corepersistence/CoreModule.java    |   2 -
 .../corepersistence/CpEntityManagerFactory.java |   9 +-
 .../migration/AppInfoMigrationPlugin.java       | 297 ------------------
 .../corepersistence/util/CpNamingUtils.java     |   8 +-
 .../persistence/EntityManagerFactory.java       |   2 +-
 .../apache/usergrid/persistence/CounterIT.java  |   2 -
 .../management/AppInfoMigrationPlugin.java      | 306 +++++++++++++++++++
 .../usergrid/management/ManagementService.java  |   2 +-
 .../cassandra/ManagementServiceImpl.java        |  16 +-
 .../usergrid/services/guice/ServiceModule.java  |   5 +
 .../org/apache/usergrid/ServiceITSetup.java     |   3 +-
 .../org/apache/usergrid/ServiceITSetupImpl.java |   7 +-
 .../migration/AppInfoMigrationPluginTest.java   |  42 +--
 13 files changed, 348 insertions(+), 353 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/d1c19434/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java
index f419d5a..dd0e1ab 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java
@@ -23,7 +23,6 @@ import org.apache.usergrid.corepersistence.asyncevents.AsyncEventService;
 import org.apache.usergrid.corepersistence.asyncevents.AsyncIndexProvider;
 import org.apache.usergrid.corepersistence.asyncevents.EventBuilder;
 import org.apache.usergrid.corepersistence.asyncevents.EventBuilderImpl;
-import org.apache.usergrid.corepersistence.migration.AppInfoMigrationPlugin;
 import org.apache.usergrid.corepersistence.migration.CoreMigration;
 import org.apache.usergrid.corepersistence.migration.CoreMigrationPlugin;
 import org.apache.usergrid.corepersistence.migration.EntityTypeMappingMigration;
@@ -119,7 +118,6 @@ public class CoreModule  extends AbstractModule {
         //wire up the collection migration plugin
         final Multibinder<MigrationPlugin> plugins = Multibinder.newSetBinder( binder(), MigrationPlugin.class );
         plugins.addBinding().to( CoreMigrationPlugin.class );
-        plugins.addBinding().to( AppInfoMigrationPlugin.class );
         plugins.addBinding().to( MigrationModuleVersionPlugin.class );
 
         bind( AllApplicationsObservable.class ).to( AllApplicationsObservableImpl.class );

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/d1c19434/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java
index e3c17f0..a962def 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java
@@ -200,13 +200,13 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
 
     @Override
     public Entity createApplicationV2(String organizationName, String name) throws Exception {
-        return createApplicationV2(organizationName, name, null);
+        return createApplicationV2(organizationName, name, null, null);
     }
 
 
     @Override
     public Entity createApplicationV2(
-        String orgName, String name, Map<String, Object> properties) throws Exception {
+        String orgName, String name, UUID applicationId, Map<String, Object> properties) throws Exception {
 
         String appName = buildAppName( orgName, name );
 
@@ -217,7 +217,7 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
             throw new ApplicationAlreadyExistsException( name );
         }
 
-        UUID applicationId = UUIDGenerator.newTimeUUID();
+        applicationId = applicationId==null ?  UUIDGenerator.newTimeUUID() : applicationId;
 
         logger.debug( "New application orgName {} orgAppName {} id {} ",
             new Object[] { orgName, name, applicationId.toString() } );
@@ -267,7 +267,6 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
 
         Map<String, Object> appInfoMap = new HashMap<String, Object>() {{
             put( PROPERTY_NAME, appName );
-            put( PROPERTY_APPLICATION_ID, applicationId );
         }};
 
         Entity appInfo;
@@ -493,7 +492,7 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application
                             final org.apache.usergrid.persistence.model.entity.Entity entityData =
                                 entity.getEntity().get();
 
-                            final UUID applicationId = ( UUID ) entityData.getField( PROPERTY_APPLICATION_ID ).getValue();
+                            final UUID applicationId = entity.getId().getUuid();
                             final String applicationName = ( String ) entityData.getField( PROPERTY_NAME ).getValue();
 
                             appMap.put( applicationName , applicationId );

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/d1c19434/stack/core/src/main/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPlugin.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPlugin.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPlugin.java
deleted file mode 100644
index 21383aa..0000000
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPlugin.java
+++ /dev/null
@@ -1,297 +0,0 @@
-/*
- * 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.corepersistence.migration;
-
-import com.google.common.base.Optional;
-import com.google.inject.Inject;
-import org.apache.usergrid.corepersistence.util.CpEntityMapUtils;
-import org.apache.usergrid.corepersistence.util.CpNamingUtils;
-import org.apache.usergrid.persistence.*;
-import org.apache.usergrid.persistence.Entity;
-import org.apache.usergrid.persistence.collection.EntityCollectionManager;
-import org.apache.usergrid.persistence.collection.EntityCollectionManagerFactory;
-import org.apache.usergrid.persistence.core.migration.data.MigrationInfoSerialization;
-import org.apache.usergrid.persistence.core.migration.data.MigrationPlugin;
-import org.apache.usergrid.persistence.core.migration.data.PluginPhase;
-import org.apache.usergrid.persistence.core.migration.data.ProgressObserver;
-import org.apache.usergrid.persistence.core.scope.ApplicationScope;
-import org.apache.usergrid.persistence.entities.Group;
-import org.apache.usergrid.persistence.graph.Edge;
-import org.apache.usergrid.persistence.graph.GraphManager;
-import org.apache.usergrid.persistence.graph.GraphManagerFactory;
-import org.apache.usergrid.persistence.graph.SearchByEdgeType;
-import org.apache.usergrid.persistence.graph.impl.SimpleSearchByEdge;
-import org.apache.usergrid.persistence.graph.impl.SimpleSearchByEdgeType;
-import org.apache.usergrid.persistence.model.entity.*;
-import org.apache.usergrid.utils.UUIDUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import rx.Observable;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.UUID;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.concurrent.atomic.AtomicLong;
-
-import static org.apache.usergrid.corepersistence.util.CpNamingUtils.getApplicationScope;
-import static org.apache.usergrid.persistence.Schema.*;
-
-
-/**
- * Migration of appinfos collection to application_info collection.
- *
- * Part of USERGRID-448 "Remove redundant appinfos collections in ManagementServiceImpl"
- * https://issues.apache.org/jira/browse/USERGRID-448
- */
-public class AppInfoMigrationPlugin implements MigrationPlugin {
-
-    /** Old and deprecated SYSTEM_APP */
-    public static final UUID SYSTEM_APP_ID = UUID.fromString( "b6768a08-b5d5-11e3-a495-10ddb1de66c3" );
-
-    private static final Logger logger = LoggerFactory.getLogger(AppInfoMigrationPlugin.class);
-
-    public static String PLUGIN_NAME = "appinfo-migration";
-
-    @Inject
-    final private MigrationInfoSerialization migrationInfoSerialization;
-
-    @Inject
-    final private EntityManagerFactory emf;
-
-    @Inject
-    final private EntityCollectionManagerFactory entityCollectionManagerFactory;
-
-    @Inject
-    final private GraphManagerFactory graphManagerFactory;
-
-
-    @Inject
-    public AppInfoMigrationPlugin(
-        EntityManagerFactory emf,
-        MigrationInfoSerialization migrationInfoSerialization,
-        EntityCollectionManagerFactory entityCollectionManagerFactory,
-        GraphManagerFactory graphManagerFactory ) {
-
-        this.emf = emf;
-        this.migrationInfoSerialization = migrationInfoSerialization;
-        this.entityCollectionManagerFactory = entityCollectionManagerFactory;
-        this.graphManagerFactory = graphManagerFactory;
-    }
-
-
-    @Override
-    public String getName() {
-        return PLUGIN_NAME;
-    }
-
-
-    @Override
-    public int getMaxVersion() {
-        return 2; // standalone plugin, happens once
-    }
-
-
-    @Override
-    public PluginPhase getPhase() {
-        return PluginPhase.MIGRATE;
-    }
-
-
-    @Override
-    public void run(ProgressObserver observer) {
-
-        final int version = migrationInfoSerialization.getVersion(getName());
-
-        if (version == getMaxVersion()) {
-            logger.debug("Skipping Migration Plugin: " + getName());
-            return;
-        }
-
-        observer.start();
-        AtomicInteger count = new AtomicInteger();
-        //get old app infos to migrate
-        final Observable<org.apache.usergrid.persistence.model.entity.Entity> oldAppInfos = getOldAppInfos();
-        oldAppInfos
-            .doOnNext(oldAppInfoEntity -> {
-                try {
-                    migrateAppInfo( oldAppInfoEntity, observer);
-                    count.incrementAndGet();
-                }catch (Exception e){
-                    logger.error("Failed to migrate app info"+oldAppInfoEntity.getId().getUuid(),e);
-                    throw new RuntimeException(e);
-                }
-
-            })
-            .doOnCompleted(() -> {
-                if(count.get()>0) {
-                    migrationInfoSerialization.setVersion(getName(), getMaxVersion());
-                }else{
-                    logger.error("Failed to migrate any app infos");
-                }
-                observer.complete();
-            }).toBlocking().lastOrDefault(null);
-
-    }
-
-
-    private void migrateAppInfo( org.apache.usergrid.persistence.model.entity.Entity oldAppInfoEntity, ProgressObserver observer) {
-        // Get appinfos from the Graph, we don't expect many so use iterator
-        final EntityManager managementEm = emf.getManagementEntityManager();
-
-        Map oldAppInfoMap = CpEntityMapUtils.toMap(oldAppInfoEntity);
-
-        final String name = (String) oldAppInfoMap.get(PROPERTY_NAME);
-
-        try {
-            final String orgName = name.split("/")[0];
-            final String appName = name.split("/")[1];
-            UUID applicationId = getUuid(oldAppInfoMap,"applicationUuid") ;
-            applicationId = applicationId == null ?  getUuid(oldAppInfoMap,"appUuid") : applicationId ;
-            //get app info from graph to see if it has been migrated already
-            Entity appInfo = getApplicationInfo(applicationId);
-            if (appInfo == null) {
-                // create and connect new APPLICATION_INFO oldAppInfo to Organization
-                appInfo = createNewAppInfo(managementEm, name, applicationId);
-                observer.update(getMaxVersion(), "Created application_info for " + appName);
-                // create org->app connections, but not for apps in dummy "usergrid" internal organization
-                if (!orgName.equals("usergrid")) {
-                    EntityRef orgRef = managementEm.getAlias(Group.ENTITY_TYPE, orgName);
-                    managementEm.createConnection(orgRef, "owns", appInfo);
-                }
-            } else {
-                //already migrated don't do anything
-                observer.update(getMaxVersion(), "Received existing application_info for " + appName + " don't do anything");
-            }
-
-        } catch (Exception e) {
-            String msg = "Exception writing application_info for " + name;
-            logger.error(msg, e);
-            observer.failed(getMaxVersion(), msg);
-            throw new RuntimeException(e);
-        }
-
-    }
-
-    private UUID getUuid(Map oldAppInfoMap, String key) {
-        UUID applicationId;
-        Object uuidObject = oldAppInfoMap.get(key);
-        if (uuidObject instanceof UUID) {
-            applicationId = (UUID) uuidObject;
-        } else {
-            applicationId = uuidObject == null ? null : UUIDUtils.tryExtractUUID(uuidObject.toString());
-        }
-        return applicationId;
-    }
-
-    private Entity createNewAppInfo(EntityManager managementEm, final String name, final UUID applicationId) throws Exception {
-        Entity appInfo;Map<String, Object> appInfoMap = new HashMap<String, Object>() {{
-            put(PROPERTY_NAME, name);
-            put(PROPERTY_APPLICATION_ID, applicationId);
-        }};
-        appInfo = managementEm.create(new SimpleId(applicationId, CpNamingUtils.APPLICATION_INFO), appInfoMap);
-        return appInfo;
-    }
-
-    /**
-     * TODO: Use Graph to get application_info for an specified Application.
-     */
-    private Entity getApplicationInfo( final UUID appId ) throws Exception {
-
-        final ApplicationScope managementAppScope = getApplicationScope(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
-        final EntityCollectionManager managementCollectionManager = entityCollectionManagerFactory.createCollectionManager(managementAppScope);
-
-        Observable<Edge> edgesObservable = getApplicationInfoEdges(appId);
-        //get the graph for all app infos
-        Observable<org.apache.usergrid.persistence.model.entity.Entity> entityObs =
-                edgesObservable.flatMap(edge -> {
-                    final Id appInfoId = edge.getTargetNode();
-                    return managementCollectionManager
-                        .load(appInfoId)
-                        .filter( entity ->{
-                            //check for app id
-                            return  entity != null ? entity.getId().getUuid().equals(appId) : false;
-                        });
-                });
-
-        // don't expect many applications, so we block
-        org.apache.usergrid.persistence.model.entity.Entity applicationInfo =
-            entityObs.toBlocking().lastOrDefault(null);
-
-        if ( applicationInfo == null ) {
-            return null;
-        }
-
-        Class clazz = Schema.getDefaultSchema().getEntityClass(applicationInfo.getId().getType());
-
-        Entity entity = EntityFactory.newEntity(
-            applicationInfo.getId().getUuid(), applicationInfo.getId().getType(), clazz );
-
-        entity.setProperties( CpEntityMapUtils.toMap( applicationInfo ) );
-
-        return entity;
-
-    }
-
-
-    /**
-     * Use Graph to get old appinfos from the old and deprecated System App.
-     */
-    public Observable<org.apache.usergrid.persistence.model.entity.Entity> getOldAppInfos( ) {
-
-        final ApplicationScope systemAppScope = getApplicationScope(SYSTEM_APP_ID);
-
-        final EntityCollectionManager systemCollectionManager =
-            entityCollectionManagerFactory.createCollectionManager(systemAppScope);
-
-        final GraphManager gm = graphManagerFactory.createEdgeManager(systemAppScope);
-
-        String edgeType = CpNamingUtils.getEdgeTypeFromCollectionName("appinfos");
-
-        Id rootAppId = systemAppScope.getApplication();
-
-        final SimpleSearchByEdgeType simpleSearchByEdgeType =  new SimpleSearchByEdgeType(
-            rootAppId, edgeType, Long.MAX_VALUE, SearchByEdgeType.Order.DESCENDING, Optional.absent());
-
-        Observable<org.apache.usergrid.persistence.model.entity.Entity> entityObs =
-            gm.loadEdgesFromSource( simpleSearchByEdgeType )
-            .flatMap(edge -> {
-                final Id appInfoId = edge.getTargetNode();
-
-                return systemCollectionManager.load(appInfoId)
-                    .filter(entity -> (entity != null));
-            });
-
-        return entityObs;
-    }
-
-    public Observable<Edge> getApplicationInfoEdges(final UUID applicationId) {
-        final ApplicationScope managementAppScope = getApplicationScope(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
-        final GraphManager gm = graphManagerFactory.createEdgeManager(managementAppScope);
-
-        String edgeType = CpNamingUtils.getEdgeTypeFromCollectionName(CpNamingUtils.APPLICATION_INFOS);
-
-
-        final SimpleSearchByEdge simpleSearchByEdgeType =  new SimpleSearchByEdge(
-            CpNamingUtils.generateApplicationId(CpNamingUtils.MANAGEMENT_APPLICATION_ID), edgeType, CpNamingUtils.generateApplicationId( applicationId ), Long.MAX_VALUE, SearchByEdgeType.Order.DESCENDING,
-            Optional.absent());
-
-        return gm.loadEdgeVersions(simpleSearchByEdgeType  );
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/d1c19434/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpNamingUtils.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpNamingUtils.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpNamingUtils.java
index c812180..e8867e1 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpNamingUtils.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpNamingUtils.java
@@ -72,11 +72,11 @@ public class CpNamingUtils {
     /**
      * Information about applications is stored in the management app using these types
      */
-    public static final String APPLICATION_INFO = "application_info";
-    public static final String APPLICATION_INFOS = "application_infos";
+    public static final String APPLICATION_INFO = "application_map";
+    public static final String APPLICATION_INFOS = "application_maps";
 
-    public static final String DELETED_APPLICATION_INFO = "deleted_application_info";
-    public static final String DELETED_APPLICATION_INFOS = "deleted_application_infos";
+    public static final String DELETED_APPLICATION_INFO = "deleted_application";
+    public static final String DELETED_APPLICATION_INFOS = "deleted_applications";
 
     /**
      * The name of the map that holds our entity id->type mapping

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/d1c19434/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManagerFactory.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManagerFactory.java b/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManagerFactory.java
index b2dc7eb..be6ff1d 100644
--- a/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManagerFactory.java
+++ b/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManagerFactory.java
@@ -77,7 +77,7 @@ public interface EntityManagerFactory {
      * @throws Exception the exception
      */
     Entity createApplicationV2(
-        String organizationName, String name, Map<String, Object> properties ) throws Exception;
+        String organizationName, String name, UUID applicationId, Map<String, Object> properties ) throws Exception;
 
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/d1c19434/stack/core/src/test/java/org/apache/usergrid/persistence/CounterIT.java
----------------------------------------------------------------------
diff --git a/stack/core/src/test/java/org/apache/usergrid/persistence/CounterIT.java b/stack/core/src/test/java/org/apache/usergrid/persistence/CounterIT.java
index 40da953..4e8b599 100644
--- a/stack/core/src/test/java/org/apache/usergrid/persistence/CounterIT.java
+++ b/stack/core/src/test/java/org/apache/usergrid/persistence/CounterIT.java
@@ -235,8 +235,6 @@ public class CounterIT extends AbstractCoreIT {
             applicationId, CpNamingUtils.APPLICATION_INFO, properties );
 
 //Creating connections like below doesn't work.
-//        em.createConnection( new SimpleEntityRef( "group", organizationEntity.getUuid() ), "owns",
-//                new SimpleEntityRef( "application_info", applicationId ) );
 
         em.createConnection( organizationEntity.toTypedEntity(), "owns", applicationEntity );
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/d1c19434/stack/services/src/main/java/org/apache/usergrid/management/AppInfoMigrationPlugin.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/management/AppInfoMigrationPlugin.java b/stack/services/src/main/java/org/apache/usergrid/management/AppInfoMigrationPlugin.java
new file mode 100644
index 0000000..3ac72d2
--- /dev/null
+++ b/stack/services/src/main/java/org/apache/usergrid/management/AppInfoMigrationPlugin.java
@@ -0,0 +1,306 @@
+/*
+ *
+ *  * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  *  contributor license agreements.  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.  For additional information regarding
+ *  * copyright in this work, please see the NOTICE file in the top level
+ *  * directory of this distribution.
+ *
+ */
+package org.apache.usergrid.management;
+
+
+import com.google.common.base.Optional;
+import com.google.inject.Inject;
+import org.apache.usergrid.corepersistence.util.CpEntityMapUtils;
+import org.apache.usergrid.corepersistence.util.CpNamingUtils;
+import org.apache.usergrid.persistence.*;
+import org.apache.usergrid.persistence.collection.EntityCollectionManager;
+import org.apache.usergrid.persistence.collection.EntityCollectionManagerFactory;
+import org.apache.usergrid.persistence.core.migration.data.MigrationInfoSerialization;
+import org.apache.usergrid.persistence.core.migration.data.MigrationPlugin;
+import org.apache.usergrid.persistence.core.migration.data.PluginPhase;
+import org.apache.usergrid.persistence.core.migration.data.ProgressObserver;
+import org.apache.usergrid.persistence.core.scope.ApplicationScope;
+import org.apache.usergrid.persistence.entities.Group;
+import org.apache.usergrid.persistence.exceptions.ApplicationAlreadyExistsException;
+import org.apache.usergrid.persistence.graph.Edge;
+import org.apache.usergrid.persistence.graph.GraphManager;
+import org.apache.usergrid.persistence.graph.GraphManagerFactory;
+import org.apache.usergrid.persistence.graph.SearchByEdgeType;
+import org.apache.usergrid.persistence.graph.impl.SimpleSearchByEdge;
+import org.apache.usergrid.persistence.graph.impl.SimpleSearchByEdgeType;
+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.utils.UUIDUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import rx.Observable;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.apache.usergrid.corepersistence.util.CpNamingUtils.getApplicationScope;
+import static org.apache.usergrid.persistence.Schema.PROPERTY_APPLICATION_ID;
+import static org.apache.usergrid.persistence.Schema.PROPERTY_NAME;
+
+/**
+ * Migration of appinfos collection to application_info collection.
+ *
+ * Part of USERGRID-448 "Remove redundant appinfos collections in ManagementServiceImpl"
+ * https://issues.apache.org/jira/browse/USERGRID-448
+ */
+public class AppInfoMigrationPlugin implements MigrationPlugin {
+
+    /** Old and deprecated SYSTEM_APP */
+    public static final UUID SYSTEM_APP_ID = UUID.fromString( "b6768a08-b5d5-11e3-a495-10ddb1de66c3" );
+
+    private static final Logger logger = LoggerFactory.getLogger(AppInfoMigrationPlugin.class);
+
+    public static String PLUGIN_NAME = "appinfo-migration";
+
+    @Inject
+    final private MigrationInfoSerialization migrationInfoSerialization;
+
+    @Inject
+    final private EntityManagerFactory emf;
+
+    @Inject
+    final private EntityCollectionManagerFactory entityCollectionManagerFactory;
+
+    @Inject
+    final private GraphManagerFactory graphManagerFactory;
+    private final ManagementService managementService;
+
+
+    @Inject
+    public AppInfoMigrationPlugin(
+        EntityManagerFactory emf,
+        MigrationInfoSerialization migrationInfoSerialization,
+        EntityCollectionManagerFactory entityCollectionManagerFactory,
+        GraphManagerFactory graphManagerFactory,
+        ManagementService managementService) {
+
+        this.emf = emf;
+        this.migrationInfoSerialization = migrationInfoSerialization;
+        this.entityCollectionManagerFactory = entityCollectionManagerFactory;
+        this.graphManagerFactory = graphManagerFactory;
+        this.managementService = managementService;
+    }
+
+
+    @Override
+    public String getName() {
+        return PLUGIN_NAME;
+    }
+
+
+    @Override
+    public int getMaxVersion() {
+        return 2; // standalone plugin, happens once
+    }
+
+
+    @Override
+    public PluginPhase getPhase() {
+        return PluginPhase.MIGRATE;
+    }
+
+
+    @Override
+    public void run(ProgressObserver observer) {
+
+        final int version = migrationInfoSerialization.getVersion(getName());
+
+        if (version == getMaxVersion()) {
+            logger.debug("Skipping Migration Plugin: " + getName());
+            return;
+        }
+
+        observer.start();
+        AtomicInteger count = new AtomicInteger();
+        //get old app infos to migrate
+        final Observable<Entity> oldAppInfos = getOldAppInfos();
+        oldAppInfos
+            .doOnNext(oldAppInfoEntity -> {
+                try {
+                    migrateAppInfo(oldAppInfoEntity, observer);
+                    count.incrementAndGet();
+                }catch (Exception e){
+                    if(e.getCause() instanceof ApplicationAlreadyExistsException){
+                        count.incrementAndGet();
+                        logger.info("Application already migrated",oldAppInfoEntity.getId().getUuid());
+                    }else {
+                        logger.error("Failed to migrate app info" + oldAppInfoEntity.getId().getUuid(), e);
+                        throw new RuntimeException(e);
+                    }
+                }
+
+            })
+            .doOnCompleted(() -> {
+                if(count.get()>0) {
+                    migrationInfoSerialization.setVersion(getName(), getMaxVersion());
+                }else{
+                    logger.error("Failed to migrate any app infos");
+                }
+                observer.complete();
+            }).toBlocking().lastOrDefault(null);
+
+    }
+
+
+    private void migrateAppInfo( org.apache.usergrid.persistence.model.entity.Entity oldAppInfoEntity, ProgressObserver observer) {
+        // Get appinfos from the Graph, we don't expect many so use iterator
+        final EntityManager managementEm = emf.getManagementEntityManager();
+
+        Map oldAppInfoMap = CpEntityMapUtils.toMap(oldAppInfoEntity);
+
+        final String name = (String) oldAppInfoMap.get(PROPERTY_NAME);
+
+        try {
+            final String orgName = name.split("/")[0];
+            final String appName = name.split("/")[1];
+            UUID applicationId = getUuid(oldAppInfoMap,"applicationUuid") ;
+            applicationId = applicationId == null ?  getUuid(oldAppInfoMap,"appUuid") : applicationId ;
+            //get app info from graph to see if it has been migrated already
+            org.apache.usergrid.persistence.Entity appInfo = getApplicationInfo(applicationId);
+            if (appInfo == null) {
+
+                observer.update(getMaxVersion(), "Created application_info for " + appName);
+                // create org->app connections, but not for apps in dummy "usergrid" internal organization
+                EntityRef orgRef = managementEm.getAlias(Group.ENTITY_TYPE, orgName);
+                // create and connect new APPLICATION_INFO oldAppInfo to Organization
+                managementService.createApplication(orgRef.getUuid(), name, applicationId, null);
+            } else {
+                //already migrated don't do anything
+                observer.update(getMaxVersion(), "Received existing application_info for " + appName + " don't do anything");
+            }
+
+        } catch (Exception e) {
+            String msg = "Exception writing application_info for " + name;
+            logger.error(msg, e);
+            observer.failed(getMaxVersion(), msg);
+            throw new RuntimeException(e);
+        }
+
+    }
+
+    private UUID getUuid(Map oldAppInfoMap, String key) {
+        UUID applicationId;
+        Object uuidObject = oldAppInfoMap.get(key);
+        if (uuidObject instanceof UUID) {
+            applicationId = (UUID) uuidObject;
+        } else {
+            applicationId = uuidObject == null ? null : UUIDUtils.tryExtractUUID(uuidObject.toString());
+        }
+        return applicationId;
+    }
+
+    private org.apache.usergrid.persistence.Entity createNewAppInfo(EntityManager managementEm, final String name, final UUID applicationId) throws Exception {
+        org.apache.usergrid.persistence.Entity appInfo;Map<String, Object> appInfoMap = new HashMap<String, Object>() {{
+            put(PROPERTY_NAME, name);
+            put(PROPERTY_APPLICATION_ID, applicationId);
+        }};
+        appInfo = managementEm.create(new SimpleId(applicationId, CpNamingUtils.APPLICATION_INFO), appInfoMap);
+        return appInfo;
+    }
+
+    /**
+     * TODO: Use Graph to get application_info for an specified Application.
+     */
+    private org.apache.usergrid.persistence.Entity getApplicationInfo( final UUID appId ) throws Exception {
+
+        final ApplicationScope managementAppScope = getApplicationScope(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
+        final EntityCollectionManager managementCollectionManager = entityCollectionManagerFactory.createCollectionManager(managementAppScope);
+
+        Observable<Edge> edgesObservable = getApplicationInfoEdges(appId);
+        //get the graph for all app infos
+        Observable<org.apache.usergrid.persistence.model.entity.Entity> entityObs =
+            edgesObservable.flatMap(edge -> {
+                final Id appInfoId = edge.getTargetNode();
+                return managementCollectionManager
+                    .load(appInfoId)
+                    .filter( entity ->{
+                        //check for app id
+                        return  entity != null ? entity.getId().getUuid().equals(appId) : false;
+                    });
+            });
+
+        // don't expect many applications, so we block
+        org.apache.usergrid.persistence.model.entity.Entity applicationInfo =
+            entityObs.toBlocking().lastOrDefault(null);
+
+        if ( applicationInfo == null ) {
+            return null;
+        }
+
+        Class clazz = Schema.getDefaultSchema().getEntityClass(applicationInfo.getId().getType());
+
+        org.apache.usergrid.persistence.Entity entity = EntityFactory.newEntity(
+            applicationInfo.getId().getUuid(), applicationInfo.getId().getType(), clazz);
+
+        entity.setProperties( CpEntityMapUtils.toMap( applicationInfo ) );
+
+        return entity;
+
+    }
+
+
+    /**
+     * Use Graph to get old appinfos from the old and deprecated System App.
+     */
+    public Observable<org.apache.usergrid.persistence.model.entity.Entity> getOldAppInfos( ) {
+
+        final ApplicationScope systemAppScope = getApplicationScope(SYSTEM_APP_ID);
+
+        final EntityCollectionManager systemCollectionManager =
+            entityCollectionManagerFactory.createCollectionManager(systemAppScope);
+
+        final GraphManager gm = graphManagerFactory.createEdgeManager(systemAppScope);
+
+        String edgeType = CpNamingUtils.getEdgeTypeFromCollectionName("appinfos");
+
+        Id rootAppId = systemAppScope.getApplication();
+
+        final SimpleSearchByEdgeType simpleSearchByEdgeType =  new SimpleSearchByEdgeType(
+            rootAppId, edgeType, Long.MAX_VALUE, SearchByEdgeType.Order.DESCENDING, Optional.absent());
+
+        Observable<org.apache.usergrid.persistence.model.entity.Entity> entityObs =
+            gm.loadEdgesFromSource( simpleSearchByEdgeType )
+                .flatMap(edge -> {
+                    final Id appInfoId = edge.getTargetNode();
+
+                    return systemCollectionManager.load(appInfoId)
+                        .filter(entity -> (entity != null));
+                });
+
+        return entityObs;
+    }
+
+    public Observable<Edge> getApplicationInfoEdges(final UUID applicationId) {
+        final ApplicationScope managementAppScope = getApplicationScope(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
+        final GraphManager gm = graphManagerFactory.createEdgeManager(managementAppScope);
+
+        String edgeType = CpNamingUtils.getEdgeTypeFromCollectionName(CpNamingUtils.APPLICATION_INFOS);
+
+
+        final SimpleSearchByEdge simpleSearchByEdgeType =  new SimpleSearchByEdge(
+            CpNamingUtils.generateApplicationId(CpNamingUtils.MANAGEMENT_APPLICATION_ID), edgeType, CpNamingUtils.generateApplicationId( applicationId ), Long.MAX_VALUE, SearchByEdgeType.Order.DESCENDING,
+            Optional.absent());
+
+        return gm.loadEdgeVersions(simpleSearchByEdgeType  );
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/d1c19434/stack/services/src/main/java/org/apache/usergrid/management/ManagementService.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/management/ManagementService.java b/stack/services/src/main/java/org/apache/usergrid/management/ManagementService.java
index aaa8d65..b5ac750 100644
--- a/stack/services/src/main/java/org/apache/usergrid/management/ManagementService.java
+++ b/stack/services/src/main/java/org/apache/usergrid/management/ManagementService.java
@@ -68,7 +68,7 @@ public interface ManagementService {
 
     public ApplicationInfo createApplication( UUID organizationId, String applicationName ) throws Exception;
 
-    public ApplicationInfo createApplication( UUID organizationId, String applicationName,
+    public ApplicationInfo createApplication( UUID organizationId, String applicationName, UUID applicationId,
                                               Map<String, Object> properties ) throws Exception;
 
     public OrganizationInfo createOrganization(String organizationName, UserInfo user, boolean activated)

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/d1c19434/stack/services/src/main/java/org/apache/usergrid/management/cassandra/ManagementServiceImpl.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/management/cassandra/ManagementServiceImpl.java b/stack/services/src/main/java/org/apache/usergrid/management/cassandra/ManagementServiceImpl.java
index 0c5fcec..3606f36 100644
--- a/stack/services/src/main/java/org/apache/usergrid/management/cassandra/ManagementServiceImpl.java
+++ b/stack/services/src/main/java/org/apache/usergrid/management/cassandra/ManagementServiceImpl.java
@@ -189,6 +189,8 @@ public class ManagementServiceImpl implements ManagementService {
 
     private static final String TOKEN_TYPE_CONFIRM = "confirm";
 
+    public static final String Org_App_Relationship = "applications";
+
     public static final String OAUTH_SECRET_SALT = "super secret oauth value";
 
     private static final String ORGANIZATION_PROPERTIES_DICTIONARY = "orgProperties";
@@ -1643,12 +1645,12 @@ public class ManagementServiceImpl implements ManagementService {
     @Override
     public ApplicationInfo createApplication( UUID organizationId, String applicationName ) throws Exception {
 
-        return createApplication( organizationId, applicationName, null );
+        return createApplication( organizationId, applicationName,null, null );
     }
 
 
     @Override
-    public ApplicationInfo createApplication( UUID organizationId, String applicationName,
+    public ApplicationInfo createApplication( UUID organizationId, String applicationName, UUID applicationId,
                                               Map<String, Object> properties ) throws Exception {
 
         if ( ( organizationId == null ) || ( applicationName == null ) ) {
@@ -1661,7 +1663,7 @@ public class ManagementServiceImpl implements ManagementService {
 
         OrganizationInfo organizationInfo = getOrganizationByUuid( organizationId );
         Entity appInfo = emf.createApplicationV2(
-            organizationInfo.getName(), applicationName, properties);
+            organizationInfo.getName(), applicationName, applicationId ,properties);
 
         writeUserToken( smf.getManagementAppId(), appInfo,
             encryptionService.plainTextCredentials(
@@ -1669,7 +1671,7 @@ public class ManagementServiceImpl implements ManagementService {
                 null,
                 smf.getManagementAppId() ) );
 
-        UUID applicationId = addApplicationToOrganization( organizationId, appInfo );
+        applicationId = addApplicationToOrganization( organizationId, appInfo );
 
         UserInfo user = null;
         try {
@@ -1761,7 +1763,7 @@ public class ManagementServiceImpl implements ManagementService {
 
         Results r = em.getSourceEntities(
             new SimpleEntityRef(CpNamingUtils.APPLICATION_INFO, applicationInfoId),
-            "owns", Group.ENTITY_TYPE, Level.ALL_PROPERTIES);
+            Org_App_Relationship, Group.ENTITY_TYPE, Level.ALL_PROPERTIES);
 
         Entity entity = r.getEntity();
         if ( entity != null ) {
@@ -1784,7 +1786,7 @@ public class ManagementServiceImpl implements ManagementService {
         // query for application_info entities
         final Results results = em.getTargetEntities(
             new SimpleEntityRef(Group.ENTITY_TYPE, organizationGroupId),
-            "owns", CpNamingUtils.APPLICATION_INFO, Level.ALL_PROPERTIES);
+            Org_App_Relationship, CpNamingUtils.APPLICATION_INFO, Level.ALL_PROPERTIES);
 
         final PagingResultsIterator itr = new PagingResultsIterator( results );
 
@@ -1839,7 +1841,7 @@ public class ManagementServiceImpl implements ManagementService {
         }
 
         EntityManager em = emf.getEntityManager( smf.getManagementAppId() );
-        em.createConnection( new SimpleEntityRef( Group.ENTITY_TYPE, organizationId ), "owns", appInfo );
+        em.createConnection( new SimpleEntityRef( Group.ENTITY_TYPE, organizationId ), Org_App_Relationship, appInfo );
 
         return applicationId;
     }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/d1c19434/stack/services/src/main/java/org/apache/usergrid/services/guice/ServiceModule.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/services/guice/ServiceModule.java b/stack/services/src/main/java/org/apache/usergrid/services/guice/ServiceModule.java
index b61b18c..f4296fb 100644
--- a/stack/services/src/main/java/org/apache/usergrid/services/guice/ServiceModule.java
+++ b/stack/services/src/main/java/org/apache/usergrid/services/guice/ServiceModule.java
@@ -20,7 +20,9 @@ package org.apache.usergrid.services.guice;
 
 
 import org.apache.usergrid.corepersistence.CoreModule;
+import org.apache.usergrid.management.AppInfoMigrationPlugin;
 import org.apache.usergrid.persistence.EntityManagerFactory;
+import org.apache.usergrid.persistence.core.migration.data.MigrationPlugin;
 import org.apache.usergrid.services.ServiceManager;
 import org.apache.usergrid.services.ServiceManagerFactory;
 import org.apache.usergrid.services.queues.ImportQueueListener;
@@ -58,6 +60,9 @@ public class ServiceModule extends AbstractModule {
         //Seems weird, aren't we just binding the factory to the exact same factory when it goes to look for it?
         bind( ServiceManagerFactory.class );
         bind( EntityManagerFactory.class );
+        final Multibinder<MigrationPlugin> plugins = Multibinder.newSetBinder( binder(), MigrationPlugin.class );
+        plugins.addBinding().to(AppInfoMigrationPlugin.class);
+
 
 
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/d1c19434/stack/services/src/test/java/org/apache/usergrid/ServiceITSetup.java
----------------------------------------------------------------------
diff --git a/stack/services/src/test/java/org/apache/usergrid/ServiceITSetup.java b/stack/services/src/test/java/org/apache/usergrid/ServiceITSetup.java
index e9bb248..460c471 100644
--- a/stack/services/src/test/java/org/apache/usergrid/ServiceITSetup.java
+++ b/stack/services/src/test/java/org/apache/usergrid/ServiceITSetup.java
@@ -17,7 +17,7 @@
 package org.apache.usergrid;
 
 
-import org.apache.usergrid.corepersistence.migration.AppInfoMigrationPlugin;
+import org.apache.usergrid.management.AppInfoMigrationPlugin;
 import org.apache.usergrid.management.ApplicationCreator;
 import org.apache.usergrid.management.ManagementService;
 import org.apache.usergrid.management.export.ExportService;
@@ -68,5 +68,4 @@ public interface ServiceITSetup extends CoreITSetup {
 
     SignInProviderFactory getProviderFactory();
 
-    AppInfoMigrationPlugin getAppInfoMigrationPlugin();
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/d1c19434/stack/services/src/test/java/org/apache/usergrid/ServiceITSetupImpl.java
----------------------------------------------------------------------
diff --git a/stack/services/src/test/java/org/apache/usergrid/ServiceITSetupImpl.java b/stack/services/src/test/java/org/apache/usergrid/ServiceITSetupImpl.java
index c17ee92..ac40ae3 100644
--- a/stack/services/src/test/java/org/apache/usergrid/ServiceITSetupImpl.java
+++ b/stack/services/src/test/java/org/apache/usergrid/ServiceITSetupImpl.java
@@ -20,7 +20,7 @@ package org.apache.usergrid;
 import java.util.Properties;
 
 import org.apache.usergrid.corepersistence.GuiceFactory;
-import org.apache.usergrid.corepersistence.migration.AppInfoMigrationPlugin;
+import org.apache.usergrid.management.AppInfoMigrationPlugin;
 import org.junit.runner.Description;
 import org.junit.runners.model.Statement;
 import org.slf4j.Logger;
@@ -176,10 +176,7 @@ public class ServiceITSetupImpl extends CoreITSetupImpl implements ServiceITSetu
         return providerFactory;
     }
 
-    @Override
-    public AppInfoMigrationPlugin getAppInfoMigrationPlugin() {
-        return appInfoMigrationPlugin;
-    }
+
 
     public void refreshIndex(UUID appid){
         this.getEntityIndex().refresh(appid);

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/d1c19434/stack/services/src/test/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPluginTest.java
----------------------------------------------------------------------
diff --git a/stack/services/src/test/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPluginTest.java b/stack/services/src/test/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPluginTest.java
index 98d73f9..ef97188 100644
--- a/stack/services/src/test/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPluginTest.java
+++ b/stack/services/src/test/java/org/apache/usergrid/corepersistence/migration/AppInfoMigrationPluginTest.java
@@ -25,18 +25,16 @@ import org.apache.usergrid.ServiceITSetup;
 import org.apache.usergrid.ServiceITSetupImpl;
 import org.apache.usergrid.cassandra.ClearShiroSubject;
 import org.apache.usergrid.corepersistence.util.CpNamingUtils;
+import org.apache.usergrid.management.AppInfoMigrationPlugin;
+import org.apache.usergrid.management.ManagementService;
 import org.apache.usergrid.management.OrganizationOwnerInfo;
 import org.apache.usergrid.persistence.*;
 import org.apache.usergrid.persistence.cassandra.CassandraService;
 import org.apache.usergrid.persistence.collection.EntityCollectionManagerFactory;
 import org.apache.usergrid.persistence.core.migration.data.MigrationInfoSerialization;
 import org.apache.usergrid.persistence.core.migration.data.ProgressObserver;
-import org.apache.usergrid.persistence.core.scope.ApplicationScopeImpl;
 import org.apache.usergrid.persistence.entities.Application;
-import org.apache.usergrid.persistence.graph.GraphManager;
 import org.apache.usergrid.persistence.graph.GraphManagerFactory;
-import org.apache.usergrid.persistence.graph.SearchByEdgeType;
-import org.apache.usergrid.persistence.graph.impl.SimpleSearchByEdgeType;
 
 import org.junit.ClassRule;
 import org.junit.Rule;
@@ -46,18 +44,9 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import rx.Observable;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-import java.util.UUID;
+import java.util.*;
 
 import static org.apache.usergrid.TestHelper.*;
-import static org.apache.usergrid.corepersistence.util.CpNamingUtils.createCollectionSearchEdge;
-import static org.apache.usergrid.corepersistence.util.CpNamingUtils.generateApplicationId;
-import static org.apache.usergrid.corepersistence.util.CpNamingUtils.getApplicationScope;
-import static org.apache.usergrid.corepersistence.util.CpNamingUtils.getEdgeTypeFromCollectionName;
 import static org.junit.Assert.*;
 
 
@@ -84,8 +73,9 @@ public class AppInfoMigrationPluginTest {
         Mockito.when(serialization.getVersion(Mockito.any())).thenReturn(0);
         EntityCollectionManagerFactory ecmf = setup.getInjector().getInstance(EntityCollectionManagerFactory.class);
         GraphManagerFactory gmf = setup.getInjector().getInstance(GraphManagerFactory.class);
+        ManagementService managementService = setup.getMgmtSvc();
 
-        AppInfoMigrationPlugin appInfoMigrationPlugin = new AppInfoMigrationPlugin(setup.getEmf(),serialization,ecmf,gmf);
+        AppInfoMigrationPlugin appInfoMigrationPlugin = new AppInfoMigrationPlugin(setup.getEmf(),serialization,ecmf,gmf,managementService);
 
        // create 10 applications, each with 10 entities
 
@@ -146,7 +136,7 @@ public class AppInfoMigrationPluginTest {
 
             final String appName = applicationInfo.getName();
             final String finalOrgId = organization.getOrganization().getUuid().toString();
-            final String finalAppId = applicationInfo.getProperty( Schema.PROPERTY_APPLICATION_ID ).toString();
+            final String finalAppId = applicationInfo.asId().getUuid().toString();
             systemAppEm.create("appinfo", new HashMap<String, Object>() {{
                 put("name", appName );
                 put("organizationUuid", finalOrgId );
@@ -191,9 +181,9 @@ public class AppInfoMigrationPluginTest {
         assertEquals( 0, appInfoResults.size() );
 
         final Results applicationInfoResults =  rootEm.searchCollection(
-            new SimpleEntityRef("application", mgmtAppId), "application_infos", Query.fromQL("select *"));
+            new SimpleEntityRef("application", mgmtAppId), CpNamingUtils.APPLICATION_INFOS, Query.fromQL("select *"));
         int appCount =  Observable.from( applicationInfoResults.getEntities() ).filter(
-            entity -> !entity.getName().startsWith( "org." ) && !entity.getName().startsWith( "usergrid" ) ).doOnNext(
+            entity -> !entity.getName().startsWith( "org." )  ).doOnNext(
             entity -> logger.info("counting entity {}", entity) ).count().toBlocking().last();
         assertEquals( appIds.size() ,appCount );
 
@@ -241,14 +231,12 @@ public class AppInfoMigrationPluginTest {
     }
 
     private Entity getApplicationInfo( UUID appId ) throws Exception {
-
-        UUID mgmtAppId = setup.getEmf().getManagementAppId();
-        EntityManager rootEm = setup.getEmf().getEntityManager( mgmtAppId );
-
-        final Results applicationInfoResults = rootEm.searchCollection(
-            new SimpleEntityRef("application", mgmtAppId), "application_infos",
-            Query.fromQL("select * where applicationId=" + appId.toString()));
-
-        return applicationInfoResults.getEntity();
+        Map<String, UUID> apps = setup.getEmf().getApplications();
+        if(apps.containsValue(appId)){
+            return setup.getEmf().getManagementEntityManager().get(appId);
+        }else{
+            fail("no app " + appId);
+            return null;
+        }
     }
 }


Mime
View raw message