use of org.teiid.language.Command in project teiid by teiid.
the class TestOlapTranslator method testCannedProcedure.
@Test
public void testCannedProcedure() throws Exception {
String ddl = "create foreign procedure proc(arg integer, arg1 date) returns table (x string) options (\"teiid_rel:native-query\" '$2 $1 something')";
String query = "exec proc(2, {d'1970-01-01'})";
TransformationMetadata tm = RealMetadataFactory.fromDDL(ddl, "x", "phy");
CommandBuilder commandBuilder = new CommandBuilder(tm);
Command obj = commandBuilder.getCommand(query);
OlapExecutionFactory oef = new OlapExecutionFactory();
Connection mock = Mockito.mock(java.sql.Connection.class);
OlapWrapper mock2 = Mockito.mock(OlapWrapper.class);
OlapConnection mock3 = Mockito.mock(OlapConnection.class);
OlapStatement mock4 = Mockito.mock(OlapStatement.class);
Mockito.stub(mock4.executeOlapQuery(Mockito.anyString())).toThrow(new TeiidRuntimeException());
Mockito.stub(mock3.createStatement()).toReturn(mock4);
Mockito.stub(mock2.unwrap(OlapConnection.class)).toReturn(mock3);
Mockito.stub(mock.unwrap(OlapWrapper.class)).toReturn(mock2);
ProcedureExecution pe = oef.createProcedureExecution((Call) obj, Mockito.mock(ExecutionContext.class), new RuntimeMetadataImpl(tm), mock);
try {
pe.execute();
fail();
} catch (TeiidRuntimeException e) {
Mockito.verify(mock4).executeOlapQuery("'1970-01-01' 2 something");
}
}
use of org.teiid.language.Command in project teiid by teiid.
the class TestJPADirectQueryExecution method testSearch.
@Test
public void testSearch() throws Exception {
String input = "exec native('search;SELECT Account.Id, Account.Type, Account.Name FROM Account')";
TranslationUtility util = FakeTranslationFactory.getInstance().getExampleTranslationUtility();
Command command = util.parseCommand(input);
ExecutionContext ec = Mockito.mock(ExecutionContext.class);
RuntimeMetadata rm = Mockito.mock(RuntimeMetadata.class);
EntityManager connection = Mockito.mock(EntityManager.class);
Query query = Mockito.mock(Query.class);
Mockito.stub(connection.createQuery("SELECT Account.Id, Account.Type, Account.Name FROM Account")).toReturn(query);
JPQLDirectQueryExecution execution = (JPQLDirectQueryExecution) TRANSLATOR.createExecution(command, ec, rm, connection);
execution.execute();
Mockito.verify(connection, Mockito.times(1)).createQuery("SELECT Account.Id, Account.Type, Account.Name FROM Account");
}
use of org.teiid.language.Command in project teiid by teiid.
the class TestSwaggerQueryExecution method helpProcedureExecute.
private ProcedureExecution helpProcedureExecute(String query, final String resultJson, String expectedURL, int responseCode, boolean decode, String expectedMethod, String expectedInput, Map<String, Object> userHeaders) throws Exception {
userHeaders.put(MessageContext.HTTP_REQUEST_HEADERS, new HashMap<String, List<String>>());
userHeaders.put(WSConnection.STATUS_CODE, new Integer(responseCode));
userHeaders.put("Content-Type", Arrays.asList("application/json"));
SwaggerExecutionFactory translator = new SwaggerExecutionFactory();
translator.start();
TranslationUtility utility = new TranslationUtility(TestSwaggerMetadataProcessor.getTransformationMetadata(TestSwaggerMetadataProcessor.petstoreMetadata(translator), translator));
Command cmd = utility.parseCommand(query);
ExecutionContext context = Mockito.mock(ExecutionContext.class);
WSConnection connection = Mockito.mock(WSConnection.class);
Dispatch<DataSource> dispatch = Mockito.mock(Dispatch.class);
Mockito.stub(dispatch.getRequestContext()).toReturn(userHeaders);
Mockito.stub(dispatch.getResponseContext()).toReturn(userHeaders);
Mockito.stub(connection.createDispatch(Mockito.eq(HTTPBinding.HTTP_BINDING), Mockito.anyString(), Mockito.eq(DataSource.class), Mockito.eq(Mode.MESSAGE))).toReturn(dispatch);
DataSource outputDS = 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(resultJson.getBytes());
return in;
}
@Override
public String getContentType() {
return "application/json";
}
};
Mockito.stub(dispatch.invoke(Mockito.any(DataSource.class))).toReturn(outputDS);
ProcedureExecution execution = translator.createProcedureExecution((Call) cmd, context, utility.createRuntimeMetadata(), connection);
execution.execute();
ArgumentCaptor<String> endpoint = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> binding = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<DataSource> input = ArgumentCaptor.forClass(DataSource.class);
Mockito.verify(connection).createDispatch(binding.capture(), endpoint.capture(), Mockito.eq(DataSource.class), Mockito.eq(Mode.MESSAGE));
Mockito.verify(dispatch).invoke(input.capture());
assertEquals(expectedURL, decode ? URLDecoder.decode(endpoint.getValue(), "utf-8") : endpoint.getValue());
assertEquals(expectedMethod, dispatch.getRequestContext().get(MessageContext.HTTP_REQUEST_METHOD));
if (expectedInput != null) {
assertEquals(expectedInput, ObjectConverterUtil.convertToString(input.getValue().getInputStream()));
}
return execution;
}
use of org.teiid.language.Command in project teiid by teiid.
the class TestSolrUpdateExecution method helpUpdate.
private UpdateRequest helpUpdate(String query, QueryResponse... responseDocs) throws TranslatorException {
Command cmd = this.utility.parseCommand(query);
ExecutionContext context = Mockito.mock(ExecutionContext.class);
Mockito.stub(context.getCommandContext()).toReturn(Mockito.mock(CommandContext.class));
SolrConnection connection = Mockito.mock(SolrConnection.class);
UpdateResponse response = Mockito.mock(UpdateResponse.class);
ArgumentCaptor<UpdateRequest> argument = ArgumentCaptor.forClass(UpdateRequest.class);
Mockito.when(connection.query(Mockito.any(SolrQuery.class))).thenReturn(responseDocs[0], responseDocs[1]);
Mockito.stub(connection.update(Mockito.any(UpdateRequest.class))).toReturn(response);
UpdateExecution execution = this.translator.createUpdateExecution(cmd, context, this.utility.createRuntimeMetadata(), connection);
execution.execute();
Mockito.verify(connection).update(argument.capture());
return argument.getValue();
}
use of org.teiid.language.Command in project teiid by teiid.
the class TestSalesForceDirectQueryExecution method testWithoutMarker.
@Test(expected = TranslatorException.class)
public void testWithoutMarker() throws Exception {
String input = "exec native('salesforce query')";
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();
}
Aggregations