Commit ced5c4af authored by Viacheslav Pavlov's avatar Viacheslav Pavlov Committed by Matija Obreza

Fix: User can`t delete uploaded file in dataset publisher.

Added Unit tests

- fixed fetching of dataset files list
parent a7b94d2b
......@@ -693,7 +693,7 @@ public class DatasetServiceImpl implements DatasetService {
dataset = getUnpublishedDataset(dataset);
final RepositoryFile repositoryFile = repositoryService.getFile(fileUuid);
dataset.getRepositoryFiles().remove(repositoryFile);
dataset.setRepositoryFiles(dataset.getRepositoryFiles().stream().filter(file -> !file.getUuid().equals(fileUuid)).collect(Collectors.toList()));
repositoryService.removeFile(repositoryFile);
return lazyLoad(datasetRepository.save(dataset));
}
......
......@@ -525,7 +525,7 @@ public class DatasetController extends ApiBaseController {
*/
@GetMapping(value = FILES_URL + "/list")
public List<RepositoryFile> getList(@PathVariable("uuid") final UUID datasetUuid) throws NotFoundElement {
return datasetService.listDatasetFiles(datasetService.getDataset(datasetUuid, null));
return datasetService.listDatasetFiles(datasetService.loadDataset(datasetUuid));
}
/**
......
/*
* Copyright 2019 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.test.server.api.v1;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.service.DatasetService;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.test.base.AbstractApiTest;
import org.genesys.test.base.WithMockOAuth2Authentication;
import org.genesys2.server.api.v1.DatasetController;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;
import org.springframework.security.test.context.support.WithMockUser;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* @author Viacheslav Pavlov
*/
@WithMockUser(username = "user", password = "user", roles = "USER")
public class DatasetFilesControllerTest extends AbstractApiTest {
@Autowired
private DatasetService datasetService;
@After
@Override
public void cleanup() throws Exception {
super.cleanup();
}
@Test
@WithMockOAuth2Authentication(roles = { "ADMINISTRATOR" }, scopes = { "write" })
public void listDatasetCreators() throws Exception {
Dataset dataset = datasetService.createDataset(setUpDataset());
final File file = new File(getClass().getResource("/mcpd20177.csv").getPath());
final FileInputStream fileInputStream = new FileInputStream(file);
final FileInputStream fileInputStream2 = new FileInputStream(file);
final FileInputStream fileInputStream3 = new FileInputStream(file);
final MockMultipartFile mockMultipartFile = new MockMultipartFile("file", file.getName(), "multipart/form-data", fileInputStream);
final MockMultipartFile mockMultipartFile2 = new MockMultipartFile("file", "2" + file.getName(), "multipart/form-data", fileInputStream2);
final MockMultipartFile mockMultipartFile3 = new MockMultipartFile("file", "3" + file.getName(), "multipart/form-data", fileInputStream3);
dataset = datasetService.addDatasetFile(dataset, mockMultipartFile);
dataset = datasetService.addDatasetFile(dataset, mockMultipartFile2);
dataset = datasetService.addDatasetFile(dataset, mockMultipartFile3);
List<RepositoryFile> repositoryFiles = datasetService.listDatasetFiles(dataset);
/*@formatter:off*/
this.mockMvc.perform(RestDocumentationRequestBuilders.get(DatasetController.CONTROLLER_URL + DatasetController.FILES_URL .concat("/list"), dataset.getUuid()))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$[0]", not(nullValue())))
.andExpect(jsonPath("$[0].uuid", is(repositoryFiles.get(0).getUuid().toString())))
.andExpect(jsonPath("$[1]", not(nullValue())))
.andExpect(jsonPath("$[1].uuid", is(repositoryFiles.get(1).getUuid().toString())))
.andExpect(jsonPath("$[2]", not(nullValue())))
.andExpect(jsonPath("$[2].uuid", is(repositoryFiles.get(2).getUuid().toString())));
/*@formatter:on*/
}
@Test
@WithMockOAuth2Authentication(roles = { "ADMINISTRATOR" }, scopes = { "write" })
public void deleteDatasetFile() throws Exception {
Dataset dataset = datasetService.createDataset(setUpDataset());
final File file = new File(getClass().getResource("/mcpd20177.csv").getPath());
final FileInputStream fileInputStream = new FileInputStream(file);
final FileInputStream fileInputStream2 = new FileInputStream(file);
final FileInputStream fileInputStream3 = new FileInputStream(file);
final MockMultipartFile mockMultipartFile = new MockMultipartFile("file", file.getName(), "multipart/form-data", fileInputStream);
final MockMultipartFile mockMultipartFile2 = new MockMultipartFile("file", "2" + file.getName(), "multipart/form-data", fileInputStream2);
final MockMultipartFile mockMultipartFile3 = new MockMultipartFile("file", "3" + file.getName(), "multipart/form-data", fileInputStream3);
dataset = datasetService.addDatasetFile(dataset, mockMultipartFile);
dataset = datasetService.addDatasetFile(dataset, mockMultipartFile2);
dataset = datasetService.addDatasetFile(dataset, mockMultipartFile3);
List<RepositoryFile> repositoryFiles = datasetService.listDatasetFiles(dataset);
assertThat(repositoryFiles.size(), is(3));
mockMvc.perform(RestDocumentationRequestBuilders.delete(DatasetController.CONTROLLER_URL + DatasetController.FILES_URL + "/delete/{fileUuid}", dataset.getUuid(), repositoryFiles.get(0).getUuid())
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.uuid", equalTo(dataset.getUuid().toString())))
.andExpect(jsonPath("$.version", equalTo(dataset.getVersion() + 1)))
.andExpect(jsonPath("$.repositoryFiles", not(nullValue())))
.andExpect(jsonPath("$.repositoryFiles", hasSize(2)));
dataset = datasetService.loadDataset(dataset.getUuid(), dataset.getVersion() + 1);
assertThat(dataset.getRepositoryFiles().size(), is(2));
assertThat(dataset.getRepositoryFiles(), containsInAnyOrder(repositoryFiles.get(1), repositoryFiles.get(2)));
}
}
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