|
| 1 | +package ai.chat2db.server.start.test.core; |
| 2 | + |
| 3 | +import ai.chat2db.server.domain.api.chart.ChartCreateParam; |
| 4 | +import ai.chat2db.server.domain.api.chart.ChartListQueryParam; |
| 5 | +import ai.chat2db.server.domain.api.chart.ChartQueryParam; |
| 6 | +import ai.chat2db.server.domain.api.chart.ChartUpdateParam; |
| 7 | +import ai.chat2db.server.domain.api.model.Chart; |
| 8 | +import ai.chat2db.server.domain.api.service.ChartService; |
| 9 | +import ai.chat2db.server.domain.repository.Dbutils; |
| 10 | +import ai.chat2db.server.start.test.TestApplication; |
| 11 | +import ai.chat2db.server.tools.base.wrapper.result.ActionResult; |
| 12 | +import ai.chat2db.server.tools.base.wrapper.result.DataResult; |
| 13 | +import ai.chat2db.server.tools.base.wrapper.result.ListResult; |
| 14 | +import ai.chat2db.server.tools.common.model.Context; |
| 15 | +import ai.chat2db.server.tools.common.model.LoginUser; |
| 16 | +import ai.chat2db.server.tools.common.util.ContextUtils; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.springframework.beans.factory.annotation.Autowired; |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 25 | + |
| 26 | + |
| 27 | +public class ChartServiceTest extends TestApplication { |
| 28 | + |
| 29 | + @Autowired |
| 30 | + private ChartService chartService; |
| 31 | + |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testCreateWithPermission() { |
| 35 | + try { |
| 36 | + userLoginIdentity(false, 4L); |
| 37 | + userLoginIdentity(true, 3L); |
| 38 | + } catch (Exception e) { |
| 39 | + throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); |
| 40 | + } |
| 41 | + |
| 42 | + ChartCreateParam createParam = new ChartCreateParam(); |
| 43 | + Optional.of(createParam).ifPresent(param -> { |
| 44 | + param.setName("chat2db"); |
| 45 | + param.setSchema("test"); |
| 46 | + param.setDataSourceId(1L); |
| 47 | + param.setType("MYSQL"); |
| 48 | + param.setDatabaseName("chat2db"); |
| 49 | + param.setSchemaName("ali_dbhub"); |
| 50 | + param.setDdl("test"); |
| 51 | + }); |
| 52 | + |
| 53 | + DataResult<Long> withPermission = chartService.createWithPermission(createParam); |
| 54 | + assertNotNull(withPermission); |
| 55 | + |
| 56 | + Long id = withPermission.getData(); |
| 57 | + chartService.find(id); |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testUpdateWithPermission() { |
| 64 | + try { |
| 65 | + userLoginIdentity(false, 1L); |
| 66 | + userLoginIdentity(true, 4L); |
| 67 | + } catch (Exception e) { |
| 68 | + throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); |
| 69 | + } |
| 70 | + |
| 71 | + ChartUpdateParam chartUpdateParam = new ChartUpdateParam(); |
| 72 | + Optional.of(chartUpdateParam).ifPresent(param -> { |
| 73 | + param.setId(1L); |
| 74 | + param.setName("chat2db"); |
| 75 | + param.setSchema("test"); |
| 76 | + param.setDataSourceId(1L); |
| 77 | + param.setType("DM"); |
| 78 | + param.setDatabaseName("chat2db"); |
| 79 | + param.setSchemaName("ali_dbhub"); |
| 80 | + param.setDdl("test"); |
| 81 | + }); |
| 82 | + |
| 83 | + ActionResult actionResult = chartService.updateWithPermission(chartUpdateParam); |
| 84 | + assertNotNull(actionResult); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testFind() { |
| 90 | + try { |
| 91 | + userLoginIdentity(false, 6L); |
| 92 | + userLoginIdentity(true, 8L); |
| 93 | + } catch (Exception e) { |
| 94 | + throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); |
| 95 | + } |
| 96 | + |
| 97 | + DataResult<Chart> result = chartService.find(2L); |
| 98 | + assertNotNull(result.getData()); |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testQueryExistent() { |
| 104 | + try { |
| 105 | + userLoginIdentity(false, 7L); |
| 106 | + userLoginIdentity(true, 9L); |
| 107 | + } catch (Exception e) { |
| 108 | + throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); |
| 109 | + } |
| 110 | + |
| 111 | + ChartQueryParam chartQueryParam = new ChartQueryParam(); |
| 112 | + chartQueryParam.setId(1L); |
| 113 | + chartQueryParam.setUserId(1L); |
| 114 | + |
| 115 | + DataResult<Chart> chartDataResult = chartService.queryExistent(chartQueryParam); |
| 116 | + DataResult<Chart> queryExistent = chartService.queryExistent(chartDataResult.getData().getId()); |
| 117 | + assertNotNull(chartDataResult); |
| 118 | + assertEquals(chartDataResult, queryExistent); |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testListQuery() { |
| 124 | + try { |
| 125 | + userLoginIdentity(false, 8L); |
| 126 | + userLoginIdentity(true, 10L); |
| 127 | + } catch (Exception e) { |
| 128 | + throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); |
| 129 | + } |
| 130 | + |
| 131 | + ChartListQueryParam param = new ChartListQueryParam(); |
| 132 | + param.setIdList(Arrays.asList(4L, 5L, 6L)); |
| 133 | + param.setUserId(1L); |
| 134 | + |
| 135 | + ListResult<Chart> listQuery = chartService.listQuery(param); |
| 136 | + assertNotNull(listQuery); |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testQueryByIds() { |
| 143 | + try { |
| 144 | + userLoginIdentity(false, 9L); |
| 145 | + userLoginIdentity(true, 11L); |
| 146 | + } catch (Exception e) { |
| 147 | + throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); |
| 148 | + } |
| 149 | + |
| 150 | + ListResult<Chart> chartListResult = chartService.queryByIds(Arrays.asList(1L, 2L, 3L)); |
| 151 | + assertNotNull(chartListResult); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void testDeleteWithPermission() { |
| 156 | + try { |
| 157 | + userLoginIdentity(false, 10L); |
| 158 | + userLoginIdentity(true, 12L); |
| 159 | + } catch (Exception e) { |
| 160 | + throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); |
| 161 | + } |
| 162 | + |
| 163 | + ActionResult actionResult = chartService.deleteWithPermission(3L); |
| 164 | + assertNotNull(actionResult); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Save the current user identity (administrator or normal user) and user ID to the context and database session for subsequent use. |
| 169 | + * |
| 170 | + * @param isAdmin |
| 171 | + * @param userId |
| 172 | + */ |
| 173 | + private static void userLoginIdentity(boolean isAdmin, Long userId) { |
| 174 | + Context context = Context.builder().loginUser( |
| 175 | + LoginUser.builder().admin(isAdmin).id(userId).build() |
| 176 | + ).build(); |
| 177 | + ContextUtils.setContext(context); |
| 178 | + Dbutils.setSession(); |
| 179 | + } |
| 180 | +} |
0 commit comments