Search in sources :

Example 21 with ExecutionContext

use of org.teiid.translator.ExecutionContext 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 22 with ExecutionContext

use of org.teiid.translator.ExecutionContext in project teiid by teiid.

the class TestQueryExecutionImpl method testBulkFlow.

@Test
public void testBulkFlow() throws Exception {
    // $NON-NLS-1$
    Select command = (Select) translationUtility.parseCommand("select Name from Account");
    SalesforceConnection connection = Mockito.mock(SalesforceConnection.class);
    JobInfo jobInfo = Mockito.mock(JobInfo.class);
    Mockito.when(connection.createBulkJob(Mockito.anyString(), Mockito.eq(OperationEnum.query), Mockito.eq(true))).thenReturn(jobInfo);
    final BatchResultInfo info = new BatchResultInfo("x");
    Mockito.when(connection.getBatchQueryResults(Mockito.anyString(), Mockito.eq(info))).thenAnswer(new Answer<BulkBatchResult>() {

        boolean first = true;

        @Override
        public BulkBatchResult answer(InvocationOnMock invocation) throws Throwable {
            if (first) {
                first = false;
                throw new DataNotAvailableException();
            }
            if (info.getAndIncrementResultNum() == 0) {
                final Iterator<List<String>> i = Arrays.asList(Arrays.asList("Name"), Arrays.asList("X")).iterator();
                return new BulkBatchResult() {

                    @Override
                    public List<String> nextRecord() throws IOException {
                        if (!i.hasNext()) {
                            return null;
                        }
                        return i.next();
                    }

                    @Override
                    public void close() {
                    }
                };
            }
            return null;
        }
    });
    Mockito.when(connection.addBatch("SELECT Account.Name FROM Account", jobInfo)).thenReturn(info);
    ExecutionContext mock = Mockito.mock(ExecutionContext.class);
    Mockito.stub(mock.getSourceHints()).toReturn(Arrays.asList("bulk"));
    QueryExecutionImpl execution = new QueryExecutionImpl(command, connection, Mockito.mock(RuntimeMetadata.class), mock, new SalesForceExecutionFactory());
    execution.execute();
    try {
        execution.next();
        fail();
    } catch (DataNotAvailableException e) {
    }
    List<?> row = execution.next();
    assertEquals(Arrays.asList("X"), row);
    assertNull(execution.next());
}
Also used : SalesForceExecutionFactory(org.teiid.translator.salesforce.SalesForceExecutionFactory) BatchResultInfo(org.teiid.translator.salesforce.SalesforceConnection.BatchResultInfo) IOException(java.io.IOException) SalesforceConnection(org.teiid.translator.salesforce.SalesforceConnection) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ExecutionContext(org.teiid.translator.ExecutionContext) JobInfo(com.sforce.async.JobInfo) BulkBatchResult(org.teiid.translator.salesforce.SalesforceConnection.BulkBatchResult) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Select(org.teiid.language.Select) Iterator(java.util.Iterator) List(java.util.List) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) Test(org.junit.Test)

Example 23 with ExecutionContext

use of org.teiid.translator.ExecutionContext 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)

Example 24 with ExecutionContext

use of org.teiid.translator.ExecutionContext in project teiid by teiid.

the class TestSalesForceDirectQueryExecution 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);
    SalesforceConnection connection = Mockito.mock(SalesforceConnection.class);
    QueryResult qr = Mockito.mock(QueryResult.class);
    Mockito.stub(qr.isDone()).toReturn(true);
    SObject[] results = new SObject[1];
    SObject s = new SObject();
    s.setType("Account");
    s.setId("The ID");
    results[0] = s;
    s.addField("Type", "The Type");
    s.addField("Name", "The Name");
    Mockito.stub(qr.getRecords()).toReturn(results);
    Mockito.stub(connection.query("SELECT Account.Id, Account.Type, Account.Name FROM Account", 0, false)).toReturn(qr);
    DirectQueryExecution execution = (DirectQueryExecution) TRANSLATOR.createExecution(command, ec, rm, connection);
    execution.execute();
    Mockito.verify(connection, Mockito.times(1)).query("SELECT Account.Id, Account.Type, Account.Name FROM Account", 0, false);
    assertArrayEquals(new Object[] { "The ID", "The Type", "The Name" }, (Object[]) execution.next().get(0));
}
Also used : QueryResult(com.sforce.soap.partner.QueryResult) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) TranslationUtility(org.teiid.cdk.api.TranslationUtility) SObject(com.sforce.soap.partner.sobject.SObject) SObject(com.sforce.soap.partner.sobject.SObject) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) SalesforceConnection(org.teiid.translator.salesforce.SalesforceConnection) Test(org.junit.Test)

Example 25 with ExecutionContext

use of org.teiid.translator.ExecutionContext in project teiid by teiid.

the class TestSalesForceDirectQueryExecution method testDelete.

@Test
public void testDelete() throws Exception {
    String input = "exec native('delete;', 'id1','id2')";
    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<String[]> payloadArgument = ArgumentCaptor.forClass(String[].class);
    Mockito.stub(connection.delete(payloadArgument.capture())).toReturn(23);
    DirectQueryExecution execution = (DirectQueryExecution) TRANSLATOR.createExecution(command, ec, rm, connection);
    execution.execute();
    Mockito.verify(connection, Mockito.times(1)).delete(payloadArgument.capture());
    assertEquals("id1", payloadArgument.getValue()[0]);
    assertEquals("id2", payloadArgument.getValue()[1]);
    assertArrayEquals(new Object[] { 23 }, (Object[]) execution.next().get(0));
}
Also used : ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) TranslationUtility(org.teiid.cdk.api.TranslationUtility) SObject(com.sforce.soap.partner.sobject.SObject) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) SalesforceConnection(org.teiid.translator.salesforce.SalesforceConnection) Test(org.junit.Test)

Aggregations

ExecutionContext (org.teiid.translator.ExecutionContext)65 Test (org.junit.Test)50 Command (org.teiid.language.Command)49 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)48 TranslationUtility (org.teiid.cdk.api.TranslationUtility)32 ResultSetExecution (org.teiid.translator.ResultSetExecution)23 List (java.util.List)17 UpdateExecution (org.teiid.translator.UpdateExecution)14 TranslatorException (org.teiid.translator.TranslatorException)11 ArrayList (java.util.ArrayList)10 QueryExpression (org.teiid.language.QueryExpression)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 LdapContext (javax.naming.ldap.LdapContext)9 ExecutionFactory (org.teiid.translator.ExecutionFactory)9 SalesforceConnection (org.teiid.translator.salesforce.SalesforceConnection)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 HashMap (java.util.HashMap)6 DataSource (javax.activation.DataSource)6 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)6 WSConnection (org.teiid.translator.WSConnection)6