Skip to content

Commit

Permalink
feat: refactor the object model and cover with unit tests (#313)
Browse files Browse the repository at this point in the history
* feat: refactor the object model and covert with unit tests
* feat: migration to generic 8.0.0

Refs: #312 #299
  • Loading branch information
grigoriev authored Dec 19, 2024
1 parent b7c1514 commit dd955b0
Show file tree
Hide file tree
Showing 50 changed files with 2,012 additions and 630 deletions.
2 changes: 1 addition & 1 deletion .mvn/maven.config
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-P polarion2404
-P polarion2410
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ All good so far.

## Upgrade

### Upgrade from version 7.x.x to 8.0.0

In version 8.0.0 support for Polarion 2404 and older has been dropped. This extensions supports only Polarion 2410.

### Upgrade from version 6.x.x to 7.0.0

In version 7.0.0 `/export-filename` REST API endpoint changed. As a result, if the endpoint has been used, it's required to adjust the calls accordingly.
Expand Down
24 changes: 5 additions & 19 deletions 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>7.7.0</version>
<version>8.0.0</version>
</parent>

<artifactId>ch.sbb.polarion.extension.pdf-exporter</artifactId>
Expand Down Expand Up @@ -76,27 +76,12 @@
</properties>

<profiles>
<!-- default profile: polarion2404 -->
<!-- default profile: polarion2410 -->
<profile>
<id>polarion2404</id>
<id>polarion2410</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<velocity-engine-core.artifactId>velocity-engine-core-2.3</velocity-engine-core.artifactId>
</properties>
</profile>

<!-- deprecated profile: polarion2310 -->
<profile>
<id>polarion2310</id>
<properties>
<velocity-engine-core.artifactId>velocity-engine-core-2.3</velocity-engine-core.artifactId>
</properties>
</profile>

<profile>
<id>polarion2410</id>
<properties>
<velocity-engine-core.artifactId>org.apache.velocity.engine-core_2.3.0</velocity-engine-core.artifactId>
</properties>
Expand Down Expand Up @@ -263,7 +248,8 @@
<configuration>
<outputFormat>JSON</outputFormat>
<resourcePackages>
<package>ch.sbb.polarion.extension.generic.rest.controller</package>
<package>ch.sbb.polarion.extension.generic.rest.controller.info</package>
<package>ch.sbb.polarion.extension.generic.rest.controller.settings</package>
<package>ch.sbb.polarion.extension.generic.rest.model</package>
<package>ch.sbb.polarion.extension.pdf_exporter.rest.controller</package>
<package>ch.sbb.polarion.extension.pdf_exporter.rest.model</package>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import ch.sbb.polarion.extension.pdf_exporter.rest.model.settings.localization.Language;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.settings.stylepackage.StylePackageModel;
import ch.sbb.polarion.extension.pdf_exporter.service.PdfExporterPolarionService;
import ch.sbb.polarion.extension.pdf_exporter.service.PolarionBaselineExecutor;
import ch.sbb.polarion.extension.pdf_exporter.settings.CoverPageSettings;
import ch.sbb.polarion.extension.pdf_exporter.settings.CssSettings;
import ch.sbb.polarion.extension.pdf_exporter.settings.HeaderFooterSettings;
Expand Down Expand Up @@ -57,7 +58,7 @@ public class PdfExporterFormExtension implements IFormExtension {
public String render(@NotNull IFormExtensionContext context) {
return TransactionalExecutor.executeSafelyInReadOnlyTransaction(transaction -> {
String baselineRevision = transaction.context().baselineRevision();
return polarionService.executeInBaseline(baselineRevision, transaction, () -> renderForm(transaction.context(), context.object().getOldApi()));
return PolarionBaselineExecutor.executeInBaseline(baselineRevision, transaction, () -> renderForm(transaction.context(), context.object().getOldApi()));
});
}

Expand Down Expand Up @@ -320,7 +321,7 @@ private String adjustButtons(@NotNull String form, @NotNull IModule module, @Not
}

private String getFilename(@NotNull IModule module) {
DocumentFileNameHelper documentFileNameHelper = new DocumentFileNameHelper(new PdfExporterPolarionService());
DocumentFileNameHelper documentFileNameHelper = new DocumentFileNameHelper();

ExportParams exportParams = ExportParams.builder()
.projectId(module.getProject().getId())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ch.sbb.polarion.extension.pdf_exporter.converter;

import ch.sbb.polarion.extension.generic.settings.SettingId;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.DocumentData;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.conversion.ExportParams;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.documents.DocumentData;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.settings.coverpage.CoverPageModel;
import ch.sbb.polarion.extension.pdf_exporter.settings.CoverPageSettings;
import ch.sbb.polarion.extension.pdf_exporter.util.MediaUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import ch.sbb.polarion.extension.generic.settings.SettingId;
import ch.sbb.polarion.extension.generic.util.ScopeUtils;
import ch.sbb.polarion.extension.pdf_exporter.properties.PdfExporterExtensionConfiguration;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.DocumentData;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.ExportMetaInfoCallback;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.WorkItemRefData;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.conversion.DocumentType;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.conversion.ExportParams;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.documents.DocumentData;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.settings.headerfooter.HeaderFooterModel;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.settings.webhooks.AuthType;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.settings.webhooks.WebhookConfig;
Expand All @@ -18,7 +18,7 @@
import ch.sbb.polarion.extension.pdf_exporter.settings.HeaderFooterSettings;
import ch.sbb.polarion.extension.pdf_exporter.settings.LocalizationSettings;
import ch.sbb.polarion.extension.pdf_exporter.settings.WebhooksSettings;
import ch.sbb.polarion.extension.pdf_exporter.util.DocumentDataHelper;
import ch.sbb.polarion.extension.pdf_exporter.util.DocumentDataFactory;
import ch.sbb.polarion.extension.pdf_exporter.util.EnumValuesProvider;
import ch.sbb.polarion.extension.pdf_exporter.util.HtmlLogger;
import ch.sbb.polarion.extension.pdf_exporter.util.HtmlProcessor;
Expand Down Expand Up @@ -59,7 +59,6 @@
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@AllArgsConstructor
@SuppressWarnings("java:S1200")
Expand All @@ -70,7 +69,6 @@ public class PdfConverter {
private final HeaderFooterSettings headerFooterSettings;
private final CssSettings cssSettings;

private final DocumentDataHelper documentDataHelper;
private final PlaceholderProcessor placeholderProcessor;
private final VelocityEvaluator velocityEvaluator;
private final CoverPageProcessor coverPageProcessor;
Expand All @@ -82,7 +80,6 @@ public PdfConverter() {
pdfExporterPolarionService = new PdfExporterPolarionService();
headerFooterSettings = new HeaderFooterSettings();
cssSettings = new CssSettings();
documentDataHelper = new DocumentDataHelper(pdfExporterPolarionService);
placeholderProcessor = new PlaceholderProcessor();
velocityEvaluator = new VelocityEvaluator();
coverPageProcessor = new CoverPageProcessor();
Expand All @@ -99,7 +96,7 @@ public byte[] convertToPdf(@NotNull ExportParams exportParams, @Nullable ExportM
generationLog.log("Starting html generation");

@Nullable ITrackerProject project = getTrackerProject(exportParams);
@NotNull final DocumentData<? extends IUniqueObject> documentData = getDocumentData(exportParams, project);
@NotNull final DocumentData<? extends IUniqueObject> documentData = DocumentDataFactory.getDocumentData(exportParams, true);
@NotNull String htmlContent = prepareHtmlContent(exportParams, project, documentData, metaInfoCallback);

generationLog.log("Html is ready, starting pdf generation");
Expand All @@ -118,7 +115,7 @@ public byte[] convertToPdf(@NotNull ExportParams exportParams, @Nullable ExportM

public @NotNull String prepareHtmlContent(@NotNull ExportParams exportParams, @Nullable ExportMetaInfoCallback metaInfoCallback) {
@Nullable ITrackerProject project = getTrackerProject(exportParams);
@NotNull final DocumentData<? extends IUniqueObject> documentData = getDocumentData(exportParams, project);
@NotNull final DocumentData<? extends IUniqueObject> documentData = DocumentDataFactory.getDocumentData(exportParams, true);
return prepareHtmlContent(exportParams, project, documentData, metaInfoCallback);
}

Expand All @@ -130,16 +127,6 @@ public byte[] convertToPdf(@NotNull ExportParams exportParams, @Nullable ExportM
return project;
}

private @NotNull DocumentData<? extends IUniqueObject> getDocumentData(@NotNull ExportParams exportParams, @Nullable ITrackerProject project) {
return switch (exportParams.getDocumentType()) {
case LIVE_DOC -> documentDataHelper.getLiveDoc(Objects.requireNonNull(project), exportParams);
case LIVE_REPORT -> documentDataHelper.getLiveReport(project, exportParams);
case TEST_RUN -> documentDataHelper.getTestRun(Objects.requireNonNull(project), exportParams);
case WIKI_PAGE -> documentDataHelper.getWikiPage(project, exportParams);
case BASELINE_COLLECTION -> throw new IllegalArgumentException("Unsupported document type: %s".formatted(exportParams.getDocumentType()));
};
}

private @NotNull String prepareHtmlContent(@NotNull ExportParams exportParams, @Nullable ITrackerProject project, @NotNull DocumentData<? extends IUniqueObject> documentData, @Nullable ExportMetaInfoCallback metaInfoCallback) {
String cssContent = getCssContent(documentData, exportParams);
String preparedDocumentContent = postProcessDocumentContent(exportParams, project, documentData.getContent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
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.service.PdfExporterPolarionService;
import ch.sbb.polarion.extension.pdf_exporter.util.DocumentDataHelper;
import ch.sbb.polarion.extension.pdf_exporter.util.PdfValidationService;
import org.jetbrains.annotations.VisibleForTesting;
import org.springframework.web.context.request.RequestAttributes;
Expand All @@ -33,14 +32,8 @@ public ConverterApiController() {

@VisibleForTesting
@SuppressWarnings("squid:S5803")
ConverterApiController(PdfExporterPolarionService pdfExporterPolarionService,
PdfConverter pdfConverter,
PdfValidationService pdfValidationService,
DocumentDataHelper documentDataHelper,
PdfConverterJobsService pdfConverterJobService,
UriInfo uriInfo,
HtmlToPdfConverter htmlToPdfConverter) {
super(pdfExporterPolarionService, pdfConverter, pdfValidationService, documentDataHelper, pdfConverterJobService, uriInfo, htmlToPdfConverter);
ConverterApiController(PdfExporterPolarionService pdfExporterPolarionService, PdfConverter pdfConverter, PdfValidationService pdfValidationService, PdfConverterJobsService pdfConverterJobService, UriInfo uriInfo, HtmlToPdfConverter htmlToPdfConverter) {
super(pdfConverter, pdfValidationService, pdfConverterJobService, uriInfo, htmlToPdfConverter);
this.polarionService = pdfExporterPolarionService;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import ch.sbb.polarion.extension.pdf_exporter.rest.model.conversion.ExportParams;
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.rest.model.documents.DocumentData;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.jobs.ConverterJobDetails;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.jobs.ConverterJobStatus;
import ch.sbb.polarion.extension.pdf_exporter.service.PdfExporterPolarionService;
import ch.sbb.polarion.extension.pdf_exporter.util.DocumentDataHelper;
import ch.sbb.polarion.extension.pdf_exporter.util.DocumentDataFactory;
import ch.sbb.polarion.extension.pdf_exporter.util.DocumentFileNameHelper;
import ch.sbb.polarion.extension.pdf_exporter.util.NumberedListsSanitizer;
import ch.sbb.polarion.extension.pdf_exporter.util.PdfValidationService;
import com.polarion.alm.tracker.model.IModule;
import com.polarion.core.util.StringUtils;
import com.polarion.platform.core.PlatformContext;
import com.polarion.platform.security.ISecurityService;
Expand All @@ -29,6 +31,7 @@
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.VisibleForTesting;
import org.springframework.http.HttpStatus;
Expand All @@ -49,6 +52,7 @@
import javax.ws.rs.core.UriInfo;
import java.net.URI;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

Expand All @@ -60,10 +64,8 @@ public class ConverterInternalController {

private static final String EXPORT_FILENAME_HEADER = "Export-Filename";

private final PdfExporterPolarionService pdfExporterPolarionService;
private final PdfConverter pdfConverter;
private final PdfValidationService pdfValidationService;
private final DocumentDataHelper documentDataHelper;
private final PdfConverterJobsService pdfConverterJobService;
private final PropertiesUtility propertiesUtility;
private final HtmlToPdfConverter htmlToPdfConverter;
Expand All @@ -72,28 +74,18 @@ public class ConverterInternalController {
private UriInfo uriInfo;

public ConverterInternalController() {
this.pdfExporterPolarionService = new PdfExporterPolarionService();
this.pdfConverter = new PdfConverter();
this.pdfValidationService = new PdfValidationService(pdfConverter);
this.documentDataHelper = new DocumentDataHelper(pdfExporterPolarionService);
ISecurityService securityService = PlatformContext.getPlatform().lookupService(ISecurityService.class);
this.pdfConverterJobService = new PdfConverterJobsService(pdfConverter, securityService);
this.propertiesUtility = new PropertiesUtility();
this.htmlToPdfConverter = new HtmlToPdfConverter();
}

@VisibleForTesting
ConverterInternalController(PdfExporterPolarionService pdfExporterPolarionService,
PdfConverter pdfConverter,
PdfValidationService pdfValidationService,
DocumentDataHelper documentDataHelper,
PdfConverterJobsService pdfConverterJobService,
UriInfo uriInfo,
HtmlToPdfConverter htmlToPdfConverter) {
this.pdfExporterPolarionService = pdfExporterPolarionService;
ConverterInternalController(PdfConverter pdfConverter, PdfValidationService pdfValidationService, PdfConverterJobsService pdfConverterJobService, UriInfo uriInfo, HtmlToPdfConverter htmlToPdfConverter) {
this.pdfConverter = pdfConverter;
this.pdfValidationService = pdfValidationService;
this.documentDataHelper = documentDataHelper;
this.pdfConverterJobService = pdfConverterJobService;
this.uriInfo = uriInfo;
this.propertiesUtility = new PropertiesUtility();
Expand Down Expand Up @@ -364,7 +356,10 @@ public WidthValidationResult validatePdfWidth(
)
@SuppressWarnings("java:S1166")
public NestedListsCheck checkNestedLists(ExportParams exportParams) {
boolean containsNestedLists = documentDataHelper.hasLiveDocNestedNumberedLists(exportParams);
DocumentData<IModule> documentData = DocumentDataFactory.getDocumentData(exportParams, true);
@NotNull String content = Objects.requireNonNull(documentData.getContent());
boolean containsNestedLists = new NumberedListsSanitizer().containsNestedNumberedLists(content);

return NestedListsCheck.builder().containsNestedLists(containsNestedLists).build();
}

Expand All @@ -386,8 +381,8 @@ private void validateExportParameters(ExportParams exportParams) {
private String getFileName(@Nullable ExportParams exportParams) {
if (exportParams != null) {
return StringUtils.isEmpty(exportParams.getFileName())
? new DocumentFileNameHelper(pdfExporterPolarionService).getDocumentFileName(exportParams)
: exportParams.getFileName();
? new DocumentFileNameHelper().getDocumentFileName(exportParams)
: exportParams.getFileName();
} else {
return "document.pdf";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public String getProjectName(@PathParam("projectId") String projectId) {
}
)
public String getFileName(ExportParams exportParams) {
return new DocumentFileNameHelper(pdfExporterPolarionService).getDocumentFileName(exportParams);
return new DocumentFileNameHelper().getDocumentFileName(exportParams);
}

@GET
Expand Down

This file was deleted.

Loading

0 comments on commit dd955b0

Please sign in to comment.