Skip to content

Commit

Permalink
feat: Added logic for handling unavailable images
Browse files Browse the repository at this point in the history
Refs: #307
  • Loading branch information
pbezliapovich committed Dec 20, 2024
1 parent f501973 commit bbc9a19
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ public byte[] getResourceAsBytes(@NotNull String resource, List<String> unavaila
});
}

private byte[] getResourceAsBytesImpl(String resource, List<String> unavailableWorkItemAttachments) throws IOException {
@VisibleForTesting
byte[] getResourceAsBytesImpl(String resource, List<String> 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;
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -117,17 +119,69 @@ void getResourceAsBytesExceptionHandling() {
assertTrue(unavailableAttachments.isEmpty());
}

@Test
@SneakyThrows
void getResourceAsBytesImplSuccessfully() {
try (MockedStatic<StreamUtils> 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<IUrlResolver> resolvers = List.of(resolver);
List<String> 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<StreamUtils> streamUtilsMockedStatic = mockStatic(StreamUtils.class); MockedStatic<WorkItemAttachmentUrlResolver> 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<String> 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);
}

Expand Down

0 comments on commit bbc9a19

Please sign in to comment.