Skip to content

Commit 8e67c6f

Browse files
joegallogeorgewallace
authored andcommitted
Cleanup RegisteredDomainProcessorTests (elastic#124118)
1 parent c507cba commit 8e67c6f

File tree

1 file changed

+75
-77
lines changed

1 file changed

+75
-77
lines changed

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RegisteredDomainProcessorTests.java

Lines changed: 75 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,45 @@
1313
import org.elasticsearch.ingest.TestIngestDocument;
1414
import org.elasticsearch.test.ESTestCase;
1515

16+
import java.util.Collections;
1617
import java.util.Map;
1718

18-
import static org.hamcrest.Matchers.containsString;
19-
import static org.hamcrest.Matchers.equalTo;
19+
import static java.util.Map.entry;
20+
import static org.hamcrest.Matchers.anEmptyMap;
21+
import static org.hamcrest.Matchers.is;
2022

2123
/**
2224
* Test parsing of an eTLD from a FQDN. The list of eTLDs is maintained here:
2325
* https://github.com/publicsuffix/list/blob/master/public_suffix_list.dat
24-
*
25-
* Effective TLDs (eTLS) are not the same as DNS TLDs. Uses for eTLDs are listed here.
26+
* <p>
27+
* Effective TLDs (eTLDs) are not the same as DNS TLDs. Uses for eTLDs are listed here:
2628
* https://publicsuffix.org/learn/
2729
*/
2830
public class RegisteredDomainProcessorTests extends ESTestCase {
29-
private Map<String, Object> buildEvent(String domain) {
30-
return Map.of("domain", domain);
31-
}
3231

3332
public void testBasic() throws Exception {
34-
testRegisteredDomainProcessor(buildEvent("www.google.com"), "www.google.com", "google.com", "com", "www");
35-
testRegisteredDomainProcessor(buildEvent("google.com"), "google.com", "google.com", "com", null);
36-
testRegisteredDomainProcessor(buildEvent(""), null, null, null, null);
37-
testRegisteredDomainProcessor(buildEvent("."), null, null, null, null);
38-
testRegisteredDomainProcessor(buildEvent("$"), null, null, null, null);
39-
testRegisteredDomainProcessor(buildEvent("foo.bar.baz"), null, null, null, null);
40-
testRegisteredDomainProcessor(buildEvent("www.books.amazon.co.uk"), "www.books.amazon.co.uk", "amazon.co.uk", "co.uk", "www.books");
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");
4140
// Verify "com" is returned as the eTLD, for that FQDN or subdomain
42-
testRegisteredDomainProcessor(buildEvent("com"), "com", null, "com", null);
43-
testRegisteredDomainProcessor(buildEvent("example.com"), "example.com", "example.com", "com", null);
44-
testRegisteredDomainProcessor(buildEvent("googleapis.com"), "googleapis.com", "googleapis.com", "com", null);
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);
4544
testRegisteredDomainProcessor(
46-
buildEvent("content-autofill.googleapis.com"),
45+
"content-autofill.googleapis.com",
4746
"content-autofill.googleapis.com",
4847
"googleapis.com",
4948
"com",
5049
"content-autofill"
5150
);
5251
// 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);
5353
testRegisteredDomainProcessor(
54-
buildEvent("global.ssl.fastly.net"),
55-
"global.ssl.fastly.net",
56-
"global.ssl.fastly.net",
57-
"ssl.fastly.net",
58-
null
59-
);
60-
testRegisteredDomainProcessor(
61-
buildEvent("1.www.global.ssl.fastly.net"),
54+
"1.www.global.ssl.fastly.net",
6255
"1.www.global.ssl.fastly.net",
6356
"global.ssl.fastly.net",
6457
"ssl.fastly.net",
@@ -67,76 +60,81 @@ public void testBasic() throws Exception {
6760
}
6861

6962
public void testUseRoot() throws Exception {
70-
Map<String, Object> source = buildEvent("www.google.co.uk");
71-
72-
String domainField = "domain";
73-
String registeredDomainField = "registered_domain";
74-
String topLevelDomainField = "top_level_domain";
75-
String subdomainField = "subdomain";
76-
7763
var processor = new RegisteredDomainProcessor(null, null, "domain", "", false);
78-
79-
IngestDocument input = TestIngestDocument.withDefaultVersion(source);
80-
IngestDocument output = processor.execute(input);
81-
82-
String domain = output.getFieldValue(domainField, String.class);
83-
assertThat(domain, equalTo("www.google.co.uk"));
84-
String registeredDomain = output.getFieldValue(registeredDomainField, String.class);
85-
assertThat(registeredDomain, equalTo("google.co.uk"));
86-
String eTLD = output.getFieldValue(topLevelDomainField, String.class);
87-
assertThat(eTLD, equalTo("co.uk"));
88-
String subdomain = output.getFieldValue(subdomainField, String.class);
89-
assertThat(subdomain, equalTo("www"));
64+
IngestDocument document = TestIngestDocument.withDefaultVersion(Map.of("domain", "www.google.co.uk"));
65+
processor.execute(document);
66+
assertThat(
67+
document.getSource(),
68+
is(
69+
Map.ofEntries(
70+
entry("domain", "www.google.co.uk"),
71+
entry("registered_domain", "google.co.uk"),
72+
entry("top_level_domain", "co.uk"),
73+
entry("subdomain", "www")
74+
)
75+
)
76+
);
9077
}
9178

9279
public void testError() throws Exception {
93-
IllegalArgumentException e = expectThrows(
94-
IllegalArgumentException.class,
95-
() -> testRegisteredDomainProcessor(buildEvent("foo.bar.baz"), null, null, null, null, false)
96-
);
97-
assertThat(e.getMessage(), containsString("unable to set domain information for document"));
98-
e = expectThrows(
99-
IllegalArgumentException.class,
100-
() -> testRegisteredDomainProcessor(buildEvent("$"), null, null, null, null, false)
101-
);
102-
assertThat(e.getMessage(), containsString("unable to set domain information for document"));
80+
var processor = new RegisteredDomainProcessor(null, null, "domain", "", false);
81+
82+
{
83+
IngestDocument document = TestIngestDocument.withDefaultVersion(Map.of("domain", "foo.bar.baz"));
84+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> processor.execute(document));
85+
assertThat(e.getMessage(), is("unable to set domain information for document"));
86+
assertThat(document.getSource(), is(Map.of("domain", "foo.bar.baz")));
87+
}
88+
89+
{
90+
IngestDocument document = TestIngestDocument.withDefaultVersion(Map.of("domain", "$"));
91+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> processor.execute(document));
92+
assertThat(e.getMessage(), is("unable to set domain information for document"));
93+
assertThat(document.getSource(), is(Map.of("domain", "$")));
94+
}
10395
}
10496

105-
private void testRegisteredDomainProcessor(
106-
Map<String, Object> source,
107-
String expectedDomain,
108-
String expectedRegisteredDomain,
109-
String expectedETLD,
110-
String expectedSubdomain
111-
) throws Exception {
112-
testRegisteredDomainProcessor(source, expectedDomain, expectedRegisteredDomain, expectedETLD, expectedSubdomain, true);
97+
public void testIgnoreMissing() throws Exception {
98+
{
99+
var processor = new RegisteredDomainProcessor(null, null, "domain", "", false);
100+
IngestDocument document = TestIngestDocument.withDefaultVersion(Map.of());
101+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> processor.execute(document));
102+
assertThat(e.getMessage(), is("field [domain] not present as part of path [domain]"));
103+
assertThat(document.getSource(), is(anEmptyMap()));
104+
}
105+
106+
{
107+
var processor = new RegisteredDomainProcessor(null, null, "domain", "", true);
108+
IngestDocument document = TestIngestDocument.withDefaultVersion(Collections.singletonMap("domain", null));
109+
processor.execute(document);
110+
assertThat(document.getSource(), is(Collections.singletonMap("domain", null)));
111+
}
113112
}
114113

115114
private void testRegisteredDomainProcessor(
116-
Map<String, Object> source,
115+
String fqdn,
117116
String expectedDomain,
118117
String expectedRegisteredDomain,
119118
String expectedETLD,
120-
String expectedSubdomain,
121-
boolean ignoreMissing
119+
String expectedSubdomain
122120
) throws Exception {
123121
String domainField = "url.domain";
124122
String registeredDomainField = "url.registered_domain";
125123
String topLevelDomainField = "url.top_level_domain";
126124
String subdomainField = "url.subdomain";
127125

128-
var processor = new RegisteredDomainProcessor(null, null, "domain", "url", ignoreMissing);
126+
var processor = new RegisteredDomainProcessor(null, null, "domain", "url", true);
129127

130-
IngestDocument input = TestIngestDocument.withDefaultVersion(source);
131-
IngestDocument output = processor.execute(input);
128+
IngestDocument document = TestIngestDocument.withDefaultVersion(Map.of("domain", fqdn));
129+
processor.execute(document);
132130

133-
String domain = output.getFieldValue(domainField, String.class, expectedDomain == null);
134-
assertThat(domain, equalTo(expectedDomain));
135-
String registeredDomain = output.getFieldValue(registeredDomainField, String.class, expectedRegisteredDomain == null);
136-
assertThat(registeredDomain, equalTo(expectedRegisteredDomain));
137-
String eTLD = output.getFieldValue(topLevelDomainField, String.class, expectedETLD == null);
138-
assertThat(eTLD, equalTo(expectedETLD));
139-
String subdomain = output.getFieldValue(subdomainField, String.class, expectedSubdomain == null);
140-
assertThat(subdomain, equalTo(expectedSubdomain));
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));
141139
}
142140
}

0 commit comments

Comments
 (0)