Skip to content

Commit 1292bde

Browse files
committed
FINERACT-2502: Resolve the issue 403 Forbidden error when a client is missing a profile image
Signed-off-by: Bhoomi <bhoomi.bt22cse@pec.edu.in>
1 parent 93f5292 commit 1292bde

3 files changed

Lines changed: 92 additions & 22 deletions

File tree

fineract-document/src/main/java/org/apache/fineract/infrastructure/documentmanagement/exception/DocumentNotFoundException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
*/
1919
package org.apache.fineract.infrastructure.documentmanagement.exception;
2020

21-
import org.apache.fineract.infrastructure.core.exception.AbstractPlatformDomainRuleException;
21+
import org.apache.fineract.infrastructure.core.exception.AbstractPlatformResourceNotFoundException;
2222

23-
public class DocumentNotFoundException extends AbstractPlatformDomainRuleException {
23+
public class DocumentNotFoundException extends AbstractPlatformResourceNotFoundException {
2424

2525
public DocumentNotFoundException(final Long id) {
2626
super("error.msg.document.id.invalid", "Document with identifier " + id + " does not exist", id);

fineract-document/src/main/java/org/apache/fineract/infrastructure/documentmanagement/service/ImageReadPlatformServiceImpl.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,21 @@ public class ImageReadPlatformServiceImpl implements ImageReadPlatformService {
4545

4646
@Override
4747
public DocumentContent retrieveImage(final String entityType, final Long entityId) {
48-
try {
49-
return imageIdAdapters.stream().filter(imageIdAdapter -> imageIdAdapter.accept(entityType)).findFirst()
50-
.flatMap(imageIdAdapter -> imageIdAdapter.get(entityId))
51-
.flatMap(
52-
imageIdResult -> imageRepository.findById(imageIdResult.getId())
53-
.map(image -> DocumentContent
54-
.builder().fileName(
55-
FilenameUtils.getName(image.getLocation()))
56-
.format(FilenameUtils.getExtension(image.getLocation()))
57-
.displayName(imageIdResult.getDisplayName())
58-
.contentType(
59-
contentDetectorManager
60-
.detect(ContentDetectorContext.builder()
61-
.fileName(FilenameUtils.getName(image.getLocation())).build())
62-
.getMimeType())
63-
.stream(storeService.download(image.getLocation())).build()))
64-
.orElseThrow(() -> new DocumentNotFoundException(entityType, entityId, -1L));
65-
} catch (final Exception e) {
66-
throw new DocumentInvalidRequestException(e);
67-
}
48+
return imageIdAdapters.stream().filter(imageIdAdapter -> imageIdAdapter.accept(entityType)).findFirst()
49+
.flatMap(imageIdAdapter -> imageIdAdapter.get(entityId))
50+
.flatMap(
51+
imageIdResult -> imageRepository.findById(imageIdResult.getId())
52+
.map(image -> DocumentContent
53+
.builder().fileName(
54+
FilenameUtils.getName(image.getLocation()))
55+
.format(FilenameUtils.getExtension(image.getLocation()))
56+
.displayName(imageIdResult.getDisplayName())
57+
.contentType(
58+
contentDetectorManager
59+
.detect(ContentDetectorContext.builder()
60+
.fileName(FilenameUtils.getName(image.getLocation())).build())
61+
.getMimeType())
62+
.stream(storeService.download(image.getLocation())).build()))
63+
.orElseThrow(() -> new DocumentNotFoundException(entityType, entityId, -1L));
6864
}
6965
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.fineract.infrastructure.documentmanagement.service;
20+
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
22+
import static org.mockito.ArgumentMatchers.anyString;
23+
import static org.mockito.Mockito.when;
24+
25+
import java.util.Collections;
26+
import java.util.List;
27+
import java.util.Optional;
28+
import org.apache.fineract.infrastructure.contentstore.detector.ContentDetectorManager;
29+
import org.apache.fineract.infrastructure.contentstore.service.ContentStoreService;
30+
import org.apache.fineract.infrastructure.documentmanagement.adapter.EntityImageIdAdapter;
31+
import org.apache.fineract.infrastructure.documentmanagement.domain.ImageRepository;
32+
import org.apache.fineract.infrastructure.documentmanagement.exception.DocumentNotFoundException;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.Test;
35+
import org.junit.jupiter.api.extension.ExtendWith;
36+
import org.mockito.Mock;
37+
import org.mockito.junit.jupiter.MockitoExtension;
38+
39+
@ExtendWith(MockitoExtension.class)
40+
class ImageReadPlatformServiceImplTest {
41+
42+
@Mock
43+
private ImageRepository imageRepository;
44+
@Mock
45+
private ContentStoreService storeService;
46+
@Mock
47+
private ContentDetectorManager contentDetectorManager;
48+
@Mock
49+
private EntityImageIdAdapter imageIdAdapter;
50+
51+
private ImageReadPlatformServiceImpl imageReadPlatformService;
52+
53+
@BeforeEach
54+
void setUp() {
55+
List<EntityImageIdAdapter> imageIdAdapters = Collections.singletonList(imageIdAdapter);
56+
imageReadPlatformService = new ImageReadPlatformServiceImpl(imageIdAdapters, storeService, imageRepository,
57+
contentDetectorManager);
58+
}
59+
60+
@Test
61+
void testRetrieveImage_NotFound_ThrowsDocumentNotFoundException() {
62+
// Arrange
63+
String entityType = "clients";
64+
Long entityId = 1L;
65+
66+
when(imageIdAdapter.accept(anyString())).thenReturn(true);
67+
when(imageIdAdapter.get(entityId)).thenReturn(Optional.empty());
68+
69+
// Act & Assert
70+
assertThrows(DocumentNotFoundException.class, () -> {
71+
imageReadPlatformService.retrieveImage(entityType, entityId);
72+
});
73+
}
74+
}

0 commit comments

Comments
 (0)