VIVO-742 Change client code to use SearchEngine interface.

Removed SolrQueryTest until I can see how to update it. Everything compiles and tests run, but haven't tried running VIVO yet.
This commit is contained in:
Jim Blake 2014-04-22 13:49:27 -04:00
parent 823848123f
commit 90886c564e
38 changed files with 398 additions and 1109 deletions

View file

@ -10,18 +10,19 @@ import java.util.regex.PatternSyntaxException;
import javax.servlet.ServletException;
import org.apache.solr.client.solrj.SolrQuery;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import stubs.edu.cornell.mannlib.vitro.webapp.modules.ApplicationStub;
import stubs.edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchEngineStub;
import stubs.javax.servlet.ServletContextStub;
import stubs.javax.servlet.http.HttpServletRequestStub;
import stubs.javax.servlet.http.HttpServletResponseStub;
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchQuery;
/**
* Tests on various methods in the class.
@ -36,6 +37,8 @@ public class JSONReconcileServletTest extends AbstractTestClass {
@Before
public void setup() throws Exception {
ApplicationStub.setup(new ServletContextStub(), new SearchEngineStub());
request = new HttpServletRequestStub();
request.setRequestUrl(new URL("http://vivo.this.that/reconcile"));
request.setMethod("POST");
@ -78,23 +81,17 @@ public class JSONReconcileServletTest extends AbstractTestClass {
propertiesList.add(properties);
// test getQuery
SolrQuery solrQuery = reconcile.getQuery(nameStr, nameType, rowNum, propertiesList);
SearchQuery searchQuery = reconcile.getQuery(nameStr, nameType, rowNum, propertiesList);
String messagePrefix = "Query should contain the text: ";
testAssertTrue(messagePrefix + orgStr, orgStr, solrQuery.toString());
testAssertTrue(messagePrefix + nameStr, nameStr, solrQuery.toString());
testAssertTrue(messagePrefix + orgType, orgType, solrQuery.toString());
testAssertTrue(messagePrefix + nameType, orgType, solrQuery.toString());
testAssertTrue(messagePrefix + orgStr, orgStr, searchQuery.toString());
testAssertTrue(messagePrefix + nameStr, nameStr, searchQuery.toString());
testAssertTrue(messagePrefix + orgType, orgType, searchQuery.toString());
testAssertTrue(messagePrefix + nameType, orgType, searchQuery.toString());
}
private void testAssertTrue(String message, String inputStr, String resultStr) {
try {
String modStr = null;
if (inputStr.contains(":") && inputStr.contains("/")) {
modStr = inputStr.replaceAll(":", "%3A").replaceAll("/", "%2F");
} else {
modStr = inputStr;
}
Pattern regex = Pattern.compile(modStr, Pattern.CASE_INSENSITIVE);
Pattern regex = Pattern.compile(inputStr, Pattern.CASE_INSENSITIVE);
Matcher regexMatcher = regex.matcher(resultStr);
Assert.assertTrue(message, regexMatcher.find());
} catch (PatternSyntaxException e) {

View file

@ -20,16 +20,16 @@ import org.junit.Test;
import stubs.edu.cornell.mannlib.vitro.webapp.dao.VClassDaoStub;
import stubs.edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactoryStub;
import stubs.edu.cornell.mannlib.vitro.webapp.modules.ApplicationStub;
import stubs.edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchEngineStub;
import stubs.javax.servlet.ServletConfigStub;
import stubs.javax.servlet.ServletContextStub;
import stubs.javax.servlet.http.HttpServletRequestStub;
import stubs.javax.servlet.http.HttpServletResponseStub;
import stubs.javax.servlet.http.HttpSessionStub;
import stubs.org.apache.solr.client.solrj.SolrServerStub;
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.dao.ModelAccess;
import edu.cornell.mannlib.vitro.webapp.search.solr.SolrSetup;
/**
* TODO
@ -85,7 +85,7 @@ public class JsonServletTest extends AbstractTestClass {
private WebappDaoFactoryStub wadf;
private VClassDaoStub vcDao;
private SolrServerStub solr;
private SearchEngineStub solr;
@Before
public void setup() throws ServletException {
@ -112,8 +112,8 @@ public class JsonServletTest extends AbstractTestClass {
vcDao = new VClassDaoStub();
wadf.setVClassDao(vcDao);
solr = new SolrServerStub();
ctx.setAttribute(SolrSetup.SOLR_SERVER, solr);
solr = new SearchEngineStub();
ApplicationStub.setup(new ServletContextStub(), solr);
}
@Test

View file

@ -1,327 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.search.solr;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.core.CoreContainer;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.xml.sax.SAXException;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.util.FileManager;
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactoryJena;
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceFactorySingle;
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.jena.model.RDFServiceModel;
import edu.cornell.mannlib.vitro.webapp.search.VitroSearchTermNames;
import edu.cornell.mannlib.vitro.webapp.search.solr.documentBuilding.IndividualToSolrDocument;
/**
* This tests what results will be returned for queries to the
* Solr service.
*
* The test will setup a temporary Solr service, setup a temporary RDF store, load the
* store with some individuals, and then index the individuals in the store.
*
* Once this is done a set of test queries will be run against that service.
*
* This code is based on a similar test from the CUL Discovery and Access
* integration layer.
*
* All RDF/XML files in webapp/test/testontologies/SolrQueryTestRDF will be
* loaded into the model.
*/
//ignored for now since the solr directory isn't yet found when running from vivo.
@Ignore
public class SolrQueryTest extends AbstractTestClass {
/** Key of system property for build directory. */
final static String buildDirSystemPropertyKey = "vitro.build.dir";
/** Solr index to run test queries on. */
static SolrServer solr = null;
/** Container for solr */
static CoreContainer coreContainer = null;
/** Folder to store the temporary Solr index. */
public static TemporaryFolder solrIndexFolder = null;
/**
* This test needs to load RDF data to use as a
* source of individuals to to build documents.
* This is relative to the build directory.
*/
final static String testRDFDir =
"/webapp/test/testontologies/SolrQueryTestRDF";
@Before
public void setup() throws Exception{
//solr makes a lot of output
suppressSysout();
suppressSyserr();
File buildDir = findBuildDir();
solrIndexFolder = new TemporaryFolder();
solrIndexFolder.create();
File tempSolrBase = solrIndexFolder.newFolder("tempSolrBase");
FileUtils.copyDirectory(getSolrTemplateDir(buildDir), tempSolrBase );
solr = setupSolr( tempSolrBase );
indexRdf( loadTestRDF( buildDir ) );
}
@After
public void takedown() throws Exception{
if( coreContainer != null )
coreContainer.shutdown();
restoreOutputStreams();
if( solrIndexFolder != null )
solrIndexFolder.delete();
}
/**
* This will return the directory to use as the Solr
* home template directory.
*
* Throws an exception if the directory is not found.
* @param buildDir - must not be null, must be the base of the build.
*/
private File getSolrTemplateDir(File buildDir) throws Exception {
if(buildDir == null || !buildDir.exists() )
throw new Exception("buildDir must not be null");
String solrTemplateDirName = buildDir.getAbsolutePath() + "/solr/homeDirectoryTemplate";
File solrTemplateDir = new File(solrTemplateDirName);
if( solrTemplateDir == null || ! solrTemplateDir.exists())
throw new Exception("Solr home directory template " +
"was not found at " + solrTemplateDirName);
else
return solrTemplateDir;
}
protected SolrServer setupSolr(File solrBase)
throws ParserConfigurationException, IOException, SAXException{
System.setProperty("solr.solr.home", solrBase.getAbsolutePath());
CoreContainer.Initializer initializer = new CoreContainer.Initializer();
coreContainer = initializer.initialize();
return new EmbeddedSolrServer(coreContainer, "");
}
private OntModel loadTestRDF(File buildDir) throws Exception {
String dirname = buildDir.getAbsolutePath() + testRDFDir;
File rdfDir = new File( dirname );
assertNotNull("could not find dir " + dirname , rdfDir);
assertTrue(dirname + " must be a directory." ,rdfDir.isDirectory());
//load all files in test dir.
File[] files = rdfDir.listFiles();
assertNotNull("no test RDF files found",files);
assertTrue("no test RDF files found", files.length > 0 );
OntModel model = ModelFactory.createOntologyModel();
for (File file : files ){
InputStream in = FileManager.get().open( file.getAbsolutePath() );
assertNotNull("Could not load file " + file.getAbsolutePath(), in );
try{
if( file.getName().endsWith(".rdf") ){
model.read(in,null);
}else if( file.getName().endsWith(".n3")){
model.read(in,null,"N3");
}else if( file.getName().endsWith(".nt")){
model.read(in,null,"N-TRIPLE");
}else if( file.getName().endsWith(".ttl")){
model.read(in,null,"TURTLE");
}else{
throw new Exception("Format unknown for file name " + file.getName());
}
}catch(Throwable th){
throw new Exception( "Could not load RDF file "
+ file.getAbsolutePath() , th);
}
}
return model ;
}
/**
* Find the base of the build directories.
* @return This method will return a non-null file to use
* or it will throw an exception if none can be found.
* @throws Exception
*/
private File findBuildDir() throws Exception {
//First try to find the base directory
//of the build in the system properties.
String buildDirName = System.getProperty(buildDirSystemPropertyKey);
if( buildDirName != null ){
File buildDir = new File(buildDirName);
if( buildDir.exists() && buildDir.isDirectory() ){
return buildDir;
}
}
//If there is no system property try to
//guess the location based on the working directory
File f = null;
String[] fallBackBases = {"","../","../../"};
List<String> attempted = new ArrayList<String>();
for( String base: fallBackBases){
String attemptedDir =base + "solr/homeDirectoryTemplate";
f = new File( attemptedDir );
attempted.add( System.getProperty("user.dir") + '/' + attemptedDir );
if( f != null && f.exists() && f.isDirectory() ){
f = new File( base );
break;
}else{
f = null;
}
}
if( f == null ){
throw new Exception(
"Could not find the base of the " +
"build directory for the project, " +
"checked the system property " + buildDirSystemPropertyKey
+ " and checked in these locations: \n" +
StringUtils.join(attempted,"\n "));
}else{
return f;
}
}
/** Query the RDF, build Solr Documents from results, load them to Solr. */
private void indexRdf(OntModel model) throws Exception {
RDFServiceModel rdfService = new RDFServiceModel(model);
IndividualToSolrDocument r2d =
SolrSetup.setupTransltion(
model,
model,
new RDFServiceFactorySingle(rdfService),
null, null);
WebappDaoFactory wdf = new WebappDaoFactoryJena(model);
for( String uri: getURISToIndex( model ) ){
SolrInputDocument doc;
try {
doc = r2d.translate( wdf.getIndividualDao().getIndividualByURI(uri));
} catch (Exception e) {
throw new Exception("Failed on building document for uri:" + uri, e);
}
try {
solr.add( doc );
} catch (Exception e) {
throw new Exception("Failed adding doc to solr for uri:" + uri, e);
}
}
solr.commit();
}
private List<String> getURISToIndex(Model model) {
//just return all the URIs in the subject position
List<String> uris = new LinkedList<String>();
ResIterator it = model.listSubjects();
while(it.hasNext() ){
Resource res = it.nextResource();
if( res != null && res.isURIResource() ){
uris.add( res.getURI() );
}
}
return uris;
}
/**
* Test that a document with the given URIs are in the results for the query.
* @throws SolrServerException */
void testQueryGetsDocs(String errmsg, SolrQuery query, String[] expectedUris) throws SolrServerException{
assertNotNull(errmsg + " but query was null", query);
assertNotNull(errmsg + " but expected URIs was null", expectedUris );
QueryResponse resp = solr.query(query);
if( resp == null )
fail( errmsg + " but Could not get a solr response");
Set<String> uris = new HashSet<String>(Arrays.asList( expectedUris ));
for( SolrDocument doc : resp.getResults()){
assertNotNull(errmsg + ": solr doc was null", doc);
String uri = (String) doc.getFirstValue( VitroSearchTermNames.URI );
assertNotNull(errmsg+": no URI field in solr doc" , uri);
uris.remove( uri );
}
if( uris.size() > 0){
String errorMsg =
"\nThe query '"+ query + "' was expected " +
"to return the following URIs but did not:";
for( String uri : uris){
errorMsg= errorMsg+"\n" + uri;
}
fail( errmsg + errorMsg);
}
}
@Test
public void testSolrWasStarted() throws SolrServerException, IOException {
assertNotNull( solr );
solr.ping();//this will throw an exception of the server is not reachable
}
@Test
public void testCorsonSearch() throws SolrServerException{
/* make sure that we have the document in the index before we do anything */
SolrQuery query = new SolrQuery().setQuery("corson");
testQueryGetsDocs("Expect to find a doc when searching for 'corson'",
query,new String[]{ "http://vivo.cornell.edu/individual/individual22972" } ) ;
}
@Test
public void testFormFeed() throws SolrServerException{
/* make sure that we have the document in the index before we do anything */
SolrQuery query = new SolrQuery().setQuery("vivo15");
testQueryGetsDocs("Expect to find a doc when searching for 'vivo15'",
query,new String[]{ "http://vivo.cornell.edu/individual/individualIssueVivo15" } ) ;
}
}

