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

fixed some tests

parent 1d2ad718
No related branches found
No related tags found
3 merge requests!81New stable release,!43Merge dev to master,!27Draft: Resolve "Zenodo Sandbox integration for PID (e.g. DOI)"
Showing
with 207 additions and 51 deletions
package at.tuwien.api.zenodo.files;
import lombok.*;
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FileUploadDto {
private String name;
}
package at.tuwien.endpoints;
import at.tuwien.service.MetadataService;
import at.tuwien.api.zenodo.files.FileResponseDto;
import at.tuwien.exception.MetadataDatabaseNotFoundException;
import at.tuwien.exception.ZenodoAuthenticationException;
import at.tuwien.service.FileService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@Log4j2
@CrossOrigin(origins = "*")
......@@ -13,13 +17,37 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/api/database/{id}/table/{tableid}/file")
public class FileEndpoint {
private final MetadataService citationService;
private final FileService fileService;
@Autowired
public FileEndpoint(MetadataService citationService) {
this.citationService = citationService;
public FileEndpoint(FileService fileService) {
this.fileService = fileService;
}
@GetMapping
public List<FileResponseDto> listAll(@Valid @RequestParam("id") Long databaseId,
@Valid @RequestParam("tableId") Long tableId)
throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException {
return fileService.listAll(databaseId, tableId);
}
// public FileResponseDto find(@Valid @RequestParam("id") Long databaseId,
// @Valid @RequestParam("tableId") Long tableId) {
//
// }
//
// public FileResponseDto create(@Valid @RequestParam("id") Long databaseId,
// @Valid @RequestParam("tableId") Long tableId) {
//
// }
//
// public FileResponseDto update(@Valid @RequestParam("id") Long databaseId,
// @Valid @RequestParam("tableId") Long tableId) {
//
// }
//
// public FileResponseDto delete(@Valid @RequestParam("id") Long databaseId,
// @Valid @RequestParam("tableId") Long tableId) {
//
// }
}
......@@ -3,6 +3,8 @@ package at.tuwien;
import at.tuwien.api.zenodo.deposit.*;
import at.tuwien.api.zenodo.files.FileResponseDto;
import at.tuwien.api.zenodo.files.FileLinksDto;
import at.tuwien.entities.database.Database;
import at.tuwien.entities.database.table.Table;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.test.context.TestPropertySource;
......@@ -13,6 +15,10 @@ import java.util.List;
@TestPropertySource(locations = "classpath:application.properties")
public abstract class BaseUnitTest {
public final static Long DATABASE_1_ID = 1L;
public final static Long TABLE_1_ID = 1L;
public final static Long DEPOSIT_1_ID = 1L;
public final static String DEPOSIT_1_TITLE = "Super cool document";
public final static String DEPOSIT_1_DESCRIPTION = "My document is the best";
......@@ -62,6 +68,16 @@ public abstract class BaseUnitTest {
public final static String DEPOSIT_1_DOI = "10.5072/zenodo.542201";
public final static Long DEPOSIT_1_REC_ID = 542201L;
public final static Table TABLE_1 = Table.builder()
.id(TABLE_1_ID)
.depositId(DEPOSIT_1_ID)
.build();
public final static Database DATABASE_1 = Database.builder()
.id(DATABASE_1_ID)
.tables(List.of(TABLE_1))
.build();
public final static CreatorDto CREATOR_1 = CreatorDto.builder()
.name(CREATOR_1_NAME)
.affiliation(CREATOR_1_AFFIL)
......
......@@ -3,11 +3,11 @@ package at.tuwien.service;
import at.tuwien.BaseUnitTest;
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.exception.ZenodoApiException;
import at.tuwien.exception.ZenodoAuthenticationException;
import at.tuwien.exception.ZenodoFileTooLargeException;
import at.tuwien.exception.ZenodoNotFoundException;
import at.tuwien.entities.database.table.Table;
import at.tuwien.exception.*;
import at.tuwien.repository.jpa.TableRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -18,8 +18,10 @@ import org.springframework.util.ResourceUtils;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
@SpringBootTest
@ExtendWith(SpringExtension.class)
......@@ -28,6 +30,9 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
@MockBean
private ReadyConfig readyConfig;
@MockBean
private TableRepository tableRepository;
@Autowired
private ZenodoFileService fileService;
......@@ -36,12 +41,25 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
@Test
public void createResource_succeeds() throws IOException, ZenodoApiException, ZenodoNotFoundException,
ZenodoAuthenticationException, ZenodoFileTooLargeException {
final DepositChangeResponseDto deposit = metadataService.storeCitation();
ZenodoAuthenticationException, ZenodoFileTooLargeException, MetadataDatabaseNotFoundException {
final File file = ResourceUtils.getFile("classpath:csv/testdata.csv");
/* request */
final DepositChangeResponseDto deposit = metadataService.storeCitation();
final FileUploadDto request = FileUploadDto.builder()
.name(FILE_1_NAME)
.build();
/* mock */
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));
/* test */
final FileResponseDto response = fileService.createResource(deposit.getId(), FILE_1_NAME, file);
final FileResponseDto response = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, request, file);
assertEquals(FILE_1_NAME, response.getFilename());
assertEquals(FILE_1_CHECKSUM, response.getChecksum());
assertEquals(FILE_1_SIZE, response.getFilesize());
......@@ -49,12 +67,25 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
@Test
public void createResource_largeFile_succeeds() throws IOException, ZenodoApiException, ZenodoNotFoundException,
ZenodoAuthenticationException, ZenodoFileTooLargeException {
final DepositChangeResponseDto deposit = metadataService.storeCitation();
ZenodoAuthenticationException, ZenodoFileTooLargeException, MetadataDatabaseNotFoundException {
final File file = ResourceUtils.getFile("classpath:csv/weatherAUS.csv");
/* request */
final DepositChangeResponseDto deposit = metadataService.storeCitation();
final FileUploadDto request = FileUploadDto.builder()
.name(FILE_2_NAME)
.build();
/* mock */
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));
/* test */
final FileResponseDto response = fileService.createResource(deposit.getId(), FILE_2_NAME, file);
final FileResponseDto response = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, request, file);
assertEquals(FILE_2_NAME, response.getFilename());
assertEquals(FILE_2_CHECKSUM, response.getChecksum());
assertEquals(FILE_2_SIZE, response.getFilesize());
......
......@@ -2,11 +2,10 @@ package at.tuwien.service;
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.exception.ZenodoApiException;
import at.tuwien.exception.ZenodoAuthenticationException;
import at.tuwien.exception.ZenodoFileTooLargeException;
import at.tuwien.exception.ZenodoNotFoundException;
import at.tuwien.exception.*;
import at.tuwien.repository.jpa.TableRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
......@@ -24,6 +23,7 @@ import org.springframework.web.client.RestTemplate;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
......@@ -45,21 +45,29 @@ public class FileServiceUnitTest extends BaseUnitTest {
@MockBean
private RestTemplate apiTemplate;
@MockBean
private TableRepository tableRepository;
@Test
public void createResource_succeeds() throws IOException, ZenodoApiException, ZenodoNotFoundException,
ZenodoAuthenticationException, ZenodoFileTooLargeException {
ZenodoAuthenticationException, ZenodoFileTooLargeException, MetadataDatabaseNotFoundException {
final File file = ResourceUtils.getFile("classpath:csv/testdata.csv");
/* mock */
when(apiTemplate.postForEntity(anyString(), Mockito.<MultiValueMap<String, HttpEntity<?>>>any(),
eq(FileResponseDto.class), anyLong(), anyString()))
eq(FileResponseDto.class), eq(DEPOSIT_1_ID), anyString()))
.thenReturn(ResponseEntity.status(HttpStatus.OK)
.body(FILE_1));
when(tableRepository.findById(TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* request */
final File file = ResourceUtils.getFile("classpath:csv/testdata.csv");
final FileUploadDto request = FileUploadDto.builder()
.name(FILE_1_NAME)
.build();
/* test */
final FileResponseDto response = fileService.createResource(DEPOSIT_1_ID, FILE_1_NAME, file);
final FileResponseDto response = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, request, file);
assertEquals(FILE_1_NAME, response.getFilename());
assertEquals(FILE_1_CHECKSUM, response.getChecksum());
assertEquals(FILE_1_SIZE, response.getFilesize());
......@@ -67,19 +75,24 @@ public class FileServiceUnitTest extends BaseUnitTest {
@Test
public void createResource_notExists_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), anyLong(), anyString()))
eq(FileResponseDto.class), eq(DEPOSIT_1_ID), anyString()))
.thenReturn(ResponseEntity.status(HttpStatus.BAD_REQUEST)
.build());
when(tableRepository.findById(TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* request */
final File file = ResourceUtils.getFile("classpath:csv/testdata.csv");
final FileUploadDto request = FileUploadDto.builder()
.name(FILE_1_NAME)
.build();
/* test */
assertThrows(ZenodoNotFoundException.class, () -> {
fileService.createResource(DEPOSIT_1_ID, FILE_1_NAME, file);
fileService.createResource(DATABASE_1_ID, TABLE_1_ID, request, file);
});
}
......
#!/bin/bash
docker pull mariadb:latest
docker pull mysql:latest
docker pull postgres:latest
\ No newline at end of file
#!/bin/bash
\ No newline at end of file
package at.tuwien.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.NOT_FOUND)
public class MetadataDatabaseNotFoundException extends Exception {
public MetadataDatabaseNotFoundException(String msg) {
super(msg);
}
public MetadataDatabaseNotFoundException(String msg, Throwable thr) {
super(msg, thr);
}
public MetadataDatabaseNotFoundException(Throwable thr) {
super(thr);
}
}
package at.tuwien.service;
import at.tuwien.api.zenodo.files.FileResponseDto;
import at.tuwien.exception.ZenodoApiException;
import at.tuwien.exception.ZenodoAuthenticationException;
import at.tuwien.exception.ZenodoFileTooLargeException;
import at.tuwien.exception.ZenodoNotFoundException;
import at.tuwien.api.zenodo.files.FileUploadDto;
import at.tuwien.exception.*;
import org.springframework.stereotype.Service;
import java.io.File;
import java.util.List;
@Service
public interface FileService {
FileResponseDto createResource(Long id, String name, File resource)
FileResponseDto createResource(Long databaseId, Long tableId, FileUploadDto data, File resource)
throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException,
ZenodoFileTooLargeException;
ZenodoFileTooLargeException, MetadataDatabaseNotFoundException;
List<FileResponseDto> listAll(Long databaseId, Long tableId) throws MetadataDatabaseNotFoundException, ZenodoAuthenticationException;
}
package at.tuwien.service;
import at.tuwien.api.zenodo.files.FileResponseDto;
import at.tuwien.api.zenodo.files.FileUploadDto;
import at.tuwien.config.ZenodoConfig;
import at.tuwien.exception.ZenodoApiException;
import at.tuwien.exception.ZenodoAuthenticationException;
import at.tuwien.exception.ZenodoFileTooLargeException;
import at.tuwien.exception.ZenodoNotFoundException;
import at.tuwien.entities.database.table.Table;
import at.tuwien.exception.*;
import at.tuwien.mapper.ZenodoMapper;
import at.tuwien.repository.jpa.TableRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
......@@ -15,35 +15,37 @@ import org.springframework.web.client.RestTemplate;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
@Service
public class ZenodoFileService implements FileService {
private final RestTemplate apiTemplate;
private final RestTemplate uploadTemplate;
private final ZenodoConfig zenodoConfig;
private final ZenodoMapper zenodoMapper;
private final TableRepository tableRepository;
@Autowired
public ZenodoFileService(RestTemplate apiTemplate, RestTemplate uploadTemplate, ZenodoConfig zenodoConfig,
ZenodoMapper zenodoMapper) {
public ZenodoFileService(RestTemplate apiTemplate, ZenodoConfig zenodoConfig, ZenodoMapper zenodoMapper,
TableRepository tableRepository) {
this.apiTemplate = apiTemplate;
this.uploadTemplate = uploadTemplate;
this.zenodoConfig = zenodoConfig;
this.zenodoMapper = zenodoMapper;
this.tableRepository = tableRepository;
}
@Override
public FileResponseDto createResource(Long id, String name, File resource)
public FileResponseDto createResource(Long databaseId, Long tableId, FileUploadDto data, File resource)
throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException,
ZenodoFileTooLargeException {
ZenodoFileTooLargeException, MetadataDatabaseNotFoundException {
if (resource.getTotalSpace() > 50_1000_1000_1000L) {
throw new ZenodoFileTooLargeException("Only 50GB per file is allowed!");
}
final Table table = getTable(tableId);
final ResponseEntity<FileResponseDto> response;
try {
response = uploadTemplate.postForEntity("/api/deposit/depositions/{deposit_id}/files?access_token={token}",
zenodoMapper.resourceToHttpEntity(name, resource), FileResponseDto.class, id, zenodoConfig.getApiKey());
response = apiTemplate.postForEntity("/api/deposit/depositions/{deposit_id}/files?access_token={token}",
zenodoMapper.resourceToHttpEntity(data.getName(), resource), FileResponseDto.class, table.getDepositId(), zenodoConfig.getApiKey());
} catch (IOException e) {
throw new ZenodoApiException("Could not map file to byte array");
}
......@@ -59,4 +61,28 @@ public class ZenodoFileService implements FileService {
return response.getBody();
}
@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();
}
private Table getTable(Long tableId) throws MetadataDatabaseNotFoundException {
final Optional<Table> table = tableRepository.findById(tableId);
if (table.isEmpty()) {
throw new MetadataDatabaseNotFoundException("Failed to find table with this id");
}
return table.get();
}
private HttpEntity<Object> addHeaders(Object body) {
final HttpHeaders headers = new HttpHeaders();
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
return new HttpEntity<>(body, headers);
}
}
......@@ -20,6 +20,10 @@ public class TableDto {
@ApiModelProperty(name = "table id", example = "1")
private Long id;
@NotNull
@ApiModelProperty(name = "deposition id", example = "100")
private Long depositId;
@NotBlank
@ApiModelProperty(name = "table name", example = "Weather Australia")
private String name;
......
......@@ -40,6 +40,10 @@ public class Table {
@ToString.Include
private Long tdbid;
@ToString.Include
@Column(nullable = true, name = "depId")
private Long depositId;
@ToString.Include
@Column(nullable = false, name = "tname")
private String name;
......
......@@ -192,6 +192,7 @@ psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-E
internal_name character varying(255) NOT NULL,
topic character varying(255) NOT NULL,
last_modified timestamp without time zone,
depId bigint UNIQUE,
tName VARCHAR(50),
tDescription TEXT,
NumCols INTEGER,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment