From bbc9a19e00dc761188db01b4262c1daafa338fc8 Mon Sep 17 00:00:00 2001 From: Pavel Bezliapovich Date: Fri, 20 Dec 2024 14:07:02 +0100 Subject: [PATCH] feat: Added logic for handling unavailable images Refs: #307 --- .../util/PdfExporterFileResourceProvider.java | 7 ++- .../PdfExporterFileResourceProviderTest.java | 58 ++++++++++++++++++- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/sbb/polarion/extension/pdf_exporter/util/PdfExporterFileResourceProvider.java b/src/main/java/ch/sbb/polarion/extension/pdf_exporter/util/PdfExporterFileResourceProvider.java index 321e2bf..3317dd8 100644 --- a/src/main/java/ch/sbb/polarion/extension/pdf_exporter/util/PdfExporterFileResourceProvider.java +++ b/src/main/java/ch/sbb/polarion/extension/pdf_exporter/util/PdfExporterFileResourceProvider.java @@ -83,14 +83,15 @@ public byte[] getResourceAsBytes(@NotNull String resource, List unavaila }); } - private byte[] getResourceAsBytesImpl(String resource, List unavailableWorkItemAttachments) throws IOException { + @VisibleForTesting + byte[] getResourceAsBytesImpl(String resource, List unavailableWorkItemAttachments) throws IOException { for (IUrlResolver resolver : resolvers) { if (resolver.canResolve(resource)) { InputStream stream = resolver.resolve(resource); if (stream != null) { byte[] result = StreamUtils.suckStreamThenClose(stream); if (result.length > 0 && WorkItemAttachmentUrlResolver.isWorkItemAttachmentUrl(resource) && isMediaTypeMismatch(resource, result)) { - unavailableWorkItemAttachments.add(getWorkItemIdFromAttachmentUrl(resource)); + unavailableWorkItemAttachments.add(getWorkItemIdsWithUnavailableAttachments(resource)); return getDefaultContent(resource); } return result; @@ -124,7 +125,7 @@ byte[] getDefaultContent(String resource) throws IOException { } @VisibleForTesting - String getWorkItemIdFromAttachmentUrl(@NotNull String url) { + String getWorkItemIdsWithUnavailableAttachments(@NotNull String url) { String prefix = "/polarion/wi-attachment/"; try { if (!url.startsWith(prefix)) { diff --git a/src/test/java/ch/sbb/polarion/extension/pdf_exporter/util/PdfExporterFileResourceProviderTest.java b/src/test/java/ch/sbb/polarion/extension/pdf_exporter/util/PdfExporterFileResourceProviderTest.java index ba20782..d85da25 100644 --- a/src/test/java/ch/sbb/polarion/extension/pdf_exporter/util/PdfExporterFileResourceProviderTest.java +++ b/src/test/java/ch/sbb/polarion/extension/pdf_exporter/util/PdfExporterFileResourceProviderTest.java @@ -7,6 +7,8 @@ import com.polarion.alm.tracker.internal.url.IUrlResolver; import com.polarion.alm.tracker.internal.url.ParentUrlResolver; import com.polarion.alm.tracker.internal.url.PolarionUrlResolver; +import com.polarion.alm.tracker.internal.url.WorkItemAttachmentUrlResolver; +import com.polarion.core.util.StreamUtils; import lombok.SneakyThrows; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -117,17 +119,69 @@ void getResourceAsBytesExceptionHandling() { assertTrue(unavailableAttachments.isEmpty()); } + @Test + @SneakyThrows + void getResourceAsBytesImplSuccessfully() { + try (MockedStatic streamUtilsMockedStatic = mockStatic(StreamUtils.class)) { + String resource = "valid/resource/url"; + byte[] expectedBytes = "expectedBytes".getBytes(); + streamUtilsMockedStatic.when(() -> StreamUtils.suckStreamThenClose(any(InputStream.class))).thenReturn(expectedBytes); + + IUrlResolver resolver = mock(IUrlResolver.class); + when(resolver.canResolve(resource)).thenReturn(true); + when(resolver.resolve(resource)).thenReturn(new ByteArrayInputStream(expectedBytes)); + + List resolvers = List.of(resolver); + List unavailableWorkItemAttachments = new ArrayList<>(); + + PdfExporterFileResourceProvider fileResourceProvider = new PdfExporterFileResourceProvider(resolvers); + + byte[] result = fileResourceProvider.getResourceAsBytesImpl(resource, unavailableWorkItemAttachments); + + assertArrayEquals(expectedBytes, result); + assertTrue(unavailableWorkItemAttachments.isEmpty()); + } + } + + @Test + @SneakyThrows + void getResourceAsBytesImplWithMediaTypeMismatch() { + try (MockedStatic streamUtilsMockedStatic = mockStatic(StreamUtils.class); MockedStatic workItemAttachmentUrlResolverMockedStatic = mockStatic(WorkItemAttachmentUrlResolver.class)) { + String resource = "workitem/attachment/url"; + byte[] resolvedBytes = "resolved".getBytes(); + byte[] defaultBytes = "default".getBytes(); + streamUtilsMockedStatic.when(() -> StreamUtils.suckStreamThenClose(any(InputStream.class))).thenReturn(resolvedBytes); + workItemAttachmentUrlResolverMockedStatic.when(() -> WorkItemAttachmentUrlResolver.isWorkItemAttachmentUrl(resource)).thenReturn(true); + IUrlResolver resolver = mock(IUrlResolver.class); + when(resolver.canResolve(resource)).thenReturn(true); + when(resolver.resolve(resource)).thenReturn(new ByteArrayInputStream(resolvedBytes)); + + PdfExporterFileResourceProvider fileResourceProvider = spy(new PdfExporterFileResourceProvider(List.of(resolver))); + doReturn(true).when(fileResourceProvider).isMediaTypeMismatch(resource, resolvedBytes); + doReturn(defaultBytes).when(fileResourceProvider).getDefaultContent(resource); + doReturn("unavailableId").when(fileResourceProvider).getWorkItemIdsWithUnavailableAttachments(resource); + + List unavailableWorkItemAttachments = new ArrayList<>(); + + byte[] result = fileResourceProvider.getResourceAsBytesImpl(resource, unavailableWorkItemAttachments); + + assertArrayEquals(defaultBytes, result); + assertEquals(1, unavailableWorkItemAttachments.size()); + assertEquals("unavailableId", unavailableWorkItemAttachments.get(0)); + } + } + @Test void getWorkItemIdFromAttachmentUrlValidUrl() { String url = "http://localhost/polarion/wi-attachment/elibrary/EL-14852/attachment.png"; - String result = resourceProvider.getWorkItemIdFromAttachmentUrl(url); + String result = resourceProvider.getWorkItemIdsWithUnavailableAttachments(url); assertEquals("EL-14852", result); } @Test void getWorkItemIdFromAttachmentUrlInvalidUrl() { String url = "/http://example.com/invalid/url"; - String result = resourceProvider.getWorkItemIdFromAttachmentUrl(url); + String result = resourceProvider.getWorkItemIdsWithUnavailableAttachments(url); assertNull(result); }