Skip to content

Commit

Permalink
saving metadata json (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
nitin-ebi authored May 8, 2024
1 parent 6b1b342 commit 8b1d94f
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 27 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>io.hypersistence</groupId>
<artifactId>hypersistence-utils-hibernate-55</artifactId>
<version>3.7.5</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public Submission stripUserDetails(Submission submission) {
Submission responseSubmission = new Submission(submission.getSubmissionId());
responseSubmission.setStatus(submission.getStatus());
responseSubmission.setUploadUrl(submission.getUploadUrl());

responseSubmission.setInitiationTime(submission.getInitiationTime());
responseSubmission.setUploadedTime(submission.getUploadedTime());
responseSubmission.setCompletionTime(submission.getCompletionTime());
return responseSubmission;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.ac.ebi.eva.submission.controller.submissionws;

import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
Expand All @@ -10,13 +11,15 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import uk.ac.ebi.eva.submission.controller.BaseController;
import uk.ac.ebi.eva.submission.entity.Submission;
import uk.ac.ebi.eva.submission.entity.SubmissionAccount;
import uk.ac.ebi.eva.submission.exception.RequiredFieldsMissingException;
import uk.ac.ebi.eva.submission.model.SubmissionStatus;
import uk.ac.ebi.eva.submission.service.LsriTokenService;
import uk.ac.ebi.eva.submission.service.SubmissionService;
Expand Down Expand Up @@ -81,15 +84,20 @@ public ResponseEntity<?> initiateSubmission(@RequestHeader("Authorization") Stri
})
@PutMapping("submission/{submissionId}/uploaded")
public ResponseEntity<?> markSubmissionUploaded(@RequestHeader("Authorization") String bearerToken,
@PathVariable("submissionId") String submissionId) {
@PathVariable("submissionId") String submissionId,
@RequestBody JsonNode metadataJson) {
SubmissionAccount submissionAccount = this.getSubmissionAccount(bearerToken);
if (Objects.isNull(submissionAccount) || !submissionService.checkUserHasAccessToSubmission(submissionAccount, submissionId)) {
return new ResponseEntity<>("Unauthorized", HttpStatus.UNAUTHORIZED);
}

Submission submission = this.submissionService.markSubmissionUploaded(submissionId);
submissionService.sendMailNotificationForStatusUpdate(submissionAccount, submissionId, SubmissionStatus.UPLOADED, true);
return new ResponseEntity<>(stripUserDetails(submission), HttpStatus.OK);
try {
Submission submission = this.submissionService.uploadMetadataJsonAndMarkUploaded(submissionId, metadataJson);
submissionService.sendMailNotificationForStatusUpdate(submissionAccount, submissionId, SubmissionStatus.UPLOADED, true);
return new ResponseEntity<>(stripUserDetails(submission), HttpStatus.OK);
} catch (RequiredFieldsMissingException ex) {
return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST);
}
}

@Operation(summary = "Given a submission id, this endpoint retrieves the current status of a submission")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package uk.ac.ebi.eva.submission.entity;

import com.fasterxml.jackson.databind.JsonNode;
import io.hypersistence.utils.hibernate.type.json.JsonType;
import org.hibernate.annotations.TypeDef;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
Expand All @@ -9,8 +13,8 @@

