From 875a1f4dc7c356b86422f10cbffe8369f863dea7 Mon Sep 17 00:00:00 2001 From: Philipp Zehnder Date: Mon, 8 Apr 2024 10:54:33 +0200 Subject: [PATCH] refactor(#2708): Add unit test for checkFileContentChanged in FileManager --- .../streampipes/manager/file/FileManager.java | 8 +++-- .../manager/file/TestFileManager.java | 34 ++++++++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/file/FileManager.java b/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/file/FileManager.java index 0259716d50..84bb7f47b7 100644 --- a/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/file/FileManager.java +++ b/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/file/FileManager.java @@ -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() { @@ -49,6 +52,7 @@ public FileManager() { .getNoSqlStore() .getFileMetadataStorage(); this.fileHandler = new FileHandler(); + this.fileHasher = new FileHasher(); } public List getAllFiles() { @@ -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); } diff --git a/streampipes-pipeline-management/src/test/java/org/apache/streampipes/manager/file/TestFileManager.java b/streampipes-pipeline-management/src/test/java/org/apache/streampipes/manager/file/TestFileManager.java index f62fb9a888..da62437bc6 100644 --- a/streampipes-pipeline-management/src/test/java/org/apache/streampipes/manager/file/TestFileManager.java +++ b/streampipes-pipeline-management/src/test/java/org/apache/streampipes/manager/file/TestFileManager.java @@ -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; @@ -51,6 +52,8 @@ public class TestFileManager { private FileManager fileManager; private IFileMetadataStorage fileMetadataStorage; private FileHandler fileHandler; + private FileHasher fileHasher; + private static final String TEST_USER = "testUser"; @@ -58,7 +61,8 @@ public class TestFileManager { 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 @@ -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); + } + } \ No newline at end of file