package speculoos.jndi;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.naming.Context;
import speculoos.config.xml.XMLConfigurator;
import speculoos.config.xml.XMLConfiguratorException;
import speculoos.core.FunctorMapper;
import speculoos.core.Mapper;
import speculoos.core.MapperException;
import speculoos.jndi.mappers.AttributeJoinMapperImpl;
import speculoos.manager.Configure;
import speculoos.manager.MapperConfigurationException;
import speculoos.manager.MapperManager;
import speculoos.utils.TypeHelper;
import junit.framework.TestCase;
/**
* Main integration testing class for jind mappers. This class uses DSML ldap
* backend to test the execution of the various mappers defined in
* speculoos.jndi. We use a DSML file provided in the examples folder from sun's
* dsmlv2 package.
*
*
* @author nono
*
*/
public class JNDIMapperTest extends TestCase {
private HashMap env;
private MapperManager manager;
private XMLConfigurator xc;
protected void setUp() throws Exception {
super.setUp();
xc = new XMLConfigurator();
manager = new MapperManager();
xc.setValidating(true);
}
/**
* basic search of one person
*
* @throws XMLConfiguratorException
* @throws MapperException
*/
public void test01BasicSearch() throws XMLConfiguratorException,
MapperException {
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("pdc-maps.xml"));
// configure
xc.configure(manager);
// start and run
manager.start();
Map env = new HashMap();
env.put("name", "Bohr");
SearchMapper sm = (SearchMapper) manager.getMapper("searchUserByName");
List l = sm.search(null, env);
manager.release(sm);
assertEquals(1, l.size());
Person p = (Person) l.get(0);
assertEquals("Niels Bohr", p.getFullName());
assertEquals("+1 408 555 7562", p.getPhone());
assertEquals(4, p.getClasses().size());
}
/**
* Add a person. Check value using XPath.
*
* @throws XMLConfiguratorException
* @throws MapperException
*/
public void test02BasicAdd() throws XMLConfiguratorException,
MapperException {
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("pdc-maps-2.xml"));
// configure
xc.configure(manager);
// start and run
manager.start();
Person p = new Person();
p.setLastName("Einstein");
p.setFullName("Albert Einstein");
p.setPhone("+1 123 456 789");
p.setFax("+1 234 567 890");
AddMapper am = (AddMapper) manager.getMapper("addUser");
try {
am.add(p, null);
fail("Should have thrown exception");
} catch (MapperException e) {
// NOP
}
p.setEmail("albert@nowhere.com");
am.add(p, null);
manager.release(am);
// TODO: check that node is created using XPath
}
public void test03FilterSearch() throws XMLConfiguratorException,
MapperException {
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("pdc-maps.xml"));
// configure
xc.configure(manager);
// start and run
manager.start();
// create filter object
Person f = new Person();
f.setLastName("Bohr");
f.setPhone("+1 408 555 7562");
SearchMapper sm = (SearchMapper) manager.getMapper("searchUser");
List l = sm.search(f, env);
manager.release(sm);
assertEquals(1, l.size());
Person p = (Person) l.get(0);
assertEquals("Niels Bohr", p.getFullName());
assertEquals("+1 408 555 7562", p.getPhone());
assertEquals(4, p.getClasses().size());
assertEquals("cn=Niels Bohr,ou=People,o=jnditutorial",p.getDN());
}
public void test04NoFilterSearch() throws XMLConfiguratorException,
MapperException {
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("pdc-maps.xml"));
// configure
xc.configure(manager);
// start and run
manager.start();
Map env = new HashMap();
env.put("__ROOT__","cn=Niels Bohr,ou=People,o=jnditutorial");
// create filter object
SearchMapper sm = (SearchMapper) manager.getMapper("searchUserRoot");
List l = sm.search(null, env);
manager.release(sm);
assertEquals(1, l.size());
Person p = (Person) l.get(0);
assertEquals("Niels Bohr", p.getFullName());
assertEquals("+1 408 555 7562", p.getPhone());
assertEquals(4, p.getClasses().size());
assertEquals("cn=Niels Bohr,ou=People,o=jnditutorial",p.getDN());
}
/*
* A simple join search: retrieve all persons in a group.
*/
public void test05JoinSearch() throws XMLConfiguratorException,
MapperException {
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("pdc-maps.xml"));
// configure
xc.configure(manager);
// start and run
manager.start();
Map env = new HashMap();
env.put("groupname", "Directory Administrators");
// create filter object
Mapper jm = manager.getMapper("usersInGroup");
List l = (List)jm.map(null, env);
assertEquals(4,l.size());
/* check correctness of DNs with round trip query */
Person p = (Person)l.get(0);
System.err.println("DN="+p.getDN());
Mapper sm = manager.getMapper("searchUserRoot");
env = new HashMap();
env.put("__ROOT__",p.getDN());
List ll = (List)sm.map(null,env);
assertEquals(1,ll.size());
}
/*
* A simple join search: retrieve all persons in a group by name,
* this test was setup to illustrate bug for root context doubling
* in join queries.
*
* @see <a href="http://sourceforge.net/tracker/index.php?func=detail&aid=1517964&group_id=169899&atid=852198">issue tracker</a>
*/
public void test06Join2Search() throws XMLConfiguratorException,
MapperException {
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("pdc-maps.xml"));
// configure
xc.configure(manager);
// start and run
manager.start();
Map env = new HashMap();
env.put("groupname", "Directory Administrators");
// create filter object
Mapper jm = manager.getMapper("usersInGroupByName");
List l = (List)jm.map(null, env);
assertEquals(4,l.size());
/* check correctness of DNs with round trip query */
Person p = (Person)l.get(0);
System.err.println("DN="+p.getDN());
Mapper sm = manager.getMapper("searchUserRoot");
env = new HashMap();
env.put("__ROOT__",p.getDN());
List ll = (List)sm.map(null,env);
assertEquals(1,ll.size());
}
/*
* (non-Javadoc)
*
* @see junit.framework.TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
manager.stop();
}
}