Search in sources :

Example 11 with SalesforceConnection

use of org.teiid.translator.salesforce.SalesforceConnection in project teiid by teiid.

the class TestBulkInsertExecution method testFlowAndInvocationStack.

@Test
public void testFlowAndInvocationStack() throws Exception {
    NamedTable table = new NamedTable("temp", null, Mockito.mock(Table.class));
    ArrayList<ColumnReference> elements = new ArrayList<ColumnReference>();
    elements.add(new ColumnReference(table, "one", Mockito.mock(Column.class), Integer.class));
    elements.add(new ColumnReference(table, "two", Mockito.mock(Column.class), String.class));
    List<Expression> values = new ArrayList<Expression>();
    Parameter param = new Parameter();
    param.setType(DataTypeManager.DefaultDataClasses.INTEGER);
    param.setValueIndex(0);
    values.add(param);
    param = new Parameter();
    param.setType(DataTypeManager.DefaultDataClasses.STRING);
    param.setValueIndex(1);
    values.add(param);
    ExpressionValueSource valueSource = new ExpressionValueSource(values);
    Insert insert = new Insert(table, elements, valueSource);
    insert.setParameterValues(Arrays.asList(Arrays.asList(2, '2'), Arrays.asList(2, '2'), Arrays.asList(3, '3')).iterator());
    Result r1 = Mockito.mock(Result.class);
    Result r2 = Mockito.mock(Result.class);
    Result r3 = Mockito.mock(Result.class);
    Mockito.when(r1.isSuccess()).thenReturn(true);
    Mockito.when(r1.isCreated()).thenReturn(true);
    Mockito.when(r2.isSuccess()).thenReturn(true);
    Mockito.when(r2.isCreated()).thenReturn(true);
    Mockito.when(r3.isSuccess()).thenReturn(true);
    Mockito.when(r3.isCreated()).thenReturn(true);
    BatchResult batchResult = Mockito.mock(BatchResult.class);
    Mockito.when(batchResult.getResult()).thenReturn(new Result[] { r1 }).thenReturn((new Result[] { r2 })).thenReturn(new Result[] { r3 });
    SalesforceConnection connection = Mockito.mock(SalesforceConnection.class);
    JobInfo jobInfo = Mockito.mock(JobInfo.class);
    Mockito.when(connection.createBulkJob(Mockito.anyString(), Mockito.eq(OperationEnum.insert), Mockito.eq(false))).thenReturn(jobInfo);
    Mockito.when(connection.getBulkResults(Mockito.any(JobInfo.class), Mockito.anyList())).thenReturn(new BatchResult[] { batchResult, batchResult, batchResult });
    SalesForceExecutionFactory config = new SalesForceExecutionFactory();
    config.setMaxBulkInsertBatchSize(1);
    InsertExecutionImpl updateExecution = new InsertExecutionImpl(config, insert, connection, Mockito.mock(RuntimeMetadata.class), Mockito.mock(ExecutionContext.class));
    while (true) {
        try {
            updateExecution.execute();
            org.junit.Assert.assertArrayEquals(new int[] { 1, 1, 1 }, updateExecution.getUpdateCounts());
            break;
        } catch (DataNotAvailableException e) {
            continue;
        }
    }
    Mockito.verify(connection, Mockito.times(1)).createBulkJob(Mockito.anyString(), Mockito.eq(OperationEnum.insert), Mockito.eq(false));
    Mockito.verify(connection, Mockito.times(1)).getBulkResults(Mockito.any(JobInfo.class), Mockito.anyList());
}
Also used : NamedTable(org.teiid.language.NamedTable) NamedTable(org.teiid.language.NamedTable) Table(org.teiid.metadata.Table) SalesForceExecutionFactory(org.teiid.translator.salesforce.SalesForceExecutionFactory) ArrayList(java.util.ArrayList) Insert(org.teiid.language.Insert) BatchResult(com.sforce.async.BatchResult) SalesforceConnection(org.teiid.translator.salesforce.SalesforceConnection) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) Result(com.sforce.async.Result) BatchResult(com.sforce.async.BatchResult) ExecutionContext(org.teiid.translator.ExecutionContext) Expression(org.teiid.language.Expression) JobInfo(com.sforce.async.JobInfo) Parameter(org.teiid.language.Parameter) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) ColumnReference(org.teiid.language.ColumnReference) ExpressionValueSource(org.teiid.language.ExpressionValueSource) Test(org.junit.Test)

Example 12 with SalesforceConnection

use of org.teiid.translator.salesforce.SalesforceConnection 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();
}
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 13 with SalesforceConnection

use of org.teiid.translator.salesforce.SalesforceConnection 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));
}
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)

Example 14 with SalesforceConnection

use of org.teiid.translator.salesforce.SalesforceConnection 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));
}
Also used : ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) TranslationUtility(org.teiid.cdk.api.TranslationUtility) List(java.util.List) SObject(com.sforce.soap.partner.sobject.SObject) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) SalesforceConnection(org.teiid.translator.salesforce.SalesforceConnection) Test(org.junit.Test)

Example 15 with SalesforceConnection

