Add support for temporary files
Support has been added for saving temporary files. In a forthcoming
commit, these temporary files will be utilized for uploading resources
to ChatGPT.
Change-Id: I2757f48d1af97d08318bfa89c14cd4fce70aa3fc
Signed-off-by: Patrizio <patrizio.gelosi@amarulasolutions.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/FileUtils.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/FileUtils.java
index 8e7e934..099e420 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/FileUtils.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/FileUtils.java
@@ -1,13 +1,29 @@
package com.googlesource.gerrit.plugins.chatgpt.utils;
+import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.Objects;
public class FileUtils {
-
public static InputStreamReader getInputStreamReader(String filename) {
return new InputStreamReader(Objects.requireNonNull(
FileUtils.class.getClassLoader().getResourceAsStream(filename)), StandardCharsets.UTF_8);
}
-}
\ No newline at end of file
+
+ public static Path createTempFileWithContent(String prefix, String suffix, String content) {
+ Path tempFile;
+ try {
+ tempFile = Files.createTempFile(prefix, suffix);
+ Files.write(tempFile, content.getBytes(StandardCharsets.UTF_8));
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ tempFile.toFile().deleteOnExit();
+
+ return tempFile;
+ }
+
+}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/chatgpt/TemporaryFileTest.java b/src/test/java/com/googlesource/gerrit/plugins/chatgpt/TemporaryFileTest.java
new file mode 100644
index 0000000..f2bb978
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/chatgpt/TemporaryFileTest.java
@@ -0,0 +1,37 @@
+package com.googlesource.gerrit.plugins.chatgpt;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import com.googlesource.gerrit.plugins.chatgpt.utils.FileUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+public class TemporaryFileTest {
+
+ @Rule
+ public TemporaryFolder tempFolder = new TemporaryFolder();
+
+ @Test
+ public void testCreateTempFileWithContent() throws IOException {
+ // Prepare
+ String prefix = "testFile";
+ String suffix = ".txt";
+ String content = "This is a test content";
+
+ // Execute
+ Path tempFile = FileUtils.createTempFileWithContent(prefix, suffix, content);
+
+ // Verify the file exists
+ assertTrue("Temporary file should exist", Files.exists(tempFile));
+
+ // Read back the content
+ String fileContent = Files.readString(tempFile);
+ assertEquals("File content should match", content, fileContent);
+ }
+
+}