4242import org .junit .BeforeClass ;
4343import org .junit .Test ;
4444
45- import java .io .IOException ;
4645import java .nio .charset .StandardCharsets ;
4746import java .util .HashMap ;
4847import java .util .Map ;
@@ -62,16 +61,21 @@ public class TestHttpUDFWithAliases extends ClusterTest {
6261
6362 private static AliasRegistry storageAliasesRegistry ;
6463 private static AliasRegistry tableAliasesRegistry ;
65- private static final int MOCK_SERVER_PORT = 47778 ;
6664 private static String TEST_JSON_PAGE1 ;
67- private static final String DUMMY_URL = "http://localhost:" + MOCK_SERVER_PORT ;
65+ private static MockWebServer server ;
66+ private static String mockServerUrl ;
6867
6968 @ BeforeClass
7069 public static void setUpBeforeClass () throws Exception {
7170
7271 TEST_JSON_PAGE1 = Files .asCharSource (DrillFileUtils .getResourceAsFile ("/data/p1.json" ),
7372 StandardCharsets .UTF_8 ).read ();
7473
74+ // Start MockWebServer with dynamic port allocation
75+ server = new MockWebServer ();
76+ server .start (0 ); // Use port 0 for dynamic allocation
77+ mockServerUrl = server .url ("/" ).toString ().replaceAll ("/$" , "" );
78+
7579 cluster = ClusterFixture .bareBuilder (dirTestWatcher )
7680 .configProperty (ExecConstants .USER_AUTHENTICATION_ENABLED , true )
7781 .configProperty (ExecConstants .IMPERSONATION_ENABLED , true )
@@ -106,7 +110,7 @@ public static void setUpBeforeClass() throws Exception {
106110 .build ();
107111
108112 HttpApiConfig basicJson = HttpApiConfig .builder ()
109- .url (String .format ("%s/json" , DUMMY_URL ))
113+ .url (String .format ("%s/json" , mockServerUrl ))
110114 .method ("get" )
111115 .jsonOptions (jsonOptions )
112116 .requireTail (false )
@@ -131,7 +135,7 @@ public void testSeveralRowsAndRequestsAndPublicStorageAlias() throws Exception {
131135 storageAliasesRegistry .getPublicAliases ().put ("`foobar`" , "`local`" , false );
132136
133137 String sql = "SELECT http_request('foobar.basicJson', `col1`) as data FROM cp.`/data/p4.json`" ;
134- try ( MockWebServer server = startServer ()) {
138+ try {
135139 server .enqueue (new MockResponse ().setResponseCode (200 ).setBody (TEST_JSON_PAGE1 ));
136140 server .enqueue (new MockResponse ().setResponseCode (200 ).setBody (TEST_JSON_PAGE1 ));
137141
@@ -161,8 +165,7 @@ public void testSeveralRowsAndRequestsAndPublicStorageAlias() throws Exception {
161165 @ Test
162166 public void testSeveralRowsAndRequestsAndUserStorageAlias () throws Exception {
163167 String sql = "SELECT http_request('foobar.basicJson', `col1`) as data FROM cp.`/data/p4.json`" ;
164- try (MockWebServer server = startServer ()) {
165-
168+ try {
166169 ClientFixture client = cluster .clientBuilder ()
167170 .property (DrillProperties .USER , TEST_USER_2 )
168171 .property (DrillProperties .PASSWORD , TEST_USER_2_PASSWORD )
@@ -203,7 +206,7 @@ public void testSeveralRowsAndRequestsAndPublicTableAlias() throws Exception {
203206 tableAliasesRegistry .getPublicAliases ().put ("`foobar`" , "`basicJson`" , false );
204207
205208 String sql = "SELECT http_request('local.foobar', `col1`) as data FROM cp.`/data/p4.json`" ;
206- try ( MockWebServer server = startServer ()) {
209+ try {
207210 server .enqueue (new MockResponse ().setResponseCode (200 ).setBody (TEST_JSON_PAGE1 ));
208211 server .enqueue (new MockResponse ().setResponseCode (200 ).setBody (TEST_JSON_PAGE1 ));
209212
@@ -233,8 +236,7 @@ public void testSeveralRowsAndRequestsAndPublicTableAlias() throws Exception {
233236 @ Test
234237 public void testSeveralRowsAndRequestsAndUserTableAlias () throws Exception {
235238 String sql = "SELECT http_request('local.foobar', `col1`) as data FROM cp.`/data/p4.json`" ;
236- try (MockWebServer server = startServer ()) {
237-
239+ try {
238240 ClientFixture client = cluster .clientBuilder ()
239241 .property (DrillProperties .USER , TEST_USER_2 )
240242 .property (DrillProperties .PASSWORD , TEST_USER_2_PASSWORD )
@@ -268,10 +270,4 @@ public void testSeveralRowsAndRequestsAndUserTableAlias() throws Exception {
268270 tableAliasesRegistry .deleteUserAliases (TEST_USER_2 );
269271 }
270272 }
271-
272- public static MockWebServer startServer () throws IOException {
273- MockWebServer server = new MockWebServer ();
274- server .start (MOCK_SERVER_PORT );
275- return server ;
276- }
277273}
0 commit comments