diff --git a/fda-citation-service/rest-service/src/main/java/at/tuwien/endpoints/FileEndpoint.java b/fda-citation-service/rest-service/src/main/java/at/tuwien/endpoints/FileEndpoint.java index 0ac736b16cf1d42e9a480c1df9f8a273190803e7..263fee91f41c21da2edd37166870e49345253106 100644 --- a/fda-citation-service/rest-service/src/main/java/at/tuwien/endpoints/FileEndpoint.java +++ b/fda-citation-service/rest-service/src/main/java/at/tuwien/endpoints/FileEndpoint.java @@ -30,7 +30,7 @@ public class FileEndpoint { public List<FileResponseDto> listAll(@Valid @RequestParam("id") Long databaseId, @Valid @RequestParam("tableId") Long tableId) throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoApiException, - ZenodoNotFoundException { + ZenodoNotFoundException, ZenodoUnavailableException { return fileService.listResources(databaseId, tableId); } @@ -39,7 +39,7 @@ public class FileEndpoint { @Valid @RequestParam("tableId") Long tableId, @NotBlank @RequestParam("fileId") String fileId) throws MetadataDatabaseNotFoundException, ZenodoApiException, ZenodoNotFoundException, - ZenodoAuthenticationException { + ZenodoAuthenticationException, ZenodoUnavailableException { return fileService.findResource(databaseId, tableId, fileId); } @@ -49,7 +49,7 @@ public class FileEndpoint { @Valid @RequestParam("data") FileUploadDto data, @Valid @RequestParam("file") MultipartFile file) throws MetadataDatabaseNotFoundException, ZenodoApiException, ZenodoFileTooLargeException, - ZenodoNotFoundException, ZenodoAuthenticationException { + ZenodoNotFoundException, ZenodoAuthenticationException, ZenodoUnavailableException { return fileService.createResource(databaseId, tableId, data, file); } diff --git a/fda-citation-service/rest-service/src/main/java/at/tuwien/endpoints/MetadataEndpoint.java b/fda-citation-service/rest-service/src/main/java/at/tuwien/endpoints/MetadataEndpoint.java index 6dfcc31d4ba475aa811c8f6e0f936fa2f81972ce..fc64bd4e8942fe25674494f7661bf0f7a14b605d 100644 --- a/fda-citation-service/rest-service/src/main/java/at/tuwien/endpoints/MetadataEndpoint.java +++ b/fda-citation-service/rest-service/src/main/java/at/tuwien/endpoints/MetadataEndpoint.java @@ -3,10 +3,7 @@ package at.tuwien.endpoints; import at.tuwien.api.zenodo.deposit.DepositChangeRequestDto; import at.tuwien.api.zenodo.deposit.DepositChangeResponseDto; import at.tuwien.api.zenodo.deposit.DepositResponseDto; -import at.tuwien.exception.MetadataDatabaseNotFoundException; -import at.tuwien.exception.ZenodoApiException; -import at.tuwien.exception.ZenodoAuthenticationException; -import at.tuwien.exception.ZenodoNotFoundException; +import at.tuwien.exception.*; import at.tuwien.service.MetadataService; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; @@ -32,14 +29,14 @@ public class MetadataEndpoint { @GetMapping public List<DepositResponseDto> findAll(@Valid @RequestParam("id") Long databaseId, @Valid @RequestParam("tableId") Long tableId) throws ZenodoApiException, - ZenodoAuthenticationException, MetadataDatabaseNotFoundException { + ZenodoAuthenticationException, MetadataDatabaseNotFoundException, ZenodoUnavailableException { return metadataService.listCitations(databaseId, tableId); } @PostMapping public DepositChangeResponseDto create(@Valid @RequestParam("id") Long databaseId, @Valid @RequestParam("tableId") Long tableId) throws ZenodoApiException, - ZenodoAuthenticationException, MetadataDatabaseNotFoundException { + ZenodoAuthenticationException, MetadataDatabaseNotFoundException, ZenodoUnavailableException { return metadataService.storeCitation(databaseId, tableId); } @@ -47,7 +44,7 @@ public class MetadataEndpoint { public DepositResponseDto find(@Valid @RequestParam("id") Long databaseId, @Valid @RequestParam("tableId") Long tableId) throws MetadataDatabaseNotFoundException, ZenodoApiException, ZenodoNotFoundException, - ZenodoAuthenticationException { + ZenodoAuthenticationException, ZenodoUnavailableException { return metadataService.findCitation(databaseId, tableId); } @@ -56,7 +53,7 @@ public class MetadataEndpoint { @Valid @RequestParam("tableId") Long tableId, @Valid @RequestBody DepositChangeRequestDto data) throws MetadataDatabaseNotFoundException, ZenodoApiException, ZenodoNotFoundException, - ZenodoAuthenticationException { + ZenodoAuthenticationException, ZenodoUnavailableException { return metadataService.updateCitation(databaseId, tableId, data); } @@ -64,7 +61,7 @@ public class MetadataEndpoint { public void delete(@Valid @RequestParam("id") Long databaseId, @Valid @RequestParam("tableId") Long tableId, @NotBlank @RequestParam("fileId") String fileId) throws MetadataDatabaseNotFoundException, - ZenodoApiException, ZenodoAuthenticationException { + ZenodoApiException, ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoUnavailableException { metadataService.deleteCitation(databaseId, tableId); } } diff --git a/fda-citation-service/rest-service/src/test/java/at/tuwien/service/FileServiceIntegrationTest.java b/fda-citation-service/rest-service/src/test/java/at/tuwien/service/FileServiceIntegrationTest.java index 9dc19f4f831101d2bfa4515700e597ab4c1de897..cc2c31f35f9807916c0a651e63479f7bbbb5d126 100644 --- a/fda-citation-service/rest-service/src/test/java/at/tuwien/service/FileServiceIntegrationTest.java +++ b/fda-citation-service/rest-service/src/test/java/at/tuwien/service/FileServiceIntegrationTest.java @@ -5,10 +5,12 @@ import at.tuwien.api.zenodo.deposit.DepositChangeResponseDto; import at.tuwien.api.zenodo.files.FileResponseDto; import at.tuwien.api.zenodo.files.FileUploadDto; import at.tuwien.config.ReadyConfig; +import at.tuwien.entities.database.Database; import at.tuwien.entities.database.table.Table; import at.tuwien.exception.*; import at.tuwien.repository.jpa.TableRepository; import org.apache.commons.io.FileUtils; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; @@ -18,6 +20,7 @@ import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.util.ResourceUtils; +import java.io.FileNotFoundException; import java.io.IOException; import java.util.List; import java.util.Optional; @@ -41,12 +44,21 @@ public class FileServiceIntegrationTest extends BaseUnitTest { @Autowired private ZenodoMetadataService metadataService; + final Database DATABASE_1 = Database.builder() + .id(DATABASE_1_ID) + .build(); + @Test public void createResource_succeeds() throws IOException, ZenodoApiException, ZenodoNotFoundException, - ZenodoAuthenticationException, ZenodoFileTooLargeException, MetadataDatabaseNotFoundException { + ZenodoAuthenticationException, ZenodoFileTooLargeException, MetadataDatabaseNotFoundException, + ZenodoUnavailableException { final MockMultipartFile file = new MockMultipartFile("testdata.csv", FileUtils.readFileToByteArray( ResourceUtils.getFile("classpath:csv/testdata.csv"))); + /* mock */ + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) + .thenReturn(Optional.of(TABLE_1)); + /* request */ final DepositChangeResponseDto deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID); final FileUploadDto request = FileUploadDto.builder() @@ -58,8 +70,6 @@ public class FileServiceIntegrationTest extends BaseUnitTest { .id(TABLE_1_ID) .depositId(deposit.getId()) .build(); - when(tableRepository.findById(TABLE_1_ID)) - .thenReturn(Optional.of(TABLE_1)); /* test */ final FileResponseDto response = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, request, file); @@ -69,11 +79,17 @@ public class FileServiceIntegrationTest extends BaseUnitTest { } @Test + @Disabled("slow internet") 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( ResourceUtils.getFile("classpath:csv/weatherAUS.csv"))); + /* mock */ + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) + .thenReturn(Optional.of(TABLE_1)); + /* request */ final DepositChangeResponseDto deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID); final FileUploadDto request = FileUploadDto.builder() @@ -85,8 +101,6 @@ public class FileServiceIntegrationTest extends BaseUnitTest { .id(TABLE_1_ID) .depositId(deposit.getId()) .build(); - when(tableRepository.findById(TABLE_1_ID)) - .thenReturn(Optional.of(TABLE_1)); /* test */ final FileResponseDto response = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, request, file); @@ -97,9 +111,12 @@ public class FileServiceIntegrationTest extends BaseUnitTest { @Test public void listAll_notFound_fails() { + final Table TABLE_1 = Table.builder() + .id(-1L) + .build(); /* mock */ - when(tableRepository.findById(TABLE_1_ID)) + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) .thenReturn(Optional.of(TABLE_1)); /* test */ @@ -110,10 +127,15 @@ public class FileServiceIntegrationTest extends BaseUnitTest { @Test 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( ResourceUtils.getFile("classpath:csv/testdata.csv"))); + /* mock */ + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) + .thenReturn(Optional.of(TABLE_1)); + /* request */ final DepositChangeResponseDto deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID); final FileUploadDto upload = FileUploadDto.builder() @@ -123,8 +145,6 @@ public class FileServiceIntegrationTest extends BaseUnitTest { .id(TABLE_1_ID) .depositId(deposit.getId()) .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); /* mock */ @@ -140,10 +160,15 @@ public class FileServiceIntegrationTest extends BaseUnitTest { @Test 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( ResourceUtils.getFile("classpath:csv/testdata.csv"))); + /* mock */ + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) + .thenReturn(Optional.of(TABLE_1)); + /* request */ final DepositChangeResponseDto deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID); final FileUploadDto upload = FileUploadDto.builder() @@ -153,8 +178,6 @@ public class FileServiceIntegrationTest extends BaseUnitTest { .id(TABLE_1_ID) .depositId(deposit.getId()) .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); /* mock */ @@ -169,10 +192,15 @@ public class FileServiceIntegrationTest extends BaseUnitTest { @Test 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( ResourceUtils.getFile("classpath:csv/testdata.csv"))); + /* mock */ + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) + .thenReturn(Optional.of(TABLE_1)); + /* request */ final DepositChangeResponseDto deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID); final FileUploadDto upload = FileUploadDto.builder() @@ -182,8 +210,6 @@ public class FileServiceIntegrationTest extends BaseUnitTest { .id(TABLE_1_ID) .depositId(deposit.getId()) .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); /* mock */ diff --git a/fda-citation-service/rest-service/src/test/java/at/tuwien/service/FileServiceUnitTest.java b/fda-citation-service/rest-service/src/test/java/at/tuwien/service/FileServiceUnitTest.java index 9905223ec6558a3cc25c620255577f4ef658e686..f160cf99b71a608f9e6783cf0b0f9d5e51bdd0dd 100644 --- a/fda-citation-service/rest-service/src/test/java/at/tuwien/service/FileServiceUnitTest.java +++ b/fda-citation-service/rest-service/src/test/java/at/tuwien/service/FileServiceUnitTest.java @@ -4,6 +4,7 @@ import at.tuwien.BaseUnitTest; import at.tuwien.api.zenodo.files.FileResponseDto; import at.tuwien.api.zenodo.files.FileUploadDto; import at.tuwien.config.ReadyConfig; +import at.tuwien.entities.database.Database; import at.tuwien.exception.*; import at.tuwien.repository.jpa.TableRepository; import org.apache.commons.io.FileUtils; @@ -21,6 +22,7 @@ import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.util.MultiValueMap; import org.springframework.util.ResourceUtils; +import org.springframework.web.client.ResourceAccessException; import org.springframework.web.client.RestTemplate; import java.io.IOException; @@ -29,8 +31,8 @@ import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.when; @SpringBootTest @@ -49,9 +51,14 @@ public class FileServiceUnitTest extends BaseUnitTest { @MockBean private TableRepository tableRepository; + final Database DATABASE_1 = Database.builder() + .id(DATABASE_1_ID) + .build(); + @Test public void createResource_succeeds() throws IOException, ZenodoApiException, ZenodoNotFoundException, - ZenodoAuthenticationException, ZenodoFileTooLargeException, MetadataDatabaseNotFoundException { + ZenodoAuthenticationException, ZenodoFileTooLargeException, MetadataDatabaseNotFoundException, + ZenodoUnavailableException { final MockMultipartFile file = new MockMultipartFile("testdata.csv", FileUtils.readFileToByteArray( ResourceUtils.getFile("classpath:csv/testdata.csv"))); @@ -60,7 +67,7 @@ public class FileServiceUnitTest extends BaseUnitTest { eq(FileResponseDto.class), eq(DEPOSIT_1_ID), anyString())) .thenReturn(ResponseEntity.status(HttpStatus.OK) .body(FILE_1)); - when(tableRepository.findById(TABLE_1_ID)) + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) .thenReturn(Optional.of(TABLE_1)); /* request */ @@ -75,6 +82,30 @@ public class FileServiceUnitTest extends BaseUnitTest { 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 public void createResource_notExists_fails() throws IOException { final MockMultipartFile file = new MockMultipartFile("testdata.csv", FileUtils.readFileToByteArray( @@ -85,7 +116,7 @@ public class FileServiceUnitTest extends BaseUnitTest { eq(FileResponseDto.class), eq(DEPOSIT_1_ID), anyString())) .thenReturn(ResponseEntity.status(HttpStatus.BAD_REQUEST) .build()); - when(tableRepository.findById(TABLE_1_ID)) + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) .thenReturn(Optional.of(TABLE_1)); /* request */ @@ -108,7 +139,7 @@ public class FileServiceUnitTest extends BaseUnitTest { when(apiTemplate.postForEntity(anyString(), Mockito.<MultiValueMap<String, HttpEntity<?>>>any(), eq(FileResponseDto.class), eq(DEPOSIT_1_ID), anyString())) .thenReturn(ResponseEntity.accepted().body(null)); - when(tableRepository.findById(TABLE_1_ID)) + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) .thenReturn(Optional.of(TABLE_1)); /* request */ @@ -124,13 +155,13 @@ public class FileServiceUnitTest extends BaseUnitTest { @Test public void listAll_succeeds() throws MetadataDatabaseNotFoundException, ZenodoApiException, - ZenodoNotFoundException, ZenodoAuthenticationException { + ZenodoNotFoundException, ZenodoAuthenticationException, ZenodoUnavailableException { /* mock */ when(apiTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(FileResponseDto[].class), eq(DEPOSIT_1_ID), anyString())) .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)); /* test */ @@ -138,6 +169,23 @@ public class FileServiceUnitTest extends BaseUnitTest { 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 public void listAll_noContent_fails() { @@ -145,7 +193,7 @@ public class FileServiceUnitTest extends BaseUnitTest { when(apiTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(FileResponseDto[].class), eq(DEPOSIT_1_ID), anyString())) .thenReturn(ResponseEntity.ok().body(null)); - when(tableRepository.findById(TABLE_1_ID)) + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) .thenReturn(Optional.of(TABLE_1)); /* test */ @@ -158,7 +206,7 @@ public class FileServiceUnitTest extends BaseUnitTest { public void listAll_notFound_fails() { /* mock */ - when(tableRepository.findById(TABLE_1_ID)) + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) .thenReturn(Optional.of(TABLE_1)); /* test */ @@ -169,13 +217,13 @@ public class FileServiceUnitTest extends BaseUnitTest { @Test public void findResource_succeeds() throws MetadataDatabaseNotFoundException, ZenodoApiException, - ZenodoNotFoundException, ZenodoAuthenticationException { + ZenodoNotFoundException, ZenodoAuthenticationException, ZenodoUnavailableException { /* mock */ when(apiTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(FileResponseDto.class), eq(DEPOSIT_1_ID), eq(FILE_1_ID), anyString())) .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)); /* test */ @@ -186,6 +234,23 @@ public class FileServiceUnitTest extends BaseUnitTest { 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 public void findResource_noContent_fails() { @@ -193,7 +258,7 @@ public class FileServiceUnitTest extends BaseUnitTest { when(apiTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(FileResponseDto.class), eq(DEPOSIT_1_ID), eq(FILE_1_ID), anyString())) .thenReturn(ResponseEntity.ok().body(null)); - when(tableRepository.findById(TABLE_1_ID)) + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) .thenReturn(Optional.of(TABLE_1)); /* test */ @@ -206,7 +271,7 @@ public class FileServiceUnitTest extends BaseUnitTest { public void findResource_notFound_fails() { /* mock */ - when(tableRepository.findById(TABLE_1_ID)) + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) .thenReturn(Optional.empty()); /* test */ @@ -217,19 +282,36 @@ public class FileServiceUnitTest extends BaseUnitTest { @Test public void deleteResource_succeeds() throws MetadataDatabaseNotFoundException, ZenodoApiException, - ZenodoNotFoundException, ZenodoAuthenticationException { + ZenodoNotFoundException, ZenodoAuthenticationException, ZenodoUnavailableException { /* mock */ when(apiTemplate.exchange(anyString(), eq(HttpMethod.DELETE), Mockito.any(), eq(String.class), eq(DEPOSIT_1_ID), eq(FILE_1_ID), anyString())) .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)); + + /* test */ + assertThrows(ZenodoUnavailableException.class, () -> { + fileService.deleteResource(DATABASE_1_ID, TABLE_1_ID, FILE_1_ID); + }); + } + @Test public void deleteResource_wrongStatus_fails() { @@ -237,7 +319,7 @@ public class FileServiceUnitTest extends BaseUnitTest { when(apiTemplate.exchange(anyString(), eq(HttpMethod.DELETE), Mockito.any(), eq(String.class), eq(DEPOSIT_1_ID), eq(FILE_1_ID), anyString())) .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)); /* test */ diff --git a/fda-citation-service/rest-service/src/test/java/at/tuwien/service/MetadataServiceIntegrationTest.java b/fda-citation-service/rest-service/src/test/java/at/tuwien/service/MetadataServiceIntegrationTest.java index 2aae07d1db30f97b9669cdf8e0ea5bce9fe2c487..3ad09233a9eabe8f18c239c2cd2dbf45b37e61eb 100644 --- a/fda-citation-service/rest-service/src/test/java/at/tuwien/service/MetadataServiceIntegrationTest.java +++ b/fda-citation-service/rest-service/src/test/java/at/tuwien/service/MetadataServiceIntegrationTest.java @@ -3,11 +3,9 @@ package at.tuwien.service; import at.tuwien.BaseUnitTest; import at.tuwien.api.zenodo.deposit.*; import at.tuwien.config.ReadyConfig; +import at.tuwien.entities.database.Database; import at.tuwien.entities.database.table.Table; -import at.tuwien.exception.MetadataDatabaseNotFoundException; -import at.tuwien.exception.ZenodoApiException; -import at.tuwien.exception.ZenodoAuthenticationException; -import at.tuwien.exception.ZenodoNotFoundException; +import at.tuwien.exception.*; import at.tuwien.repository.jpa.TableRepository; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -34,8 +32,17 @@ public class MetadataServiceIntegrationTest extends BaseUnitTest { @MockBean private TableRepository tableRepository; + final Database DATABASE_1 = Database.builder() + .id(DATABASE_1_ID) + .build(); + @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 */ zenodoService.listCitations(DATABASE_1_ID, TABLE_1_ID); @@ -43,7 +50,11 @@ public class MetadataServiceIntegrationTest extends BaseUnitTest { @Test 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 */ final DepositChangeResponseDto response = zenodoService.storeCitation(DATABASE_1_ID, TABLE_1_ID); @@ -52,7 +63,11 @@ public class MetadataServiceIntegrationTest extends BaseUnitTest { @Test 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 DepositChangeRequestDto request = DepositChangeRequestDto.builder() .metadata(METADATA_1) @@ -63,8 +78,6 @@ public class MetadataServiceIntegrationTest extends BaseUnitTest { .id(TABLE_1_ID) .depositId(deposit.getId()) .build(); - when(tableRepository.findById(TABLE_1_ID)) - .thenReturn(Optional.of(TABLE_1)); /* test */ final DepositChangeResponseDto response2 = zenodoService.updateCitation(DATABASE_1_ID, TABLE_1_ID, request); diff --git a/fda-citation-service/rest-service/src/test/java/at/tuwien/service/MetadataServiceUnitTest.java b/fda-citation-service/rest-service/src/test/java/at/tuwien/service/MetadataServiceUnitTest.java index 8f88da1e42e054fed3899920257f3d912d5ae73b..e44fcc5ab4685bbf4410fe589b382a1aa2df922e 100644 --- a/fda-citation-service/rest-service/src/test/java/at/tuwien/service/MetadataServiceUnitTest.java +++ b/fda-citation-service/rest-service/src/test/java/at/tuwien/service/MetadataServiceUnitTest.java @@ -6,10 +6,8 @@ import at.tuwien.api.zenodo.deposit.DepositResponseDto; import at.tuwien.api.zenodo.deposit.DepositChangeRequestDto; import at.tuwien.api.zenodo.deposit.MetadataDto; import at.tuwien.config.ReadyConfig; -import at.tuwien.exception.MetadataDatabaseNotFoundException; -import at.tuwien.exception.ZenodoApiException; -import at.tuwien.exception.ZenodoAuthenticationException; -import at.tuwien.exception.ZenodoNotFoundException; +import at.tuwien.entities.database.Database; +import at.tuwien.exception.*; import at.tuwien.repository.jpa.TableRepository; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -22,6 +20,7 @@ import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.web.client.ResourceAccessException; import org.springframework.web.client.RestTemplate; import java.util.List; @@ -49,8 +48,13 @@ public class MetadataServiceUnitTest extends BaseUnitTest { @MockBean private TableRepository tableRepository; + final Database DATABASE_1 = Database.builder() + .id(DATABASE_1_ID) + .build(); + @Test - public void listCitations_succeeds() throws ZenodoApiException, ZenodoAuthenticationException { + public void listCitations_succeeds() throws ZenodoApiException, ZenodoAuthenticationException, + ZenodoUnavailableException { /* mocks */ when(apiTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(DepositResponseDto[].class), @@ -62,6 +66,21 @@ public class MetadataServiceUnitTest extends BaseUnitTest { 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 public void listCitations_empty_fails() { @@ -78,9 +97,11 @@ public class MetadataServiceUnitTest extends BaseUnitTest { @Test public void storeCitation_succeed() throws ZenodoApiException, ZenodoAuthenticationException, - MetadataDatabaseNotFoundException { + MetadataDatabaseNotFoundException, ZenodoUnavailableException { /* 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), anyString())) .thenReturn(ResponseEntity.status(HttpStatus.CREATED) @@ -92,12 +113,29 @@ public class MetadataServiceUnitTest extends BaseUnitTest { 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 public void deleteCitation_succeeds() throws ZenodoApiException, ZenodoAuthenticationException, - MetadataDatabaseNotFoundException { + MetadataDatabaseNotFoundException, ZenodoNotFoundException, ZenodoUnavailableException { /* mocks */ - when(tableRepository.findById(TABLE_1_ID)) + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) .thenReturn(Optional.of(TABLE_1)); when(apiTemplate.exchange(anyString(), eq(HttpMethod.DELETE), Mockito.any(), eq(String.class), anyLong(), anyString())) @@ -108,11 +146,28 @@ public class MetadataServiceUnitTest extends BaseUnitTest { 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 public void deleteCitation_fails() { /* mocks */ - when(tableRepository.findById(TABLE_1_ID)) + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) .thenReturn(Optional.of(TABLE_1)); when(apiTemplate.exchange(anyString(), eq(HttpMethod.DELETE), Mockito.any(), eq(String.class), anyLong(), anyString())) @@ -127,10 +182,10 @@ public class MetadataServiceUnitTest extends BaseUnitTest { @Test public void updateCitation_succeeds() throws ZenodoApiException, ZenodoAuthenticationException, - ZenodoNotFoundException, MetadataDatabaseNotFoundException { + ZenodoNotFoundException, MetadataDatabaseNotFoundException, ZenodoUnavailableException { /* mocks */ - when(tableRepository.findById(TABLE_1_ID)) + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) .thenReturn(Optional.of(TABLE_1)); when(apiTemplate.exchange(anyString(), eq(HttpMethod.PUT), Mockito.<HttpEntity<DepositChangeRequestDto>>any(), eq(DepositChangeResponseDto.class), eq(DEPOSIT_1_ID), anyString())) @@ -146,11 +201,33 @@ public class MetadataServiceUnitTest extends BaseUnitTest { 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 public void updateCitation_only1orcid_fails() { /* mocks */ - when(tableRepository.findById(TABLE_1_ID)) + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) .thenReturn(Optional.of(TABLE_1)); when(apiTemplate.exchange(anyString(), eq(HttpMethod.PUT), Mockito.<HttpEntity<DepositChangeRequestDto>>any(), eq(DepositChangeResponseDto.class), eq(DEPOSIT_1_ID), anyString())) @@ -174,7 +251,7 @@ public class MetadataServiceUnitTest extends BaseUnitTest { public void updateCitation_notExists_fails() { /* mocks */ - when(tableRepository.findById(TABLE_1_ID)) + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) .thenReturn(Optional.of(TABLE_1)); when(apiTemplate.exchange(anyString(), eq(HttpMethod.PUT), Mockito.<HttpEntity<DepositChangeRequestDto>>any(), eq(DepositChangeResponseDto.class), eq(DEPOSIT_1_ID), anyString())) @@ -196,7 +273,7 @@ public class MetadataServiceUnitTest extends BaseUnitTest { public void updateCitation_notFound_fails() { /* mocks */ - when(tableRepository.findById(TABLE_1_ID)) + when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID)) .thenReturn(Optional.empty()); when(apiTemplate.exchange(anyString(), eq(HttpMethod.PUT), Mockito.<HttpEntity<DepositChangeRequestDto>>any(), eq(DepositChangeResponseDto.class), diff --git a/fda-citation-service/services/src/main/java/at/tuwien/exception/ZenodoUnavailableException.java b/fda-citation-service/services/src/main/java/at/tuwien/exception/ZenodoUnavailableException.java new file mode 100644 index 0000000000000000000000000000000000000000..bb9d8c93b3aeb66c6e8765fbb971e1355e0876bc --- /dev/null +++ b/fda-citation-service/services/src/main/java/at/tuwien/exception/ZenodoUnavailableException.java @@ -0,0 +1,21 @@ +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); + } + +} diff --git a/fda-citation-service/services/src/main/java/at/tuwien/service/FileService.java b/fda-citation-service/services/src/main/java/at/tuwien/service/FileService.java index 1cf3b2af3b925ae90591e8dcca275e1f2b48afeb..ff8fada54575ab81033e572dc86c353804590564 100644 --- a/fda-citation-service/services/src/main/java/at/tuwien/service/FileService.java +++ b/fda-citation-service/services/src/main/java/at/tuwien/service/FileService.java @@ -28,7 +28,7 @@ public interface FileService { */ FileResponseDto createResource(Long databaseId, Long tableId, FileUploadDto data, MultipartFile resource) throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException, - ZenodoFileTooLargeException, MetadataDatabaseNotFoundException; + ZenodoFileTooLargeException, MetadataDatabaseNotFoundException, ZenodoUnavailableException; /** * List all files known to a deposit number (through the database-table id pair) @@ -41,7 +41,7 @@ public interface FileService { * @throws ZenodoNotFoundException The deposit id was not found on the remote server * @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 @@ -55,7 +55,7 @@ public interface FileService { * @throws ZenodoNotFoundException The deposit id was not found on the remote server * @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 @@ -70,5 +70,5 @@ public interface FileService { */ void deleteResource(Long databaseId, Long tableId, String fileId) throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException, - ZenodoApiException; + ZenodoApiException, ZenodoUnavailableException; } diff --git a/fda-citation-service/services/src/main/java/at/tuwien/service/MetadataService.java b/fda-citation-service/services/src/main/java/at/tuwien/service/MetadataService.java index 0c7af980132d38c1ced6885ad24e094f4d407306..94e4f3e272f70b567fecc222d75a21bb756c4784 100644 --- a/fda-citation-service/services/src/main/java/at/tuwien/service/MetadataService.java +++ b/fda-citation-service/services/src/main/java/at/tuwien/service/MetadataService.java @@ -1,10 +1,7 @@ package at.tuwien.service; import at.tuwien.api.zenodo.deposit.*; -import at.tuwien.exception.MetadataDatabaseNotFoundException; -import at.tuwien.exception.ZenodoApiException; -import at.tuwien.exception.ZenodoAuthenticationException; -import at.tuwien.exception.ZenodoNotFoundException; +import at.tuwien.exception.*; import org.springframework.stereotype.Service; import java.util.List; @@ -21,7 +18,7 @@ public interface MetadataService { * @throws ZenodoAuthenticationException Token invalid * @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 @@ -32,7 +29,7 @@ public interface MetadataService { * @throws ZenodoAuthenticationException Token invalid * @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 @@ -46,7 +43,7 @@ public interface MetadataService { * @throws ZenodoNotFoundException The deposit id was not found on the remote server */ 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 @@ -61,7 +58,7 @@ public interface MetadataService { */ DepositResponseDto findCitation(Long databaseId, Long tableId) throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException, - MetadataDatabaseNotFoundException; + MetadataDatabaseNotFoundException, ZenodoUnavailableException; /** * Delete a deposit from a given id @@ -71,5 +68,5 @@ public interface MetadataService { * @throws ZenodoAuthenticationException Token invalid * @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; } diff --git a/fda-citation-service/services/src/main/java/at/tuwien/service/ZenodoFileService.java b/fda-citation-service/services/src/main/java/at/tuwien/service/ZenodoFileService.java index ad2eaeb431abd2c3da2442f3f8d2d0e9eb25de5e..8a6b30de379455c90c7b148194d62640a2a34c67 100644 --- a/fda-citation-service/services/src/main/java/at/tuwien/service/ZenodoFileService.java +++ b/fda-citation-service/services/src/main/java/at/tuwien/service/ZenodoFileService.java @@ -12,11 +12,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.*; 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.multipart.MultipartFile; import javax.transaction.Transactional; -import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.List; @@ -43,7 +43,7 @@ public class ZenodoFileService implements FileService { @Transactional public FileResponseDto createResource(Long databaseId, Long tableId, FileUploadDto data, MultipartFile resource) throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException, - ZenodoFileTooLargeException, MetadataDatabaseNotFoundException { + ZenodoFileTooLargeException, MetadataDatabaseNotFoundException, ZenodoUnavailableException { if (resource.getSize() > 50_1000_1000_1000L) { throw new ZenodoFileTooLargeException("Only 50GB per file is allowed!"); } @@ -54,6 +54,8 @@ public class ZenodoFileService implements FileService { zenodoMapper.resourceToHttpEntity(data.getName(), resource), FileResponseDto.class, table.getDepositId(), zenodoConfig.getApiKey()); } catch (IOException e) { 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) { throw new ZenodoAuthenticationException("Token is missing or invalid."); } catch (HttpClientErrorException.BadRequest e) { @@ -71,12 +73,14 @@ public class ZenodoFileService implements FileService { @Override @Transactional public List<FileResponseDto> listResources(Long databaseId, Long tableId) throws MetadataDatabaseNotFoundException, - ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoApiException { + ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoApiException, ZenodoUnavailableException { final Table table = getTable(databaseId, tableId); final ResponseEntity<FileResponseDto[]> response; try { response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}/files?access_token={token}", 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) { throw new ZenodoNotFoundException("Did not find the resoource with this id"); } catch (HttpClientErrorException.Unauthorized e) { @@ -92,13 +96,15 @@ public class ZenodoFileService implements FileService { @Transactional public FileResponseDto findResource(Long databaseId, Long tableId, String fileId) throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException, - ZenodoApiException { + ZenodoApiException, ZenodoUnavailableException { final Table table = getTable(databaseId, tableId); final ResponseEntity<FileResponseDto> response; try { response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}/files/{file_id}?access_token={token}", HttpMethod.GET, addHeaders(null), FileResponseDto.class, table.getDepositId(), fileId, zenodoConfig.getApiKey()); + } catch (ResourceAccessException e) { + throw new ZenodoUnavailableException("Zenodo host is not reachable from the service network", e); } catch (HttpClientErrorException.NotFound e) { throw new ZenodoNotFoundException("Did not find the resoource with this ID"); } catch (HttpClientErrorException.Unauthorized e) { @@ -113,13 +119,16 @@ public class ZenodoFileService implements FileService { @Override @Transactional 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 ResponseEntity<String> response; try { response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}/files/{file_id}?access_token={token}", HttpMethod.DELETE, addHeaders(null), String.class, table.getDepositId(), fileId, zenodoConfig.getApiKey()); + } catch (ResourceAccessException e) { + throw new ZenodoUnavailableException("Zenodo host is not reachable from the service network", e); } catch (HttpClientErrorException.NotFound e) { throw new ZenodoNotFoundException("Did not find the resource with this ID"); } catch (HttpClientErrorException.Unauthorized e) { @@ -134,7 +143,7 @@ public class ZenodoFileService implements FileService { * Wrapper function to throw error when table with id was not found * * @param databaseId The database id - * @param tableId The table id + * @param tableId The table id * @return The table * @throws MetadataDatabaseNotFoundException The error */ diff --git a/fda-citation-service/services/src/main/java/at/tuwien/service/ZenodoMetadataService.java b/fda-citation-service/services/src/main/java/at/tuwien/service/ZenodoMetadataService.java index 1243b3c9c270e5483edc3f1be45e94dd24804340..ab69c44588aea18cdb89b09845361a19705ca9ca 100644 --- a/fda-citation-service/services/src/main/java/at/tuwien/service/ZenodoMetadataService.java +++ b/fda-citation-service/services/src/main/java/at/tuwien/service/ZenodoMetadataService.java @@ -4,14 +4,13 @@ import at.tuwien.api.zenodo.deposit.*; import at.tuwien.config.ZenodoConfig; import at.tuwien.entities.database.Database; import at.tuwien.entities.database.table.Table; -import at.tuwien.exception.MetadataDatabaseNotFoundException; -import at.tuwien.exception.ZenodoApiException; -import at.tuwien.exception.ZenodoAuthenticationException; -import at.tuwien.exception.ZenodoNotFoundException; +import at.tuwien.exception.*; import at.tuwien.repository.jpa.TableRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.*; import org.springframework.stereotype.Service; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.ResourceAccessException; import org.springframework.web.client.RestTemplate; import javax.transaction.Transactional; @@ -36,9 +35,14 @@ public class ZenodoMetadataService implements MetadataService { @Override @Transactional public List<DepositResponseDto> listCitations(Long databaseId, Long tableId) throws ZenodoAuthenticationException, - ZenodoApiException { - final ResponseEntity<DepositResponseDto[]> response = apiTemplate.exchange("/api/deposit/depositions?access_token={token}", - HttpMethod.GET, addHeaders(null), DepositResponseDto[].class, zenodoConfig.getApiKey()); + ZenodoApiException, ZenodoUnavailableException { + final ResponseEntity<DepositResponseDto[]> response; + try { + response = apiTemplate.exchange("/api/deposit/depositions?access_token={token}", + 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)) { throw new ZenodoAuthenticationException("Token is missing or invalid."); } @@ -51,10 +55,15 @@ public class ZenodoMetadataService implements MetadataService { @Override @Transactional public DepositChangeResponseDto storeCitation(Long databaseId, Long tableId) throws ZenodoAuthenticationException, - ZenodoApiException, MetadataDatabaseNotFoundException { + ZenodoApiException, MetadataDatabaseNotFoundException, ZenodoUnavailableException { final Table table = getTable(databaseId, tableId); - final ResponseEntity<DepositChangeResponseDto> response = apiTemplate.exchange("/api/deposit/depositions?access_token={token}", - HttpMethod.POST, addHeaders("{}"), DepositChangeResponseDto.class, zenodoConfig.getApiKey()); + final ResponseEntity<DepositChangeResponseDto> response; + 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)) { throw new ZenodoAuthenticationException("Token is missing or invalid."); } @@ -73,11 +82,18 @@ public class ZenodoMetadataService implements MetadataService { @Transactional public DepositChangeResponseDto updateCitation(Long databaseId, Long tableId, DepositChangeRequestDto data) throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException, - MetadataDatabaseNotFoundException { + MetadataDatabaseNotFoundException, ZenodoUnavailableException { final Table table = getTable(databaseId, tableId); - final ResponseEntity<DepositChangeResponseDto> response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}?access_token={token}", - HttpMethod.PUT, addHeaders(data), DepositChangeResponseDto.class, table.getDepositId(), - zenodoConfig.getApiKey()); + final ResponseEntity<DepositChangeResponseDto> response; + try { + response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}?access_token={token}", + HttpMethod.PUT, addHeaders(data), DepositChangeResponseDto.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)) { throw new ZenodoAuthenticationException("Token is missing or invalid."); } @@ -96,11 +112,18 @@ public class ZenodoMetadataService implements MetadataService { @Override @Transactional public DepositResponseDto findCitation(Long databaseId, Long tableId) throws ZenodoAuthenticationException, - ZenodoApiException, ZenodoNotFoundException, MetadataDatabaseNotFoundException { + ZenodoApiException, ZenodoNotFoundException, MetadataDatabaseNotFoundException, ZenodoUnavailableException { final Table table = getTable(databaseId, tableId); - final ResponseEntity<DepositResponseDto> response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}?access_token={token}", - HttpMethod.GET, addHeaders(null), DepositResponseDto.class, table.getDepositId(), - zenodoConfig.getApiKey()); + final ResponseEntity<DepositResponseDto> response; + try { + response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}?access_token={token}", + HttpMethod.GET, addHeaders(null), DepositResponseDto.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)) { throw new ZenodoAuthenticationException("Token is missing or invalid."); } @@ -116,10 +139,17 @@ public class ZenodoMetadataService implements MetadataService { @Override @Transactional public void deleteCitation(Long databaseId, Long tableId) throws ZenodoAuthenticationException, ZenodoApiException, - MetadataDatabaseNotFoundException { + MetadataDatabaseNotFoundException, ZenodoUnavailableException, ZenodoNotFoundException { final Table table = getTable(databaseId, tableId); - final ResponseEntity<String> response = apiTemplate.exchange("/api/deposit/depositions/{deposit_id}?access_token={token}", - HttpMethod.DELETE, addHeaders(null), String.class, table.getDepositId(), zenodoConfig.getApiKey()); + 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()); + } 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)) { throw new ZenodoAuthenticationException("Token is missing or invalid."); } diff --git a/fda-ui/components/QueryList.vue b/fda-ui/components/QueryList.vue index 1b1cad9609d22096c1b3049db3acf4f24c0f41cc..c2a5759d2abe15c1c54f0348c01ca8ac246a3392 100644 --- a/fda-ui/components/QueryList.vue +++ b/fda-ui/components/QueryList.vue @@ -73,6 +73,9 @@ <v-btn color="primary" :to="`/databases/${$route.params.database_id}/queries/${item.id}`"> <v-icon left>mdi-run</v-icon> Execute Again </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-row> </v-expansion-panel-content> diff --git a/fda-ui/layouts/default.vue b/fda-ui/layouts/default.vue index 25eed6ac8c0e282688b02c026bea92466d83f0aa..242fbdd34ff9615ed6bf6a3dcb62e62f37c0b08f 100644 --- a/fda-ui/layouts/default.vue +++ b/fda-ui/layouts/default.vue @@ -93,7 +93,7 @@ export default { needsContainer: true } ], - title: 'FAIR Data Repository' + title: 'FAIR Data Austria — Database Repository' } }, computed: { diff --git a/fda-ui/pages/databases/_database_id/queries/_query_id/metadata.vue b/fda-ui/pages/databases/_database_id/queries/_query_id/metadata.vue new file mode 100644 index 0000000000000000000000000000000000000000..c9caf90e8e0d78048c68130740bc2da4650f54d9 --- /dev/null +++ b/fda-ui/pages/databases/_database_id/queries/_query_id/metadata.vue @@ -0,0 +1,34 @@ +<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>