Skip to content

Commit

Permalink
tests(hotfix always use FileOperations to process documents)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabrice B committed Dec 12, 2024
1 parent bee1204 commit cd19871
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 186 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<groupId>fr.insee.rmes</groupId>
<artifactId>Bauhaus-BO</artifactId>
<packaging>jar</packaging>
<version>4.1.6</version>
<version>4.1.7-beta2</version>
<name>Bauhaus-Back-Office</name>
<description>Back-office services for Bauhaus</description>
<url>https://github.com/InseeFr/Bauhaus-Back-Office</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class FileSystemOperation implements FilesOperations {
protected Config config;

@Override
public void delete(String path) {
public void delete(Path absolutePath) {
try {
Files.delete(Paths.get(path));
Files.delete(absolutePath);
} catch (IOException e) {
throw new RmesFileException(path, "Failed to delete file: " + path, e);
throw new RmesFileException(absolutePath.getFileName().toString(), "Failed to delete file: " + absolutePath, e);
}
}

Expand All @@ -35,6 +35,11 @@ public InputStream read(String fileName) {
}
}

@Override
public boolean existsInStorage(String filename) {
return Files.exists(Paths.get(config.getDocumentsStorageGestion()).resolve(filename));
}

@Override
public void write(InputStream content, Path destPath) {
try {
Expand All @@ -59,4 +64,5 @@ public void copy(String srcPath, String destPath) {
public boolean dirExists(Path gestionStorageFolder) {
return Files.isDirectory(requireNonNull(gestionStorageFolder));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import java.nio.file.Path;

public interface FilesOperations {
void delete(String path);
InputStream read(String path);
void delete(Path absolutePath);
InputStream read(String filename);
void write(InputStream content, Path destPath);
void copy(String srcPath, String destPath);

boolean dirExists(Path gestionStorageFolder);

boolean existsInStorage(String filename);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import static java.util.Objects.requireNonNull;

public record MinioFilesOperation(MinioClient minioClient, String bucketName, String directoryGestion, String directoryPublication) implements FilesOperations {

@Override
Expand All @@ -33,6 +35,18 @@ private static String extractFileName(String filePath) {
return Path.of(filePath).getFileName().toString();
}

@Override
public boolean existsInStorage(String filename) {
var objectName = extractFileName(requireNonNull(filename));
try {
return minioClient.statObject(StatObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build()).size() > 0;
} catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) {
return false;
}
}

@Override
public void write(InputStream content, Path filePath) {
Expand Down Expand Up @@ -80,7 +94,8 @@ public boolean dirExists(Path gestionStorageFolder) {
}

@Override
public void delete(String objectName) {
public void delete(Path absolutePath) {
String objectName = absolutePath.getFileName().toString();
try {
minioClient.removeObject(RemoveObjectArgs.builder()
.bucket(bucketName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,19 @@ private Set<String> exportRubricsDocuments(JSONObject sims, Path directory) thro

for (int i = 0; i < documents.length(); i++) {
JSONObject document = documents.getJSONObject(i);
String url = document.getString("url").replace("file://", "");
String url = DocumentsUtils.getDocumentUrlFromDocument(document);
if(!history.contains(url)){
history.add(url);
logger.debug("Extracting document {}", url);


Path documentPath = Path.of(url);
String documentFilename = DocumentsUtils.getDocumentNameFromUrl(url);

if(!Files.exists(documentPath)){
if(!documentsUtils.existsInStorage(documentFilename)){
missingDocuments.add(document.getString("id"));
} else {
String documentFileName = FilesUtils.generateFinalFileNameWithExtension(UriUtils.getLastPartFromUri(url), maxLength);
try (InputStream inputStream = Files.newInputStream(documentPath)){
try (InputStream inputStream = documentsUtils.retrieveDocumentFromStorage(documentFilename)){
Path documentDirectory = Path.of(directory.toString(), "documents");
if (!Files.exists(documentDirectory)) {
logger.debug("Creating the documents folder");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void publishAllDocumentsInSims(String idSims) throws RmesException {
for (Map.Entry<Integer, String> doc : mapIdUrls.entrySet()) {
String docId = doc.getKey().toString();
String originalPath = doc.getValue();
String filename = docUtils.getDocumentNameFromUrl(originalPath);
String filename = DocumentsUtils.getDocumentNameFromUrl(originalPath);
// Publish the physical files
copyFileInPublicationFolders(originalPath);
// Change url in document (getModelToPublish) and publish the RDF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -282,7 +281,7 @@ private void checkUrlDoesNotExist(String id, String url, int errorCode, String e
JSONObject existingUriJson = repoGestion.getResponseAsObject(DocumentsQueries.getDocumentUriQuery(url));
if (existingUriJson.length() > 0) {
String uri = existingUriJson.getString(Constants.DOCUMENT);
String existingId = getIdFromUri(uri);
String existingId = getDocumentNameFromUrl(uri);
if (!existingId.equals(id)) {
throw new RmesNotAcceptableException(errorCode, errorMessage, uri);
}
Expand Down Expand Up @@ -556,21 +555,13 @@ private void changeDocumentsURL(String iri, String docUrl, String newUrl) throws

private void deleteFile(String docUrl) {
Path path = Paths.get(docUrl);
try {
Files.delete(path);
} catch (IOException e) {
logger.error(e.getMessage());
}
filesOperations.delete(path);
}

public String getDocumentNameFromUrl(String docUrl) {
public static String getDocumentNameFromUrl(String docUrl) {
return UriUtils.getLastPartFromUri(docUrl);
}

private String getIdFromUri(String uri) {
return UriUtils.getLastPartFromUri(uri);
}

private String createFileUrl(String name) throws RmesException {
Path gestionStorageFolder=Path.of(config.getDocumentsStorageGestion());
if (!filesOperations.dirExists(gestionStorageFolder)){
Expand Down Expand Up @@ -603,7 +594,7 @@ private IRI getDocumentUri(IRI url) throws RmesException {
return RdfUtils.toURI(uri.getString(Constants.DOCUMENT));
}

public String getDocumentUrlFromDocument(JSONObject jsonDoc) {
public static String getDocumentUrlFromDocument(JSONObject jsonDoc) {
return jsonDoc.getString(Constants.URL).replace(SCHEME_FILE, "");
}

Expand Down Expand Up @@ -696,5 +687,13 @@ private String getFileName(String path) {
// Extraire juste le nom de fichier du chemin
return Paths.get(path).getFileName().toString();
}

public InputStream retrieveDocumentFromStorage(String filename) {
return filesOperations.read(filename);
}

public boolean existsInStorage(String filename) {
return filesOperations.existsInStorage(filename);
}
}

121 changes: 0 additions & 121 deletions src/main/java/fr/insee/rmes/utils/RestTemplateUtils.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.HashMap;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -71,6 +72,7 @@ void testExportAsZip_success() throws Exception {
document.put("id", "1");

when(documentsUtils.getDocumentsUriAndUrlForSims("sims123")).thenReturn(new JSONArray().put(document));
when(documentsUtils.existsInStorage(any())).thenReturn(false);
var sims = new JSONObject();
sims.put("id", "sims123");
sims.put("labelLg1", "simsLabel");
Expand All @@ -93,8 +95,8 @@ void testExportAsZip_success() throws Exception {

assertEquals(HttpStatus.OK, response.getStatusCode());
assertNotNull(response.getBody());
assertEquals(response.getStatusCode(), HttpStatus.OK);
assertEquals(response.getHeaders().get("X-Missing-Documents").get(0), "1");
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getHeaders().get("X-Missing-Documents").getFirst()).isEqualTo("1");
}

@Test
Expand Down
Loading

0 comments on commit cd19871

Please sign in to comment.