Package com.hp.hpl.squirrelrdf.ldap.test

Source Code of com.hp.hpl.squirrelrdf.ldap.test.LdapTest

package com.hp.hpl.squirrelrdf.ldap.test;

import java.util.Iterator;
import java.util.Map;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

import junit.framework.TestCase;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.query.core.Binding;
import com.hp.hpl.jena.query.core.BindingRoot;
import com.hp.hpl.jena.query.engine.QueryIterator;
import com.hp.hpl.jena.query.engine1.QueryEngine;
import com.hp.hpl.jena.query.engine1.QueryIterSingleton;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.squirrelrdf.ldap.LdapQueryEngine;
import com.hp.hpl.squirrelrdf.ldap.LdapSparqlMap;
import com.hp.hpl.squirrelrdf.ldap.LdapSubQuery;

public class LdapTest extends TestCase
{
  final static Log log = LogFactory.getLog(LdapTest.class);
  final static String directory = "ldap://ldap.hp.com/";
 
  DirContext context;
  Model config;
 
  protected void setUp() throws Exception
  {
    super.setUp();
    Logger.getLogger(LdapTest.class).setLevel(Level.DEBUG);
    context = new InitialDirContext();
    config = FileManager.get().loadModel("examples/ldap_map.n3");
  }

  protected void tearDown() throws Exception
  {
    super.tearDown();
    context.close();
  }
 
  /* A couple of simple sanity tests. Don't run normally (rename to run) */
  public void notestGeo() throws NamingException
  {
    Attributes attrs = context.getAttributes("ldap://ldap.hp.com/hprealestateid=buk03,%20ou=locations,%20o=hp.com");
   
    NamingEnumeration<? extends Attribute> enume = attrs.getAll();
   
    while (enume.hasMore())
    {
      Attribute attr = enume.next();
      log.info("Attr: " + attr.getID() + " " + attr.get());
    }
  }
 
  /* As above, best ignored */
  public void notest1() throws NamingException
  {
    SearchControls sc = new SearchControls();
    sc.setDerefLinkFlag(true);
    //sc.setReturningAttributes(new String[]{"sn","cn","manager"});
    sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
    NamingEnumeration<SearchResult> results = context.search(directory + "o=hp.com", "(cn=*shabajee*)", sc);
   
    assertTrue("I found Paul", results.hasMore());
   
    while (results.hasMore())
    {
      SearchResult result = results.next();
      Attributes attributes = result.getAttributes();
      NamingEnumeration<? extends Attribute> theAttributes = attributes.getAll();
      while (theAttributes.hasMore())
      {
        Attribute attribute = theAttributes.next();
        log.info("Attribute: " + attribute.getID());
        DirContext foo = attribute.getAttributeSyntaxDefinition();
        assertNotNull("I have a syntax definition", foo);
        Attributes lister = foo.getAttributes("");
        if (lister.get("numericoid").get().equals("1.3.6.1.4.1.1466.115.121.1.12"))
        {
          String bar = (String) attribute.get();
          log.info("Points to: " + bar);
          log.info(context.getAttributes(directory + bar));
        }
        foo.close();
       
        NamingEnumeration<?> vals = attribute.getAll();
        while (vals.hasMore())
        {
          Object val = vals.next();
          log.info("\t" + val + " {" + val.getClass() + "}");
        }
      }
    }
     
  }
 
  /* Check some internals */
  public void test2() throws NamingException
  {
    assertNotNull("I have a config", config);
   
    LdapSubQuery map = new LdapSubQuery(config);
   
    map.add(Triple.create("?s http://xmlns.com/foaf/0.1/name 'Paul\\sShabajee'"));
    map.add(Triple.create("?s http://xmlns.com/foaf/0.1/mbox ?mbox"));
   
    assertEquals("Predicate is foaf:name", Node.createURI("http://xmlns.com/foaf/0.1/name") , map.getTriples().get(0).getPredicate());
       
    NamingEnumeration<SearchResult> results = map.executeLdap(new BindingRoot(), null);
   
    assertTrue("I got a result", results.hasMore());
   
    SearchResult result = results.next();
   
    log.info("Result: " + result.getNameInNamespace());
   
    assertEquals("Paul's email address is right", "paul.shabajee@hp.com", result.getAttributes().get("uid").get(0));
   
    results.close();
   
    Iterator<Map <String, Node>> res = map.execute(new BindingRoot(), null);
   
    assertTrue("I got a result", res.hasNext());
   
    Map<String, Node> nextBinding = res.next();
   
    assertTrue("I have a subject", nextBinding.containsKey("s"));
   
    log.info("s: " + nextBinding.get("s"));
  }
 
