Skip to content

Commit

Permalink
refactor(#2708): Add unit tests for getAllFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
tenthe committed Apr 7, 2024
1 parent 0ed7c88 commit 024ff2a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,25 @@

public class FileManager {

private final IFileMetadataStorage fileMetadataStorage;

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

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

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

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

Expand Down Expand Up @@ -123,6 +136,7 @@ protected void writeToFile(String sanitizedFilename, InputStream fileInputStream
}


@Deprecated
private IFileMetadataStorage getFileMetadataStorage() {
return StorageDispatcher
.INSTANCE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,88 @@
*/
package org.apache.streampipes.manager.file;

import org.apache.streampipes.model.file.FileMetadata;
import org.apache.streampipes.storage.api.IFileMetadataStorage;

import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class TestFileManager {

private FileManager fileManager;
private IFileMetadataStorage fileMetadataStorage;

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

@Test
public void getAllFiles_returnsAllFiles() {
var expected = prepareFileMetadataStorageWithTwoSampleFiles();

var result = fileManager.getAllFiles();

assertEquals(expected, result);
}

@Test
public void getAllFiles_returnsAllFilesWhenFiletypesIsNull() {
var expected = prepareFileMetadataStorageWithTwoSampleFiles();

var result = fileManager.getAllFiles(null);

assertEquals(expected, result);
}

@Test
public void getAllFiles_returnsFilteredFilesWhenFiletypesIsNotNull() {
var files = prepareFileMetadataStorageWithTwoSampleFiles();

List<FileMetadata> result = fileManager.getAllFiles("csv");

assertEquals(1, result.size());
assertEquals(files.get(0), result.get(0));
}

@Test
public void getAllFiles_returnsEmptyListWhenNoMatchingFiletypes() {
prepareFileMetadataStorageWithTwoSampleFiles();

var result = fileManager.getAllFiles("xml");

assertEquals(0, result.size());
}

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

return allFiles;
}

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


@Test
public void storeFile_throwsExceptionForInvalidFileType() {
var filename = "testFile.invalid";
Expand Down

0 comments on commit 024ff2a

Please sign in to comment.