From f0f3a3322309bf93384916baeca5faf2d492fd5d Mon Sep 17 00:00:00 2001 From: Martin Weise <martin.weise@tuwien.ac.at> Date: Wed, 29 Sep 2021 13:38:13 +0200 Subject: [PATCH] added more tests Former-commit-id: 6e591f0ed18991fa7450f4fc17df5555a0ffc69d --- .../at/tuwien/endpoints/FileEndpoint.java | 5 +- .../service/FileServiceIntegrationTest.java | 14 ++++ .../tuwien/service/FileServiceUnitTest.java | 68 ++++++++++++++++++- .../java/at/tuwien/service/FileService.java | 2 +- .../at/tuwien/service/ZenodoFileService.java | 39 +++++++++-- 5 files changed, 120 insertions(+), 8 deletions(-) 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 6b432e2bbf..60b58785eb 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 @@ -2,7 +2,9 @@ package at.tuwien.endpoints; import at.tuwien.api.zenodo.files.FileResponseDto; import at.tuwien.exception.MetadataDatabaseNotFoundException; +import at.tuwien.exception.ZenodoApiException; import at.tuwien.exception.ZenodoAuthenticationException; +import at.tuwien.exception.ZenodoNotFoundException; import at.tuwien.service.FileService; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; @@ -27,7 +29,8 @@ public class FileEndpoint { @GetMapping public List<FileResponseDto> listAll(@Valid @RequestParam("id") Long databaseId, @Valid @RequestParam("tableId") Long tableId) - throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException { + throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoApiException, + ZenodoNotFoundException { return fileService.listAll(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 d67e5eab00..cc18b5b1a3 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 @@ -21,6 +21,7 @@ import java.io.IOException; import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.when; @SpringBootTest @@ -91,4 +92,17 @@ public class FileServiceIntegrationTest extends BaseUnitTest { assertEquals(FILE_2_SIZE, response.getFilesize()); } + @Test + public void listAll_notFound_fails() { + + /* mock */ + when(tableRepository.findById(TABLE_1_ID)) + .thenReturn(Optional.of(TABLE_1)); + + /* test */ + assertThrows(ZenodoNotFoundException.class, () -> { + fileService.listAll(DATABASE_1_ID, TABLE_1_ID); + }); + } + } \ No newline at end of file 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 3893fe79a4..f4cf2e7f54 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 @@ -1,6 +1,7 @@ package at.tuwien.service; import at.tuwien.BaseUnitTest; +import at.tuwien.api.zenodo.deposit.DepositResponseDto; import at.tuwien.api.zenodo.files.FileResponseDto; import at.tuwien.api.zenodo.files.FileUploadDto; import at.tuwien.config.ReadyConfig; @@ -23,13 +24,13 @@ import org.springframework.web.client.RestTemplate; import java.io.File; import java.io.IOException; +import java.util.List; 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.Mockito.anyLong; import static org.mockito.Mockito.when; @SpringBootTest @@ -96,4 +97,69 @@ public class FileServiceUnitTest extends BaseUnitTest { }); } + @Test + public void createResource_bodyEmpty_fails() throws IOException { + final File file = ResourceUtils.getFile("classpath:csv/testdata.csv"); + + /* mock */ + 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)) + .thenReturn(Optional.of(TABLE_1)); + + /* request */ + final FileUploadDto request = FileUploadDto.builder() + .name(FILE_1_NAME) + .build(); + + /* test */ + assertThrows(ZenodoApiException.class, () -> { + fileService.createResource(DATABASE_1_ID, TABLE_1_ID, request, file); + }); + } + + @Test + public void listAll_succeeds() throws MetadataDatabaseNotFoundException, ZenodoApiException, + ZenodoNotFoundException, ZenodoAuthenticationException { + + /* 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)) + .thenReturn(Optional.of(TABLE_1)); + + /* test */ + final List<FileResponseDto> response = fileService.listAll(DATABASE_1_ID, TABLE_1_ID); + assertEquals(1, response.size()); + } + + @Test + public void listAll_noContent_fails() { + + /* mock */ + 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)) + .thenReturn(Optional.of(TABLE_1)); + + /* test */ + assertThrows(ZenodoApiException.class, () -> { + fileService.listAll(DATABASE_1_ID, TABLE_1_ID); + }); + } + + @Test + public void listAll_notFound_fails() { + + /* mock */ + when(tableRepository.findById(TABLE_1_ID)) + .thenReturn(Optional.of(TABLE_1)); + + /* test */ + assertThrows(MetadataDatabaseNotFoundException.class, () -> { + fileService.listAll(DATABASE_1_ID, 9999L); + }); + } + } \ No newline at end of file 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 a4a1d90ac7..33156ab397 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 @@ -15,5 +15,5 @@ public interface FileService { throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException, ZenodoFileTooLargeException, MetadataDatabaseNotFoundException; - List<FileResponseDto> listAll(Long databaseId, Long tableId) throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException; + List<FileResponseDto> listAll(Long databaseId, Long tableId) throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoApiException; } 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 9567b9b135..d742822a0e 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 @@ -10,10 +10,12 @@ 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.RestTemplate; import java.io.File; import java.io.IOException; +import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -63,13 +65,34 @@ public class ZenodoFileService implements FileService { @Override public List<FileResponseDto> listAll(Long databaseId, Long tableId) throws MetadataDatabaseNotFoundException, - ZenodoAuthenticationException { -// final Table table = getTable(tableId); -// final ResponseEntity<FileResponseDto> response = apiTemplate.postForEntity("/api/deposit/depositions/{deposit_id}/files?access_token={token}", -// addHeaders(null), FileResponseDto.class, table.getDepositId(), zenodoConfig.getApiKey()); - return List.of(); + ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoApiException { + final Table table = getTable(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 (HttpClientErrorException.NotFound e) { + throw new ZenodoNotFoundException("Did not find the deposit with this id"); + } + if (response.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) { + throw new ZenodoAuthenticationException("Token is missing or invalid."); + } + if (response.getStatusCode().equals(HttpStatus.NOT_FOUND)) { + throw new ZenodoNotFoundException("Did not find the deposit with this id"); + } + if (response.getBody() == null) { + throw new ZenodoApiException("Endpoint returned null body"); + } + return Arrays.asList(response.getBody()); } + /** + * Wrapper function to throw error when table with id was not found + * + * @param tableId The table id + * @return The table + * @throws MetadataDatabaseNotFoundException The error + */ private Table getTable(Long tableId) throws MetadataDatabaseNotFoundException { final Optional<Table> table = tableRepository.findById(tableId); if (table.isEmpty()) { @@ -78,6 +101,12 @@ public class ZenodoFileService implements FileService { return table.get(); } + /** + * Wrapper to add headers to all non-file upload requests + * + * @param body The request data + * @return The request with headers + */ private HttpEntity<Object> addHeaders(Object body) { final HttpHeaders headers = new HttpHeaders(); headers.setAccept(List.of(MediaType.APPLICATION_JSON)); -- GitLab