use of org.teiid.translator.UpdateExecution in project teiid by teiid.
the class TestODataUpdateExecution method testInsertNavigation.
@Test
public void testInsertNavigation() throws Exception {
String query = "INSERT INTO People_Friends(UserName, FirstName, LastName, People_UserName) " + "VALUES('jdoe', 'John', 'Doe', 'russel')";
String expectedURL = "People('russel')/Friends";
String returnResponse = "{\n" + " \"UserName\":\"jdoe\",\n" + " \"FirstName\":\"John\",\n" + " \"LastName\":\"Doe\"\n" + "}";
String expectedPayload = "{\"@odata.type\":\"#Microsoft.OData.SampleService.Models.TripPin.Person\"," + "\"UserName@odata.type\":\"String\",\"UserName\":\"jdoe\"," + "\"FirstName@odata.type\":\"String\",\"FirstName\":\"John\"," + "\"LastName@odata.type\":\"String\",\"LastName\":\"Doe\"}";
UpdateExecution excution = helpExecute(TestODataMetadataProcessor.tripPinMetadata(), query, expectedPayload, returnResponse, expectedURL, "POST", 201);
}
use of org.teiid.translator.UpdateExecution in project teiid by teiid.
the class TestODataUpdateExecution method testInsertEntitySet.
@Test
public void testInsertEntitySet() throws Exception {
String query = "INSERT INTO People(UserName,FirstName,LastName, EMails, Gender, Concurrency) " + "values ('jdoe', 'John', 'Doe', ('jdoe@cantfind.ws',), 'Male', 1234)";
String expectedURL = "People";
String returnResponse = "{\n" + " \"UserName\":\"russellwhyte\",\n" + " \"FirstName\":\"Russell\",\n" + " \"LastName\":\"Whyte\"\n" + "}";
String expectedPayload = "{\"@odata.type\":\"#Microsoft.OData.SampleService.Models.TripPin.Person\"," + "\"UserName@odata.type\":\"String\",\"UserName\":\"jdoe\"," + "\"FirstName@odata.type\":\"String\",\"FirstName\":\"John\"," + "\"LastName@odata.type\":\"String\",\"LastName\":\"Doe\"," + "\"Emails@odata.type\":\"#Collection(String)\",\"Emails\":[\"jdoe@cantfind.ws\"]," + "\"Gender@odata.type\":\"#Microsoft.OData.SampleService.Models.TripPin.PersonGender\"," + "\"Gender\":\"Male\"," + "\"Concurrency@odata.type\":\"Int64\",\"Concurrency\":1234}";
UpdateExecution excution = helpExecute(TestODataMetadataProcessor.tripPinMetadata(), query, expectedPayload, returnResponse, expectedURL, "POST", 201);
}
use of org.teiid.translator.UpdateExecution in project teiid by teiid.
the class TestODataUpdateExecution method helpExecute.
private UpdateExecution helpExecute(MetadataFactory mf, String query, String expectedPayload, final String resultJson, String expectedURL, String expectedMethod, int responseCode) throws Exception {
ODataExecutionFactory translator = new ODataExecutionFactory();
translator.start();
TranslationUtility utility = new TranslationUtility(TestODataMetadataProcessor.getTransformationMetadata(mf, translator));
Command cmd = utility.parseCommand(query);
ExecutionContext context = Mockito.mock(ExecutionContext.class);
WSConnection connection = Mockito.mock(WSConnection.class);
Map<String, Object> headers = new HashMap<String, Object>();
headers.put(MessageContext.HTTP_REQUEST_HEADERS, new HashMap<String, List<String>>());
headers.put(WSConnection.STATUS_CODE, new Integer(responseCode));
Dispatch<DataSource> dispatch = Mockito.mock(Dispatch.class);
Mockito.stub(dispatch.getRequestContext()).toReturn(headers);
Mockito.stub(dispatch.getResponseContext()).toReturn(headers);
Mockito.stub(connection.createDispatch(Mockito.eq(HTTPBinding.HTTP_BINDING), Mockito.anyString(), Mockito.eq(DataSource.class), Mockito.eq(Mode.MESSAGE))).toReturn(dispatch);
DataSource ds = 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";
}
};
ArgumentCaptor<DataSource> data = ArgumentCaptor.forClass(DataSource.class);
Mockito.stub(dispatch.invoke(data.capture())).toReturn(ds);
UpdateExecution execution = translator.createUpdateExecution(cmd, context, utility.createRuntimeMetadata(), connection);
execution.execute();
ArgumentCaptor<String> endpoint = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> binding = ArgumentCaptor.forClass(String.class);
Mockito.verify(connection).createDispatch(binding.capture(), endpoint.capture(), Mockito.eq(DataSource.class), Mockito.eq(Mode.MESSAGE));
assertEquals(expectedURL, URLDecoder.decode(endpoint.getValue(), "utf-8"));
String payload = new String(ObjectConverterUtil.convertToByteArray(((ClobInputStreamFactory) data.getValue()).getInputStream()));
assertEquals(expectedPayload, payload);
assertEquals(expectedMethod, dispatch.getRequestContext().get(MessageContext.HTTP_REQUEST_METHOD));
return execution;
}
use of org.teiid.translator.UpdateExecution in project teiid by teiid.
the class TestUpdates method testBatchedUpdate.
@Test
public void testBatchedUpdate() throws TranslatorException {
CassandraExecutionFactory cef = new CassandraExecutionFactory();
String input = "insert into pm1.g1 (e1) values ('a')";
TranslationUtility util = FakeTranslationFactory.getInstance().getExampleTranslationUtility();
Command command = util.parseCommand(input);
Command command1 = util.parseCommand("update pm1.g1 set e1 = 'b'");
command = new BatchedUpdates(Arrays.asList(command, command1));
ExecutionContext ec = Mockito.mock(ExecutionContext.class);
RuntimeMetadata rm = Mockito.mock(RuntimeMetadata.class);
CassandraConnection connection = Mockito.mock(CassandraConnection.class);
ResultSetFuture rsf = Mockito.mock(ResultSetFuture.class);
Mockito.stub(rsf.isDone()).toReturn(true);
Mockito.stub(connection.executeBatch(Arrays.asList("INSERT INTO g1 (e1) VALUES ('a')", "UPDATE g1 SET e1 = 'b'"))).toReturn(rsf);
UpdateExecution execution = (UpdateExecution) cef.createExecution(command, ec, rm, connection);
execution.execute();
assertArrayEquals(new int[] { 2 }, execution.getUpdateCounts());
Mockito.verify(connection).executeBatch(Arrays.asList("INSERT INTO g1 (e1) VALUES ('a')", "UPDATE g1 SET e1 = 'b'"));
}
use of org.teiid.translator.UpdateExecution in project teiid by teiid.
the class TestSimpleDBExecution method testDelete.
@Test
public void testDelete() throws Exception {
String query = "delete from item where somedate = {ts '2014-04-04 10:50:45'}";
Mockito.stub(connection.performDelete(Mockito.anyString(), Mockito.anyString())).toReturn(100);
Command cmd = utility.parseCommand(query);
ExecutionContext context = Mockito.mock(ExecutionContext.class);
UpdateExecution exec = translator.createUpdateExecution(cmd, context, Mockito.mock(RuntimeMetadata.class), connection);
exec.execute();
Mockito.verify(connection).performDelete("item", "SELECT itemName() FROM item WHERE somedate = '2014-04-04 10:50:45.0'");
}
Aggregations