Skip to content

Commit

Permalink
refactor(#2708): Add unit test for checkFileContentChanged in FileMan…
Browse files Browse the repository at this point in the history
…ager
  • Loading branch information
tenthe committed Apr 8, 2024
1 parent 49bfded commit 875a1f4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ public class FileManager {

private final IFileMetadataStorage fileMetadataStorage;
private final FileHandler fileHandler;
private final FileHasher fileHasher;

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

public FileManager() {
Expand All @@ -49,6 +52,7 @@ public FileManager() {
.getNoSqlStore()
.getFileMetadataStorage();
this.fileHandler = new FileHandler();
this.fileHasher = new FileHasher();
}

public List<FileMetadata> getAllFiles() {
Expand Down Expand Up @@ -123,7 +127,7 @@ protected InputStream cleanFile(InputStream fileInputStream, String filetype) {
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.streampipes.manager.file;

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

Expand Down Expand Up @@ -51,14 +52,17 @@ public class TestFileManager {
private FileManager fileManager;
private IFileMetadataStorage fileMetadataStorage;
private FileHandler fileHandler;
private FileHasher fileHasher;


private static final String TEST_USER = "testUser";

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

@Test
Expand Down Expand Up @@ -303,4 +307,32 @@ public void validateFileName_returnsTrueForJson() {
public void validateFileName_returnsFalseForSh() {
assertFalse(fileManager.validateFileType("file.sh"));
}

@Test
public void checkFileContentChanged_returnsTrueWhenContentHasChanged() throws IOException {
var filename = "testFile.csv";
var originalHash = "originalHash";
var changedHash = "changedHash";

when(fileHandler.getFile(filename)).thenReturn(new File(filename));
when(fileHasher.hash(any(File.class))).thenReturn(changedHash);

var result = fileManager.checkFileContentChanged(filename, originalHash);

assertTrue(result);
}

@Test
public void checkFileContentChanged_returnsFalseWhenContentHasNotChanged() throws IOException {
var filename = "testFile.csv";
var originalHash = "originalHash";

when(fileHandler.getFile(filename)).thenReturn(new File(filename));
when(fileHasher.hash(any(File.class))).thenReturn(originalHash);

var result = fileManager.checkFileContentChanged(filename, originalHash);

assertFalse(result);
}

}

0 comments on commit 875a1f4

Please sign in to comment.