package com.estate.expert.admin.service; import com.alibaba.fastjson.JSONObject; import com.deepoove.poi.XWPFTemplate; import com.deepoove.poi.config.Configure; import com.deepoove.poi.config.ConfigureBuilder; import com.estate.expert.admin.domain.report.ReportTemplateType; import com.estate.expert.admin.stat.report.builder.DeviceReportBuilder; import com.estate.expert.admin.stat.report.builder.SystemReportBuilder; import com.estate.expert.admin.stat.report.render.CheckInfoTablePolicy; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.util.Map; @Slf4j @SpringBootTest @ExtendWith(SpringExtension.class) @Transactional public class ReportBuilderTest { @Resource UserFileService userFileService; @Test public void testDoc() throws IOException { // load file from resource var resource = getClass().getClassLoader().getResourceAsStream("templates/test.docx"); ConfigureBuilder builder = Configure.builder(); builder.bind("detail_table", new CheckInfoTablePolicy()); var data = JSONObject.parseObject(Files.readString(new File("/Volumes/Works/tmp/system_report.json").toPath())); var checkInfos = data.getJSONObject("评估结果").getJSONObject("安全检查").getJSONArray("分项评估"); XWPFTemplate template = XWPFTemplate.compile(resource, builder.build()).render( Map.of("detail_table", checkInfos) ); template.writeAndClose(new FileOutputStream("/Volumes/Works/tmp/output.docx")); } @Test public void testSystemReport() throws IOException { var data = JSONObject.parseObject(Files.readString(new File("/Volumes/Works/tmp/system_report.json").toPath())); XWPFTemplate template = new SystemReportBuilder(userFileService).buildReport(data, ReportTemplateType.ThirdPartyReport.getConfig());; template.writeAndClose(new FileOutputStream("/Volumes/Works/tmp/system_report.docx")); } @Test public void testDeviceReport() throws IOException { var data = JSONObject.parseObject(Files.readString(new File("/Volumes/Works/tmp/device_report.json").toPath())); XWPFTemplate template = new DeviceReportBuilder(userFileService).buildReport(data, null); template.writeAndClose(new FileOutputStream("/Volumes/Works/tmp/device_report.docx")); } @Test public void testEmptyDeviceReport() throws IOException { XWPFTemplate template = new DeviceReportBuilder(userFileService).buildReport(new JSONObject(), null); template.writeAndClose(new FileOutputStream("/Volumes/Works/tmp/empty_device_report.docx")); } }