Skip to content

Commit

Permalink
Merge branch 'main' into setup-sonarcloud
Browse files Browse the repository at this point in the history
  • Loading branch information
ariwk authored Oct 22, 2024
2 parents 4451ddd + 429a2db commit ee8a329
Show file tree
Hide file tree
Showing 7 changed files with 405 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ch.sbb.polarion.extension.pdf_exporter.util;

import ch.sbb.polarion.extension.pdf_exporter.util.configuration.WeasyPrintStatusProvider;
import lombok.experimental.UtilityClass;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@UtilityClass
public class VersionUtils {

public static @Nullable String getLatestCompatibleVersionWeasyPrintService() {
try (InputStream input = WeasyPrintStatusProvider.class.getClassLoader().getResourceAsStream("versions.properties")) {
if (input == null) {
return null;
}

Properties properties = new Properties();
properties.load(input);
return properties.getProperty("weasyprint-service.version");
} catch (IOException e) {
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ch.sbb.polarion.extension.pdf_exporter.util.configuration;

import ch.sbb.polarion.extension.generic.configuration.ConfigurationStatus;
import ch.sbb.polarion.extension.generic.configuration.ConfigurationStatusProvider;
import ch.sbb.polarion.extension.generic.configuration.Status;
import ch.sbb.polarion.extension.generic.util.Discoverable;
import ch.sbb.polarion.extension.pdf_exporter.converter.HtmlToPdfConverter;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.conversion.Orientation;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.conversion.PaperSize;
import org.jetbrains.annotations.NotNull;

@Discoverable
public class WeasyPrintProbeStatusProvider extends ConfigurationStatusProvider {

private static final String WEASY_PRINT_SERVICE_TEST_CONVERSION = "WeasyPrint Service: WeasyPrint probe";

private final HtmlToPdfConverter htmlToPdfConverter;

public WeasyPrintProbeStatusProvider() {
this.htmlToPdfConverter = new HtmlToPdfConverter();
}

public WeasyPrintProbeStatusProvider(HtmlToPdfConverter htmlToPdfConverter) {
this.htmlToPdfConverter = htmlToPdfConverter;
}

@Override
public @NotNull ConfigurationStatus getStatus(@NotNull Context context) {
try {
htmlToPdfConverter.convert("<html><body>test html</body></html>", Orientation.PORTRAIT, PaperSize.A4);

return new ConfigurationStatus(WEASY_PRINT_SERVICE_TEST_CONVERSION, Status.OK);
} catch (Exception e) {
return new ConfigurationStatus(WEASY_PRINT_SERVICE_TEST_CONVERSION, Status.ERROR, e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import ch.sbb.polarion.extension.generic.configuration.ConfigurationStatusProvider;
import ch.sbb.polarion.extension.generic.configuration.Status;
import ch.sbb.polarion.extension.generic.util.Discoverable;
import ch.sbb.polarion.extension.pdf_exporter.converter.HtmlToPdfConverter;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.conversion.Orientation;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.conversion.PaperSize;
import ch.sbb.polarion.extension.pdf_exporter.util.VersionUtils;
import ch.sbb.polarion.extension.pdf_exporter.weasyprint.service.WeasyPrintServiceConnector;
import ch.sbb.polarion.extension.pdf_exporter.weasyprint.service.model.WeasyPrintInfo;
import org.jetbrains.annotations.NotNull;
Expand All @@ -18,6 +16,16 @@
@Discoverable
public class WeasyPrintStatusProvider extends ConfigurationStatusProvider {

private final WeasyPrintServiceConnector weasyPrintServiceConnector;

public WeasyPrintStatusProvider() {
this.weasyPrintServiceConnector = new WeasyPrintServiceConnector();
}

public WeasyPrintStatusProvider(WeasyPrintServiceConnector weasyPrintServiceConnector) {
this.weasyPrintServiceConnector = weasyPrintServiceConnector;
}

private enum WeasyPrintServiceInfo {
VERSION,
PYTHON,
Expand All @@ -35,14 +43,9 @@ private enum WeasyPrintServiceInfo {
@Override
public @NotNull List<ConfigurationStatus> getStatuses(@NotNull Context context) {
try {
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
WeasyPrintServiceConnector weasyPrintServiceConnector = new WeasyPrintServiceConnector();

WeasyPrintInfo weasyPrintInfo = weasyPrintServiceConnector.getWeasyPrintInfo();
htmlToPdfConverter.convert("<html><body>test html</body></html>", Orientation.PORTRAIT, PaperSize.A4);

return List.of(
createWeasyPrintStatus(WEASY_PRINT_SERVICE_INFO.get(WeasyPrintServiceInfo.VERSION), weasyPrintInfo.getWeasyprintService() + " (" + weasyPrintInfo.getTimestamp() + ")"),
createWeasyPrintStatus(WEASY_PRINT_SERVICE_INFO.get(WeasyPrintServiceInfo.VERSION), weasyPrintInfo.getWeasyprintService(), weasyPrintInfo.getTimestamp(), VersionUtils.getLatestCompatibleVersionWeasyPrintService()),
createWeasyPrintStatus(WEASY_PRINT_SERVICE_INFO.get(WeasyPrintServiceInfo.PYTHON), weasyPrintInfo.getPython()),
createWeasyPrintStatus(WEASY_PRINT_SERVICE_INFO.get(WeasyPrintServiceInfo.WEASYPRINT), weasyPrintInfo.getWeasyprint()),
createWeasyPrintStatus(WEASY_PRINT_SERVICE_INFO.get(WeasyPrintServiceInfo.CHROMIUM), weasyPrintInfo.getChromium())
Expand All @@ -52,11 +55,34 @@ private enum WeasyPrintServiceInfo {
}
}

private static @NotNull ConfigurationStatus createWeasyPrintStatus(@NotNull String name, @Nullable String description) {
if (description == null || description.isBlank()) {
return new ConfigurationStatus(name, Status.WARNING, "Unknown");
private static @NotNull ConfigurationStatus createWeasyPrintStatus(@NotNull String name, @Nullable String version) {
if (version == null || version.isBlank()) {
return new ConfigurationStatus(name, Status.ERROR, "Unknown");
} else {
return new ConfigurationStatus(name, Status.OK, version);
}
}

private static @NotNull ConfigurationStatus createWeasyPrintStatus(@NotNull String name, @Nullable String version, @Nullable String timestamp, @Nullable String latestCompatibleVersion) {
if (version == null || version.isBlank()) {
return new ConfigurationStatus(name, Status.ERROR, createUseLatestCompatibleWeasyPrintMessage("Unknown", timestamp, latestCompatibleVersion));
} else if (!version.equals(latestCompatibleVersion)) {
return new ConfigurationStatus(name, Status.WARNING, createUseLatestCompatibleWeasyPrintMessage(version, timestamp, latestCompatibleVersion));
} else {
return new ConfigurationStatus(name, Status.OK, description);
return new ConfigurationStatus(name, Status.OK, version);
}
}

private static @NotNull String createUseLatestCompatibleWeasyPrintMessage(@NotNull String version, @Nullable String timestamp, @Nullable String latestCompatibleVersion) {
StringBuilder message = new StringBuilder();
message.append(version);
if (timestamp != null && !timestamp.isBlank()) {
message.append(" (").append(timestamp).append(")");
}
if (latestCompatibleVersion != null && !latestCompatibleVersion.isBlank()) {
message.append(": <span style='color: red;'>use latest compatible</span> <a href='https://github.com/SchweizerischeBundesbahnen/weasyprint-service/releases/tag/v").append(latestCompatibleVersion).append("' target='_blank'>").append(latestCompatibleVersion).append("</a>");
}
return message.toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package ch.sbb.polarion.extension.pdf_exporter.weasyprint.service.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.Nullable;

@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class WeasyPrintInfo {
private @Nullable String chromium;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/versions.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
weasyprint-service.version=62.4.6
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ch.sbb.polarion.extension.pdf_exporter.util.configuration;

import ch.sbb.polarion.extension.generic.configuration.ConfigurationStatus;
import ch.sbb.polarion.extension.generic.configuration.ConfigurationStatusProvider;
import ch.sbb.polarion.extension.generic.configuration.Status;
import ch.sbb.polarion.extension.pdf_exporter.converter.HtmlToPdfConverter;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.conversion.Orientation;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.conversion.PaperSize;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import javax.ws.rs.ProcessingException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith({MockitoExtension.class})
class WeasyPrintProbeStatusProviderTest {

@Test
void testHappyPath() {
HtmlToPdfConverter htmlToPdfConverter = mock(HtmlToPdfConverter.class);
when(htmlToPdfConverter.convert("<html><body>test html</body></html>", Orientation.PORTRAIT, PaperSize.A4)).thenReturn(new byte[0]);
WeasyPrintProbeStatusProvider weasyPrintProbeStatusProvider = new WeasyPrintProbeStatusProvider(htmlToPdfConverter);

ConfigurationStatus status = weasyPrintProbeStatusProvider.getStatus(ConfigurationStatusProvider.Context.builder().build());

assertEquals(new ConfigurationStatus("WeasyPrint Service: WeasyPrint probe", Status.OK), status);
}

@Test
void testConnectionRefused() {
HtmlToPdfConverter htmlToPdfConverter = mock(HtmlToPdfConverter.class);
when(htmlToPdfConverter.convert("<html><body>test html</body></html>", Orientation.PORTRAIT, PaperSize.A4)).thenThrow(new ProcessingException("java.net.ConnectException: Connection refused"));
WeasyPrintProbeStatusProvider weasyPrintProbeStatusProvider = new WeasyPrintProbeStatusProvider(htmlToPdfConverter);

ConfigurationStatus status = weasyPrintProbeStatusProvider.getStatus(ConfigurationStatusProvider.Context.builder().build());

assertEquals(new ConfigurationStatus("WeasyPrint Service: WeasyPrint probe", Status.ERROR, "java.net.ConnectException: Connection refused"), status);
}
}
Loading

0 comments on commit ee8a329

Please sign in to comment.