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

Commit

Permalink
Making file-based tests less brittle, introducing new TestIndexer cla…
Browse files Browse the repository at this point in the history
…ss with associated (non-functional) IndexerGroupIT tests for more direct testing of IndexerGroup functionality
  • Loading branch information
escowles committed Nov 26, 2013
1 parent 8355d05 commit 77d7454
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 4 deletions.
@@ -0,0 +1,78 @@
/**
* 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;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
* Indexer implementation that tracks which PIDs it has received messages for.
*
* @author Esmé Cowles
* Date: Nov. 25, 2013
**/
public class TestIndexer implements Indexer {
private final Logger logger = LoggerFactory.getLogger(TestIndexer.class);

private Set<String> updates;
private Set<String> removes;

/**
* Default constructor.
**/
public TestIndexer() {
updates = new HashSet<String>();
removes = new HashSet<String>();
}

/**
* Create or update an index entry for the object.
**/
public void update(String pid, String content) throws IOException {
logger.warn( "update: {}", pid);
updates.add(pid);
}

/**
* Remove the object from the index.
**/
public void remove(String pid) throws IOException {
logger.warn( "remove: {}", pid);
removes.add(pid);
}

/**
* Test whether an update message has been received for a PID.
**/
public boolean receivedUpdate(String pid) {
logger.warn( "receivedUpdate: {}, {}", pid, updates.contains(pid));
return updates.contains(pid);
}

/**
* Test whether a remove message has been received for a PID.
**/
public boolean receivedRemove(String pid) {
logger.warn( "receivedRemove: {}, {}", pid, removes.contains(pid));
return removes.contains(pid);
}
}
Expand Up @@ -38,6 +38,7 @@
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.iq80.leveldb.util.FileUtils;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
Expand Down Expand Up @@ -75,6 +76,8 @@ public class IndexerGroupIT {
private SparqlIndexer sparqlIndexer;
@Inject
private FileSerializer fileSerializer;
@Inject
private TestIndexer testIndexer;
private File fileSerializerPath;

private static TextMessage getMessage(String operation,
Expand Down Expand Up @@ -158,11 +161,11 @@ public void indexerGroupDeleteTest() throws Exception {
File[] files = fileSerializerPath.listFiles(filter);

assertNotNull(files);
assertEquals(2, files.length);
assertTrue("Should have multiple files", files.length > 1);

Arrays.sort(files); // sort files by filename (i.e., creation time)
File f1 = files[0];
File f2 = files[1];
File f2 = files[files.length - 1];
assertTrue("Filename doesn't match: " + f1.getAbsolutePath(),
f1.getName().startsWith(pid) );
assertTrue("File size too small: " + f1.length(), f1.length() > 500);
Expand Down Expand Up @@ -211,7 +214,7 @@ private void waitForFiles(int expectedFiles, FilenameFilter filter) throws Inter
long maxWait = 15000; // 15 seconds

List<File> files = FileUtils.listFiles(fileSerializerPath, filter);
while (expectedFiles != files.size() && (elapsed < maxWait)) {
while (expectedFiles > files.size() && (elapsed < maxWait)) {
Thread.sleep(restingWait);
files = FileUtils.listFiles(fileSerializerPath, filter);

Expand All @@ -233,4 +236,46 @@ private void waitForTriples(String pid) throws InterruptedException {
}
}

@Ignore
@Test
public void testIndexerUpdate() throws Exception {
// create dummy object
final String uri = serverAddress + "foo";
final HttpPost method = new HttpPost(uri);
final HttpResponse response = client.execute(method);
assertEquals(201, response.getStatusLine().getStatusCode());

// test indexer should receive update event
waitForUpdate(uri);
assertTrue("Test indexer should receive update message",testIndexer.receivedUpdate(uri));
}

@Ignore
@Test
public void testIndexerDelete() throws Exception {
// create dummy object
testIndexerUpdate();

final String uri = serverAddress + "foo";
final HttpDelete method = new HttpDelete(uri);
final HttpResponse response = client.execute(method);
assertEquals(204, response.getStatusLine().getStatusCode());

// test indexer should receive update event
waitForRemove(uri);
assertTrue("Test indexer should receive remove message",testIndexer.receivedRemove(uri));
}

private void waitForRemove( String uri ) throws Exception {
for ( int n = 0; n < 10 && !testIndexer.receivedRemove(uri); n++ ) {
Thread.sleep(1000);
}
}

private void waitForUpdate( String uri ) throws Exception {
for ( int n = 0; n < 10 && !testIndexer.receivedUpdate(uri); n++ ) {
Thread.sleep(1000);
}
}

}
Expand Up @@ -29,11 +29,14 @@
-->
</bean>

<!-- test indexer -->
<bean id="testIndexer" class="org.fcrepo.indexer.TestIndexer"/>

<!-- file serializer -->
<bean id="fileSerializer" class="org.fcrepo.indexer.FileSerializer">
<property name="path" value="./target/test-classes/fileSerializer/"/>
</bean>

<!-- Solr Indexer START-->
<bean id="solrIndexer" class="org.fcrepo.indexer.SolrIndexer"/>

Expand Down Expand Up @@ -61,6 +64,7 @@
<property name="repositoryURL" value="http://localhost:${test.port:8080}" />
<property name="indexers">
<set>
<ref bean="testIndexer"/>
<ref bean="fileSerializer"/>
<ref bean="sparqlUpdate"/>
</set>
Expand Down

0 comments on commit 77d7454

Please sign in to comment.