Search in sources :

Example 6 with DataNotAvailableException

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

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

the class TestBulkInsertExecution method testFlowAndErrorReturn.

@Test
public void testFlowAndErrorReturn() 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(false);
    Mockito.when(r3.isCreated()).thenReturn(false);
    com.sforce.async.Error error = new com.sforce.async.Error();
    error.setMessage("failed, check your data");
    error.setStatusCode(StatusCode.CANNOT_DISABLE_LAST_ADMIN);
    Mockito.when(r3.getErrors()).thenReturn(new Error[] { error });
    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, -3 }, 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) ArrayList(java.util.ArrayList) Error(com.sforce.async.Error) Insert(org.teiid.language.Insert) BatchResult(com.sforce.async.BatchResult) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) Result(com.sforce.async.Result) BatchResult(com.sforce.async.BatchResult) JobInfo(com.sforce.async.JobInfo) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) ExpressionValueSource(org.teiid.language.ExpressionValueSource) NamedTable(org.teiid.language.NamedTable) Table(org.teiid.metadata.Table) SalesForceExecutionFactory(org.teiid.translator.salesforce.SalesForceExecutionFactory) Error(com.sforce.async.Error) SalesforceConnection(org.teiid.translator.salesforce.SalesforceConnection) ExecutionContext(org.teiid.translator.ExecutionContext) Expression(org.teiid.language.Expression) Parameter(org.teiid.language.Parameter) ColumnReference(org.teiid.language.ColumnReference) Test(org.junit.Test)

Example 8 with DataNotAvailableException

use of org.teiid.translator.DataNotAvailableException 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 9 with DataNotAvailableException

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

Example 10 with DataNotAvailableException

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

the class TestUpdates method testIds.

@Test
public void testIds() throws Exception {
    Delete delete = (Delete) translationUtility.parseCommand("delete from contacts");
    SalesforceConnection connection = Mockito.mock(SalesforceConnection.class);
    SalesForceExecutionFactory config = new SalesForceExecutionFactory();
    DeleteExecutionImpl updateExecution = new DeleteExecutionImpl(config, delete, connection, Mockito.mock(RuntimeMetadata.class), Mockito.mock(ExecutionContext.class));
    ArgumentCaptor<String> queryArgument = ArgumentCaptor.forClass(String.class);
    QueryResult qr = new QueryResult();
    SObject so = new SObject();
    so.setType("Contact");
    so.addField("Id", "x");
    qr.setRecords(new SObject[] { so });
    qr.setSize(1);
    qr.setDone(true);
    Mockito.stub(connection.query(queryArgument.capture(), Mockito.anyInt(), Mockito.anyBoolean())).toReturn(qr);
    Mockito.stub(connection.delete(new String[] { "x" })).toReturn(1);
    while (true) {
        try {
            updateExecution.execute();
            org.junit.Assert.assertArrayEquals(new int[] { 1 }, updateExecution.getUpdateCounts());
            break;
        } catch (DataNotAvailableException e) {
            continue;
        }
    }
    Mockito.verify(connection, Mockito.times(1)).query(queryArgument.capture(), Mockito.anyInt(), Mockito.anyBoolean());
    String query = queryArgument.getValue();
    assertEquals("SELECT Id FROM Contact ", query);
}
Also used : Delete(org.teiid.language.Delete) QueryResult(com.sforce.soap.partner.QueryResult) ExecutionContext(org.teiid.translator.ExecutionContext) SalesForceExecutionFactory(org.teiid.translator.salesforce.SalesForceExecutionFactory) SObject(com.sforce.soap.partner.sobject.SObject) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) SalesforceConnection(org.teiid.translator.salesforce.SalesforceConnection) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) Test(org.junit.Test)

Aggregations

DataNotAvailableException (org.teiid.translator.DataNotAvailableException)16 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)10 ExecutionContext (org.teiid.translator.ExecutionContext)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 Test (org.junit.Test)9 TranslatorException (org.teiid.translator.TranslatorException)9 ResultSetExecution (org.teiid.translator.ResultSetExecution)5 SalesForceExecutionFactory (org.teiid.translator.salesforce.SalesForceExecutionFactory)5 SalesforceConnection (org.teiid.translator.salesforce.SalesforceConnection)5 IOException (java.io.IOException)4 Table (org.teiid.metadata.Table)4 JobInfo (com.sforce.async.JobInfo)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)3 ColumnReference (org.teiid.language.ColumnReference)3 Command (org.teiid.language.Command)3 Expression (org.teiid.language.Expression)3 ExpressionValueSource (org.teiid.language.ExpressionValueSource)3 Insert (org.teiid.language.Insert)3