From 8ee9203e26c1a1928e4dd8a352a25cd8b92a2231 Mon Sep 17 00:00:00 2001 From: Pavel Bezliapovich Date: Tue, 3 Dec 2024 16:41:14 +0100 Subject: [PATCH] chore: added tests Refs: #285 --- .../CollectionInternalController.java | 3 +- .../service/PdfExporterPolarionService.java | 9 +--- .../PdfExporterPolarionServiceTest.java | 54 +++++++++++++++++++ src/test/js/ExportContextTest.js | 13 +++++ 4 files changed, 71 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/sbb/polarion/extension/pdf_exporter/rest/controller/CollectionInternalController.java b/src/main/java/ch/sbb/polarion/extension/pdf_exporter/rest/controller/CollectionInternalController.java index 5ea4a115..2e35fbb4 100644 --- a/src/main/java/ch/sbb/polarion/extension/pdf_exporter/rest/controller/CollectionInternalController.java +++ b/src/main/java/ch/sbb/polarion/extension/pdf_exporter/rest/controller/CollectionInternalController.java @@ -3,6 +3,7 @@ import ch.sbb.polarion.extension.pdf_exporter.rest.model.collections.CollectionItem; import ch.sbb.polarion.extension.pdf_exporter.service.PdfExporterPolarionService; +import com.polarion.alm.shared.api.transaction.TransactionalExecutor; import io.swagger.v3.oas.annotations.Hidden; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; @@ -41,6 +42,6 @@ public CollectionInternalController() { ) public List getCollectionItems(@Parameter(description = "Project ID", required = true) @PathParam("projectId") String projectId, @Parameter(description = "Collection ID", required = true) @PathParam("collectionId") String collectionId) { - return pdfExporterPolarionService.getCollectionItems(projectId, collectionId); + return TransactionalExecutor.executeSafelyInReadOnlyTransaction(transaction -> pdfExporterPolarionService.getCollectionItems(projectId, collectionId, transaction)); } } diff --git a/src/main/java/ch/sbb/polarion/extension/pdf_exporter/service/PdfExporterPolarionService.java b/src/main/java/ch/sbb/polarion/extension/pdf_exporter/service/PdfExporterPolarionService.java index a31f0e51..175872d0 100644 --- a/src/main/java/ch/sbb/polarion/extension/pdf_exporter/service/PdfExporterPolarionService.java +++ b/src/main/java/ch/sbb/polarion/extension/pdf_exporter/service/PdfExporterPolarionService.java @@ -13,7 +13,7 @@ import ch.sbb.polarion.extension.pdf_exporter.util.WildcardUtils; import com.polarion.alm.projects.IProjectService; import com.polarion.alm.shared.api.model.baselinecollection.BaselineCollectionReference; -import com.polarion.alm.shared.api.transaction.TransactionalExecutor; +import com.polarion.alm.shared.api.transaction.ReadOnlyTransaction; import com.polarion.alm.tracker.ITestManagementService; import com.polarion.alm.tracker.ITrackerService; import com.polarion.alm.tracker.model.IModule; @@ -171,11 +171,9 @@ private boolean sameDocument(@Nullable String projectId, @NotNull String spaceId return testRunAttachment; } - public @NotNull List getCollectionItems(@NotNull String projectId, @NotNull String collectionId) { + public @NotNull List getCollectionItems(@NotNull String projectId, @NotNull String collectionId, @NotNull ReadOnlyTransaction transaction) { List collectionItemList = new ArrayList<>(); - TransactionalExecutor.executeSafelyInReadOnlyTransaction(transaction -> { IBaselineCollection collection = new BaselineCollectionReference(projectId, collectionId).get(transaction).getOldApi(); - collection.getElements() .stream() .map(IBaselineCollectionElement::getObjectWithRevision) @@ -184,9 +182,6 @@ private boolean sameDocument(@Nullable String projectId, @NotNull String spaceId .forEach(module -> { collectionItemList.add(new CollectionItem(module.getModuleNameWithSpace().replaceFirst("\\s*/\\s*", "/"), module.getLastRevision())); }); - - return null; - }); return collectionItemList; } } diff --git a/src/test/java/ch/sbb/polarion/extension/pdf_exporter/service/PdfExporterPolarionServiceTest.java b/src/test/java/ch/sbb/polarion/extension/pdf_exporter/service/PdfExporterPolarionServiceTest.java index 3559fcb6..8be7093c 100644 --- a/src/test/java/ch/sbb/polarion/extension/pdf_exporter/service/PdfExporterPolarionServiceTest.java +++ b/src/test/java/ch/sbb/polarion/extension/pdf_exporter/service/PdfExporterPolarionServiceTest.java @@ -6,21 +6,30 @@ import ch.sbb.polarion.extension.generic.util.PObjectListStub; import ch.sbb.polarion.extension.generic.util.ScopeUtils; import ch.sbb.polarion.extension.pdf_exporter.rest.model.attachments.TestRunAttachment; +import ch.sbb.polarion.extension.pdf_exporter.rest.model.collections.CollectionItem; import ch.sbb.polarion.extension.pdf_exporter.rest.model.settings.stylepackage.StylePackageModel; import ch.sbb.polarion.extension.pdf_exporter.rest.model.settings.stylepackage.StylePackageWeightInfo; import ch.sbb.polarion.extension.pdf_exporter.settings.StylePackageSettings; import com.polarion.alm.projects.IProjectService; +import com.polarion.alm.shared.api.model.baselinecollection.BaselineCollection; +import com.polarion.alm.shared.api.model.baselinecollection.BaselineCollectionReference; +import com.polarion.alm.shared.api.transaction.ReadOnlyTransaction; import com.polarion.alm.tracker.ITestManagementService; import com.polarion.alm.tracker.ITrackerService; +import com.polarion.alm.tracker.model.IModule; import com.polarion.alm.tracker.model.ITestRun; import com.polarion.alm.tracker.model.ITestRunAttachment; import com.polarion.alm.tracker.model.ITrackerProject; +import com.polarion.alm.tracker.model.baselinecollection.IBaselineCollection; +import com.polarion.alm.tracker.model.baselinecollection.IBaselineCollectionElement; import com.polarion.platform.IPlatformService; import com.polarion.platform.security.ISecurityService; import com.polarion.platform.service.repository.IRepositoryService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.MockedConstruction; import org.mockito.MockedStatic; +import org.mockito.Mockito; import java.util.ArrayList; import java.util.Collection; @@ -226,4 +235,49 @@ void testGetTestRunAttachment() { assertThrows(IllegalArgumentException.class, () -> service.getTestRunAttachment("testProjectId", "testTestRunId", "nonExistingAttachmentId", null)); } + + @Test + void testGetCollectionItems() { + // Mock dependencies + String projectId = "testProjectId"; + String collectionId = "testCollectionId"; + + IBaselineCollection mockCollection = mock(IBaselineCollection.class); + BaselineCollectionReference mockReference = mock(BaselineCollectionReference.class); + + // Mock collection elements + IModule mockModule1 = mock(IModule.class); + IModule mockModule2 = mock(IModule.class); + + when(mockModule1.getModuleNameWithSpace()).thenReturn("space1/Module1"); + when(mockModule1.getLastRevision()).thenReturn("1"); + + when(mockModule2.getModuleNameWithSpace()).thenReturn("space2/Module2"); + when(mockModule2.getLastRevision()).thenReturn("2"); + + IBaselineCollectionElement mockElement1 = mock(IBaselineCollectionElement.class); + IBaselineCollectionElement mockElement2 = mock(IBaselineCollectionElement.class); + + when(mockElement1.getObjectWithRevision()).thenReturn(mockModule1); + when(mockElement2.getObjectWithRevision()).thenReturn(mockModule2); + + BaselineCollection baselineCollection = mock(BaselineCollection.class); + when(mockCollection.getElements()).thenReturn(List.of(mockElement1, mockElement2)); + when(mockReference.get(Mockito.any())).thenReturn(baselineCollection); + when(baselineCollection.getOldApi()).thenReturn(mockCollection); + + try (MockedConstruction mockedStaticReference = mockConstruction(BaselineCollectionReference.class, (mock, context) -> { + when(mock.get(Mockito.any())).thenReturn(baselineCollection); + })) { + List result = service.getCollectionItems(projectId, collectionId, mock(ReadOnlyTransaction.class)); + + assertNotNull(result); + assertEquals(2, result.size()); + assertEquals("space1/Module1", result.get(0).getModuleNameWithSpace()); + assertEquals("1", result.get(0).getRevision()); + + assertEquals("space2/Module2", result.get(1).getModuleNameWithSpace()); + assertEquals("2", result.get(1).getRevision()); + } + } } diff --git a/src/test/js/ExportContextTest.js b/src/test/js/ExportContextTest.js index ff7f35d3..576fcff3 100644 --- a/src/test/js/ExportContextTest.js +++ b/src/test/js/ExportContextTest.js @@ -119,4 +119,17 @@ describe('ExportContext Class', function () { expect(exportContext.getDocumentName()).to.be.undefined; }); + it('URL: #/project/elibrary/collection', function () { + const locationHash = "#/project/elibrary/collection"; + const exportContext = new ExportContext({documentType: ExportParams.DocumentType.BASELINE_COLLECTION, polarionLocationHash: locationHash}); + + expect(exportContext.documentType).to.equal(ExportParams.DocumentType.BASELINE_COLLECTION); + expect(exportContext.projectId).to.equal('elibrary'); + expect(exportContext.locationPath).to.be.undefined; + expect(exportContext.revision).to.be.undefined; + expect(exportContext.urlQueryParameters).to.be.undefined; + + expect(exportContext.getSpaceId()).to.be.undefined; + expect(exportContext.getDocumentName()).to.be.undefined; + }); });