forked from apache/nutch
-
Notifications
You must be signed in to change notification settings - Fork 3
Add end to end tests for the SitemapInjector #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6ba78a0
tests: set up a test environment for sitemaps
lfoppiano 1709bb4
tests: make the test failing with the data should be returned
lfoppiano 0d3450c
chore: add TODOs to make this experiment more explicit
lfoppiano eff16aa
fix: make it portable
lfoppiano c9d525e
fix: use human readable sitemaps
lfoppiano d38d4b5
fix: update tests
lfoppiano 7e75b33
fix: update tests (2)
lfoppiano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
181 changes: 181 additions & 0 deletions
181
src/test/org/apache/nutch/crawl/TestSitemapInjector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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.apache.nutch.crawl; | ||
|
|
||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.fs.FileSystem; | ||
| import org.apache.hadoop.fs.Path; | ||
| import org.apache.hadoop.io.SequenceFile; | ||
| import org.apache.hadoop.io.SequenceFile.Reader.Option; | ||
| import org.apache.hadoop.io.Text; | ||
| import org.apache.logging.log4j.core.config.Configurator; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * End-to-end test for {@link SitemapInjector}: points the injector at a local | ||
| * {@code file://} sitemap, runs the full two-step MapReduce pipeline, then | ||
| * reads the resulting CrawlDb and asserts the expected URLs (including | ||
| * hreflang alternates) are present. | ||
| */ | ||
| public class TestSitemapInjector { | ||
|
|
||
| private Configuration conf; | ||
| private FileSystem fs; | ||
| private final static Path testdir = new Path("build/test/sitemap-inject-test"); | ||
| private Path crawldbPath; | ||
| private Path urlPath; | ||
| private String sitemapUrl; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() throws Exception { | ||
| Configurator.setLevel( | ||
| "org.apache.hadoop.mapred", org.apache.logging.log4j.Level.DEBUG); | ||
|
|
||
| conf = CrawlDBTestUtil.createContext().getConfiguration(); | ||
|
|
||
| conf.set("plugin.folders", new File("build/plugins").getAbsolutePath()); | ||
| conf.set("plugin.includes", | ||
| "protocol-file|urlfilter-regex|index-basic"); | ||
| conf.setInt("http.time.limit", 120); | ||
| conf.setBoolean("mime.type.magic", false); | ||
| conf.setBoolean("db.injector.sitemap.check-cross-submits", false); | ||
| conf.setInt("mapreduce.mapper.multithreadedmapper.threads", 1); | ||
| conf.set("http.filter.ipaddress.exclude", ""); | ||
|
|
||
| conf.set("urlfilter.regex.rules", "+."); | ||
| // conf.set("urlfilter.regex.file", | ||
| // new File("src/testresources/regex-urlfilter-test.txt").getAbsoluteFile().toURI().toString()); | ||
|
|
||
| urlPath = new Path(testdir, "urls"); | ||
| crawldbPath = new Path(testdir, "crawldb"); | ||
| fs = FileSystem.get(conf); | ||
| fs.delete(testdir, true); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() throws IOException { | ||
| fs.delete(testdir, true); | ||
| } | ||
|
|
||
| @Test | ||
| public void injectsUrlsFromLocalSitemap1() throws Exception { | ||
| sitemapUrl = resolveFixture("sitemaps/sitemap.example.1.xml"); | ||
|
|
||
| List<String> seeds = new ArrayList<>(); | ||
| seeds.add(sitemapUrl); | ||
| CrawlDBTestUtil.generateSeedList(fs, urlPath, seeds); | ||
|
|
||
| SitemapInjector sitemapInjector = new SitemapInjector(); | ||
| sitemapInjector.setConf(conf); | ||
| sitemapInjector.inject(crawldbPath, urlPath); | ||
|
|
||
| List<String> injected = readCrawldb(); | ||
|
|
||
| assertFalse(injected.isEmpty(), | ||
| "SitemapInjector produced an empty CrawlDb"); | ||
|
|
||
| // Primary <loc> URL from sitemap.example.1.xml | ||
| assertTrue( | ||
| injected.contains("https://example.com/sitemap.html"), | ||
| "Primary <loc> URL missing from CrawlDb"); | ||
|
|
||
| // hreflang alternate from the same <url> block - exercises the | ||
| // sitemap-localized-links extraction path. | ||
| assertTrue( | ||
| injected.contains("https://example.com/tr/en/sitemap.html"), | ||
| "hreflang alternate missing from CrawlDb (localized-links extraction failed)"); | ||
|
|
||
| assertThat(injected.size(), is(6)); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void injectsUrlsFromLocalSitemap2() throws Exception { | ||
| sitemapUrl = resolveFixture("sitemaps/sitemap.example.2.xml"); | ||
|
|
||
| List<String> seeds = new ArrayList<>(); | ||
| seeds.add(sitemapUrl); | ||
| CrawlDBTestUtil.generateSeedList(fs, urlPath, seeds); | ||
|
|
||
| SitemapInjector sitemapInjector = new SitemapInjector(); | ||
| sitemapInjector.setConf(conf); | ||
| sitemapInjector.inject(crawldbPath, urlPath); | ||
|
|
||
| List<String> injected = readCrawldb(); | ||
|
|
||
| assertFalse(injected.isEmpty(), | ||
| "SitemapInjector produced an empty CrawlDb"); | ||
|
|
||
| assertThat(injected.size(), is(3)); | ||
| } | ||
|
|
||
| /** | ||
| * Resolve a fixture path either from {@code build/test/data} (where | ||
| * {@code ant test-core} copies {@code src/testresources}) or directly from | ||
| * {@code src/testresources} when running from an IDE. | ||
| */ | ||
| private String resolveFixture(String relative) { | ||
| String testData = System.getProperty("test.build.data"); | ||
| if (testData != null) { | ||
| File f = new File(testData, relative); | ||
| if (f.exists()) { | ||
| return f.toURI().toString(); | ||
| } | ||
| } | ||
| File src = new File("src/testresources", relative); | ||
| if (!src.exists()) { | ||
| src = new File("../src/testresources", relative); | ||
| } | ||
| return src.getAbsoluteFile().toURI().toString(); | ||
| } | ||
|
|
||
| private List<String> readCrawldb() throws IOException { | ||
| Path dbfile = new Path(crawldbPath, | ||
| CrawlDb.CURRENT_NAME + "/part-r-00000/data"); | ||
| Option rFile = SequenceFile.Reader.file(dbfile); | ||
| @SuppressWarnings("resource") | ||
| SequenceFile.Reader reader = new SequenceFile.Reader(conf, rFile); | ||
| List<String> read = new ArrayList<>(); | ||
| try { | ||
| while (true) { | ||
| Text key = new Text(); | ||
| CrawlDatum value = new CrawlDatum(); | ||
| if (!reader.next(key, value)) { | ||
| break; | ||
| } | ||
| read.add(key.toString()); | ||
| } | ||
| } finally { | ||
| reader.close(); | ||
| } | ||
| Collections.sort(read); | ||
| return read; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| file:src/testresources/sitemaps/sitemap.example.1.xml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?xml version="1.0" encoding="windows-1252"?> | ||
| <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xmlns:xhtml="http://www.w3.org/1999/xhtml" | ||
| xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" | ||
| xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"> | ||
| <url> | ||
| <loc>https://example.com/sitemap.html</loc> | ||
| <lastmod>2020-12-12T07:00:11.833Z</lastmod> | ||
| <changefreq>weekly</changefreq> | ||
| <priority>0.2</priority> | ||
| <xhtml:link rel="alternate" hreflang="en-tr" href="https://example.com/tr/en/sitemap.html"/> | ||
| <xhtml:link rel="alternate" hreflang="tr-tr" href="https://example.com/tr/tr/sitemap.html"/> | ||
| </url> | ||
| <url> | ||
| <loc>https://example.com/help.html</loc> | ||
| <lastmod>2017-01-27T12:54:30.733Z</lastmod> | ||
| <changefreq>monthly</changefreq> | ||
| <priority>0.2</priority> | ||
| <image:image> | ||
| <image:loc>https://example.com/logo.png</image:loc> | ||
| </image:image> | ||
| <xhtml:link rel="alternate" hreflang="x-default" href="https://example.com/help.html"/> | ||
| <xhtml:link rel="alternate" hreflang="de-en" href="https://example.com/de/en/hilfe.html"/> | ||
| <xhtml:link rel="alternate" hreflang="it-en" href="https://example.com/it/en/aiuto.html"/> | ||
| </url> | ||
| </urlset> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| file:src/testresources/sitemaps/sitemap.example.2.xml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?xml version="1.0" encoding="windows-1252"?> | ||
| <urlset xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
| <url> | ||
| <loc>https://www.example.org/example/</loc> | ||
| <xhtml:link rel="alternate" hreflang="fr-FR" href="https://www.ba.fr/example/"/> | ||
| <xhtml:link rel="alternate" hreflang="fr-BE" href="https://www.ba.be/example/"/> | ||
| </url> | ||
| <url> | ||
| <loc>https://www.example.org/example/</loc> | ||
| </url> | ||
| </urlset> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.