Commit 07edb134 authored by Matija Obreza's avatar Matija Obreza

Fix: remove files from Repository when dataset is deleted

parent aeb3084b
......@@ -460,6 +460,15 @@ public class DatasetServiceImpl implements DatasetService {
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'delete')")
public Dataset removeDataset(final Dataset dataset) {
final Dataset loadedDataset = loadDataset(dataset);
for (RepositoryFile repositoryFile : loadedDataset.getRepositoryFiles()) {
try {
repositoryService.removeFile(repositoryFile);
} catch (NoSuchRepositoryFileException | IOException e) {
LOG.warn("Could not delete file {} from dataset {}", repositoryFile.getUuid(), dataset.getUuid());
}
}
datasetRepository.delete(loadedDataset);
return loadedDataset;
}
......
......@@ -511,6 +511,29 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
assertThat(dataset.getRepositoryFiles().get(0).getVersion(), is(1));
}
@Test
public void testRemoveFilesOnDatasetDelete() throws IOException, InvalidRepositoryFileDataException, NotFoundElement, InvalidRepositoryPathException {
final File file = new File(getClass().getResource("/mcpd20177.csv").getPath());
final FileInputStream fileInputStream = new FileInputStream(file);
final MockMultipartFile mockMultipartFile = new MockMultipartFile("file", file.getName(), "multipart/form-data", fileInputStream);
final Dataset created = buildAndSaveDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, PublishState.DRAFT);
Dataset dataset = datasetService.addDatasetFile(created, mockMultipartFile);
final FileInputStream fileInputStream2 = new FileInputStream(file);
final MockMultipartFile mockMultipartFile2 = new MockMultipartFile("file", "2" + file.getName(), "multipart/form-data", fileInputStream2);
dataset = datasetService.addDatasetFile(dataset, mockMultipartFile2);
assertThat(dataset.getRepositoryFiles(), is(notNullValue()));
assertThat(dataset.getRepositoryFiles().size(), is(2));
assertThat(dataset.getRepositoryFiles().get(0).getVersion(), is(1));
datasetService.removeDataset(dataset);
assertThat(repositoryFilePersistence.count(), is(0l));
}
@Test
public void testUpdateFile() throws IOException, InvalidRepositoryFileDataException, NotFoundElement, InvalidRepositoryPathException, NoSuchRepositoryFileException {
final File file = new File(getClass().getResource("/mcpd20177.csv").getPath());
......@@ -544,6 +567,7 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
assertThat(dataset.getRepositoryFiles().size(), is(0));
assertThat(repositoryFilePersistence.findByUuid(input.getRepositoryFiles().get(0).getUuid()), nullValue());
assertThat(repositoryFilePersistence.count(), is(0l));
}
@Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment