longz
2026-04-21 c4db0a4526f61758272c92396aa788d84f098102
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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<RabbitTemplate> 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));
    }
}