Search in sources :

Example 11 with ExecutionContext

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

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

the class TestExcelExecution method helpExecute.

private ArrayList helpExecute(String ddl, FileConnection connection, String query, boolean format) throws Exception {
    ExcelExecutionFactory translator = new ExcelExecutionFactory();
    translator.setFormatStrings(format);
    translator.start();
    TransformationMetadata metadata = RealMetadataFactory.fromDDL(ddl, "vdb", "excel");
    TranslationUtility utility = new TranslationUtility(metadata);
    Command cmd = utility.parseCommand(query);
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    ResultSetExecution execution = translator.createResultSetExecution((QueryExpression) cmd, context, utility.createRuntimeMetadata(), connection);
    try {
        execution.execute();
        ArrayList results = new ArrayList();
        while (true) {
            List<?> row = execution.next();
            if (row == null) {
                break;
            }
            results.add(row);
        }
        return results;
    } finally {
        execution.close();
    }
}
Also used : ResultSetExecution(org.teiid.translator.ResultSetExecution) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) TranslationUtility(org.teiid.cdk.api.TranslationUtility) ArrayList(java.util.ArrayList)

Example 13 with ExecutionContext

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

the class TestNativeSpreadsheet method testDirect.

@Test
public void testDirect() throws TranslatorException {
    SpreadsheetExecutionFactory sef = new SpreadsheetExecutionFactory();
    sef.setSupportsDirectQueryProcedure(true);
    String input = "call native('worksheet=x;query=$1 foo;limit=2', 'a')";
    TranslationUtility util = FakeTranslationFactory.getInstance().getExampleTranslationUtility();
    Command command = util.parseCommand(input);
    ExecutionContext ec = Mockito.mock(ExecutionContext.class);
    RuntimeMetadata rm = Mockito.mock(RuntimeMetadata.class);
    GoogleSpreadsheetConnection connection = Mockito.mock(GoogleSpreadsheetConnection.class);
    RowsResult result = Mockito.mock(RowsResult.class);
    Mockito.stub(result.iterator()).toReturn(Arrays.asList(new SheetRow()).iterator());
    Mockito.stub(connection.executeQuery("x", "'a' foo", null, 2, 0)).toReturn(result);
    ResultSetExecution execution = (ResultSetExecution) sef.createExecution(command, ec, rm, connection);
    execution.execute();
    List<?> vals = execution.next();
    assertTrue(vals.get(0) instanceof Object[]);
}
Also used : TranslationUtility(org.teiid.cdk.api.TranslationUtility) RowsResult(org.teiid.translator.google.api.result.RowsResult) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) SheetRow(org.teiid.translator.google.api.result.SheetRow) Command(org.teiid.language.Command) GoogleSpreadsheetConnection(org.teiid.translator.google.api.GoogleSpreadsheetConnection) Test(org.junit.Test)

Example 14 with ExecutionContext

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

the class TestLDAPDirectQueryExecution method testUpdate.

@Test
public void testUpdate() throws Exception {
    String input = "exec native('update;uid=doe,ou=people,o=teiid.org;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);
    LdapContext connection = Mockito.mock(LdapContext.class);
    LdapContext ctx = Mockito.mock(LdapContext.class);
    Mockito.stub(connection.lookup("")).toReturn(ctx);
    LDAPDirectCreateUpdateDeleteQueryExecution execution = (LDAPDirectCreateUpdateDeleteQueryExecution) TRANSLATOR.createExecution(command, ec, rm, connection);
    execution.execute();
    ArgumentCaptor<String> nameArgument = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<ModificationItem[]> modificationItemArgument = ArgumentCaptor.forClass(ModificationItem[].class);
    Mockito.verify(ctx).modifyAttributes(nameArgument.capture(), modificationItemArgument.capture());
    assertEquals("uid=doe,ou=people,o=teiid.org", nameArgument.getValue());
    assertEquals("one", modificationItemArgument.getValue()[0].getAttribute().getID());
    assertEquals("one", modificationItemArgument.getValue()[0].getAttribute().get());
    assertEquals("two", modificationItemArgument.getValue()[1].getAttribute().getID());
    assertEquals("2", modificationItemArgument.getValue()[1].getAttribute().get());
    assertEquals("three", modificationItemArgument.getValue()[2].getAttribute().getID());
    assertEquals("3.0", modificationItemArgument.getValue()[2].getAttribute().get());
}
Also used : ModificationItem(javax.naming.directory.ModificationItem) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) TranslationUtility(org.teiid.cdk.api.TranslationUtility) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) LdapContext(javax.naming.ldap.LdapContext) Test(org.junit.Test)

Example 15 with ExecutionContext

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

the class TestLDAPDirectQueryExecution method testSearchDefaultsAndEscaping.

@Test
public void testSearchDefaultsAndEscaping() throws Exception {
    String input = "exec native('search;context-name=corporate;filter=(;;)')";
    TranslationUtility util = FakeTranslationFactory.getInstance().getExampleTranslationUtility();
    Command command = util.parseCommand(input);
    ExecutionContext ec = Mockito.mock(ExecutionContext.class);
    RuntimeMetadata rm = Mockito.mock(RuntimeMetadata.class);
    LdapContext connection = Mockito.mock(LdapContext.class);
    LdapContext ctx = Mockito.mock(LdapContext.class);
    Mockito.stub(connection.lookup("corporate")).toReturn(ctx);
    LDAPDirectSearchQueryExecution execution = (LDAPDirectSearchQueryExecution) TRANSLATOR.createExecution(command, ec, rm, connection);
    execution.execute();
    LDAPSearchDetails details = execution.getDelegate().getSearchDetails();
    assertEquals("corporate", details.getContextName());
    assertEquals("(;)", details.getContextFilter());
    assertEquals(-1, details.getCountLimit());
    assertEquals(0, details.getTimeLimit());
    assertEquals(1, details.getSearchScope());
    assertEquals(0, details.getElementList().size());
}
Also used : ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) TranslationUtility(org.teiid.cdk.api.TranslationUtility) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) LdapContext(javax.naming.ldap.LdapContext) Test(org.junit.Test)

Aggregations

ExecutionContext (org.teiid.translator.ExecutionContext)65 Test (org.junit.Test)50 Command (org.teiid.language.Command)49 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)48 TranslationUtility (org.teiid.cdk.api.TranslationUtility)32 ResultSetExecution (org.teiid.translator.ResultSetExecution)23 List (java.util.List)17 UpdateExecution (org.teiid.translator.UpdateExecution)14 TranslatorException (org.teiid.translator.TranslatorException)11 ArrayList (java.util.ArrayList)10 QueryExpression (org.teiid.language.QueryExpression)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 LdapContext (javax.naming.ldap.LdapContext)9 ExecutionFactory (org.teiid.translator.ExecutionFactory)9 SalesforceConnection (org.teiid.translator.salesforce.SalesforceConnection)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 HashMap (java.util.HashMap)6 DataSource (javax.activation.DataSource)6 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)6 WSConnection (org.teiid.translator.WSConnection)6