Skip to content

Commit 9de1e3d

Browse files
authored
Refactor RegisteredDomainProcessorTests (#124175) (#124247)
1 parent 8b1c98d commit 9de1e3d

File tree

2 files changed

+95
-52
lines changed

2 files changed

+95
-52
lines changed

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/RegisteredDomainProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.apache.http.conn.util.PublicSuffixMatcher;
1313
import org.apache.http.conn.util.PublicSuffixMatcherLoader;
14+
import org.elasticsearch.common.Strings;
1415
import org.elasticsearch.core.Nullable;
1516
import org.elasticsearch.ingest.AbstractProcessor;
1617
import org.elasticsearch.ingest.ConfigurationUtils;
@@ -85,7 +86,7 @@ public IngestDocument execute(IngestDocument document) throws Exception {
8586
@Nullable
8687
// visible for testing
8788
static DomainInfo getRegisteredDomain(@Nullable String fqdn) {
88-
if (fqdn == null) {
89+
if (Strings.hasText(fqdn) == false) {
8990
return null;
9091
}
9192
String registeredDomain = SUFFIX_MATCHER.getDomainRoot(fqdn);

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

Lines changed: 93 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@
1111

1212
import org.elasticsearch.ingest.IngestDocument;
1313
import org.elasticsearch.ingest.TestIngestDocument;
14+
import org.elasticsearch.ingest.common.RegisteredDomainProcessor.DomainInfo;
1415
import org.elasticsearch.test.ESTestCase;
1516

1617
import java.util.Collections;
1718
import java.util.Map;
1819

1920
import static java.util.Map.entry;
21+
import static org.elasticsearch.ingest.common.RegisteredDomainProcessor.getRegisteredDomain;
2022
import static org.hamcrest.Matchers.anEmptyMap;
2123
import static org.hamcrest.Matchers.is;
24+
import static org.hamcrest.Matchers.nullValue;
2225

2326
/**
2427
* Test parsing of an eTLD from a FQDN. The list of eTLDs is maintained here:
@@ -29,36 +32,102 @@
2932
*/
3033
public class RegisteredDomainProcessorTests extends ESTestCase {
3134

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+
);
4048
// 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"))
5055
);
5156
// 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"))
5964
);
6065
}
6166

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+
62131
public void testUseRoot() throws Exception {
63132
var processor = new RegisteredDomainProcessor(null, null, "domain", "", false);
64133
IngestDocument document = TestIngestDocument.withDefaultVersion(Map.of("domain", "www.google.co.uk"));
@@ -110,31 +179,4 @@ public void testIgnoreMissing() throws Exception {
110179
assertThat(document.getSource(), is(Collections.singletonMap("domain", null)));
111180
}
112181
}
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-
}
140182
}

0 commit comments

Comments
 (0)