13
13
*/
14
14
package io .trino .plugin .httpquery ;
15
15
16
+ import io .airlift .configuration .validation .FileExists ;
16
17
import io .airlift .units .Duration ;
17
18
import org .junit .jupiter .api .Test ;
18
19
20
+ import java .io .File ;
21
+ import java .io .IOException ;
22
+ import java .nio .file .Files ;
23
+ import java .nio .file .Path ;
19
24
import java .util .List ;
20
25
import java .util .Map ;
26
+ import java .util .Set ;
27
+ import java .util .UUID ;
21
28
import java .util .concurrent .TimeUnit ;
22
29
23
30
import static io .airlift .configuration .testing .ConfigAssertions .assertFullMapping ;
24
31
import static io .airlift .configuration .testing .ConfigAssertions .assertRecordedDefaults ;
25
32
import static io .airlift .configuration .testing .ConfigAssertions .recordDefaults ;
33
+ import static io .airlift .testing .ValidationAssertions .assertFailsValidation ;
34
+ import static org .assertj .core .api .Assertions .assertThat ;
26
35
27
36
final class TestHttpEventListenerConfig
28
37
{
29
38
@ Test
30
- void testDefaults ()
39
+ void testGetHttpHeadersLoadsFromConfigFile ()
31
40
throws Exception
41
+ {
42
+ // Create a temp file with header values in properties format, including single and double quotes
43
+ Path tempFile = Files .createTempFile ("headers" , ".properties" );
44
+ String fileContent = "Authorization=Trust Me\n Cache-Control=no-cache\n Custom-Header='single-quoted'\n Another-Header=\" double-quoted\" \n " ;
45
+ Files .writeString (tempFile , fileContent );
46
+
47
+ HttpEventListenerConfig config = new HttpEventListenerConfig ();
48
+ config .setHttpHeadersConfigFile (tempFile .toFile ());
49
+
50
+ Map <String , String > headers = config .getHttpHeaders ();
51
+ assertThat (headers .size ()).isEqualTo (4 );
52
+ assertThat (headers .get ("Authorization" )).isEqualTo ("Trust Me" );
53
+ assertThat (headers .get ("Cache-Control" )).isEqualTo ("no-cache" );
54
+ assertThat (headers .get ("Custom-Header" )).isEqualTo ("'single-quoted'" );
55
+ assertThat (headers .get ("Another-Header" )).isEqualTo ("\" double-quoted\" " );
56
+ }
57
+
58
+ @ Test
59
+ void testValidateHeaderConfigRedundant ()
60
+ throws IOException
61
+ {
62
+ HttpEventListenerConfig config = new HttpEventListenerConfig ();
63
+ // Neither set: valid
64
+ assertThat (config .validateHeaderConfigRedundant ()).isTrue ();
65
+
66
+ // Only httpHeaders set: valid
67
+ config .setHttpHeaders (List .of ("Authorization: Trust Me" ));
68
+ assertThat (config .validateHeaderConfigRedundant ()).isTrue ();
69
+
70
+ // Only httpHeadersConfigFile set: valid
71
+ config = new HttpEventListenerConfig ();
72
+ config .setHttpHeadersConfigFile (Files .createTempFile (null , null ).toFile ());
73
+ assertThat (config .validateHeaderConfigRedundant ()).isTrue ();
74
+
75
+ // Both set: invalid
76
+ config .setHttpHeaders (List .of ("Authorization: Trust Me" ));
77
+ assertThat (config .validateHeaderConfigRedundant ()).isFalse ();
78
+ }
79
+
80
+ @ Test
81
+ void testDefaults ()
32
82
{
33
83
assertRecordedDefaults (recordDefaults (HttpEventListenerConfig .class )
34
84
.setHttpHeaders (List .of ())
85
+ .setHttpHeadersConfigFile (null )
35
86
.setIngestUri (null )
36
87
.setRetryCount (0 )
37
88
.setRetryDelay (Duration .succinctDuration (1 , TimeUnit .SECONDS ))
@@ -44,33 +95,77 @@ void testDefaults()
44
95
}
45
96
46
97
@ Test
47
- void testExplicitPropertyMappings ()
48
- throws Exception
98
+ void testExplicitPropertyMappingsSkippingConnectHttpHeaders ()
99
+ throws IOException
49
100
{
50
- Map <String , String > properties = Map .of (
51
- "http-event-listener.log-created" , "true" ,
52
- "http-event-listener.log-completed" , "true" ,
53
- "http-event-listener.log-split" , "true" ,
54
- "http-event-listener.connect-ingest-uri" , "http://example.com:8080/api" ,
55
- "http-event-listener.connect-http-headers" , "Authorization: Trust Me, Cache-Control: no-cache" ,
56
- "http-event-listener.connect-retry-count" , "2" ,
57
- "http-event-listener.connect-http-method" , "PUT" ,
58
- "http-event-listener.connect-retry-delay" , "101s" ,
59
- "http-event-listener.connect-backoff-base" , "1.5" ,
60
- "http-event-listener.connect-max-delay" , "10m" );
101
+ Path httpHeadersConfigFile = Files .createTempFile (null , null );
102
+
103
+ Map <String , String > properties = Map .ofEntries (
104
+ Map .entry ("http-event-listener.http-headers.config-file" , httpHeadersConfigFile .toString ()),
105
+ Map .entry ("http-event-listener.log-created" , "true" ),
106
+ Map .entry ("http-event-listener.log-completed" , "true" ),
107
+ Map .entry ("http-event-listener.log-split" , "true" ),
108
+ Map .entry ("http-event-listener.connect-ingest-uri" , "http://example.com:8080/api" ),
109
+ Map .entry ("http-event-listener.connect-retry-count" , "2" ),
110
+ Map .entry ("http-event-listener.connect-http-method" , "PUT" ),
111
+ Map .entry ("http-event-listener.connect-retry-delay" , "101s" ),
112
+ Map .entry ("http-event-listener.connect-backoff-base" , "1.5" ),
113
+ Map .entry ("http-event-listener.connect-max-delay" , "10m" ));
61
114
62
115
HttpEventListenerConfig expected = new HttpEventListenerConfig ()
116
+ .setHttpHeadersConfigFile (httpHeadersConfigFile .toFile ())
63
117
.setLogCompleted (true )
64
118
.setLogCreated (true )
65
119
.setLogSplit (true )
66
120
.setIngestUri ("http://example.com:8080/api" )
121
+ .setRetryCount (2 )
122
+ .setHttpMethod (HttpEventListenerHttpMethod .PUT )
123
+ .setRetryDelay (Duration .succinctDuration (101 , TimeUnit .SECONDS ))
124
+ .setBackoffBase (1.5 )
125
+ .setMaxDelay (Duration .succinctDuration (10 , TimeUnit .MINUTES ));
126
+
127
+ assertFullMapping (properties , expected , Set .of ("http-event-listener.connect-http-headers" ));
128
+ }
129
+
130
+ @ Test
131
+ void testExplicitPropertyMappings ()
132
+ {
133
+ Map <String , String > properties = Map .ofEntries (
134
+ Map .entry ("http-event-listener.connect-http-headers" , "Authorization: Trust Me, Cache-Control: no-cache" ),
135
+ Map .entry ("http-event-listener.log-created" , "true" ),
136
+ Map .entry ("http-event-listener.log-completed" , "true" ),
137
+ Map .entry ("http-event-listener.log-split" , "true" ),
138
+ Map .entry ("http-event-listener.connect-ingest-uri" , "http://example.com:8080/api" ),
139
+ Map .entry ("http-event-listener.connect-retry-count" , "2" ),
140
+ Map .entry ("http-event-listener.connect-http-method" , "PUT" ),
141
+ Map .entry ("http-event-listener.connect-retry-delay" , "101s" ),
142
+ Map .entry ("http-event-listener.connect-backoff-base" , "1.5" ),
143
+ Map .entry ("http-event-listener.connect-max-delay" , "10m" ));
144
+
145
+ HttpEventListenerConfig expected = new HttpEventListenerConfig ()
67
146
.setHttpHeaders (List .of ("Authorization: Trust Me" , "Cache-Control: no-cache" ))
147
+ .setLogCompleted (true )
148
+ .setLogCreated (true )
149
+ .setLogSplit (true )
150
+ .setIngestUri ("http://example.com:8080/api" )
68
151
.setRetryCount (2 )
69
152
.setHttpMethod (HttpEventListenerHttpMethod .PUT )
70
153
.setRetryDelay (Duration .succinctDuration (101 , TimeUnit .SECONDS ))
71
154
.setBackoffBase (1.5 )
72
155
.setMaxDelay (Duration .succinctDuration (10 , TimeUnit .MINUTES ));
73
156
74
- assertFullMapping (properties , expected );
157
+ assertFullMapping (properties , expected , Set .of ("http-event-listener.http-headers.config-file" ));
158
+ }
159
+
160
+ @ Test
161
+ public void testConfigFileDoesNotExist ()
162
+ {
163
+ File file = new File ("/doesNotExist-" + UUID .randomUUID ());
164
+ assertFailsValidation (
165
+ new HttpEventListenerConfig ()
166
+ .setHttpHeadersConfigFile (file ),
167
+ "httpHeadersConfigFile" ,
168
+ "file does not exist: " + file ,
169
+ FileExists .class );
75
170
}
76
171
}
0 commit comments