Search in sources :

Example 6 with UpdateExecution

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

the class TestODataUpdateExecution method helpExecute.

private String helpExecute(String query, final String resultXML, String expectedURL, int[] responseCode, TransformationMetadata metadata, int times) throws Exception {
    ODataExecutionFactory translator = new ODataExecutionFactory();
    translator.start();
    TranslationUtility utility = new TranslationUtility(metadata);
    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[0]));
    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(resultXML.getBytes());
            return in;
        }

        @Override
        public String getContentType() {
            return "application/xml";
        }
    };
    ArgumentCaptor<DataSource> payload = ArgumentCaptor.forClass(DataSource.class);
    Mockito.stub(dispatch.invoke(payload.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, Mockito.times(times)).createDispatch(binding.capture(), endpoint.capture(), Mockito.eq(DataSource.class), Mockito.eq(Mode.MESSAGE));
    Mockito.verify(dispatch, Mockito.times(times)).invoke(payload.capture());
    assertEquals(expectedURL, URLDecoder.decode(endpoint.getValue(), "utf-8"));
    if (payload.getAllValues() != null) {
        List<DataSource> listDS = payload.getAllValues();
        InputStream in = null;
        if (times > 1) {
            in = listDS.get(1).getInputStream();
        } else {
            in = listDS.get(0).getInputStream();
        }
        return new String(ObjectConverterUtil.convertToByteArray(in));
    }
    return "";
}
Also used : WSConnection(org.teiid.translator.WSConnection) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TranslationUtility(org.teiid.cdk.api.TranslationUtility) UpdateExecution(org.teiid.translator.UpdateExecution) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataSource(javax.activation.DataSource) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List)

Example 7 with UpdateExecution

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

the class TestODataUpdateExecution method testInsertComplexTypeTripPin.

@Test
public void testInsertComplexTypeTripPin() throws Exception {
    String query = "INSERT INTO People_AddressInfo(Address, People_UserName) " + "VALUES('sesame street', 'russel')";
    String expectedURL = "People('russel')/AddressInfo";
    String returnResponse = "{\n" + "   \"Address\":\"russellwhyte\",\n" + "   \"FirstName\":\"Russell\",\n" + "   \"LastName\":\"Whyte\"\n" + "}";
    String expectedPayload = "{\"@odata.type\":\"#Microsoft.OData.SampleService.Models.TripPin.Location\"," + "\"value\":[{\"@odata.type\":\"#Microsoft.OData.SampleService.Models.TripPin.Location\"," + "\"Address@odata.type\":\"String\"," + "\"Address\":\"sesame street\"}]}";
    // collection needs PUT
    UpdateExecution excution = helpExecute(TestODataMetadataProcessor.tripPinMetadata(), query, expectedPayload, returnResponse, expectedURL, "PUT", 201);
}
Also used : UpdateExecution(org.teiid.translator.UpdateExecution) Test(org.junit.Test)

Example 8 with UpdateExecution

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

the class TestODataUpdateExecution method testInsertComplexType.

@Test
public void testInsertComplexType() throws Exception {
    String query = "INSERT INTO Persons_address(street, city, state, Persons_ssn) " + "VALUES('sesame street', 'Newyork', 'NY', 1234)";
    String expectedURL = "Persons(1234)/address";
    String returnResponse = "{\n" + "   \"UserName\":\"russellwhyte\",\n" + "   \"FirstName\":\"Russell\",\n" + "   \"LastName\":\"Whyte\"\n" + "}";
    String expectedPayload = "{\"@odata.type\":\"#Edm.Address\"," + "\"street@odata.type\":\"String\",\"street\":\"sesame street\"," + "\"city@odata.type\":\"String\",\"city\":\"Newyork\"," + "\"state@odata.type\":\"String\",\"state\":\"NY\"}";
    // single complex requires PATCH
    UpdateExecution excution = helpExecute(TestODataMetadataProcessor.getEntityWithComplexProperty(), query, expectedPayload, returnResponse, expectedURL, "PATCH", 201);
}
Also used : UpdateExecution(org.teiid.translator.UpdateExecution) Test(org.junit.Test)

Example 9 with UpdateExecution

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

the class TestHotrodExecution method testServer.

