// System.setProperty("bixo.root.level", "TRACE");
final int numPages = 1;
final int port = 8089;
Payload payload = new Payload();
payload.put("payload-field-1", 1);
Tap in = makeInputData(platform, "testRedirectException", "localhost:" + port, numPages, payload);
Pipe pipe = new Pipe("urlSource");
BaseScoreGenerator scorer = new FixedScoreGenerator();
FetcherPolicy policy = new FetcherPolicy();
policy.setRedirectMode(RedirectMode.FOLLOW_TEMP);
BaseFetcher fetcher = new SimpleHttpFetcher(1, policy, ConfigUtils.BIXO_TEST_AGENT);
FetchPipe fetchPipe = new FetchPipe(pipe, scorer, fetcher, 1);
String output = "build/test/FetchPipeTest/testRedirectException";
BasePath outputPath = platform.makePath(output);
BasePath statusPath = platform.makePath(outputPath, "status");
BasePath contentPath = platform.makePath(outputPath, "content");
Tap status = platform.makeTap(platform.makeBinaryScheme(StatusDatum.FIELDS), statusPath, SinkMode.REPLACE);
Tap content = platform.makeTap(platform.makeBinaryScheme(FetchedDatum.FIELDS), contentPath, SinkMode.REPLACE);
// Finally we can run it.
FlowConnector flowConnector = platform.makeFlowConnector();
Flow flow = flowConnector.connect(in, FetchPipe.makeSinkMap(status, content), fetchPipe);
TestWebServer webServer = null;
try {
webServer = new TestWebServer(new RedirectResponseHandler(true), port);
flow.complete();
} finally {
webServer.stop();
}
// Verify numPages fetched and numPages status entries were saved.
Tap validate = platform.makeTap(platform.makeBinaryScheme(FetchedDatum.FIELDS), contentPath);
TupleEntryIterator tupleEntryIterator = validate.openForRead(platform.makeFlowProcess());
Assert.assertFalse(tupleEntryIterator.hasNext());
tupleEntryIterator.close();
validate = platform.makeTap(platform.makeBinaryScheme(StatusDatum.FIELDS), statusPath);
tupleEntryIterator = validate.openForRead(platform.makeFlowProcess());
int totalEntries = 0;
boolean[] fetchedPages = new boolean[numPages];
while (tupleEntryIterator.hasNext()) {
TupleEntry entry = tupleEntryIterator.next();
totalEntries += 1;
// Verify we can convert properly
StatusDatum sd = new StatusDatum(entry);
Assert.assertTrue(sd.getException() instanceof RedirectFetchException);
RedirectFetchException redirectException =
(RedirectFetchException)(sd.getException());
Assert.assertEquals(RedirectResponseHandler.REDIRECT_TARGET_URL,
redirectException.getRedirectedUrl());
Assert.assertEquals(payload.get("payload-field-1"),
sd.getPayloadValue("payload-field-1"));
// Verify that we got one of each page
String url = sd.getUrl();
Assert.assertNotNull(url);