Search in sources :

Example 31 with ExecutionContext

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

the class TestLDAPDirectQueryExecution method testCreate.

@Test
public void testCreate() throws Exception {
    String input = "exec native('create;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<BasicAttributes> createItemArgument = ArgumentCaptor.forClass(BasicAttributes.class);
    Mockito.verify(ctx).createSubcontext(nameArgument.capture(), createItemArgument.capture());
    assertEquals("uid=doe,ou=people,o=teiid.org", nameArgument.getValue());
    assertEquals("one", createItemArgument.getValue().get("one").getID());
    assertEquals("one", createItemArgument.getValue().get("one").get());
    assertEquals("two", createItemArgument.getValue().get("two").getID());
    assertEquals("2", createItemArgument.getValue().get("two").get());
    assertEquals("three", createItemArgument.getValue().get("three").getID());
    assertEquals("3.0", createItemArgument.getValue().get("three").get());
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) 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 32 with ExecutionContext

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

the class TestQueryExecution method testUnwrap.

@Test
public void testUnwrap() throws Exception {
    TranslationUtility util = new TranslationUtility(RealMetadataFactory.fromDDL("CREATE FOREIGN TABLE GROUP_PEOPLE (objectClass string options (\"teiid_ldap:unwrap\" true)) OPTIONS(nameinsource 'ou=Infrastructure,ou=Support,o=DEMOCORP,c=AU', updatable true);", "x", "y"));
    Command command = util.parseCommand("select * from group_people");
    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("ou=Infrastructure,ou=Support,o=DEMOCORP,c=AU")).toReturn(ctx);
    Attributes attribs = Mockito.mock(Attributes.class);
    Attribute attrib = Mockito.mock(Attribute.class);
    Mockito.stub(attrib.size()).toReturn(2);
    NamingEnumeration attribValues = new SimpleNamingEnumeration(Arrays.asList("foo", "bar").iterator());
    Mockito.stub(attrib.getAll()).toReturn(attribValues);
    Mockito.stub(attribs.get("objectClass")).toReturn(attrib);
    final SearchResult sr = new SearchResult("x", null, attribs);
    NamingEnumeration<SearchResult> enumeration = new SimpleNamingEnumeration(Arrays.asList(sr).iterator());
    Mockito.stub(ctx.search((String) Mockito.any(), (String) Mockito.any(), (SearchControls) Mockito.any())).toReturn(enumeration);
    LDAPExecutionFactory lef = new LDAPExecutionFactory();
    lef.start();
    LDAPSyncQueryExecution execution = (LDAPSyncQueryExecution) lef.createExecution(command, ec, rm, connection);
    execution.execute();
    List<?> result = execution.next();
    assertEquals(Arrays.asList("foo"), result);
    result = execution.next();
    assertEquals(Arrays.asList("bar"), result);
    assertNull(execution.next());
    // missing attribute handling
    Mockito.stub(attribs.get("objectClass")).toReturn(null);
    enumeration = new SimpleNamingEnumeration(Arrays.asList(sr).iterator());
    Mockito.stub(ctx.search((String) Mockito.any(), (String) Mockito.any(), (SearchControls) Mockito.any())).toReturn(enumeration);
    execution = (LDAPSyncQueryExecution) lef.createExecution(command, ec, rm, connection);
    execution.execute();
    result = execution.next();
    assertEquals(Collections.singletonList(null), result);
    assertNull(execution.next());
    // empty attribute handling
    attribValues = new SimpleNamingEnumeration(new ArrayList<Object>().iterator());
    Mockito.stub(attrib.size()).toReturn(0);
    Mockito.stub(attrib.getAll()).toReturn(attribValues);
    Mockito.stub(attribs.get("objectClass")).toReturn(attrib);
    enumeration = new SimpleNamingEnumeration(Arrays.asList(sr).iterator());
    Mockito.stub(ctx.search((String) Mockito.any(), (String) Mockito.any(), (SearchControls) Mockito.any())).toReturn(enumeration);
    execution = (LDAPSyncQueryExecution) lef.createExecution(command, ec, rm, connection);
    execution.execute();
    result = execution.next();
    assertEquals(Collections.singletonList(null), result);
    assertNull(execution.next());
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) TranslationUtility(org.teiid.cdk.api.TranslationUtility) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) LdapContext(javax.naming.ldap.LdapContext) Test(org.junit.Test)

Example 33 with ExecutionContext

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

the class TestEmbeddedMongoExecution method executeCmd.

private Execution executeCmd(String sql) throws Exception {
    Command cmd = utility.parseCommand(sql);
    CommandContext cc = Mockito.mock(CommandContext.class);
    Mockito.stub(cc.isReturnAutoGeneratedKeys()).toReturn(false);
    ExecutionContext ec = Mockito.mock(ExecutionContext.class);
    Mockito.stub(ec.getCommandContext()).toReturn(cc);
    Execution exec = translator.createExecution(cmd, ec, utility.createRuntimeMetadata(), this.connection);
    exec.execute();
    return exec;
}
Also used : ExecutionContext(org.teiid.translator.ExecutionContext) Execution(org.teiid.translator.Execution) CommandContext(org.teiid.CommandContext) Command(org.teiid.language.Command)

Example 34 with ExecutionContext

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

the class TestMongoDBDirectQueryExecution method testShellDirect.

@Test
public void testShellDirect() throws Exception {
    Command cmd = this.utility.parseCommand("SELECT * FROM Customers");
    MongoDBConnection connection = Mockito.mock(MongoDBConnection.class);
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    DBCollection dbCollection = Mockito.mock(DBCollection.class);
    DB db = Mockito.mock(DB.class);
    Mockito.stub(db.getCollection("MyTable")).toReturn(dbCollection);
    Mockito.stub(db.collectionExists(Mockito.anyString())).toReturn(true);
    Mockito.stub(connection.getDatabase()).toReturn(db);
    Argument arg = new Argument(Direction.IN, null, String.class, null);
    arg.setArgumentValue(new Literal("$ShellCmd;MyTable;remove;{ qty: { $gt: 20 }}", String.class));
    ResultSetExecution execution = this.translator.createDirectExecution(Arrays.asList(arg), cmd, context, this.utility.createRuntimeMetadata(), connection);
    execution.execute();
    Mockito.verify(dbCollection).remove(QueryBuilder.start("qty").greaterThan(20).get());
}
Also used : DBCollection(com.mongodb.DBCollection) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) Argument(org.teiid.language.Argument) Command(org.teiid.language.Command) MongoDBConnection(org.teiid.mongodb.MongoDBConnection) Literal(org.teiid.language.Literal) DB(com.mongodb.DB) Test(org.junit.Test)

Example 35 with ExecutionContext

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

the class TestJPADirectQueryExecution method testWithoutMarker.

@Test
public void testWithoutMarker() throws Exception {
    String input = "exec native('jpa query')";
    TranslationUtility util = FakeTranslationFactory.getInstance().getExampleTranslationUtility();
    Command command = util.parseCommand(input);
    ExecutionContext ec = Mockito.mock(ExecutionContext.class);
    RuntimeMetadata rm = Mockito.mock(RuntimeMetadata.class);
    EntityManager connection = Mockito.mock(EntityManager.class);
    try {
        JPQLDirectQueryExecution execution = (JPQLDirectQueryExecution) TRANSLATOR.createExecution(command, ec, rm, connection);
        execution.execute();
        fail("the above should have thrown exception");
    } catch (TranslatorException e) {
    }
}
Also used : EntityManager(javax.persistence.EntityManager) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) TranslationUtility(org.teiid.cdk.api.TranslationUtility) TranslatorException(org.teiid.translator.TranslatorException) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) 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