  /* Check it works when a subject is given, and email handling */
  public void test3() throws NamingException
  {
    LdapSubQuery map = new LdapSubQuery(config);
   
    map.add(Triple.create("ldap://ldap.hp.com/uid=paul.shabajee@hp.com,ou=People,o=hp.com http://xmlns.com/foaf/0.1/mbox ?mbox"));
   
    Iterator<Map <String, Node>> res = map.execute(new BindingRoot(), null);
   
    assertTrue("I got a result", res.hasNext());
   
    Map<String, Node> nextBinding = res.next();
   
    assertEquals("Paul's email is right", Node.createURI("mailto:paul.shabajee@hp.com"), nextBinding.get("mbox"));
  }
 
  /* Check ldap node reference works. Also checks non-search (just get attribute for paul node) */
  public void test4()
  {
    LdapSubQuery map = new LdapSubQuery(config);
   
    map.add(Triple.create("ldap://ldap.hp.com/uid=paul.shabajee@hp.com,ou=People,o=hp.com http://jena.hpl.hp.com/schemas/hpcorp#manager ?manager"));
   
    Iterator<Map <String, Node>> res = map.execute(new BindingRoot(), null);
   
    assertTrue("I got a result", res.hasNext());
   
    Map<String, Node> nextBinding = res.next();
   
    assertEquals("Paul has right manager", Node.createURI("ldap://ldap.hp.com/uid=martin.merry@hp.com,%20ou=people,%20o=hp.com"), nextBinding.get("manager"));
  }
 
  /* Join! */
  public void test5()
  {
    LdapSparqlMap map = new LdapSparqlMap(config);
   
    map.addTriple(Triple.create("?paul http://xmlns.com/foaf/0.1/name 'Paul\\sShabajee'"));
    map.addTriple(Triple.create("?paul http://jena.hpl.hp.com/schemas/hpcorp#manager ?manager"));
    map.addTriple(Triple.create("?manager http://xmlns.com/foaf/0.1/name ?name"));
   
    QueryIterator results = new QueryIterSingleton(new BindingRoot());
   
    results = map.execute(results, null);
   
    assertTrue("I have a manager for Paul", results.hasNext());
   
    Binding binding = results.nextBinding();
   
    assertEquals("I have the right manager for Paul", Node.createLiteral("Martin John Merry"), binding.get("name"));
  }
 
  /* More email handling -- checks un-emailing*/
  public void test6() throws NamingException
  {
    LdapSubQuery map = new LdapSubQuery(config);
   
    map.add(Triple.create("?person http://xmlns.com/foaf/0.1/mbox mailto:paul.shabajee@hp.com"));
    map.add(Triple.create("?person http://xmlns.com/foaf/0.1/name ?name"));
   
    Iterator<Map <String, Node>> res = map.execute(new BindingRoot(), null);
   
    assertTrue("I got a result", res.hasNext());
   
    Map<String, Node> nextBinding = res.next();
   
    assertEquals("Paul's name is right", Node.createLiteral("Paul Shabajee"), nextBinding.get("name"));
  }
 
  /* Tests joining and unldaping */
  public void test7() throws NamingException
  {
    LdapSparqlMap map = new LdapSparqlMap(config);
   
    map.addTriple(Triple.create("?person http://xmlns.com/foaf/0.1/mbox mailto:martin.merry@hp.com"));
    map.addTriple(Triple.create("?underling http://jena.hpl.hp.com/schemas/hpcorp#manager ?person"));
    map.addTriple(Triple.create("?underling http://xmlns.com/foaf/0.1/name ?name"));
   
    QueryIterator results = new QueryIterSingleton(new BindingRoot());
   
    results = map.execute(results, null);
   
    assertTrue("I got a result", results.hasNext());
   
    while (results.hasNext())
    {
      Binding nextBinding = results.nextBinding();
      log.info("Name: " + nextBinding.get("name"));
    }
  }
 
  /* Full SPARQL Query */
  public void test8()
  {
    Query query = QueryFactory.create(
        "prefix foaf: <http://xmlns.com/foaf/0.1/> \n" +
        "prefix hp: <http://jena.hpl.hp.com/schemas/hpcorp#> \n" +
        "\n" +
        "select * where \n" +
        "{\n" +
        " _:person foaf:mbox <mailto:martin.merry@hp.com> . \n" +
        " _:minion hp:manager _:person ; foaf:name ?name . \n" +
        "}"
        );
   
    QueryEngine qe = new LdapQueryEngine(query, config);
    qe.setDataset(DatasetFactory.create());
    ResultSet res = qe.execSelect();
    assertTrue("I have results", res.hasNext());
    ResultSetFormatter.out(System.out, res);
  }
 
