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

added more tests

Former-commit-id: 6e591f0e
parent 849b689f
No related branches found
No related tags found
1 merge request!42Fixed the query service tests
......@@ -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);
}
......
......@@ -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
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
......@@ -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;
}
......@@ -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));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment