http://git-wip-us.apache.org/repos/asf/usergrid/blob/5f552e7e/stack/mongo-emulator/src/test/java/org/apache/usergrid/mongo/DatabaseInitializer.java ---------------------------------------------------------------------- diff --git a/stack/mongo-emulator/src/test/java/org/apache/usergrid/mongo/DatabaseInitializer.java b/stack/mongo-emulator/src/test/java/org/apache/usergrid/mongo/DatabaseInitializer.java deleted file mode 100644 index e527a7e..0000000 --- a/stack/mongo-emulator/src/test/java/org/apache/usergrid/mongo/DatabaseInitializer.java +++ /dev/null @@ -1,146 +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.mongo; - - -import java.util.Map; -import java.util.Properties; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.apache.usergrid.management.ManagementService; -import org.apache.usergrid.mq.QueueManagerFactory; -import org.apache.usergrid.persistence.EntityManagerFactory; -import org.apache.usergrid.services.ServiceManagerFactory; - - -public class DatabaseInitializer { - - private static final Logger logger = LoggerFactory.getLogger( DatabaseInitializer.class ); - - protected EntityManagerFactory emf; - - protected ServiceManagerFactory smf; - - protected ManagementService management; - - protected Properties properties; - - protected QueueManagerFactory qmf; - - - public DatabaseInitializer() { - - } - - - public EntityManagerFactory getEntityManagerFactory() { - return emf; - } - - - @Autowired - public void setEntityManagerFactory( EntityManagerFactory emf ) { - this.emf = emf; - } - - - public ServiceManagerFactory getServiceManagerFactory() { - return smf; - } - - - @Autowired - public void setServiceManagerFactory( ServiceManagerFactory smf ) { - this.smf = smf; - } - - - public ManagementService getManagementService() { - return management; - } - - - @Autowired - public void setManagementService( ManagementService management ) { - this.management = management; - } - - - public Properties getProperties() { - return properties; - } - - - @Autowired - public void setProperties( Properties properties ) { - this.properties = properties; - } - - - public QueueManagerFactory getQueueManagerFactory() { - return qmf; - } - - - @Autowired - public void setQueueManagerFactory( QueueManagerFactory qmf ) { - this.qmf = qmf; - } - - - boolean databaseInitializationPerformed = false; - - - public void init() { - logger.info( "Initializing server with Spring" ); - - // If we're running an embedded Cassandra, we always need to initialize - // it since Hector wipes the data on startup. - // - - if ( databaseInitializationPerformed ) { - logger.info( "Can only attempt to initialized database once per JVM process" ); - return; - } - databaseInitializationPerformed = true; - - logger.info( "Initializing Cassandra database" ); - Map<String, String> properties = emf.getServiceProperties(); - if ( properties != null ) { - logger.error( "System properties are initialized, database is set up already." ); - return; - } - - try { - emf.setup(); - } - catch ( Exception e ) { - logger.error( "Unable to complete core database setup, possibly due to it being setup already", e ); - } - - try { - management.setup(); - } - catch ( Exception e ) { - logger.error( "Unable to complete management database setup, possibly due to it being setup already", e ); - } - - logger.info( "Usergrid schema setup" ); - } -}
http://git-wip-us.apache.org/repos/asf/usergrid/blob/5f552e7e/stack/mongo-emulator/src/test/java/org/apache/usergrid/mongo/MongoQueryTest.java ---------------------------------------------------------------------- diff --git a/stack/mongo-emulator/src/test/java/org/apache/usergrid/mongo/MongoQueryTest.java b/stack/mongo-emulator/src/test/java/org/apache/usergrid/mongo/MongoQueryTest.java deleted file mode 100644 index 98bc7ba..0000000 --- a/stack/mongo-emulator/src/test/java/org/apache/usergrid/mongo/MongoQueryTest.java +++ /dev/null @@ -1,519 +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.mongo; - - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Set; -import java.util.UUID; - -import org.bson.types.BasicBSONList; -import org.junit.Ignore; -import org.junit.Test; -import org.apache.usergrid.persistence.EntityManager; - -import com.mongodb.BasicDBObject; -import com.mongodb.DB; -import com.mongodb.DBCollection; -import com.mongodb.DBCursor; -import com.mongodb.DBObject; -import com.mongodb.Mongo; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - - -@Ignore -public class MongoQueryTest extends AbstractMongoTest { - - @Test - public void stringEqual() throws Exception { - - UUID appId = emf.lookupApplication( "test-organization/test-app" ); - EntityManager em = emf.getEntityManager( appId ); - - Map<String, Object> properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Kings of Leon" ); - properties.put( "genre", "Southern Rock" ); - properties.put( "founded", 2000 ); - em.create( "stringequal", properties ); - - properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Stone Temple Pilots" ); - properties.put( "genre", "Rock" ); - properties.put( "founded", 1986 ); - em.create( "stringequal", properties ); - - properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Journey" ); - properties.put( "genre", "Classic Rock" ); - properties.put( "founded", 1973 ); - em.create( "stringequal", properties ); - - // See http://www.mongodb.org/display/DOCS/Java+Tutorial - - Mongo m = new Mongo( "localhost", 27017 ); - - DB db = m.getDB( "test-organization/test-app" ); - db.authenticate( "test", "test".toCharArray() ); - - Set<String> colls = db.getCollectionNames(); - - assertTrue( colls.contains( "stringequals" ) ); - - DBCollection coll = db.getCollection( "stringequals" ); - DBCursor cur = coll.find(); - int count = 0; - - while ( cur.hasNext() ) { - cur.next(); - count++; - } - - assertEquals( 3, count ); - - BasicDBObject query = new BasicDBObject(); - query.put( "genre", "Southern Rock" ); - cur = coll.find( query ); - - assertTrue( cur.hasNext() ); - - DBObject result = cur.next(); - assertEquals( "Kings of Leon", result.get( "name" ) ); - assertEquals( "Southern Rock", result.get( "genre" ) ); - - assertFalse( cur.hasNext() ); - } - - - @Test - public void greaterThan() throws Exception { - - UUID appId = emf.lookupApplication( "test-organization/test-app" ); - EntityManager em = emf.getEntityManager( appId ); - - Map<String, Object> properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Kings of Leon" ); - properties.put( "genre", "Southern Rock" ); - properties.put( "founded", 2000 ); - em.create( "greaterthan", properties ); - - properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Stone Temple Pilots" ); - properties.put( "genre", "Rock" ); - properties.put( "founded", 1986 ); - em.create( "greaterthan", properties ); - - properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Journey" ); - properties.put( "genre", "Classic Rock" ); - properties.put( "founded", 1973 ); - em.create( "greaterthan", properties ); - - // See http://www.mongodb.org/display/DOCS/Java+Tutorial - - Mongo m = new Mongo( "localhost", 27017 ); - - DB db = m.getDB( "test-organization/test-app" ); - db.authenticate( "test", "test".toCharArray() ); - - BasicDBObject query = new BasicDBObject(); - query.put( "founded", new BasicDBObject( "$gt", 1973 ) ); - - DBCollection coll = db.getCollection( "greaterthans" ); - DBCursor cur = coll.find( query ); - - assertTrue( cur.hasNext() ); - - DBObject result = cur.next(); - assertEquals( "Stone Temple Pilots", result.get( "name" ) ); - assertEquals( "Rock", result.get( "genre" ) ); - - result = cur.next(); - assertEquals( "Kings of Leon", result.get( "name" ) ); - assertEquals( "Southern Rock", result.get( "genre" ) ); - - assertFalse( cur.hasNext() ); - } - - - @Test - public void greaterThanEqual() throws Exception { - - UUID appId = emf.lookupApplication( "test-organization/test-app" ); - EntityManager em = emf.getEntityManager( appId ); - - Map<String, Object> properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Kings of Leon" ); - properties.put( "genre", "Southern Rock" ); - properties.put( "founded", 2000 ); - em.create( "greaterthanequal", properties ); - - properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Stone Temple Pilots" ); - properties.put( "genre", "Rock" ); - properties.put( "founded", 1986 ); - em.create( "greaterthanequal", properties ); - - properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Journey" ); - properties.put( "genre", "Classic Rock" ); - properties.put( "founded", 1973 ); - em.create( "greaterthanequal", properties ); - - // See http://www.mongodb.org/display/DOCS/Java+Tutorial - - Mongo m = new Mongo( "localhost", 27017 ); - - DB db = m.getDB( "test-organization/test-app" ); - db.authenticate( "test", "test".toCharArray() ); - - BasicDBObject query = new BasicDBObject(); - query.put( "founded", new BasicDBObject( "$gte", 1973 ) ); - - DBCollection coll = db.getCollection( "greaterthanequals" ); - DBCursor cur = coll.find( query ); - - assertTrue( cur.hasNext() ); - - DBObject result = cur.next(); - assertEquals( "Journey", result.get( "name" ) ); - assertEquals( "Classic Rock", result.get( "genre" ) ); - - result = cur.next(); - assertEquals( "Stone Temple Pilots", result.get( "name" ) ); - assertEquals( "Rock", result.get( "genre" ) ); - - result = cur.next(); - assertEquals( "Kings of Leon", result.get( "name" ) ); - assertEquals( "Southern Rock", result.get( "genre" ) ); - - assertFalse( cur.hasNext() ); - } - - - @Test - public void lessThan() throws Exception { - - UUID appId = emf.lookupApplication( "test-organization/test-app" ); - EntityManager em = emf.getEntityManager( appId ); - - Map<String, Object> properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Kings of Leon" ); - properties.put( "genre", "Southern Rock" ); - properties.put( "founded", 2000 ); - em.create( "lessthan", properties ); - - properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Stone Temple Pilots" ); - properties.put( "genre", "Rock" ); - properties.put( "founded", 1986 ); - em.create( "lessthan", properties ); - - properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Journey" ); - properties.put( "genre", "Classic Rock" ); - properties.put( "founded", 1973 ); - em.create( "lessthan", properties ); - - // See http://www.mongodb.org/display/DOCS/Java+Tutorial - - Mongo m = new Mongo( "localhost", 27017 ); - - DB db = m.getDB( "test-organization/test-app" ); - db.authenticate( "test", "test".toCharArray() ); - - BasicDBObject query = new BasicDBObject(); - query.put( "founded", new BasicDBObject( "$lt", 2000 ) ); - - DBCollection coll = db.getCollection( "lessthans" ); - DBCursor cur = coll.find( query ); - - assertTrue( cur.hasNext() ); - - DBObject result = cur.next(); - assertEquals( "Journey", result.get( "name" ) ); - assertEquals( "Classic Rock", result.get( "genre" ) ); - - result = cur.next(); - assertEquals( "Stone Temple Pilots", result.get( "name" ) ); - assertEquals( "Rock", result.get( "genre" ) ); - - assertFalse( cur.hasNext() ); - } - - - @Test - public void lessThanEqual() throws Exception { - - UUID appId = emf.lookupApplication( "test-organization/test-app" ); - EntityManager em = emf.getEntityManager( appId ); - - Map<String, Object> properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Kings of Leon" ); - properties.put( "genre", "Southern Rock" ); - properties.put( "founded", 2000 ); - em.create( "lessthanequal", properties ); - - properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Stone Temple Pilots" ); - properties.put( "genre", "Rock" ); - properties.put( "founded", 1986 ); - em.create( "lessthanequal", properties ); - - properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Journey" ); - properties.put( "genre", "Classic Rock" ); - properties.put( "founded", 1973 ); - em.create( "lessthanequal", properties ); - - // See http://www.mongodb.org/display/DOCS/Java+Tutorial - - Mongo m = new Mongo( "localhost", 27017 ); - - DB db = m.getDB( "test-organization/test-app" ); - db.authenticate( "test", "test".toCharArray() ); - - BasicDBObject query = new BasicDBObject(); - query.put( "founded", new BasicDBObject( "$lte", 2000 ) ); - - DBCollection coll = db.getCollection( "lessthanequals" ); - DBCursor cur = coll.find( query ); - - assertTrue( cur.hasNext() ); - - DBObject result = cur.next(); - assertEquals( "Journey", result.get( "name" ) ); - assertEquals( "Classic Rock", result.get( "genre" ) ); - - result = cur.next(); - assertEquals( "Stone Temple Pilots", result.get( "name" ) ); - assertEquals( "Rock", result.get( "genre" ) ); - - result = cur.next(); - assertEquals( "Kings of Leon", result.get( "name" ) ); - assertEquals( "Southern Rock", result.get( "genre" ) ); - - assertFalse( cur.hasNext() ); - } - - - @Test - public void in() throws Exception { - - UUID appId = emf.lookupApplication( "test-organization/test-app" ); - EntityManager em = emf.getEntityManager( appId ); - - Map<String, Object> properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Kings of Leon" ); - properties.put( "genre", "Southern Rock" ); - properties.put( "founded", 2000 ); - em.create( "testin", properties ); - - properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Stone Temple Pilots" ); - properties.put( "genre", "Rock" ); - properties.put( "founded", 1986 ); - em.create( "testin", properties ); - - properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Journey" ); - properties.put( "genre", "Classic Rock" ); - properties.put( "founded", 1973 ); - em.create( "testin", properties ); - - // See http://www.mongodb.org/display/DOCS/Java+Tutorial - - Mongo m = new Mongo( "localhost", 27017 ); - - DB db = m.getDB( "test-organization/test-app" ); - db.authenticate( "test", "test".toCharArray() ); - - BasicBSONList list = new BasicBSONList(); - list.add( "Stone Temple Pilots" ); - list.add( "Journey" ); - - BasicDBObject query = new BasicDBObject(); - query.put( "name", new BasicDBObject( "$in", list ) ); - - DBCollection coll = db.getCollection( "testins" ); - DBCursor cur = coll.find( query ); - - assertTrue( cur.hasNext() ); - - DBObject result = cur.next(); - assertEquals( "Journey", result.get( "name" ) ); - assertEquals( "Classic Rock", result.get( "genre" ) ); - - result = cur.next(); - assertEquals( "Stone Temple Pilots", result.get( "name" ) ); - assertEquals( "Rock", result.get( "genre" ) ); - - assertFalse( cur.hasNext() ); - } - - - @Test - public void or() throws Exception { - - UUID appId = emf.lookupApplication( "test-organization/test-app" ); - EntityManager em = emf.getEntityManager( appId ); - - Map<String, Object> properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Kings of Leon" ); - properties.put( "genre", "Southern Rock" ); - properties.put( "founded", 2000 ); - em.create( "testor", properties ); - - properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Stone Temple Pilots" ); - properties.put( "genre", "Rock" ); - properties.put( "founded", 1986 ); - em.create( "testor", properties ); - - properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Journey" ); - properties.put( "genre", "Classic Rock" ); - properties.put( "founded", 1973 ); - em.create( "testor", properties ); - - // See http://www.mongodb.org/display/DOCS/Java+Tutorial - - Mongo m = new Mongo( "localhost", 27017 ); - - DB db = m.getDB( "test-organization/test-app" ); - db.authenticate( "test", "test".toCharArray() ); - - BasicBSONList list = new BasicBSONList(); - list.add( new BasicDBObject( "founded", new BasicDBObject( "$gte", 2000 ) ) ); - list.add( new BasicDBObject( "founded", new BasicDBObject( "$lte", 1973 ) ) ); - - BasicDBObject query = new BasicDBObject(); - query.put( "$or", list ); - - DBCollection coll = db.getCollection( "testors" ); - DBCursor cur = coll.find( query ); - - assertTrue( cur.hasNext() ); - - DBObject result = cur.next(); - assertEquals( "Journey", result.get( "name" ) ); - assertEquals( "Classic Rock", result.get( "genre" ) ); - - result = cur.next(); - assertEquals( "Kings of Leon", result.get( "name" ) ); - assertEquals( "Southern Rock", result.get( "genre" ) ); - - assertFalse( cur.hasNext() ); - } - - - @Test - public void and() throws Exception { - - UUID appId = emf.lookupApplication( "test-organization/test-app" ); - EntityManager em = emf.getEntityManager( appId ); - - Map<String, Object> properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Kings of Leon" ); - properties.put( "genre", "Southern Rock" ); - properties.put( "founded", 2000 ); - em.create( "testand", properties ); - - properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Stone Temple Pilots" ); - properties.put( "genre", "Rock" ); - properties.put( "founded", 1986 ); - em.create( "testand", properties ); - - properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Journey" ); - properties.put( "genre", "Classic Rock" ); - properties.put( "founded", 1973 ); - em.create( "testand", properties ); - - // See http://www.mongodb.org/display/DOCS/Java+Tutorial - - Mongo m = new Mongo( "localhost", 27017 ); - - DB db = m.getDB( "test-organization/test-app" ); - db.authenticate( "test", "test".toCharArray() ); - - BasicBSONList list = new BasicBSONList(); - list.add( new BasicDBObject( "founded", new BasicDBObject( "$gte", 2000 ) ) ); - list.add( new BasicDBObject( "founded", new BasicDBObject( "$lte", 2005 ) ) ); - - BasicDBObject query = new BasicDBObject(); - query.put( "$and", list ); - - DBCollection coll = db.getCollection( "testands" ); - DBCursor cur = coll.find( query ); - - assertTrue( cur.hasNext() ); - - DBObject result = cur.next(); - assertEquals( "Kings of Leon", result.get( "name" ) ); - assertEquals( "Southern Rock", result.get( "genre" ) ); - assertFalse( cur.hasNext() ); - } - - - @Test - public void withFieldSelector() throws Exception { - UUID appId = emf.lookupApplication( "test-organization/test-app" ); - EntityManager em = emf.getEntityManager( appId ); - - Map<String, Object> properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Kings of Leon" ); - properties.put( "genre", "Southern Rock" ); - properties.put( "founded", 2000 ); - em.create( "withfieldselector", properties ); - - properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Stone Temple Pilots" ); - properties.put( "genre", "Rock" ); - properties.put( "founded", 1986 ); - em.create( "withfieldselector", properties ); - - - properties = new LinkedHashMap<String, Object>(); - properties.put( "name", "Journey" ); - properties.put( "genre", "Classic Rock" ); - properties.put( "founded", 1973 ); - em.create( "withfieldselector", properties ); - - Mongo m = new Mongo( "localhost", 27017 ); - - DB db = m.getDB( "test-organization/test-app" ); - db.authenticate( "test", "test".toCharArray() ); - - BasicDBObject queryName = new BasicDBObject(); - queryName.put( "name", "Journey" ); - - BasicDBObject limitName = new BasicDBObject(); - limitName.put( "name", 1 ); - - //query.put(); - DBCollection coll = db.getCollection( "withfieldselectors" ); - - DBCursor cur = coll.find( queryName, limitName ); - - assertTrue( cur.hasNext() ); - } -} http://git-wip-us.apache.org/repos/asf/usergrid/blob/5f552e7e/stack/mongo-emulator/src/test/resources/usergrid-test-context.xml ---------------------------------------------------------------------- diff --git a/stack/mongo-emulator/src/test/resources/usergrid-test-context.xml b/stack/mongo-emulator/src/test/resources/usergrid-test-context.xml deleted file mode 100644 index d066bb2..0000000 --- a/stack/mongo-emulator/src/test/resources/usergrid-test-context.xml +++ /dev/null @@ -1,48 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - 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. ---> -<beans xmlns="http://www.springframework.org/schema/beans" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" - xmlns:context="http://www.springframework.org/schema/context" - xmlns:p="http://www.springframework.org/schema/p" - xsi:schemaLocation=" - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd - http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> - - - <import resource="classpath:/usergrid-services-context.xml"/> - - <bean id="properties" - class="org.springframework.beans.factory.config.PropertiesFactoryBean"> - <property name="singleton" value="true" /> - <property name="ignoreResourceNotFound" value="true" /> - <property name="locations"> - <list> - <value>classpath:/usergrid-default.properties</value> - <value>classpath:/usergrid-test.properties</value> - <value>${usergrid-custom-spring-test-properties}</value> - </list> - </property> - </bean> - - <bean id="mongoServer" class="org.apache.usergrid.mongo.MongoServer" - init-method="startServer" destroy-method="stopServer" /> - - <bean id="databaseInitializer" class="org.apache.usergrid.mongo.DatabaseInitializer" - init-method="init" /> -</beans>