77
88package org .elasticsearch .xpack .patternedtext ;
99
10- import org .elasticsearch .index .mapper .DateFieldMapper ;
1110import org .elasticsearch .test .ESTestCase ;
1211import org .hamcrest .Matchers ;
1312
@@ -17,7 +16,6 @@ public void testEmpty() {
1716 String text = "" ;
1817 PatternedTextValueProcessor .Parts parts = PatternedTextValueProcessor .split (text );
1918 assertEquals (text , parts .template ());
20- assertNull (parts .timestamp ());
2119 assertTrue (parts .args ().isEmpty ());
2220 assertEquals (text , PatternedTextValueProcessor .merge (parts ));
2321 }
@@ -26,7 +24,6 @@ public void testWhitespace() {
2624 String text = " " ;
2725 PatternedTextValueProcessor .Parts parts = PatternedTextValueProcessor .split (text );
2826 assertEquals (text , parts .template ());
29- assertNull (parts .timestamp ());
3027 assertTrue (parts .args ().isEmpty ());
3128 assertEquals (text , PatternedTextValueProcessor .merge (parts ));
3229 }
@@ -35,107 +32,56 @@ public void testWithoutTimestamp() {
3532 String text = " some text with arg1 and 2arg2 and 333 " ;
3633 PatternedTextValueProcessor .Parts parts = PatternedTextValueProcessor .split (text );
3734 assertEquals (" some text with %W and %W and %W " , parts .template ());
38- assertNull (parts .timestamp ());
3935 assertThat (parts .args (), Matchers .contains ("arg1" , "2arg2" , "333" ));
4036 assertEquals (text , PatternedTextValueProcessor .merge (parts ));
4137 }
4238
4339 public void testWithTimestamp () {
4440 String text = " 2021-04-13T13:51:38.000Z some text with arg1 and arg2 and arg3" ;
4541 PatternedTextValueProcessor .Parts parts = PatternedTextValueProcessor .split (text );
46- assertEquals (" %T some text with %W and %W and %W" , parts .template ());
47- assertEquals (DateFieldMapper .DEFAULT_DATE_TIME_FORMATTER .parseMillis ("2021-04-13T13:51:38.000Z" ), (long ) parts .timestamp ());
48- assertThat (parts .args (), Matchers .contains ("arg1" , "arg2" , "arg3" ));
42+ assertEquals (" %W some text with %W and %W and %W" , parts .template ());
43+ assertThat (parts .args (), Matchers .contains ("2021-04-13T13:51:38.000Z" , "arg1" , "arg2" , "arg3" ));
4944 assertEquals (text , PatternedTextValueProcessor .merge (parts ));
5045 }
5146
5247 public void testWithDateSpaceTime () {
5348 String text = " 2021-04-13 13:51:38 some text with arg1 and arg2 and arg3" ;
5449 PatternedTextValueProcessor .Parts parts = PatternedTextValueProcessor .split (text );
55- assertEquals (" %T some text with %W and %W and %W" , parts .template ());
56- assertEquals (DateFieldMapper .DEFAULT_DATE_TIME_FORMATTER .parseMillis ("2021-04-13T13:51:38.000Z" ), (long ) parts .timestamp ());
57- assertThat (parts .args (), Matchers .contains ("arg1" , "arg2" , "arg3" ));
58- assertEquals (text .replace ("2021-04-13 13:51:38" , "2021-04-13T13:51:38.000Z" ), PatternedTextValueProcessor .merge (parts ));
50+ assertEquals (" %W %W some text with %W and %W and %W" , parts .template ());
51+ assertThat (parts .args (), Matchers .contains ("2021-04-13" , "13:51:38" , "arg1" , "arg2" , "arg3" ));
52+ assertEquals (text , PatternedTextValueProcessor .merge (parts ));
5953 }
6054
6155 public void testMalformedDate () {
6256 String text = "2020/09/06 10:11:38 Using namespace: kubernetes-dashboard' | HTTP status: 400, message: [1:395]" ;
6357 PatternedTextValueProcessor .Parts parts = PatternedTextValueProcessor .split (text );
64- assertEquals ("%T Using namespace: kubernetes-dashboard' | HTTP status: %W message: [%W]" , parts .template ());
65- assertEquals (DateFieldMapper .DEFAULT_DATE_TIME_FORMATTER .parseMillis ("2020-09-06T10:11:38" ), (long ) parts .timestamp ());
66- assertThat (parts .args (), Matchers .contains ("400," , "1:395" ));
67- assertEquals (text .replace ("2020/09/06 10:11:38" , "2020-09-06T10:11:38.000Z" ), PatternedTextValueProcessor .merge (parts ));
58+ assertEquals ("%W %W Using namespace: kubernetes-dashboard' | HTTP status: %W message: [%W]" , parts .template ());
59+ assertThat (parts .args (), Matchers .contains ("2020/09/06" , "10:11:38" , "400," , "1:395" ));
60+ assertEquals (text , PatternedTextValueProcessor .merge (parts ));
6861 }
6962
7063 public void testUUID () {
7164 String text = "[2020-08-18T00:58:56.751+00:00][15][2354][action_controller][INFO]: [18be2355-6306-4a00-9db9-f0696aa1a225] "
7265 + "some text with arg1 and arg2" ;
7366 PatternedTextValueProcessor .Parts parts = PatternedTextValueProcessor .split (text );
74- assertEquals ("[%T][%W][%W][action_controller][INFO]: [%U] some text with %W and %W" , parts .template ());
75- assertEquals (DateFieldMapper .DEFAULT_DATE_TIME_FORMATTER .parseMillis ("2020-08-18T00:58:56.751+00:00" ), (long ) parts .timestamp ());
76- assertThat (parts .args (), Matchers .contains ("15" , "2354" , "AEoGY1UjvhgloqFqafC5nQ" , "arg1" , "arg2" ));
77- assertEquals (text .replace ("+00:00" , "Z" ), PatternedTextValueProcessor .merge (parts ));
67+ assertEquals ("[%W][%W][%W][action_controller][INFO]: [%W] some text with %W and %W" , parts .template ());
68+ assertThat (parts .args (), Matchers .contains ("2020-08-18T00:58:56.751+00:00" , "15" , "2354" , "18be2355-6306-4a00-9db9-f0696aa1a225" , "arg1" , "arg2" ));
69+ assertEquals (text , PatternedTextValueProcessor .merge (parts ));
7870 }
7971
8072 public void testIP () {
8173 String text = "[2020-08-18T00:58:56.751+00:00][15][2354][action_controller][INFO]: from 94.168.152.150 and arg1" ;
8274 PatternedTextValueProcessor .Parts parts = PatternedTextValueProcessor .split (text );
83- assertEquals ("[%T][%W][%W][action_controller][INFO]: from %I and %W" , parts .template ());
84- assertEquals (DateFieldMapper .DEFAULT_DATE_TIME_FORMATTER .parseMillis ("2020-08-18T00:58:56.751+00:00" ), (long ) parts .timestamp ());
85- assertThat (parts .args (), Matchers .contains ("15" , "2354" , "XqiYlg" , "arg1" ));
86- assertEquals (text .replace ("+00:00" , "Z" ), PatternedTextValueProcessor .merge (parts ));
75+ assertEquals ("[%W][%W][%W][action_controller][INFO]: from %W and %W" , parts .template ());
76+ assertThat (parts .args (), Matchers .contains ("2020-08-18T00:58:56.751+00:00" , "15" , "2354" , "94.168.152.150" , "arg1" ));
77+ assertEquals (text , PatternedTextValueProcessor .merge (parts ));
8778 }
8879
8980 public void testSecondDate () {
9081 String text = "[2020-08-18T00:58:56.751+00:00][15][2354][action_controller][INFO]: at 2020-08-18 00:58:56 +0000 and arg1" ;
9182 PatternedTextValueProcessor .Parts parts = PatternedTextValueProcessor .split (text );
92- assertEquals ("[%T][%W][%W][action_controller][INFO]: at %D and %W" , parts .template ());
93- assertEquals (DateFieldMapper .DEFAULT_DATE_TIME_FORMATTER .parseMillis ("2020-08-18T00:58:56.751+00:00" ), (long ) parts .timestamp ());
94- assertThat (parts .args (), Matchers .contains ("15" , "2354" , "gIQT/3MBAAA" , "arg1" ));
95- assertEquals (
96- text .replace ("2020-08-18 00:58:56 +0000" , "2020-08-18T00:58:56.000Z" ).replace ("+00:00" , "Z" ),
97- PatternedTextValueProcessor .merge (parts )
98- );
99- }
100-
101- public void testIsUUID () {
102- String [] validUUIDs = { "123e4567-e89b-12d3-a456-426614174000" , "550e8400-e29b-41d4-a716-446655440000" };
103-
104- String [] invalidUUIDs = {
105- "not-a-uuid" , // very invalid
106- "550e8400-e29b-41d4-a716-4466554400000" , // Invalid last extra character
107- "550e8400-e29b-41d4-a716-44665544000g" // Invalid character
108- };
109-
110- byte [] bytes = new byte [16 ];
111- for (String uuid : validUUIDs ) {
112- assertTrue ("Expected valid UUID: " + uuid , PatternedTextValueProcessor .isUUID (uuid , bytes ));
113- }
114- for (String uuid : invalidUUIDs ) {
115- assertFalse ("Expected invalid UUID: " + uuid , PatternedTextValueProcessor .isUUID (uuid , bytes ));
116- }
117- }
118-
119- public void testIsIPv4 () {
120- String [] validIPv4s = { "192.168.1.1" , "10.0.0.1" , "172.16.0.1" , "255.255.255.255" , "0.0.0.0" };
121-
122- String [] invalidIPv4s = {
123- "256.256.256.256" , // Out of range
124- "192.168.1" , // Missing one octet
125- "192.168.1.1.1" , // Extra octet
126- "192.168.1.a" // Invalid character
127- };
128-
129- byte [] bytes = new byte [4 ];
130- for (String ip : validIPv4s ) {
131- assertTrue ("Expected valid IPv4: " + ip , PatternedTextValueProcessor .isIpv4 (ip , bytes ));
132- String [] octets = ip .split ("\\ ." );
133- for (int i = 0 ; i < 4 ; i ++) {
134- assertEquals ("Expected valid IPv4 octet: " + octets [i ], Integer .parseInt (octets [i ]), bytes [i ] & 0xFF );
135- }
136- }
137- for (String ip : invalidIPv4s ) {
138- assertFalse ("Expected invalid IPv4: " + ip , PatternedTextValueProcessor .isIpv4 (ip , bytes ));
139- }
83+ assertEquals ("[%W][%W][%W][action_controller][INFO]: at %W %W %W and %W" , parts .template ());
84+ assertThat (parts .args (), Matchers .contains ("2020-08-18T00:58:56.751+00:00" , "15" , "2354" , "2020-08-18" , "00:58:56" , "+0000" , "arg1" ));
85+ assertEquals (text , PatternedTextValueProcessor .merge (parts ));
14086 }
14187}
0 commit comments