Skip to content

Commit

Permalink
feat!: remove WeasyPrint CLI support
Browse files Browse the repository at this point in the history
Refs: #129
  • Loading branch information
grigoriev committed Aug 7, 2024
1 parent 1f3da30 commit 70564f9
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 124 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>ch.sbb.polarion.extensions</groupId>
<artifactId>ch.sbb.polarion.extension.generic</artifactId>
<version>6.5.2</version>
<version>6.6.0</version>
</parent>

<artifactId>ch.sbb.polarion.extension.pdf-exporter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.polarion.core.util.logging.Logger;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
Expand All @@ -31,6 +33,17 @@ public class WeasyPrintServiceConnector implements WeasyPrintConverter {
private static final AtomicReference<String> weasyPrintVersion = new AtomicReference<>();
private static final AtomicReference<String> weasyPrintServiceVersion = new AtomicReference<>();

@Getter
private final @NotNull String weasyPrintServiceBaseUrl;

public WeasyPrintServiceConnector() {
this(PdfExporterExtensionConfiguration.getInstance().getWeasyprintService());
}

public WeasyPrintServiceConnector(@NotNull String weasyPrintServiceBaseUrl) {
this.weasyPrintServiceBaseUrl = weasyPrintServiceBaseUrl;
}

@Override
public byte[] convertToPdf(String htmlPage, WeasyPrintOptions weasyPrintOptions) {
Client client = null;
Expand Down Expand Up @@ -91,10 +104,6 @@ public WeasyPrintInfo getWeasyPrintInfo() {
}
}

private String getWeasyPrintServiceBaseUrl() {
return PdfExporterExtensionConfiguration.getInstance().getWeasyprintService();
}

private void logWeasyPrintVersionFromHeader(Response response) {
String actualPythonVersion = response.getHeaderString(PYTHON_VERSION_HEADER);
String actualWeasyPrintVersion = response.getHeaderString(WEASYPRINT_VERSION_HEADER);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package ch.sbb.polarion.extension.pdf.exporter.weasyprint;

import ch.sbb.polarion.extension.pdf.exporter.util.MediaUtils;
import ch.sbb.polarion.extension.pdf.exporter.weasyprint.exporter.WeasyPrintExporter;
import ch.sbb.polarion.extension.pdf.exporter.weasyprint.service.WeasyPrintServiceConnector;
import com.polarion.core.util.StringUtils;
import lombok.SneakyThrows;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;

import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
Expand All @@ -17,9 +19,13 @@
import java.util.Base64;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertTrue;

@SkipTestWhenParamNotSet
public abstract class BaseWeasyPrintTest {

public static final String DOCKER_IMAGE_NAME = "ghcr.io/schweizerischebundesbahnen/weasyprint-service:latest";

public static final String IMPL_NAME_PARAM = "wpExporterImpl";
public static final String PAGE_SUFFIX = "_page_";
public static final String WEASYPRINT_TEST_RESOURCES_FOLDER = "/weasyprint/html/";
Expand All @@ -29,12 +35,14 @@ public abstract class BaseWeasyPrintTest {
public static final String FONT_BASE64_REPLACE_PARAM = "{FONT_BASE64}";
public static final String CSS_BASIC = "basic";
public static final String FONT_REGULAR = "OpenSans-Regular";

protected static final String REPORTS_FOLDER_PATH = "target/surefire-reports/";
protected static final String EXT_HTML = ".html";
protected static final String EXT_PNG = ".png";
protected static final String EXT_PDF = ".pdf";
protected static final String EXT_CSS = ".css";
protected static final String EXT_WOFF = ".woff";

private static final Logger logger = LoggerFactory.getLogger(BaseWeasyPrintTest.class);

@SneakyThrows
Expand Down Expand Up @@ -66,7 +74,18 @@ public static byte[] readFontResource(String resourceName) {
}

protected byte[] exportToPdf(String html, @NotNull WeasyPrintOptions weasyPrintOptions) {
return WeasyPrintExporter.IMPL_REGISTRY.get(System.getProperty(IMPL_NAME_PARAM).toLowerCase()).exportToPdf(html, weasyPrintOptions);
try (GenericContainer<?> weasyPrintService = new GenericContainer<>(DOCKER_IMAGE_NAME)) {
weasyPrintService
.withExposedPorts(9080)
.waitingFor(Wait.forHttp("/version").forPort(9080))
.start();

assertTrue(weasyPrintService.isRunning());

String weasyPrintServiceBaseUrl = "http://" + weasyPrintService.getHost() + ":" + weasyPrintService.getFirstMappedPort();
WeasyPrintServiceConnector weasyPrintServiceConnector = new WeasyPrintServiceConnector(weasyPrintServiceBaseUrl);
return weasyPrintServiceConnector.convertToPdf(html, weasyPrintOptions);
}
}

protected List<BufferedImage> exportAndGetAsImages(String fileName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ch.sbb.polarion.extension.pdf.exporter.weasyprint;

import ch.sbb.polarion.extension.pdf.exporter.weasyprint.exporter.WeasyPrintExporter;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
Expand All @@ -12,21 +11,18 @@
*/
public class ParamSetCondition implements ExecutionCondition {

public static final String DOCKER = "docker";

private static final Logger logger = LoggerFactory.getLogger(ParamSetCondition.class);

@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext extensionContext) {
String implValue = System.getProperty(BaseWeasyPrintTest.IMPL_NAME_PARAM);
if (implValue == null) {
if (DOCKER.equalsIgnoreCase(implValue)) {
return ConditionEvaluationResult.enabled("ok");
} else {
logger.info("Param {} doesn't set, skipping weasyprint test", BaseWeasyPrintTest.IMPL_NAME_PARAM);
return ConditionEvaluationResult.disabled("required param doesn't exist");
}
WeasyPrintExporter exporter = WeasyPrintExporter.IMPL_REGISTRY.get(implValue.toLowerCase());
if (exporter == null) {
logger.info("Param {} contains unsupported value '{}', skipping weasyprint test", BaseWeasyPrintTest.IMPL_NAME_PARAM, implValue);
return ConditionEvaluationResult.disabled("unsupported param");
} else {
return ConditionEvaluationResult.enabled("ok");
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

20 changes: 0 additions & 20 deletions src/test/resources/weasyprint/Dockerfile

This file was deleted.

0 comments on commit 70564f9

Please sign in to comment.