Skip to content

Commit

Permalink
[ issue #15 ] Added (not yet completed) SparqlSearchComponent test case
Browse files Browse the repository at this point in the history
  • Loading branch information
agazzarini committed Feb 15, 2015
1 parent 5eaa108 commit 1b7001a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.search.QParser;
import org.apache.solr.search.SyntaxError;
import org.gazzax.labs.solrdf.Names;
import org.gazzax.labs.solrdf.graph.SolRDFDatasetGraph;
import org.gazzax.labs.solrdf.search.qparser.SparqlQuery;
Expand All @@ -34,11 +35,7 @@ public void process(final ResponseBuilder responseBuilder) throws IOException {
final SolrQueryResponse response = responseBuilder.rsp;

try {
final QParser parser = QParser.getParser(
queryString(request),
DEFAULT_DEF_TYPE,
request);

final QParser parser = qParser(request);
final SparqlQuery wrapper = (SparqlQuery) parser.getQuery();
final Query query = wrapper.getQuery();

Expand Down Expand Up @@ -86,6 +83,20 @@ public void prepare(final ResponseBuilder responseBuilder) {
// Nothing to be done here...
}

/**
* Returns the {@link QParser} associated with this request.
*
* @param request the {@link SolrQueryRequest}.
* @return the {@link QParser} associated with this request.
* @throws SyntaxError in case of syntax errors.
*/
QParser qParser(final SolrQueryRequest request) throws SyntaxError {
return QParser.getParser(
queryString(request),
DEFAULT_DEF_TYPE,
request);
}

/**
* Returns the query string associated with the current request.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.gazzax.labs.solrdf.search.component;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.request.SolrQueryRequest;
import org.junit.Before;
import org.junit.Test;

/**
* {@link SparqlSearchComponent} test case.
*
* @author Andrea Gazzarini
* @since 1.0
*/
public class SparqlSearchComponentTestCase {
private SparqlSearchComponent cut;
private SolrQueryRequest request;
private ModifiableSolrParams params = new ModifiableSolrParams();

private final static String SAMPLE_SPARQL_SELECT = "SELECT * WHERE { ?s ?p ?o }";
private final static String ANOTHER_SAMPLE_SPARQL_SELECT = "SELECT ?s WHERE { ?s ?p ?o }";

@Before
public void setUp() {
cut = new SparqlSearchComponent();
request = mock(SolrQueryRequest.class);
when(request.getParams()).thenReturn(params);
}

/**
* Query string can be passed in request with the "q" parameter.
*/
@Test
public void queryStringIn_q_parameter() {
params.add(CommonParams.Q, SAMPLE_SPARQL_SELECT);

final String queryString = cut.queryString(request);
assertEquals(SAMPLE_SPARQL_SELECT, queryString);
}

/**
* Query string can be passed in request with the "query" parameter.
*/
@Test
public void queryStringIn_query_parameter() {
params.add(CommonParams.QUERY, SAMPLE_SPARQL_SELECT);

final String queryString = cut.queryString(request);
assertEquals(SAMPLE_SPARQL_SELECT, queryString);
}

/**
* If both "q" and "query" parameters have been specified, then "q" got a higher priority.
*/
@Test
public void queryAndQ() {
params.add(CommonParams.QUERY, SAMPLE_SPARQL_SELECT);
params.add(CommonParams.Q, ANOTHER_SAMPLE_SPARQL_SELECT);

final String queryString = cut.queryString(request);
assertEquals(ANOTHER_SAMPLE_SPARQL_SELECT, queryString);
}

/**
* If the incoming request doesn't contain a query then an exception is thrown.
*/
@Test
public void nullQuery() {
assertNull(request.getParams().get(CommonParams.Q));
assertNull(request.getParams().get(CommonParams.QUERY));

try {
cut.queryString(request);
} catch (final SolrException expected) {
assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, expected.code());
}
}
}

0 comments on commit 1b7001a

Please sign in to comment.