use of org.teiid.metadata.RuntimeMetadata 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());
}
use of org.teiid.metadata.RuntimeMetadata 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) {
}
}
use of org.teiid.metadata.RuntimeMetadata in project teiid by teiid.
the class TestJPADirectQueryExecution method testCreate.
@Test
public void testCreate() throws Exception {
String input = "exec native('create;', 'one')";
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);
ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
Mockito.stub(connection.merge(argument.capture())).toReturn(new String("one"));
JPQLDirectQueryExecution execution = (JPQLDirectQueryExecution) TRANSLATOR.createExecution(command, ec, rm, connection);
execution.execute();
Mockito.verify(connection).merge(argument.capture());
assertEquals("one", argument.getValue());
}
use of org.teiid.metadata.RuntimeMetadata in project teiid by teiid.
the class TestJPADirectQueryExecution method testDelete.
@Test
public void testDelete() throws Exception {
String input = "exec native('delete;delete-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);
Query query = Mockito.mock(Query.class);
Mockito.stub(query.executeUpdate()).toReturn(12);
Mockito.stub(connection.createQuery("delete-query")).toReturn(query);
ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
JPQLDirectQueryExecution execution = (JPQLDirectQueryExecution) TRANSLATOR.createExecution(command, ec, rm, connection);
execution.execute();
Mockito.verify(connection, Mockito.times(1)).createQuery(argument.capture());
assertEquals("delete-query", argument.getValue());
assertArrayEquals(new Object[] { 12 }, (Object[]) execution.next().get(0));
}
use of org.teiid.metadata.RuntimeMetadata in project teiid by teiid.
the class TestSalesForceDirectQueryExecution method testCreateFail.
@Test(expected = TranslatorException.class)
public void testCreateFail() throws Exception {
String input = "exec native('create;id=pk;type=table;attributes=one,two,three', 'one')";
TranslationUtility util = FakeTranslationFactory.getInstance().getExampleTranslationUtility();
Command command = util.parseCommand(input);
ExecutionContext ec = Mockito.mock(ExecutionContext.class);
RuntimeMetadata rm = Mockito.mock(RuntimeMetadata.class);
SalesforceConnection connection = Mockito.mock(SalesforceConnection.class);
DirectQueryExecution execution = (DirectQueryExecution) TRANSLATOR.createExecution(command, ec, rm, connection);
execution.execute();
}
Aggregations