public ServiceResults getApplicationEntity( ServiceContext context ) throws Exception { checkPermissionsForPath( context, "/" ); Entity entity = em.get( em.getApplicationRef() ); Results r = Results.fromEntity( entity ); Map<String, Object> collections = em.getApplicationCollectionMetadata(); // Set<String> collections = em.getApplicationCollections(); if ( collections.size() > 0 ) { r.setMetadata( em.getApplicationRef().getUuid(), "collections", collections ); } return genericServiceResults( r ); }
@Override public Map<String, String> getServiceProperties() { Map<String, String> props = new HashMap<String,String>(); EntityManager em = getEntityManager(getManagementAppId()); Query q = Query.fromQL("select *"); Results results = null; try { results = em.searchCollection( em.getApplicationRef(), "propertymaps", q); } catch (Exception ex) { logger.error("Error getting system properties", ex); } if ( results == null || results.isEmpty() ) { return props; } org.apache.usergrid.persistence.Entity e = results.getEntity(); for ( String key : e.getProperties().keySet() ) { props.put( key, props.get(key).toString() ); } return props; }
@Override public ServiceResults getApplicationMetadata( UUID applicationId ) throws Exception { if ( applicationId == null ) { return ServiceResults.genericServiceResults(); } EntityManager em = emf.getEntityManager( applicationId ); Entity entity = em.get( em.getApplicationRef() ); Results r = Results.fromEntity( entity ); Map<String, Object> collections = em.getApplicationCollectionMetadata(); if ( collections.size() > 0 ) { r.setMetadata( em.getApplicationRef().getUuid(), "collections", collections ); } return genericServiceResults( r ); }
@Test public void testAddMapToDictionaries() throws Exception { logger.info( "EntityDictionaryIT.testAddMapToDictionaries" ); Map<String,Object> testMap = new HashMap<String,Object>(); EntityManager em = app.getEntityManager(); assertNotNull( em ); testMap.put( "testName","testval" ); em.addMapToDictionary( em.getApplicationRef(), "testProvider",testMap ); Object o = em.getDictionaryElementValue( em.getApplicationRef(), "testProvider","testName" ); assertEquals("testval" , o.toString() ); } }
@Override public List<OrganizationInfo> getOrganizations( UUID startResult, int count ) throws Exception { // still need the bimap to search for existing BiMap<UUID, String> organizations = HashBiMap.create(); EntityManager em = emf.getEntityManager(smf.getManagementAppId()); Results results = em.getCollection(em.getApplicationRef(), Schema.COLLECTION_GROUPS, startResult, count, Level.ALL_PROPERTIES, false); List<OrganizationInfo> orgs = new ArrayList<>( results.size() ); OrganizationInfo orgInfo; for ( Entity entity : results.getEntities() ) { String path = ( String ) entity.getProperty( PROPERTY_PATH ); if ( organizations.containsValue( path ) ) { path += "DUPLICATE"; } orgInfo = new OrganizationInfo( entity.getUuid(), path ); orgs.add( orgInfo ); organizations.put( entity.getUuid(), path ); } return orgs; }
@Override public JobStat getStatsForJob( String jobName, UUID jobId ) throws Exception { EntityManager em = emf.getEntityManager( emf.getManagementAppId() ); Query query = Query.fromQL( "select * where " + JOB_NAME + " = '" + jobName + "' AND " + JOB_ID + " = " + jobId ); Results r = em.searchCollection( em.getApplicationRef(), "job_stats", query ); if ( r.size() == 1 ) { return ( JobStat ) r.getEntity(); } return null; }
@Override public Results getResults( Query query ) throws Exception { return app.getEntityManager().searchCollection( app.getEntityManager().getApplicationRef(), "tests", query ); } }
@Override public Results queryJobData( Query query ) throws Exception { if ( query == null ) { query = new Query(); } String jobDataType = Schema.getDefaultSchema().getEntityType(JobData.class); return getEm().searchCollection( getEm().getApplicationRef(), Schema.defaultCollectionName(jobDataType), query ); }
/** * Go around EntityManager and execute query directly against Core Persistence. * Results may include stale index entries. */ private Results queryCollectionEm( final String collName, final String query ) throws Exception { EntityManager em = app.getEntityManager(); final Results results = em.searchCollection( em.getApplicationRef(), collName, Query.fromQL( query ).withLimit( 10000 ) ); return results; } }
@Test public void testCreateAndDelete() throws Exception { logger.info( "EntityDaoTest.testCreateAndDelete" ); EntityManager em = app.getEntityManager(); String name = "test.thing" + UUIDUtils.newTimeUUID(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "name", name ); properties.put( "foo", "bar" ); logger.info( "Starting entity create" ); Entity thing = em.create( "thing", properties ); logger.info( "Entity created" ); app.waitForQueueDrainAndRefreshIndex(); logger.info( "Starting entity delete" ); em.delete( thing ); logger.info( "Entity deleted" ); app.waitForQueueDrainAndRefreshIndex(); // now search by username, no results should be returned final Query emailQuery = Query.fromQL( "name = '" + name + "'" ); Results r = em.searchCollection( em.getApplicationRef(), "thing", emailQuery ); assertEquals( 0, r.size() ); }
@Test public void stringWithSpaces() throws Exception { EntityManager em = app.getEntityManager(); assertNotNull( em ); logger.debug( "stringWithSpaces" ); Map<String, Object> props = new HashMap<String, Object>(); props.put( "myString", "My simple string" ); Entity saved = em.create( "test", props ); app.waitForQueueDrainAndRefreshIndex(); Query query = Query.fromQL( "myString = 'My simple string'" ); Results results = em.searchCollection( em.getApplicationRef(), "tests", query ); Entity entity = results.getEntitiesMap().get( saved.getUuid() ); assertNotNull( entity ); }
@Test public void badOrderByBadGrammarAsc() throws Exception { logger.debug( "badOrderByBadGrammarAsc" ); EntityManager em = app.getEntityManager(); assertNotNull( em ); String s = "select * where name = 'bob' order by"; String error = null; String entityType = null; String propertyName = null; try { em.searchCollection( em.getApplicationRef(), "users", Query.fromQL( s ) ); fail( "I should throw an exception" ); } catch ( Exception nie ) { error = nie.getMessage(); // entityType = nie.getEntityType(); // propertyName = nie.getPropertyName(); } // assertEquals( "Entity 'user' with property named '' is not indexed. " // + "You cannot use the this field in queries.", error ); // assertEquals( "user", entityType ); // assertEquals( "", propertyName ); }
protected List<EntityRef> getNotificationReceipts(EntityRef notification) throws Exception { Query query = new Query(); query.setCollection("receipts"); query.setLimit(100); PathQuery<Receipt> pathQuery = new PathQuery<Receipt>( new SimpleEntityRef(app.getEntityManager().getApplicationRef()), query ); Iterator<Receipt> it = pathQuery.iterator(app.getEntityManager()); List<EntityRef> list =new ArrayList<EntityRef>();//get all while(it.hasNext()){ Receipt receipt =it.next(); if(receipt.getNotificationUUID().equals(notification.getUuid())) { list.add(receipt); } } return list; }
private int countEntities( EntityManager em, String collectionName, int expectedEntities) throws Exception { app.waitForQueueDrainAndRefreshIndex(); Query q = Query.fromQL( "select * where key1=1000" ).withLimit( 1000 ); Results results = em.searchCollectionConsistent( em.getApplicationRef(), collectionName, q, expectedEntities ); int count = 0; while ( true ) { count += results.size(); if ( results.hasCursor() ) { logger.info( "Counted {} : query again with cursor", count ); q.setCursor( results.getCursor() ); results = em.searchCollection( em.getApplicationRef(), collectionName, q ); } else { break; } } assertEquals( "Did not get expected entities", expectedEntities, count ); return count; }
@Test public void testGetDictionaries() throws Exception { logger.info( "EntityDictionaryIT.testGetDictionaries" ); Application.OAuthProvider provider = new Application.OAuthProvider(); provider.setClientId( "123456789012.apps.googleusercontent.com" ); provider.setClientSecret( "abcdefghijklmnopqrstuvwx" ); provider.setRedirectUris( "https://www.example.com/oauth2callback" ); provider.setJavaScriptOrigins( "https://www.example.com" ); provider.setAuthorizationEndpointUrl( "https://accounts.google.com/o/oauth2/auth" ); provider.setAccessTokenEndpointUrl( "https://accounts.google.com/o/oauth2/token" ); provider.setVersion( "2.0" ); EntityManager em = app.getEntityManager(); assertNotNull( em ); em.addToDictionary( em.getApplicationRef(), "oauthproviders", "google", provider ); Object o = em.getDictionaryElementValue( em.getApplicationRef(), "oauthproviders", "google" ); assertNotNull( o ); Set<String> set = em.getDictionaryNames( em.getApplicationRef() ); assertTrue( set.contains( "oauthproviders" ) ); } @Test
@Test public void groupTitleSearch() throws Exception { logger.debug( "groupTitleSearch" ); EntityManager em = app.getEntityManager(); assertNotNull( em ); String titleName = "groupName" + UUIDUtils.newTimeUUID(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "title", titleName ); properties.put( "path", "testPath" ); properties.put( "name", "testName" ); Entity group = em.create( "group", properties ); assertNotNull( group ); app.waitForQueueDrainAndRefreshIndex(); // EntityRef final Query query = Query.fromQL( "title = '" + titleName + "'" ); Results r = em.searchCollection( em.getApplicationRef(), "groups", query ); assertTrue( r.size() > 0 ); Entity returned = r.getEntities().get( 0 ); assertEquals( group.getUuid(), returned.getUuid() ); }
@Test public void groupNameSearch() throws Exception { logger.debug( "groupNameSearch" ); EntityManager em = app.getEntityManager(); assertNotNull( em ); String groupName = "groupName" + UUIDUtils.newTimeUUID(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "title", "testTitle" ); properties.put( "path", "testPath" ); properties.put( "name", groupName ); Entity group = em.create( "group", properties ); assertNotNull( group ); app.waitForQueueDrainAndRefreshIndex(); // EntityRef final Query query = Query.fromQL( "name = '" + groupName + "'" ); Results r = em.searchCollection( em.getApplicationRef(), "groups", query ); assertTrue( r.size() > 0 ); Entity returned = r.getEntities().get( 0 ); assertEquals( group.getUuid(), returned.getUuid() ); }
@Test public void testApplicationDictionaries() throws Exception { Application.OAuthProvider provider = new Application.OAuthProvider(); provider.setClientId( "123456789012.apps.googleusercontent.com" ); provider.setClientSecret( "abcdefghijklmnopqrstuvwx" ); provider.setRedirectUris( "https://www.example.com/oauth2callback" ); provider.setJavaScriptOrigins( "https://www.example.com" ); provider.setAuthorizationEndpointUrl( "https://accounts.google.com/o/oauth2/auth" ); provider.setAccessTokenEndpointUrl( "https://accounts.google.com/o/oauth2/token" ); provider.setVersion( "2.0" ); logger.info( "EntityDictionaryIT.testApplicationDictionaries" ); EntityManager em = app.getEntityManager(); assertNotNull( em ); em.addToDictionary( em.getApplicationRef(), "oauthproviders", "google", provider ); Object o = em.getDictionaryElementValue( em.getApplicationRef(), "oauthproviders", "google" ); logger.info( JsonUtils.mapToFormattedJsonString( o ) ); }
@Test public void userLastNameSearch() throws Exception { logger.debug( "userLastNameSearch" ); EntityManager em = app.getEntityManager(); assertNotNull( em ); String lastName = "lastName" + UUIDUtils.newTimeUUID(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "username", "edanuff" ); properties.put( "email", "ed@anuff.com" ); properties.put( "lastname", lastName ); Entity user = em.create( "user", properties ); assertNotNull( user ); app.waitForQueueDrainAndRefreshIndex(); // EntityRef final Query query = Query.fromQL( "lastname = '" + lastName + "'" ); Results r = em.searchCollection( em.getApplicationRef(), "users", query ); assertTrue( r.size() > 0 ); Entity returned = r.getEntities().get( 0 ); assertEquals( user.getUuid(), returned.getUuid() ); }
private void checkApplicationsOk( String orgName) throws Exception { if (logger.isDebugEnabled()) { logger.debug("\n\nChecking applications OK\n"); } for (int i=0; i<10; i++) { String appName = orgName + "/application" + i; UUID uuid = setup.getEmf().lookupApplication(appName); assertTrue ("Should be able to get application", uuid != null ); EntityManager em = setup.getEmf().getEntityManager( uuid ); Application app = em.getApplication(); assertEquals( appName, app.getName() ); Results results = em.searchCollection( em.getApplicationRef(), "things", Query.fromQL("select *")); assertEquals( "Should have 10 entities", 10, results.size() ); } }