package de.superx.bianalysis; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.Base64; import java.util.Enumeration; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import org.jdom2.Document; import org.jdom2.input.SAXBuilder; import org.jdom2.output.XMLOutputter; import org.junit.Before; import org.junit.After; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import com.fasterxml.jackson.databind.ObjectMapper; import de.superx.BIATestUtils; import de.superx.BaseDbTest; import de.superx.bianalysis.jasper.JasperReportJson; import de.superx.bianalysis.jasper.JasperReportJsonWrapper; import de.superx.bianalysis.models.Right; import de.superx.bianalysis.models.RightParam; import de.superx.bianalysis.rest.BiAnalysisApi; import de.superx.bianalysis.service.DbMetaAdapter; import de.superx.bianalysis.service.JasperReportExport; import de.superx.rest.model.Download; public class JasperTest extends BaseDbTest{ @Autowired DbMetaAdapter dbMetaAdapter; @Autowired BiAnalysisApi biAnalysis; @Autowired JasperReportExport jasperReportExport; JasperReportJson jrj = new JasperReportJson(); private static String TEST_TEMP_FOLDER = String.join(File.separator, new String[] {"test", "tmp"}); private static String TEMP_BIANYLSIS_JSON = String.join(File.separator, new String[] {TEST_TEMP_FOLDER, "biAnalysis.json"}); private static String TEMP_JASPER_REPORT = String.join(File.separator, new String[] {TEST_TEMP_FOLDER, "MainReport.jrxml"}); private static String JASPER_JSON = String.join(File.separator, new String[] {BIATestUtils.RW_TEST_PATH, "result_jasper", "jasper" + ".json"}); private static String MAIN_REPORT = String.join(File.separator, new String[] {BIATestUtils.RW_TEST_PATH, "result_jasper", "MainReport" + ".jrxml"}); private static String DADATA_ADAPTER_REST_0 = String.join(File.separator, new String[] {BIATestUtils.RW_TEST_PATH, "result_jasper", "DataAdapterRest0" + ".jrdax"}); private static String DADATA_ADAPTER_REST_1 = String.join(File.separator, new String[] {BIATestUtils.RW_TEST_PATH, "result_jasper", "DataAdapterRest1" + ".jrdax"}); @Before public void init() { user.setHisInOneOrgUnitLidOfRole(Integer.valueOf(2)); setRightParamValue(Right.CREATE_ANALYSIS, RightParam.TOPIC_AREA, "130"); setRightParamValue(Right.DELETE_ANALYSIS, RightParam.TOPIC_AREA, "130"); } @After public void deleteTmpFolder() { deleteDir(new File(TEST_TEMP_FOLDER)); } @Test public void testInitJasperReportJson() throws Exception { String file = "storedReport"; StoredReport storedReportFromFile = BIATestUtils.readStoredReportFromJson(file); int storedReportId = biAnalysis.persistReportDefinition(storedReportFromFile); JasperReportJsonWrapper jasperReportJsonWrapper = biAnalysis.getJasperJsonDatasource(storedReportId); ObjectMapper mapper = new ObjectMapper(); String json = normalizeJsonString(mapper.writeValueAsString(jasperReportJsonWrapper)); String expectedJson = normalizeJsonString(mapper.readTree(Files.readAllBytes(new File(JASPER_JSON).toPath())).toString()); boolean reportDeleted = biAnalysis.deleteReportDefinition(storedReportId); assertEquals(expectedJson, json); assertTrue(reportDeleted); } @Test public void testExportJasperReport() throws Exception { StoredReport storedReportFromFile = BIATestUtils.readStoredReportFromJson("storedReport"); int storedReportId = biAnalysis.persistReportDefinition(storedReportFromFile); Download download = biAnalysis.getJasperTemplate(storedReportId); // unpack zip byte[] bytes = Base64.getDecoder().decode(download.base64String); File zipFile = File.createTempFile("temp", ".zip"); try (FileOutputStream fos = new FileOutputStream(zipFile)) { fos.write(bytes); } ZipFile zip = new ZipFile(zipFile); Enumeration entries = zip.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); File file2 = new File(String.join(File.separator, new String[] {TEST_TEMP_FOLDER, entry.getName()})); file2.getParentFile().mkdirs(); try (InputStream is = zip.getInputStream(entry); FileOutputStream fos = new FileOutputStream(file2)) { byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) != -1) { fos.write(buffer, 0, len); } } } // compare Json-File File expectedJsonFile = new File(JASPER_JSON); File actualJsonFile = new File(TEMP_BIANYLSIS_JSON); ObjectMapper mapper = new ObjectMapper(); String actualJson = normalizeJsonString(mapper.readTree(Files.readAllBytes(actualJsonFile.toPath())).toString()); String expectedJson = normalizeJsonString(mapper.readTree(Files.readAllBytes(expectedJsonFile.toPath())).toString()); assertEquals(expectedJson, actualJson); // compare JasperReport-File File expectedMainReport = new File(MAIN_REPORT); File actualMainReport = new File(TEMP_JASPER_REPORT); SAXBuilder saxBuilder = new SAXBuilder(); Document expectedReport = saxBuilder.build(expectedMainReport); Document actualReport = saxBuilder.build(actualMainReport); XMLOutputter outputter = new XMLOutputter(); String expectedReportXML = outputter.outputString(expectedReport); String actualReportXML = outputter.outputString(actualReport); assertEquals(expectedReportXML, actualReportXML); assertTrue(biAnalysis.deleteReportDefinition(storedReportId)); } @Test public void testDataAdapterRest() throws IOException { jasperReportExport.setJsonRestAdapter(0); Path path = new File(JasperReportExport.getPath(JasperReportExport.DATA_ADAPTER_REST)).toPath(); String dataAdapterContent = String.join("", Files.readAllLines(path)).replace("\r\n", "\n"); Path path0 = new File(DADATA_ADAPTER_REST_0).toPath(); String dataAdapterContent0 = String.join("", Files.readAllLines(path0)).replace("\r\n", "\n"); assertEquals(dataAdapterContent0, dataAdapterContent); jasperReportExport.setJsonRestAdapter(1); String dataAdapterContentNew = new String(Files.readAllBytes(new File(JasperReportExport.getPath(JasperReportExport.DATA_ADAPTER_REST)).toPath())).replace("\r\n", "\n"); String dataAdapterContent1 = new String(Files.readAllBytes(new File(DADATA_ADAPTER_REST_1).toPath())).replace("\r\n", "\n"); assertEquals(dataAdapterContent1, dataAdapterContentNew); } public static void deleteDir(File path) { if (path.isDirectory()) { File[] files = path.listFiles(); if (files != null) { for (File file : files) { deleteDir(file); // Rekursion für Unterordner } } } path.delete(); // Datei oder leeren Ordner löschen } public static String normalizeJsonString(String jsonString) { // Entferne alle Zahlen (einschließlich Dezimalstellen) aus dem JSON-String Pattern pattern = Pattern.compile("-?\\d+(\\.\\d+)?"); // Regex für Zahlen Matcher matcher = pattern.matcher(jsonString); return matcher.replaceAll(""); } }