Search in sources :

Example 1 with MongoDBConnection

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

the class TestMongoDBQueryExecution method testNextWithGroupAndOrder.

@Test
public void testNextWithGroupAndOrder() throws Exception {
    String query = "select \"FirstName\" from \"TeiidArray\" group by \"FirstName\" order by \"FirstName\" limit 1000";
    String[] expectedCollection = new String[] { "TeiidArray" };
    TransformationMetadata metadata = RealMetadataFactory.fromDDL("CREATE FOREIGN TABLE TeiidArray (ID String PRIMARY KEY, FirstName varchar(25), LastName varchar(25), Score object[]) OPTIONS(UPDATABLE 'TRUE');", "x", "y");
    TranslationUtility util = new TranslationUtility(metadata);
    Command cmd = util.parseCommand(query);
    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);
    }
    Cursor c = Mockito.mock(Cursor.class);
    Mockito.stub(c.hasNext()).toAnswer(new Answer<Boolean>() {

        boolean next = true;

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            if (next) {
                next = false;
                return true;
            }
            return false;
        }
    });
    DBObject dbo = Mockito.mock(DBObject.class);
    Mockito.stub(c.next()).toReturn(dbo);
    Mockito.stub(dbCollection.aggregate((List<DBObject>) Mockito.anyList(), (AggregationOptions) Mockito.anyObject())).toReturn(c);
    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, util.createRuntimeMetadata(), connection);
    execution.execute();
    execution.next();
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) TranslationUtility(org.teiid.cdk.api.TranslationUtility) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) MongoDBConnection(org.teiid.mongodb.MongoDBConnection) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 2 with MongoDBConnection

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

the class TestMongoDBUpdateExecution method helpUpdate.

private DBCollection helpUpdate(String query, String[] expectedCollection, DBObject match_result, ArrayList<DBObject> results) throws TranslatorException {
    Command cmd = this.utility.parseCommand(query);
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    CommandContext cc = Mockito.mock(CommandContext.class);
    Mockito.stub(context.getCommandContext()).toReturn(cc);
    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);
    }
    Mockito.stub(db.collectionExists(Mockito.anyString())).toReturn(true);
    Mockito.stub(connection.getDatabase()).toReturn(db);
    Mockito.stub(db.getCollectionFromString(Mockito.anyString())).toReturn(dbCollection);
    Mockito.stub(dbCollection.findOne(Mockito.any(BasicDBObject.class))).toReturn(match_result);
    WriteResult result = Mockito.mock(WriteResult.class);
    Mockito.stub(result.getN()).toReturn(1);
    Mockito.stub(dbCollection.insert(Mockito.any(BasicDBObject.class), Mockito.any(WriteConcern.class))).toReturn(result);
    Mockito.stub(dbCollection.update(Mockito.any(BasicDBObject.class), Mockito.any(BasicDBObject.class), Mockito.eq(false), Mockito.eq(true), Mockito.any(WriteConcern.class))).toReturn(result);
    if (results != null) {
        Cursor out = new ResultsCursor(results);
        for (DBObject obj : results) {
            Mockito.stub(dbCollection.aggregate(Mockito.anyList(), Mockito.any(AggregationOptions.class))).toReturn(out);
            Mockito.stub(dbCollection.aggregate(Mockito.anyList(), Mockito.any(AggregationOptions.class))).toReturn(out);
        }
    }
    UpdateExecution execution = this.translator.createUpdateExecution(cmd, context, this.utility.createRuntimeMetadata(), connection);
    execution.execute();
    return dbCollection;
}
Also used : CommandContext(org.teiid.CommandContext) UpdateExecution(org.teiid.translator.UpdateExecution) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) MongoDBConnection(org.teiid.mongodb.MongoDBConnection)

Example 3 with MongoDBConnection

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

the class TestEmbeddedMongoExecution method getConnection.

private static MongoDBConnection getConnection(MongoClient client) {
    MongoDBConnection connection = Mockito.mock(MongoDBConnection.class);
    Mockito.stub(connection.getDatabase()).toReturn(client.getDB("test"));
    return connection;
}
Also used : MongoDBConnection(org.teiid.mongodb.MongoDBConnection)

Example 4 with MongoDBConnection

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

the class TestMongoDBDirectQueryExecution method testDirect.

