Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Commit

Permalink
More unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Dec 17, 2013
1 parent 068d6f8 commit df70a5b
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 15 deletions.
Expand Up @@ -166,13 +166,6 @@ public UpdateResponse call() {
};
}

/**
* @return the {@link SolrServer} in use
*/
public SolrServer getServer() {
return server;
}

@Override
public IndexerType getIndexerType() {
return NAMEDFIELDS;
Expand Down
Expand Up @@ -19,25 +19,37 @@
import static java.lang.System.currentTimeMillis;
import static java.lang.Thread.sleep;
import static java.util.Arrays.asList;
import static java.util.UUID.randomUUID;
import static org.apache.solr.core.CoreContainer.createAndLoad;
import static org.fcrepo.indexer.Indexer.IndexerType.NAMEDFIELDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

import static org.mockito.Mockito.any;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutionException;

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.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.core.CoreContainer;
import org.fcrepo.indexer.NamedFields;
import org.fcrepo.indexer.solr.SolrIndexer;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.slf4j.Logger;


Expand All @@ -50,30 +62,98 @@ public class SolrIndexerTest {

private final String solrHome = "target/test-classes/solr";

private SolrIndexer indexer;
private SolrIndexer testIndexer;

final CoreContainer container =
createAndLoad(solrHome, new File(solrHome, "solr.xml"));

private SolrServer server = new EmbeddedSolrServer(container, "testCore");

private SolrServer server;
@Mock
private SolrServer mockServer;

@Mock
private UpdateResponse mockUpdateResponse;

private static final Logger LOGGER = getLogger(SolrIndexerTest.class);

private static final long TIMEOUT = 15000;

private static final long TIME_TO_WAIT_STEP = 1000;

private static Boolean successfulExecution = false;


@Before
public void setUp() throws Exception {
final CoreContainer container =
createAndLoad(solrHome, new File(solrHome, "solr.xml"));
LOGGER.debug("Using Solr home: {}", new File(container.getSolrHome())
.getAbsolutePath());
server = new EmbeddedSolrServer(container, "testCore");
indexer = new SolrIndexer(server);
testIndexer = new SolrIndexer(server);
initMocks(this);
}

@Test
public void testMutatesWithBadResults() throws SolrServerException, IOException,
InterruptedException, ExecutionException {
final String id = "testBadUpdate:" + randomUUID();
final SolrIndexer hold = testIndexer;
when(mockServer.add(any(SolrInputDocument.class))).thenReturn(
mockUpdateResponse);
when(mockServer.deleteById(any(String.class))).thenReturn(
mockUpdateResponse);
// update failure
when(mockUpdateResponse.getStatus()).thenReturn(1);
testIndexer = new SolrIndexer(mockServer);
final Collection<String> values = asList(id);
final NamedFields testContent = new NamedFields(of("id", values));

UpdateResponse result = testIndexer.update(id, testContent).get();
assertEquals("Got wrong update response code!", 1, result.getStatus());
result = testIndexer.remove(id).get();
assertEquals("Got wrong update response code!", 1, result.getStatus());


testIndexer = hold;
}

@Test(expected = ExecutionException.class)
public void testUpdateThatExplodes() throws SolrServerException,
IOException, InterruptedException, ExecutionException {
final String id = "testExplodingUpdate:" + randomUUID();
final SolrIndexer hold = testIndexer;
// update failure
when(mockServer.add(any(SolrInputDocument.class))).thenThrow(
new SolrServerException("Expected."));

testIndexer = new SolrIndexer(mockServer);
final Collection<String> values = asList(id);
final NamedFields testContent = new NamedFields(of("id", values));

testIndexer.update(id, testContent).get();
testIndexer = hold;
}

@Test(expected = ExecutionException.class)
public void testUpdateWithAlternateExplosion() throws SolrServerException,
IOException, InterruptedException, ExecutionException {
final String id = "testExplodingUpdate2:" + randomUUID();
final SolrIndexer hold = testIndexer;
// update failure
when(mockServer.add(any(SolrInputDocument.class))).thenThrow(
new IOException("Expected."));

testIndexer = new SolrIndexer(mockServer);
final Collection<String> values = asList(id);
final NamedFields testContent = new NamedFields(of("id", values));

testIndexer.update(id, testContent).get();
testIndexer = hold;
}

@Test
public void testUpdate() throws SolrServerException, IOException, InterruptedException {
doUpdate("456");

}

private void doUpdate(final String pid) throws SolrServerException, IOException, InterruptedException {
Expand All @@ -82,7 +162,7 @@ private void doUpdate(final String pid) throws SolrServerException, IOException,
LOGGER.debug(
"Trying update operation with identifier: {} and content: \"{}\".",
pid, testContent);
indexer.update(pid, testContent);
testIndexer.update(pid, testContent);

final SolrParams query = new SolrQuery("id:" + pid);
List<SolrDocument> results = server.query(query).getResults();
Expand All @@ -102,7 +182,7 @@ private void doUpdate(final String pid) throws SolrServerException, IOException,
public void testRemove() throws IOException, SolrServerException, InterruptedException {
final String pid = "123";
doUpdate(pid);
indexer.remove(pid);
testIndexer.remove(pid);
final SolrParams query = new SolrQuery("id:" + pid);
List<SolrDocument> results = server.query(query).getResults();
Boolean success = results.size() == 0;
Expand All @@ -117,4 +197,30 @@ public void testRemove() throws IOException, SolrServerException, InterruptedExc
assertTrue("Found our record when we shouldn't have!", success);
}

@Test
public void testGetIndexerType() {
assertEquals("Got wrong testIndexer type!", NAMEDFIELDS, testIndexer
.getIndexerType());
}

@Test
public void testExecutorService() throws InterruptedException {
testIndexer.executorService().execute(new Runnable() {

@Override
public void run() {
successfulExecution = true;

}});
final Long start = currentTimeMillis();
while (!successfulExecution && (currentTimeMillis() - start ) < TIMEOUT) {
sleep(TIME_TO_WAIT_STEP);
}
assertTrue(
"Failed to execute a task in this indexer's executor service before "
+ TIMEOUT + "ms!", successfulExecution);
}



}
@@ -0,0 +1,38 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.indexer.sparql;

import static org.fcrepo.indexer.Indexer.IndexerType.RDF;
import static org.junit.Assert.assertEquals;
import static org.slf4j.LoggerFactory.getLogger;

import org.junit.Test;
import org.slf4j.Logger;


public class SparqlIndexerTest {

private static final Logger LOGGER = getLogger(SparqlIndexerTest.class);

private SparqlIndexer testIndexer = new SparqlIndexer();

@Test
public void testGetIndexerType() {
assertEquals("Got wrong indexer type!", RDF, testIndexer
.getIndexerType());
LOGGER.debug("Received correct indexer type.");
}
}

0 comments on commit df70a5b

Please sign in to comment.