Skip to content

Commit

Permalink
Assign VM after un-publshing to the owner
Browse files Browse the repository at this point in the history
Make sure that we unpublish public virtual study. Fail otherwise.
  • Loading branch information
forus committed Jul 10, 2024
1 parent b99e106 commit 84249a3
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
3 changes: 2 additions & 1 deletion docs/Create-And-Publish-Virtual-Study.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ Specifying the `pmid` enables a link to the PubMed page of the study.
## Un-publish Virtual Study

To un-publish a virtual study, you need to supply the publisher API key in the `X-PUBLISHER-API-KEY` header.
After un-publishing, the virtual study is still accessible by the link, but it will be unlisted from the list of public virtual studies and not shown on the landing page anymore.
After un-publishing, virtual study will no longer be displayed in the `Public Virtual Studies` section on the landing page.
However, it reappears in the `My Virtual Studies` section for the owner.

Here is the command to un-publish a virtual study:
```shell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;

@Controller
Expand Down Expand Up @@ -72,15 +73,21 @@ public ResponseEntity<Void> publishVirtualStudy(

@DeleteMapping("/{id}")
@ApiResponse(responseCode = "200", description = "OK")
public ResponseEntity<Void> retractVirtualStudy(
public ResponseEntity<Void> unPublishVirtualStudy(
@PathVariable String id,
@RequestHeader(value = "X-PUBLISHER-API-KEY") String providedPublisherApiKey
) {
ensureProvidedPublisherApiKeyCorrect(providedPublisherApiKey);
sessionServiceRequestHandler.softRemoveVirtualStudy(id);
unPublishVirtualStudy(id);
return ResponseEntity.ok().build();
}

/**
* Publishes virtual study optionally updating metadata fields
* @param id - id of public virtual study to publish
* @param typeOfCancerId - if specified (not null) update type of cancer of published virtual study
* @param pmid - if specified (not null) update PubMed ID of published virtual study
*/
private void publishVirtualStudy(String id, String typeOfCancerId, String pmid) {
VirtualStudy virtualStudyDataToPublish = sessionServiceRequestHandler.getVirtualStudyById(id);
VirtualStudyData virtualStudyData = virtualStudyDataToPublish.getData();
Expand All @@ -89,6 +96,24 @@ private void publishVirtualStudy(String id, String typeOfCancerId, String pmid)
sessionServiceRequestHandler.updateVirtualStudy(virtualStudyDataToPublish);
}

/**
* Un-publish virtual study
* @param id - id of public virtual study to un-publish
*/
private void unPublishVirtualStudy(String id) {
VirtualStudy virtualStudyToUnPublish = sessionServiceRequestHandler.getVirtualStudyById(id);
if (virtualStudyToUnPublish == null) {
throw new NoSuchElementException("The virtual study with id=" + id + " has not been found in the public list.");
}
VirtualStudyData virtualStudyData = virtualStudyToUnPublish.getData();
Set<String> users = virtualStudyData.getUsers();
if (users == null || users.isEmpty() || !users.contains(ALL_USERS)) {
throw new NoSuchElementException("The virtual study with id=" + id + " has not been found in the public list.");
}
virtualStudyData.setUsers(Set.of(virtualStudyData.getOwner()));
sessionServiceRequestHandler.updateVirtualStudy(virtualStudyToUnPublish);
}

private void ensureProvidedPublisherApiKeyCorrect(String providedPublisherApiKey) {
if (requiredPublisherApiKey.isBlank()
|| !requiredPublisherApiKey.equals(providedPublisherApiKey)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.Iterator;
import java.util.NoSuchElementException;


// TODO
Expand Down Expand Up @@ -207,4 +208,9 @@ public ResponseEntity<ErrorResponse> handleBadSqlGrammar(BadSqlGrammarException
HttpStatus.INTERNAL_SERVER_ERROR
);
}

@ExceptionHandler(NoSuchElementException.class)
public ResponseEntity<ErrorResponse> handleNoSuchElementException(NoSuchElementException ex) {
return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ public void test2CreateVirtualStudy() {
virtualStudyId = savedVs.getId();
}

@Test
public void test2_1UnPublishVirtualStudyFails() {
ResponseEntity<Object> response = restTemplate.exchange(
CBIO_URL + "/api/public_virtual_studies/" + virtualStudyId,
HttpMethod.DELETE,
new HttpEntity<>(null, validKeyContainingHeaders),
Object.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}

@Test
public void test3PublishVirtualStudy() {
String url = CBIO_URL + "/api/public_virtual_studies/" + virtualStudyId + "?typeOfCancerId=acc&pmid=12345";
Expand Down Expand Up @@ -158,28 +168,28 @@ public void test4ListJustPublishedStudy() {

@Test
public void test5UnpublishVirtualStudy() {
ResponseEntity<Object> response5 = restTemplate.exchange(
ResponseEntity<Void> response5 = restTemplate.exchange(
CBIO_URL + "/api/public_virtual_studies/" + virtualStudyId,
HttpMethod.DELETE,
null,
Object.class);
Void.class);
assertThat(response5.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);

response5 = restTemplate.exchange(
CBIO_URL + "/api/public_virtual_studies/" + virtualStudyId,
HttpMethod.DELETE,
new HttpEntity<>(null, invalidKeyContainingHeaders),
Object.class);
Void.class);
assertThat(response5.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);

response5 = restTemplate.exchange(
CBIO_URL + "/api/public_virtual_studies/" + virtualStudyId,
HttpMethod.DELETE,
new HttpEntity<>(null, validKeyContainingHeaders),
Object.class);
Void.class);
assertThat(response5.getStatusCode().is2xxSuccessful()).isTrue();
}

@Test
public void test6NoPublicVirtualStudiesAfterRemoval() {
ResponseEntity<List<VirtualStudy>> response6 = restTemplate.exchange(
Expand All @@ -192,6 +202,18 @@ public void test6NoPublicVirtualStudiesAfterRemoval() {
assertThat(response6.getBody()).isEmpty();
}

@Test
public void test7UnpublishedVirtualStudyExists() {
ResponseEntity<VirtualStudy> response = restTemplate.exchange(
CBIO_URL + "/api/session/virtual_study/" + virtualStudyId,
HttpMethod.GET,
null,
VirtualStudy.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
VirtualStudy body = response.getBody();
assertThat(body).isNotNull();
}

static VirtualStudyData createTestVsData() {
VirtualStudyData data = new VirtualStudyData();
data.setName("test virtual study name");
Expand Down

0 comments on commit 84249a3

Please sign in to comment.