View file

@ -7,19 +7,24 @@ package edu.cornell.mannlib.vitro.webapp.search.solr;
import java.io.InputStream;
import org.apache.log4j.Level;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.SolrInputField;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import stubs.edu.cornell.mannlib.vitro.webapp.modules.ApplicationStub;
import stubs.edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchEngineStub;
import stubs.javax.servlet.ServletContextStub;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.impl.RDFDefaultErrorHandler;
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
import edu.cornell.mannlib.vitro.webapp.application.ApplicationUtils;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.IndividualImpl;
import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchInputDocument;
import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchInputField;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceFactory;
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceFactorySingle;
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.jena.model.RDFServiceModel;
@ -37,6 +42,7 @@ public class ThumbnailImageURLTest extends AbstractTestClass{
@Before
public void setUp() throws Exception {
setLoggerLevel(RDFDefaultErrorHandler.class, Level.OFF);
ApplicationStub.setup(new ServletContextStub(), new SearchEngineStub());
Model model = ModelFactory.createDefaultModel();
InputStream in = ThumbnailImageURLTest.class.getResourceAsStream("testPerson.n3");
@ -52,7 +58,7 @@ public class ThumbnailImageURLTest extends AbstractTestClass{
*/
@Test
public void testThumbnailFieldCreatedInSolrDoc() {
SolrInputDocument doc = new SolrInputDocument();
SearchInputDocument doc = ApplicationUtils.instance().getSearchEngine().createInputDocument();
ThumbnailImageURL testMe = new ThumbnailImageURL( testRDF );
Individual ind = new IndividualImpl();
ind.setURI(personsURI);
@ -66,11 +72,11 @@ public class ThumbnailImageURLTest extends AbstractTestClass{
//make sure that a Solr document field got created for the thumbnail image
SolrInputField thumbnailField = doc.getField( VitroSearchTermNames.THUMBNAIL_URL );
SearchInputField thumbnailField = doc.getField( VitroSearchTermNames.THUMBNAIL_URL );
Assert.assertNotNull(thumbnailField);
Assert.assertNotNull( thumbnailField.getValues() );
Assert.assertEquals(1, thumbnailField.getValueCount());
Assert.assertEquals(1, thumbnailField.getValues().size());
Assert.assertEquals("http://vivo.cornell.edu/individual/n54945", thumbnailField.getFirstValue());
}

View file

@ -3,7 +3,6 @@
package stubs.edu.cornell.mannlib.vitro.webapp.modules.searchEngine;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -14,6 +13,7 @@ import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchEngineExcepti
import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchInputDocument;
import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchQuery;
import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchResponse;
import edu.cornell.mannlib.vitro.webapp.searchengine.base.BaseSearchInputDocument;
/**
* TODO
@ -53,6 +53,11 @@ public class SearchEngineStub implements SearchEngine {
}
}
@Override
public SearchInputDocument createInputDocument() {
return new BaseSearchInputDocument();
}
// ----------------------------------------------------------------------
// Un-implemented methods
@ -80,13 +85,6 @@ public class SearchEngineStub implements SearchEngine {
throw new RuntimeException("SearchEngineStub.ping() not implemented.");
}
@Override
public SearchInputDocument createInputDocument() {
// TODO Auto-generated method stub
throw new RuntimeException(
"SearchEngineStub.createInputDocument() not implemented.");
}
@Override
public void add(SearchInputDocument... docs) throws SearchEngineException {
// TODO Auto-generated method stub

View file

@ -1,46 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package stubs.org.apache.solr.client.solrj;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.util.NamedList;
/**
* TODO
*/
public class SolrServerStub extends SolrServer {
private static final Log log = LogFactory.getLog(SolrServerStub.class);
// ----------------------------------------------------------------------
// Stub infrastructure
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
/*
* (non-Javadoc)
*
* @see
* org.apache.solr.client.solrj.SolrServer#request(org.apache.solr.client
* .solrj.SolrRequest)
*/
@Override
public NamedList<Object> request(SolrRequest request)
throws SolrServerException, IOException {
// TODO not really an implementation.
return new NamedList<Object>();
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
}