Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve error message when downloading a missing document #403

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -5,36 +5,32 @@
import fr.insee.rmes.stubs.KeycloakServicesStub;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.*;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.autoconfigure.web.client.RestClientTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpEntity;
import org.springframework.test.context.TestPropertySource;
import org.springframework.web.client.RestTemplate;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.*;
import java.util.Date;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.when;

@RestClientTest
@Import({KeycloakServicesStub.class, KeycloakServerZoneConfiguration.class})
@TestPropertySource(properties = {
"fr.insee.rmes.bauhaus.keycloak.client.secret = SECRET
"fr.insee.rmes.bauhaus.keycloak.client.secret = XXX",
"fr.insee.rmes.bauhaus.keycloak.client.id = XXX",
"fr.insee.rmes.bauhaus.auth-server-url = SECRET
"fr.insee.rmes.bauhaus.keycloak.client.dmz.secret = SECRET
"fr.insee.rmes.bauhaus.auth-server-url= keycloak.interne",
"fr.insee.rmes.bauhaus.keycloak.client.dmz.secret = XXX",
"fr.insee.rmes.bauhaus.keycloak.client.dmz.id = XXX",
"fr.insee.rmes.bauhaus.dmz.auth-server-url = SECRET
"fr.insee.rmes.bauhaus.dmz.auth-server-url = keycloak.dmz",
"fr.insee.rmes.bauhaus.keycloak-configuration.zoneByServers.[serverinterne.insee.fr].zone=interne",
"fr.insee.rmes.bauhaus.keycloak-configuration.zoneByServers.[servergestion.insee.fr].zone=interne",
"fr.insee.rmes.bauhaus.keycloak-configuration.zoneByServers.[serverdmz.insee.fr].zone=dmz",
Expand All @@ -46,14 +42,13 @@ class KeycloakServicesTest {

@Autowired
private KeycloakServicesStub keycloakServices;
private Token token;

@Test
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 All @@ -64,7 +59,7 @@ void nowPlus1Second() {

@BeforeEach
void given(){
this.token=new Token(){
Token token = new Token() {
@Override
public String getAccessToken() {
return "token";
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());
}
}