Skip to content
Snippets Groups Projects
Unverified Commit ebf35de7 authored by Martin Weise's avatar Martin Weise
Browse files

added some metadata mock for frontend, fixed some tests

parent 1b393c6b
Branches
Tags
3 merge requests!81New stable release,!43Merge dev to master,!27Draft: Resolve "Zenodo Sandbox integration for PID (e.g. DOI)"
Showing
with 398 additions and 109 deletions
...@@ -30,7 +30,7 @@ public class FileEndpoint { ...@@ -30,7 +30,7 @@ public class FileEndpoint {
public List<FileResponseDto> listAll(@Valid @RequestParam("id") Long databaseId, public List<FileResponseDto> listAll(@Valid @RequestParam("id") Long databaseId,
@Valid @RequestParam("tableId") Long tableId) @Valid @RequestParam("tableId") Long tableId)
throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoApiException, throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoApiException,
ZenodoNotFoundException { ZenodoNotFoundException, ZenodoUnavailableException {
return fileService.listResources(databaseId, tableId); return fileService.listResources(databaseId, tableId);
} }
...@@ -39,7 +39,7 @@ public class FileEndpoint { ...@@ -39,7 +39,7 @@ public class FileEndpoint {
@Valid @RequestParam("tableId") Long tableId, @Valid @RequestParam("tableId") Long tableId,
@NotBlank @RequestParam("fileId") String fileId) @NotBlank @RequestParam("fileId") String fileId)
throws MetadataDatabaseNotFoundException, ZenodoApiException, ZenodoNotFoundException, throws MetadataDatabaseNotFoundException, ZenodoApiException, ZenodoNotFoundException,
ZenodoAuthenticationException { ZenodoAuthenticationException, ZenodoUnavailableException {
return fileService.findResource(databaseId, tableId, fileId); return fileService.findResource(databaseId, tableId, fileId);
} }
...@@ -49,7 +49,7 @@ public class FileEndpoint { ...@@ -49,7 +49,7 @@ public class FileEndpoint {
@Valid @RequestParam("data") FileUploadDto data, @Valid @RequestParam("data") FileUploadDto data,
@Valid @RequestParam("file") MultipartFile file) @Valid @RequestParam("file") MultipartFile file)
throws MetadataDatabaseNotFoundException, ZenodoApiException, ZenodoFileTooLargeException, throws MetadataDatabaseNotFoundException, ZenodoApiException, ZenodoFileTooLargeException,
ZenodoNotFoundException, ZenodoAuthenticationException { ZenodoNotFoundException, ZenodoAuthenticationException, ZenodoUnavailableException {
return fileService.createResource(databaseId, tableId, data, file); return fileService.createResource(databaseId, tableId, data, file);
} }
......
...@@ -3,10 +3,7 @@ package at.tuwien.endpoints; ...@@ -3,10 +3,7 @@ package at.tuwien.endpoints;
import at.tuwien.api.zenodo.deposit.DepositChangeRequestDto; import at.tuwien.api.zenodo.deposit.DepositChangeRequestDto;
import at.tuwien.api.zenodo.deposit.DepositChangeResponseDto; import at.tuwien.api.zenodo.deposit.DepositChangeResponseDto;
import at.tuwien.api.zenodo.deposit.DepositResponseDto; import at.tuwien.api.zenodo.deposit.DepositResponseDto;
import at.tuwien.exception.MetadataDatabaseNotFoundException; import at.tuwien.exception.*;
import at.tuwien.exception.ZenodoApiException;
import at.tuwien.exception.ZenodoAuthenticationException;
import at.tuwien.exception.ZenodoNotFoundException;
import at.tuwien.service.MetadataService; import at.tuwien.service.MetadataService;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -32,14 +29,14 @@ public class MetadataEndpoint { ...@@ -32,14 +29,14 @@ public class MetadataEndpoint {
@GetMapping @GetMapping
public List<DepositResponseDto> findAll(@Valid @RequestParam("id") Long databaseId, public List<DepositResponseDto> findAll(@Valid @RequestParam("id") Long databaseId,
@Valid @RequestParam("tableId") Long tableId) throws ZenodoApiException, @Valid @RequestParam("tableId") Long tableId) throws ZenodoApiException,
ZenodoAuthenticationException, MetadataDatabaseNotFoundException { ZenodoAuthenticationException, MetadataDatabaseNotFoundException, ZenodoUnavailableException {
return metadataService.listCitations(databaseId, tableId); return metadataService.listCitations(databaseId, tableId);
} }
@PostMapping @PostMapping
public DepositChangeResponseDto create(@Valid @RequestParam("id") Long databaseId, public DepositChangeResponseDto create(@Valid @RequestParam("id") Long databaseId,
@Valid @RequestParam("tableId") Long tableId) throws ZenodoApiException, @Valid @RequestParam("tableId") Long tableId) throws ZenodoApiException,
ZenodoAuthenticationException, MetadataDatabaseNotFoundException { ZenodoAuthenticationException, MetadataDatabaseNotFoundException, ZenodoUnavailableException {
return metadataService.storeCitation(databaseId, tableId); return metadataService.storeCitation(databaseId, tableId);
} }
...@@ -47,7 +44,7 @@ public class MetadataEndpoint { ...@@ -47,7 +44,7 @@ public class MetadataEndpoint {
public DepositResponseDto find(@Valid @RequestParam("id") Long databaseId, public DepositResponseDto find(@Valid @RequestParam("id") Long databaseId,
@Valid @RequestParam("tableId") Long tableId) @Valid @RequestParam("tableId") Long tableId)
throws MetadataDatabaseNotFoundException, ZenodoApiException, ZenodoNotFoundException, throws MetadataDatabaseNotFoundException, ZenodoApiException, ZenodoNotFoundException,
ZenodoAuthenticationException { ZenodoAuthenticationException, ZenodoUnavailableException {
return metadataService.findCitation(databaseId, tableId); return metadataService.findCitation(databaseId, tableId);
} }
...@@ -56,7 +53,7 @@ public class MetadataEndpoint { ...@@ -56,7 +53,7 @@ public class MetadataEndpoint {
@Valid @RequestParam("tableId") Long tableId, @Valid @RequestParam("tableId") Long tableId,
@Valid @RequestBody DepositChangeRequestDto data) @Valid @RequestBody DepositChangeRequestDto data)
throws MetadataDatabaseNotFoundException, ZenodoApiException, ZenodoNotFoundException, throws MetadataDatabaseNotFoundException, ZenodoApiException, ZenodoNotFoundException,
ZenodoAuthenticationException { ZenodoAuthenticationException, ZenodoUnavailableException {
return metadataService.updateCitation(databaseId, tableId, data); return metadataService.updateCitation(databaseId, tableId, data);
} }
...@@ -64,7 +61,7 @@ public class MetadataEndpoint { ...@@ -64,7 +61,7 @@ public class MetadataEndpoint {
public void delete(@Valid @RequestParam("id") Long databaseId, public void delete(@Valid @RequestParam("id") Long databaseId,
@Valid @RequestParam("tableId") Long tableId, @Valid @RequestParam("tableId") Long tableId,
@NotBlank @RequestParam("fileId") String fileId) throws MetadataDatabaseNotFoundException, @NotBlank @RequestParam("fileId") String fileId) throws MetadataDatabaseNotFoundException,
ZenodoApiException, ZenodoAuthenticationException { ZenodoApiException, ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoUnavailableException {
metadataService.deleteCitation(databaseId, tableId); metadataService.deleteCitation(databaseId, tableId);
} }
} }
...@@ -5,10 +5,12 @@ import at.tuwien.api.zenodo.deposit.DepositChangeResponseDto; ...@@ -5,10 +5,12 @@ import at.tuwien.api.zenodo.deposit.DepositChangeResponseDto;
import at.tuwien.api.zenodo.files.FileResponseDto; import at.tuwien.api.zenodo.files.FileResponseDto;
import at.tuwien.api.zenodo.files.FileUploadDto; import at.tuwien.api.zenodo.files.FileUploadDto;
import at.tuwien.config.ReadyConfig; import at.tuwien.config.ReadyConfig;
import at.tuwien.entities.database.Database;
import at.tuwien.entities.database.table.Table; import at.tuwien.entities.database.table.Table;
import at.tuwien.exception.*; import at.tuwien.exception.*;
import at.tuwien.repository.jpa.TableRepository; import at.tuwien.repository.jpa.TableRepository;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -18,6 +20,7 @@ import org.springframework.mock.web.MockMultipartFile; ...@@ -18,6 +20,7 @@ import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
...@@ -41,12 +44,21 @@ public class FileServiceIntegrationTest extends BaseUnitTest { ...@@ -41,12 +44,21 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
@Autowired @Autowired
private ZenodoMetadataService metadataService; private ZenodoMetadataService metadataService;
final Database DATABASE_1 = Database.builder()
.id(DATABASE_1_ID)
.build();
@Test @Test
public void createResource_succeeds() throws IOException, ZenodoApiException, ZenodoNotFoundException, public void createResource_succeeds() throws IOException, ZenodoApiException, ZenodoNotFoundException,
ZenodoAuthenticationException, ZenodoFileTooLargeException, MetadataDatabaseNotFoundException { ZenodoAuthenticationException, ZenodoFileTooLargeException, MetadataDatabaseNotFoundException,
ZenodoUnavailableException {
final MockMultipartFile file = new MockMultipartFile("testdata.csv", FileUtils.readFileToByteArray( final MockMultipartFile file = new MockMultipartFile("testdata.csv", FileUtils.readFileToByteArray(
ResourceUtils.getFile("classpath:csv/testdata.csv"))); ResourceUtils.getFile("classpath:csv/testdata.csv")));
/* mock */
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* request */ /* request */
final DepositChangeResponseDto deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID); final DepositChangeResponseDto deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID);
final FileUploadDto request = FileUploadDto.builder() final FileUploadDto request = FileUploadDto.builder()
...@@ -58,8 +70,6 @@ public class FileServiceIntegrationTest extends BaseUnitTest { ...@@ -58,8 +70,6 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
.id(TABLE_1_ID) .id(TABLE_1_ID)
.depositId(deposit.getId()) .depositId(deposit.getId())
.build(); .build();
when(tableRepository.findById(TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* test */ /* test */
final FileResponseDto response = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, request, file); final FileResponseDto response = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, request, file);
...@@ -69,11 +79,17 @@ public class FileServiceIntegrationTest extends BaseUnitTest { ...@@ -69,11 +79,17 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
} }
@Test @Test
@Disabled("slow internet")
public void createResource_largeFile_succeeds() throws IOException, ZenodoApiException, ZenodoNotFoundException, public void createResource_largeFile_succeeds() throws IOException, ZenodoApiException, ZenodoNotFoundException,
ZenodoAuthenticationException, ZenodoFileTooLargeException, MetadataDatabaseNotFoundException { ZenodoAuthenticationException, ZenodoFileTooLargeException, MetadataDatabaseNotFoundException,
ZenodoUnavailableException {
final MockMultipartFile file = new MockMultipartFile("weatherAUS.csv", FileUtils.readFileToByteArray( final MockMultipartFile file = new MockMultipartFile("weatherAUS.csv", FileUtils.readFileToByteArray(
ResourceUtils.getFile("classpath:csv/weatherAUS.csv"))); ResourceUtils.getFile("classpath:csv/weatherAUS.csv")));
/* mock */
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* request */ /* request */
final DepositChangeResponseDto deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID); final DepositChangeResponseDto deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID);
final FileUploadDto request = FileUploadDto.builder() final FileUploadDto request = FileUploadDto.builder()
...@@ -85,8 +101,6 @@ public class FileServiceIntegrationTest extends BaseUnitTest { ...@@ -85,8 +101,6 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
.id(TABLE_1_ID) .id(TABLE_1_ID)
.depositId(deposit.getId()) .depositId(deposit.getId())
.build(); .build();
when(tableRepository.findById(TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* test */ /* test */
final FileResponseDto response = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, request, file); final FileResponseDto response = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, request, file);
...@@ -97,9 +111,12 @@ public class FileServiceIntegrationTest extends BaseUnitTest { ...@@ -97,9 +111,12 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
@Test @Test
public void listAll_notFound_fails() { public void listAll_notFound_fails() {
final Table TABLE_1 = Table.builder()
.id(-1L)
.build();
/* mock */ /* mock */
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
/* test */ /* test */
...@@ -110,10 +127,15 @@ public class FileServiceIntegrationTest extends BaseUnitTest { ...@@ -110,10 +127,15 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
@Test @Test
public void listAll_succeeds() throws MetadataDatabaseNotFoundException, ZenodoApiException, public void listAll_succeeds() throws MetadataDatabaseNotFoundException, ZenodoApiException,
ZenodoNotFoundException, ZenodoAuthenticationException, IOException, ZenodoFileTooLargeException { ZenodoNotFoundException, ZenodoAuthenticationException, IOException, ZenodoFileTooLargeException,
ZenodoUnavailableException {
final MockMultipartFile file = new MockMultipartFile("testdata.csv", FileUtils.readFileToByteArray( final MockMultipartFile file = new MockMultipartFile("testdata.csv", FileUtils.readFileToByteArray(
ResourceUtils.getFile("classpath:csv/testdata.csv"))); ResourceUtils.getFile("classpath:csv/testdata.csv")));
/* mock */
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* request */ /* request */
final DepositChangeResponseDto deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID); final DepositChangeResponseDto deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID);
final FileUploadDto upload = FileUploadDto.builder() final FileUploadDto upload = FileUploadDto.builder()
...@@ -123,8 +145,6 @@ public class FileServiceIntegrationTest extends BaseUnitTest { ...@@ -123,8 +145,6 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
.id(TABLE_1_ID) .id(TABLE_1_ID)
.depositId(deposit.getId()) .depositId(deposit.getId())
.build(); .build();
when(tableRepository.findById(TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
final FileResponseDto fileResponse = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, upload, file); final FileResponseDto fileResponse = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, upload, file);
/* mock */ /* mock */
...@@ -140,10 +160,15 @@ public class FileServiceIntegrationTest extends BaseUnitTest { ...@@ -140,10 +160,15 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
@Test @Test
public void findResource_noContent_fails() throws MetadataDatabaseNotFoundException, ZenodoApiException, public void findResource_noContent_fails() throws MetadataDatabaseNotFoundException, ZenodoApiException,
ZenodoFileTooLargeException, ZenodoNotFoundException, ZenodoAuthenticationException, IOException { ZenodoFileTooLargeException, ZenodoNotFoundException, ZenodoAuthenticationException,
ZenodoUnavailableException, IOException {
final MockMultipartFile file = new MockMultipartFile("testdata.csv", FileUtils.readFileToByteArray( final MockMultipartFile file = new MockMultipartFile("testdata.csv", FileUtils.readFileToByteArray(
ResourceUtils.getFile("classpath:csv/testdata.csv"))); ResourceUtils.getFile("classpath:csv/testdata.csv")));
/* mock */
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* request */ /* request */
final DepositChangeResponseDto deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID); final DepositChangeResponseDto deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID);
final FileUploadDto upload = FileUploadDto.builder() final FileUploadDto upload = FileUploadDto.builder()
...@@ -153,8 +178,6 @@ public class FileServiceIntegrationTest extends BaseUnitTest { ...@@ -153,8 +178,6 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
.id(TABLE_1_ID) .id(TABLE_1_ID)
.depositId(deposit.getId()) .depositId(deposit.getId())
.build(); .build();
when(tableRepository.findById(TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
final FileResponseDto fileResponse = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, upload, file); final FileResponseDto fileResponse = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, upload, file);
/* mock */ /* mock */
...@@ -169,10 +192,15 @@ public class FileServiceIntegrationTest extends BaseUnitTest { ...@@ -169,10 +192,15 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
@Test @Test
public void deleteRessource_succeeds() throws MetadataDatabaseNotFoundException, ZenodoApiException, public void deleteRessource_succeeds() throws MetadataDatabaseNotFoundException, ZenodoApiException,
ZenodoFileTooLargeException, ZenodoNotFoundException, ZenodoAuthenticationException, IOException { ZenodoFileTooLargeException, ZenodoNotFoundException, ZenodoAuthenticationException, IOException,
ZenodoUnavailableException {
final MockMultipartFile file = new MockMultipartFile("testdata.csv", FileUtils.readFileToByteArray( final MockMultipartFile file = new MockMultipartFile("testdata.csv", FileUtils.readFileToByteArray(
ResourceUtils.getFile("classpath:csv/testdata.csv"))); ResourceUtils.getFile("classpath:csv/testdata.csv")));
/* mock */
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* request */ /* request */
final DepositChangeResponseDto deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID); final DepositChangeResponseDto deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID);
final FileUploadDto upload = FileUploadDto.builder() final FileUploadDto upload = FileUploadDto.builder()
...@@ -182,8 +210,6 @@ public class FileServiceIntegrationTest extends BaseUnitTest { ...@@ -182,8 +210,6 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
.id(TABLE_1_ID) .id(TABLE_1_ID)
.depositId(deposit.getId()) .depositId(deposit.getId())
.build(); .build();
when(tableRepository.findById(TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
final FileResponseDto fileResponse = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, upload, file); final FileResponseDto fileResponse = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, upload, file);
/* mock */ /* mock */
......
...@@ -4,6 +4,7 @@ import at.tuwien.BaseUnitTest; ...@@ -4,6 +4,7 @@ import at.tuwien.BaseUnitTest;
import at.tuwien.api.zenodo.files.FileResponseDto; import at.tuwien.api.zenodo.files.FileResponseDto;
import at.tuwien.api.zenodo.files.FileUploadDto; import at.tuwien.api.zenodo.files.FileUploadDto;
import at.tuwien.config.ReadyConfig; import at.tuwien.config.ReadyConfig;
import at.tuwien.entities.database.Database;
import at.tuwien.exception.*; import at.tuwien.exception.*;
import at.tuwien.repository.jpa.TableRepository; import at.tuwien.repository.jpa.TableRepository;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
...@@ -21,6 +22,7 @@ import org.springframework.mock.web.MockMultipartFile; ...@@ -21,6 +22,7 @@ import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.io.IOException; import java.io.IOException;
...@@ -29,8 +31,8 @@ import java.util.Optional; ...@@ -29,8 +31,8 @@ import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.*;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@SpringBootTest @SpringBootTest
...@@ -49,9 +51,14 @@ public class FileServiceUnitTest extends BaseUnitTest { ...@@ -49,9 +51,14 @@ public class FileServiceUnitTest extends BaseUnitTest {
@MockBean @MockBean
private TableRepository tableRepository; private TableRepository tableRepository;
final Database DATABASE_1 = Database.builder()
.id(DATABASE_1_ID)
.build();
@Test @Test
public void createResource_succeeds() throws IOException, ZenodoApiException, ZenodoNotFoundException, public void createResource_succeeds() throws IOException, ZenodoApiException, ZenodoNotFoundException,
ZenodoAuthenticationException, ZenodoFileTooLargeException, MetadataDatabaseNotFoundException { ZenodoAuthenticationException, ZenodoFileTooLargeException, MetadataDatabaseNotFoundException,
ZenodoUnavailableException {
final MockMultipartFile file = new MockMultipartFile("testdata.csv", FileUtils.readFileToByteArray( final MockMultipartFile file = new MockMultipartFile("testdata.csv", FileUtils.readFileToByteArray(
ResourceUtils.getFile("classpath:csv/testdata.csv"))); ResourceUtils.getFile("classpath:csv/testdata.csv")));
...@@ -60,7 +67,7 @@ public class FileServiceUnitTest extends BaseUnitTest { ...@@ -60,7 +67,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
eq(FileResponseDto.class), eq(DEPOSIT_1_ID), anyString())) eq(FileResponseDto.class), eq(DEPOSIT_1_ID), anyString()))
.thenReturn(ResponseEntity.status(HttpStatus.OK) .thenReturn(ResponseEntity.status(HttpStatus.OK)
.body(FILE_1)); .body(FILE_1));
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
/* request */ /* request */
...@@ -75,6 +82,30 @@ public class FileServiceUnitTest extends BaseUnitTest { ...@@ -75,6 +82,30 @@ public class FileServiceUnitTest extends BaseUnitTest {
assertEquals(FILE_1_SIZE, response.getFilesize()); assertEquals(FILE_1_SIZE, response.getFilesize());
} }
@Test
public void createResource_unavailable_fails() throws IOException {
final MockMultipartFile file = new MockMultipartFile("testdata.csv", FileUtils.readFileToByteArray(
ResourceUtils.getFile("classpath:csv/testdata.csv")));
/* mock */
doThrow(ResourceAccessException.class)
.when(apiTemplate)
.postForEntity(anyString(), Mockito.<MultiValueMap<String, HttpEntity<?>>>any(),
eq(FileResponseDto.class), eq(DEPOSIT_1_ID), anyString());
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* request */
final FileUploadDto request = FileUploadDto.builder()
.name(FILE_1_NAME)
.build();
/* test */
assertThrows(ZenodoUnavailableException.class, () -> {
fileService.createResource(DATABASE_1_ID, TABLE_1_ID, request, file);
});
}
@Test @Test
public void createResource_notExists_fails() throws IOException { public void createResource_notExists_fails() throws IOException {
final MockMultipartFile file = new MockMultipartFile("testdata.csv", FileUtils.readFileToByteArray( final MockMultipartFile file = new MockMultipartFile("testdata.csv", FileUtils.readFileToByteArray(
...@@ -85,7 +116,7 @@ public class FileServiceUnitTest extends BaseUnitTest { ...@@ -85,7 +116,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
eq(FileResponseDto.class), eq(DEPOSIT_1_ID), anyString())) eq(FileResponseDto.class), eq(DEPOSIT_1_ID), anyString()))
.thenReturn(ResponseEntity.status(HttpStatus.BAD_REQUEST) .thenReturn(ResponseEntity.status(HttpStatus.BAD_REQUEST)
.build()); .build());
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
/* request */ /* request */
...@@ -108,7 +139,7 @@ public class FileServiceUnitTest extends BaseUnitTest { ...@@ -108,7 +139,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
when(apiTemplate.postForEntity(anyString(), Mockito.<MultiValueMap<String, HttpEntity<?>>>any(), when(apiTemplate.postForEntity(anyString(), Mockito.<MultiValueMap<String, HttpEntity<?>>>any(),
eq(FileResponseDto.class), eq(DEPOSIT_1_ID), anyString())) eq(FileResponseDto.class), eq(DEPOSIT_1_ID), anyString()))
.thenReturn(ResponseEntity.accepted().body(null)); .thenReturn(ResponseEntity.accepted().body(null));
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
/* request */ /* request */
...@@ -124,13 +155,13 @@ public class FileServiceUnitTest extends BaseUnitTest { ...@@ -124,13 +155,13 @@ public class FileServiceUnitTest extends BaseUnitTest {
@Test @Test
public void listAll_succeeds() throws MetadataDatabaseNotFoundException, ZenodoApiException, public void listAll_succeeds() throws MetadataDatabaseNotFoundException, ZenodoApiException,
ZenodoNotFoundException, ZenodoAuthenticationException { ZenodoNotFoundException, ZenodoAuthenticationException, ZenodoUnavailableException {
/* mock */ /* mock */
when(apiTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(FileResponseDto[].class), when(apiTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(FileResponseDto[].class),
eq(DEPOSIT_1_ID), anyString())) eq(DEPOSIT_1_ID), anyString()))
.thenReturn(ResponseEntity.ok().body(new FileResponseDto[]{FILE_1})); .thenReturn(ResponseEntity.ok().body(new FileResponseDto[]{FILE_1}));
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
/* test */ /* test */
...@@ -138,6 +169,23 @@ public class FileServiceUnitTest extends BaseUnitTest { ...@@ -138,6 +169,23 @@ public class FileServiceUnitTest extends BaseUnitTest {
assertEquals(1, response.size()); assertEquals(1, response.size());
} }
@Test
public void listAll_unavailable_fails() {
/* mock */
doThrow(ResourceAccessException.class)
.when(apiTemplate)
.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(FileResponseDto[].class),
eq(DEPOSIT_1_ID), anyString());
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* test */
assertThrows(ZenodoUnavailableException.class, () -> {
fileService.listResources(DATABASE_1_ID, TABLE_1_ID);
});
}
@Test @Test
public void listAll_noContent_fails() { public void listAll_noContent_fails() {
...@@ -145,7 +193,7 @@ public class FileServiceUnitTest extends BaseUnitTest { ...@@ -145,7 +193,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
when(apiTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(FileResponseDto[].class), when(apiTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(FileResponseDto[].class),
eq(DEPOSIT_1_ID), anyString())) eq(DEPOSIT_1_ID), anyString()))
.thenReturn(ResponseEntity.ok().body(null)); .thenReturn(ResponseEntity.ok().body(null));
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
/* test */ /* test */
...@@ -158,7 +206,7 @@ public class FileServiceUnitTest extends BaseUnitTest { ...@@ -158,7 +206,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
public void listAll_notFound_fails() { public void listAll_notFound_fails() {
/* mock */ /* mock */
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
/* test */ /* test */
...@@ -169,13 +217,13 @@ public class FileServiceUnitTest extends BaseUnitTest { ...@@ -169,13 +217,13 @@ public class FileServiceUnitTest extends BaseUnitTest {
@Test @Test
public void findResource_succeeds() throws MetadataDatabaseNotFoundException, ZenodoApiException, public void findResource_succeeds() throws MetadataDatabaseNotFoundException, ZenodoApiException,
ZenodoNotFoundException, ZenodoAuthenticationException { ZenodoNotFoundException, ZenodoAuthenticationException, ZenodoUnavailableException {
/* mock */ /* mock */
when(apiTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(FileResponseDto.class), when(apiTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(FileResponseDto.class),
eq(DEPOSIT_1_ID), eq(FILE_1_ID), anyString())) eq(DEPOSIT_1_ID), eq(FILE_1_ID), anyString()))
.thenReturn(ResponseEntity.ok().body(FILE_1)); .thenReturn(ResponseEntity.ok().body(FILE_1));
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
/* test */ /* test */
...@@ -186,6 +234,23 @@ public class FileServiceUnitTest extends BaseUnitTest { ...@@ -186,6 +234,23 @@ public class FileServiceUnitTest extends BaseUnitTest {
assertEquals(FILE_1_CHECKSUM, file.getChecksum()); assertEquals(FILE_1_CHECKSUM, file.getChecksum());
} }
@Test
public void findResource_unavailable_fails() {
/* mock */
doThrow(ResourceAccessException.class)
.when(apiTemplate)
.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(FileResponseDto.class),
eq(DEPOSIT_1_ID), eq(FILE_1_ID), anyString());
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* test */
assertThrows(ZenodoUnavailableException.class, () -> {
fileService.findResource(DATABASE_1_ID, TABLE_1_ID, FILE_1_ID);
});
}
@Test @Test
public void findResource_noContent_fails() { public void findResource_noContent_fails() {
...@@ -193,7 +258,7 @@ public class FileServiceUnitTest extends BaseUnitTest { ...@@ -193,7 +258,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
when(apiTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(FileResponseDto.class), when(apiTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(FileResponseDto.class),
eq(DEPOSIT_1_ID), eq(FILE_1_ID), anyString())) eq(DEPOSIT_1_ID), eq(FILE_1_ID), anyString()))
.thenReturn(ResponseEntity.ok().body(null)); .thenReturn(ResponseEntity.ok().body(null));
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
/* test */ /* test */
...@@ -206,7 +271,7 @@ public class FileServiceUnitTest extends BaseUnitTest { ...@@ -206,7 +271,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
public void findResource_notFound_fails() { public void findResource_notFound_fails() {
/* mock */ /* mock */
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.empty()); .thenReturn(Optional.empty());
/* test */ /* test */
...@@ -217,17 +282,34 @@ public class FileServiceUnitTest extends BaseUnitTest { ...@@ -217,17 +282,34 @@ public class FileServiceUnitTest extends BaseUnitTest {
@Test @Test
public void deleteResource_succeeds() throws MetadataDatabaseNotFoundException, ZenodoApiException, public void deleteResource_succeeds() throws MetadataDatabaseNotFoundException, ZenodoApiException,
ZenodoNotFoundException, ZenodoAuthenticationException { ZenodoNotFoundException, ZenodoAuthenticationException, ZenodoUnavailableException {
/* mock */ /* mock */
when(apiTemplate.exchange(anyString(), eq(HttpMethod.DELETE), Mockito.any(), eq(String.class), when(apiTemplate.exchange(anyString(), eq(HttpMethod.DELETE), Mockito.any(), eq(String.class),
eq(DEPOSIT_1_ID), eq(FILE_1_ID), anyString())) eq(DEPOSIT_1_ID), eq(FILE_1_ID), anyString()))
.thenReturn(ResponseEntity.status(HttpStatus.NO_CONTENT).build()); .thenReturn(ResponseEntity.status(HttpStatus.NO_CONTENT).build());
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* test */
fileService.deleteResource(DATABASE_1_ID, TABLE_1_ID, FILE_1_ID);
}
@Test
public void deleteResource_unavailable_fails() {
/* mock */
doThrow(ResourceAccessException.class)
.when(apiTemplate)
.exchange(anyString(), eq(HttpMethod.DELETE), Mockito.any(), eq(String.class),
eq(DEPOSIT_1_ID), eq(FILE_1_ID), anyString());
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
/* test */ /* test */
assertThrows(ZenodoUnavailableException.class, () -> {
fileService.deleteResource(DATABASE_1_ID, TABLE_1_ID, FILE_1_ID); fileService.deleteResource(DATABASE_1_ID, TABLE_1_ID, FILE_1_ID);
});
} }
@Test @Test
...@@ -237,7 +319,7 @@ public class FileServiceUnitTest extends BaseUnitTest { ...@@ -237,7 +319,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
when(apiTemplate.exchange(anyString(), eq(HttpMethod.DELETE), Mockito.any(), eq(String.class), when(apiTemplate.exchange(anyString(), eq(HttpMethod.DELETE), Mockito.any(), eq(String.class),
eq(DEPOSIT_1_ID), eq(FILE_1_ID), anyString())) eq(DEPOSIT_1_ID), eq(FILE_1_ID), anyString()))
.thenReturn(ResponseEntity.status(HttpStatus.OK).build()); .thenReturn(ResponseEntity.status(HttpStatus.OK).build());
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
/* test */ /* test */
......
...@@ -3,11 +3,9 @@ package at.tuwien.service; ...@@ -3,11 +3,9 @@ package at.tuwien.service;
import at.tuwien.BaseUnitTest; import at.tuwien.BaseUnitTest;
import at.tuwien.api.zenodo.deposit.*; import at.tuwien.api.zenodo.deposit.*;
import at.tuwien.config.ReadyConfig; import at.tuwien.config.ReadyConfig;
import at.tuwien.entities.database.Database;
import at.tuwien.entities.database.table.Table; import at.tuwien.entities.database.table.Table;
import at.tuwien.exception.MetadataDatabaseNotFoundException; import at.tuwien.exception.*;
import at.tuwien.exception.ZenodoApiException;
import at.tuwien.exception.ZenodoAuthenticationException;
import at.tuwien.exception.ZenodoNotFoundException;
import at.tuwien.repository.jpa.TableRepository; import at.tuwien.repository.jpa.TableRepository;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -34,8 +32,17 @@ public class MetadataServiceIntegrationTest extends BaseUnitTest { ...@@ -34,8 +32,17 @@ public class MetadataServiceIntegrationTest extends BaseUnitTest {
@MockBean @MockBean
private TableRepository tableRepository; private TableRepository tableRepository;
final Database DATABASE_1 = Database.builder()
.id(DATABASE_1_ID)
.build();
@Test @Test
public void listDeposit_succeeds() throws ZenodoApiException, ZenodoAuthenticationException { public void listDeposit_succeeds() throws ZenodoApiException, ZenodoAuthenticationException,
ZenodoUnavailableException {
/* mock */
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* test */ /* test */
zenodoService.listCitations(DATABASE_1_ID, TABLE_1_ID); zenodoService.listCitations(DATABASE_1_ID, TABLE_1_ID);
...@@ -43,7 +50,11 @@ public class MetadataServiceIntegrationTest extends BaseUnitTest { ...@@ -43,7 +50,11 @@ public class MetadataServiceIntegrationTest extends BaseUnitTest {
@Test @Test
public void createDeposit_succeeds() throws ZenodoApiException, ZenodoAuthenticationException, public void createDeposit_succeeds() throws ZenodoApiException, ZenodoAuthenticationException,
MetadataDatabaseNotFoundException { MetadataDatabaseNotFoundException, ZenodoUnavailableException {
/* mock */
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* test */ /* test */
final DepositChangeResponseDto response = zenodoService.storeCitation(DATABASE_1_ID, TABLE_1_ID); final DepositChangeResponseDto response = zenodoService.storeCitation(DATABASE_1_ID, TABLE_1_ID);
...@@ -52,7 +63,11 @@ public class MetadataServiceIntegrationTest extends BaseUnitTest { ...@@ -52,7 +63,11 @@ public class MetadataServiceIntegrationTest extends BaseUnitTest {
@Test @Test
public void updateDeposit_succeeds() throws ZenodoApiException, ZenodoAuthenticationException, public void updateDeposit_succeeds() throws ZenodoApiException, ZenodoAuthenticationException,
ZenodoNotFoundException, MetadataDatabaseNotFoundException { ZenodoNotFoundException, MetadataDatabaseNotFoundException, ZenodoUnavailableException {
/* mock */
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
final DepositChangeResponseDto deposit = zenodoService.storeCitation(DATABASE_1_ID, TABLE_1_ID); final DepositChangeResponseDto deposit = zenodoService.storeCitation(DATABASE_1_ID, TABLE_1_ID);
final DepositChangeRequestDto request = DepositChangeRequestDto.builder() final DepositChangeRequestDto request = DepositChangeRequestDto.builder()
.metadata(METADATA_1) .metadata(METADATA_1)
...@@ -63,8 +78,6 @@ public class MetadataServiceIntegrationTest extends BaseUnitTest { ...@@ -63,8 +78,6 @@ public class MetadataServiceIntegrationTest extends BaseUnitTest {
.id(TABLE_1_ID) .id(TABLE_1_ID)
.depositId(deposit.getId()) .depositId(deposit.getId())
.build(); .build();
when(tableRepository.findById(TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* test */ /* test */
final DepositChangeResponseDto response2 = zenodoService.updateCitation(DATABASE_1_ID, TABLE_1_ID, request); final DepositChangeResponseDto response2 = zenodoService.updateCitation(DATABASE_1_ID, TABLE_1_ID, request);
......
...@@ -6,10 +6,8 @@ import at.tuwien.api.zenodo.deposit.DepositResponseDto; ...@@ -6,10 +6,8 @@ import at.tuwien.api.zenodo.deposit.DepositResponseDto;
import at.tuwien.api.zenodo.deposit.DepositChangeRequestDto; import at.tuwien.api.zenodo.deposit.DepositChangeRequestDto;
import at.tuwien.api.zenodo.deposit.MetadataDto; import at.tuwien.api.zenodo.deposit.MetadataDto;
import at.tuwien.config.ReadyConfig; import at.tuwien.config.ReadyConfig;
import at.tuwien.exception.MetadataDatabaseNotFoundException; import at.tuwien.entities.database.Database;
import at.tuwien.exception.ZenodoApiException; import at.tuwien.exception.*;
import at.tuwien.exception.ZenodoAuthenticationException;
import at.tuwien.exception.ZenodoNotFoundException;
import at.tuwien.repository.jpa.TableRepository; import at.tuwien.repository.jpa.TableRepository;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
...@@ -22,6 +20,7 @@ import org.springframework.http.HttpMethod; ...@@ -22,6 +20,7 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.util.List; import java.util.List;
...@@ -49,8 +48,13 @@ public class MetadataServiceUnitTest extends BaseUnitTest { ...@@ -49,8 +48,13 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
@MockBean @MockBean
private TableRepository tableRepository; private TableRepository tableRepository;
final Database DATABASE_1 = Database.builder()
.id(DATABASE_1_ID)
.build();
@Test @Test
public void listCitations_succeeds() throws ZenodoApiException, ZenodoAuthenticationException { public void listCitations_succeeds() throws ZenodoApiException, ZenodoAuthenticationException,
ZenodoUnavailableException {
/* mocks */ /* mocks */
when(apiTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(DepositResponseDto[].class), when(apiTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(DepositResponseDto[].class),
...@@ -62,6 +66,21 @@ public class MetadataServiceUnitTest extends BaseUnitTest { ...@@ -62,6 +66,21 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
assertEquals(1, response.size()); assertEquals(1, response.size());
} }
@Test
public void listCitations_unavailable_fails() {
/* mocks */
doThrow(ResourceAccessException.class)
.when(apiTemplate)
.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(DepositResponseDto[].class),
anyString());
/* test */
assertThrows(ZenodoUnavailableException.class, () -> {
zenodoService.listCitations(DATABASE_1_ID, TABLE_1_ID);
});
}
@Test @Test
public void listCitations_empty_fails() { public void listCitations_empty_fails() {
...@@ -78,9 +97,11 @@ public class MetadataServiceUnitTest extends BaseUnitTest { ...@@ -78,9 +97,11 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
@Test @Test
public void storeCitation_succeed() throws ZenodoApiException, ZenodoAuthenticationException, public void storeCitation_succeed() throws ZenodoApiException, ZenodoAuthenticationException,
MetadataDatabaseNotFoundException { MetadataDatabaseNotFoundException, ZenodoUnavailableException {
/* mocks */ /* mocks */
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
when(apiTemplate.exchange(anyString(), eq(HttpMethod.POST), Mockito.<HttpEntity<String>>any(), eq(DepositChangeResponseDto.class), when(apiTemplate.exchange(anyString(), eq(HttpMethod.POST), Mockito.<HttpEntity<String>>any(), eq(DepositChangeResponseDto.class),
anyString())) anyString()))
.thenReturn(ResponseEntity.status(HttpStatus.CREATED) .thenReturn(ResponseEntity.status(HttpStatus.CREATED)
...@@ -92,12 +113,29 @@ public class MetadataServiceUnitTest extends BaseUnitTest { ...@@ -92,12 +113,29 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
assertEquals(DEPOSIT_1_MODIFIED, response.getModified()); assertEquals(DEPOSIT_1_MODIFIED, response.getModified());
} }
@Test
public void storeCitation_unavailable_fails() {
/* mocks */
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
doThrow(ResourceAccessException.class)
.when(apiTemplate)
.exchange(anyString(), eq(HttpMethod.POST), Mockito.<HttpEntity<String>>any(), eq(DepositChangeResponseDto.class),
anyString());
/* test */
assertThrows(ZenodoUnavailableException.class, () -> {
zenodoService.storeCitation(DATABASE_1_ID, TABLE_1_ID);
});
}
@Test @Test
public void deleteCitation_succeeds() throws ZenodoApiException, ZenodoAuthenticationException, public void deleteCitation_succeeds() throws ZenodoApiException, ZenodoAuthenticationException,
MetadataDatabaseNotFoundException { MetadataDatabaseNotFoundException, ZenodoNotFoundException, ZenodoUnavailableException {
/* mocks */ /* mocks */
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
when(apiTemplate.exchange(anyString(), eq(HttpMethod.DELETE), Mockito.any(), eq(String.class), anyLong(), when(apiTemplate.exchange(anyString(), eq(HttpMethod.DELETE), Mockito.any(), eq(String.class), anyLong(),
anyString())) anyString()))
...@@ -108,11 +146,28 @@ public class MetadataServiceUnitTest extends BaseUnitTest { ...@@ -108,11 +146,28 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
zenodoService.deleteCitation(DATABASE_1_ID, TABLE_1_ID); zenodoService.deleteCitation(DATABASE_1_ID, TABLE_1_ID);
} }
@Test
public void deleteCitation_unavailable_fails() {
/* mocks */
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
doThrow(ResourceAccessException.class)
.when(apiTemplate)
.exchange(anyString(), eq(HttpMethod.DELETE), Mockito.any(), eq(String.class), anyLong(),
anyString());
/* test */
assertThrows(ZenodoUnavailableException.class, () -> {
zenodoService.deleteCitation(DATABASE_1_ID, TABLE_1_ID);
});
}
@Test @Test
public void deleteCitation_fails() { public void deleteCitation_fails() {
/* mocks */ /* mocks */
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
when(apiTemplate.exchange(anyString(), eq(HttpMethod.DELETE), Mockito.any(), eq(String.class), anyLong(), when(apiTemplate.exchange(anyString(), eq(HttpMethod.DELETE), Mockito.any(), eq(String.class), anyLong(),
anyString())) anyString()))
...@@ -127,10 +182,10 @@ public class MetadataServiceUnitTest extends BaseUnitTest { ...@@ -127,10 +182,10 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
@Test @Test
public void updateCitation_succeeds() throws ZenodoApiException, ZenodoAuthenticationException, public void updateCitation_succeeds() throws ZenodoApiException, ZenodoAuthenticationException,
ZenodoNotFoundException, MetadataDatabaseNotFoundException { ZenodoNotFoundException, MetadataDatabaseNotFoundException, ZenodoUnavailableException {
/* mocks */ /* mocks */
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
when(apiTemplate.exchange(anyString(), eq(HttpMethod.PUT), Mockito.<HttpEntity<DepositChangeRequestDto>>any(), eq(DepositChangeResponseDto.class), when(apiTemplate.exchange(anyString(), eq(HttpMethod.PUT), Mockito.<HttpEntity<DepositChangeRequestDto>>any(), eq(DepositChangeResponseDto.class),
eq(DEPOSIT_1_ID), anyString())) eq(DEPOSIT_1_ID), anyString()))
...@@ -146,11 +201,33 @@ public class MetadataServiceUnitTest extends BaseUnitTest { ...@@ -146,11 +201,33 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
zenodoService.updateCitation(DATABASE_1_ID, TABLE_1_ID, request); zenodoService.updateCitation(DATABASE_1_ID, TABLE_1_ID, request);
} }
@Test
public void updateCitation_unavailable_fails() {
/* mocks */
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
doThrow(ResourceAccessException.class)
.when(apiTemplate)
.exchange(anyString(), eq(HttpMethod.PUT), Mockito.<HttpEntity<DepositChangeRequestDto>>any(), eq(DepositChangeResponseDto.class),
eq(DEPOSIT_1_ID), anyString());
/* request */
final DepositChangeRequestDto request = DepositChangeRequestDto.builder()
.metadata(METADATA_1)
.build();
/* test */
assertThrows(ZenodoUnavailableException.class, () -> {
zenodoService.updateCitation(DATABASE_1_ID, TABLE_1_ID, request);
});
}
@Test @Test
public void updateCitation_only1orcid_fails() { public void updateCitation_only1orcid_fails() {
/* mocks */ /* mocks */
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
when(apiTemplate.exchange(anyString(), eq(HttpMethod.PUT), Mockito.<HttpEntity<DepositChangeRequestDto>>any(), eq(DepositChangeResponseDto.class), when(apiTemplate.exchange(anyString(), eq(HttpMethod.PUT), Mockito.<HttpEntity<DepositChangeRequestDto>>any(), eq(DepositChangeResponseDto.class),
eq(DEPOSIT_1_ID), anyString())) eq(DEPOSIT_1_ID), anyString()))
...@@ -174,7 +251,7 @@ public class MetadataServiceUnitTest extends BaseUnitTest { ...@@ -174,7 +251,7 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
public void updateCitation_notExists_fails() { public void updateCitation_notExists_fails() {
/* mocks */ /* mocks */
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
when(apiTemplate.exchange(anyString(), eq(HttpMethod.PUT), Mockito.<HttpEntity<DepositChangeRequestDto>>any(), eq(DepositChangeResponseDto.class), when(apiTemplate.exchange(anyString(), eq(HttpMethod.PUT), Mockito.<HttpEntity<DepositChangeRequestDto>>any(), eq(DepositChangeResponseDto.class),
eq(DEPOSIT_1_ID), anyString())) eq(DEPOSIT_1_ID), anyString()))
...@@ -196,7 +273,7 @@ public class MetadataServiceUnitTest extends BaseUnitTest { ...@@ -196,7 +273,7 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
public void updateCitation_notFound_fails() { public void updateCitation_notFound_fails() {
/* mocks */ /* mocks */
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.empty()); .thenReturn(Optional.empty());
when(apiTemplate.exchange(anyString(), eq(HttpMethod.PUT), Mockito.<HttpEntity<DepositChangeRequestDto>>any(), when(apiTemplate.exchange(anyString(), eq(HttpMethod.PUT), Mockito.<HttpEntity<DepositChangeRequestDto>>any(),
eq(DepositChangeResponseDto.class), eq(DepositChangeResponseDto.class),
......
package at.tuwien.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.NO_CONTENT)
public class ZenodoUnavailableException extends Exception {
public ZenodoUnavailableException(String msg) {
super(msg);
}
public ZenodoUnavailableException(String msg, Throwable thr) {
super(msg, thr);
}
public ZenodoUnavailableException(Throwable thr) {
super(thr);
}
}
...@@ -28,7 +28,7 @@ public interface FileService { ...@@ -28,7 +28,7 @@ public interface FileService {
*/ */
FileResponseDto createResource(Long databaseId, Long tableId, FileUploadDto data, MultipartFile resource) FileResponseDto createResource(Long databaseId, Long tableId, FileUploadDto data, MultipartFile resource)
throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException, throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException,
ZenodoFileTooLargeException, MetadataDatabaseNotFoundException; ZenodoFileTooLargeException, MetadataDatabaseNotFoundException, ZenodoUnavailableException;
/** /**
* List all files known to a deposit number (through the database-table id pair) * List all files known to a deposit number (through the database-table id pair)
...@@ -41,7 +41,7 @@ public interface FileService { ...@@ -41,7 +41,7 @@ public interface FileService {
* @throws ZenodoNotFoundException The deposit id was not found on the remote server * @throws ZenodoNotFoundException The deposit id was not found on the remote server
* @throws MetadataDatabaseNotFoundException The deposit was not found on the metadata database * @throws MetadataDatabaseNotFoundException The deposit was not found on the metadata database
*/ */
List<FileResponseDto> listResources(Long databaseId, Long tableId) throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoApiException; List<FileResponseDto> listResources(Long databaseId, Long tableId) throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoApiException, ZenodoUnavailableException;
/** /**
* Find a file for a deposit (through the database-table id pair) by id * Find a file for a deposit (through the database-table id pair) by id
...@@ -55,7 +55,7 @@ public interface FileService { ...@@ -55,7 +55,7 @@ public interface FileService {
* @throws ZenodoNotFoundException The deposit id was not found on the remote server * @throws ZenodoNotFoundException The deposit id was not found on the remote server
* @throws ZenodoApiException Something other went wrong * @throws ZenodoApiException Something other went wrong
*/ */
FileResponseDto findResource(Long databaseId, Long tableId, String fileId) throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoApiException; FileResponseDto findResource(Long databaseId, Long tableId, String fileId) throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoApiException, ZenodoUnavailableException;
/** /**
* Delete a file based on the database-table id pair by id * Delete a file based on the database-table id pair by id
...@@ -70,5 +70,5 @@ public interface FileService { ...@@ -70,5 +70,5 @@ public interface FileService {
*/ */
void deleteResource(Long databaseId, Long tableId, String fileId) void deleteResource(Long databaseId, Long tableId, String fileId)
throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException, throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException,
ZenodoApiException; ZenodoApiException, ZenodoUnavailableException;
} }
package at.tuwien.service; package at.tuwien.service;
import at.tuwien.api.zenodo.deposit.*; import at.tuwien.api.zenodo.deposit.*;
import at.tuwien.exception.MetadataDatabaseNotFoundException; import at.tuwien.exception.*;
import at.tuwien.exception.ZenodoApiException;
import at.tuwien.exception.ZenodoAuthenticationException;
import at.tuwien.exception.ZenodoNotFoundException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
...@@ -21,7 +18,7 @@ public interface MetadataService { ...@@ -21,7 +18,7 @@ public interface MetadataService {
* @throws ZenodoAuthenticationException Token invalid * @throws ZenodoAuthenticationException Token invalid
* @throws ZenodoApiException Something other went wrong * @throws ZenodoApiException Something other went wrong
*/ */
List<DepositResponseDto> listCitations(Long databaseId, Long tableId) throws ZenodoAuthenticationException, ZenodoApiException, MetadataDatabaseNotFoundException; List<DepositResponseDto> listCitations(Long databaseId, Long tableId) throws ZenodoAuthenticationException, ZenodoApiException, MetadataDatabaseNotFoundException, ZenodoUnavailableException;
/** /**
* Create a new deposit * Create a new deposit
...@@ -32,7 +29,7 @@ public interface MetadataService { ...@@ -32,7 +29,7 @@ public interface MetadataService {
* @throws ZenodoAuthenticationException Token invalid * @throws ZenodoAuthenticationException Token invalid
* @throws ZenodoApiException Something other went wrong * @throws ZenodoApiException Something other went wrong
*/ */
DepositChangeResponseDto storeCitation(Long databaseId, Long tableId) throws ZenodoAuthenticationException, ZenodoApiException, MetadataDatabaseNotFoundException; DepositChangeResponseDto storeCitation(Long databaseId, Long tableId) throws ZenodoAuthenticationException, ZenodoApiException, MetadataDatabaseNotFoundException, ZenodoUnavailableException;
/** /**
* Update a deposit with new metadata for a given id * Update a deposit with new metadata for a given id
...@@ -46,7 +43,7 @@ public interface MetadataService { ...@@ -46,7 +43,7 @@ public interface MetadataService {
* @throws ZenodoNotFoundException The deposit id was not found on the remote server * @throws ZenodoNotFoundException The deposit id was not found on the remote server
*/ */
DepositChangeResponseDto updateCitation(Long databaseId, Long tableId, DepositChangeRequestDto data) throws ZenodoAuthenticationException, DepositChangeResponseDto updateCitation(Long databaseId, Long tableId, DepositChangeRequestDto data) throws ZenodoAuthenticationException,
ZenodoApiException, ZenodoNotFoundException, MetadataDatabaseNotFoundException; ZenodoApiException, ZenodoNotFoundException, MetadataDatabaseNotFoundException, ZenodoUnavailableException;
/** /**
* Find a deposit by database-table id pair * Find a deposit by database-table id pair
...@@ -61,7 +58,7 @@ public interface MetadataService { ...@@ -61,7 +58,7 @@ public interface MetadataService {
*/ */
DepositResponseDto findCitation(Long databaseId, Long tableId) DepositResponseDto findCitation(Long databaseId, Long tableId)
throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException, throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException,
MetadataDatabaseNotFoundException; MetadataDatabaseNotFoundException, ZenodoUnavailableException;
/** /**
* Delete a deposit from a given id * Delete a deposit from a given id
...@@ -71,5 +68,5 @@ public interface MetadataService { ...@@ -71,5 +68,5 @@ public interface MetadataService {
* @throws ZenodoAuthenticationException Token invalid * @throws ZenodoAuthenticationException Token invalid
* @throws ZenodoApiException Something other went wrong * @throws ZenodoApiException Something other went wrong
*/ */
void deleteCitation(Long databaseId, Long tableId) throws ZenodoAuthenticationException, ZenodoApiException, MetadataDatabaseNotFoundException; void deleteCitation(Long databaseId, Long tableId) throws ZenodoAuthenticationException, ZenodoApiException, MetadataDatabaseNotFoundException, ZenodoUnavailableException, ZenodoNotFoundException;
} }
...@@ -12,11 +12,11 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -12,11 +12,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
...@@ -43,7 +43,7 @@ public class ZenodoFileService implements FileService { ...@@ -43,7 +43,7 @@ public class ZenodoFileService implements FileService {
@Transactional @Transactional
public FileResponseDto createResource(Long databaseId, Long tableId, FileUploadDto data, MultipartFile resource) public FileResponseDto createResource(Long databaseId, Long tableId, FileUploadDto data, MultipartFile resource)
throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException, throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException,
ZenodoFileTooLargeException, MetadataDatabaseNotFoundException { ZenodoFileTooLargeException, MetadataDatabaseNotFoundException, ZenodoUnavailableException {
if (resource.getSize() > 50_1000_1000_1000L) { if (resource.getSize() > 50_1000_1000_1000L) {
throw new ZenodoFileTooLargeException("Only 50GB per file is allowed!"); throw new ZenodoFileTooLargeException("Only 50GB per file is allowed!");
} }
...@@ -54,6 +54,8 @@ public class ZenodoFileService implements FileService { ...@@ -54,6 +54,8 @@ public class ZenodoFileService implements FileService {
zenodoMapper.resourceToHttpEntity(data.getName(), resource), FileResponseDto.class, table.getDepositId(), zenodoConfig.getApiKey()); zenodoMapper.resourceToHttpEntity(data.getName(), resource), FileResponseDto.class, table.getDepositId(), zenodoConfig.getApiKey());
} catch (IOException e) { } catch (IOException e) {
throw new ZenodoApiException("Could not map file to byte array"); throw new ZenodoApiException("Could not map file to byte array");
} catch (ResourceAccessException e) {
throw new ZenodoUnavailableException("Zenodo host is not reachable from the service network", e);
} catch (HttpClientErrorException.Unauthorized e) { } catch (HttpClientErrorException.Unauthorized e) {
throw new ZenodoAuthenticationException("Token is missing or invalid."); throw new ZenodoAuthenticationException("Token is missing or invalid.");
} catch (HttpClientErrorException.BadRequest e) { } catch (HttpClientErrorException.BadRequest e) {
...@@ -71,12 +73,14 @@ public class ZenodoFileService implements FileService { ...@@ -71,12 +73,14 @@ public class ZenodoFileService implements FileService {
@Override @Override
@Transactional @Transactional
public List<FileResponseDto> listResources(Long databaseId, Long tableId) throws MetadataDatabaseNotFoundException, public List<FileResponseDto> listResources(Long databaseId, Long tableId) throws MetadataDatabaseNotFoundException,
ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoApiException { ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoApiException, ZenodoUnavailableException {
final Table table = getTable(databaseId, tableId); final Table table = getTable(databaseId, tableId);
final ResponseEntity<FileResponseDto[]> response; final ResponseEntity<FileResponseDto[]> response;
try { try {
response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}/files?access_token={token}", response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}/files?access_token={token}",
HttpMethod.GET, addHeaders(null), FileResponseDto[].class, table.getDepositId(), zenodoConfig.getApiKey()); HttpMethod.GET, addHeaders(null), FileResponseDto[].class, table.getDepositId(), zenodoConfig.getApiKey());
} catch (ResourceAccessException e) {
throw new ZenodoUnavailableException("Zenodo host is not reachable from the service network", e);
} catch (HttpClientErrorException.NotFound e) { } catch (HttpClientErrorException.NotFound e) {
throw new ZenodoNotFoundException("Did not find the resoource with this id"); throw new ZenodoNotFoundException("Did not find the resoource with this id");
} catch (HttpClientErrorException.Unauthorized e) { } catch (HttpClientErrorException.Unauthorized e) {
...@@ -92,13 +96,15 @@ public class ZenodoFileService implements FileService { ...@@ -92,13 +96,15 @@ public class ZenodoFileService implements FileService {
@Transactional @Transactional
public FileResponseDto findResource(Long databaseId, Long tableId, String fileId) public FileResponseDto findResource(Long databaseId, Long tableId, String fileId)
throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException, throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException,
ZenodoApiException { ZenodoApiException, ZenodoUnavailableException {
final Table table = getTable(databaseId, tableId); final Table table = getTable(databaseId, tableId);
final ResponseEntity<FileResponseDto> response; final ResponseEntity<FileResponseDto> response;
try { try {
response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}/files/{file_id}?access_token={token}", response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}/files/{file_id}?access_token={token}",
HttpMethod.GET, addHeaders(null), FileResponseDto.class, table.getDepositId(), fileId, HttpMethod.GET, addHeaders(null), FileResponseDto.class, table.getDepositId(), fileId,
zenodoConfig.getApiKey()); zenodoConfig.getApiKey());
} catch (ResourceAccessException e) {
throw new ZenodoUnavailableException("Zenodo host is not reachable from the service network", e);
} catch (HttpClientErrorException.NotFound e) { } catch (HttpClientErrorException.NotFound e) {
throw new ZenodoNotFoundException("Did not find the resoource with this ID"); throw new ZenodoNotFoundException("Did not find the resoource with this ID");
} catch (HttpClientErrorException.Unauthorized e) { } catch (HttpClientErrorException.Unauthorized e) {
...@@ -113,13 +119,16 @@ public class ZenodoFileService implements FileService { ...@@ -113,13 +119,16 @@ public class ZenodoFileService implements FileService {
@Override @Override
@Transactional @Transactional
public void deleteResource(Long databaseId, Long tableId, String fileId) public void deleteResource(Long databaseId, Long tableId, String fileId)
throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoApiException { throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException,
ZenodoApiException, ZenodoUnavailableException {
final Table table = getTable(databaseId, tableId); final Table table = getTable(databaseId, tableId);
final ResponseEntity<String> response; final ResponseEntity<String> response;
try { try {
response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}/files/{file_id}?access_token={token}", response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}/files/{file_id}?access_token={token}",
HttpMethod.DELETE, addHeaders(null), String.class, table.getDepositId(), fileId, HttpMethod.DELETE, addHeaders(null), String.class, table.getDepositId(), fileId,
zenodoConfig.getApiKey()); zenodoConfig.getApiKey());
} catch (ResourceAccessException e) {
throw new ZenodoUnavailableException("Zenodo host is not reachable from the service network", e);
} catch (HttpClientErrorException.NotFound e) { } catch (HttpClientErrorException.NotFound e) {
throw new ZenodoNotFoundException("Did not find the resource with this ID"); throw new ZenodoNotFoundException("Did not find the resource with this ID");
} catch (HttpClientErrorException.Unauthorized e) { } catch (HttpClientErrorException.Unauthorized e) {
......
...@@ -4,14 +4,13 @@ import at.tuwien.api.zenodo.deposit.*; ...@@ -4,14 +4,13 @@ import at.tuwien.api.zenodo.deposit.*;
import at.tuwien.config.ZenodoConfig; import at.tuwien.config.ZenodoConfig;
import at.tuwien.entities.database.Database; import at.tuwien.entities.database.Database;
import at.tuwien.entities.database.table.Table; import at.tuwien.entities.database.table.Table;
import at.tuwien.exception.MetadataDatabaseNotFoundException; import at.tuwien.exception.*;
import at.tuwien.exception.ZenodoApiException;
import at.tuwien.exception.ZenodoAuthenticationException;
import at.tuwien.exception.ZenodoNotFoundException;
import at.tuwien.repository.jpa.TableRepository; import at.tuwien.repository.jpa.TableRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import javax.transaction.Transactional; import javax.transaction.Transactional;
...@@ -36,9 +35,14 @@ public class ZenodoMetadataService implements MetadataService { ...@@ -36,9 +35,14 @@ public class ZenodoMetadataService implements MetadataService {
@Override @Override
@Transactional @Transactional
public List<DepositResponseDto> listCitations(Long databaseId, Long tableId) throws ZenodoAuthenticationException, public List<DepositResponseDto> listCitations(Long databaseId, Long tableId) throws ZenodoAuthenticationException,
ZenodoApiException { ZenodoApiException, ZenodoUnavailableException {
final ResponseEntity<DepositResponseDto[]> response = apiTemplate.exchange("/api/deposit/depositions?access_token={token}", final ResponseEntity<DepositResponseDto[]> response;
try {
response = apiTemplate.exchange("/api/deposit/depositions?access_token={token}",
HttpMethod.GET, addHeaders(null), DepositResponseDto[].class, zenodoConfig.getApiKey()); HttpMethod.GET, addHeaders(null), DepositResponseDto[].class, zenodoConfig.getApiKey());
} catch (ResourceAccessException e) {
throw new ZenodoUnavailableException("Zenodo host is not reachable from the service network", e);
}
if (response.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) { if (response.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
throw new ZenodoAuthenticationException("Token is missing or invalid."); throw new ZenodoAuthenticationException("Token is missing or invalid.");
} }
...@@ -51,10 +55,15 @@ public class ZenodoMetadataService implements MetadataService { ...@@ -51,10 +55,15 @@ public class ZenodoMetadataService implements MetadataService {
@Override @Override
@Transactional @Transactional
public DepositChangeResponseDto storeCitation(Long databaseId, Long tableId) throws ZenodoAuthenticationException, public DepositChangeResponseDto storeCitation(Long databaseId, Long tableId) throws ZenodoAuthenticationException,
ZenodoApiException, MetadataDatabaseNotFoundException { ZenodoApiException, MetadataDatabaseNotFoundException, ZenodoUnavailableException {
final Table table = getTable(databaseId, tableId); final Table table = getTable(databaseId, tableId);
final ResponseEntity<DepositChangeResponseDto> response = apiTemplate.exchange("/api/deposit/depositions?access_token={token}", final ResponseEntity<DepositChangeResponseDto> response;
HttpMethod.POST, addHeaders("{}"), DepositChangeResponseDto.class, zenodoConfig.getApiKey()); try {
response = apiTemplate.exchange("/api/deposit/depositions?access_token={token}", HttpMethod.POST,
addHeaders("{}"), DepositChangeResponseDto.class, zenodoConfig.getApiKey());
} catch (ResourceAccessException e) {
throw new ZenodoUnavailableException("Zenodo host is not reachable from the service network", e);
}
if (response.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) { if (response.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
throw new ZenodoAuthenticationException("Token is missing or invalid."); throw new ZenodoAuthenticationException("Token is missing or invalid.");
} }
...@@ -73,11 +82,18 @@ public class ZenodoMetadataService implements MetadataService { ...@@ -73,11 +82,18 @@ public class ZenodoMetadataService implements MetadataService {
@Transactional @Transactional
public DepositChangeResponseDto updateCitation(Long databaseId, Long tableId, DepositChangeRequestDto data) public DepositChangeResponseDto updateCitation(Long databaseId, Long tableId, DepositChangeRequestDto data)
throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException, throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException,
MetadataDatabaseNotFoundException { MetadataDatabaseNotFoundException, ZenodoUnavailableException {
final Table table = getTable(databaseId, tableId); final Table table = getTable(databaseId, tableId);
final ResponseEntity<DepositChangeResponseDto> response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}?access_token={token}", final ResponseEntity<DepositChangeResponseDto> response;
try {
response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}?access_token={token}",
HttpMethod.PUT, addHeaders(data), DepositChangeResponseDto.class, table.getDepositId(), HttpMethod.PUT, addHeaders(data), DepositChangeResponseDto.class, table.getDepositId(),
zenodoConfig.getApiKey()); zenodoConfig.getApiKey());
} catch (ResourceAccessException e) {
throw new ZenodoUnavailableException("Zenodo host is not reachable from the service network", e);
} catch (HttpClientErrorException.NotFound | HttpClientErrorException.BadRequest e) {
throw new ZenodoNotFoundException("Could not get the citation.", e);
}
if (response.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) { if (response.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
throw new ZenodoAuthenticationException("Token is missing or invalid."); throw new ZenodoAuthenticationException("Token is missing or invalid.");
} }
...@@ -96,11 +112,18 @@ public class ZenodoMetadataService implements MetadataService { ...@@ -96,11 +112,18 @@ public class ZenodoMetadataService implements MetadataService {
@Override @Override
@Transactional @Transactional
public DepositResponseDto findCitation(Long databaseId, Long tableId) throws ZenodoAuthenticationException, public DepositResponseDto findCitation(Long databaseId, Long tableId) throws ZenodoAuthenticationException,
ZenodoApiException, ZenodoNotFoundException, MetadataDatabaseNotFoundException { ZenodoApiException, ZenodoNotFoundException, MetadataDatabaseNotFoundException, ZenodoUnavailableException {
final Table table = getTable(databaseId, tableId); final Table table = getTable(databaseId, tableId);
final ResponseEntity<DepositResponseDto> response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}?access_token={token}", final ResponseEntity<DepositResponseDto> response;
try {
response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}?access_token={token}",
HttpMethod.GET, addHeaders(null), DepositResponseDto.class, table.getDepositId(), HttpMethod.GET, addHeaders(null), DepositResponseDto.class, table.getDepositId(),
zenodoConfig.getApiKey()); zenodoConfig.getApiKey());
} catch (ResourceAccessException e) {
throw new ZenodoUnavailableException("Zenodo host is not reachable from the service network", e);
} catch (HttpClientErrorException.NotFound | HttpClientErrorException.BadRequest e) {
throw new ZenodoNotFoundException("Could not get the citation.", e);
}
if (response.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) { if (response.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
throw new ZenodoAuthenticationException("Token is missing or invalid."); throw new ZenodoAuthenticationException("Token is missing or invalid.");
} }
...@@ -116,10 +139,17 @@ public class ZenodoMetadataService implements MetadataService { ...@@ -116,10 +139,17 @@ public class ZenodoMetadataService implements MetadataService {
@Override @Override
@Transactional @Transactional
public void deleteCitation(Long databaseId, Long tableId) throws ZenodoAuthenticationException, ZenodoApiException, public void deleteCitation(Long databaseId, Long tableId) throws ZenodoAuthenticationException, ZenodoApiException,
MetadataDatabaseNotFoundException { MetadataDatabaseNotFoundException, ZenodoUnavailableException, ZenodoNotFoundException {
final Table table = getTable(databaseId, tableId); final Table table = getTable(databaseId, tableId);
final ResponseEntity<String> response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}?access_token={token}", final ResponseEntity<String> response;
try {
response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}?access_token={token}",
HttpMethod.DELETE, addHeaders(null), String.class, table.getDepositId(), zenodoConfig.getApiKey()); HttpMethod.DELETE, addHeaders(null), String.class, table.getDepositId(), zenodoConfig.getApiKey());
} catch (ResourceAccessException e) {
throw new ZenodoUnavailableException("Zenodo host is not reachable from the service network", e);
} catch (HttpClientErrorException.NotFound | HttpClientErrorException.BadRequest e) {
throw new ZenodoNotFoundException("Could not get the citation.", e);
}
if (response.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) { if (response.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
throw new ZenodoAuthenticationException("Token is missing or invalid."); throw new ZenodoAuthenticationException("Token is missing or invalid.");
} }
......
...@@ -73,6 +73,9 @@ ...@@ -73,6 +73,9 @@
<v-btn color="primary" :to="`/databases/${$route.params.database_id}/queries/${item.id}`"> <v-btn color="primary" :to="`/databases/${$route.params.database_id}/queries/${item.id}`">
<v-icon left>mdi-run</v-icon> Execute Again <v-icon left>mdi-run</v-icon> Execute Again
</v-btn> </v-btn>
<v-btn color="primary" :to="`/databases/${$route.params.database_id}/queries/${item.id}/metadata`">
<v-icon left>mdi-doi</v-icon> Issue DOI
</v-btn>
</v-col> </v-col>
</v-row> </v-row>
</v-expansion-panel-content> </v-expansion-panel-content>
......
...@@ -93,7 +93,7 @@ export default { ...@@ -93,7 +93,7 @@ export default {
needsContainer: true needsContainer: true
} }
], ],
title: 'FAIR Data Repository' title: 'FAIR Data Austria — Database Repository'
} }
}, },
computed: { computed: {
......
<template>
<div>
<v-card>
<v-card-title v-if="!loading">
Metadata
</v-card-title>
<v-card-subtitle v-if="!loading">
Subtitle
</v-card-subtitle>
<div>
aaa
</div>
</v-card>
</div>
</template>
<script>
export default {
name: 'QueryDoiMetadata',
components: {
},
data () {
return {
loading: false
}
},
mounted () {
},
methods: {
}
}
</script>
<style>
</style>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment