|
11 | 11 |
|
12 | 12 | import org.elasticsearch.ingest.IngestDocument; |
13 | 13 | import org.elasticsearch.ingest.TestIngestDocument; |
| 14 | +import org.elasticsearch.ingest.common.RegisteredDomainProcessor.DomainInfo; |
14 | 15 | import org.elasticsearch.test.ESTestCase; |
15 | 16 |
|
16 | 17 | import java.util.Collections; |
17 | 18 | import java.util.Map; |
18 | 19 |
|
19 | 20 | import static java.util.Map.entry; |
| 21 | +import static org.elasticsearch.ingest.common.RegisteredDomainProcessor.getRegisteredDomain; |
20 | 22 | import static org.hamcrest.Matchers.anEmptyMap; |
21 | 23 | import static org.hamcrest.Matchers.is; |
| 24 | +import static org.hamcrest.Matchers.nullValue; |
22 | 25 |
|
23 | 26 | /** |
24 | 27 | * Test parsing of an eTLD from a FQDN. The list of eTLDs is maintained here: |
|
29 | 32 | */ |
30 | 33 | public class RegisteredDomainProcessorTests extends ESTestCase { |
31 | 34 |
|
32 | | - public void testBasic() throws Exception { |
33 | | - testRegisteredDomainProcessor("www.google.com", "www.google.com", "google.com", "com", "www"); |
34 | | - testRegisteredDomainProcessor("google.com", "google.com", "google.com", "com", null); |
35 | | - testRegisteredDomainProcessor("", null, null, null, null); |
36 | | - testRegisteredDomainProcessor(".", null, null, null, null); |
37 | | - testRegisteredDomainProcessor("$", null, null, null, null); |
38 | | - testRegisteredDomainProcessor("foo.bar.baz", null, null, null, null); |
39 | | - testRegisteredDomainProcessor("www.books.amazon.co.uk", "www.books.amazon.co.uk", "amazon.co.uk", "co.uk", "www.books"); |
| 35 | + public void testGetRegisteredDomain() { |
| 36 | + assertThat(getRegisteredDomain("www.google.com"), is(new DomainInfo("www.google.com", "google.com", "com", "www"))); |
| 37 | + assertThat(getRegisteredDomain("google.com"), is(new DomainInfo("google.com", "google.com", "com", null))); |
| 38 | + assertThat(getRegisteredDomain(null), nullValue()); |
| 39 | + assertThat(getRegisteredDomain(""), nullValue()); |
| 40 | + assertThat(getRegisteredDomain(" "), nullValue()); |
| 41 | + assertThat(getRegisteredDomain("."), nullValue()); |
| 42 | + assertThat(getRegisteredDomain("$"), nullValue()); |
| 43 | + assertThat(getRegisteredDomain("foo.bar.baz"), nullValue()); |
| 44 | + assertThat( |
| 45 | + getRegisteredDomain("www.books.amazon.co.uk"), |
| 46 | + is(new DomainInfo("www.books.amazon.co.uk", "amazon.co.uk", "co.uk", "www.books")) |
| 47 | + ); |
40 | 48 | // Verify "com" is returned as the eTLD, for that FQDN or subdomain |
41 | | - testRegisteredDomainProcessor("com", "com", null, "com", null); |
42 | | - testRegisteredDomainProcessor("example.com", "example.com", "example.com", "com", null); |
43 | | - testRegisteredDomainProcessor("googleapis.com", "googleapis.com", "googleapis.com", "com", null); |
44 | | - testRegisteredDomainProcessor( |
45 | | - "content-autofill.googleapis.com", |
46 | | - "content-autofill.googleapis.com", |
47 | | - "googleapis.com", |
48 | | - "com", |
49 | | - "content-autofill" |
| 49 | + assertThat(getRegisteredDomain("com"), is(new DomainInfo("com", null, "com", null))); |
| 50 | + assertThat(getRegisteredDomain("example.com"), is(new DomainInfo("example.com", "example.com", "com", null))); |
| 51 | + assertThat(getRegisteredDomain("googleapis.com"), is(new DomainInfo("googleapis.com", "googleapis.com", "com", null))); |
| 52 | + assertThat( |
| 53 | + getRegisteredDomain("content-autofill.googleapis.com"), |
| 54 | + is(new DomainInfo("content-autofill.googleapis.com", "googleapis.com", "com", "content-autofill")) |
50 | 55 | ); |
51 | 56 | // Verify "ssl.fastly.net" is returned as the eTLD, for that FQDN or subdomain |
52 | | - testRegisteredDomainProcessor("global.ssl.fastly.net", "global.ssl.fastly.net", "global.ssl.fastly.net", "ssl.fastly.net", null); |
53 | | - testRegisteredDomainProcessor( |
54 | | - "1.www.global.ssl.fastly.net", |
55 | | - "1.www.global.ssl.fastly.net", |
56 | | - "global.ssl.fastly.net", |
57 | | - "ssl.fastly.net", |
58 | | - "1.www" |
| 57 | + assertThat( |
| 58 | + getRegisteredDomain("global.ssl.fastly.net"), |
| 59 | + is(new DomainInfo("global.ssl.fastly.net", "global.ssl.fastly.net", "ssl.fastly.net", null)) |
| 60 | + ); |
| 61 | + assertThat( |
| 62 | + getRegisteredDomain("1.www.global.ssl.fastly.net"), |
| 63 | + is(new DomainInfo("1.www.global.ssl.fastly.net", "global.ssl.fastly.net", "ssl.fastly.net", "1.www")) |
59 | 64 | ); |
60 | 65 | } |
61 | 66 |
|
| 67 | + public void testBasic() throws Exception { |
| 68 | + var processor = new RegisteredDomainProcessor(null, null, "input", "output", false); |
| 69 | + { |
| 70 | + IngestDocument document = TestIngestDocument.withDefaultVersion(Map.of("input", "www.google.co.uk")); |
| 71 | + processor.execute(document); |
| 72 | + assertThat( |
| 73 | + document.getSource(), |
| 74 | + is( |
| 75 | + Map.ofEntries( |
| 76 | + entry("input", "www.google.co.uk"), |
| 77 | + entry( |
| 78 | + "output", |
| 79 | + Map.ofEntries( |
| 80 | + entry("domain", "www.google.co.uk"), |
| 81 | + entry("registered_domain", "google.co.uk"), |
| 82 | + entry("top_level_domain", "co.uk"), |
| 83 | + entry("subdomain", "www") |
| 84 | + ) |
| 85 | + ) |
| 86 | + ) |
| 87 | + ) |
| 88 | + ); |
| 89 | + } |
| 90 | + { |
| 91 | + IngestDocument document = TestIngestDocument.withDefaultVersion(Map.of("input", "example.com")); |
| 92 | + processor.execute(document); |
| 93 | + assertThat( |
| 94 | + document.getSource(), |
| 95 | + is( |
| 96 | + Map.ofEntries( |
| 97 | + entry("input", "example.com"), |
| 98 | + entry( |
| 99 | + "output", |
| 100 | + Map.ofEntries( |
| 101 | + entry("domain", "example.com"), |
| 102 | + entry("registered_domain", "example.com"), |
| 103 | + entry("top_level_domain", "com") |
| 104 | + ) |
| 105 | + ) |
| 106 | + ) |
| 107 | + ) |
| 108 | + ); |
| 109 | + } |
| 110 | + { |
| 111 | + IngestDocument document = TestIngestDocument.withDefaultVersion(Map.of("input", "com")); |
| 112 | + processor.execute(document); |
| 113 | + assertThat( |
| 114 | + document.getSource(), |
| 115 | + is( |
| 116 | + Map.ofEntries( |
| 117 | + entry("input", "com"), |
| 118 | + entry( |
| 119 | + "output", |
| 120 | + Map.ofEntries( |
| 121 | + entry("domain", "com"), // |
| 122 | + entry("top_level_domain", "com") |
| 123 | + ) |
| 124 | + ) |
| 125 | + ) |
| 126 | + ) |
| 127 | + ); |
| 128 | + } |
| 129 | + } |
| 130 | + |
62 | 131 | public void testUseRoot() throws Exception { |
63 | 132 | var processor = new RegisteredDomainProcessor(null, null, "domain", "", false); |
64 | 133 | IngestDocument document = TestIngestDocument.withDefaultVersion(Map.of("domain", "www.google.co.uk")); |
@@ -110,31 +179,4 @@ public void testIgnoreMissing() throws Exception { |
110 | 179 | assertThat(document.getSource(), is(Collections.singletonMap("domain", null))); |
111 | 180 | } |
112 | 181 | } |
113 | | - |
114 | | - private void testRegisteredDomainProcessor( |
115 | | - String fqdn, |
116 | | - String expectedDomain, |
117 | | - String expectedRegisteredDomain, |
118 | | - String expectedETLD, |
119 | | - String expectedSubdomain |
120 | | - ) throws Exception { |
121 | | - String domainField = "url.domain"; |
122 | | - String registeredDomainField = "url.registered_domain"; |
123 | | - String topLevelDomainField = "url.top_level_domain"; |
124 | | - String subdomainField = "url.subdomain"; |
125 | | - |
126 | | - var processor = new RegisteredDomainProcessor(null, null, "domain", "url", true); |
127 | | - |
128 | | - IngestDocument document = TestIngestDocument.withDefaultVersion(Map.of("domain", fqdn)); |
129 | | - processor.execute(document); |
130 | | - |
131 | | - String domain = document.getFieldValue(domainField, String.class, expectedDomain == null); |
132 | | - assertThat(domain, is(expectedDomain)); |
133 | | - String registeredDomain = document.getFieldValue(registeredDomainField, String.class, expectedRegisteredDomain == null); |
134 | | - assertThat(registeredDomain, is(expectedRegisteredDomain)); |
135 | | - String eTLD = document.getFieldValue(topLevelDomainField, String.class, expectedETLD == null); |
136 | | - assertThat(eTLD, is(expectedETLD)); |
137 | | - String subdomain = document.getFieldValue(subdomainField, String.class, expectedSubdomain == null); |
138 | | - assertThat(subdomain, is(expectedSubdomain)); |
139 | | - } |
140 | 182 | } |
0 commit comments