云
2 小时以前 a7cf1913c376b53f487f0ef8dfff084c5bc010a7
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
package cn.iocoder.yudao.module.mes.service.pro.qrexport;
 
import cn.iocoder.yudao.module.mes.dal.dataobject.pro.qrexport.MesProQrExportTaskDO;
import cn.iocoder.yudao.module.mes.dal.mysql.pro.qrexport.MesProQrExportTaskMapper;
import cn.iocoder.yudao.module.mes.enums.pro.MesProQrExportTaskStatusEnum;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.util.ReflectionTestUtils;
 
import java.nio.file.Files;
import java.nio.file.Path;
import java.lang.reflect.Proxy;
 
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
 
class MesProQrExportTaskDownloadTest {
 
    @TempDir
    Path tempDir;
 
    @Test
    void rangeDownloadReturnsExactBytesAndKeepsFileForRetry() throws Exception {
        byte[] content = "0123456789".getBytes();
        Path file = tempDir.resolve("export.zip");
        Files.write(file, content);
        MesProQrExportTaskDO task = MesProQrExportTaskDO.builder()
                .id(13L).taskName("test").status(MesProQrExportTaskStatusEnum.FINISHED.getStatus())
                .filePath(file.toString()).build();
        MesProQrExportTaskMapper mapper = (MesProQrExportTaskMapper) Proxy.newProxyInstance(
                getClass().getClassLoader(), new Class[]{MesProQrExportTaskMapper.class},
                (proxy, method, args) -> {
                    if (method.getName().equals("selectById")) {
                        return task;
                    }
                    throw new UnsupportedOperationException(method.getName());
                });
        MesProQrExportTaskServiceImpl service = new MesProQrExportTaskServiceImpl();
        ReflectionTestUtils.setField(service, "taskMapper", mapper);
 
        MockHttpServletResponse partial = new MockHttpServletResponse();
        service.download(13L, "bytes=3-6", partial);
        assertEquals(206, partial.getStatus());
        assertEquals("bytes 3-6/10", partial.getHeader("Content-Range"));
        assertEquals("4", partial.getHeader("Content-Length"));
        assertArrayEquals("3456".getBytes(), partial.getContentAsByteArray());
 
        MockHttpServletResponse invalid = new MockHttpServletResponse();
        service.download(13L, "bytes=10-11", invalid);
        assertEquals(416, invalid.getStatus());
        assertEquals("bytes */10", invalid.getHeader("Content-Range"));
 
        MockHttpServletResponse full = new MockHttpServletResponse();
        service.download(13L, null, full);
        assertEquals(200, full.getStatus());
        assertArrayEquals(content, full.getContentAsByteArray());
        assertTrue(Files.exists(file));
    }
 
}