use of org.teiid.translator.salesforce.SalesforceConnection in project teiid by teiid.

the class TestSingleInsert method testDateTypes.

@Test
public void testDateTypes() throws Exception {
    NamedTable table = new NamedTable("temp", null, Mockito.mock(Table.class));
    ArrayList<ColumnReference> elements = new ArrayList<ColumnReference>();
    elements.add(new ColumnReference(table, "one", Mockito.mock(Column.class), Integer.class));
    elements.add(new ColumnReference(table, "two", Mockito.mock(Column.class), Date.class));
    elements.add(new ColumnReference(table, "three", Mockito.mock(Column.class), Timestamp.class));
    List<Expression> values = new ArrayList<Expression>();
    values.add(new Literal(1, DataTypeManager.DefaultDataClasses.INTEGER));
    values.add(new Literal(TimestampUtil.createDate(100, 01, 1), DataTypeManager.DefaultDataClasses.DATE));
    values.add(new Literal(TimestampUtil.createTimestamp(100, 01, 1, 0, 4, 0, 0), DataTypeManager.DefaultDataClasses.TIMESTAMP));
    ExpressionValueSource valueSource = new ExpressionValueSource(values);
    Insert insert = new Insert(table, elements, valueSource);
    SalesforceConnection connection = Mockito.mock(SalesforceConnection.class);
    Mockito.stub(connection.create(Mockito.any(DataPayload.class))).toAnswer(new Answer<Integer>() {

        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            DataPayload payload = (DataPayload) invocation.getArguments()[0];
            List<DataPayload.Field> fields = payload.getMessageElements();
            assertEquals(3, fields.size());
            assertEquals(1, fields.get(0).value);
            assertEquals(TimestampUtil.createDate(100, 01, 1), fields.get(1).value);
            Calendar cal = (Calendar) fields.get(2).value;
            assertEquals(TimeZone.getTimeZone("GMT-1"), cal.getTimeZone());
            return 1;
        }
    });
    Mockito.stub(connection.upsert(Mockito.any(DataPayload.class))).toReturn(1);
    SalesForceExecutionFactory config = new SalesForceExecutionFactory();
    config.setMaxBulkInsertBatchSize(1);
    InsertExecutionImpl updateExecution = new InsertExecutionImpl(config, insert, connection, Mockito.mock(RuntimeMetadata.class), Mockito.mock(ExecutionContext.class));
    while (true) {
        try {
            updateExecution.execute();
            org.junit.Assert.assertArrayEquals(new int[] { 1 }, updateExecution.getUpdateCounts());
            break;
        } catch (DataNotAvailableException e) {
            continue;
        }
    }
    insert.setUpsert(true);
    updateExecution = new InsertExecutionImpl(config, insert, connection, Mockito.mock(RuntimeMetadata.class), Mockito.mock(ExecutionContext.class));
    while (true) {
        try {
            updateExecution.execute();
            org.junit.Assert.assertArrayEquals(new int[] { 1 }, updateExecution.getUpdateCounts());
            break;
        } catch (DataNotAvailableException e) {
            continue;
        }
    }
    Mockito.verify(connection).upsert(Mockito.any(DataPayload.class));
}
Also used : NamedTable(org.teiid.language.NamedTable) ArrayList(java.util.ArrayList) Insert(org.teiid.language.Insert) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) Timestamp(java.sql.Timestamp) Literal(org.teiid.language.Literal) ArrayList(java.util.ArrayList) List(java.util.List) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) ExpressionValueSource(org.teiid.language.ExpressionValueSource) NamedTable(org.teiid.language.NamedTable) Table(org.teiid.metadata.Table) Calendar(java.util.Calendar) SalesForceExecutionFactory(org.teiid.translator.salesforce.SalesForceExecutionFactory) SalesforceConnection(org.teiid.translator.salesforce.SalesforceConnection) Date(java.sql.Date) ExecutionContext(org.teiid.translator.ExecutionContext) Expression(org.teiid.language.Expression) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ColumnReference(org.teiid.language.ColumnReference) Test(org.junit.Test)

Aggregations

ExecutionContext (org.teiid.translator.ExecutionContext)17 SalesforceConnection (org.teiid.translator.salesforce.SalesforceConnection)17 Test (org.junit.Test)16 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)16 SalesForceExecutionFactory (org.teiid.translator.salesforce.SalesForceExecutionFactory)10 SObject (com.sforce.soap.partner.sobject.SObject)7 Command (org.teiid.language.Command)7 TranslationUtility (org.teiid.cdk.api.TranslationUtility)6 QueryResult (com.sforce.soap.partner.QueryResult)5 Select (org.teiid.language.Select)5 DataNotAvailableException (org.teiid.translator.DataNotAvailableException)5 JobInfo (com.sforce.async.JobInfo)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ColumnReference (org.teiid.language.ColumnReference)3 Expression (org.teiid.language.Expression)3 ExpressionValueSource (org.teiid.language.ExpressionValueSource)3 Insert (org.teiid.language.Insert)3 NamedTable (org.teiid.language.NamedTable)3 Table (org.teiid.metadata.Table)3