Search in sources :

Example 6 with MongoDBConnection

use of org.teiid.mongodb.MongoDBConnection in project teiid by teiid.

the class TestMongoDBDirectQueryExecution method testShellDirect.

@Test
public void testShellDirect() throws Exception {
    Command cmd = this.utility.parseCommand("SELECT * FROM Customers");
    MongoDBConnection connection = Mockito.mock(MongoDBConnection.class);
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    DBCollection dbCollection = Mockito.mock(DBCollection.class);
    DB db = Mockito.mock(DB.class);
    Mockito.stub(db.getCollection("MyTable")).toReturn(dbCollection);
    Mockito.stub(db.collectionExists(Mockito.anyString())).toReturn(true);
    Mockito.stub(connection.getDatabase()).toReturn(db);
    Argument arg = new Argument(Direction.IN, null, String.class, null);
    arg.setArgumentValue(new Literal("$ShellCmd;MyTable;remove;{ qty: { $gt: 20 }}", String.class));
    ResultSetExecution execution = this.translator.createDirectExecution(Arrays.asList(arg), cmd, context, this.utility.createRuntimeMetadata(), connection);
    execution.execute();
    Mockito.verify(dbCollection).remove(QueryBuilder.start("qty").greaterThan(20).get());
}
Also used : DBCollection(com.mongodb.DBCollection) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) Argument(org.teiid.language.Argument) Command(org.teiid.language.Command) MongoDBConnection(org.teiid.mongodb.MongoDBConnection) Literal(org.teiid.language.Literal) DB(com.mongodb.DB) Test(org.junit.Test)

Example 7 with MongoDBConnection

use of org.teiid.mongodb.MongoDBConnection in project teiid by teiid.

the class TestMongoDBMetadataProcessor method processExampleMetadata.

