Skip to content

Commit

Permalink
[core] rebase master.
Browse files Browse the repository at this point in the history
  • Loading branch information
LinMingQiang committed Jan 10, 2025
1 parent 943bf79 commit 84069c5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
package org.apache.paimon.partition.actions;

import org.apache.paimon.CoreOptions;
import org.apache.paimon.rest.DefaultErrorHandler;
import org.apache.paimon.annotation.VisibleForTesting;
import org.apache.paimon.rest.HttpClient;
import org.apache.paimon.rest.HttpClientOptions;
import org.apache.paimon.rest.RESTClient;
import org.apache.paimon.rest.RESTObjectMapper;
import org.apache.paimon.rest.RESTRequest;
import org.apache.paimon.rest.RESTResponse;
import org.apache.paimon.table.FileStoreTable;
Expand All @@ -37,7 +36,6 @@

import java.io.IOException;
import java.util.Collections;
import java.util.Optional;

import static org.apache.paimon.CoreOptions.PARTITION_MARK_DONE_ACTION_URL;

Expand Down Expand Up @@ -66,11 +64,9 @@ public HttpReportMarkDoneAction(FileStoreTable fileStoreTable, CoreOptions optio
HttpClientOptions httpClientOptions =
new HttpClientOptions(
options.httpReportMarkDoneActionUrl(),
Optional.of(options.httpReportMarkDoneActionTimeout()),
Optional.of(options.httpReportMarkDoneActionTimeout()),
RESTObjectMapper.create(),
1,
DefaultErrorHandler.getInstance());
options.httpReportMarkDoneActionTimeout(),
options.httpReportMarkDoneActionTimeout(),
1);
this.client = new HttpClient(httpClientOptions);
}

Expand Down Expand Up @@ -108,6 +104,7 @@ public void close() throws IOException {

/** RestRequest only for HttpReportMarkDoneAction. */
@JsonIgnoreProperties(ignoreUnknown = true)
@VisibleForTesting
public static class HttpReportMarkDoneRequest implements RESTRequest {

private static final String MARK_DONE_PARTITION = "partition";
Expand Down Expand Up @@ -162,6 +159,7 @@ public String getParams() {

/** Response only for HttpReportMarkDoneAction. */
@JsonIgnoreProperties(ignoreUnknown = true)
@VisibleForTesting
public static class HttpReportMarkDoneResponse implements RESTResponse {
private static final String RESULT = "result";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ static List<PartitionMarkDoneAction> createActions(
case MARK_EVENT:
return new MarkPartitionDoneEventAction(
createPartitionHandler(fileStoreTable, options));
createMetastoreClient(fileStoreTable, options));
case HTTP_REPORT:
return new HttpReportMarkDoneAction(fileStoreTable, options);
case CUSTOM:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
import org.apache.paimon.rest.responses.ErrorResponse;
import org.apache.paimon.rest.responses.ErrorResponseResourceType;

import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.ObjectMapper;

import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -45,29 +41,28 @@ public class HttpClientTest {

private static final String MOCK_PATH = "/v1/api/mock";
private static final String TOKEN = "token";
private static final ObjectMapper OBJECT_MAPPER = RESTObjectMapper.create();

private MockWebServer mockWebServer;
private TestHttpWebServer server;
private HttpClient httpClient;
private ErrorHandler errorHandler;
private MockRESTData mockResponseData;
private String mockResponseDataStr;
private ErrorResponse errorResponse;
private String errorResponseStr;
private Map<String, String> headers;

@Before
public void setUp() throws IOException {
mockWebServer = new MockWebServer();
mockWebServer.start();
String baseUrl = mockWebServer.url("").toString();
public void setUp() throws Exception {
server = new TestHttpWebServer(MOCK_PATH);
server.start();
errorHandler = DefaultErrorHandler.getInstance();
HttpClientOptions httpClientOptions =
new HttpClientOptions(baseUrl, Duration.ofSeconds(3), Duration.ofSeconds(3), 1);
new HttpClientOptions(
server.getBaseUrl(), Duration.ofSeconds(3), Duration.ofSeconds(3), 1);
mockResponseData = new MockRESTData(MOCK_PATH);
mockResponseDataStr = OBJECT_MAPPER.writeValueAsString(mockResponseData);
errorResponse = new ErrorResponse(ErrorResponseResourceType.DATABASE, "test", "test", 400);
errorResponseStr = OBJECT_MAPPER.writeValueAsString(errorResponse);
mockResponseDataStr = server.createResponseBody(mockResponseData);
errorResponseStr =
server.createResponseBody(
new ErrorResponse(ErrorResponseResourceType.DATABASE, "test", "test", 400));
httpClient = new HttpClient(httpClientOptions);
httpClient.setErrorHandler(errorHandler);
CredentialsProvider credentialsProvider = new BearTokenCredentialsProvider(TOKEN);
Expand All @@ -76,61 +71,49 @@ public void setUp() throws IOException {

@After
public void tearDown() throws IOException {
mockWebServer.shutdown();
server.stop();
}

@Test
public void testGetSuccess() {
mockHttpCallWithCode(mockResponseDataStr, 200);
server.enqueueResponse(mockResponseDataStr, 200);
MockRESTData response = httpClient.get(MOCK_PATH, MockRESTData.class, headers);
assertEquals(mockResponseData.data(), response.data());
}

@Test
public void testGetFail() {
mockHttpCallWithCode(errorResponseStr, 400);
server.enqueueResponse(errorResponseStr, 400);
assertThrows(
BadRequestException.class,
() -> httpClient.get(MOCK_PATH, MockRESTData.class, headers));
}

@Test
public void testPostSuccess() {
mockHttpCallWithCode(mockResponseDataStr, 200);
server.enqueueResponse(mockResponseDataStr, 200);
MockRESTData response =
httpClient.post(MOCK_PATH, mockResponseData, MockRESTData.class, headers);
assertEquals(mockResponseData.data(), response.data());
}

@Test
public void testPostFail() {
mockHttpCallWithCode(errorResponseStr, 400);
server.enqueueResponse(errorResponseStr, 400);
assertThrows(
BadRequestException.class,
() -> httpClient.post(MOCK_PATH, mockResponseData, ErrorResponse.class, headers));
}

@Test
public void testDeleteSuccess() {
mockHttpCallWithCode(mockResponseDataStr, 200);
server.enqueueResponse(mockResponseDataStr, 200);
assertDoesNotThrow(() -> httpClient.delete(MOCK_PATH, headers));
}

@Test
public void testDeleteFail() {
mockHttpCallWithCode(errorResponseStr, 400);
server.enqueueResponse(errorResponseStr, 400);
assertThrows(BadRequestException.class, () -> httpClient.delete(MOCK_PATH, headers));
}

private void mockHttpCallWithCode(String body, Integer code) {
MockResponse mockResponseObj = generateMockResponse(body, code);
mockWebServer.enqueue(mockResponseObj);
}

private MockResponse generateMockResponse(String data, Integer code) {
return new MockResponse()
.setResponseCode(code)
.setBody(data)
.addHeader("Content-Type", "application/json");
}
}

0 comments on commit 84069c5

Please sign in to comment.