|
| 1 | +package ai.chat2db.server.start.test.core; |
| 2 | + |
| 3 | +import ai.chat2db.server.domain.api.model.Operation; |
| 4 | +import ai.chat2db.server.domain.api.param.operation.OperationPageQueryParam; |
| 5 | +import ai.chat2db.server.domain.api.param.operation.OperationQueryParam; |
| 6 | +import ai.chat2db.server.domain.api.param.operation.OperationSavedParam; |
| 7 | +import ai.chat2db.server.domain.api.param.operation.OperationUpdateParam; |
| 8 | +import ai.chat2db.server.domain.api.service.OperationService; |
| 9 | +import ai.chat2db.server.domain.repository.Dbutils; |
| 10 | +import ai.chat2db.server.start.test.TestApplication; |
| 11 | +import ai.chat2db.server.start.test.dialect.DialectProperties; |
| 12 | +import ai.chat2db.server.start.test.dialect.TestUtils; |
| 13 | +import ai.chat2db.server.tools.base.wrapper.result.ActionResult; |
| 14 | +import ai.chat2db.server.tools.base.wrapper.result.DataResult; |
| 15 | +import ai.chat2db.server.tools.base.wrapper.result.PageResult; |
| 16 | +import ai.chat2db.server.tools.common.model.Context; |
| 17 | +import ai.chat2db.server.tools.common.model.LoginUser; |
| 18 | +import ai.chat2db.server.tools.common.util.ContextUtils; |
| 19 | +import org.junit.jupiter.api.Assertions; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | + |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Juechen |
| 27 | + * @version : OperationServiceTest.java |
| 28 | + */ |
| 29 | +public class OperationServiceTest extends TestApplication { |
| 30 | + |
| 31 | + @Autowired |
| 32 | + private OperationService operationService; |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private List<DialectProperties> dialectPropertiesList; |
| 36 | + |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testCreateWithPermission() { |
| 40 | + |
| 41 | + userLoginIdentity(true, 7L); |
| 42 | + |
| 43 | + for (DialectProperties dialectProperties : dialectPropertiesList) { |
| 44 | + Long dataSourceId = TestUtils.nextLong(); |
| 45 | + Long consoleId = TestUtils.nextLong(); |
| 46 | + TestUtils.buildContext(dialectProperties, dataSourceId, consoleId); |
| 47 | + |
| 48 | + OperationSavedParam param = new OperationSavedParam(); |
| 49 | + param.setDataSourceId(dataSourceId); |
| 50 | + param.setType(dialectProperties.getDbType()); |
| 51 | +// param.setStatus("DRAFT"); |
| 52 | + param.setStatus("RELEASE"); |
| 53 | + |
| 54 | + DataResult<Long> result = operationService.createWithPermission(param); |
| 55 | + System.out.println(dialectProperties.getDbType() + "---" + result.getData()); |
| 56 | + Assertions.assertTrue(result.success(), result.getErrorMessage()); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testUpdateWithPermission() { |
| 62 | + |
| 63 | + userLoginIdentity(true, 3L); |
| 64 | + |
| 65 | + for (DialectProperties dialectProperties : dialectPropertiesList) { |
| 66 | + Long dataSourceId = TestUtils.nextLong(); |
| 67 | + Long consoleId = TestUtils.nextLong(); |
| 68 | + TestUtils.buildContext(dialectProperties, dataSourceId, consoleId); |
| 69 | + |
| 70 | + OperationUpdateParam param = new OperationUpdateParam(); |
| 71 | + param.setId(9L); |
| 72 | + |
| 73 | + ActionResult result = operationService.updateWithPermission(param); |
| 74 | + Assertions.assertTrue(result.success(), result.getErrorMessage()); |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testFind() { |
| 81 | + userLoginIdentity(true, 6L); |
| 82 | + |
| 83 | + for (DialectProperties dialectProperties : dialectPropertiesList) { |
| 84 | + Long dataSourceId = TestUtils.nextLong(); |
| 85 | + Long consoleId = TestUtils.nextLong(); |
| 86 | + TestUtils.buildContext(dialectProperties, dataSourceId, consoleId); |
| 87 | + |
| 88 | + DataResult<Operation> result = operationService.find(18L); |
| 89 | + Assertions.assertTrue(result.success(), result.getErrorMessage()); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testQueryExistent() { |
| 95 | + userLoginIdentity(false, 7L); |
| 96 | + |
| 97 | + for (DialectProperties dialectProperties : dialectPropertiesList) { |
| 98 | + Long dataSourceId = TestUtils.nextLong(); |
| 99 | + Long consoleId = TestUtils.nextLong(); |
| 100 | + TestUtils.buildContext(dialectProperties, dataSourceId, consoleId); |
| 101 | + |
| 102 | + OperationQueryParam param = new OperationQueryParam(); |
| 103 | + param.setId(11L); |
| 104 | + |
| 105 | + DataResult<Operation> result = operationService.queryExistent(10L); |
| 106 | + DataResult<Operation> result1 = operationService.queryExistent(param); |
| 107 | + Assertions.assertTrue(result.success(), result.getErrorMessage()); |
| 108 | + Assertions.assertTrue(result1.success(), result1.getErrorMessage()); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testDeleteWithPermission() { |
| 114 | + |
| 115 | + userLoginIdentity(true, 8L); |
| 116 | + |
| 117 | + for (DialectProperties dialectProperties : dialectPropertiesList) { |
| 118 | + Long dataSourceId = TestUtils.nextLong(); |
| 119 | + Long consoleId = TestUtils.nextLong(); |
| 120 | + TestUtils.buildContext(dialectProperties, dataSourceId, consoleId); |
| 121 | + |
| 122 | + OperationSavedParam param = new OperationSavedParam(); |
| 123 | + param.setDataSourceId(10L); |
| 124 | + param.setType(dialectProperties.getDbType()); |
| 125 | +// param.setStatus("DRAFT"); |
| 126 | + param.setStatus("RELEASE"); |
| 127 | + |
| 128 | + DataResult<Long> service = operationService.createWithPermission(param); |
| 129 | + ActionResult result = operationService.deleteWithPermission(service.getData()); |
| 130 | + Assertions.assertTrue(result.success(), result.getErrorMessage()); |
| 131 | + |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testQueryPage() { |
| 137 | + |
| 138 | + userLoginIdentity(false, 9L); |
| 139 | + |
| 140 | + for (DialectProperties dialectProperties : dialectPropertiesList) { |
| 141 | + Long dataSourceId = TestUtils.nextLong(); |
| 142 | + Long consoleId = TestUtils.nextLong(); |
| 143 | + TestUtils.buildContext(dialectProperties, dataSourceId, consoleId); |
| 144 | + |
| 145 | + OperationPageQueryParam param = new OperationPageQueryParam(); |
| 146 | + param.setStatus("RELEASE"); |
| 147 | + param.setSearchKey("test"); |
| 148 | + param.setDataSourceId(dataSourceId); |
| 149 | + param.setDatabaseName(dialectProperties.getDatabaseName()); |
| 150 | + param.setTabOpened("Y"); |
| 151 | + param.setPageNo(1); |
| 152 | + param.setPageSize(10); |
| 153 | + param.setOrderByDesc(true); |
| 154 | + param.setOrderByCreateDesc(true); |
| 155 | + |
| 156 | + PageResult<Operation> result = operationService.queryPage(param); |
| 157 | + Assertions.assertTrue(result.success(), result.getErrorMessage()); |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Save the current user identity (administrator or normal user) and user ID to the context and database session for subsequent use. |
| 165 | + * |
| 166 | + * @param isAdmin |
| 167 | + * @param userId |
| 168 | + */ |
| 169 | + private static void userLoginIdentity(boolean isAdmin, Long userId) { |
| 170 | + Context context = Context.builder().loginUser( |
| 171 | + LoginUser.builder().admin(isAdmin).id(userId).build() |
| 172 | + ).build(); |
| 173 | + ContextUtils.setContext(context); |
| 174 | + Dbutils.setSession(); |
| 175 | + } |
| 176 | +} |
0 commit comments