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();
}
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;
}
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;
}
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));
}
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", " "));
}
Aggregations