Skip to content

Commit

Permalink
feat: refactor the object model and covert with unit tests
Browse files Browse the repository at this point in the history
Refs: #312
  • Loading branch information
grigoriev committed Dec 16, 2024
1 parent 16b39b0 commit 29b88fe
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch.sbb.polarion.extension.pdf_exporter.test_extensions.generic;
package ch.sbb.polarion.extension.pdf_exporter.test_extensions;

import ch.sbb.polarion.extension.pdf_exporter.rest.model.conversion.ExportParams;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.documents.DocumentData;
Expand All @@ -10,7 +10,6 @@

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static org.mockito.Mockito.*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.polarion.platform.IPlatformService;
import com.polarion.platform.core.IPlatform;
import com.polarion.platform.core.PlatformContext;
import com.polarion.platform.persistence.IDataService;
import com.polarion.platform.security.ILoginPolicy;
import com.polarion.platform.security.ISecurityService;
import com.polarion.platform.service.repository.IRepositoryService;
Expand All @@ -28,6 +29,7 @@ public class PlatformContextMockExtension implements BeforeEachCallback, AfterEa
private ISecurityService securityService;
private IPlatformService platformService;
private IRepositoryService repositoryService;
private IDataService dataService;
private ILoginPolicy loginPolicy;
private ITestManagementService testManagementService;

