Skip to content

Commit

Permalink
fix test fail in RESTCatalogTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry-024 committed Dec 19, 2024
1 parent a682c0f commit a90c527
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public RESTCatalog(CatalogContext catalogContext) {
options, catalogContext.preferIO(), catalogContext.fallbackIO());
this.resourcePaths =
ResourcePaths.forCatalogProperties(options.get(RESTCatalogInternalOptions.PREFIX));
this.fileIO = getFileIOFromOptions(catalogContext);
this.fileIO = getFileIOFromOptions(context);
}

// todo: whether it's ok
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.paimon.rest.responses.ErrorResponse;
import org.apache.paimon.rest.responses.GetDatabaseResponse;
import org.apache.paimon.rest.responses.ListDatabasesResponse;
import org.apache.paimon.rest.responses.ListTablesResponse;

import org.apache.paimon.shade.guava30.com.google.common.collect.Lists;

Expand Down Expand Up @@ -81,4 +82,12 @@ public static AlterDatabaseResponse alterDatabaseResponse() {
return new AlterDatabaseResponse(
Lists.newArrayList("remove"), Lists.newArrayList("add"), new ArrayList<>());
}

public static ListTablesResponse listTablesResponse() {
return new ListTablesResponse(Lists.newArrayList("table"));
}

public static ListTablesResponse listTablesEmptyResponse() {
return new ListTablesResponse(Lists.newArrayList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.paimon.rest.responses.ErrorResponse;
import org.apache.paimon.rest.responses.GetDatabaseResponse;
import org.apache.paimon.rest.responses.ListDatabasesResponse;
import org.apache.paimon.rest.responses.ListTablesResponse;

import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -36,7 +37,9 @@
import okhttp3.mockwebserver.MockWebServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -51,7 +54,6 @@
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/** Test for REST Catalog. */
public class RESTCatalogTest {
Expand All @@ -61,6 +63,8 @@ public class RESTCatalogTest {
private RESTCatalog restCatalog;
private RESTCatalog mockRestCatalog;
private CatalogContext context;
private String warehouseStr;
@Rule public TemporaryFolder folder = new TemporaryFolder();

@Before
public void setUp() throws IOException {
Expand All @@ -72,10 +76,14 @@ public void setUp() throws IOException {
String initToken = "init_token";
options.set(RESTCatalogOptions.TOKEN, initToken);
options.set(RESTCatalogOptions.THREAD_POOL_SIZE, 1);
warehouseStr = folder.getRoot().getPath();
String mockResponse =
String.format(
"{\"defaults\": {\"%s\": \"%s\"}}",
RESTCatalogInternalOptions.PREFIX.key(), "prefix");
"{\"defaults\": {\"%s\": \"%s\", \"%s\": \"%s\"}}",
RESTCatalogInternalOptions.PREFIX.key(),
"prefix",
CatalogOptions.WAREHOUSE.key(),
warehouseStr);
mockResponse(mockResponse, 200);
context = CatalogContext.create(options);
restCatalog = new RESTCatalog(context);
Expand All @@ -90,7 +98,7 @@ public void tearDown() throws IOException {
@Test
public void testInitFailWhenDefineWarehouse() {
Options options = new Options();
options.set(CatalogOptions.WAREHOUSE, "/a/b/c");
options.set(CatalogOptions.WAREHOUSE, warehouseStr);
assertThrows(
IllegalArgumentException.class,
() -> new RESTCatalog(CatalogContext.create(options)));
Expand Down Expand Up @@ -169,8 +177,9 @@ public void testDropDatabaseWhenNoExistAndIgnoreIfNotExistsIsTrue() throws Excep
public void testDropDatabaseWhenCascadeIsFalseAndNoTables() throws Exception {
String name = MockRESTMessage.databaseName();
boolean cascade = false;
ListTablesResponse response = MockRESTMessage.listTablesEmptyResponse();
mockResponse(mapper.writeValueAsString(response), 200);
mockResponse("", 200);
when(mockRestCatalog.listTables(name)).thenReturn(new ArrayList<>());
assertDoesNotThrow(() -> mockRestCatalog.dropDatabase(name, false, cascade));
verify(mockRestCatalog, times(1)).dropDatabase(eq(name), eq(false), eq(cascade));
verify(mockRestCatalog, times(1)).listTables(eq(name));
Expand All @@ -180,10 +189,8 @@ public void testDropDatabaseWhenCascadeIsFalseAndNoTables() throws Exception {
public void testDropDatabaseWhenCascadeIsFalseAndTablesExist() throws Exception {
String name = MockRESTMessage.databaseName();
boolean cascade = false;
mockResponse("", 200);
List<String> tables = new ArrayList<>();
tables.add("t1");
when(mockRestCatalog.listTables(name)).thenReturn(tables);
ListTablesResponse response = MockRESTMessage.listTablesResponse();
mockResponse(mapper.writeValueAsString(response), 200);
assertThrows(
Catalog.DatabaseNotEmptyException.class,
() -> mockRestCatalog.dropDatabase(name, false, cascade));
Expand Down

0 comments on commit a90c527

Please sign in to comment.