Skip to content

Commit

Permalink
refactor(#2708): Make all methods of FileManager none static (#2709)
Browse files Browse the repository at this point in the history
* refactor(#2708): Make all methods of FileManager none static

* refactor(#2708): Add unit tests for getAllFiles

* refactor(#2708): Add unit tests for getFile

* refactor(#2708): Add tests in FileManager for storeFile and deleteFile

* refactor(#2708): Make FileHasher none static and add tests

* refactor(#2708): Add unit test for checkFileContentChanged in FileManager

* refactor(#2708): Remove hash code from FileMetadata
  • Loading branch information
tenthe authored Apr 8, 2024
1 parent 5b67345 commit 34accf4
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@

public class FileHasher {

public static String hash(File file) throws IOException {
public String hash(File file) throws IOException {

if (file == null) {
throw new IOException("Input file is null. Please provide a valid file to calculate the hash.");
}

try (InputStream is = Files.newInputStream(Paths.get(file.toURI()))) {
return DigestUtils.md5Hex(is);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.streampipes.commons.file;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class FileHasherTest {

private FileHasher fileHasher;

@BeforeEach
public void setup() {
this.fileHasher = new FileHasher();
}

@Test
void hash_returnsCorrectHashForFile() throws IOException {
var file = new File("src/test/resources/test.txt");
assertEquals("6df4d50a41a5d20bc4faad8a6f09aa8f", fileHasher.hash(file));
}

@Test
void hash_throwsIOExceptionForNonExistingFile() {
var file = new File("src/test/resources/nonExistingFile.txt");
assertThrows(IOException.class, () -> fileHasher.hash(file));
}

@Test
void hash_throwsIOExceptionForNullFile() {
assertThrows(IOException.class, () -> fileHasher.hash(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public byte[] generateExportPackage() throws IOException {
String filename = fileResolver.findDocument(item.getResourceId()).getFilename();
addDoc(builder, item, new FileResolver(), manifest::addFile);
try {
builder.addBinary(filename, Files.readAllBytes(FileManager.getFile(filename).toPath()));
builder.addBinary(filename, Files.readAllBytes(new FileManager().getFile(filename).toPath()));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static InputStream getFileInputStream(String selectedFilename) throws Fil
*/
private static boolean checkIfFileChanged(String selectedFilename) {
try {
var hash = FileHasher.hash(getFile(selectedFilename));
var hash = new FileHasher().hash(getFile(selectedFilename));
StreamPipesClient client = getStreamPipesClientInstance();
return client.fileApi()
.checkFileContentChanged(selectedFilename, hash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ public String getFiletype() {
public void setFiletype(String filetype) {
this.filetype = filetype;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,38 @@

public class FileManager {

public static List<FileMetadata> getAllFiles() {
private final IFileMetadataStorage fileMetadataStorage;
private final FileHandler fileHandler;
private final FileHasher fileHasher;

public FileManager(IFileMetadataStorage fileMetadataStorage,
FileHandler fileHandler,
FileHasher fileHasher) {
this.fileMetadataStorage = fileMetadataStorage;
this.fileHandler = fileHandler;
this.fileHasher = fileHasher;
}

public FileManager() {
this.fileMetadataStorage = StorageDispatcher
.INSTANCE
.getNoSqlStore()
.getFileMetadataStorage();
this.fileHandler = new FileHandler();
this.fileHasher = new FileHasher();
}

public List<FileMetadata> getAllFiles() {
return getAllFiles(null);
}

public static List<FileMetadata> getAllFiles(String filetypes) {
List<FileMetadata> allFiles = getFileMetadataStorage().getAllFileMetadataDescriptions();
public List<FileMetadata> getAllFiles(String filetypes) {
List<FileMetadata> allFiles = fileMetadataStorage.getAllFileMetadataDescriptions();
return filetypes != null ? filterFiletypes(allFiles, filetypes) : allFiles;
}

public static File getFile(String filename) {
return new FileHandler().getFile(filename);
public File getFile(String filename) {
return fileHandler.getFile(filename);
}

/**
Expand All @@ -56,7 +77,7 @@ public static File getFile(String filename) {
* @param fileInputStream content of file
* @return metadata of file
*/
public static FileMetadata storeFile(String user,
public FileMetadata storeFile(String user,
String filename,
InputStream fileInputStream) throws IOException {

Expand All @@ -72,13 +93,15 @@ public static FileMetadata storeFile(String user,
}


public static void deleteFile(String id) {
FileMetadata fileMetadata = getFileMetadataStorage().getMetadataById(id);
new FileHandler().deleteFile(fileMetadata.getFilename());
getFileMetadataStorage().deleteFileMetadata(id);
public void deleteFile(String id) {
var fileMetadata = fileMetadataStorage.getMetadataById(id);
if (fileMetadata != null) {
fileHandler.deleteFile(fileMetadata.getFilename());
fileMetadataStorage.deleteFileMetadata(id);
}
}

private static InputStream validateFileNameAndCleanFile(String filename,
private InputStream validateFileNameAndCleanFile(String filename,
String filetype,
InputStream fileInputStream) {
if (!validateFileType(filename)) {
Expand All @@ -95,42 +118,34 @@ private static InputStream validateFileNameAndCleanFile(String filename,
* @param filetype file of type
* @return input stream without BOM
*/
protected static InputStream cleanFile(InputStream fileInputStream, String filetype) {
protected InputStream cleanFile(InputStream fileInputStream, String filetype) {
if (Filetypes.CSV.getFileExtensions().contains(filetype.toLowerCase())) {
fileInputStream = new BOMInputStream(fileInputStream);
}

return fileInputStream;
}

public static boolean checkFileContentChanged(String filename, String hash) throws IOException {
var fileHash = FileHasher.hash(getFile(filename));
public boolean checkFileContentChanged(String filename, String hash) throws IOException {
var fileHash = fileHasher.hash(getFile(filename));
return !fileHash.equals(hash);
}

public static String sanitizeFilename(String filename) {
public String sanitizeFilename(String filename) {
return filename.replaceAll("[^a-zA-Z0-9.\\-]", "_");
}

public static boolean validateFileType(String filename) {
public boolean validateFileType(String filename) {
return Filetypes.getAllFileExtensions()
.stream()
.anyMatch(filename::endsWith);
}

protected static void writeToFile(String sanitizedFilename, InputStream fileInputStream) throws IOException {
new FileHandler().storeFile(sanitizedFilename, fileInputStream);
}


private static IFileMetadataStorage getFileMetadataStorage() {
return StorageDispatcher
.INSTANCE
.getNoSqlStore()
.getFileMetadataStorage();
protected void writeToFile(String sanitizedFilename, InputStream fileInputStream) throws IOException {
fileHandler.storeFile(sanitizedFilename, fileInputStream);
}

protected static FileMetadata makeAndStoreFileMetadata(String user,
protected FileMetadata makeAndStoreFileMetadata(String user,
String sanitizedFilename,
String filetype) {
var fileMetadata = makeFileMetadata(user, sanitizedFilename, filetype);
Expand All @@ -139,7 +154,7 @@ protected static FileMetadata makeAndStoreFileMetadata(String user,
return fileMetadata;
}

private static FileMetadata makeFileMetadata(String user,
private FileMetadata makeFileMetadata(String user,
String filename,
String filetype) {

Expand All @@ -152,12 +167,12 @@ private static FileMetadata makeFileMetadata(String user,
return fileMetadata;
}

private static void storeFileMetadata(FileMetadata fileMetadata) {
getFileMetadataStorage().addFileMetadata(fileMetadata);
private void storeFileMetadata(FileMetadata fileMetadata) {
fileMetadataStorage.addFileMetadata(fileMetadata);
}


private static List<FileMetadata> filterFiletypes(List<FileMetadata> allFiles, String filetypes) {
private List<FileMetadata> filterFiletypes(List<FileMetadata> allFiles, String filetypes) {
return allFiles
.stream()
.filter(fileMetadata -> Arrays
Expand Down
Loading

0 comments on commit 34accf4

Please sign in to comment.