@Test
public void testServer() throws Exception {
    InfinispanConnection connection = getConnection("default");
    CACHE_NAME = connection.getCache().getName();
    ResultSetExecution exec = null;
    Command command = null;
    UpdateExecution update = null;
    // the below also test one-2-one relation.
    command = UTILITY.parseCommand("DELETE FROM G2");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2, g3_e1, g3_e2 FROM G2");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertNull(exec.next());
    command = UTILITY.parseCommand("INSERT INTO G2 (e1, e2, g3_e1, g3_e2) values (1, 'one', 1, 'one')");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("INSERT INTO G2 (e1, e2, g3_e1, g3_e2) values (2, 'two', 2, 'two')");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2, g3_e1, g3_e2 FROM G2");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one", new Integer(1), "one" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), "two", new Integer(2), "two" }, exec.next().toArray());
    assertNull(exec.next());
    command = UTILITY.parseCommand("INSERT INTO G4 (e1, e2, G2_e1) values (1, 'one', 1)");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("INSERT INTO G4 (e1, e2, G2_e1) values (2, 'one-one', 1)");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("INSERT INTO G4 (e1, e2, G2_e1) values (3, 'two', 2)");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("INSERT INTO G4 (e1, e2, G2_e1) values (4, 'two-two', 2)");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2 FROM G4");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), "one-one" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(3), "two" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(4), "two-two" }, exec.next().toArray());
    assertNull(exec.next());
    command = UTILITY.parseCommand("SELECT g2.e1, g4.e1, g4.e2 FROM G2 g2 JOIN G4 g4 " + "ON g2.e1 = g4.G2_e1 WHERE g2.e2 = 'two'");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(2), new Integer(3), "two" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), new Integer(4), "two-two" }, exec.next().toArray());
    assertNull(exec.next());
    // updates
    command = UTILITY.parseCommand("UPDATE G2 SET e2 = 'two-m', g3_e2 = 'two-mm' WHERE e1 = 2");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2, g3_e1, g3_e2 FROM G2 ORDER BY e1");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one", new Integer(1), "one" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), "two-m", new Integer(2), "two-mm" }, exec.next().toArray());
    assertNull(exec.next());
    // complex updates
    command = UTILITY.parseCommand("UPDATE G4 SET e2 = 'two-2' WHERE e2 = 'two-two' OR e2 = 'one-one'");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2 FROM G4");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), "two-2" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(3), "two" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(4), "two-2" }, exec.next().toArray());
    assertNull(exec.next());
    // deletes
    command = UTILITY.parseCommand("DELETE FROM G4 where e2 = 'two-2'");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2 FROM G4");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(3), "two" }, exec.next().toArray());
    assertNull(exec.next());
    command = UTILITY.parseCommand("DELETE FROM G2 where e1 = 1");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT * FROM G2");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(2), "two-m", new Integer(2), "two-mm", null, null }, exec.next().toArray());
    assertNull(exec.next());
    // upsert
    command = UTILITY.parseCommand("UPSERT INTO G2 (e1, e2, g3_e1, g3_e2) values (1, 'one', 1, 'one')");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT * FROM G2 order by e1");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one", new Integer(1), "one", null, null }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), "two-m", new Integer(2), "two-mm", null, null }, exec.next().toArray());
    assertNull(exec.next());
    command = UTILITY.parseCommand("UPSERT INTO G2 (e1, e2, g3_e1, g3_e2) values (2, 'two', 2, 'two')");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT * FROM G2");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one", new Integer(1), "one", null, null }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), "two", new Integer(2), "two", null, null }, exec.next().toArray());
    assertNull(exec.next());
    command = UTILITY.parseCommand("UPSERT INTO G4 (e1, e2, G2_e1) values (5, 'upsert', 2)");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2 FROM G4");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(3), "two" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(5), "upsert" }, exec.next().toArray());
    assertNull(exec.next());
    Timestamp timestamp = new Timestamp(1504889513361L);
    Date date = new Date(1504889513361L);
    Time time = new Time(1504889513361L);
    String sql = "UPSERT INTO G5 (e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, e17, e18) " + "values (512, 'one', convert(512.34, double), 512.25, 256, 1, 't', 1504889513361, convert(123445656.12313, bigdecimal), " + "convert(1332434343, biginteger), " + "{t '" + new SimpleDateFormat("HH:mm:ss").format(time) + "'}, " + "{ts '" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp) + "'}, " + "{d '" + new SimpleDateFormat("yyyy-MM-dd").format(date) + "'}, " + "null, null, " + "convert('clob contents', clob), xmlparse(CONTENT '<a>foo</a>'), null)";
    System.out.println(sql);
    command = UTILITY.parseCommand(sql);
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, e17, e18 FROM G5");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    List<?> results = exec.next();
    assertEquals(new Integer(512), (Integer) results.get(0));
    assertEquals("one", (String) results.get(1));
    // assertEquals(512.34, (double)results.get(2));
    // assertEquals(512.25, (float)results.get(3));
    assertEquals(new Short("256"), results.get(4));
    assertEquals(new Byte("1"), results.get(5));
    assertEquals(new String("t"), results.get(6));
    assertEquals(new Long(1504889513361L), results.get(7));
    assertEquals(new BigDecimal("123445656.12313").toPlainString(), ((BigDecimal) results.get(8)).toPlainString());
    assertEquals(new BigInteger("1332434343").toString(), ((BigInteger) results.get(9)).toString());
    assertEquals(new Time(new SimpleDateFormat("HH:mm:ss").parse(time.toString()).getTime()), results.get(10));
    assertEquals(new Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2017-09-08 11:51:53").getTime()), results.get(11));
    assertEquals(new Date(new SimpleDateFormat("yyyy-MM-dd").parse("2017-09-08").getTime()), results.get(12));
    assertEquals("clob contents", ObjectConverterUtil.convertToString(((Clob) results.get(15)).getCharacterStream()));
    assertEquals("<a>foo</a>", ObjectConverterUtil.convertToString(((SQLXML) results.get(16)).getCharacterStream()));
    assertNull(exec.next());
}
Also used : UpdateExecution(org.teiid.translator.UpdateExecution) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.sql.Date) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) ResultSetExecution(org.teiid.translator.ResultSetExecution) SQLXML(java.sql.SQLXML) Command(org.teiid.language.Command) InfinispanConnection(org.teiid.infinispan.api.InfinispanConnection) BigInteger(java.math.BigInteger) QueryExpression(org.teiid.language.QueryExpression) Clob(java.sql.Clob) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 10 with UpdateExecution

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

Aggregations

UpdateExecution (org.teiid.translator.UpdateExecution)21 Test (org.junit.Test)16 Command (org.teiid.language.Command)16 ExecutionContext (org.teiid.translator.ExecutionContext)14 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)10 List (java.util.List)5 TranslationUtility (org.teiid.cdk.api.TranslationUtility)4 ResultSetExecution (org.teiid.translator.ResultSetExecution)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)3 QueryExpression (org.teiid.language.QueryExpression)3 DataNotAvailableException (org.teiid.translator.DataNotAvailableException)3 TranslatorException (org.teiid.translator.TranslatorException)3 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 BigInteger (java.math.BigInteger)2 DataSource (javax.activation.DataSource)2 CommandContext (org.teiid.CommandContext)2