use of org.teiid.translator.ExecutionContext in project teiid by teiid.
the class TestConnectorWorkItem method testConversionError.
@Test
public void testConversionError() throws Exception {
BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
final ExecutionFactory<Object, Object> ef = new ExecutionFactory<Object, Object>() {
@Override
public boolean isSourceRequired() {
return false;
}
@Override
public ResultSetExecution createResultSetExecution(QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
List<String> list1 = new ArrayList<String>();
list1.add("1");
List<String> list2 = new ArrayList<String>();
list2.add("a");
final Iterator<List<String>> iter = Arrays.asList(list1, list2).iterator();
return new ResultSetExecution() {
@Override
public void execute() throws TranslatorException {
}
@Override
public void close() {
}
@Override
public void cancel() throws TranslatorException {
}
@Override
public List<?> next() throws TranslatorException, DataNotAvailableException {
if (iter.hasNext()) {
return iter.next();
}
return null;
}
};
}
};
ConnectorManager cm = new // $NON-NLS-1$ //$NON-NLS-2$
ConnectorManager(// $NON-NLS-1$ //$NON-NLS-2$
"FakeConnector", // $NON-NLS-1$ //$NON-NLS-2$
"FakeConnector") {
public ExecutionFactory getExecutionFactory() {
return ef;
}
public Object getConnectionFactory() {
return null;
}
};
cm.start();
ef.setCopyLobs(true);
AtomicRequestMessage requestMsg = createNewAtomicRequestMessage(1, 1);
// $NON-NLS-1$
requestMsg.setCommand(helpGetCommand("SELECT intkey FROM bqt1.smalla", EXAMPLE_BQT));
requestMsg.setBufferManager(bm);
ConnectorWorkItem cwi = new ConnectorWorkItem(requestMsg, cm);
cwi.execute();
AtomicResultsMessage message = cwi.more();
List[] results = message.getResults();
assertEquals(1, results.length);
List<?> tuple = results[0];
assertEquals(1, tuple.get(0));
assertEquals(-1, message.getFinalRow());
try {
cwi.more();
fail();
} catch (TranslatorException e) {
// should throw the conversion error
}
}
use of org.teiid.translator.ExecutionContext in project teiid by teiid.
the class TestConnectorWorkItem method testUnmodifibleList.
@Test
public void testUnmodifibleList() throws Exception {
BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
final ExecutionFactory<Object, Object> ef = new ExecutionFactory<Object, Object>() {
@Override
public boolean isSourceRequired() {
return false;
}
@Override
public ResultSetExecution createResultSetExecution(QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
List<String> list1 = Collections.singletonList("1");
final Iterator<List<String>> iter = Arrays.asList(list1).iterator();
return new ResultSetExecution() {
@Override
public void execute() throws TranslatorException {
}
@Override
public void close() {
}
@Override
public void cancel() throws TranslatorException {
}
@Override
public List<?> next() throws TranslatorException, DataNotAvailableException {
if (iter.hasNext()) {
return iter.next();
}
return null;
}
};
}
};
ConnectorManager cm = new // $NON-NLS-1$ //$NON-NLS-2$
ConnectorManager(// $NON-NLS-1$ //$NON-NLS-2$
"FakeConnector", // $NON-NLS-1$ //$NON-NLS-2$
"FakeConnector") {
public ExecutionFactory getExecutionFactory() {
return ef;
}
public Object getConnectionFactory() {
return null;
}
};
cm.start();
AtomicRequestMessage requestMsg = createNewAtomicRequestMessage(1, 1);
// $NON-NLS-1$
requestMsg.setCommand(helpGetCommand("SELECT intkey FROM bqt1.smalla", EXAMPLE_BQT));
requestMsg.setBufferManager(bm);
ConnectorWorkItem cwi = new ConnectorWorkItem(requestMsg, cm);
cwi.execute();
AtomicResultsMessage message = cwi.more();
List[] results = message.getResults();
assertEquals(1, results.length);
List<?> tuple = results[0];
assertEquals(1, tuple.get(0));
assertEquals(1, message.getFinalRow());
}
use of org.teiid.translator.ExecutionContext in project teiid by teiid.
the class TestODataIntegration method testCheckGeneratedColumns.
@Test
public void testCheckGeneratedColumns() throws Exception {
HardCodedExecutionFactory hc = new HardCodedExecutionFactory() {
@Override
public UpdateExecution createUpdateExecution(org.teiid.language.Command command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
GeneratedKeys keys = executionContext.getCommandContext().returnGeneratedKeys(new String[] { "a" }, new Class[] { String.class });
keys.addKey(Arrays.asList("ax"));
return super.createUpdateExecution(command, executionContext, metadata, connection);
}
@Override
public boolean supportsCompareCriteriaEquals() {
return true;
}
};
hc.addUpdate("INSERT INTO x (b, c) VALUES ('b', 5)", new int[] { 1 });
// this gets called right after insert.
hc.addData("SELECT x.a, x.b, x.c FROM x WHERE x.a = 'ax'", Arrays.asList(Arrays.asList("a", "b", 2)));
teiid.addTranslator("x", hc);
try {
ModelMetaData mmd = new ModelMetaData();
mmd.setName("m");
mmd.addSourceMetadata("ddl", "create foreign table x (a string, b string, c integer, " + "primary key (a)) options (updatable true);");
mmd.addSourceMapping("x", "x", null);
teiid.deployVDB("northwind", mmd);
localClient = getClient(teiid.getDriver(), "northwind", new Properties());
ContentResponse response = http.newRequest(baseURL + "/northwind/m/x").method("POST").content(new StringContentProvider("{\"b\":\"b\", \"c\":5}"), "application/json").send();
assertEquals(201, response.getStatus());
} finally {
localClient = null;
teiid.undeployVDB("northwind");
}
}
use of org.teiid.translator.ExecutionContext 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.translator.ExecutionContext 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;
}
Aggregations