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());
}
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());
}
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());
}
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));
}
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);
}
Aggregations