Skip to content

Commit

Permalink
Merge pull request #1450 from miahemu/dev
Browse files Browse the repository at this point in the history
Some typos and unit tests.
  • Loading branch information
miahemu authored Jul 2, 2024
2 parents debbf3a + 820e2d5 commit f4b039e
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
public interface ChartService {
/**
* Save report
* Create report
*
* @param param
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class PermissionUtils {

/**
* Verify whether the currently logged in user has permission to operate on the current content
* Verify whether the currently logged-in user has permission to operate on the current content
*
* @param createUserId The creator of the current content
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ public class ChartDO implements Serializable {
private String schemaName;

/**
* ddl content
* DDL content
*/
private String ddl;

/**
* Whether it has been deleted, y means deleted, n means not deleted
* Whether it has been deleted, 'Y' means deleted, 'N' means not deleted
*/
private String deleted;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package ai.chat2db.server.start.test.core;

import ai.chat2db.server.domain.api.chart.ChartCreateParam;
import ai.chat2db.server.domain.api.chart.ChartListQueryParam;
import ai.chat2db.server.domain.api.chart.ChartQueryParam;
import ai.chat2db.server.domain.api.chart.ChartUpdateParam;
import ai.chat2db.server.domain.api.model.Chart;
import ai.chat2db.server.domain.api.service.ChartService;
import ai.chat2db.server.domain.repository.Dbutils;
import ai.chat2db.server.start.test.TestApplication;
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
import ai.chat2db.server.tools.common.model.Context;
import ai.chat2db.server.tools.common.model.LoginUser;
import ai.chat2db.server.tools.common.util.ContextUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Optional;

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


public class ChartServiceTest extends TestApplication {

@Autowired
private ChartService chartService;


@Test
public void testCreateWithPermission() {
try {
userLoginIdentity(false, 4L);
userLoginIdentity(true, 3L);
} catch (Exception e) {
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
}

ChartCreateParam createParam = new ChartCreateParam();
Optional.of(createParam).ifPresent(param -> {
param.setName("chat2db");
param.setSchema("test");
param.setDataSourceId(1L);
param.setType("MYSQL");
param.setDatabaseName("chat2db");
param.setSchemaName("ali_dbhub");
param.setDdl("test");
});

DataResult<Long> withPermission = chartService.createWithPermission(createParam);
assertNotNull(withPermission);

Long id = withPermission.getData();
chartService.find(id);

}


@Test
public void testUpdateWithPermission() {
try {
userLoginIdentity(false, 1L);
userLoginIdentity(true, 4L);
} catch (Exception e) {
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
}

ChartUpdateParam chartUpdateParam = new ChartUpdateParam();
Optional.of(chartUpdateParam).ifPresent(param -> {
param.setId(1L);
param.setName("chat2db");
param.setSchema("test");
param.setDataSourceId(1L);
param.setType("DM");
param.setDatabaseName("chat2db");
param.setSchemaName("ali_dbhub");
param.setDdl("test");
});

ActionResult actionResult = chartService.updateWithPermission(chartUpdateParam);
assertNotNull(actionResult);
}


@Test
public void testFind() {
try {
userLoginIdentity(false, 6L);
userLoginIdentity(true, 8L);
} catch (Exception e) {
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
}

DataResult<Chart> result = chartService.find(2L);
assertNotNull(result.getData());
}


@Test
public void testQueryExistent() {
try {
userLoginIdentity(false, 7L);
userLoginIdentity(true, 9L);
} catch (Exception e) {
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
}

ChartQueryParam chartQueryParam = new ChartQueryParam();
chartQueryParam.setId(1L);
chartQueryParam.setUserId(1L);

DataResult<Chart> chartDataResult = chartService.queryExistent(chartQueryParam);
DataResult<Chart> queryExistent = chartService.queryExistent(chartDataResult.getData().getId());
assertNotNull(chartDataResult);
assertEquals(chartDataResult, queryExistent);
}


@Test
public void testListQuery() {
try {
userLoginIdentity(false, 8L);
userLoginIdentity(true, 10L);
} catch (Exception e) {
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
}

ChartListQueryParam param = new ChartListQueryParam();
param.setIdList(Arrays.asList(4L, 5L, 6L));
param.setUserId(1L);

ListResult<Chart> listQuery = chartService.listQuery(param);
assertNotNull(listQuery);

}


@Test
public void testQueryByIds() {
try {
userLoginIdentity(false, 9L);
userLoginIdentity(true, 11L);
} catch (Exception e) {
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
}

ListResult<Chart> chartListResult = chartService.queryByIds(Arrays.asList(1L, 2L, 3L));
assertNotNull(chartListResult);
}

@Test
public void testDeleteWithPermission() {
try {
userLoginIdentity(false, 10L);
userLoginIdentity(true, 12L);
} catch (Exception e) {
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
}

ActionResult actionResult = chartService.deleteWithPermission(3L);
assertNotNull(actionResult);
}

/**
* Save the current user identity (administrator or normal user) and user ID to the context and database session for subsequent use.
*
* @param isAdmin
* @param userId
*/
private static void userLoginIdentity(boolean isAdmin, Long userId) {
Context context = Context.builder().loginUser(
LoginUser.builder().admin(isAdmin).id(userId).build()
).build();
ContextUtils.setContext(context);
Dbutils.setSession();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package ai.chat2db.server.start.test.core;

import ai.chat2db.server.domain.api.model.Config;
import ai.chat2db.server.domain.api.param.SystemConfigParam;
import ai.chat2db.server.domain.api.service.ConfigService;
import ai.chat2db.server.domain.repository.Dbutils;
import ai.chat2db.server.start.test.TestApplication;
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
import ai.chat2db.server.tools.common.model.Context;
import ai.chat2db.server.tools.common.model.LoginUser;
import ai.chat2db.server.tools.common.util.ContextUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.security.SecureRandom;
import java.util.Optional;

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

public class ConfigServiceTest extends TestApplication {

@Autowired
private ConfigService configService;

@Test
public void testCreate() {
try {
userLoginIdentity(true, 1L);
userLoginIdentity(false, 2L);
} catch (Exception e) {
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
}

SystemConfigParam systemConfigParam = new SystemConfigParam();
Optional.ofNullable(systemConfigParam).ifPresent(param -> {
param.setCode(RandomCodeGenerator.generateRandomCode(6));
param.setContent(RandomCodeGenerator.generateRandomCode(6));
param.setSummary(RandomCodeGenerator.generateRandomCode(6));
});

ActionResult actionResult = configService.create(systemConfigParam);
assertNotNull(actionResult);
}

@Test
public void testUpdate() {
try {
userLoginIdentity(true, 4L);
userLoginIdentity(false, 5L);
} catch (Exception e) {
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
}

SystemConfigParam systemConfigParam = new SystemConfigParam();
systemConfigParam.setCode(RandomCodeGenerator.generateRandomCode(6));
systemConfigParam.setContent(RandomCodeGenerator.generateRandomCode(6));
systemConfigParam.setSummary(RandomCodeGenerator.generateRandomCode(6));

ActionResult update = configService.update(systemConfigParam);
assertNotNull(update);

}

@Test
public void testCreateOrUpdate() {
try {
userLoginIdentity(true, 3L);
userLoginIdentity(false, 6L);
} catch (Exception e) {
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
}

SystemConfigParam systemConfigParam = new SystemConfigParam();
systemConfigParam.setCode(RandomCodeGenerator.generateRandomCode(6));
systemConfigParam.setContent(RandomCodeGenerator.generateRandomCode(6));
systemConfigParam.setSummary(RandomCodeGenerator.generateRandomCode(6));
ActionResult orUpdate = configService.createOrUpdate(systemConfigParam);
assertNotNull(orUpdate);

}

@Test
public void testFind() {
try {
userLoginIdentity(true, 9L);
userLoginIdentity(false, 4L);
} catch (Exception e) {
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
}

DataResult<Config> configDataResult = configService.find("4TxfzW");
assertNotNull(configDataResult.getData());
}

@Test
public void testDelete() {
try {
userLoginIdentity(true, 11L);
userLoginIdentity(false, 12L);
} catch (Exception e) {
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
}

ActionResult result = configService.delete("4TxfzW");
assertNotNull(result);
}

/**
* Save the current user identity (administrator or normal user) and user ID to the context and database session for subsequent use.
*
* @param isAdmin
* @param userId
*/
private static void userLoginIdentity(boolean isAdmin, Long userId) {
Context context = Context.builder().loginUser(
LoginUser.builder().admin(isAdmin).id(userId).build()
).build();
ContextUtils.setContext(context);
Dbutils.setSession();
}

public class RandomCodeGenerator {
private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
private static final SecureRandom RANDOM = new SecureRandom();

public static String generateRandomCode(int length) {
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
sb.append(CHARACTERS.charAt(RANDOM.nextInt(CHARACTERS.length())));
}
return sb.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class Function implements Serializable {
private static final long serialVersionUID = 1L;
//FUNCTION_CAT String => function catalog (may be null)
//FUNCTION_SCHEM String => function schema (may be null)
//FUNCTION_SCHEME String => function schema (may be null)
//FUNCTION_NAME String => function name. This is the name used to invoke the function
//REMARKS String => explanatory comment on the function
//FUNCTION_TYPE short => kind of function:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class Procedure implements Serializable {
private static final long serialVersionUID = 1L;
//PROCEDURE_CAT String => procedure catalog (may be null)
//PROCEDURE_SCHEM String => procedure schema (may be null)
//PROCEDURE_SCHEME String => procedure schema (may be null)
//PROCEDURE_NAME String => procedure name
//REMARKS String => explanatory comment on the procedure
//PROCEDURE_TYPE short => kind of procedure:
Expand Down

0 comments on commit f4b039e

Please sign in to comment.