13
13
import org .elasticsearch .ingest .TestIngestDocument ;
14
14
import org .elasticsearch .test .ESTestCase ;
15
15
16
+ import java .util .Collections ;
16
17
import java .util .Map ;
17
18
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 ;
20
22
21
23
/**
22
24
* Test parsing of an eTLD from a FQDN. The list of eTLDs is maintained here:
23
25
* 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:
26
28
* https://publicsuffix.org/learn/
27
29
*/
28
30
public class RegisteredDomainProcessorTests extends ESTestCase {
29
- private Map <String , Object > buildEvent (String domain ) {
30
- return Map .of ("domain" , domain );
31
- }
32
31
33
32
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" );
41
40
// 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 );
45
44
testRegisteredDomainProcessor (
46
- buildEvent ( "content-autofill.googleapis.com" ) ,
45
+ "content-autofill.googleapis.com" ,
47
46
"content-autofill.googleapis.com" ,
48
47
"googleapis.com" ,
49
48
"com" ,
50
49
"content-autofill"
51
50
);
52
51
// 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
53
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" ,
62
55
"1.www.global.ssl.fastly.net" ,
63
56
"global.ssl.fastly.net" ,
64
57
"ssl.fastly.net" ,
@@ -67,76 +60,81 @@ public void testBasic() throws Exception {
67
60
}
68
61
69
62
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
-
77
63
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
+ );
90
77
}
91
78
92
79
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
+ }
103
95
}
104
96
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
+ }
113
112
}
114
113
115
114
private void testRegisteredDomainProcessor (
116
- Map < String , Object > source ,
115
+ String fqdn ,
117
116
String expectedDomain ,
118
117
String expectedRegisteredDomain ,
119
118
String expectedETLD ,
120
- String expectedSubdomain ,
121
- boolean ignoreMissing
119
+ String expectedSubdomain
122
120
) throws Exception {
123
121
String domainField = "url.domain" ;
124
122
String registeredDomainField = "url.registered_domain" ;
125
123
String topLevelDomainField = "url.top_level_domain" ;
126
124
String subdomainField = "url.subdomain" ;
127
125
128
- var processor = new RegisteredDomainProcessor (null , null , "domain" , "url" , ignoreMissing );
126
+ var processor = new RegisteredDomainProcessor (null , null , "domain" , "url" , true );
129
127
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 );
132
130
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 ));
141
139
}
142
140
}
0 commit comments