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

added more annotations

Former-commit-id: 6c2e74cb
parent d15816bb
Branches
Tags
1 merge request!42Fixed the query service tests
Showing
with 208 additions and 159 deletions
......@@ -15,7 +15,7 @@ import java.util.stream.Collectors;
@Log4j2
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api/database/{id}/table/{tableid}/file")
@RequestMapping("/api/database/{id}/cite/file")
public class FileEndpoint {
private final FileMapper fileMapper;
......@@ -28,44 +28,39 @@ public class FileEndpoint {
}
@GetMapping
public List<FileDto> listAll(@Valid @RequestParam("id") Long databaseId,
@Valid @RequestParam("tableId") Long tableId) {
public List<FileDto> listAll(@Valid @PathVariable("id") Long databaseId) {
return fileService.listResources()
.stream()
.map(fileMapper::fileToFileDto)
.collect(Collectors.toList());
}
@GetMapping("/{fileId}")
public FileDto find(@Valid @RequestParam("id") Long databaseId,
@Valid @RequestParam("tableId") Long tableId,
@Valid @RequestParam("queryId") Long queryId)
@GetMapping("/{queryId}")
public FileDto find(@Valid @PathVariable("id") Long databaseId,
@Valid @PathVariable("queryId") Long queryId)
throws ZenodoApiException, ZenodoNotFoundException, ZenodoAuthenticationException,
ZenodoUnavailableException, QueryNotFoundException {
return fileMapper.fileToFileDto(fileService.findResource(databaseId, tableId, queryId));
ZenodoUnavailableException, QueryNotFoundException, MetadataDatabaseNotFoundException {
return fileMapper.fileToFileDto(fileService.findResource(databaseId, queryId));
}
@PostMapping("/{fileId}")
public FileDto create(@Valid @RequestParam("id") Long databaseId,
@Valid @RequestParam("tableId") Long tableId,
@Valid @RequestParam("queryId") Long queryId)
@PostMapping("/{queryId}")
public FileDto create(@Valid @PathVariable("id") Long databaseId,
@Valid @PathVariable("queryId") Long queryId)
throws ZenodoApiException, ZenodoNotFoundException, ZenodoAuthenticationException,
ZenodoUnavailableException, QueryNotFoundException, RemoteDatabaseException, TableServiceException,
ZenodoFileException {
return fileMapper.fileToFileDto(fileService.createResource(databaseId, tableId, queryId));
ZenodoFileException, MetadataDatabaseNotFoundException {
return fileMapper.fileToFileDto(fileService.createResource(databaseId, queryId));
}
@PutMapping("/{fileId}")
public FileDto update(@Valid @RequestParam("id") Long databaseId,
@Valid @RequestParam("tableId") Long tableId,
@Valid @RequestParam("queryId") Long queryId) {
@PutMapping("/{queryId}")
public FileDto update(@Valid @PathVariable("id") Long databaseId,
@Valid @PathVariable("queryId") Long queryId) {
return null;
}
@DeleteMapping("/{fileId}")
public FileDto delete(@Valid @RequestParam("id") Long databaseId,
@Valid @RequestParam("tableId") Long tableId,
@Valid @RequestParam("queryId") Long queryId) {
@DeleteMapping("/{queryId}")
public FileDto delete(@Valid @PathVariable("id") Long databaseId,
@Valid @PathVariable("queryId") Long queryId) {
return null;
}
}
......@@ -7,6 +7,7 @@ import at.tuwien.mapper.QueryMapper;
import at.tuwien.service.MetadataService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
......@@ -16,7 +17,7 @@ import java.util.stream.Collectors;
@Log4j2
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api/database/{id}/table/{tableid}/metadata")
@RequestMapping("/api/database/{id}/cite/metadata")
public class MetadataEndpoint {
private final QueryMapper queryMapper;
......@@ -29,55 +30,55 @@ public class MetadataEndpoint {
}
@GetMapping
public List<QueryDto> findAll(@Valid @RequestParam("id") Long databaseId,
@Valid @RequestParam("tableId") Long tableId) {
return metadataService.listCitations(databaseId, tableId)
@Transactional
public List<QueryDto> findAll(@Valid @PathVariable("id") Long databaseId) throws MetadataDatabaseNotFoundException {
return metadataService.listCitations(databaseId)
.stream()
.map(queryMapper::queryToQueryDto)
.collect(Collectors.toList());
}
@PostMapping
public QueryDto create(@Valid @RequestParam("id") Long databaseId,
@Valid @RequestParam("tableId") Long tableId) throws ZenodoApiException,
@Transactional
public QueryDto create(@Valid @PathVariable("id") Long databaseId) throws ZenodoApiException,
ZenodoAuthenticationException, MetadataDatabaseNotFoundException, ZenodoUnavailableException {
return queryMapper.queryToQueryDto(metadataService.storeCitation(databaseId, tableId));
return queryMapper.queryToQueryDto(metadataService.storeCitation(databaseId));
}
@GetMapping("/{queryId}")
public QueryDto find(@Valid @RequestParam("id") Long databaseId,
@Valid @RequestParam("tableId") Long tableId,
@Transactional
public QueryDto find(@Valid @PathVariable("id") Long databaseId,
@Valid @RequestParam("queryId") Long queryId)
throws MetadataDatabaseNotFoundException, ZenodoApiException, ZenodoNotFoundException,
ZenodoAuthenticationException, ZenodoUnavailableException, QueryNotFoundException {
return queryMapper.queryToQueryDto(metadataService.findCitation(databaseId, tableId, queryId));
return queryMapper.queryToQueryDto(metadataService.findCitation(databaseId, queryId));
}
@PutMapping("/{queryId}")
public QueryDto update(@Valid @RequestParam("id") Long databaseId,
@Valid @RequestParam("tableId") Long tableId,
@Valid @RequestParam("queryId") Long queryId,
@Transactional
public QueryDto update(@Valid @PathVariable("id") Long databaseId,
@Valid @PathVariable("queryId") Long queryId,
@Valid @RequestBody DepositChangeRequestDto data)
throws ZenodoApiException, ZenodoNotFoundException, ZenodoAuthenticationException,
ZenodoUnavailableException, QueryNotFoundException {
return queryMapper.queryToQueryDto(metadataService.updateCitation(databaseId, tableId, queryId, data));
ZenodoUnavailableException, QueryNotFoundException, MetadataDatabaseNotFoundException {
return queryMapper.queryToQueryDto(metadataService.updateCitation(databaseId, queryId, data));
}
@DeleteMapping("/{queryId}")
public void delete(@Valid @RequestParam("id") Long databaseId,
@Valid @RequestParam("tableId") Long tableId,
@Valid @RequestParam("queryId") Long queryId) throws MetadataDatabaseNotFoundException,
@Transactional
public void delete(@Valid @PathVariable("id") Long databaseId,
@Valid @PathVariable("queryId") Long queryId) throws MetadataDatabaseNotFoundException,
ZenodoApiException, ZenodoAuthenticationException, ZenodoNotFoundException, ZenodoUnavailableException,
QueryNotFoundException {
metadataService.deleteCitation(databaseId, tableId, queryId);
metadataService.deleteCitation(databaseId, queryId);
}
@PostMapping("/{queryId}/publish")
public QueryDto publish(@Valid @RequestParam("id") Long databaseId,
@Valid @RequestParam("tableId") Long tableId,
@Valid @RequestParam("queryId") Long queryId) throws ZenodoApiException,
@Transactional
public QueryDto publish(@Valid @PathVariable("id") Long databaseId,
@Valid @PathVariable("queryId") Long queryId) throws ZenodoApiException,
ZenodoAuthenticationException, MetadataDatabaseNotFoundException, ZenodoUnavailableException,
ZenodoNotFoundException, QueryNotFoundException {
return queryMapper.queryToQueryDto(metadataService.publishCitation(databaseId, tableId, queryId));
return queryMapper.queryToQueryDto(metadataService.publishCitation(databaseId, queryId));
}
}
......@@ -72,10 +72,10 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
ZenodoFileException {
/* integrate */
final Query deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID);
final Query deposit = metadataService.storeCitation(DATABASE_1_ID);
/* test */
final File response = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
final File response = fileService.createResource(DATABASE_1_ID, QUERY_1_ID);
}
@Test
......@@ -87,13 +87,13 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
ResourceUtils.getFile("classpath:csv/weatherAUS.csv")));
/* request */
final Query deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID);
final Query deposit = metadataService.storeCitation(DATABASE_1_ID);
final FileUploadDto request = FileUploadDto.builder()
.name(FILE_2_NAME)
.build();
/* test */
final File response = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
final File response = fileService.createResource(DATABASE_1_ID, QUERY_1_ID);
assertEquals(FILE_1_ID, response.getId());
}
......@@ -112,11 +112,11 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
RemoteDatabaseException, TableServiceException, ZenodoFileException {
/* request */
final Query deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID);
final File fileResponse = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
final Query deposit = metadataService.storeCitation(DATABASE_1_ID);
final File fileResponse = fileService.createResource(DATABASE_1_ID, QUERY_1_ID);
/* test */
final File findResponse = fileService.findResource(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
final File findResponse = fileService.findResource(DATABASE_1_ID, QUERY_1_ID);
assertEquals(fileResponse.getId(), findResponse.getId());
}
......@@ -127,11 +127,11 @@ public class FileServiceIntegrationTest extends BaseUnitTest {
RemoteDatabaseException, TableServiceException, ZenodoFileException {
/* request */
final Query deposit = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID);
final File fileResponse = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
final Query deposit = metadataService.storeCitation(DATABASE_1_ID);
final File fileResponse = fileService.createResource(DATABASE_1_ID, QUERY_1_ID);
/* test */
fileService.deleteResource(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
fileService.deleteResource(DATABASE_1_ID, QUERY_1_ID);
}
}
\ No newline at end of file
......@@ -69,7 +69,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
@Test
public void createResource_succeeds() throws ZenodoApiException, ZenodoNotFoundException,
ZenodoAuthenticationException, ZenodoUnavailableException, QueryNotFoundException,
RemoteDatabaseException, TableServiceException, ZenodoFileException {
RemoteDatabaseException, TableServiceException, ZenodoFileException, MetadataDatabaseNotFoundException {
/* mock */
when(zenodoTemplate.postForEntity(anyString(), Mockito.<MultiValueMap<String, HttpEntity<?>>>any(),
......@@ -83,7 +83,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
.thenReturn(ResponseEntity.ok(QUERY_1_RESULT));
/* test */
final File response = fileService.createResource(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
final File response = fileService.createResource(DATABASE_1_ID, QUERY_1_ID);
assertEquals(FILE_1_ID, response.getId());
}
......@@ -103,7 +103,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
/* test */
assertThrows(ZenodoUnavailableException.class, () -> {
fileService.createResource(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
fileService.createResource(DATABASE_1_ID, QUERY_1_ID);
});
}
......@@ -122,7 +122,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
/* test */
assertThrows(ZenodoNotFoundException.class, () -> {
fileService.createResource(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
fileService.createResource(DATABASE_1_ID, QUERY_1_ID);
});
}
......@@ -141,7 +141,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
/* test */
assertThrows(ZenodoApiException.class, () -> {
fileService.createResource(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
fileService.createResource(DATABASE_1_ID, QUERY_1_ID);
});
}
......@@ -162,7 +162,8 @@ public class FileServiceUnitTest extends BaseUnitTest {
@Test
public void findResource_succeeds() throws ZenodoApiException, ZenodoNotFoundException,
ZenodoAuthenticationException, ZenodoUnavailableException, QueryNotFoundException {
ZenodoAuthenticationException, ZenodoUnavailableException, QueryNotFoundException,
MetadataDatabaseNotFoundException {
/* mock */
when(zenodoTemplate.exchange(anyString(), eq(HttpMethod.GET), Mockito.any(), eq(FileDto.class),
......@@ -172,7 +173,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
.thenReturn(Optional.of(QUERY_1));
/* test */
final File file = fileService.findResource(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
final File file = fileService.findResource(DATABASE_1_ID, QUERY_1_ID);
assertEquals(FILE_1_ID, file.getId());
}
......@@ -189,7 +190,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
/* test */
assertThrows(ZenodoUnavailableException.class, () -> {
fileService.findResource(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
fileService.findResource(DATABASE_1_ID, QUERY_1_ID);
});
}
......@@ -205,7 +206,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
/* test */
assertThrows(ZenodoApiException.class, () -> {
fileService.findResource(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
fileService.findResource(DATABASE_1_ID, QUERY_1_ID);
});
}
......@@ -222,7 +223,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
/* test */
assertThrows(ZenodoNotFoundException.class, () -> {
fileService.findResource(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
fileService.findResource(DATABASE_1_ID, QUERY_1_ID);
});
}
......@@ -235,13 +236,14 @@ public class FileServiceUnitTest extends BaseUnitTest {
/* test */
assertThrows(QueryNotFoundException.class, () -> {
fileService.findResource(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
fileService.findResource(DATABASE_1_ID, QUERY_1_ID);
});
}
@Test
public void deleteResource_succeeds() throws ZenodoApiException, ZenodoNotFoundException,
ZenodoAuthenticationException, ZenodoUnavailableException, QueryNotFoundException {
ZenodoAuthenticationException, ZenodoUnavailableException, QueryNotFoundException,
MetadataDatabaseNotFoundException {
/* mock */
when(zenodoTemplate.exchange(anyString(), eq(HttpMethod.DELETE), Mockito.any(), eq(String.class),
......@@ -251,7 +253,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
.thenReturn(Optional.of(QUERY_1));
/* test */
fileService.deleteResource(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
fileService.deleteResource(DATABASE_1_ID, QUERY_1_ID);
}
@Test
......@@ -267,7 +269,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
/* test */
assertThrows(ZenodoUnavailableException.class, () -> {
fileService.deleteResource(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
fileService.deleteResource(DATABASE_1_ID, QUERY_1_ID);
});
}
......@@ -283,7 +285,7 @@ public class FileServiceUnitTest extends BaseUnitTest {
/* test */
assertThrows(ZenodoApiException.class, () -> {
fileService.deleteResource(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
fileService.deleteResource(DATABASE_1_ID, QUERY_1_ID);
});
}
......
......@@ -64,10 +64,10 @@ public class MetadataServiceIntegrationTest extends BaseUnitTest {
@Test
@Transactional
public void listDeposit_succeeds() {
public void listDeposit_succeeds() throws MetadataDatabaseNotFoundException {
/* test */
final List<Query> response = metadataService.listCitations(DATABASE_1_ID, TABLE_1_ID);
final List<Query> response = metadataService.listCitations(DATABASE_1_ID);
assertEquals(1, response.size());
}
......@@ -76,7 +76,7 @@ public class MetadataServiceIntegrationTest extends BaseUnitTest {
MetadataDatabaseNotFoundException, ZenodoUnavailableException {
/* test */
final Query response = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID);
final Query response = metadataService.storeCitation(DATABASE_1_ID);
assertNotNull(response.getId());
assertNotNull(response.getDoi());
}
......@@ -85,10 +85,10 @@ public class MetadataServiceIntegrationTest extends BaseUnitTest {
public void updateDeposit_succeeds() throws ZenodoApiException, ZenodoAuthenticationException,
ZenodoNotFoundException, MetadataDatabaseNotFoundException, ZenodoUnavailableException,
QueryNotFoundException {
final Query query = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID);
final Query query = metadataService.storeCitation(DATABASE_1_ID);
/* test */
final Query response = metadataService.updateCitation(DATABASE_1_ID, TABLE_1_ID, query.getId(), DEPOST_1_REQUEST);
final Query response = metadataService.updateCitation(DATABASE_1_ID, query.getId(), DEPOST_1_REQUEST);
assertNotNull(response.getId());
}
......@@ -97,8 +97,8 @@ public class MetadataServiceIntegrationTest extends BaseUnitTest {
public void publishDeposit_succeeds() throws ZenodoApiException, ZenodoAuthenticationException,
ZenodoNotFoundException, MetadataDatabaseNotFoundException, ZenodoUnavailableException,
QueryNotFoundException, RemoteDatabaseException, TableServiceException, ZenodoFileException {
final Query query = metadataService.storeCitation(DATABASE_1_ID, TABLE_1_ID);
fileService.createResource(DATABASE_1_ID, TABLE_1_ID, query.getId());
final Query query = metadataService.storeCitation(DATABASE_1_ID);
fileService.createResource(DATABASE_1_ID, query.getId());
/* integrate */
final DepositChangeRequestDto request = DepositChangeRequestDto.builder()
......@@ -109,10 +109,10 @@ public class MetadataServiceIntegrationTest extends BaseUnitTest {
.creators(METADATA_1_CREATORS)
.build())
.build();
metadataService.updateCitation(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID, request);
metadataService.updateCitation(DATABASE_1_ID, QUERY_1_ID, request);
/* test */
final Query response = metadataService.publishCitation(DATABASE_1_ID, TABLE_1_ID, query.getId());
final Query response = metadataService.publishCitation(DATABASE_1_ID, query.getId());
assertNotNull(response.getId());
assertEquals(METADATA_1_TITLE, response.getTitle());
assertNotNull(response.getDoi());
......
......@@ -2,7 +2,6 @@ package at.tuwien.service;
import at.tuwien.BaseUnitTest;
import at.tuwien.api.database.deposit.DepositChangeRequestDto;
import at.tuwien.api.database.deposit.DepositDto;
import at.tuwien.api.database.deposit.DepositTzDto;
import at.tuwien.config.ReadyConfig;
import at.tuwien.entities.database.Database;
......@@ -54,16 +53,16 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
private TableRepository tableRepository;
@Test
public void listCitations_succeeds() {
public void listCitations_succeeds() throws MetadataDatabaseNotFoundException {
/* mocks */
when(zenodoService.listCitations(DATABASE_1_ID, TABLE_1_ID))
when(zenodoService.listCitations(DATABASE_1_ID))
.thenReturn(List.of(QUERY_1));
when(queryRepository.findAll())
.thenReturn(List.of(QUERY_1));
/* test */
final List<Query> response = zenodoService.listCitations(DATABASE_1_ID, TABLE_1_ID);
final List<Query> response = zenodoService.listCitations(DATABASE_1_ID);
assertEquals(1, response.size());
assertEquals(QUERY_1, response.get(0));
}
......@@ -85,7 +84,7 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
.body(DEPOSIT_1));
/* test */
final Query response = zenodoService.storeCitation(DATABASE_1_ID, TABLE_1_ID);
final Query response = zenodoService.storeCitation(DATABASE_1_ID);
assertEquals(QUERY_1_ID, response.getId());
}
......@@ -104,7 +103,7 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
/* test */
assertThrows(ZenodoUnavailableException.class, () -> {
zenodoService.storeCitation(DATABASE_1_ID, TABLE_1_ID);
zenodoService.storeCitation(DATABASE_1_ID);
});
}
......@@ -119,13 +118,14 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
/* test */
assertThrows(MetadataDatabaseNotFoundException.class, () -> {
zenodoService.storeCitation(DATABASE_1_ID, TABLE_1_ID);
zenodoService.storeCitation(DATABASE_1_ID);
});
}
@Test
public void deleteCitation_succeeds() throws ZenodoApiException, ZenodoAuthenticationException,
ZenodoNotFoundException, ZenodoUnavailableException, QueryNotFoundException {
ZenodoNotFoundException, ZenodoUnavailableException, QueryNotFoundException,
MetadataDatabaseNotFoundException {
/* mocks */
when(queryRepository.findById(QUERY_1_ID))
......@@ -136,7 +136,7 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
.build());
/* test */
zenodoService.deleteCitation(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
zenodoService.deleteCitation(DATABASE_1_ID, QUERY_1_ID);
}
@Test
......@@ -152,7 +152,7 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
/* test */
assertThrows(ZenodoUnavailableException.class, () -> {
zenodoService.deleteCitation(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
zenodoService.deleteCitation(DATABASE_1_ID, QUERY_1_ID);
});
}
......@@ -169,7 +169,7 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
/* test */
assertThrows(QueryNotFoundException.class, () -> {
zenodoService.deleteCitation(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
zenodoService.deleteCitation(DATABASE_1_ID, QUERY_1_ID);
});
}
......@@ -186,13 +186,13 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
/* test */
assertThrows(ZenodoApiException.class, () -> {
zenodoService.deleteCitation(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID);
zenodoService.deleteCitation(DATABASE_1_ID, QUERY_1_ID);
});
}
@Test
public void updateCitation_succeeds() throws ZenodoApiException, ZenodoAuthenticationException,
ZenodoNotFoundException, ZenodoUnavailableException, QueryNotFoundException {
ZenodoNotFoundException, ZenodoUnavailableException, QueryNotFoundException, MetadataDatabaseNotFoundException {
/* mocks */
when(queryRepository.findById(QUERY_1_ID))
......@@ -207,7 +207,7 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
.build();
/* test */
final Query response = zenodoService.updateCitation(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID, request);
final Query response = zenodoService.updateCitation(DATABASE_1_ID, QUERY_1_ID, request);
assertEquals(QUERY_1_ID, response.getId());
}
......@@ -229,7 +229,7 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
/* test */
assertThrows(ZenodoUnavailableException.class, () -> {
zenodoService.updateCitation(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID, request);
zenodoService.updateCitation(DATABASE_1_ID, QUERY_1_ID, request);
});
}
......@@ -251,7 +251,7 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
/* test */
assertThrows(ZenodoNotFoundException.class, () -> {
zenodoService.updateCitation(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID, request);
zenodoService.updateCitation(DATABASE_1_ID, QUERY_1_ID, request);
});
}
......@@ -274,7 +274,7 @@ public class MetadataServiceUnitTest extends BaseUnitTest {
/* test */
assertThrows(QueryNotFoundException.class, () -> {
zenodoService.updateCitation(DATABASE_1_ID, TABLE_1_ID, QUERY_1_ID, request);
zenodoService.updateCitation(DATABASE_1_ID, QUERY_1_ID, request);
});
}
......
package at.tuwien.repository.jpa;
import at.tuwien.entities.database.Database;
import at.tuwien.entities.database.query.Query;
import at.tuwien.entities.database.table.Table;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public interface QueryRepository extends JpaRepository<Query, Long> {
List<Query> findByDatabase(Database database);
Optional<Query> findByDatabaseAndId(Database database, Long id);
}
......@@ -14,7 +14,6 @@ public interface FileService {
* Upload a new file to a remote server for a given database-table id pair and metadata
*
* @param databaseId The database-table id paid
* @param tableId The database-table id pair
* @param queryId The query id
* @return The new file
* @throws ZenodoAuthenticationException Token invalid
......@@ -24,9 +23,9 @@ public interface FileService {
* @throws QueryNotFoundException The deposit was not found on the metadata database
*/
@Transactional
File createResource(Long databaseId, Long tableId, Long queryId)
File createResource(Long databaseId, Long queryId)
throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException,
ZenodoUnavailableException, QueryNotFoundException, RemoteDatabaseException, TableServiceException, ZenodoFileException;
ZenodoUnavailableException, QueryNotFoundException, RemoteDatabaseException, TableServiceException, ZenodoFileException, MetadataDatabaseNotFoundException;
/**
* List all files known to a deposit number (through the database-table id pair)
......@@ -40,7 +39,6 @@ public interface FileService {
* Find a file for a deposit (through the database-table id pair) by id
*
* @param databaseId The database-table id pair
* @param tableId The database-table id pair
* @param queryId The query id
* @return The file
* @throws QueryNotFoundException The deposit was not found on the metadata database
......@@ -50,15 +48,14 @@ public interface FileService {
* @throws ZenodoApiException Something other went wrong
*/
@Transactional
File findResource(Long databaseId, Long tableId, Long queryId)
File findResource(Long databaseId, Long queryId)
throws ZenodoAuthenticationException, ZenodoNotFoundException,
ZenodoApiException, ZenodoUnavailableException, QueryNotFoundException;
ZenodoApiException, ZenodoUnavailableException, QueryNotFoundException, MetadataDatabaseNotFoundException;
/**
* Delete a file based on the database-table id pair by id
*
* @param databaseId The database-table id pair
* @param tableId The database-table id pair
* @param queryId The query id
* @throws QueryNotFoundException The deposit was not found on the metadata database
* @throws ZenodoAuthenticationException Token invalid
......@@ -67,6 +64,6 @@ public interface FileService {
* @throws ZenodoApiException Something other went wrong
*/
@Transactional
void deleteResource(Long databaseId, Long tableId, Long queryId) throws ZenodoAuthenticationException,
ZenodoNotFoundException, ZenodoApiException, ZenodoUnavailableException, QueryNotFoundException;
void deleteResource(Long databaseId, Long queryId) throws ZenodoAuthenticationException,
ZenodoNotFoundException, ZenodoApiException, ZenodoUnavailableException, QueryNotFoundException, MetadataDatabaseNotFoundException;
}
package at.tuwien.service;
import at.tuwien.api.database.deposit.DepositChangeRequestDto;
import at.tuwien.api.database.query.QueryDto;
import at.tuwien.entities.database.query.Query;
import at.tuwien.exception.*;
import org.springframework.stereotype.Service;
......@@ -15,29 +14,26 @@ public interface MetadataService {
* List all deposits (e.g. datasets) available
*
* @param databaseId The database-table id pair
* @param tableId The database-table id pair
* @return The deposists
*/
List<Query> listCitations(Long databaseId, Long tableId);
List<Query> listCitations(Long databaseId) throws MetadataDatabaseNotFoundException;
/**
* Create a new deposit
*
* @param databaseId The database-table id pair
* @param tableId The database-table id pair
* @return The created deposit
* @throws ZenodoAuthenticationException Token invalid
* @throws ZenodoApiException Something other went wrong
* @throws ZenodoUnavailableException The remote server is not available
*/
Query storeCitation(Long databaseId, Long tableId) throws ZenodoAuthenticationException,
Query storeCitation(Long databaseId) throws ZenodoAuthenticationException,
ZenodoApiException, MetadataDatabaseNotFoundException, ZenodoUnavailableException;
/**
* Update a deposit with new metadata for a given id
*
* @param databaseId The database-table id pair
* @param tableId The database-table id pair
* @param queryId The query id
* @param data The new metadata
* @return The updated deposit
......@@ -46,15 +42,14 @@ public interface MetadataService {
* @throws ZenodoNotFoundException The deposit id was not found on the remote server
* @throws ZenodoUnavailableException The remote server is not available
*/
Query updateCitation(Long databaseId, Long tableId, Long queryId,
Query updateCitation(Long databaseId, Long queryId,
DepositChangeRequestDto data) throws ZenodoAuthenticationException, ZenodoApiException,
ZenodoNotFoundException, ZenodoUnavailableException, QueryNotFoundException;
ZenodoNotFoundException, ZenodoUnavailableException, QueryNotFoundException, MetadataDatabaseNotFoundException;
/**
* Find a deposit by database-table id pair
*
* @param databaseId The database-table id pair
* @param tableId The database-table id pair
* @param queryId The query id
* @return The deposit
* @throws ZenodoAuthenticationException Token invalid
......@@ -63,7 +58,7 @@ public interface MetadataService {
* @throws MetadataDatabaseNotFoundException The deposit id was not found in the metadata database
* @throws ZenodoUnavailableException The remote server is not available
*/
Query findCitation(Long databaseId, Long tableId, Long queryId)
Query findCitation(Long databaseId, Long queryId)
throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException,
MetadataDatabaseNotFoundException, ZenodoUnavailableException, QueryNotFoundException;
......@@ -71,7 +66,6 @@ public interface MetadataService {
* Delete a deposit from a given id
*
* @param databaseId The database-table id pair
* @param tableId The database-table id pair
* @param queryId The query id
* @throws ZenodoAuthenticationException Token invalid
* @throws ZenodoApiException Something other went wrong
......@@ -79,7 +73,7 @@ public interface MetadataService {
* @throws ZenodoUnavailableException The remote server is not available
* @throws ZenodoNotFoundException The deposit was not found on the remote server
*/
void deleteCitation(Long databaseId, Long tableId, Long queryId) throws ZenodoAuthenticationException,
void deleteCitation(Long databaseId, Long queryId) throws ZenodoAuthenticationException,
ZenodoApiException, MetadataDatabaseNotFoundException, ZenodoUnavailableException, ZenodoNotFoundException,
QueryNotFoundException;
......@@ -87,7 +81,6 @@ public interface MetadataService {
* Publishes a deposit with database-table id pair
*
* @param databaseId The database-table id pair
* @param tableId The database-table id pair
* @param queryId The query id
* @return The deposit
* @throws ZenodoAuthenticationException Token invalid
......@@ -96,7 +89,7 @@ public interface MetadataService {
* @throws ZenodoUnavailableException The remote server is not available
* @throws ZenodoNotFoundException The deposit was not found on the remote server
*/
Query publishCitation(Long databaseId, Long tableId, Long queryId) throws ZenodoAuthenticationException,
Query publishCitation(Long databaseId, Long queryId) throws ZenodoAuthenticationException,
ZenodoApiException, MetadataDatabaseNotFoundException, ZenodoUnavailableException, ZenodoNotFoundException,
QueryNotFoundException;
}
......@@ -3,14 +3,15 @@ package at.tuwien.service;
import at.tuwien.api.database.deposit.files.FileDto;
import at.tuwien.api.database.query.QueryResultDto;
import at.tuwien.config.ZenodoConfig;
import at.tuwien.entities.database.Database;
import at.tuwien.entities.database.query.File;
import at.tuwien.entities.database.query.Query;
import at.tuwien.exception.*;
import at.tuwien.mapper.FileMapper;
import at.tuwien.mapper.ZenodoMapper;
import at.tuwien.repository.jpa.DatabaseRepository;
import at.tuwien.repository.jpa.FileRepository;
import at.tuwien.repository.jpa.QueryRepository;
import at.tuwien.repository.jpa.TableRepository;
import com.opencsv.CSVWriter;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -45,11 +46,12 @@ public class ZenodoFileService implements FileService {
private final RestTemplate zenodoTemplate;
private final FileRepository fileRepository;
private final QueryRepository queryRepository;
private final DatabaseRepository databaseRepository;
@Autowired
public ZenodoFileService(FileMapper fileMapper, RestTemplate zenodoTemplate, RestTemplate queryTemplate,
ZenodoConfig zenodoConfig, ZenodoMapper zenodoMapper, FileRepository fileRepository,
QueryRepository queryRepository) {
QueryRepository queryRepository, DatabaseRepository databaseRepository) {
this.fileMapper = fileMapper;
this.zenodoTemplate = zenodoTemplate;
this.queryTemplate = queryTemplate;
......@@ -57,18 +59,20 @@ public class ZenodoFileService implements FileService {
this.zenodoMapper = zenodoMapper;
this.fileRepository = fileRepository;
this.queryRepository = queryRepository;
this.databaseRepository = databaseRepository;
}
@Override
public File createResource(Long databaseId, Long tableId, Long queryId)
public File createResource(Long databaseId, Long queryId)
throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException,
ZenodoUnavailableException, QueryNotFoundException, RemoteDatabaseException, TableServiceException,
ZenodoFileException {
ZenodoFileException, MetadataDatabaseNotFoundException {
final Database database = find(databaseId);
final Query query = getQuery(queryId);
final ResponseEntity<FileDto> response;
try {
response = zenodoTemplate.postForEntity("/api/deposit/depositions/{deposit_id}/files?access_token={token}",
zenodoMapper.resourceToHttpEntity(query.getTitle(), getDataset(databaseId, tableId, queryId)),
zenodoMapper.resourceToHttpEntity(query.getTitle(), getDataset(databaseId, queryId)),
FileDto.class, query.getDepositId(), zenodoConfig.getApiKey());
} catch (IOException e) {
throw new ZenodoApiException("Could not map file to byte array");
......@@ -95,9 +99,10 @@ public class ZenodoFileService implements FileService {
}
@Override
public File findResource(Long databaseId, Long tableId, Long queryId)
public File findResource(Long databaseId, Long queryId)
throws ZenodoAuthenticationException, ZenodoNotFoundException,
ZenodoApiException, ZenodoUnavailableException, QueryNotFoundException {
ZenodoApiException, ZenodoUnavailableException, QueryNotFoundException, MetadataDatabaseNotFoundException {
final Database database = find(databaseId);
final Query query = getQuery(queryId);
if (query.getFiles().size() != 1) {
log.error("Currently we only support one file");
......@@ -122,8 +127,9 @@ public class ZenodoFileService implements FileService {
}
@Override
public void deleteResource(Long databaseId, Long tableId, Long queryId) throws ZenodoAuthenticationException,
ZenodoNotFoundException, ZenodoApiException, ZenodoUnavailableException, QueryNotFoundException {
public void deleteResource(Long databaseId, Long queryId) throws ZenodoAuthenticationException,
ZenodoNotFoundException, ZenodoApiException, ZenodoUnavailableException, QueryNotFoundException, MetadataDatabaseNotFoundException {
final Database database = find(databaseId);
final Query query = getQuery(queryId);
if (query.getFiles().size() != 1) {
log.error("Currently we only support one file");
......@@ -180,11 +186,10 @@ public class ZenodoFileService implements FileService {
* Retrieve a result set from the Table Service and create a multipart file from it
*
* @param databaseId The database-table id pair
* @param tableId The database-table id pair
* @param queryId The query id
* @return The create multipart file
*/
private MultipartFile getDataset(Long databaseId, Long tableId, Long queryId) throws QueryNotFoundException,
private MultipartFile getDataset(Long databaseId, Long queryId) throws QueryNotFoundException,
RemoteDatabaseException, TableServiceException, ZenodoFileException {
final ResponseEntity<QueryResultDto> response;
try {
......@@ -204,6 +209,21 @@ public class ZenodoFileService implements FileService {
return file;
}
/**
* Find database by id in the metadata database
*
* @param id The id
* @return The database
* @throws MetadataDatabaseNotFoundException When not found
*/
private Database find(Long id) throws MetadataDatabaseNotFoundException {
final Optional<Database> database = databaseRepository.findById(id);
if (database.isEmpty()) {
throw new MetadataDatabaseNotFoundException("Database not found in the metadata database");
}
return database.get();
}
/**
* Create a temporary data set file and return a multipart file
*
......
......@@ -6,11 +6,11 @@ import at.tuwien.api.database.deposit.DepositTzDto;
import at.tuwien.config.ZenodoConfig;
import at.tuwien.entities.database.Database;
import at.tuwien.entities.database.query.Query;
import at.tuwien.entities.database.table.Table;
import at.tuwien.exception.*;
import at.tuwien.mapper.ZenodoMapper;
import at.tuwien.repository.jpa.DatabaseRepository;
import at.tuwien.repository.jpa.QueryRepository;
import at.tuwien.repository.jpa.TableRepository;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.*;
......@@ -23,41 +23,38 @@ import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;
@Log4j2
@Service
public class ZenodoMetadataService implements MetadataService {
private final ZenodoConfig zenodoConfig;
private final ZenodoMapper zenodoMapper;
private final RestTemplate zenodoTemplate;
private final TableRepository tableRepository;
private final DatabaseRepository databaseRepository;
private final QueryRepository queryRepository;
@Autowired
public ZenodoMetadataService(@Qualifier("zenodoTemplate") RestTemplate zenodoTemplate, ZenodoConfig zenodoConfig,
ZenodoMapper zenodoMapper, TableRepository tableRepository,
ZenodoMapper zenodoMapper, DatabaseRepository databaseRepository,
QueryRepository queryRepository) {
this.zenodoConfig = zenodoConfig;
this.zenodoMapper = zenodoMapper;
this.zenodoTemplate = zenodoTemplate;
this.tableRepository = tableRepository;
this.databaseRepository = databaseRepository;
this.queryRepository = queryRepository;
}
@Override
@Transactional
public List<Query> listCitations(Long databaseId, Long tableId) {
return queryRepository.findAll();
public List<Query> listCitations(Long databaseId) throws MetadataDatabaseNotFoundException {
return queryRepository.findByDatabase(find(databaseId));
}
@Override
@Transactional
public Query storeCitation(Long databaseId, Long tableId) throws ZenodoAuthenticationException,
public Query storeCitation(Long databaseId) throws ZenodoAuthenticationException,
ZenodoApiException, ZenodoUnavailableException, MetadataDatabaseNotFoundException {
final Optional<Table> table = tableRepository.findByDatabaseAndId(Database.builder().id(databaseId).build(),
tableId);
if (table.isEmpty()) {
throw new MetadataDatabaseNotFoundException("Table not found in the metadata database");
}
final Database database = find(databaseId);
final ResponseEntity<DepositTzDto> response;
try {
response = zenodoTemplate.exchange("/api/deposit/depositions?access_token={token}", HttpMethod.POST,
......@@ -74,16 +71,20 @@ public class ZenodoMetadataService implements MetadataService {
if (response.getBody() == null) {
throw new ZenodoApiException("Endpoint returned null body");
}
return queryRepository.save(zenodoMapper.depositTzDtoToQuery(response.getBody()));
final Query query = zenodoMapper.depositTzDtoToQuery(response.getBody());
log.info("Created query metadata id {} and doi {}", query.getId(), query.getDoi());
log.debug("Created query metadata {}", response.getBody());
return query;
}
@Override
@Transactional
public Query updateCitation(Long databaseId, Long tableId, Long queryId,
public Query updateCitation(Long databaseId, Long queryId,
DepositChangeRequestDto data)
throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException,
ZenodoUnavailableException, QueryNotFoundException {
final Optional<Query> query = queryRepository.findById(queryId);
ZenodoUnavailableException, QueryNotFoundException, MetadataDatabaseNotFoundException {
final Database database = find(databaseId);
final Optional<Query> query = queryRepository.findByDatabaseAndId(database, queryId);
if (query.isEmpty()) {
throw new QueryNotFoundException("Query not found in query store");
}
......@@ -109,15 +110,18 @@ public class ZenodoMetadataService implements MetadataService {
if (response.getBody() == null) {
throw new ZenodoApiException("Endpoint returned null body");
}
log.info("Updated query metadata id {}", queryId);
log.debug("Updated query metadata {}", response.getBody());
return query.get();
}
@Override
@Transactional
public Query findCitation(Long databaseId, Long tableId, Long queryId)
public Query findCitation(Long databaseId, Long queryId)
throws ZenodoAuthenticationException, ZenodoApiException, ZenodoNotFoundException,
QueryNotFoundException, ZenodoUnavailableException {
final Optional<Query> query = queryRepository.findById(queryId);
QueryNotFoundException, ZenodoUnavailableException, MetadataDatabaseNotFoundException {
final Database database = find(databaseId);
final Optional<Query> query = queryRepository.findByDatabaseAndId(database, queryId);
if (query.isEmpty()) {
throw new QueryNotFoundException("Query not found in query store");
}
......@@ -140,14 +144,16 @@ public class ZenodoMetadataService implements MetadataService {
if (response.getBody() == null) {
throw new ZenodoApiException("Endpoint returned null body");
}
log.debug("Found query metadata {}", response.getBody());
return query.get();
}
@Override
@Transactional
public void deleteCitation(Long databaseId, Long tableId, Long queryId) throws ZenodoAuthenticationException, ZenodoApiException,
QueryNotFoundException, ZenodoUnavailableException, ZenodoNotFoundException {
final Optional<Query> query = queryRepository.findById(queryId);
public void deleteCitation(Long databaseId, Long queryId) throws ZenodoAuthenticationException, ZenodoApiException,
QueryNotFoundException, ZenodoUnavailableException, ZenodoNotFoundException, MetadataDatabaseNotFoundException {
final Database database = find(databaseId);
final Optional<Query> query = queryRepository.findByDatabaseAndId(database, queryId);
if (query.isEmpty()) {
throw new QueryNotFoundException("Query not found in query store");
}
......@@ -167,14 +173,17 @@ public class ZenodoMetadataService implements MetadataService {
if (!response.getStatusCode().equals(HttpStatus.CREATED)) {
throw new ZenodoApiException("Could not delete the deposit");
}
log.info("Deleted query metadata id {}", queryId);
log.debug("Deleted query metadata {}", response.getBody());
}
@Override
@Transactional
public Query publishCitation(Long databaseId, Long tableId, Long queryId)
public Query publishCitation(Long databaseId, Long queryId)
throws ZenodoAuthenticationException, ZenodoApiException, QueryNotFoundException,
ZenodoUnavailableException, ZenodoNotFoundException {
final Optional<Query> query = queryRepository.findById(queryId);
ZenodoUnavailableException, ZenodoNotFoundException, MetadataDatabaseNotFoundException {
final Database database = find(databaseId);
final Optional<Query> query = queryRepository.findByDatabaseAndId(database, queryId);
if (query.isEmpty()) {
throw new QueryNotFoundException("Query not found in query store");
}
......@@ -194,9 +203,26 @@ public class ZenodoMetadataService implements MetadataService {
if (!response.getStatusCode().equals(HttpStatus.ACCEPTED)) {
throw new ZenodoApiException("Could not publish the deposit");
}
log.info("Published query metadata id {} and doi {}", queryId, query.get().getDoi());
log.debug("Published query metadata {}", response.getBody());
return query.get();
}
/**
* Find database by id in the metadata database
*
* @param id The id
* @return The database
* @throws MetadataDatabaseNotFoundException When not found
*/
private Database find(Long id) throws MetadataDatabaseNotFoundException {
final Optional<Database> database = databaseRepository.findById(id);
if (database.isEmpty()) {
throw new MetadataDatabaseNotFoundException("Database not found in the metadata database");
}
return database.get();
}
/**
* Wrapper to add headers to all non-file upload requests
*
......
......@@ -28,8 +28,7 @@ public class GatewayConfig {
.method("POST", "GET", "PUT", "DELETE")
.and()
.uri("lb://fda-query-service"))
.route("fda-citation-service", r -> r.path("/api/database/**/table/**/file/**",
"/api/database/**/table/**/metadata/**")
.route("fda-citation-service", r -> r.path("/api/database/**/cite/**")
.and()
.method("POST", "GET", "PUT", "DELETE")
.and()
......
......@@ -6,6 +6,7 @@ import lombok.*;
@Getter
@Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class DepositChangeRequestDto {
......
......@@ -14,6 +14,7 @@ import java.util.List;
@Getter
@Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class DepositDto {
......
......@@ -5,6 +5,7 @@ import lombok.*;
@Setter
@Getter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class DepositLinksDto {
......
......@@ -13,6 +13,7 @@ import java.util.List;
@Getter
@Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class DepositTzDto {
......
......@@ -8,6 +8,7 @@ import java.net.URI;
@Getter
@Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class LinksDto {
......
......@@ -7,6 +7,7 @@ import org.springframework.http.HttpEntity;
@Getter
@Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class FileBinaryRequestDto {
......
......@@ -8,6 +8,7 @@ import java.time.Instant;
@Getter
@Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class FileDto {
......
......@@ -5,6 +5,7 @@ import lombok.*;
@Getter
@Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class FileLinksDto {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment