Search in sources :

Example 81 with Command

use of org.teiid.language.Command in project teiid by teiid.

the class TestLDAPDirectQueryExecution method testSearch.

@Test
public void testSearch() throws Exception {
    String input = "exec native('search;context-name=corporate;filter=(objectClass=*);count-limit=5;timeout=6;search-scope=ONELEVEL_SCOPE;attributes=uid,cn')";
    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("(objectClass=*)", details.getContextFilter());
    assertEquals(5, details.getCountLimit());
    assertEquals(6, details.getTimeLimit());
    assertEquals(1, details.getSearchScope());
    assertEquals(2, details.getElementList().size());
    assertEquals("uid", details.getElementList().get(0).getName());
    assertEquals("cn", details.getElementList().get(1).getName());
}
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)

Example 82 with Command

use of org.teiid.language.Command 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 83 with Command

use of org.teiid.language.Command 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 84 with Command

use of org.teiid.language.Command 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 85 with Command

use of org.teiid.language.Command 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)

Aggregations

Command (org.teiid.language.Command)136 Test (org.junit.Test)99 ExecutionContext (org.teiid.translator.ExecutionContext)62 TranslationUtility (org.teiid.cdk.api.TranslationUtility)52 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)40 CommandBuilder (org.teiid.cdk.CommandBuilder)25 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)20 ResultSetExecution (org.teiid.translator.ResultSetExecution)18 TranslatedCommand (org.teiid.translator.jdbc.TranslatedCommand)17 UpdateExecution (org.teiid.translator.UpdateExecution)16 SimpleDBSQLVisitor (org.teiid.translator.simpledb.SimpleDBSQLVisitor)14 List (java.util.List)11 Connection (java.sql.Connection)10 LdapContext (javax.naming.ldap.LdapContext)9 Range (org.apache.accumulo.core.data.Range)9 SQLConversionVisitor (org.teiid.translator.jdbc.SQLConversionVisitor)9 MetadataFactory (org.teiid.metadata.MetadataFactory)8 ArrayList (java.util.ArrayList)7 FunctionTree (org.teiid.query.function.FunctionTree)7 UDFSource (org.teiid.query.function.UDFSource)7