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));
|
}
|
|
}
|