@Entity
@Table(schema = "eva_submissions", name = "submission_details")
@TypeDef(typeClass = JsonType.class, defaultForType = JsonNode.class)
public class SubmissionDetails {

@Id
@Column(name = "submission_id")
private String submissionId;
Expand All @@ -19,18 +23,20 @@ public class SubmissionDetails {
@PrimaryKeyJoinColumn(name = "submission_id", referencedColumnName = "submission_id")
private Submission submission;

@Column(nullable = false, name = "project_alias")
private String projectAlias;
@Column(nullable = false, name = "project_title")
private String projectTitle;

@Column(nullable = false, name = "description")
private String description;
@Column(nullable = false, name = "project_description")
private String projectDescription;

public SubmissionDetails() {
@Column(columnDefinition = "jsonb", name = "metadata_json", nullable = false)
private JsonNode metadataJson;

public SubmissionDetails() {
}

public SubmissionDetails(Submission submission) {
this.submission = submission;
public SubmissionDetails(String submissionId) {
this.submissionId = submissionId;
}

public String getSubmissionId() {
Expand All @@ -41,19 +47,31 @@ public Submission getSubmission() {
return submission;
}

public String getProjectAlias() {
return projectAlias;
public void setSubmission(Submission submission) {
this.submission = submission;
}

public String getProjectTitle() {
return projectTitle;
}

public void setProjectTitle(String projectTitle) {
this.projectTitle = projectTitle;
}

public String getProjectDescription() {
return projectDescription;
}

public void setProjectAlias(String projectAlias) {
this.projectAlias = projectAlias;
public void setProjectDescription(String projectDescription) {
this.projectDescription = projectDescription;
}

public String getDescription() {
return description;
public JsonNode getMetadataJson() {
return metadataJson;
}

public void setDescription(String description) {
this.description = description;
public void setMetadataJson(JsonNode metadataJson) {
this.metadataJson = metadataJson;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package uk.ac.ebi.eva.submission.exception;

public class RequiredFieldsMissingException extends RuntimeException {
public RequiredFieldsMissingException(String msg) {
super(msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
import uk.ac.ebi.eva.submission.entity.SubmissionDetails;

public interface SubmissionDetailsRepository extends CrudRepository<SubmissionDetails, String> {
SubmissionDetails findBySubmissionId(String submissionId);
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package uk.ac.ebi.eva.submission.service;

import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import uk.ac.ebi.eva.submission.entity.Submission;
import uk.ac.ebi.eva.submission.entity.SubmissionAccount;
import uk.ac.ebi.eva.submission.entity.SubmissionDetails;
import uk.ac.ebi.eva.submission.exception.RequiredFieldsMissingException;
import uk.ac.ebi.eva.submission.exception.SubmissionDoesNotExistException;
import uk.ac.ebi.eva.submission.model.SubmissionStatus;
import uk.ac.ebi.eva.submission.repository.SubmissionAccountRepository;
import uk.ac.ebi.eva.submission.repository.SubmissionDetailsRepository;
import uk.ac.ebi.eva.submission.repository.SubmissionRepository;
import uk.ac.ebi.eva.submission.util.EmailNotificationHelper;
import uk.ac.ebi.eva.submission.util.HTMLHelper;
import uk.ac.ebi.eva.submission.util.MailSender;

import java.time.LocalDateTime;
Expand All @@ -19,6 +21,9 @@

@Service
public class SubmissionService {
private static final String PROJECT = "project";
private static final String TITLE = "title";
private static final String DESCRIPTION = "description";

private final SubmissionRepository submissionRepository;

Expand Down Expand Up @@ -68,7 +73,23 @@ public Submission initiateSubmission(SubmissionAccount submissionAccount) {
return submissionRepository.save(submission);
}

public Submission markSubmissionUploaded(String submissionId) {
public Submission uploadMetadataJsonAndMarkUploaded(String submissionId, JsonNode metadataJson) {
SubmissionDetails submissionDetails = new SubmissionDetails(submissionId);
try {
JsonNode project = metadataJson.get(PROJECT);
String projectTitle = project.get(TITLE).asText();
String projectDescription = project.get(DESCRIPTION).asText();

submissionDetails.setProjectTitle(projectTitle);
submissionDetails.setProjectDescription(projectDescription);
} catch (Exception e) {
throw new RequiredFieldsMissingException("Required fields project title and project description " +
"could not be found in metadata json");
}

submissionDetails.setMetadataJson(metadataJson);
submissionDetailsRepository.save(submissionDetails);

Submission submission = submissionRepository.findBySubmissionId(submissionId);
submission.setStatus(SubmissionStatus.UPLOADED.toString());
submission.setUploadedTime(LocalDateTime.now());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import org.testcontainers.junit.jupiter.Testcontainers;
import uk.ac.ebi.eva.submission.entity.Submission;
import uk.ac.ebi.eva.submission.entity.SubmissionAccount;
import uk.ac.ebi.eva.submission.entity.SubmissionDetails;
import uk.ac.ebi.eva.submission.model.SubmissionStatus;
import uk.ac.ebi.eva.submission.repository.SubmissionAccountRepository;
import uk.ac.ebi.eva.submission.repository.SubmissionDetailsRepository;
import uk.ac.ebi.eva.submission.repository.SubmissionRepository;
import uk.ac.ebi.eva.submission.service.GlobusDirectoryProvisioner;
import uk.ac.ebi.eva.submission.service.GlobusTokenRefreshService;
Expand Down Expand Up @@ -65,6 +67,9 @@ public class SubmissionWSIntegrationTest {
@Autowired
private SubmissionAccountRepository submissionAccountRepository;

@Autowired
private SubmissionDetailsRepository submissionDetailsRepository;

@MockBean
private WebinTokenService webinTokenService;

Expand Down Expand Up @@ -230,18 +235,21 @@ public void testSubmissionGetStatus() throws Exception {

@Test
@Transactional
public void testMarkSubmissionUploaded() throws Exception {
public void testUploadMetadataJsonAndMarkUploadedd() throws Exception {
String userToken = "webinUserToken";
SubmissionAccount submissionAccount = getWebinUserAccount();
when(webinTokenService.getWebinUserAccountFromToken(anyString())).thenReturn(submissionAccount);
doNothing().when(mailSender).sendEmail(anyString(), anyString(), anyString());

String submissionId = createNewSubmissionEntry(submissionAccount);

String projectTitle = "test_project_title";
String projectDescription = "test_project_description";
String metadataJson = "{\"project\": {\"title\":\"" + projectTitle + "\",\"description\":\"" + projectDescription + "\"}}";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setBearerAuth(userToken);
mvc.perform(put("/v1/submission/" + submissionId + "/uploaded")
.headers(httpHeaders)
.content(metadataJson)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());

Expand All @@ -251,6 +259,38 @@ public void testMarkSubmissionUploaded() throws Exception {
assertThat(submission.getStatus()).isEqualTo(SubmissionStatus.UPLOADED.toString());
assertThat(submission.getUploadedTime()).isNotNull();
assertThat(submission.getCompletionTime()).isNull();

SubmissionDetails submissionDetails = submissionDetailsRepository.findBySubmissionId(submissionId);
assertThat(submissionDetails).isNotNull();
assertThat(submissionDetails.getSubmissionId()).isEqualTo(submissionId);
assertThat(submissionDetails.getProjectTitle()).isEqualTo(projectTitle);
assertThat(submissionDetails.getProjectDescription()).isEqualTo(projectDescription);
assertThat(submissionDetails.getMetadataJson()).isNotNull();
assertThat(submissionDetails.getMetadataJson().get("project").get("title").asText()).isEqualTo(projectTitle);
assertThat(submissionDetails.getMetadataJson().get("project").get("title").asText()).isEqualTo(projectTitle);
}

@Test
@Transactional
public void testRequiredMetadataFieldsNotProvided() throws Exception {
String userToken = "webinUserToken";
SubmissionAccount submissionAccount = getWebinUserAccount();
when(webinTokenService.getWebinUserAccountFromToken(anyString())).thenReturn(submissionAccount);
doNothing().when(mailSender).sendEmail(anyString(), anyString(), anyString());

String submissionId = createNewSubmissionEntry(submissionAccount);
String projectDescription = "test_project_description";
String metadataJson = "{\"project\": {\"description\":\"" + projectDescription + "\"}}";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setBearerAuth(userToken);

mvc.perform(put("/v1/submission/" + submissionId + "/uploaded")
.headers(httpHeaders)
.content(metadataJson)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(content().string("Required fields project title and project description " +
"could not be found in metadata json"));
}

@Test
Expand All @@ -261,11 +301,15 @@ public void testSubmissionDoesNotExistException() throws Exception {
when(webinTokenService.getWebinUserAccountFromToken(anyString())).thenReturn(submissionAccount);

String submissionId = "wrong_submission_id";
String projectTitle = "test_project_title";
String projectDescription = "test_project_description";
String metadataJson = "{\"project\": {\"title\":\"" + projectTitle + "\",\"description\":\"" + projectDescription + "\"}}";

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setBearerAuth(userToken);
mvc.perform(put("/v1/submission/" + submissionId + "/uploaded")
.headers(httpHeaders)
.content(metadataJson)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound())
.andExpect(content().string("Given submission with id " + submissionId + " does not exist"));
Expand All @@ -274,7 +318,6 @@ public void testSubmissionDoesNotExistException() throws Exception {
@Test
@Transactional
public void testMarkSubmissionStatusCorrect() throws Exception {
String userToken = "webinUserToken";
SubmissionAccount submissionAccount = getWebinUserAccount();
when(webinTokenService.getWebinUserAccountFromToken(anyString())).thenReturn(submissionAccount);

Expand All @@ -298,7 +341,6 @@ public void testMarkSubmissionStatusCorrect() throws Exception {
@Test
@Transactional
public void testMarkSubmissionStatusWrong() throws Exception {
String userToken = "webinUserToken";
SubmissionAccount submissionAccount = getWebinUserAccount();
when(webinTokenService.getWebinUserAccountFromToken(anyString())).thenReturn(submissionAccount);

Expand Down

0 comments on commit 8b1d94f

Please sign in to comment.