forked from apache/paimon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
80 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 75 additions & 24 deletions
99
...link/paimon-flink-common/src/test/java/org/apache/paimon/flink/MockRESTCatalogServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,97 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.paimon.flink; | ||
|
||
import java.io.IOException; | ||
import org.apache.paimon.catalog.Catalog; | ||
import org.apache.paimon.catalog.CatalogContext; | ||
import org.apache.paimon.catalog.CatalogFactory; | ||
import org.apache.paimon.options.CatalogOptions; | ||
import org.apache.paimon.options.Options; | ||
import org.apache.paimon.rest.RESTCatalogInternalOptions; | ||
|
||
import okhttp3.mockwebserver.Dispatcher; | ||
import okhttp3.mockwebserver.MockResponse; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
import okhttp3.mockwebserver.RecordedRequest; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.apache.paimon.flink.FlinkCatalogOptions.LOG_SYSTEM_AUTO_REGISTER; | ||
|
||
/** Mock REST server for testing. */ | ||
public class MockRESTCatalogServer { | ||
public static void main(String[] args) { | ||
MockWebServer server = new MockWebServer(); | ||
final Dispatcher dispatcher = new Dispatcher() { | ||
|
||
private final Catalog catalog; | ||
private final Dispatcher dispatcher; | ||
private final MockWebServer server; | ||
|
||
public MockRESTCatalogServer(String warehouse) { | ||
Options conf = new Options(); | ||
conf.setString("warehouse", warehouse); | ||
conf.set(LOG_SYSTEM_AUTO_REGISTER, true); | ||
this.catalog = | ||
CatalogFactory.createCatalog( | ||
CatalogContext.create(conf), this.getClass().getClassLoader()); | ||
this.dispatcher = initDispatcher(); | ||
MockWebServer mockWebServer = new MockWebServer(); | ||
mockWebServer.setDispatcher(dispatcher); | ||
server = mockWebServer; | ||
} | ||
|
||
public void start() throws IOException { | ||
server.start(); | ||
} | ||
|
||
public String getUrl() { | ||
return server.url("").toString(); | ||
} | ||
|
||
public void shutdown() throws IOException { | ||
server.shutdown(); | ||
} | ||
|
||
public static Dispatcher initDispatcher() { | ||
return new Dispatcher() { | ||
@Override | ||
public MockResponse dispatch(RecordedRequest request) throws InterruptedException { | ||
|
||
switch (request.getPath()) { | ||
case "/v1/login/auth/": | ||
return new MockResponse().setResponseCode(200); | ||
case "/v1/check/version/": | ||
case "/v1/config": | ||
return new MockResponse() | ||
.setResponseCode(200) | ||
.setBody(getConfigBody("/tmp/1")); | ||
case "/v1/prefix/databases/": | ||
return new MockResponse().setResponseCode(200).setBody("version=9"); | ||
case "/v1/profile/info": | ||
return new MockResponse().setResponseCode(200).setBody("profile"); | ||
} | ||
return new MockResponse().setResponseCode(404); | ||
} | ||
}; | ||
server.setDispatcher(dispatcher); | ||
try { | ||
server.start(8099); | ||
String serverUrl = server.url("").toString(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
System.exit(0); | ||
} | ||
while (true) { | ||
try { | ||
Thread.sleep(1000); | ||
RecordedRequest request = server.takeRequest(); | ||
System.out.println("Request: " + request.getPath()); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
private static String getConfigBody(String warehouseStr) { | ||
return String.format( | ||
"{\"defaults\": {\"%s\": \"%s\", \"%s\": \"%s\"}}", | ||
RESTCatalogInternalOptions.PREFIX.key(), | ||
"prefix", | ||
CatalogOptions.WAREHOUSE.key(), | ||
warehouseStr); | ||
} | ||
} |