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

Commit

Permalink
Renaming integration test, now passes testing object creation and del…
Browse files Browse the repository at this point in the history
…etion
  • Loading branch information
escowles committed Aug 20, 2013
1 parent 443e523 commit 0dbbb80
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 88 deletions.
3 changes: 1 addition & 2 deletions src/main/java/org/fcrepo/indexer/FileSerializer.java
Expand Up @@ -68,7 +68,6 @@ public void update(String pid, String content) throws IOException {
* Remove the object from the index.
**/
public void remove(String pid) throws IOException {
// write empty file
update( pid, "" );
update(pid,""); // empty update
}
}
84 changes: 0 additions & 84 deletions src/test/java/org/fcrepo/indexer/FileSerializerIT.java

This file was deleted.

183 changes: 183 additions & 0 deletions src/test/java/org/fcrepo/indexer/IndexerGroupTest.java
@@ -0,0 +1,183 @@
/**
* 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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;

import static org.apache.abdera.model.Text.Type.TEXT;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;

import org.apache.abdera.Abdera;
import org.apache.abdera.model.Entry;
import org.apache.activemq.ActiveMQConnectionFactory;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/spring-test/test-container.xml"})
public class IndexerGroupTest {

protected static final int SERVER_PORT = Integer.parseInt(System
.getProperty("test.port", "8080"));

protected static final String serverAddress = "http://localhost:" +
SERVER_PORT + "/rest/objects/";

protected final PoolingClientConnectionManager connectionManager =
new PoolingClientConnectionManager();

protected static HttpClient client;

final private Logger logger = LoggerFactory
.getLogger(IndexerGroupTest.class);

private static String TEST_PID = "changeme_1001";

private static SimpleDateFormat fmt = new SimpleDateFormat("HHmmssSSS");

private File fileSerializerPath;
private Connection connection;

private IndexerGroup indexerGroup;

private FileSerializer fileSerializer;

private static TextMessage getMessage(String operation)
throws JMSException {
Abdera abdera = new Abdera();

Entry entry = abdera.newEntry();
entry.setTitle(operation, TEXT)
.setBaseUri("http://localhost:8080/rest");
entry.addCategory("xsd:string", TEST_PID, "fedora-types:pid");
entry.setContent("contentds");
StringWriter writer = new StringWriter();
try {
entry.writeTo(writer);
} catch (IOException e) {
// hush
}
String atomMessage = writer.toString();

TextMessage msg = mock(TextMessage.class);
when(msg.getText()).thenReturn(atomMessage);
return msg;
}

public IndexerGroupTest() throws JMSException {
// http client
client = new DefaultHttpClient(connectionManager);

// setup indexer
fileSerializer = new FileSerializer();
fileSerializerPath = new File("./target/test-classes/fileSerializer/");
fileSerializer.setPath( fileSerializerPath.getAbsolutePath() );

// indexer group
HashSet<Indexer> indexerSet = new HashSet<Indexer>();
indexerGroup = new IndexerGroup();
indexerSet.add( fileSerializer );
indexerGroup.setIndexers( indexerSet );
indexerGroup.setRepositoryURL( serverAddress );
}

@Test
public void indexerGroupUpdateTest() throws IOException, JMSException {
// create dummy object
final HttpPost method = new HttpPost(serverAddress + TEST_PID);
final HttpResponse response = client.execute(method);
assertEquals(201, response.getStatusLine().getStatusCode());

try {
Thread.sleep(1000); // wait for message to be processed
} catch ( Exception ex ) { }

// file should exist and contain triple starting with URI
File[] files = fileSerializerPath.listFiles();
assertNotNull(files);
assertTrue("There should be 1 file", files.length == 1);

File f = fileSerializerPath.listFiles()[0];
assertTrue("Filename doesn't match: " + f.getAbsolutePath(),
f.getName().startsWith(TEST_PID) );
assertTrue("File size too small: " + f.length(), f.length() > 500);
}

@Test
public void indexerGroupDeleteTest() throws IOException, JMSException {
// delete dummy object
final HttpDelete method = new HttpDelete(serverAddress + TEST_PID);
final HttpResponse response = client.execute(method);
assertEquals(204, response.getStatusLine().getStatusCode());

// create update message and send to indexer group
Message updateMessage = getMessage("purgeObject");
indexerGroup.onMessage( updateMessage );

try {
Thread.sleep(1000); // wait for message to be processed
} catch ( Exception ex ) { }

// file should exist and contain triple starting with URI
File[] files = fileSerializerPath.listFiles();
assertNotNull(files);
assertTrue("There should be 2 files", files.length == 2);

Arrays.sort(files); // sort files by filename (i.e., creation time)
File f1 = files[0];
File f2 = files[1];
assertTrue("Filename doesn't match: " + f1.getAbsolutePath(),
f1.getName().startsWith(TEST_PID) );
assertTrue("File size too small: " + f1.length(), f1.length() > 500);
assertTrue("Filename doesn't match: " + f2.getAbsolutePath(),
f2.getName().startsWith(TEST_PID) );
assertTrue("File size should be 0: " + f2.length(), f2.length() == 0);
}
}
2 changes: 1 addition & 1 deletion src/test/resources/spring-test/indexer.xml
Expand Up @@ -36,7 +36,7 @@

<!-- Message Driven POJO (MDP) that manages individual indexers -->
<bean id="indexerGroup" class="org.fcrepo.indexer.IndexerGroup">
<property name="repositoryURL" value="http://localhost:${test.port:8080}/rest" />
<property name="repositoryURL" value="http://localhost:${test.port:8080}/rest/objects/" />
<property name="indexers">
<set>
<ref bean="fileSerializer"/>
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/spring-test/test-container.xml
Expand Up @@ -12,4 +12,4 @@
<property name="configLocation" value="classpath:web.xml" />
</bean>

</beans>
</beans>

0 comments on commit 0dbbb80

Please sign in to comment.