diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/SolrJsonReconcileServlet.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/SolrJsonReconcileServlet.java index b314dc732..e5ce4230e 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/SolrJsonReconcileServlet.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/SolrJsonReconcileServlet.java @@ -97,7 +97,7 @@ public class SolrJsonReconcileServlet extends VitroHttpServlet { } } - protected JSONObject getResult(VitroRequest vreq, HttpServletRequest req, + private JSONObject getResult(VitroRequest vreq, HttpServletRequest req, HttpServletResponse resp) throws ServletException { HashMap searchWithTypeMap = new HashMap(); @@ -363,7 +363,7 @@ public class SolrJsonReconcileServlet extends VitroHttpServlet { return qJson; } - private SolrQuery getQuery(String queryStr, String searchType, int limit, ArrayList propertiesList) { + protected SolrQuery getQuery(String queryStr, String searchType, int limit, ArrayList propertiesList) { if ( queryStr == null) { log.error("There was no parameter '"+ PARAM_QUERY diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/SolrJsonReconcileServletTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/SolrJsonReconcileServletTest.java index 2179b825f..2bbe42c31 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/SolrJsonReconcileServletTest.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/SolrJsonReconcileServletTest.java @@ -2,20 +2,25 @@ package edu.cornell.mannlib.vitro.webapp.controller; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - import java.net.URL; +import java.util.ArrayList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +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.javax.servlet.http.HttpServletRequestStub; import stubs.javax.servlet.http.HttpServletResponseStub; + import edu.cornell.mannlib.vitro.testing.AbstractTestClass; /** @@ -25,6 +30,9 @@ import edu.cornell.mannlib.vitro.testing.AbstractTestClass; */ public class SolrJsonReconcileServletTest extends AbstractTestClass { + + + private HttpServletRequestStub request; private HttpServletResponseStub response; private SolrJsonReconcileServlet reconcile; @@ -50,11 +58,50 @@ public class SolrJsonReconcileServletTest extends AbstractTestClass { jsonResult = reconcile.getMetadata(request, response, defaultNamespace, defaultTypeList, serverName, serverPort); schemaSpaceOutput = jsonResult.getString("schemaSpace"); } catch (ServletException e) { - System.err.println("SolrJsonReconcileServletTest ServletException: " + e); + System.err.println("SolrJsonReconcileServletTest getMetadata ServletException: " + e); } catch (JSONException e) { - System.err.println("SolrJsonReconcileServletTest JSONException: " + e); + System.err.println("SolrJsonReconcileServletTest getMetadata JSONException: " + e); + } + Assert.assertNotNull("output should not be null", jsonResult); + Assert.assertEquals("schemaSpaceOutput", defaultNamespace, schemaSpaceOutput); + } + + @Test + public void getQuery() { + // contruct query + int rowNum = 3; + + String nameStr = "Joe"; + String nameType = "http://xmlns.com/foaf/0.1/Person"; + + ArrayList propertiesList = new ArrayList(); + String orgStr = "Something"; + String orgType = "http://xmlns.com/foaf/0.1/Organization"; + String[] properties = {orgType, orgStr}; + propertiesList.add(properties); + + // test getQuery + SolrQuery solrQuery = 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()); + } + + 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); + Matcher regexMatcher = regex.matcher(resultStr); + Assert.assertTrue(message, regexMatcher.find()); + } catch (PatternSyntaxException ex) { + // Syntax error in the regular expression } - assertNotNull("output should not be null", jsonResult); - assertEquals("schemaSpaceOutput", defaultNamespace, schemaSpaceOutput); } }