Skip to content

Commit

Permalink
WIP - testing image inclusion in reports
Browse files Browse the repository at this point in the history
Signed-off-by:Nathan Erwin <nathan.d.erwin@gmail.com>
  • Loading branch information
nderwin committed Oct 16, 2024
1 parent 004bbf5 commit e777077
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ pom.xml.releaseBackup
pom.xml.versionsBackup
release.properties
ObjectStore
/integration-tests/nbproject/
26 changes: 26 additions & 0 deletions integration-tests/src/main/jasperreports/ImagesReport.jrxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!-- Created with Jaspersoft Studio version 7.0.0.final using JasperReports Library version 7.0.0 -->
<jasperReport name="ImagesReport" language="java" pageWidth="612" pageHeight="792" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="7ed005ff-df0a-4cda-8daa-ffa0721c8e4f">
<title height="530" splitType="Stretch">
<element kind="staticText" uuid="465ce796-bd9a-432d-b0fe-70672e692e72" x="0" y="0" width="572" height="30" hTextAlign="Center" vTextAlign="Middle">
<text><![CDATA[Images]]></text>
</element>
<element kind="staticText" uuid="fa677fd0-4085-4b99-9dab-2262569dfb15" x="0" y="40" width="280" height="50" hTextAlign="Center" vTextAlign="Middle">
<text><![CDATA[Relative to report]]></text>
</element>
<element kind="image" uuid="63ad371f-62a0-4324-bae6-4088e6b359bd" x="280" y="40" width="50" height="50" hImageAlign="Center" vImageAlign="Middle">
<expression><![CDATA["images/jasperreports.svg"]]></expression>
</element>
<element kind="staticText" uuid="ae851b5b-8475-43bb-9d82-9e2d020c7d41" x="0" y="100" width="280" height="50" hTextAlign="Center" vTextAlign="Middle">
<text><![CDATA[URL to localhost, gif]]></text>
</element>
<element kind="image" uuid="72c0a16f-5b16-4a1d-9132-00387f80b179" x="280" y="100" width="50" height="50" hImageAlign="Center" vImageAlign="Middle">
<expression><![CDATA["http://localhost:8080/jasperreports.gif"]]></expression>
</element>
<element kind="staticText" uuid="f8eb2a65-ed08-4dd4-9d93-da923703a386" x="0" y="160" width="280" height="50" hTextAlign="Center" vTextAlign="Middle">
<text><![CDATA[URL to remote server]]></text>
</element>
<element kind="image" uuid="183403c6-1aef-410a-b6f1-5767241f28bf" x="280" y="160" width="50" height="50" hImageAlign="Center" vImageAlign="Middle">
<expression><![CDATA["https://raw.githubusercontent.com/quarkiverse/quarkus-jasperreports/refs/heads/main/docs/modules/ROOT/assets/images/jasperreports.svg"]]></expression>
</element>
</title>
</jasperReport>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.quarkiverse.jasperreports.it;

import static jakarta.ws.rs.core.MediaType.TEXT_HTML;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import org.eclipse.microprofile.openapi.annotations.media.Content;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.w3c.dom.Document;

import io.quarkiverse.jasperreports.repository.ReadOnlyStreamingService;
import io.quarkus.logging.Log;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.SimpleReportContext;
import net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.engine.util.JRXmlUtils;

@Path("jasper/image")
@ApplicationScoped
@Produces({ TEXT_HTML })
public class JasperReportsImageResource extends AbstractJasperResource {

private static final String TEST_REPORT_NAME = "ImagesReport.jasper";

@Inject
ReadOnlyStreamingService repo;

@APIResponse(responseCode = "200", description = "Fetch a report with images", content = @Content(mediaType = TEXT_HTML))
@GET
public Response get() throws JRException {
final Map<String, Object> params = new HashMap<>();
Document document = JRXmlUtils.parse(JRLoader.getLocationInputStream("data/northwind.xml"));
params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, document);
params.put(JRXPathQueryExecuterFactory.XML_DATE_PATTERN, "yyyy-MM-dd");
params.put(JRXPathQueryExecuterFactory.XML_NUMBER_PATTERN, "#,##0.##");
params.put(JRXPathQueryExecuterFactory.XML_LOCALE, Locale.ENGLISH);
params.put(JRParameter.REPORT_LOCALE, Locale.US);
params.put(JRParameter.REPORT_CONTEXT, new SimpleReportContext());

final long start = System.currentTimeMillis();
final JasperPrint jasperPrint = JasperFillManager.getInstance(repo.getContext()).fillFromRepo(TEST_REPORT_NAME, params);
final Response.ResponseBuilder response = Response.ok();
ByteArrayOutputStream outputStream = exportHtml(jasperPrint);
Log.infof("HTML creation time : %s", (System.currentTimeMillis() - start));
response.entity(outputStream.toByteArray());
response.type(MediaType.TEXT_HTML);

return response.build();
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions integration-tests/src/main/resources/images/jasperreports.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.quarkiverse.jasperreports.it;

import static io.restassured.RestAssured.given;
import static jakarta.ws.rs.core.MediaType.TEXT_HTML;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class JasperReportsImageResourceTest {

@Test
void testExportHTML() {
given()
.when().header("accept", TEXT_HTML).get("/jasper/image")
.then()
.assertThat()
.statusCode(200)
.body(notNullValue())
.and()
.body(not(emptyString()))
.and()
.body(not(containsString("<img src=\"\"")));
}

}

0 comments on commit e777077

Please sign in to comment.