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

even more tests!

Former-commit-id: b9b55213
parent f0f3a332
Branches
Tags
1 merge request!42Fixed the query service tests
...@@ -15,6 +15,8 @@ public class FileResponseDto { ...@@ -15,6 +15,8 @@ public class FileResponseDto {
private Long filesize; private Long filesize;
private Boolean locked;
private String id; private String id;
private FileLinksDto links; private FileLinksDto links;
......
...@@ -31,7 +31,7 @@ public class FileEndpoint { ...@@ -31,7 +31,7 @@ public class FileEndpoint {
@Valid @RequestParam("tableId") Long tableId) @Valid @RequestParam("tableId") Long tableId)
throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoApiException, throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoApiException,
ZenodoNotFoundException { ZenodoNotFoundException {
return fileService.listAll(databaseId, tableId); return fileService.listResources(databaseId, tableId);
} }
// public FileResponseDto find(@Valid @RequestParam("id") Long databaseId, // public FileResponseDto find(@Valid @RequestParam("id") Long databaseId,
......
...@@ -8,20 +8,27 @@ import at.tuwien.config.ReadyConfig; ...@@ -8,20 +8,27 @@ import at.tuwien.config.ReadyConfig;
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.junit.jupiter.api.Order;
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.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
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.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.*;
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.when; import static org.mockito.Mockito.when;
@SpringBootTest @SpringBootTest
...@@ -55,7 +62,7 @@ public class FileServiceIntegrationTest extends BaseUnitTest { ...@@ -55,7 +62,7 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
final Table TABLE_1 = Table.builder() final Table TABLE_1 = Table.builder()
.id(TABLE_1_ID) .id(TABLE_1_ID)
.depositId(deposit.getId()) .depositId(deposit.getId())
.build();; .build();
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findById(TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
...@@ -101,8 +108,91 @@ public class FileServiceIntegrationTest extends BaseUnitTest { ...@@ -101,8 +108,91 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
/* test */ /* test */
assertThrows(ZenodoNotFoundException.class, () -> { assertThrows(ZenodoNotFoundException.class, () -> {
fileService.listAll(DATABASE_1_ID, TABLE_1_ID); fileService.listResources(DATABASE_1_ID, TABLE_1_ID);
}); });
} }
@Test
public void listAll_succeeds() throws MetadataDatabaseNotFoundException, ZenodoApiException,
ZenodoNotFoundException, ZenodoAuthenticationException, FileNotFoundException, ZenodoFileTooLargeException {
final File file = ResourceUtils.getFile("classpath:csv/testdata.csv");
/* request */
final DepositChangeResponseDto deposit = metadataService.storeCitation();
final FileUploadDto upload = FileUploadDto.builder()
.name(FILE_1_NAME)
.build();
final Table TABLE_1 = Table.builder()
.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 */
when(tableRepository.findById(TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* test */
final List<FileResponseDto> listResponse = fileService.listResources(DATABASE_1_ID, TABLE_1_ID);
assertEquals(1, listResponse.size());
assertEquals(FILE_1_CHECKSUM, listResponse.get(0).getChecksum());
assertEquals(fileResponse.getId(), listResponse.get(0).getId());
}
@Test
public void findResource_noContent_fails() throws MetadataDatabaseNotFoundException, ZenodoApiException,
ZenodoFileTooLargeException, ZenodoNotFoundException, ZenodoAuthenticationException, FileNotFoundException {
final File file = ResourceUtils.getFile("classpath:csv/testdata.csv");
/* request */
final DepositChangeResponseDto deposit = metadataService.storeCitation();
final FileUploadDto upload = FileUploadDto.builder()
.name(FILE_1_NAME)
.build();
final Table TABLE_1 = Table.builder()
.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 */
when(tableRepository.findById(TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* test */
final FileResponseDto findResponse = fileService.findResource(DATABASE_1_ID, TABLE_1_ID, fileResponse.getId());
assertEquals(FILE_1_CHECKSUM, findResponse.getChecksum());
assertEquals(fileResponse.getId(), findResponse.getId());
}
@Test
public void deleteRessource_succeeds() throws MetadataDatabaseNotFoundException, ZenodoApiException,
ZenodoFileTooLargeException, ZenodoNotFoundException, ZenodoAuthenticationException, FileNotFoundException {
final File file = ResourceUtils.getFile("classpath:csv/testdata.csv");
/* request */
final DepositChangeResponseDto deposit = metadataService.storeCitation();
final FileUploadDto upload = FileUploadDto.builder()
.name(FILE_1_NAME)
.build();
final Table TABLE_1 = Table.builder()
.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 */
when(tableRepository.findById(TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* test */
fileService.deleteResource(DATABASE_1_ID, TABLE_1_ID, fileResponse.getId());
}
} }
\ No newline at end of file
package at.tuwien.service; package at.tuwien.service;
import at.tuwien.BaseUnitTest; import at.tuwien.BaseUnitTest;
import at.tuwien.api.zenodo.deposit.DepositResponseDto;
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;
...@@ -124,13 +123,14 @@ public class FileServiceUnitTest extends BaseUnitTest { ...@@ -124,13 +123,14 @@ public class FileServiceUnitTest extends BaseUnitTest {
ZenodoNotFoundException, ZenodoAuthenticationException { ZenodoNotFoundException, ZenodoAuthenticationException {
/* mock */ /* mock */
when(apiTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(FileResponseDto[].class), eq(DEPOSIT_1_ID), anyString())) 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})); .thenReturn(ResponseEntity.ok().body(new FileResponseDto[]{FILE_1}));
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findById(TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
/* test */ /* test */
final List<FileResponseDto> response = fileService.listAll(DATABASE_1_ID, TABLE_1_ID); final List<FileResponseDto> response = fileService.listResources(DATABASE_1_ID, TABLE_1_ID);
assertEquals(1, response.size()); assertEquals(1, response.size());
} }
...@@ -138,14 +138,15 @@ public class FileServiceUnitTest extends BaseUnitTest { ...@@ -138,14 +138,15 @@ public class FileServiceUnitTest extends BaseUnitTest {
public void listAll_noContent_fails() { public void listAll_noContent_fails() {
/* mock */ /* mock */
when(apiTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(FileResponseDto[].class), eq(DEPOSIT_1_ID), anyString())) when(apiTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(FileResponseDto[].class),
eq(DEPOSIT_1_ID), anyString()))
.thenReturn(ResponseEntity.ok().body(null)); .thenReturn(ResponseEntity.ok().body(null));
when(tableRepository.findById(TABLE_1_ID)) when(tableRepository.findById(TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1)); .thenReturn(Optional.of(TABLE_1));
/* test */ /* test */
assertThrows(ZenodoApiException.class, () -> { assertThrows(ZenodoApiException.class, () -> {
fileService.listAll(DATABASE_1_ID, TABLE_1_ID); fileService.listResources(DATABASE_1_ID, TABLE_1_ID);
}); });
} }
...@@ -158,7 +159,86 @@ public class FileServiceUnitTest extends BaseUnitTest { ...@@ -158,7 +159,86 @@ public class FileServiceUnitTest extends BaseUnitTest {
/* test */ /* test */
assertThrows(MetadataDatabaseNotFoundException.class, () -> { assertThrows(MetadataDatabaseNotFoundException.class, () -> {
fileService.listAll(DATABASE_1_ID, 9999L); fileService.listResources(DATABASE_1_ID, 9999L);
});
}
@Test
public void findResource_succeeds() throws MetadataDatabaseNotFoundException, ZenodoApiException,
ZenodoNotFoundException, ZenodoAuthenticationException {
/* 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))
.thenReturn(Optional.of(TABLE_1));
/* test */
final FileResponseDto file = fileService.findResource(DATABASE_1_ID, TABLE_1_ID, FILE_1_ID);
assertEquals(FILE_1_ID, file.getId());
assertEquals(FILE_1_NAME, file.getFilename());
assertEquals(FILE_1_SIZE, file.getFilesize());
assertEquals(FILE_1_CHECKSUM, file.getChecksum());
}
@Test
public void findResource_noContent_fails() {
/* 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(null));
when(tableRepository.findById(TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* test */
assertThrows(ZenodoApiException.class, () -> {
fileService.findResource(DATABASE_1_ID, TABLE_1_ID, FILE_1_ID);
});
}
@Test
public void findResource_notFound_fails() {
/* mock */
when(tableRepository.findById(TABLE_1_ID))
.thenReturn(Optional.empty());
/* test */
assertThrows(MetadataDatabaseNotFoundException.class, () -> {
fileService.findResource(DATABASE_1_ID, TABLE_1_ID, FILE_1_ID);
});
}
@Test
public void deleteResource_succeeds() throws MetadataDatabaseNotFoundException, ZenodoApiException,
ZenodoNotFoundException, ZenodoAuthenticationException {
/* 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))
.thenReturn(Optional.of(TABLE_1));
/* test */
fileService.deleteResource(DATABASE_1_ID, TABLE_1_ID, FILE_1_ID);
}
@Test
public void deleteResource_wrongStatus_fails() {
/* 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.OK).build());
when(tableRepository.findById(TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* test */
assertThrows(ZenodoApiException.class, () -> {
fileService.deleteResource(DATABASE_1_ID, TABLE_1_ID, FILE_1_ID);
}); });
} }
......
...@@ -15,5 +15,11 @@ public interface FileService { ...@@ -15,5 +15,11 @@ public interface FileService {
throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException, throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException,
ZenodoFileTooLargeException, MetadataDatabaseNotFoundException; ZenodoFileTooLargeException, MetadataDatabaseNotFoundException;
List<FileResponseDto> listAll(Long databaseId, Long tableId) throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoApiException; List<FileResponseDto> listResources(Long databaseId, Long tableId) throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoApiException;
FileResponseDto findResource(Long databaseId, Long tableId, String fileId) throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoApiException;
void deleteResource(Long databaseId, Long tableId, String fileId)
throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException,
ZenodoApiException;
} }
...@@ -50,12 +50,13 @@ public class ZenodoFileService implements FileService { ...@@ -50,12 +50,13 @@ 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 (HttpClientErrorException.Unauthorized e) {
if (response.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
throw new ZenodoAuthenticationException("Token is missing or invalid."); throw new ZenodoAuthenticationException("Token is missing or invalid.");
} catch (HttpClientErrorException.BadRequest e) {
throw new ZenodoNotFoundException("Did not find the resource with this id");
} }
if (response.getStatusCode().equals(HttpStatus.BAD_REQUEST)) { if (response.getStatusCode().equals(HttpStatus.BAD_REQUEST)) {
throw new ZenodoNotFoundException("Did not find the deposit with this id"); throw new ZenodoNotFoundException("Did not find the resource with this id");
} }
if (response.getBody() == null) { if (response.getBody() == null) {
throw new ZenodoApiException("Endpoint returned null body"); throw new ZenodoApiException("Endpoint returned null body");
...@@ -64,7 +65,7 @@ public class ZenodoFileService implements FileService { ...@@ -64,7 +65,7 @@ public class ZenodoFileService implements FileService {
} }
@Override @Override
public List<FileResponseDto> listAll(Long databaseId, Long tableId) throws MetadataDatabaseNotFoundException, public List<FileResponseDto> listResources(Long databaseId, Long tableId) throws MetadataDatabaseNotFoundException,
ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoApiException { ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoApiException {
final Table table = getTable(tableId); final Table table = getTable(tableId);
final ResponseEntity<FileResponseDto[]> response; final ResponseEntity<FileResponseDto[]> response;
...@@ -72,20 +73,56 @@ public class ZenodoFileService implements FileService { ...@@ -72,20 +73,56 @@ public class ZenodoFileService implements FileService {
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 (HttpClientErrorException.NotFound e) { } catch (HttpClientErrorException.NotFound e) {
throw new ZenodoNotFoundException("Did not find the deposit with this id"); throw new ZenodoNotFoundException("Did not find the resoource with this id");
} } catch (HttpClientErrorException.Unauthorized e) {
if (response.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
throw new ZenodoAuthenticationException("Token is missing or invalid."); 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) { if (response.getBody() == null) {
throw new ZenodoApiException("Endpoint returned null body"); throw new ZenodoApiException("Endpoint returned null body");
} }
return Arrays.asList(response.getBody()); return Arrays.asList(response.getBody());
} }
@Override
public FileResponseDto findResource(Long databaseId, Long tableId, String fileId)
throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException,
ZenodoApiException {
final Table table = getTable(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 (HttpClientErrorException.NotFound e) {
throw new ZenodoNotFoundException("Did not find the resoource with this ID");
} catch (HttpClientErrorException.Unauthorized e) {
throw new ZenodoAuthenticationException("Token is missing or invalid.");
}
if (response.getBody() == null) {
throw new ZenodoApiException("Endpoint returned null body");
}
return response.getBody();
}
@Override
public void deleteResource(Long databaseId, Long tableId, String fileId)
throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoApiException {
final Table table = getTable(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 (HttpClientErrorException.NotFound e) {
throw new ZenodoNotFoundException("Did not find the resource with this ID");
} catch (HttpClientErrorException.Unauthorized e) {
throw new ZenodoAuthenticationException("Token is missing or invalid.");
}
if (!response.getStatusCode().equals(HttpStatus.NO_CONTENT)) {
throw new ZenodoApiException("Failed to delete the resource with this ID");
}
}
/** /**
* Wrapper function to throw error when table with id was not found * Wrapper function to throw error when table with id was not found
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment