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

Deposit files works for invenio

parent 71896227
No related branches found
No related tags found
2 merge requests!81New stable release,!80Multiple features connected with user management and ownership of databases
This commit is part of merge request !81. Comments created here will be created in the context of that merge request.
Showing
with 533 additions and 104 deletions
__pycache__ __pycache__
features.csv
\ No newline at end of file
...@@ -46,7 +46,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)): ...@@ -46,7 +46,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
def __init__(self): def __init__(self):
"""Constructor""" """Constructor"""
# Default Base url # Default Base url
self.host = "http://localhost:9097" self.host = "http://localhost:9095"
# Temp file folder for downloading files # Temp file folder for downloading files
self.temp_folder_path = None self.temp_folder_path = None
......
...@@ -46,7 +46,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)): ...@@ -46,7 +46,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
def __init__(self): def __init__(self):
"""Constructor""" """Constructor"""
# Default Base url # Default Base url
self.host = "http://localhost:9091" self.host = "http://localhost:9095"
# Temp file folder for downloading files # Temp file folder for downloading files
self.temp_folder_path = None self.temp_folder_path = None
......
...@@ -46,7 +46,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)): ...@@ -46,7 +46,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
def __init__(self): def __init__(self):
"""Constructor""" """Constructor"""
# Default Base url # Default Base url
self.host = "http://localhost:9092" self.host = "http://localhost:9095"
# Temp file folder for downloading files # Temp file folder for downloading files
self.temp_folder_path = None self.temp_folder_path = None
......
...@@ -46,7 +46,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)): ...@@ -46,7 +46,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
def __init__(self): def __init__(self):
"""Constructor""" """Constructor"""
# Default Base url # Default Base url
self.host = "http://localhost:9099" self.host = "http://localhost:9095"
# Temp file folder for downloading files # Temp file folder for downloading files
self.temp_folder_path = None self.temp_folder_path = None
......
...@@ -46,7 +46,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)): ...@@ -46,7 +46,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
def __init__(self): def __init__(self):
"""Constructor""" """Constructor"""
# Default Base url # Default Base url
self.host = "http://localhost:9096" self.host = "http://localhost:9095"
# Temp file folder for downloading files # Temp file folder for downloading files
self.temp_folder_path = None self.temp_folder_path = None
......
...@@ -259,7 +259,7 @@ class TableDataEndpointApi(object): ...@@ -259,7 +259,7 @@ class TableDataEndpointApi(object):
auth_settings = [] # noqa: E501 auth_settings = [] # noqa: E501
return self.api_client.call_api( return self.api_client.call_api(
'/api/container/{id}/database/{databaseId}/table/{tableId}/data', 'HEAD', '/api/container/{id}/database/{databaseId}/table/{tableId}/data', 'GET',
path_params, path_params,
query_params, query_params,
header_params, header_params,
...@@ -380,7 +380,7 @@ class TableDataEndpointApi(object): ...@@ -380,7 +380,7 @@ class TableDataEndpointApi(object):
auth_settings = [] # noqa: E501 auth_settings = [] # noqa: E501
return self.api_client.call_api( return self.api_client.call_api(
'/api/container/{id}/database/{databaseId}/table/{tableId}/data', 'GET', '/api/container/{id}/database/{databaseId}/table/{tableId}/data', 'HEAD',
path_params, path_params,
query_params, query_params,
header_params, header_params,
......
...@@ -46,7 +46,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)): ...@@ -46,7 +46,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
def __init__(self): def __init__(self):
"""Constructor""" """Constructor"""
# Default Base url # Default Base url
self.host = "http://localhost:9093" self.host = "http://localhost:9095"
# Temp file folder for downloading files # Temp file folder for downloading files
self.temp_folder_path = None self.temp_folder_path = None
......
...@@ -46,7 +46,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)): ...@@ -46,7 +46,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
def __init__(self): def __init__(self):
"""Constructor""" """Constructor"""
# Default Base url # Default Base url
self.host = "http://localhost:9094" self.host = "http://localhost:9095"
# Temp file folder for downloading files # Temp file folder for downloading files
self.temp_folder_path = None self.temp_folder_path = None
......
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Test
......
#!/usr/bin/env python3 #!/usr/bin/env python3
import json import re
import csv
import requests import requests
from api_authentication.api.authentication_endpoint_api import AuthenticationEndpointApi doi = '10.5281/zenodo.5649276'
from api_authentication.api.user_endpoint_api import UserEndpointApi headers = {
from api_container.api.container_endpoint_api import ContainerEndpointApi 'Authorize': 'Bearer djCvqkoOW69keHajybZiwE8bBjyir2QSZOLKpAtc4S1Wp17KXgcHmMoWJwft'
from api_database.api.container_database_endpoint_api import ContainerDatabaseEndpointApi }
authentication = AuthenticationEndpointApi() # Resolve DOI
user = UserEndpointApi() response = requests.get('https://doi.org/' + doi)
container = ContainerEndpointApi() id = re.findall('/([a-z0-9-]+)$', response.url)[0]
database = ContainerDatabaseEndpointApi() host = re.findall('^https?:\/\/([a-z0-9]+\.[a-z]+)', response.url)[0]
print("Resolved DOI to", host, "and record id", id)
# # Create account # Find files
# response = user.register({ url = 'https://' + host + '/api/records/' + id
# 'username': 'mweise', response = requests.get(url, headers=headers)
# 'password': 'fda', record = response.json()
# 'email': 'martin.weise@tuwien.ac.at'
# })
# print('Created account with username %s' % response.username)
#
# # Create authentication
# response = authentication.authenticate_user1({
# 'username': 'mweise',
# 'password': 'fda'
# })
# container.api_client.default_headers = {
# 'Authorization': 'Bearer ' + response.token
# }
# database.api_client.default_headers = {
# 'Authorization': 'Bearer ' + response.token
# }
#
# # Create container
# response = container.create1({
# 'name': 'MIR ' + str(uuid.uuid1()),
# 'repository': 'mariadb',
# 'tag': '10.5'
# })
# cid = response.id
# print('Created container with id %d' % cid)
#
# # Start container
# response = container.modify({
# 'action': 'START'
# }, cid)
# time.sleep(5)
# print('Started container with id %d' % cid)
#
# # Create database
# response = database.create({
# 'name': 'MIR ' + str(uuid.uuid1()),
# 'description': 'Music Information Retrieval',
# 'is_public': True
# }, cid)
# dbid = response.id
# print('Created database with id %d' % dbid)
# Analyse Table # Write some .csv
response = requests.post('http://localhost:5000/api/analyse/determinedt', json={ i = 0
'filepath': '/tmp/test.csv', with open('./features.csv', 'w') as f:
}) writer = csv.writer(f)
data = json.loads(response.content) writer.writerow(['key', 'size', 'link'])
print('Determined data types') for file in record['files']:
print(response) requests.get(file['links']['self'])
print("... feature extract from", file['links']['self'])
writer.writerow([file['key'], file['size'], file['links']['self']])
i += 1
if i > 10:
break
print("Generated a feature .csv")
%% Cell type:markdown id: tags:
# Feature Extraction & Deposit
In this notebook we define an example of creating a database from a .csv and perform feature extraction of audio files. The APIs are auto-generated from the Swagger Endpoint documentations using [`generate.sh`](https://gitlab.phaidra.org/fair-data-austria-db-repository/fda-docs/-/blob/master/swagger/generate.sh). Steps we perform:
1. Download a music file from a public repository
2. Perform feature extraction
3. Create an account at DBRepo
4. Create an authentication token
5. Create a mariadb container
6. Start the mariadb container
7. Create a database within the mariadb container
8. Import the feature .csv (manually)
%% Cell type:code id: tags:
``` python
import os.path
import uuid
import time
import re
import csv
import requests as rq
from api_authentication.api.authentication_endpoint_api import AuthenticationEndpointApi
from api_authentication.api.user_endpoint_api import UserEndpointApi
from api_container.api.container_endpoint_api import ContainerEndpointApi
from api_database.api.container_database_endpoint_api import ContainerDatabaseEndpointApi
from api_table.api.table_endpoint_api import TableEndpointApi
authentication = AuthenticationEndpointApi()
user = UserEndpointApi()
container = ContainerEndpointApi()
database = ContainerDatabaseEndpointApi()
table = TableEndpointApi()
doi = "10.5281/zenodo.5649276"
email = "some@example.com"
```
%% Cell type:markdown id: tags:
## 1. Download wav
Resolve the DOI to URI
%% Cell type:code id: tags:
``` python
response = rq.get("https://doi.org/" + doi)
id = re.findall("/([a-z0-9-]+)$", response.url)[0]
host = re.findall("^https?:\/\/([a-z0-9]+\.[a-z]+)", response.url)[0]
print("Resolved DOI to", host, "and record id", id)
```
%% Output
Resolved DOI to zenodo.org and record id 5649276
%% Cell type:markdown id: tags:
## 2. Perform feature extraction
%% Cell type:code id: tags:
``` python
response = rq.get("https://" + host + "/api/records/" + id)
record = response.json()
i = 0
with open(os.path.expanduser("~/features.csv"), "w") as f:
writer = csv.writer(f)
writer.writerow(["key", "size", "link"])
for file in record["files"]:
rq.get(file["links"]["self"])
print("... feature extract from", file["links"]["self"])
writer.writerow([file["key"], file["size"], file["links"]["self"]])
i += 1
if i > 10:
break
print("Generated a feature .csv in your home directory")
```
%% Output
... feature extract from https://zenodo.org/api/files/22d69a63-2aff-47ae-b818-be78a23e9889/colive.0044_20200518133554_1_m4a_1.wav
... feature extract from https://zenodo.org/api/files/22d69a63-2aff-47ae-b818-be78a23e9889/colive.0044_20200518133554_2_m4a_1.wav
... feature extract from https://zenodo.org/api/files/22d69a63-2aff-47ae-b818-be78a23e9889/colive.0066_20200611134530_1_m4a_0.wav
... feature extract from https://zenodo.org/api/files/22d69a63-2aff-47ae-b818-be78a23e9889/colive.0066_20200611134530_2_m4a_0.wav
... feature extract from https://zenodo.org/api/files/22d69a63-2aff-47ae-b818-be78a23e9889/colive.0066_20200612072315_1_m4a_0.wav
... feature extract from https://zenodo.org/api/files/22d69a63-2aff-47ae-b818-be78a23e9889/colive.0066_20200612072315_2_m4a_0.wav
... feature extract from https://zenodo.org/api/files/22d69a63-2aff-47ae-b818-be78a23e9889/colive.0066_20200613082517_1_m4a_0.wav
... feature extract from https://zenodo.org/api/files/22d69a63-2aff-47ae-b818-be78a23e9889/colive.0066_20200613082517_2_m4a_0.wav
... feature extract from https://zenodo.org/api/files/22d69a63-2aff-47ae-b818-be78a23e9889/colive.0066_20200614080017_1_m4a_0.wav
... feature extract from https://zenodo.org/api/files/22d69a63-2aff-47ae-b818-be78a23e9889/colive.0066_20200614080017_2_m4a_0.wav
... feature extract from https://zenodo.org/api/files/22d69a63-2aff-47ae-b818-be78a23e9889/colive.0066_20200615070238_1_m4a_0.wav
Generated a feature .csv in your home directory
%% Cell type:markdown id: tags:
## 3. Create an account at DBRepo
%% Cell type:code id: tags:
``` python
response = user.register({
"username": "user",
"password": "user",
"email": email
})
print(response)
```
%% Output
{'authorities': [{'authority': 'ROLE_RESEARCHER'}],
'containers': None,
'databases': None,
'email': 'martinweiseat@gmail.com',
'firstname': None,
'id': 2,
'identifiers': None,
'lastname': None,
'titles_after': None,
'titles_before': None,
'username': 'user'}
%% Cell type:markdown id: tags:
## 4. Create an authentication token
%% Cell type:code id: tags:
``` python
response = authentication.authenticate_user1({
"username": "user",
"password": "user"
})
container.api_client.default_headers = {"Authorization": "Bearer " + response.token}
database.api_client.default_headers = {"Authorization": "Bearer " + response.token}
table.api_client.default_headers = {"Authorization": "Bearer " + response.token}
```
%% Cell type:markdown id: tags:
## 5. Create a mariadb container
%% Cell type:code id: tags:
``` python
response = container.create1({
"name": "MIR " + str(uuid.uuid1()),
"repository": "mariadb",
"tag": "10.5"
})
container_id = response.id
print(response)
```
%% Output
{'hash': 'f5a649a71aae3748e62228721c44627ffc866f665d677bb890c37b9111590ffa',
'id': 2,
'internal_name': 'fda-userdb-mir-1010b964-f6fa-11ec-9f77-64bc58900b78',
'is_public': None,
'name': 'MIR 1010b964-f6fa-11ec-9f77-64bc58900b78'}
%% Cell type:markdown id: tags:
## 6. Start the mariadb container
%% Cell type:code id: tags:
``` python
response = container.modify({
"action": "START"
}, container_id)
time.sleep(5)
print(response)
```
%% Output
{'hash': 'f5a649a71aae3748e62228721c44627ffc866f665d677bb890c37b9111590ffa',
'id': 2,
'internal_name': 'fda-userdb-mir-1010b964-f6fa-11ec-9f77-64bc58900b78',
'is_public': None,
'name': 'MIR 1010b964-f6fa-11ec-9f77-64bc58900b78'}
%% Cell type:markdown id: tags:
## 7. Create a database within the mariadb container
%% Cell type:code id: tags:
``` python
response = database.create({
"name": "MIR " + str(uuid.uuid1()),
"description": "Music Information Retrieval",
"is_public": True
}, container_id)
database_id = response.id
```
%% Cell type:markdown id: tags:
## 8. Import the feature .csv
Now open [http://localhost:3000/](http://localhost:3000/) and import the .csv file by clicking the database. After successful creation of the table, come back here.
invenio-client==0.1.0 requests==2.28.0
six==1.16.0 \ No newline at end of file
\ No newline at end of file
package at.tuwien.endpoints; package at.tuwien.endpoints;
import at.tuwien.api.document.record.CreateDraftDto; import at.tuwien.api.document.record.CreateDraftDto;
import at.tuwien.api.document.record.DraftDto; import at.tuwien.api.document.record.RecordDto;
import at.tuwien.exception.DraftRecordCreateException; import at.tuwien.exception.DraftRecordCreateException;
import at.tuwien.service.DocumentService; import at.tuwien.service.DocumentService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
...@@ -20,9 +20,8 @@ import java.security.Principal; ...@@ -20,9 +20,8 @@ import java.security.Principal;
@Log4j2 @Log4j2
@RestController
@CrossOrigin(origins = "*") @CrossOrigin(origins = "*")
@ControllerAdvice @RestController
@RequestMapping("/api/document") @RequestMapping("/api/document")
public class DocumentEndpoint { public class DocumentEndpoint {
...@@ -37,9 +36,9 @@ public class DocumentEndpoint { ...@@ -37,9 +36,9 @@ public class DocumentEndpoint {
@PreAuthorize("hasRole('ROLE_RESEARCHER')") @PreAuthorize("hasRole('ROLE_RESEARCHER')")
@Transactional(readOnly = true) @Transactional(readOnly = true)
@Operation(summary = "Create a draft", security = @SecurityRequirement(name = "bearerAuth")) @Operation(summary = "Create a draft", security = @SecurityRequirement(name = "bearerAuth"))
public ResponseEntity<DraftDto> create(@NotNull @Valid @RequestBody CreateDraftDto data, public ResponseEntity<RecordDto> create(@NotNull @Valid @RequestBody CreateDraftDto data,
@NotNull Principal principal) throws DraftRecordCreateException { @NotNull Principal principal) throws DraftRecordCreateException {
final DraftDto document = documentService.create(data, principal); final RecordDto document = documentService.create(data, principal);
return ResponseEntity.status(HttpStatus.CREATED) return ResponseEntity.status(HttpStatus.CREATED)
.body(document); .body(document);
} }
...@@ -48,22 +47,33 @@ public class DocumentEndpoint { ...@@ -48,22 +47,33 @@ public class DocumentEndpoint {
@PreAuthorize("hasRole('ROLE_RESEARCHER')") @PreAuthorize("hasRole('ROLE_RESEARCHER')")
@Transactional(readOnly = true) @Transactional(readOnly = true)
@Operation(summary = "Find a draft", security = @SecurityRequirement(name = "bearerAuth")) @Operation(summary = "Find a draft", security = @SecurityRequirement(name = "bearerAuth"))
public ResponseEntity<DraftDto> find(@NotNull @PathVariable("id") String documentId, public ResponseEntity<RecordDto> find(@NotNull @PathVariable("id") String documentId,
@NotNull Principal principal) throws DraftRecordCreateException { @NotNull Principal principal) throws DraftRecordCreateException {
final DraftDto document = documentService.findById(documentId, principal); final RecordDto document = documentService.findById(documentId, principal);
log.info("Found draft record with id {}", documentId); log.info("Found draft record with id {}", documentId);
log.debug("found draft record {}", document); log.debug("found draft record {}", document);
return ResponseEntity.status(HttpStatus.OK) return ResponseEntity.status(HttpStatus.OK)
.body(document); .body(document);
} }
@PutMapping("/{id}/publish")
@PreAuthorize("hasRole('ROLE_RESEARCHER')")
@Transactional(readOnly = true)
@Operation(summary = "Publish a draft", security = @SecurityRequirement(name = "bearerAuth"))
public ResponseEntity<RecordDto> publish(@NotNull @PathVariable("id") String documentId,
@NotNull Principal principal) throws DraftRecordCreateException {
final RecordDto document = documentService.publish(documentId, principal);
return ResponseEntity.status(HttpStatus.ACCEPTED)
.body(document);
}
@PostMapping("/{id}") @PostMapping("/{id}")
@PreAuthorize("hasRole('ROLE_RESEARCHER')") @PreAuthorize("hasRole('ROLE_RESEARCHER')")
@Transactional(readOnly = true) @Transactional(readOnly = true)
@Operation(summary = "Reserve draft DOI", security = @SecurityRequirement(name = "bearerAuth")) @Operation(summary = "Reserve draft DOI", security = @SecurityRequirement(name = "bearerAuth"))
public ResponseEntity<DraftDto> reserve(@NotNull @PathVariable("id") String documentId, public ResponseEntity<RecordDto> reserve(@NotNull @PathVariable("id") String documentId,
@NotNull Principal principal) throws DraftRecordCreateException { @NotNull Principal principal) throws DraftRecordCreateException {
final DraftDto document = documentService.reserveDoi(documentId, principal); final RecordDto document = documentService.reserveDoi(documentId, principal);
return ResponseEntity.status(HttpStatus.CREATED) return ResponseEntity.status(HttpStatus.CREATED)
.body(document); .body(document);
} }
......
package at.tuwien.endpoints; package at.tuwien.endpoints;
import at.tuwien.api.document.file.FileStartDto; import at.tuwien.api.document.file.FileDto;
import at.tuwien.exception.FileUploadException;
import at.tuwien.exception.CommitFileUploadException;
import at.tuwien.exception.DraftRecordCreateException; import at.tuwien.exception.DraftRecordCreateException;
import at.tuwien.service.FileService; import at.tuwien.service.FileService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
...@@ -12,6 +14,7 @@ import org.springframework.http.ResponseEntity; ...@@ -12,6 +14,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.security.Principal; import java.security.Principal;
...@@ -20,7 +23,6 @@ import java.security.Principal; ...@@ -20,7 +23,6 @@ import java.security.Principal;
@Log4j2 @Log4j2
@RestController @RestController
@CrossOrigin(origins = "*") @CrossOrigin(origins = "*")
@ControllerAdvice
@RequestMapping("/api/document/{id}/file") @RequestMapping("/api/document/{id}/file")
public class FileEndpoint { public class FileEndpoint {
...@@ -34,11 +36,14 @@ public class FileEndpoint { ...@@ -34,11 +36,14 @@ public class FileEndpoint {
@PostMapping @PostMapping
@PreAuthorize("hasRole('ROLE_RESEARCHER')") @PreAuthorize("hasRole('ROLE_RESEARCHER')")
@Transactional(readOnly = true) @Transactional(readOnly = true)
@Operation(summary = "Start draft files", security = @SecurityRequirement(name = "bearerAuth")) @Operation(summary = "Upload file", security = @SecurityRequirement(name = "bearerAuth"))
public ResponseEntity<FileStartDto> start(@NotNull @PathVariable("id") String documentId, public ResponseEntity<FileDto> uploadFile(@NotNull @PathVariable("id") String documentId,
@NotNull Principal principal) throws DraftRecordCreateException { @NotNull @RequestParam("file") MultipartFile file,
final FileStartDto document = fileService.start(documentId, principal); @NotNull Principal principal)
return ResponseEntity.status(HttpStatus.CREATED) throws DraftRecordCreateException, CommitFileUploadException, FileUploadException,
org.apache.tomcat.util.http.fileupload.FileUploadException {
final FileDto document = fileService.uploadFile(documentId, file, principal);
return ResponseEntity.status(HttpStatus.ACCEPTED)
.body(document); .body(document);
} }
......
...@@ -30,5 +30,6 @@ eureka: ...@@ -30,5 +30,6 @@ eureka:
fda: fda:
mount.path: /tmp mount.path: /tmp
ready.path: /ready ready.path: /ready
gateway.endpoint: http://fda-gateway-service:9095
document.endpoint: https://test.researchdata.tuwien.ac.at document.endpoint: https://test.researchdata.tuwien.ac.at
dev.token: "${TOKEN}" dev.token: "${TOKEN}"
\ No newline at end of file
...@@ -7,7 +7,7 @@ spring: ...@@ -7,7 +7,7 @@ spring:
username: postgres username: postgres
password: postgres password: postgres
jpa: jpa:
show-sql: false show-sql: true
database-platform: org.hibernate.dialect.PostgreSQLDialect database-platform: org.hibernate.dialect.PostgreSQLDialect
hibernate: hibernate:
ddl-auto: validate ddl-auto: validate
...@@ -16,7 +16,6 @@ spring: ...@@ -16,7 +16,6 @@ spring:
name: fda-document-service name: fda-document-service
cloud: cloud:
loadbalancer.ribbon.enabled: false loadbalancer.ribbon.enabled: false
springdoc.swagger-ui.enabled: true
server.port: 9099 server.port: 9099
logging: logging:
pattern.console: "%d %highlight(%-5level) %msg%n" pattern.console: "%d %highlight(%-5level) %msg%n"
...@@ -31,5 +30,6 @@ eureka: ...@@ -31,5 +30,6 @@ eureka:
fda: fda:
mount.path: /tmp mount.path: /tmp
ready.path: ./ready ready.path: ./ready
gateway.endpoint: http://localhost:9095
document.endpoint: https://test.researchdata.tuwien.ac.at document.endpoint: https://test.researchdata.tuwien.ac.at
dev.token: "${TOKEN}" dev.token: "${TOKEN}"
\ No newline at end of file
...@@ -2,6 +2,8 @@ package at.tuwien; ...@@ -2,6 +2,8 @@ package at.tuwien;
import at.tuwien.api.document.metadata.*; import at.tuwien.api.document.metadata.*;
import at.tuwien.api.document.record.*; import at.tuwien.api.document.record.*;
import at.tuwien.api.user.UserDetailsDto;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.TestPropertySource;
import java.time.Instant; import java.time.Instant;
...@@ -11,30 +13,58 @@ import java.util.List; ...@@ -11,30 +13,58 @@ import java.util.List;
@TestPropertySource(locations = "classpath:application.properties") @TestPropertySource(locations = "classpath:application.properties")
public abstract class BaseUnitTest { public abstract class BaseUnitTest {
public final static Long USER_1_ID = 1L;
public final static String USER_1_USERNAME = "junit"; public final static String USER_1_USERNAME = "junit";
public final static UserDetails USER_1_DETAILS = UserDetailsDto.builder()
.id(USER_1_ID)
.username(USER_1_USERNAME)
.build();
public final static AccessTypeDto DOCUMENT_1_RECORD_TYPE = AccessTypeDto.PUBLIC; public final static AccessTypeDto DOCUMENT_1_RECORD_TYPE = AccessTypeDto.PUBLIC;
public final static FileTypeDto DOCUMENT_1_FILE_TYPE = FileTypeDto.PUBLIC; public final static FileTypeDto DOCUMENT_1_FILE_TYPE = FileTypeDto.PUBLIC;
public final static AccessTypeDto DOCUMENT_2_RECORD_TYPE = AccessTypeDto.PUBLIC;
public final static FileTypeDto DOCUMENT_2_FILE_TYPE = FileTypeDto.RESTRICTED;
public final static AccessOptionsDto DOCUMENT_1_ACCESS_OPTIONS = AccessOptionsDto.builder() public final static AccessOptionsDto DOCUMENT_1_ACCESS_OPTIONS = AccessOptionsDto.builder()
.record(DOCUMENT_1_RECORD_TYPE) .record(DOCUMENT_1_RECORD_TYPE)
.files(DOCUMENT_1_FILE_TYPE) .files(DOCUMENT_1_FILE_TYPE)
.build(); .build();
public final static Boolean DOCUMENT_1_FILES_ENABLED = true; public final static AccessOptionsDto DOCUMENT_2_ACCESS_OPTIONS = AccessOptionsDto.builder()
.record(DOCUMENT_2_RECORD_TYPE)
.files(DOCUMENT_2_FILE_TYPE)
.build();
public final static Boolean DOCUMENT_1_FILES_ENABLED = false;
public final static Boolean DOCUMENT_2_FILES_ENABLED = true;
public final static FilesOptionsDto DOCUMENT_1_FILES_OPTIONS = FilesOptionsDto.builder() public final static FilesOptionsDto DOCUMENT_1_FILES_OPTIONS = FilesOptionsDto.builder()
.enabled(DOCUMENT_1_FILES_ENABLED) .enabled(DOCUMENT_1_FILES_ENABLED)
.build(); .build();
public final static String DOCUMENT_1_TITLE = "Test Draft"; public final static FilesOptionsDto DOCUMENT_2_FILES_OPTIONS = FilesOptionsDto.builder()
.enabled(DOCUMENT_2_FILES_ENABLED)
.build();
public final static String DOCUMENT_1_TITLE = "Public Test-Record";
public final static String DOCUMENT_1_RESOURCE_TYPE_TYPE = "other"; public final static String DOCUMENT_1_RESOURCE_TYPE_TYPE = "other";
public final static Date DOCUMENT_1_PUBLICATION_DATE = Date.from(Instant.now()); public final static Date DOCUMENT_1_PUBLICATION_DATE = Date.from(Instant.now());
public final static String DOCUMENT_2_TITLE = "Restricted Test-Record";
public final static String DOCUMENT_2_RESOURCE_TYPE_TYPE = "other";
public final static Date DOCUMENT_2_PUBLICATION_DATE = Date.from(Instant.now());
public final static ResourceTypeDto DOCUMENT_1_RESOURCE_TYPE = ResourceTypeDto.builder() public final static ResourceTypeDto DOCUMENT_1_RESOURCE_TYPE = ResourceTypeDto.builder()
.id(DOCUMENT_1_RESOURCE_TYPE_TYPE) .id(DOCUMENT_1_RESOURCE_TYPE_TYPE)
.build(); .build();
public final static ResourceTypeDto DOCUMENT_2_RESOURCE_TYPE = ResourceTypeDto.builder()
.id(DOCUMENT_2_RESOURCE_TYPE_TYPE)
.build();
public final static String IDENTIFIER_1_IDENTIFIER = "0000-0003-4216-302X"; public final static String IDENTIFIER_1_IDENTIFIER = "0000-0003-4216-302X";
public final static IdentifierTypeDto IDENTIFIER_1_TYPE = IdentifierTypeDto.ORCID; public final static IdentifierTypeDto IDENTIFIER_1_TYPE = IdentifierTypeDto.ORCID;
...@@ -72,10 +102,23 @@ public abstract class BaseUnitTest { ...@@ -72,10 +102,23 @@ public abstract class BaseUnitTest {
.creators(List.of(CREATOR_1)) .creators(List.of(CREATOR_1))
.build(); .build();
public final static MetadataDto DOCUMENT_2_METADATA = MetadataDto.builder()
.title(DOCUMENT_2_TITLE)
.resourceType(DOCUMENT_2_RESOURCE_TYPE)
.publicationDate(DOCUMENT_2_PUBLICATION_DATE)
.creators(List.of(CREATOR_1))
.build();
public final static CreateDraftDto DOCUMENT_1_CREATE_DRAFT = CreateDraftDto.builder() public final static CreateDraftDto DOCUMENT_1_CREATE_DRAFT = CreateDraftDto.builder()
.access(DOCUMENT_1_ACCESS_OPTIONS) .access(DOCUMENT_1_ACCESS_OPTIONS)
.files(DOCUMENT_1_FILES_OPTIONS) .files(DOCUMENT_1_FILES_OPTIONS)
.metadata(DOCUMENT_1_METADATA) .metadata(DOCUMENT_1_METADATA)
.build(); .build();
public final static CreateDraftDto DOCUMENT_2_CREATE_DRAFT = CreateDraftDto.builder()
.access(DOCUMENT_2_ACCESS_OPTIONS)
.files(DOCUMENT_2_FILES_OPTIONS)
.metadata(DOCUMENT_2_METADATA)
.build();
} }
package at.tuwien.endpoint; package at.tuwien.endpoint;
import at.tuwien.BaseUnitTest; import at.tuwien.BaseUnitTest;
import at.tuwien.api.document.record.CreateDraftDto;
import at.tuwien.api.document.record.RecordDto;
import at.tuwien.endpoints.DocumentEndpoint;
import at.tuwien.exception.DraftRecordCreateException;
import at.tuwien.gateway.AuthenticationServiceGateway;
import org.apache.http.auth.BasicUserPrincipal;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
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.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.security.Principal;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
@SpringBootTest @SpringBootTest
public class DocumentEndpointUnitTest extends BaseUnitTest { public class DocumentEndpointUnitTest extends BaseUnitTest {
@Autowired
private DocumentEndpoint documentEndpoint;
@MockBean
private AuthenticationServiceGateway authenticationServiceGateway;
@Test
@WithMockUser(username = USER_1_USERNAME, authorities = {"ROLE_RESEARCHER"})
public void create_succeed() throws DraftRecordCreateException {
final CreateDraftDto request = DOCUMENT_1_CREATE_DRAFT;
final Principal principal = new BasicUserPrincipal(USER_1_USERNAME);
/* mock */
when(authenticationServiceGateway.validate(anyString()))
.thenReturn(USER_1_DETAILS);
/* test */
final ResponseEntity<RecordDto> response = documentEndpoint.create(request, principal);
assertEquals(HttpStatus.CREATED, response.getStatusCode());
}
} }
...@@ -2,7 +2,7 @@ package at.tuwien.service; ...@@ -2,7 +2,7 @@ package at.tuwien.service;
import at.tuwien.BaseUnitTest; import at.tuwien.BaseUnitTest;
import at.tuwien.api.document.record.CreateDraftDto; import at.tuwien.api.document.record.CreateDraftDto;
import at.tuwien.api.document.record.DraftDto; import at.tuwien.api.document.record.RecordDto;
import at.tuwien.exception.DraftRecordCreateException; import at.tuwien.exception.DraftRecordCreateException;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.apache.http.auth.BasicUserPrincipal; import org.apache.http.auth.BasicUserPrincipal;
...@@ -35,7 +35,7 @@ public class DocumentServiceIntegrationTest extends BaseUnitTest { ...@@ -35,7 +35,7 @@ public class DocumentServiceIntegrationTest extends BaseUnitTest {
/* mock */ /* mock */
/* test */ /* test */
final DraftDto response = documentService.create(request, principal); final RecordDto response = documentService.create(request, principal);
assertEquals(DOCUMENT_1_TITLE, response.getMetadata().getTitle()); assertEquals(DOCUMENT_1_TITLE, response.getMetadata().getTitle());
} }
...@@ -47,8 +47,8 @@ public class DocumentServiceIntegrationTest extends BaseUnitTest { ...@@ -47,8 +47,8 @@ public class DocumentServiceIntegrationTest extends BaseUnitTest {
/* mock */ /* mock */
/* test */ /* test */
final DraftDto document = documentService.create(request, principal); final RecordDto document = documentService.create(request, principal);
final DraftDto response = documentService.reserveDoi(document.getId(), principal); final RecordDto response = documentService.reserveDoi(document.getId(), principal);
assertNotNull(response.getPids().getDoi()); assertNotNull(response.getPids().getDoi());
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment