package eu.balticdiversity.feedreaders;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import fi.luomus.commons.reporting.ErrorReporingToSystemErr;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
public class FeedReaderTest {
@Test
public void serviceStartsStops() throws Exception {
FeedReaderData data = new FeedReaderData();
data.setFeedName("mock");
data.setStartAtTimestamp(0L);
data.setUri(new URI("http://www.mock.com"));
FeedReaderService reader = new FeedReaderWithMockSeq(data, new MockFeedReaderClient(), new MockDAO());
assertFalse(reader.isRunning());
reader.start();
Thread.sleep(100);
assertTrue(reader.isRunning());
reader.stop();
Thread.sleep(100);
assertFalse(reader.isRunning());
}
@Test
public void entriesSaved() throws Exception {
FeedReaderData data = new FeedReaderData();
data.setFeedName("mock");
data.setUri(new URI("http://www.mock.com"));
data.setStartAtTimestamp(0L);
data.setErrorReporter(new ErrorReporingToSystemErr());
MockDAO mockDAO = new MockDAO();
MockFeedReaderClient mockFeedReaderClient = new MockFeedReaderClient();
FeedReaderService reader = new FeedReaderWithMockSeq(data, mockFeedReaderClient, mockDAO);
reader.insertFromFeedToDatabase();
assertEquals(mockFeedReaderClient.docs.size(), mockDAO.uris.size());
}
@Test
public void sequenceUpdated() throws Exception {
FeedReaderData data = new FeedReaderData();
data.setFeedName("mock");
data.setUri(new URI("http://www.mock.com"));
data.setStartAtTimestamp(0L);
data.setErrorReporter(new ErrorReporingToSystemErr());
MockDAO mockDAO = new MockDAO();
MockFeedReaderClient mockFeedReaderClient = new MockFeedReaderClient();
FeedReaderWithMockSeq reader = new FeedReaderWithMockSeq(data, mockFeedReaderClient, mockDAO);
assertNull(mockDAO.sequence);
reader.insertFromFeedToDatabase();
assertEquals(mockDAO.sequence, Long.valueOf(mockFeedReaderClient.docs.size()));
}
@Test
public void sequenceRead() throws Exception {
FeedReaderData data = new FeedReaderData();
data.setFeedName("mock");
data.setUri(new URI("http://www.mock.com"));
data.setStartAtTimestamp(0L);
data.setErrorReporter(new ErrorReporingToSystemErr());
MockDAO mockDAO = new MockDAO();
MockFeedReaderClient mockFeedReaderClient = new MockFeedReaderClient();
FeedReaderWithMockSeq reader = new FeedReaderWithMockSeq(data,
mockFeedReaderClient,
mockDAO);
reader.insertFromFeedToDatabase();
assertEquals(mockFeedReaderClient.docs.size(), mockDAO.uris.size());
assertEquals(Long.valueOf(mockFeedReaderClient.docs.size()), mockDAO.sequence);
reader.insertFromFeedToDatabase();
assertEquals(mockFeedReaderClient.docs.size(), mockDAO.uris.size());
assertEquals(Long.valueOf(mockFeedReaderClient.docs.size()), mockDAO.sequence);
}
@Test
public void entriesSavedCorrectly() throws Exception {
FeedReaderData data = new FeedReaderData();
data.setFeedName("mock");
data.setUri(new URI("http://www.mock.com"));
data.setStartAtTimestamp(0L);
data.setErrorReporter(new ErrorReporingToSystemErr());
MockDAO mockDAO = new MockDAO();
mockDAO.setLastReadSequence(0L);
MockFeedReaderClient mockFeedReaderClient = new MockFeedReaderClient();
FeedReaderWithMockSeq reader = new FeedReaderWithMockSeq(data,
mockFeedReaderClient,
mockDAO);
reader.insertFromFeedToDatabase();
assertEquals(mockFeedReaderClient.docs.size(), mockDAO.uris.size());
assertEquals(mockFeedReaderClient.getFeedEntriesFromSeq(new URI("http://mock.fi"), 1, 1), mockDAO.uris);
}
private class MockDAO extends DAO {
public Long sequence = null;
public List<FeedEntry> uris = new ArrayList<FeedEntry>();
public MockDAO() {
super(null, 1);
}
@Override
public void addDocumentToQueue(FeedEntry feedEntryWithSeqID) throws SQLException {
uris.add(feedEntryWithSeqID);
}
@Override
public void setLastReadSequence(Long lastReadSequence) throws SQLException {
sequence = lastReadSequence;
}
@Override
public Long getLastReadSequence() throws SQLException {
return sequence;
}
@Override
public void close() {
}
}
private class FeedReaderWithMockSeq extends FeedReaderService {
public FeedReaderWithMockSeq(FeedReaderData data, FeedReaderClient feedReaderClient, DAO dao) throws Exception {
super(data, feedReaderClient, dao);
}
}
private class MockFeedReaderClient implements FeedReaderClient {
private final Map<FeedEntryWithSeqID, String> docs = new HashMap<FeedEntryWithSeqID, String>();
public MockFeedReaderClient() throws URISyntaxException {
init();
}
@Override
public List<FeedEntryWithSeqID> getFeedEntriesFromSeq(URI uri, long seqId, int limit) throws FeedReaderClientException {
List<FeedEntryWithSeqID> entries = new ArrayList<FeedEntryWithSeqID>();
for (FeedEntryWithSeqID feedEntryWithSeqID : docs.keySet()) {
if (feedEntryWithSeqID.getSeqID() >= seqId) {
entries.add(feedEntryWithSeqID);
}
}
Collections.sort(entries, new Comparator<FeedEntryWithSeqID>() {
@Override
public int compare(FeedEntryWithSeqID o1, FeedEntryWithSeqID o2) {
return (int) (o1.getSeqID() - o2.getSeqID());
}
});
return entries;
}
@Override
public List<FeedEntryWithSeqID> getFeedEntriesSinceTimestamp(URI uri, long seqId, int limit) throws FeedReaderClientException {
return this.getFeedEntriesFromSeq(uri, 0, limit);
}
@Override
public String getFeedDocument(URI uri) throws FeedReaderClientException {
throw new RuntimeException("not implemented");
}
@Override
public void close() throws IOException {
}
private void init() throws URISyntaxException {
for (int i = 1; i < 5; i++) {
FeedEntryWithSeqID entry = new FeedEntryWithSeqID();
entry.setSeqID((long) i);
entry.setDocumentId("id " + i);
entry.setUri(new URI("http://www.mock.com/" + i));
docs.put(entry, "doc " + i);
}
}
}
}