2727import io .trino .gateway .ha .router .schema .ExternalRouterResponse ;
2828import jakarta .servlet .http .HttpServletRequest ;
2929import jakarta .ws .rs .HttpMethod ;
30+ import jakarta .ws .rs .WebApplicationException ;
3031import org .junit .jupiter .api .BeforeAll ;
3132import org .junit .jupiter .api .Test ;
3233import org .junit .jupiter .api .TestInstance ;
3334import org .junit .jupiter .api .extension .ExtendWith ;
3435import org .mockito .Mockito ;
3536import org .mockito .junit .jupiter .MockitoExtension ;
3637
37- import java .util .Arrays ;
3838import java .util .Collections ;
3939import java .util .List ;
4040import java .util .Map ;
4141
4242import static io .trino .gateway .ha .handler .HttpUtils .USER_HEADER ;
4343import static org .assertj .core .api .Assertions .assertThat ;
44+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
4445import static org .mockito .ArgumentMatchers .any ;
4546import static org .mockito .Mockito .when ;
4647
@@ -60,6 +61,8 @@ static HaGatewayConfiguration provideGatewayConfiguration()
6061 HaGatewayConfiguration config = new HaGatewayConfiguration ();
6162 config .setRequestAnalyzerConfig (new RequestAnalyzerConfig ());
6263
64+ config .getRouting ().setDefaultRoutingGroup ("default-group" );
65+
6366 // Configure excluded headers
6467 RulesExternalConfiguration rulesExternalConfig = new RulesExternalConfiguration ();
6568 rulesExternalConfig .setExcludeHeaders (List .of ("Authorization" , "Cookie" ));
@@ -109,7 +112,9 @@ void setUp()
109112 request = prepareMockRequest ();
110113
111114 // Initialize the handler with the configuration
112- handler = new RoutingTargetHandler (routingManager , RoutingGroupSelector .byRoutingExternal (httpClient , config .getRoutingRules ().getRulesExternalConfiguration (), config .getRequestAnalyzerConfig ()), config );
115+ handler = new RoutingTargetHandler (
116+ routingManager ,
117+ RoutingGroupSelector .byRoutingExternal (httpClient , config .getRoutingRules ().getRulesExternalConfiguration (), config .getRequestAnalyzerConfig ()), config );
113118 }
114119
115120 @ Test
@@ -220,29 +225,86 @@ void testEmptyRoutingGroup()
220225 RoutingTargetResponse response = handler .resolveRouting (request );
221226
222227 // Verify that when no routing group header is set, we default to "adhoc"
223- assertThat (response .routingDestination ().routingGroup ()).isEqualTo ("adhoc " );
228+ assertThat (response .routingDestination ().routingGroup ()).isEqualTo ("default-group " );
224229 assertThat (response .modifiedRequest ().getHeader ("X-Empty-Group-Header" ))
225230 .isEqualTo ("should-be-set" );
226231 }
227232
228233 @ Test
229- void testErrorsGiven ()
230- throws Exception
234+ void testResponsePropertiesNull ()
235+ {
236+ ExternalRouterResponse mockResponse = new ExternalRouterResponse (null , null , ImmutableMap .of ());
237+ when (httpClient .execute (any (), any ())).thenReturn (mockResponse );
238+
239+ RoutingTargetResponse result = handler .resolveRouting (request );
240+
241+ assertThat (result .routingDestination ().routingGroup ()).isEqualTo ("default-group" );
242+ }
243+
244+ @ Test
245+ void testResponseGroupSetResponseErrorsNull ()
231246 {
232- // Setup routing group selector response with errors
233- Map <String , String > modifiedHeaders = ImmutableMap .of (
234- "X-New-Header" , "new-value" );
235- List <String > someErrors = Arrays .asList ("ErrorA" , "ErrorB" );
236247 ExternalRouterResponse mockResponse = new ExternalRouterResponse (
237- "test-group" , // This value should be ignored due to errors
238- someErrors ,
239- modifiedHeaders );
248+ "test-group" , null , ImmutableMap .of ());
240249 when (httpClient .execute (any (), any ())).thenReturn (mockResponse );
241250
242- // Execute
243- RoutingTargetResponse response = handler .resolveRouting (request );
251+ RoutingTargetResponse result = handler .resolveRouting (request );
252+
253+ assertThat (result .routingDestination ().routingGroup ()).isEqualTo ("test-group" );
254+ }
255+
256+ @ Test
257+ void testPropagateErrorsFalseResponseGroupNullResponseErrorsSet ()
258+ {
259+ ExternalRouterResponse mockResponse = new ExternalRouterResponse (null , List .of ("some-error" ), ImmutableMap .of ());
260+ when (httpClient .execute (any (), any ())).thenReturn (mockResponse );
261+
262+ RoutingTargetResponse result = handler .resolveRouting (request );
263+
264+ assertThat (result .routingDestination ().routingGroup ()).isEqualTo ("default-group" );
265+ }
266+
267+ @ Test
268+ void testPropagateErrorsFalseResponseGroupAndErrorsSet ()
269+ {
270+ ExternalRouterResponse mockResponse = new ExternalRouterResponse ("test-group" , List .of ("some-error" ), ImmutableMap .of ());
271+ when (httpClient .execute (any (), any ())).thenReturn (mockResponse );
272+
273+ RoutingTargetResponse result = handler .resolveRouting (request );
274+
275+ assertThat (result .routingDestination ().routingGroup ()).isEqualTo ("test-group" );
276+ }
277+
278+ @ Test
279+ void testPropagateErrorsTrueResponseGroupNullResponseErrorsSet ()
280+ {
281+ RoutingTargetHandler handler = createHandlerWithPropagateErrorsTrue ();
244282
245- // Verify that when errors are present, we default to "adhoc" group
246- assertThat (response .routingDestination ().routingGroup ()).isEqualTo ("adhoc" );
283+ config .getRoutingRules ().getRulesExternalConfiguration ().setPropagateErrors (true );
284+ ExternalRouterResponse mockResponse = new ExternalRouterResponse (null , List .of ("some-error" ), ImmutableMap .of ());
285+ when (httpClient .execute (any (), any ())).thenReturn (mockResponse );
286+
287+ assertThatThrownBy (() -> handler .resolveRouting (request ))
288+ .isInstanceOf (WebApplicationException .class );
289+ }
290+
291+ @ Test
292+ void testPropagateErrorsTrueResponseGroupAndErrorsSet ()
293+ {
294+ RoutingTargetHandler handler = createHandlerWithPropagateErrorsTrue ();
295+
296+ ExternalRouterResponse response = new ExternalRouterResponse ("test-group" , List .of ("some-error" ), ImmutableMap .of ());
297+ when (httpClient .execute (any (), any ())).thenReturn (response );
298+
299+ assertThatThrownBy (() -> handler .resolveRouting (request ))
300+ .isInstanceOf (WebApplicationException .class );
301+ }
302+
303+ private RoutingTargetHandler createHandlerWithPropagateErrorsTrue ()
304+ {
305+ config .getRoutingRules ().getRulesExternalConfiguration ().setPropagateErrors (true );
306+ return new RoutingTargetHandler (
307+ routingManager ,
308+ RoutingGroupSelector .byRoutingExternal (httpClient , config .getRoutingRules ().getRulesExternalConfiguration (), config .getRequestAnalyzerConfig ()), config );
247309 }
248310}
0 commit comments