package com.sinata.appointment.service; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import com.sinata.appointment.dto.ExportTaskStatusVo; import java.nio.file.Files; import java.nio.file.Path; import java.util.Map; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.ObjectProvider; @ExtendWith(MockitoExtension.class) class ExportApplicationServiceTest { @Mock private ObjectProvider rabbitTemplateProvider; @Mock private AppointmentApplicationService appointmentApplicationService; @Mock private LocalExportAsyncService localExportAsyncService; @InjectMocks private ExportApplicationService exportApplicationService; @TempDir Path tempDir; @Test void enqueueExportWithoutRabbitStartsLocalAsyncAndQueuesTask() { when(rabbitTemplateProvider.getIfAvailable()).thenReturn(null); String taskId = exportApplicationService.enqueueExport("appointment", Map.of("status", "PENDING_APPROVAL")); assertNotNull(taskId); ExportTaskStatusVo status = exportApplicationService.getTaskStatus(taskId); assertEquals("QUEUED", status.getStatus()); verify(localExportAsyncService).process(taskId, "appointment", Map.of("status", "PENDING_APPROVAL")); } @Test void processLocalExportCreatesFileAndMarksCompleted() throws Exception { String oldTmpDir = System.getProperty("java.io.tmpdir"); System.setProperty("java.io.tmpdir", tempDir.toString()); try { when(appointmentApplicationService.exportExcel(null, null, null, null, null, null, null, null, null, null)) .thenReturn("xlsx-content".getBytes()); String taskId = exportApplicationService.enqueueExport("appointment", Map.of()); exportApplicationService.processLocalExport(taskId, "appointment", Map.of()); ExportTaskStatusVo status = exportApplicationService.getTaskStatus(taskId); assertEquals("COMPLETED", status.getStatus()); Path filePath = exportApplicationService.getExportFilePath(taskId); assertNotNull(filePath); assertTrue(Files.exists(filePath)); assertArrayEquals("xlsx-content".getBytes(), Files.readAllBytes(filePath)); } finally { System.setProperty("java.io.tmpdir", oldTmpDir); } } @Test void processLocalExportMarksFailedForUnsupportedType() { when(rabbitTemplateProvider.getIfAvailable()).thenReturn(null); String taskId = exportApplicationService.enqueueExport("other", Map.of()); exportApplicationService.processLocalExport(taskId, "other", Map.of()); ExportTaskStatusVo status = exportApplicationService.getTaskStatus(taskId); assertEquals("FAILED", status.getStatus()); assertTrue(status.getErrorMessage().contains("unsupported export type")); assertNull(exportApplicationService.getExportFilePath(taskId)); } }