|
| 1 | +package com.dataengine.datamanagement.application.service; |
| 2 | + |
| 3 | +import com.dataengine.datamanagement.application.service.DatasetApplicationService; |
| 4 | +import com.dataengine.datamanagement.domain.model.dataset.Dataset; |
| 5 | +import com.dataengine.datamanagement.domain.model.dataset.StatusConstants; |
| 6 | +import com.dataengine.datamanagement.domain.model.dataset.Tag; |
| 7 | +import com.dataengine.datamanagement.interfaces.dto.*; |
| 8 | +import com.dataengine.datamanagement.interfaces.rest.DatasetController; |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.DisplayName; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 14 | +import org.mockito.InjectMocks; |
| 15 | +import org.mockito.Mock; |
| 16 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 17 | +import org.springframework.data.domain.Page; |
| 18 | +import org.springframework.data.domain.PageImpl; |
| 19 | +import org.springframework.data.domain.PageRequest; |
| 20 | +import org.springframework.http.HttpStatus; |
| 21 | +import org.springframework.http.ResponseEntity; |
| 22 | + |
| 23 | +import java.time.LocalDateTime; |
| 24 | +import java.util.*; |
| 25 | + |
| 26 | +import static org.junit.jupiter.api.Assertions.*; |
| 27 | +import static org.mockito.ArgumentMatchers.*; |
| 28 | +import static org.mockito.Mockito.*; |
| 29 | + |
| 30 | +@ExtendWith(MockitoExtension.class) |
| 31 | +class DatasetControllerTest { |
| 32 | + |
| 33 | + @Mock |
| 34 | + private DatasetApplicationService datasetApplicationService; |
| 35 | + |
| 36 | + @InjectMocks |
| 37 | + private DatasetController controller; |
| 38 | + |
| 39 | + private Dataset sampleDataset; |
| 40 | + private Tag sampleTag; |
| 41 | + private ObjectMapper objectMapper; |
| 42 | + |
| 43 | + @BeforeEach |
| 44 | + void setUp() { |
| 45 | + objectMapper = new ObjectMapper(); |
| 46 | + |
| 47 | + sampleTag = new Tag(); |
| 48 | + sampleTag.setId("tag-id-1"); |
| 49 | + sampleTag.setName("test-tag"); |
| 50 | + sampleTag.setColor("#ff0000"); |
| 51 | + sampleTag.setDescription("Test tag"); |
| 52 | + sampleTag.setUsageCount(5L); |
| 53 | + |
| 54 | + sampleDataset = new Dataset(); |
| 55 | + sampleDataset.setId("dataset-id-1"); |
| 56 | + sampleDataset.setName("Test Dataset"); |
| 57 | + sampleDataset.setDescription("Test description"); |
| 58 | + sampleDataset.setDatasetType("CSV"); |
| 59 | + sampleDataset.setStatus(StatusConstants.DatasetStatuses.ACTIVE); |
| 60 | + sampleDataset.setDataSourceId(1L); |
| 61 | + sampleDataset.setPath("/test/path"); |
| 62 | + sampleDataset.setFileCount(10L); |
| 63 | + sampleDataset.setSizeBytes(1024L); |
| 64 | + sampleDataset.setCompletionRate(80.0); |
| 65 | + sampleDataset.setCreatedAt(LocalDateTime.now()); |
| 66 | + sampleDataset.setUpdatedAt(LocalDateTime.now()); |
| 67 | + sampleDataset.setCreatedBy("testuser"); |
| 68 | + sampleDataset.getTags().add(sampleTag); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + @DisplayName("datasetsGet: 正常分页查询数据集") |
| 73 | + void datasetsGet_success() { |
| 74 | + // Given |
| 75 | + List<Dataset> datasets = Arrays.asList(sampleDataset); |
| 76 | + Page<Dataset> page = new PageImpl<>(datasets, PageRequest.of(0, 20), 1); |
| 77 | + when(datasetApplicationService.getDatasets(eq("CSV"), eq("ACTIVE"), eq("test"), |
| 78 | + eq(Arrays.asList("tag1", "tag2")), any())).thenReturn(page); |
| 79 | + |
| 80 | + // When |
| 81 | + ResponseEntity<PagedDatasetResponse> response = controller.datasetsGet(0, 20, "CSV", |
| 82 | + "tag1,tag2", "test", "ACTIVE"); |
| 83 | + |
| 84 | + // Then |
| 85 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 86 | + assertNotNull(response.getBody()); |
| 87 | + assertEquals(1, response.getBody().getContent().size()); |
| 88 | + assertEquals(0, response.getBody().getPage()); |
| 89 | + assertEquals(20, response.getBody().getSize()); |
| 90 | + assertEquals(1, response.getBody().getTotalElements()); |
| 91 | + assertEquals(1, response.getBody().getTotalPages()); |
| 92 | + assertTrue(response.getBody().getFirst()); |
| 93 | + assertTrue(response.getBody().getLast()); |
| 94 | + |
| 95 | + DatasetResponse datasetResponse = response.getBody().getContent().get(0); |
| 96 | + assertEquals("dataset-id-1", datasetResponse.getId()); |
| 97 | + assertEquals("Test Dataset", datasetResponse.getName()); |
| 98 | + assertEquals("CSV", datasetResponse.getType().getCode()); |
| 99 | + assertEquals(1, datasetResponse.getTags().size()); |
| 100 | + assertEquals("test-tag", datasetResponse.getTags().get(0).getName()); |
| 101 | + |
| 102 | + verify(datasetApplicationService).getDatasets(eq("CSV"), eq("ACTIVE"), eq("test"), |
| 103 | + eq(Arrays.asList("tag1", "tag2")), any()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + @DisplayName("datasetsGet: 默认分页参数") |
| 108 | + void datasetsGet_defaultPaging() { |
| 109 | + Page<Dataset> emptyPage = new PageImpl<>(Collections.emptyList(), PageRequest.of(0, 20), 0); |
| 110 | + when(datasetApplicationService.getDatasets(isNull(), isNull(), isNull(), isNull(), any())) |
| 111 | + .thenReturn(emptyPage); |
| 112 | + |
| 113 | + ResponseEntity<PagedDatasetResponse> response = controller.datasetsGet(null, null, null, |
| 114 | + null, null, null); |
| 115 | + |
| 116 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 117 | + assertTrue(response.getBody().getContent().isEmpty()); |
| 118 | + verify(datasetApplicationService).getDatasets(isNull(), isNull(), isNull(), isNull(), |
| 119 | + argThat(pageable -> pageable.getPageNumber() == 0 && pageable.getPageSize() == 20)); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + @DisplayName("datasetsGet: 标签参数解析") |
| 124 | + void datasetsGet_tagsProcessing() { |
| 125 | + Page<Dataset> page = new PageImpl<>(Collections.emptyList(), PageRequest.of(0, 20), 0); |
| 126 | + when(datasetApplicationService.getDatasets(isNull(), isNull(), isNull(), any(), any())) |
| 127 | + .thenReturn(page); |
| 128 | + |
| 129 | + // 测试空标签 |
| 130 | + controller.datasetsGet(null, null, null, "", null, null); |
| 131 | + verify(datasetApplicationService).getDatasets(isNull(), isNull(), isNull(), isNull(), any()); |
| 132 | + |
| 133 | + // 测试空白标签 |
| 134 | + controller.datasetsGet(null, null, null, " ", null, null); |
| 135 | + verify(datasetApplicationService, times(2)).getDatasets(isNull(), isNull(), isNull(), isNull(), any()); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + @DisplayName("datasetsPost: 正常创建数据集") |
| 140 | + void datasetsPost_success() { |
| 141 | + CreateDatasetRequest request = new CreateDatasetRequest(); |
| 142 | + request.setName("New Dataset"); |
| 143 | + request.setDescription("New description"); |
| 144 | + request.setType("JSON"); |
| 145 | + request.setTags(Arrays.asList("tag1", "tag2")); |
| 146 | + request.setDataSource("123"); |
| 147 | + request.setTargetLocation("/new/path"); |
| 148 | + |
| 149 | + when(datasetApplicationService.createDataset(eq("New Dataset"), eq("New description"), |
| 150 | + eq("JSON"), eq(Arrays.asList("tag1", "tag2")), eq(123L), eq("/new/path"), |
| 151 | + isNull(), eq("system"))).thenReturn(sampleDataset); |
| 152 | + |
| 153 | + ResponseEntity<DatasetResponse> response = controller.datasetsPost(request); |
| 154 | + |
| 155 | + assertEquals(HttpStatus.CREATED, response.getStatusCode()); |
| 156 | + assertNotNull(response.getBody()); |
| 157 | + assertEquals("dataset-id-1", response.getBody().getId()); |
| 158 | + verify(datasetApplicationService).createDataset(eq("New Dataset"), eq("New description"), |
| 159 | + eq("JSON"), eq(Arrays.asList("tag1", "tag2")), eq(123L), eq("/new/path"), |
| 160 | + isNull(), eq("system")); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + @DisplayName("datasetsPost: 数据源ID转换异常时传入null") |
| 165 | + void datasetsPost_invalidDataSourceId() { |
| 166 | + CreateDatasetRequest request = new CreateDatasetRequest(); |
| 167 | + request.setName("New Dataset"); |
| 168 | + request.setDataSource("invalid-id"); |
| 169 | + |
| 170 | + when(datasetApplicationService.createDataset(eq("New Dataset"), isNull(), |
| 171 | + isNull(), isNull(), isNull(), isNull(), isNull(), eq("system"))) |
| 172 | + .thenReturn(sampleDataset); |
| 173 | + |
| 174 | + ResponseEntity<DatasetResponse> response = controller.datasetsPost(request); |
| 175 | + |
| 176 | + assertEquals(HttpStatus.CREATED, response.getStatusCode()); |
| 177 | + verify(datasetApplicationService).createDataset(eq("New Dataset"), isNull(), |
| 178 | + isNull(), isNull(), isNull(), isNull(), isNull(), eq("system")); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + @DisplayName("datasetsPost: 服务抛异常时返回400") |
| 183 | + void datasetsPost_serviceException() { |
| 184 | + CreateDatasetRequest request = new CreateDatasetRequest(); |
| 185 | + request.setName("Duplicate Dataset"); |
| 186 | + |
| 187 | + when(datasetApplicationService.createDataset(anyString(), any(), any(), any(), |
| 188 | + any(), any(), any(), anyString())).thenThrow(new IllegalArgumentException("Already exists")); |
| 189 | + |
| 190 | + ResponseEntity<DatasetResponse> response = controller.datasetsPost(request); |
| 191 | + |
| 192 | + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); |
| 193 | + assertNull(response.getBody()); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + @DisplayName("datasetsDatasetIdGet: 正常获取数据集详情") |
| 198 | + void datasetsDatasetIdGet_success() { |
| 199 | + when(datasetApplicationService.getDataset("dataset-id-1")).thenReturn(sampleDataset); |
| 200 | + |
| 201 | + ResponseEntity<DatasetResponse> response = controller.datasetsDatasetIdGet("dataset-id-1"); |
| 202 | + |
| 203 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 204 | + assertNotNull(response.getBody()); |
| 205 | + assertEquals("dataset-id-1", response.getBody().getId()); |
| 206 | + assertEquals("Test Dataset", response.getBody().getName()); |
| 207 | + assertEquals("1", response.getBody().getDataSource()); |
| 208 | + assertEquals("/test/path", response.getBody().getTargetLocation()); |
| 209 | + assertEquals(10, response.getBody().getFileCount()); |
| 210 | + assertEquals(1024L, response.getBody().getTotalSize()); |
| 211 | + assertEquals(80.0f, response.getBody().getCompletionRate()); |
| 212 | + assertEquals("testuser", response.getBody().getCreatedBy()); |
| 213 | + |
| 214 | + verify(datasetApplicationService).getDataset("dataset-id-1"); |
| 215 | + } |
| 216 | + |
| 217 | + @Test |
| 218 | + @DisplayName("datasetsDatasetIdGet: 数据集不存在时返回404") |
| 219 | + void datasetsDatasetIdGet_notFound() { |
| 220 | + when(datasetApplicationService.getDataset("not-exist")) |
| 221 | + .thenThrow(new IllegalArgumentException("Dataset not found")); |
| 222 | + |
| 223 | + ResponseEntity<DatasetResponse> response = controller.datasetsDatasetIdGet("not-exist"); |
| 224 | + |
| 225 | + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); |
| 226 | + assertNull(response.getBody()); |
| 227 | + } |
| 228 | + |
| 229 | + |
| 230 | + @Test |
| 231 | + @DisplayName("datasetsDatasetIdPut: 状态为null时传入null") |
| 232 | + void datasetsDatasetIdPut_nullStatus() { |
| 233 | + UpdateDatasetRequest request = new UpdateDatasetRequest(); |
| 234 | + request.setName("Updated Name"); |
| 235 | + request.setStatus(null); |
| 236 | + |
| 237 | + when(datasetApplicationService.updateDataset(eq("dataset-id-1"), eq("Updated Name"), |
| 238 | + isNull(), isNull(), isNull())).thenReturn(sampleDataset); |
| 239 | + |
| 240 | + ResponseEntity<DatasetResponse> response = controller.datasetsDatasetIdPut("dataset-id-1", request); |
| 241 | + |
| 242 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 243 | + verify(datasetApplicationService).updateDataset(eq("dataset-id-1"), eq("Updated Name"), |
| 244 | + isNull(), isNull(), isNull()); |
| 245 | + } |
| 246 | + |
| 247 | + @Test |
| 248 | + @DisplayName("datasetsDatasetIdPut: 数据集不存在时返回404") |
| 249 | + void datasetsDatasetIdPut_notFound() { |
| 250 | + UpdateDatasetRequest request = new UpdateDatasetRequest(); |
| 251 | + request.setName("Updated Name"); |
| 252 | + |
| 253 | + when(datasetApplicationService.updateDataset(anyString(), anyString(), any(), any(), any())) |
| 254 | + .thenThrow(new IllegalArgumentException("Dataset not found")); |
| 255 | + |
| 256 | + ResponseEntity<DatasetResponse> response = controller.datasetsDatasetIdPut("not-exist", request); |
| 257 | + |
| 258 | + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); |
| 259 | + assertNull(response.getBody()); |
| 260 | + } |
| 261 | + |
| 262 | + @Test |
| 263 | + @DisplayName("datasetsDatasetIdDelete: 正常删除数据集") |
| 264 | + void datasetsDatasetIdDelete_success() { |
| 265 | + doNothing().when(datasetApplicationService).deleteDataset("dataset-id-1"); |
| 266 | + |
| 267 | + ResponseEntity<Void> response = controller.datasetsDatasetIdDelete("dataset-id-1"); |
| 268 | + |
| 269 | + assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); |
| 270 | + verify(datasetApplicationService).deleteDataset("dataset-id-1"); |
| 271 | + } |
| 272 | + |
| 273 | + @Test |
| 274 | + @DisplayName("datasetsDatasetIdDelete: 数据集不存在时返回404") |
| 275 | + void datasetsDatasetIdDelete_notFound() { |
| 276 | + doThrow(new IllegalArgumentException("Dataset not found")) |
| 277 | + .when(datasetApplicationService).deleteDataset("not-exist"); |
| 278 | + |
| 279 | + ResponseEntity<Void> response = controller.datasetsDatasetIdDelete("not-exist"); |
| 280 | + |
| 281 | + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); |
| 282 | + } |
| 283 | + |
| 284 | + @Test |
| 285 | + @DisplayName("datasetsDatasetIdStatisticsGet: 正常获取统计信息") |
| 286 | + void datasetsDatasetIdStatisticsGet_success() { |
| 287 | + Map<String, Object> stats = new HashMap<>(); |
| 288 | + stats.put("totalFiles", 100); |
| 289 | + stats.put("completedFiles", 80); |
| 290 | + stats.put("totalSize", 2048L); |
| 291 | + stats.put("completionRate", 80.0f); |
| 292 | + stats.put("fileTypeDistribution", Map.of("csv", 60, "json", 40)); |
| 293 | + stats.put("statusDistribution", Map.of("COMPLETED", 80, "PROCESSING", 20)); |
| 294 | + |
| 295 | + when(datasetApplicationService.getDatasetStatistics("dataset-id-1")).thenReturn(stats); |
| 296 | + |
| 297 | + ResponseEntity<DatasetStatisticsResponse> response = controller.datasetsDatasetIdStatisticsGet("dataset-id-1"); |
| 298 | + |
| 299 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 300 | + assertNotNull(response.getBody()); |
| 301 | + assertEquals(100, response.getBody().getTotalFiles()); |
| 302 | + assertEquals(80, response.getBody().getCompletedFiles()); |
| 303 | + assertEquals(2048L, response.getBody().getTotalSize()); |
| 304 | + assertEquals(80.0f, response.getBody().getCompletionRate()); |
| 305 | + assertNotNull(response.getBody().getFileTypeDistribution()); |
| 306 | + assertNotNull(response.getBody().getStatusDistribution()); |
| 307 | + |
| 308 | + verify(datasetApplicationService).getDatasetStatistics("dataset-id-1"); |
| 309 | + } |
| 310 | + |
| 311 | + @Test |
| 312 | + @DisplayName("datasetsDatasetIdStatisticsGet: 数据集不存在时返回404") |
| 313 | + void datasetsDatasetIdStatisticsGet_notFound() { |
| 314 | + when(datasetApplicationService.getDatasetStatistics("not-exist")) |
| 315 | + .thenThrow(new IllegalArgumentException("Dataset not found")); |
| 316 | + |
| 317 | + ResponseEntity<DatasetStatisticsResponse> response = controller.datasetsDatasetIdStatisticsGet("not-exist"); |
| 318 | + |
| 319 | + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); |
| 320 | + assertNull(response.getBody()); |
| 321 | + } |
| 322 | + |
| 323 | + @Test |
| 324 | + @DisplayName("datasetsDatasetIdStatisticsGet: 其他异常时返回500") |
| 325 | + void datasetsDatasetIdStatisticsGet_internalError() { |
| 326 | + when(datasetApplicationService.getDatasetStatistics("dataset-id-1")) |
| 327 | + .thenThrow(new RuntimeException("Internal error")); |
| 328 | + |
| 329 | + ResponseEntity<DatasetStatisticsResponse> response = controller.datasetsDatasetIdStatisticsGet("dataset-id-1"); |
| 330 | + |
| 331 | + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); |
| 332 | + assertNull(response.getBody()); |
| 333 | + } |
| 334 | + |
| 335 | +} |
0 commit comments