Skip to content

Commit

Permalink
[ issue #15 ] Sparql11UpdateRdfDataLoader (unit) test case
Browse files Browse the repository at this point in the history
  • Loading branch information
agazzarini committed Mar 23, 2015
1 parent 996a7fd commit 539c081
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.gazzax.labs.solrdf.handler.update;

import static org.gazzax.labs.solrdf.Strings.isNullOrEmptyString;

import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.handler.loader.ContentStreamLoader;
Expand Down Expand Up @@ -30,10 +34,14 @@ public void load(
final ContentStream stream,
final UpdateRequestProcessor processor) throws Exception {

final String q = request.getParams().get(CommonParams.Q);
if (isNullOrEmptyString(q)) {
throw new SolrException(ErrorCode.BAD_REQUEST, "Invalid query : " + q);
}

UpdateAction.execute(
UpdateFactory.create(
request.getParams().get(CommonParams.Q)),
new SolRDFDatasetGraph(request, response));

}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.gazzax.labs.solrdf.handler;

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

import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -12,7 +12,6 @@

import javax.servlet.http.HttpServletRequest;

import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.core.SolrCore;
import org.apache.solr.request.SolrQueryRequest;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.gazzax.labs.solrdf.handler.update;

import static org.gazzax.labs.solrdf.TestUtility.randomString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.update.processor.UpdateRequestProcessor;
import org.junit.Before;
import org.junit.Test;

import com.hp.hpl.jena.query.QueryParseException;

/**
* Test case for {@link Sparql11UpdateRdfDataLoader}.
*
* @author Andrea Gazzarini
* @since 1.0
*/
public class Sparql11UpdateRdfDataLoaderTestCase {
private Sparql11UpdateRdfDataLoader cut;
private SolrQueryRequest request;
private SolrQueryResponse response;
private ContentStream stream;
private UpdateRequestProcessor processor;
private ModifiableSolrParams parameters;

@Before
public void setUp() {
cut = new Sparql11UpdateRdfDataLoader();
request = mock(SolrQueryRequest.class);
response = mock(SolrQueryResponse.class);
parameters = new ModifiableSolrParams();
stream = mock(ContentStream.class);
processor = mock(UpdateRequestProcessor.class);

when(request.getParams()).thenReturn(parameters);
}

/**
* In case of a null query a {@link QueryParseException} must be raised.
*/
@Test
public void nullOrEmptyQuery() {
final String [] invalidQueries = {"", " "};
for (final String invalidQuery : invalidQueries) {
parameters.set(CommonParams.Q, invalidQuery);
try {
cut.load(request, response, stream, processor);
fail();
} catch (final Exception expected) {
assertTrue(expected instanceof SolrException);
assertEquals(ErrorCode.BAD_REQUEST.code, ((SolrException)expected).code());
}
}
}

/**
* In case of an invalid query a {@link QueryParseException} must be raised.
*/
@Test
public void invalidQuery() {
final String [] invalidQueries = {"BLABALBALABLA", randomString()};

for (final String invalidQuery : invalidQueries) {
parameters.set(CommonParams.Q, invalidQuery);
try {
cut.load(request, response, stream, processor);
fail();
} catch (final Exception expected) {
assertTrue(expected instanceof QueryParseException);
}
}
}
}

0 comments on commit 539c081

Please sign in to comment.