private MetadataFactory processExampleMetadata(MongoDBMetadataProcessor mp) throws TranslatorException {
    MetadataFactory mf = new MetadataFactory("vdb", 1, "mongodb", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), null);
    MongoDBConnection conn = Mockito.mock(MongoDBConnection.class);
    DBCollection tableDBCollection = Mockito.mock(DBCollection.class);
    DBCollection embeddedDBCollection = Mockito.mock(DBCollection.class);
    DBCollection emptyDBCollection = Mockito.mock(DBCollection.class);
    DBCollection emptyFirstDBCollection = Mockito.mock(DBCollection.class);
    LinkedHashSet<String> tables = new LinkedHashSet<String>();
    tables.add("table");
    tables.add("embedded");
    tables.add("empty");
    tables.add("emptyFirst");
    DB db = Mockito.mock(DB.class);
    BasicDBList array = new BasicDBList();
    array.add("one");
    array.add("two");
    BasicDBObject row = new BasicDBObject();
    row.append("_id", new Integer(1));
    row.append("col2", new Double(2.0));
    row.append("col3", new Long(3L));
    row.append("col5", Boolean.TRUE);
    row.append("col6", new Date(0L));
    row.append("col6", new DBRef(db.getName(), "ns", "one"));
    row.append("col7", array);
    row.append("col8", new Binary("binary".getBytes()));
    BasicDBObject child = new BasicDBObject();
    child.append("col1", "one");
    child.append("col2", "two");
    row.append("child", child);
    BasicDBObject emptyFirstRow = new BasicDBObject();
    emptyFirstRow.append("_id", new ObjectId("5835a598944716c40d2f26ae"));
    emptyFirstRow.append("col2", new Double(2.0));
    emptyFirstRow.append("col3", new Long(3L));
    BasicDBObject embedded = new BasicDBObject();
    embedded.append("col1", 1);
    embedded.append("col2", new byte[0]);
    row.append("embedded", embedded);
    Mockito.stub(db.getCollectionNames()).toReturn(tables);
    Mockito.stub(db.getCollection(Mockito.eq("table"))).toReturn(tableDBCollection);
    Mockito.stub(db.getCollection(Mockito.eq("embedded"))).toReturn(embeddedDBCollection);
    Mockito.stub(db.getCollection(Mockito.eq("empty"))).toReturn(emptyDBCollection);
    Mockito.stub(db.getCollection(Mockito.eq("emptyFirst"))).toReturn(emptyFirstDBCollection);
    BasicDBObject nextRow = new BasicDBObject();
    nextRow.append("_id", new Integer(2));
    nextRow.append("col2", new Double(3.0));
    nextRow.append("col3", "A");
    nextRow.append("col5", Boolean.TRUE);
    nextRow.append("col9", "another");
    DBCursor tableCursor = Mockito.mock(DBCursor.class);
    Mockito.when(tableCursor.numSeen()).thenReturn(1).thenReturn(2);
    Mockito.when(tableCursor.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
    Mockito.when(tableCursor.next()).thenReturn(row).thenReturn(nextRow);
    Mockito.when(tableDBCollection.find()).thenReturn(tableCursor);
    DBCursor embeddedCursor = Mockito.mock(DBCursor.class);
    Mockito.when(embeddedCursor.hasNext()).thenReturn(true).thenReturn(false);
    Mockito.when(embeddedCursor.next()).thenReturn(child);
    Mockito.when(embeddedDBCollection.find()).thenReturn(embeddedCursor);
    DBCursor emptyFirstCursor = Mockito.mock(DBCursor.class);
    Mockito.when(emptyFirstCursor.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
    Mockito.when(emptyFirstCursor.next()).thenReturn(null).thenReturn(emptyFirstRow);
    Mockito.when(emptyFirstDBCollection.find()).thenReturn(emptyFirstCursor);
    DBCursor emptyCursor = Mockito.mock(DBCursor.class);
    Mockito.when(emptyCursor.hasNext()).thenReturn(true).thenReturn(false);
    Mockito.when(emptyCursor.next()).thenReturn(null);
    Mockito.when(emptyDBCollection.find()).thenReturn(emptyCursor);
    Mockito.stub(conn.getDatabase()).toReturn(db);
    mp.process(mf, conn);
    return mf;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ObjectId(org.bson.types.ObjectId) DBRef(com.mongodb.DBRef) Properties(java.util.Properties) Date(java.sql.Date) DBCollection(com.mongodb.DBCollection) BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) MetadataFactory(org.teiid.metadata.MetadataFactory) MongoDBConnection(org.teiid.mongodb.MongoDBConnection) Binary(org.bson.types.Binary) DB(com.mongodb.DB)

Example 8 with MongoDBConnection

use of org.teiid.mongodb.MongoDBConnection in project teiid by teiid.

the class TestMongoDBQueryExecution method helpExecute.

private DBCollection helpExecute(Command cmd, String[] expectedCollection, int expectedParameters) throws TranslatorException {
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    Mockito.stub(context.getBatchSize()).toReturn(256);
    MongoDBConnection connection = Mockito.mock(MongoDBConnection.class);
    DB db = Mockito.mock(DB.class);
    DBCollection dbCollection = Mockito.mock(DBCollection.class);
    for (String collection : expectedCollection) {
        Mockito.stub(db.getCollection(collection)).toReturn(dbCollection);
    }
    AggregationOutput output = Mockito.mock(AggregationOutput.class);
    Mockito.stub(output.results()).toReturn(new ArrayList<DBObject>());
    ArrayList<DBObject> params = new ArrayList<DBObject>();
    for (int i = 0; i < expectedParameters; i++) {
        params.add(Mockito.any(DBObject.class));
    }
    Mockito.stub(dbCollection.aggregate(params.remove(0), params.toArray(new DBObject[params.size()]))).toReturn(output);
    Mockito.stub(db.collectionExists(Mockito.anyString())).toReturn(true);
    Mockito.stub(connection.getDatabase()).toReturn(db);
    Mockito.stub(db.getCollectionFromString(Mockito.anyString())).toReturn(dbCollection);
    ResultSetExecution execution = this.translator.createResultSetExecution((QueryExpression) cmd, context, this.utility.createRuntimeMetadata(), connection);
    execution.execute();
    return dbCollection;
}
Also used : ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) MongoDBConnection(org.teiid.mongodb.MongoDBConnection) ArrayList(java.util.ArrayList)

Aggregations

MongoDBConnection (org.teiid.mongodb.MongoDBConnection)8 ExecutionContext (org.teiid.translator.ExecutionContext)5 DB (com.mongodb.DB)4 DBCollection (com.mongodb.DBCollection)4 Test (org.junit.Test)4 Command (org.teiid.language.Command)4 ResultSetExecution (org.teiid.translator.ResultSetExecution)4 BasicDBObject (com.mongodb.BasicDBObject)3 BasicDBList (com.mongodb.BasicDBList)2 DBCursor (com.mongodb.DBCursor)2 DBRef (com.mongodb.DBRef)2 Date (java.sql.Date)2 LinkedHashSet (java.util.LinkedHashSet)2 Properties (java.util.Properties)2 Binary (org.bson.types.Binary)2 ObjectId (org.bson.types.ObjectId)2 Argument (org.teiid.language.Argument)2 Literal (org.teiid.language.Literal)2 MetadataFactory (org.teiid.metadata.MetadataFactory)2 AggregationOptions (com.mongodb.AggregationOptions)1