Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-pol committed Sep 5, 2024
1 parent 86f6798 commit 699cb98
Showing 1 changed file with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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");
}

}

0 comments on commit 699cb98

Please sign in to comment.