Search in sources :

Example 41 with Command

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");
    }
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) OlapConnection(org.olap4j.OlapConnection) Connection(java.sql.Connection) OlapConnection(org.olap4j.OlapConnection) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) OlapStatement(org.olap4j.OlapStatement) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) OlapWrapper(org.olap4j.OlapWrapper) ProcedureExecution(org.teiid.translator.ProcedureExecution) RuntimeMetadataImpl(org.teiid.dqp.internal.datamgr.RuntimeMetadataImpl) CommandBuilder(org.teiid.cdk.CommandBuilder) Test(org.junit.Test)

Example 42 with Command

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");
}
Also used : EntityManager(javax.persistence.EntityManager) ExecutionContext(org.teiid.translator.ExecutionContext) Query(javax.persistence.Query) Command(org.teiid.language.Command) TranslationUtility(org.teiid.cdk.api.TranslationUtility) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) Test(org.junit.Test)

Example 43 with Command

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;
}
Also used : WSConnection(org.teiid.translator.WSConnection) TranslationUtility(org.teiid.cdk.api.TranslationUtility) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataSource(javax.activation.DataSource) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) ByteArrayInputStream(java.io.ByteArrayInputStream) ProcedureExecution(org.teiid.translator.ProcedureExecution) List(java.util.List)

Example 44 with Command

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();
}
Also used : UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) ExecutionContext(org.teiid.translator.ExecutionContext) CommandContext(org.teiid.CommandContext) Command(org.teiid.language.Command) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) UpdateExecution(org.teiid.translator.UpdateExecution) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Example 45 with Command

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();
}
Also used : ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) TranslationUtility(org.teiid.cdk.api.TranslationUtility) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) SalesforceConnection(org.teiid.translator.salesforce.SalesforceConnection) Test(org.junit.Test)

Aggregations

Command (org.teiid.language.Command)136 Test (org.junit.Test)99 ExecutionContext (org.teiid.translator.ExecutionContext)62 TranslationUtility (org.teiid.cdk.api.TranslationUtility)52 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)40 CommandBuilder (org.teiid.cdk.CommandBuilder)25 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)20 ResultSetExecution (org.teiid.translator.ResultSetExecution)18 TranslatedCommand (org.teiid.translator.jdbc.TranslatedCommand)17 UpdateExecution (org.teiid.translator.UpdateExecution)16 SimpleDBSQLVisitor (org.teiid.translator.simpledb.SimpleDBSQLVisitor)14 List (java.util.List)11 Connection (java.sql.Connection)10 LdapContext (javax.naming.ldap.LdapContext)9 Range (org.apache.accumulo.core.data.Range)9 SQLConversionVisitor (org.teiid.translator.jdbc.SQLConversionVisitor)9 MetadataFactory (org.teiid.metadata.MetadataFactory)8 ArrayList (java.util.ArrayList)7 FunctionTree (org.teiid.query.function.FunctionTree)7 UDFSource (org.teiid.query.function.UDFSource)7