From a6ce4374dceefd2864bbfe47e6ef65e1ba7b3288 Mon Sep 17 00:00:00 2001 From: Jim Blake Date: Fri, 17 Oct 2014 10:52:53 -0400 Subject: [PATCH] Unit test for NameFields search document modifier. --- .../documentBuilding/NameFieldsTest.java | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 webapp/test/edu/cornell/mannlib/vitro/webapp/search/documentBuilding/NameFieldsTest.java diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/search/documentBuilding/NameFieldsTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/search/documentBuilding/NameFieldsTest.java new file mode 100644 index 000000000..db99b7b62 --- /dev/null +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/search/documentBuilding/NameFieldsTest.java @@ -0,0 +1,144 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.search.documentBuilding; + +import static edu.cornell.mannlib.vitro.webapp.search.VitroSearchTermNames.NAME_RAW; +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import stubs.edu.cornell.mannlib.vitro.webapp.beans.IndividualStub; + +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.rdf.model.Statement; + +import edu.cornell.mannlib.vitro.webapp.beans.Individual; +import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchInputDocument; +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; +import edu.cornell.mannlib.vitro.webapp.searchengine.base.BaseSearchInputDocument; + +/** + * TODO NameFields should add the values as separate objects. + */ +public class NameFieldsTest { + private static final String INDIVIDUAL_URI = "http://mydomain.edu/individual/n3012"; + private static final String LABEL_PROPERTY_URI = "http://www.w3.org/2000/01/rdf-schema#label"; + private Model baseModel; + private NameFields nameFields; + private BaseSearchInputDocument doc; + + @Before + public void setup() { + baseModel = ModelFactory.createDefaultModel(); + + doc = new BaseSearchInputDocument(); + + RDFServiceModel rdfService = new RDFServiceModel(baseModel); + RDFServiceFactory rdfServiceFactory = new RDFServiceFactorySingle( + rdfService); + nameFields = new NameFields(rdfServiceFactory); + } + + @Test + public void nullIndividual() throws SkipIndividualException { + SearchInputDocument expected = new BaseSearchInputDocument(doc); + + assertResultingSearchDocument(null, expected); + } + + @Test + public void nullUri() throws SkipIndividualException { + SearchInputDocument expected = new BaseSearchInputDocument(doc); + + assertResultingSearchDocument(new IndividualStub(null), expected); + } + + @Test + public void foundNoLabels() throws SkipIndividualException { + SearchInputDocument expected = new BaseSearchInputDocument(doc); + expected.addField(NAME_RAW, ""); + + assertResultingSearchDocument(new IndividualStub(INDIVIDUAL_URI), + expected); + } + + @Test + public void foundOneLabel() throws SkipIndividualException { + baseModel.add(stmt(INDIVIDUAL_URI, LABEL_PROPERTY_URI, "label1")); + + SearchInputDocument expected = new BaseSearchInputDocument(doc); + expected.addField(NAME_RAW, "label1 "); + + assertResultingSearchDocument(new IndividualStub(INDIVIDUAL_URI), + expected); + } + + @Test + public void foundTwoLabels() throws SkipIndividualException { + baseModel.add(stmt(INDIVIDUAL_URI, LABEL_PROPERTY_URI, "label1")); + baseModel.add(stmt(INDIVIDUAL_URI, LABEL_PROPERTY_URI, "label2")); + + SearchInputDocument expected = new BaseSearchInputDocument(doc); + expected.addField(NAME_RAW, "label2 label1 "); + + assertResultingSearchDocument(new IndividualStub(INDIVIDUAL_URI), + expected); + } + + // ---------------------------------------------------------------------- + // Helper methods + // ---------------------------------------------------------------------- + + private Statement stmt(String subjectUri, String propertyUri, String literal) { + return baseModel.createStatement(baseModel.createResource(subjectUri), + baseModel.createProperty(propertyUri), + baseModel.createLiteral(literal)); + } + + private void assertResultingSearchDocument(Individual ind, + SearchInputDocument expected) throws SkipIndividualException { + nameFields.modifyDocument(ind, doc); + assertEquals(expected, doc); + } + + /** + * TODO Test plan + * + *
+	 * // also run SPARQL query to get rdfs:label values
+	 * String query = "SELECT ?label WHERE {  " + "<" + ind.getURI() + "> "
+	 * 		+ "<http://www.w3.org/2000/01/rdf-schema#label> ?label  }";
+	 * 
+	 * try {
+	 * 	RDFService rdfService = rsf.getRDFService();
+	 * 	BufferedReader stream = new BufferedReader(new InputStreamReader(
+	 * 			rdfService.sparqlSelectQuery(query, ResultFormat.CSV)));
+	 * 
+	 * 	StringBuffer buffer = new StringBuffer();
+	 * 	String line;
+	 * 
+	 * 	// throw out first line since it is just a header
+	 * 	stream.readLine();
+	 * 
+	 * 	while ((line = stream.readLine()) != null) {
+	 * 		buffer.append(line).append(' ');
+	 * 	}
+	 * 
+	 * 	log.debug("Adding labels for " + ind.getURI() + " \"" + buffer.toString()
+	 * 			+ "\"");
+	 * 	doc.addField(term.NAME_RAW, buffer.toString());
+	 * 
+	 * } catch (RDFServiceException e) {
+	 * 	log.error("could not get the rdfs:label for " + ind.getURI(), e);
+	 * } catch (IOException e) {
+	 * 	log.error("could not get the rdfs:label for " + ind.getURI(), e);
+	 * }
+	 * 
+	 * 
+ */ + +}