Skip to content

Commit

Permalink
feat: improve error message when downloading a missing document (#403)
Browse files Browse the repository at this point in the history
* feat: improve error message when downloading a missing document

* feat: update tes
  • Loading branch information
EmmanuelDemey committed Nov 7, 2023
1 parent 73a9f63 commit f666e9a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;

@Service
Expand Down Expand Up @@ -92,7 +93,7 @@ public String changeDocument(String docId, InputStream documentFile, String docu


@Override
public ResponseEntity<Object> downloadDocument(String id) throws RmesException {
public ResponseEntity<Object> downloadDocument(String id) throws RmesException, IOException {
return documentsUtils.downloadDocumentFile(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ public List<String> getDocumentPath(String id) throws RmesException {
* @return Response containing the file (inputStream)
* @throws RmesException
*/
public ResponseEntity<Object> downloadDocumentFile(String id) throws RmesException {
public ResponseEntity<Object> downloadDocumentFile(String id) throws RmesException, IOException {
List<String> pathAndFileName = this.getDocumentPath(id);
Path path = Path.of(pathAndFileName.get(0));
String fileName = pathAndFileName.get(1);
Expand All @@ -661,14 +661,10 @@ public ResponseEntity<Object> downloadDocumentFile(String id) throws RmesExcepti

//Get document as resource
ByteArrayResource resource = null;
try {
InputStream input = Files.newInputStream(path);
resource = new ByteArrayResource(IOUtils.toByteArray(input));
input.close();
} catch (IOException e) {
logger.error("Failed to getBytes of resource");
throw new RmesException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), "IOException");
}
InputStream input = Files.newInputStream(path);
resource = new ByteArrayResource(IOUtils.toByteArray(input));
input.close();


//return the response with document
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package fr.insee.rmes.webservice;
package fr.insee.rmes.webservice.operations;

import fr.insee.rmes.bauhaus_services.Constants;
import fr.insee.rmes.bauhaus_services.DocumentsService;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.model.operations.documentations.Document;
import fr.insee.rmes.webservice.GenericResources;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
Expand All @@ -24,6 +25,7 @@
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.NoSuchFileException;

/**
* WebService class for resources of Documents and Links
Expand Down Expand Up @@ -102,9 +104,12 @@ public ResponseEntity<Object> downloadDocument(@PathVariable(Constants.ID) Strin
} catch (RmesException e) {
logger.error(e.getDetails());
return returnRmesException(e);
} catch (NoSuchFileException e) {
logger.error("NoSuchFileException {}", e.getMessage());
return ResponseEntity.status(HttpStatus.NOT_FOUND).contentType(MediaType.TEXT_PLAIN).body(e.getMessage() + " does not exist");
} catch (IOException e) {
logger.error("IOException {}", e.getMessage());
return ResponseEntity.status(HttpStatus.NOT_FOUND).contentType(MediaType.TEXT_PLAIN).body(e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType(MediaType.TEXT_PLAIN).body(e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void nowPlus1Second() {
var keycloakServerZoneConfiguration = new KeycloakServerZoneConfiguration();
keycloakServerZoneConfiguration.setZoneByServers(Map.of());
var keycloakServices = new KeycloakServices("s", "i", "s",
"d", "di", "dk",keycloakServerZoneConfiguration);
"d", "di", "dk",keycloakServerZoneConfiguration);
var start= new Date();
var actual=keycloakServices.nowPlus1Second();
var nowPlus1= Date.from(start.toInstant().plusSeconds(1));
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/fr/insee/rmes/webservice/DocumentsResourcesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package fr.insee.rmes.webservice;

import fr.insee.rmes.bauhaus_services.DocumentsService;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.webservice.operations.DocumentsResources;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.io.IOException;
import java.nio.file.NoSuchFileException;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;


class DocumentsResourcesTest {

@Mock
private DocumentsService documentsService;

@InjectMocks
private DocumentsResources documentsResources;

@BeforeEach
public void init() {
MockitoAnnotations.openMocks(this);
}


@Test
void shouldReturnNotFoundException() throws RmesException, IOException {
when(documentsService.downloadDocument(anyString())).thenThrow(new NoSuchFileException("id"));
ResponseEntity<Object> response = documentsResources.downloadDocument("id");
Assertions.assertEquals(HttpStatus.NOT_FOUND.value(), response.getStatusCode().value());
Assertions.assertEquals("id does not exist", response.getBody());
}

@Test
void shouldReturnInternalException() throws RmesException, IOException {
when(documentsService.downloadDocument(anyString())).thenThrow(new IOException(anyString()));
ResponseEntity<Object> response = documentsResources.downloadDocument("id");
Assertions.assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), response.getStatusCode().value());
}
}

0 comments on commit f666e9a

Please sign in to comment.