Skip to content

Commit

Permalink
refactor(#2708): Add unit tests for getFile
Browse files Browse the repository at this point in the history
  • Loading branch information
tenthe committed Apr 7, 2024
1 parent 024ff2a commit 7688dd1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,20 @@
public class FileManager {

private final IFileMetadataStorage fileMetadataStorage;
private final FileHandler fileHandler;

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

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

public List<FileMetadata> getAllFiles() {
Expand All @@ -57,7 +61,7 @@ public List<FileMetadata> getAllFiles(String filetypes) {
}

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

/**
Expand Down Expand Up @@ -87,7 +91,7 @@ public FileMetadata storeFile(String user,

public void deleteFile(String id) {
FileMetadata fileMetadata = getFileMetadataStorage().getMetadataById(id);
new FileHandler().deleteFile(fileMetadata.getFilename());
fileHandler.deleteFile(fileMetadata.getFilename());
getFileMetadataStorage().deleteFileMetadata(id);
}

Expand Down Expand Up @@ -132,7 +136,7 @@ public boolean validateFileType(String filename) {
}

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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
Expand All @@ -32,6 +33,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
Expand All @@ -41,11 +43,13 @@ public class TestFileManager {

private FileManager fileManager;
private IFileMetadataStorage fileMetadataStorage;
private FileHandler fileHandler;

@BeforeEach
public void setup() {
fileMetadataStorage = mock(IFileMetadataStorage.class);
fileManager = new FileManager(fileMetadataStorage);
fileHandler = mock(FileHandler.class);
fileManager = new FileManager(fileMetadataStorage, fileHandler);
}

@Test
Expand Down Expand Up @@ -86,18 +90,36 @@ public void getAllFiles_returnsEmptyListWhenNoMatchingFiletypes() {
}

private List<FileMetadata> prepareFileMetadataStorageWithTwoSampleFiles() {
List<FileMetadata> allFiles = Arrays.asList(createFile("csv"), createFile("json"));
List<FileMetadata> allFiles = Arrays.asList(createFileMetadata("csv"), createFileMetadata("json"));
when(fileMetadataStorage.getAllFileMetadataDescriptions()).thenReturn(allFiles);

return allFiles;
}

private FileMetadata createFile(String fileType) {
private FileMetadata createFileMetadata(String fileType) {
FileMetadata file = new FileMetadata();
file.setFiletype(fileType);
return file;
}

@Test
public void getFile_returnsExistingFile() {
var filename = "existingFile.txt";
var expectedFile = new File(filename);
when(fileHandler.getFile(filename)).thenReturn(expectedFile);

var result = fileManager.getFile(filename);

assertEquals(expectedFile, result);
}

@Test
public void getFile_returnsNullForNonExistingFile() {
var filename = "fileDoesNotExist.txt";
when(fileHandler.getFile(filename)).thenReturn(null);

assertNull(fileManager.getFile(filename));
}

@Test
public void storeFile_throwsExceptionForInvalidFileType() {
Expand Down

0 comments on commit 7688dd1

Please sign in to comment.