@Test
public void testDirect() 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);
    AggregationOutput output = Mockito.mock(AggregationOutput.class);
    Mockito.stub(output.results()).toReturn(new ArrayList<DBObject>());
    Mockito.stub(dbCollection.aggregate(Mockito.any(DBObject.class), Mockito.any(DBObject.class))).toReturn(output);
    Argument arg = new Argument(Direction.IN, null, String.class, null);
    arg.setArgumentValue(new Literal("MyTable;{$match:{\"id\":\"$1\"}};{$project:{\"_m0\":\"$user\"}}", String.class));
    Argument arg2 = new Argument(Direction.IN, null, String.class, null);
    arg2.setArgumentValue(new Literal("foo", String.class));
    ResultSetExecution execution = this.translator.createDirectExecution(Arrays.asList(arg, arg2), cmd, context, this.utility.createRuntimeMetadata(), connection);
    execution.execute();
    List<DBObject> pipeline = TestMongoDBQueryExecution.buildArray(new BasicDBObject("$match", new BasicDBObject("id", "foo")), new BasicDBObject("$project", new BasicDBObject("_m0", "$user")));
    Mockito.verify(dbCollection).aggregate(Mockito.eq(pipeline), Mockito.any(AggregationOptions.class));
}
Also used : AggregationOptions(com.mongodb.AggregationOptions) Argument(org.teiid.language.Argument) AggregationOutput(com.mongodb.AggregationOutput) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBCollection(com.mongodb.DBCollection) ResultSetExecution(org.teiid.translator.ResultSetExecution) BasicDBObject(com.mongodb.BasicDBObject) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) MongoDBConnection(org.teiid.mongodb.MongoDBConnection) Literal(org.teiid.language.Literal) DB(com.mongodb.DB) Test(org.junit.Test)

Example 5 with MongoDBConnection

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

the class TestMongoDBMetadataProcessor method testExclusion.

@Test
public void testExclusion() throws TranslatorException {
    MongoDBMetadataProcessor mp = new MongoDBMetadataProcessor();
    mp.setExcludeTables("e.*");
    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", "one");
    embedded.append("col2", "two");
    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);
    DBCursor tableCursor = Mockito.mock(DBCursor.class);
    Mockito.when(tableCursor.hasNext()).thenReturn(true).thenReturn(false);
    Mockito.when(tableCursor.next()).thenReturn(row);
    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);
    String metadataDDL = DDLStringVisitor.getDDLString(mf.getSchema(), null, null);
    String expected = "SET NAMESPACE 'http://www.teiid.org/ext/relational/2012' AS teiid_rel;\n" + "SET NAMESPACE 'http://www.teiid.org/translator/mongodb/2013' AS teiid_mongo;\n" + "\n" + "CREATE FOREIGN TABLE \"table\" (\n" + "    \"_id\" integer,\n" + "    col2 double,\n" + "    col3 long,\n" + "    col5 boolean,\n" + "    col6 string,\n" + "    col7 object[] OPTIONS (SEARCHABLE 'Unsearchable'),\n" + "    col8 varbinary OPTIONS (NATIVE_TYPE 'org.bson.types.Binary'),\n" + "    CONSTRAINT PK0 PRIMARY KEY(\"_id\"),\n" + "    CONSTRAINT FK_col6 FOREIGN KEY(col6) REFERENCES ns \n" + ") OPTIONS (UPDATABLE TRUE, \"teiid_rel:fqn\" 'collection=table');\n" + "\n" + "CREATE FOREIGN TABLE child (\n" + "    col1 string,\n" + "    col2 string,\n" + "    \"_id\" integer OPTIONS (UPDATABLE FALSE),\n" + "    CONSTRAINT PK0 PRIMARY KEY(\"_id\"),\n" + "    FOREIGN KEY(\"_id\") REFERENCES \"table\" \n" + ") OPTIONS (UPDATABLE TRUE, \"teiid_rel:fqn\" 'collection=table/embedded=child', \"teiid_mongo:MERGE\" 'table');\n" + "\n" + "CREATE FOREIGN TABLE embedded (\n" + "    col1 string,\n" + "    col2 string,\n" + "    \"_id\" integer OPTIONS (UPDATABLE FALSE),\n" + "    CONSTRAINT PK0 PRIMARY KEY(\"_id\"),\n" + "    FOREIGN KEY(\"_id\") REFERENCES \"table\" \n" + ") OPTIONS (UPDATABLE TRUE, \"teiid_rel:fqn\" 'collection=table/embedded=embedded', \"teiid_mongo:MERGE\" 'table');";
    assertEquals(expected, metadataDDL.replace("\t", "    "));
}
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) Test(org.junit.Test)

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