  /* This will fail -- no lat and long for bristol (?) */
  public void testNegPos()
  {
    Query query = QueryFactory.create(
        "prefix foaf: <http://xmlns.com/foaf/0.1/> \n" +
        "prefix hp: <http://jena.hpl.hp.com/schemas/hpcorp#> \n" +
        "prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> \n" +
        "\n" +
        "select * where \n" +
        "{\n" +
        " _:person foaf:mbox <mailto:damian.steer@hp.com> . \n" +
        " _:person foaf:based_near [ geo:lat ?lat ; geo:long ?long ; ] . \n" +
        "}"
        );
   
    QueryEngine qe = new LdapQueryEngine(query, config);
    qe.setDataset(DatasetFactory.create());
    ResultSet res = qe.execSelect();
    assertFalse("I have results", res.hasNext());
  }
 
  /* There is a country, however */
  public void testPosPos()
  {
    Query query = QueryFactory.create(
        "prefix foaf: <http://xmlns.com/foaf/0.1/> \n" +
        "prefix hp: <http://jena.hpl.hp.com/schemas/hpcorp#> \n" +
        "\n" +
        "select * where \n" +
        "{\n" +
        " _:person foaf:mbox <mailto:damian.steer@hp.com> . \n" +
        " _:person foaf:based_near [ hp:country ?country ] . \n" +
        "}"
        );
   
    QueryEngine qe = new LdapQueryEngine(query, config);
    qe.setDataset(DatasetFactory.create());
    ResultSet res = qe.execSelect();
    assertTrue("I have results", res.hasNext());
    QuerySolution result = res.nextSolution();
    assertTrue("I have a country", result.contains("country"));
    assertEquals("I have the correct country", Node.createLiteral("GB"), result.get("country").asNode());
  }
 
  /* As above, but ASK */
  public void testNegAskPos()
  {
    Query query = QueryFactory.create(
        "prefix foaf: <http://xmlns.com/foaf/0.1/> \n" +
        "prefix hp: <http://jena.hpl.hp.com/schemas/hpcorp#> \n" +
        "\n" +
        "ask where \n" +
        "{\n" +
        " _:person foaf:mbox <mailto:damian.steer@hp.com> . \n" +
        " _:person foaf:based_near [ hp:country \"UK\" ] . \n" +
        "}"
        );
   
    QueryEngine qe = new LdapQueryEngine(query, config);
    qe.setDataset(DatasetFactory.create());
    boolean res = qe.execAsk();
    assertFalse("I'm based near UK", res);
  }
 
  /* A positive ask */
  public void testPosAskPos()
  {
    Query query = QueryFactory.create(
        "prefix foaf: <http://xmlns.com/foaf/0.1/> \n" +
        "prefix hp: <http://jena.hpl.hp.com/schemas/hpcorp#> \n" +
        "\n" +
        "ask where \n" +
        "{\n" +
        " _:person foaf:mbox <mailto:damian.steer@hp.com> . \n" +
        " _:person foaf:based_near [ hp:country \"GB\" ] . \n" +
        "}"
        );
   
    QueryEngine qe = new LdapQueryEngine(query, config);
    qe.setDataset(DatasetFactory.create());
    boolean res = qe.execAsk();
    assertTrue("I'm based near GB", res);
  }
 
  /* Very simple, but possibly tricky... */
  public void testTripleExists()
  {
    Query query = QueryFactory.create(
        "prefix foaf: <http://xmlns.com/foaf/0.1/> \n" +
        "\n" +
        "select * where \n" +
        "{\n" +
        " <ldap://ldap.hp.com/uid=damian.steer@hp.com,%20ou=people,%20o=hp.com> \n" +
        "   foaf:mbox <mailto:damian.steer@hp.com> ; \n" +
        "   foaf:name \"Damian Steer\" . \n" +
        "}"
        );
   
    QueryEngine qe = new LdapQueryEngine(query, config);
    qe.setDataset(DatasetFactory.create());
    ResultSet res = qe.execSelect();
    assertTrue("I have results", res.hasNext());
  }
 
  /* Very simple, but possibly tricky... -- negative version of above */
  public void testTripleNotExists()
  {
    Query query = QueryFactory.create(
        "prefix foaf: <http://xmlns.com/foaf/0.1/> \n" +
        "\n" +
        "select * where \n" +
        "{\n" +
        " <ldap://ldap.hp.com/uid=damian.steer@hp.com,%20ou=people,%20o=hp.com> \n" +
        "   foaf:mbox <mailto:damian.steer@hp.com> ; \n" +
        "   foaf:name \"Damian foo Steer\" . \n" + // not my name
        "}"
        );
   
    QueryEngine qe = new LdapQueryEngine(query, config);
    qe.setDataset(DatasetFactory.create());
    ResultSet res = qe.execSelect();
    assertFalse("I have results", res.hasNext());
  }
}
TOP

Related Classes of com.hp.hpl.squirrelrdf.ldap.test.LdapTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.