Expand All @@ -40,6 +42,7 @@ public void beforeEach(ExtensionContext context) throws Exception {
securityService = mock(ISecurityService.class);
platformService = mock(IPlatformService.class);
repositoryService = mock(IRepositoryService.class);
dataService = mock(IDataService.class);
loginPolicy = mock(ILoginPolicy.class);
testManagementService = mock(ITestManagementService.class);

Expand All @@ -48,6 +51,7 @@ public void beforeEach(ExtensionContext context) throws Exception {
lenient().when(platformMock.lookupService(ISecurityService.class)).thenReturn(securityService);
lenient().when(platformMock.lookupService(IPlatformService.class)).thenReturn(platformService);
lenient().when(platformMock.lookupService(IRepositoryService.class)).thenReturn(repositoryService);
lenient().when(platformMock.lookupService(IDataService.class)).thenReturn(dataService);
lenient().when(platformMock.lookupService(ILoginPolicy.class)).thenReturn(loginPolicy);

platformContextMockedStatic = mockStatic(PlatformContext.class);
Expand All @@ -58,6 +62,7 @@ public void beforeEach(ExtensionContext context) throws Exception {
CustomExtensionMockInjector.inject(context, securityService);
CustomExtensionMockInjector.inject(context, platformService);
CustomExtensionMockInjector.inject(context, repositoryService);
CustomExtensionMockInjector.inject(context, dataService);
CustomExtensionMockInjector.inject(context, loginPolicy);

testManagementServiceAccessorMockedConstruction = mockConstruction(TestManagementServiceAccessor.class, (testManagementServiceAccessor, mockedContructionContext) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import java.io.InputStream;
import java.net.URL;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.times;

@ExtendWith(MockitoExtension.class)
class CustomResourceUrlResolverTest {
Expand All @@ -30,4 +30,4 @@ void replaceImagesUrlUnderscoreAndSpaceReplacementTest() {
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package ch.sbb.polarion.extension.pdf_exporter.util;

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.documents.ModelObjectProvider;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.documents.id.LiveDocId;
import ch.sbb.polarion.extension.pdf_exporter.service.PdfExporterPolarionService;
import ch.sbb.polarion.extension.pdf_exporter.test_extensions.generic.CustomExtensionMock;
import ch.sbb.polarion.extension.pdf_exporter.test_extensions.generic.PlatformContextMockExtension;
import ch.sbb.polarion.extension.pdf_exporter.test_extensions.generic.TransactionalExecutorExtension;
import com.polarion.alm.projects.IProjectService;
import com.polarion.alm.projects.model.IProject;
import com.polarion.alm.projects.model.IUniqueObject;
import com.polarion.alm.shared.api.model.document.Document;
import com.polarion.alm.shared.api.model.document.DocumentSelector;
import com.polarion.alm.shared.api.model.document.internal.InternalDocuments;
import com.polarion.alm.shared.api.services.internal.InternalServices;
import com.polarion.alm.shared.api.transaction.internal.InternalReadOnlyTransaction;
import com.polarion.alm.tracker.ITrackerService;
import com.polarion.alm.tracker.model.ITrackerProject;
import com.polarion.alm.tracker.model.ipi.IInternalBaselinesManager;
import com.polarion.alm.tracker.spi.model.IInternalModule;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;

@ExtendWith({MockitoExtension.class, TransactionalExecutorExtension.class, PlatformContextMockExtension.class})
class DocumentDataFactoryTest {

@CustomExtensionMock
private InternalReadOnlyTransaction internalReadOnlyTransactionMock;

@CustomExtensionMock
private IProjectService projectServiceMock;

@Mock
PdfExporterPolarionService pdfExporterPolarionServiceMock;

@BeforeEach
void setUp() {
IProject projectMock = mock(IProject.class);
when(projectMock.getId()).thenReturn("testProjectId");
when(pdfExporterPolarionServiceMock.getProject(eq("testProjectId"))).thenReturn(projectMock);

InternalDocuments internalDocumentsMock = mock(InternalDocuments.class);
DocumentSelector documentSelectorMock = mock(DocumentSelector.class);
when(documentSelectorMock.revision(ArgumentMatchers.any())).thenReturn(documentSelectorMock);
Document documentMock = mock(Document.class);
when(documentSelectorMock.spaceReferenceAndName(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(documentMock);
when(internalDocumentsMock.getBy()).thenReturn(documentSelectorMock);
when(internalReadOnlyTransactionMock.documents()).thenReturn(internalDocumentsMock);
}

@Test
void testGetDocumentData() {
IInternalModule moduleMock = mock(IInternalModule.class);
when(moduleMock.getId()).thenReturn("testModuleId");
when(moduleMock.getModuleFolder()).thenReturn("testModuleFolder");
when(moduleMock.getTitleOrName()).thenReturn("testModuleTitle");
ITrackerProject projectMock = mock(ITrackerProject.class);
when(projectMock.getId()).thenReturn("testProjectId");
when(moduleMock.getProject()).thenReturn(projectMock);
when(moduleMock.getLastRevision()).thenReturn("12345");

IInternalBaselinesManager internalBaselinesManager = mock(IInternalBaselinesManager.class);
when(projectMock.getBaselinesManager()).thenReturn(internalBaselinesManager);
when(internalBaselinesManager.getRevisionBaseline(eq("12345"))).thenReturn(null);

InternalServices internalServicesMock = mock(InternalServices.class);
ITrackerService trackerService = mock(ITrackerService.class);
when(trackerService.getTrackerProject(any(IProject.class))).thenReturn(projectMock);
lenient().when(internalServicesMock.trackerService()).thenReturn(trackerService);
lenient().when(internalReadOnlyTransactionMock.services()).thenReturn(internalServicesMock);

Document documentMock = mock(Document.class);
when(documentMock.getOldApi()).thenReturn(moduleMock);

when(projectServiceMock.getProject(eq("testProjectId"))).thenReturn(projectMock);

ExportParams exportParamsMock = ExportParams.builder()
.projectId("testProjectId")
.locationPath("testModuleFolder/testLocationPath")
.documentType(DocumentType.LIVE_DOC)
.build();
when(new ModelObjectProvider(exportParamsMock, pdfExporterPolarionServiceMock).getModelObject(internalReadOnlyTransactionMock))
.thenReturn(documentMock);

DocumentData<IUniqueObject> actualDocumentData = DocumentDataFactory.getDocumentData(exportParamsMock, false);

assertNotNull(actualDocumentData);
assertEquals("testProjectId", actualDocumentData.getId().getDocumentProject().getId());
assertEquals("testModuleFolder", ((LiveDocId) actualDocumentData.getId()).getSpaceId());
assertEquals("testModuleId", actualDocumentData.getId().getDocumentId());
assertEquals(DocumentType.LIVE_DOC, actualDocumentData.getType());
assertEquals("testModuleTitle", actualDocumentData.getTitle());
assertNull(actualDocumentData.getRevision());
assertEquals("12345", actualDocumentData.getLastRevision());
assertEquals("12345", actualDocumentData.getRevisionPlaceholder());
assertEquals("", actualDocumentData.getBaseline().asPlaceholder());
assertNull(actualDocumentData.getContent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import ch.sbb.polarion.extension.pdf_exporter.rest.model.documents.id.LiveDocId;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.settings.filename.FileNameTemplateModel;
import ch.sbb.polarion.extension.pdf_exporter.settings.FileNameTemplateSettings;
import ch.sbb.polarion.extension.pdf_exporter.test_extensions.generic.DocumentDataFactoryMockExtension;
import ch.sbb.polarion.extension.pdf_exporter.test_extensions.DocumentDataFactoryMockExtension;
import ch.sbb.polarion.extension.pdf_exporter.test_extensions.generic.PlatformContextMockExtension;
import ch.sbb.polarion.extension.pdf_exporter.test_extensions.generic.TransactionalExecutorExtension;
import ch.sbb.polarion.extension.pdf_exporter.util.velocity.VelocityEvaluator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

class WildcardUtilsTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
Expand Down

0 comments on commit 29b88fe

Please sign in to comment.