Skip to content

Commit

Permalink
chore: added tests
Browse files Browse the repository at this point in the history
Refs: #285
  • Loading branch information
pbezliapovich committed Dec 3, 2024
1 parent 658925b commit 8ee9203
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,6 +42,6 @@ public CollectionInternalController() {
)
public List<CollectionItem> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -171,11 +171,9 @@ private boolean sameDocument(@Nullable String projectId, @NotNull String spaceId
return testRunAttachment;
}

public @NotNull List<CollectionItem> getCollectionItems(@NotNull String projectId, @NotNull String collectionId) {
public @NotNull List<CollectionItem> getCollectionItems(@NotNull String projectId, @NotNull String collectionId, @NotNull ReadOnlyTransaction transaction) {
List<CollectionItem> collectionItemList = new ArrayList<>();
TransactionalExecutor.executeSafelyInReadOnlyTransaction(transaction -> {
IBaselineCollection collection = new BaselineCollectionReference(projectId, collectionId).get(transaction).getOldApi();

collection.getElements()
.stream()
.map(IBaselineCollectionElement::getObjectWithRevision)
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BaselineCollectionReference> mockedStaticReference = mockConstruction(BaselineCollectionReference.class, (mock, context) -> {
when(mock.get(Mockito.any())).thenReturn(baselineCollection);
})) {
List<CollectionItem> 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());
}
}
}
13 changes: 13 additions & 0 deletions src/test/js/ExportContextTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});

0 comments on commit 8ee9203

Please sign in to comment.