use of org.teiid.translator.ExecutionContext in project teiid by teiid.
the class TestODataQueryExecution method helpExecute.
private ResultSetExecution helpExecute(String query, final String resultXML, String expectedURL, int responseCode, TransformationMetadata metadata) throws Exception {
ODataExecutionFactory translator = new ODataExecutionFactory();
translator.start();
TranslationUtility utility = new TranslationUtility(metadata);
Command cmd = utility.parseCommand(query);
ExecutionContext context = Mockito.mock(ExecutionContext.class);
WSConnection connection = Mockito.mock(WSConnection.class);
Map<String, Object> headers = new HashMap<String, Object>();
headers.put(MessageContext.HTTP_REQUEST_HEADERS, new HashMap<String, List<String>>());
headers.put(WSConnection.STATUS_CODE, new Integer(responseCode));
Dispatch<DataSource> dispatch = Mockito.mock(Dispatch.class);
Mockito.stub(dispatch.getRequestContext()).toReturn(headers);
Mockito.stub(dispatch.getResponseContext()).toReturn(headers);
Mockito.stub(connection.createDispatch(Mockito.eq(HTTPBinding.HTTP_BINDING), Mockito.anyString(), Mockito.eq(DataSource.class), Mockito.eq(Mode.MESSAGE))).toReturn(dispatch);
DataSource ds = new DataSource() {
@Override
public OutputStream getOutputStream() throws IOException {
return new ByteArrayOutputStream();
}
@Override
public String getName() {
return "result";
}
@Override
public InputStream getInputStream() throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream(resultXML.getBytes());
return in;
}
@Override
public String getContentType() {
return "application/xml";
}
};
Mockito.stub(dispatch.invoke(Mockito.any(DataSource.class))).toReturn(ds);
ResultSetExecution execution = translator.createResultSetExecution((QueryExpression) cmd, context, utility.createRuntimeMetadata(), connection);
execution.execute();
ArgumentCaptor<String> endpoint = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> binding = ArgumentCaptor.forClass(String.class);
Mockito.verify(connection).createDispatch(binding.capture(), endpoint.capture(), Mockito.eq(DataSource.class), Mockito.eq(Mode.MESSAGE));
assertEquals(expectedURL, URLDecoder.decode(endpoint.getValue(), "utf-8"));
return execution;
}
use of org.teiid.translator.ExecutionContext 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;
}
use of org.teiid.translator.ExecutionContext in project teiid by teiid.
the class TestSalesForceDirectQueryExecution method testCreateFail.
@Test(expected = TranslatorException.class)
public void testCreateFail() throws Exception {
String input = "exec native('create;id=pk;type=table;attributes=one,two,three', 'one')";
TranslationUtility util = FakeTranslationFactory.getInstance().getExampleTranslationUtility();
Command command = util.parseCommand(input);
ExecutionContext ec = Mockito.mock(ExecutionContext.class);
RuntimeMetadata rm = Mockito.mock(RuntimeMetadata.class);
SalesforceConnection connection = Mockito.mock(SalesforceConnection.class);
DirectQueryExecution execution = (DirectQueryExecution) TRANSLATOR.createExecution(command, ec, rm, connection);
execution.execute();
}
use of org.teiid.translator.ExecutionContext in project teiid by teiid.
the class TestSalesForceDirectQueryExecution method testCreate.
@Test
public void testCreate() throws Exception {
String input = "exec native('create;id=pk;type=table;attributes=one,two,three', 'one', 2, 3.0)";
TranslationUtility util = FakeTranslationFactory.getInstance().getExampleTranslationUtility();
Command command = util.parseCommand(input);
ExecutionContext ec = Mockito.mock(ExecutionContext.class);
RuntimeMetadata rm = Mockito.mock(RuntimeMetadata.class);
SalesforceConnection connection = Mockito.mock(SalesforceConnection.class);
ArgumentCaptor<DataPayload> payloadArgument = ArgumentCaptor.forClass(DataPayload.class);
Mockito.stub(connection.create(payloadArgument.capture())).toReturn(23);
DirectQueryExecution execution = (DirectQueryExecution) TRANSLATOR.createExecution(command, ec, rm, connection);
execution.execute();
Mockito.verify(connection).create(payloadArgument.capture());
assertEquals("pk", payloadArgument.getValue().getID());
assertEquals("table", payloadArgument.getValue().getType());
assertEquals(3, payloadArgument.getValue().getMessageElements().size());
assertArrayEquals(new Object[] { 23 }, (Object[]) execution.next().get(0));
}
use of org.teiid.translator.ExecutionContext in project teiid by teiid.
the class TestSalesForceDirectQueryExecution method testUpdate.
@Test
public void testUpdate() throws Exception {
String input = "exec native('update;id=pk;type=table;attributes=one,two,three', 'one', 2, 3.0)";
TranslationUtility util = FakeTranslationFactory.getInstance().getExampleTranslationUtility();
Command command = util.parseCommand(input);
ExecutionContext ec = Mockito.mock(ExecutionContext.class);
RuntimeMetadata rm = Mockito.mock(RuntimeMetadata.class);
SalesforceConnection connection = Mockito.mock(SalesforceConnection.class);
ArgumentCaptor<List> payloadArgument = ArgumentCaptor.forClass(List.class);
Mockito.stub(connection.update(payloadArgument.capture())).toReturn(23);
DirectQueryExecution execution = (DirectQueryExecution) TRANSLATOR.createExecution(command, ec, rm, connection);
execution.execute();
Mockito.verify(connection).update(payloadArgument.capture());
assertEquals(1, payloadArgument.getValue().size());
assertEquals("pk", ((DataPayload) payloadArgument.getValue().get(0)).getID());
assertEquals("table", ((DataPayload) payloadArgument.getValue().get(0)).getType());
assertEquals(3, ((DataPayload) payloadArgument.getValue().get(0)).getMessageElements().size());
assertArrayEquals(new Object[] { 23 }, (Object[]) execution.next().get(0));
}
Aggregations