Skip to content

Commit

Permalink
improve test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
s7monk committed Jan 11, 2024
1 parent 126d6c4 commit 6da6b15
Show file tree
Hide file tree
Showing 6 changed files with 413 additions and 259 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ public void testGetCatalog() throws Exception {
.getResponse()
.getContentAsString();

R<List<CatalogInfo>> r = ObjectMapperUtils.fromJSON(responseString, new TypeReference<R<List<CatalogInfo>>>() {});
R<List<CatalogInfo>> r =
ObjectMapperUtils.fromJSON(
responseString, new TypeReference<R<List<CatalogInfo>>>() {});
assertEquals(200, r.getCode());
assertNotNull(r.getData());
assertEquals(1, r.getData().size());
assertEquals(catalogName, r.getData().get(0).getCatalogName());
}

Expand All @@ -105,17 +108,17 @@ public void testRemoveCatalog() throws Exception {
removeCatalog.setName(catalogName);

String responseString =
mockMvc.perform(
MockMvcRequestBuilders.post(catalogPath + "/remove")
.cookie(cookie)
.content(ObjectMapperUtils.toJSON(removeCatalog))
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn()
.getResponse()
.getContentAsString();
mockMvc.perform(
MockMvcRequestBuilders.post(catalogPath + "/remove")
.cookie(cookie)
.content(ObjectMapperUtils.toJSON(removeCatalog))
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn()
.getResponse()
.getContentAsString();

R<Void> remove =
ObjectMapperUtils.fromJSON(responseString, new TypeReference<R<Void>>() {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,13 @@

package org.apache.paimon.web.server.controller;

import org.apache.paimon.web.server.data.dto.CatalogDTO;
import org.apache.paimon.web.server.data.dto.DatabaseDTO;
import org.apache.paimon.web.server.data.dto.LoginDTO;
import org.apache.paimon.web.server.data.dto.TableDTO;
import org.apache.paimon.web.server.data.model.TableColumn;
import org.apache.paimon.web.server.data.result.PageR;
import org.apache.paimon.web.server.data.result.R;
import org.apache.paimon.web.server.util.ObjectMapperUtils;
import org.apache.paimon.web.server.util.PaimonDataType;
import org.apache.paimon.web.server.util.StringUtils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.io.TempDir;
Expand All @@ -48,9 +41,6 @@
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -61,9 +51,6 @@ public class ControllerTestBase {

private static final String loginPath = "/api/login";
private static final String logoutPath = "/api/logout";
private static final String catalogPath = "/api/catalog";
private static final String databasePath = "/api/database";
private static final String tablePath = "/api/table";

@Value("${spring.application.name}")
private String tokenName;
Expand All @@ -74,14 +61,6 @@ public class ControllerTestBase {

@TempDir java.nio.file.Path tempFile;

private static final Integer catalogId = 1;

private static final String catalogName = "paimon_catalog";

private static final String databaseName = "paimon_database";

private static final String tableName = "paimon_table";

@BeforeEach
public void before() throws Exception {
LoginDTO login = new LoginDTO();
Expand All @@ -104,72 +83,6 @@ public void before() throws Exception {
assertTrue(StringUtils.isNotBlank(r.getData().toString()));

cookie = (MockCookie) response.getCookie(tokenName);

/* // create default catalog
CatalogDTO catalog = new CatalogDTO();
catalog.setType("filesystem");
catalog.setName(catalogName);
catalog.setWarehouse(tempFile.toUri().toString());
catalog.setDelete(false);
ResultActions createCatalogRa =
mockMvc.perform(
MockMvcRequestBuilders.post(catalogPath + "/create")
.cookie(cookie)
.content(ObjectMapperUtils.toJSON(catalog))
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON_VALUE));
R<?> createCatalogR = getR(createCatalogRa);
assertEquals(200, createCatalogR.getCode());*/

/*// create default database
DatabaseDTO database = new DatabaseDTO();
database.setName(databaseName);
database.setCatalogName(catalogName);
ResultActions createDatabaseRa =
mockMvc.perform(
MockMvcRequestBuilders.post(databasePath + "/create")
.cookie(cookie)
.content(ObjectMapperUtils.toJSON(database))
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON_VALUE));
R<?> createDatabaseR = getR(createDatabaseRa);
assertEquals(200, createDatabaseR.getCode());
// create default table
List<TableColumn> tableColumns = new ArrayList<>();
TableColumn id =
new TableColumn("id", PaimonDataType.builder().type("INT").build(), "", false, "0");
TableColumn name =
new TableColumn(
"name", PaimonDataType.builder().type("STRING").build(), "", false, "0");
tableColumns.add(id);
tableColumns.add(name);
TableDTO table =
TableDTO.builder()
.catalogName(catalogName)
.databaseName(databaseName)
.name(tableName)
.tableColumns(tableColumns)
.partitionKey(Lists.newArrayList())
.tableOptions(Maps.newHashMap())
.build();
MockHttpServletResponse createTableResultAction =
mockMvc.perform(
MockMvcRequestBuilders.post(tablePath + "/create")
.cookie(cookie)
.content(ObjectMapperUtils.toJSON(table))
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn()
.getResponse();
String createTableResult = createTableResultAction.getContentAsString();
R<?> createTableR = ObjectMapperUtils.fromJSON(createTableResult, R.class);
assertEquals(200, createTableR.getCode());*/
}

@AfterEach
Expand All @@ -187,47 +100,6 @@ public void after() throws Exception {
.getContentAsString();
R<?> r = ObjectMapperUtils.fromJSON(result, R.class);
assertEquals(200, r.getCode());

/* ResultActions dropTableRa =
mockMvc.perform(
MockMvcRequestBuilders.delete(
tablePath
+ "/drop/"
+ catalogName
+ "/"
+ databaseName
+ "/"
+ tableName)
.cookie(cookie)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON_VALUE));
R<?> dropDatabaseR = getR(dropTableRa);
assertEquals(200, dropDatabaseR.getCode());
DatabaseDTO database = new DatabaseDTO();
database.setName(databaseName);
database.setCatalogName(catalogName);
ResultActions dropDatabasesRa =
mockMvc.perform(
MockMvcRequestBuilders.post(databasePath + "/drop")
.cookie(cookie)
.content(ObjectMapperUtils.toJSON(database))
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON_VALUE));
R<?> dropDatabasesR = getR(dropDatabasesRa);
assertEquals(200, dropDatabasesR.getCode());
CatalogDTO catalog = new CatalogDTO();
catalog.setName(catalogName);
ResultActions removeCatalogRa =
mockMvc.perform(
MockMvcRequestBuilders.post(catalogPath + "/remove")
.cookie(cookie)
.content(ObjectMapperUtils.toJSON(catalog))
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON_VALUE));
R<?> removeCatalogResult = getR(removeCatalogRa);
assertEquals(200, removeCatalogResult.getCode());*/
}

protected R<?> getR(ResultActions perform) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,22 @@

import com.fasterxml.jackson.core.type.TypeReference;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.nio.file.Files;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/** Test for DatabaseController. */
@SpringBootTest
@AutoConfigureMockMvc
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class DatabaseControllerTest extends ControllerTestBase {

private static final String catalogPath = "/api/catalog";
Expand All @@ -59,8 +52,6 @@ public class DatabaseControllerTest extends ControllerTestBase {

private static final String catalogName = "paimon_catalog";

@TempDir java.nio.file.Path tempFile;

private Integer catalogId;

@BeforeEach
Expand All @@ -71,18 +62,16 @@ public void setup() throws Exception {
catalog.setWarehouse(tempFile.toUri().toString());
catalog.setDelete(false);

// create catalog.
mockMvc.perform(
MockMvcRequestBuilders.post(catalogPath + "/create")
.cookie(cookie)
.content(ObjectMapperUtils.toJSON(catalog))
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn()
.getResponse()
.getContentAsString();
.andExpect(MockMvcResultMatchers.status().isOk());

// get catalog id.
String responseString =
mockMvc.perform(
MockMvcRequestBuilders.get(catalogPath + "/list")
Expand All @@ -94,42 +83,29 @@ public void setup() throws Exception {
.andReturn()
.getResponse()
.getContentAsString();

R<List<CatalogInfo>> r = ObjectMapperUtils.fromJSON(responseString, new TypeReference<R<List<CatalogInfo>>>() {});
R<List<CatalogInfo>> r =
ObjectMapperUtils.fromJSON(
responseString, new TypeReference<R<List<CatalogInfo>>>() {});
catalogId = r.getData().get(0).getId();
}

@Test
@Order(1)
public void testCreateDatabase() throws Exception {
// create database.
DatabaseDTO database = new DatabaseDTO();
database.setCatalogId(catalogId);
database.setName(databaseName);
database.setCatalogName(catalogName);
database.setIgnoreIfExists(true);

String responseString =
mockMvc.perform(
MockMvcRequestBuilders.post(databasePath + "/create")
.cookie(cookie)
.content(ObjectMapperUtils.toJSON(database))
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn()
.getResponse()
.getContentAsString();

R<Void> r = ObjectMapperUtils.fromJSON(responseString, new TypeReference<R<Void>>() {});
assertEquals(200, r.getCode());
assertTrue(Files.exists(tempFile.resolve("test_db.db")));
mockMvc.perform(
MockMvcRequestBuilders.post(databasePath + "/create")
.cookie(cookie)
.content(ObjectMapperUtils.toJSON(database))
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(MockMvcResultMatchers.status().isOk());
}

@Test
@Order(2)
public void testListDatabases() throws Exception {
assertTrue(Files.exists(tempFile.resolve("test_db.db")));
String responseString =
mockMvc.perform(
MockMvcRequestBuilders.get(databasePath + "/list")
Expand All @@ -143,14 +119,16 @@ public void testListDatabases() throws Exception {
.getResponse()
.getContentAsString();

R<List<DatabaseVO>> r = ObjectMapperUtils.fromJSON(responseString, new TypeReference<R<List<DatabaseVO>>>() {});
R<List<DatabaseVO>> r =
ObjectMapperUtils.fromJSON(
responseString, new TypeReference<R<List<DatabaseVO>>>() {});
assertEquals(200, r.getCode());
assertNotNull(r.getData());
assertEquals(1, r.getData().size());
assertEquals(databaseName, r.getData().get(0).getName());
}

@Test
@Order(3)
public void testDropDatabase() throws Exception {
DatabaseDTO dropDatabase = new DatabaseDTO();
dropDatabase.setCatalogName(catalogName);
Expand All @@ -160,17 +138,17 @@ public void testDropDatabase() throws Exception {
dropDatabase.setCascade(true);

String responseString =
mockMvc.perform(
MockMvcRequestBuilders.post(databasePath + "/drop")
.cookie(cookie)
.content(ObjectMapperUtils.toJSON(dropDatabase))
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn()
.getResponse()
.getContentAsString();
mockMvc.perform(
MockMvcRequestBuilders.post(databasePath + "/drop")
.cookie(cookie)
.content(ObjectMapperUtils.toJSON(dropDatabase))
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn()
.getResponse()
.getContentAsString();

R<Void> remove =
ObjectMapperUtils.fromJSON(responseString, new TypeReference<R<Void>>() {});
Expand Down
Loading

0 comments on commit 6da6b15

Please sign in to comment.