From 699cb985a783991b5ae608faab32a54365119d8d Mon Sep 17 00:00:00 2001 From: jo-pol Date: Thu, 5 Sep 2024 12:53:55 +0200 Subject: [PATCH] wip --- .../command/impl/CreateNewDataFilesTest.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/test/java/edu/harvard/iq/dataverse/engine/command/impl/CreateNewDataFilesTest.java diff --git a/src/test/java/edu/harvard/iq/dataverse/engine/command/impl/CreateNewDataFilesTest.java b/src/test/java/edu/harvard/iq/dataverse/engine/command/impl/CreateNewDataFilesTest.java new file mode 100644 index 00000000000..8da145870c1 --- /dev/null +++ b/src/test/java/edu/harvard/iq/dataverse/engine/command/impl/CreateNewDataFilesTest.java @@ -0,0 +1,74 @@ +package edu.harvard.iq.dataverse.engine.command.impl; + +import edu.harvard.iq.dataverse.Dataset; +import edu.harvard.iq.dataverse.DatasetVersion; +import edu.harvard.iq.dataverse.engine.command.CommandContext; +import edu.harvard.iq.dataverse.engine.command.DataverseRequest; +import edu.harvard.iq.dataverse.engine.command.exception.CommandException; +import edu.harvard.iq.dataverse.settings.JvmSettings; +import edu.harvard.iq.dataverse.storageuse.UploadSessionQuotaLimit; +import edu.harvard.iq.dataverse.util.SystemConfig; +import edu.harvard.iq.dataverse.util.file.CreateDataFileResult; +import edu.harvard.iq.dataverse.util.testing.JvmSetting; +import edu.harvard.iq.dataverse.util.testing.LocalJvmSettings; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static org.apache.commons.io.file.FilesUncheck.createDirectory; +import static org.apache.commons.io.file.PathUtils.deleteDirectory; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +@LocalJvmSettings +public class CreateNewDataFilesTest { + @BeforeEach + public void cleanTmpDir() throws IOException { + var tmpDir = Paths.get("target/test/tmp"); + if(tmpDir.toFile().exists()) + deleteDirectory(tmpDir); + Files.createDirectories(tmpDir); + } + + @Test + @JvmSetting(key = JvmSettings.FILES_DIRECTORY, value = "target/test/tmp") + public void execute_fails_to_upload_when_tmp_does_not_exist() throws FileNotFoundException { + var dsVersion = Mockito.mock(DatasetVersion.class); + Mockito.when(dsVersion.getDataset()).thenReturn(Mockito.mock(Dataset.class)); + + // TODO fix copilot suggestion + JvmSettings mockFilesDirectory = Mockito.mock(JvmSettings.class); + Mockito.when(mockFilesDirectory.lookup()).thenReturn("/mocked/path"); + + var cmd = new CreateNewDataFilesCommand( + Mockito.mock(DataverseRequest.class), + dsVersion, + new FileInputStream("scripts/search/data/shape/shapefile.zip"), + "example.zip", + "application/zip", + null, + new UploadSessionQuotaLimit(1000L, 500L), + "sha"); + + var ctxt = Mockito.mock(CommandContext.class); + var sysCfg = Mockito.mock(SystemConfig.class); + Mockito.when(ctxt.systemConfig()).thenReturn(sysCfg); + Mockito.when(sysCfg.isStorageQuotasEnforced()).thenReturn(true); + + assertThatThrownBy(() -> cmd.execute(ctxt)) + .isInstanceOf(CommandException.class) + .hasMessageContaining("Failed to save the upload as a temp file (temp disk space?)") + .hasRootCauseInstanceOf(NoSuchFileException.class) + .getRootCause() + .hasMessageStartingWith("target/test/tmp/temp/tmp"); + } + +}