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 19, 2024
1 parent 84da08e commit f501973
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,17 @@ boolean isMediaTypeMismatch(String resource, byte[] content) {
}

@VisibleForTesting
@SuppressWarnings("java:S1075")
byte[] getDefaultContent(String resource) throws IOException {
String defaultImagePath;
String pathToDefaultImage;
if (WorkItemAttachmentUrlResolver.isSvg(resource)) {
defaultImagePath = "/webapp/ria/images/image_not_accessible_svg.png";
pathToDefaultImage = "/webapp/ria/images/image_not_accessible_svg.png";
} else if (!StringUtils.isEmpty(MediaUtils.getImageFormat(resource))) {
defaultImagePath = "/webapp/ria/images/image_not_accessible.png";
pathToDefaultImage = "/webapp/ria/images/image_not_accessible.png";
} else {
return new byte[0];
}
File defaultImage = new File(BundleHelper.getPath("com.polarion.alm.ui", defaultImagePath));
File defaultImage = new File(BundleHelper.getPath("com.polarion.alm.ui", pathToDefaultImage));
return StreamUtils.suckStreamThenClose(new FileInputStream(defaultImage));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
PlatformContextMockExtension.class
})
class PdfExporterFileResourceProviderTest {
@Mock
PdfExporterFileResourceProvider resourceProviderMock;

@Mock
private IUrlResolver resolverMock;

Expand Down Expand Up @@ -64,6 +61,7 @@ void replaceImagesAsBase64EncodedTest() {
imgBytes = is != null ? is.readAllBytes() : new byte[0];
}

PdfExporterFileResourceProvider resourceProviderMock = mock(PdfExporterFileResourceProvider.class);
when(resourceProviderMock.getResourceAsBytes("http://localhost/some-path/img.png", null)).thenReturn(imgBytes);
when(resourceProviderMock.getResourceAsBase64String(any(), eq(null))).thenCallRealMethod();
String result = resourceProviderMock.getResourceAsBase64String("http://localhost/some-path/img.png", null);
Expand Down Expand Up @@ -128,8 +126,8 @@ void getWorkItemIdFromAttachmentUrlValidUrl() {

@Test
void getWorkItemIdFromAttachmentUrlInvalidUrl() {
String url = "http://example.com/invalid/url";
String result = resourceProviderMock.getWorkItemIdFromAttachmentUrl(url);
String url = "/http://example.com/invalid/url";
String result = resourceProvider.getWorkItemIdFromAttachmentUrl(url);
assertNull(result);
}

Expand All @@ -144,7 +142,7 @@ void isMediaTypeMismatchMatchingMimeTypes() {
mockedMediaUtils.when(() -> MediaUtils.getMimeTypeUsingTikaByResourceName(resource, null))
.thenReturn("image/png");

boolean result = resourceProviderMock.isMediaTypeMismatch(resource, content);
boolean result = resourceProvider.isMediaTypeMismatch(resource, content);
assertFalse(result);
}
}
Expand All @@ -157,8 +155,8 @@ void getDefaultContentEmptyForNonImage() {
mockedMediaUtils.when(() -> MediaUtils.getImageFormat(resource))
.thenReturn("");

byte[] content = resourceProviderMock.getDefaultContent(resource);
assertNull(content);
byte[] content = resourceProvider.getDefaultContent(resource);
assertArrayEquals(new byte[0], content);
}
}

Expand Down

0 comments on commit f501973

Please sign in to comment.