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