-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Study ws server v2 #126
Merged
andresfsilva
merged 7 commits into
EBIvariation:master
from
junaidnz97:ArchiveWSServerV2
Aug 14, 2019
Merged
Study ws server v2 #126
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ee947cc
Added archive V2 endpoint with tests
junaidnz97 e7e1eac
Refactored ArchiveWSServerV2 to StudyWSServerV2
junaidnz97 f121258
Update eva-server/src/main/java/uk/ac/ebi/eva/server/ws/StudyWSServer…
junaidnz97 73958f2
Added parameter descriptions
2b1d69d
minor formatting change
96c06c1
Apply suggestions from code review
junaidnz97 3c5d27f
Added validation for totalPages
junaidnz97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
eva-server/src/main/java/uk/ac/ebi/eva/server/ws/StudyWSServerV2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package uk.ac.ebi.eva.server.ws; | ||
|
||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiParam; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.hateoas.Link; | ||
import org.springframework.hateoas.PagedResources; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import uk.ac.ebi.eva.commons.mongodb.entities.projections.VariantStudySummary; | ||
import uk.ac.ebi.eva.commons.mongodb.services.VariantStudySummaryService; | ||
import uk.ac.ebi.eva.lib.eva_utils.DBAdaptorConnector; | ||
import uk.ac.ebi.eva.lib.eva_utils.MultiMongoDbFactory; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; | ||
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; | ||
|
||
@RestController | ||
@RequestMapping(value = "/v2/studies", produces = "application/hal+json") | ||
@Api(tags = "studies") | ||
public class StudyWSServerV2 { | ||
|
||
@Autowired | ||
private VariantStudySummaryService variantStudySummaryService; | ||
|
||
@RequestMapping(value = "", method = RequestMethod.GET) | ||
public ResponseEntity getBrowsableStudies( | ||
@ApiParam(value = "First letter of the genus, followed by the full species name, e.g. hsapiens. " + | ||
"Allowed values can be looked up in /v1/meta/species/list/ in the field named" + | ||
" 'taxonomyCode'.", required = true) | ||
@RequestParam("species") String species, | ||
@ApiParam(value = "Encoded assembly name, e.g. grch37. Allowed values can be looked up in " + | ||
"/v1/meta/species/list/ in the field named 'assemblyCode'.", required = true) | ||
@RequestParam("assembly") String assembly, | ||
@ApiParam(value = "The number of the page that should be displayed. Starts from 0 and is an integer." + | ||
" e.g. 0") | ||
@RequestParam(required = false, defaultValue = "0") Integer pageNumber, | ||
@ApiParam(value = "The number of elements that should be displayed in a single page. e.g. 5") | ||
@RequestParam(required = false, defaultValue = "20") Integer pageSize) | ||
throws IllegalArgumentException { | ||
if (species == null || species.isEmpty()) { | ||
throw new IllegalArgumentException("Please specify a species"); | ||
} | ||
|
||
MultiMongoDbFactory.setDatabaseNameForCurrentThread(DBAdaptorConnector.getDBName(species + "_" + assembly)); | ||
|
||
int totalNumberOfResults = variantStudySummaryService.countAll(); | ||
if (totalNumberOfResults == 0) { | ||
return new ResponseEntity(new PagedResources<>(Collections.EMPTY_LIST, new PagedResources.PageMetadata | ||
(pageSize, pageNumber < 0 ? 0 : pageNumber, totalNumberOfResults)), HttpStatus.NO_CONTENT); | ||
} | ||
|
||
List<VariantStudySummary> uniqueStudies = variantStudySummaryService.findAll(pageNumber, pageSize); | ||
|
||
PagedResources.PageMetadata pageMetadata; | ||
try { | ||
pageMetadata = buildPageMetadata(pageSize, pageNumber, totalNumberOfResults); | ||
} catch (IllegalArgumentException e) { | ||
return new ResponseEntity(e.getMessage(), HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE); | ||
} | ||
|
||
PagedResources pagedResources = buildPagedResources(uniqueStudies, species, assembly, pageMetadata); | ||
return new ResponseEntity(pagedResources, HttpStatus.OK); | ||
} | ||
|
||
private PagedResources.PageMetadata buildPageMetadata(Integer pageSize, Integer pageNumber, Integer | ||
totalNumberOfResults) | ||
throws IllegalArgumentException { | ||
Long totalPages = pageSize == 0L ? 0L : (long) Math.ceil((double) totalNumberOfResults / (double) pageSize); | ||
|
||
if (pageNumber < 0 || pageNumber >= totalPages) { | ||
throw new IllegalArgumentException("For the given page size, there are " + totalPages + " page(s), so " + | ||
"the correct page range is from 0 to " + (totalPages - 1) + " (both included)."); | ||
} | ||
return new PagedResources.PageMetadata(pageSize, pageNumber, totalNumberOfResults, totalPages); | ||
} | ||
|
||
private PagedResources buildPagedResources(List<VariantStudySummary> uniqueStudies, String species, | ||
String assembly, PagedResources.PageMetadata pageMetadata) { | ||
|
||
PagedResources pagedResources = new PagedResources<>(uniqueStudies, pageMetadata); | ||
|
||
int pageNumber = (int) pageMetadata.getNumber(); | ||
int pageSize = (int) pageMetadata.getSize(); | ||
|
||
if (pageNumber > 0) { | ||
pagedResources.add(createPaginationLink(species, assembly, pageNumber - 1, pageSize, "prev")); | ||
|
||
pagedResources.add(createPaginationLink(species, assembly, 0, pageSize, "first")); | ||
} | ||
|
||
if (pageNumber < (pageMetadata.getTotalPages() - 1)) { | ||
pagedResources.add(createPaginationLink(species, assembly, pageNumber + 1, pageSize, "next")); | ||
|
||
pagedResources.add(createPaginationLink(species, assembly, (int) pageMetadata.getTotalPages() - 1, | ||
pageSize, "last")); | ||
} | ||
return pagedResources; | ||
} | ||
|
||
private Link createPaginationLink(String species, String assembly, int pageNumber, int pageSize, String linkName) { | ||
return new Link(linkTo(methodOn(StudyWSServerV2.class).getBrowsableStudies(species, assembly, | ||
pageNumber, pageSize)) | ||
.toUriComponentsBuilder() | ||
.toUriString(), linkName); | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
eva-server/src/test/java/uk/ac/ebi/eva/server/ws/StudyWSServerV2IntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* European Variation Archive (EVA) - Open-access database of all types of genetic | ||
* variation data from all species | ||
* | ||
* Copyright 2019 EMBL - European Bioinformatics Institute | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package uk.ac.ebi.eva.server.ws; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.jayway.jsonpath.Configuration; | ||
import com.jayway.jsonpath.JsonPath; | ||
import com.jayway.jsonpath.Option; | ||
import com.jayway.jsonpath.TypeRef; | ||
import com.jayway.jsonpath.spi.json.JacksonJsonProvider; | ||
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider; | ||
import com.lordofthejars.nosqlunit.annotation.UsingDataSet; | ||
import com.lordofthejars.nosqlunit.mongodb.MongoDbRule; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.data.mongodb.MongoDbFactory; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import uk.ac.ebi.eva.commons.core.models.pipeline.Variant; | ||
import uk.ac.ebi.eva.commons.mongodb.entities.projections.VariantStudySummary; | ||
import uk.ac.ebi.eva.commons.mongodb.services.VariantWithSamplesAndAnnotationsService; | ||
import uk.ac.ebi.eva.lib.Profiles; | ||
import uk.ac.ebi.eva.server.configuration.MongoRepositoryTestConfiguration; | ||
|
||
import java.net.URISyntaxException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static com.lordofthejars.nosqlunit.mongodb.MongoDbRule.MongoDbRuleBuilder.newMongoDbRule; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@Import({MongoRepositoryTestConfiguration.class}) | ||
@UsingDataSet(locations = { | ||
"/test-data/files.json" | ||
}) | ||
@ActiveProfiles(Profiles.TEST_MONGO_FACTORY) | ||
public class StudyWSServerV2IntegrationTest { | ||
|
||
private static final String TEST_DB = "test-db"; | ||
|
||
@Autowired | ||
private ApplicationContext applicationContext; | ||
|
||
@Rule | ||
public MongoDbRule mongoDbRule = newMongoDbRule().defaultSpringMongoDb(TEST_DB); | ||
|
||
@Autowired | ||
MongoDbFactory mongoDbFactory; | ||
|
||
@Autowired | ||
private TestRestTemplate restTemplate; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@Test | ||
public void testGetStudies() { | ||
String url = "/v2/studies?species=mmusculus&assembly=grcm38&pageNumber=0&pageSize=1"; | ||
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); | ||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
|
||
Configuration configuration = Configuration.defaultConfiguration() | ||
.jsonProvider(new JacksonJsonProvider()) | ||
.mappingProvider(new JacksonMappingProvider(objectMapper)) | ||
.addOptions(Option.SUPPRESS_EXCEPTIONS); | ||
|
||
List<VariantStudySummary> variantList = JsonPath.using(configuration).parse(response.getBody()) | ||
.read("$['_embedded']['variantStudySummaryList']", new TypeRef<List<VariantStudySummary>>() { | ||
}); | ||
|
||
assertEquals("PRJX00001", variantList.get(0).getStudyId()); | ||
assertEquals("Human Variation Data From dbSNP build 144", variantList.get(0).getStudyName()); | ||
assertEquals(1, variantList.size()); | ||
|
||
Integer totalNumberOfElements = JsonPath.using(configuration).parse(response.getBody()) | ||
.read("$['page']['totalElements']", Integer.class); | ||
Integer pageNumber = JsonPath.using(configuration).parse(response.getBody()) | ||
.read("$['page']['number']", Integer.class); | ||
Integer size = JsonPath.using(configuration).parse(response.getBody()) | ||
.read("$['page']['size']", Integer.class); | ||
Integer totalPages = JsonPath.using(configuration).parse(response.getBody()) | ||
.read("$['page']['totalPages']", Integer.class); | ||
|
||
assertEquals(2, totalNumberOfElements.intValue()); | ||
assertEquals(0, pageNumber.intValue()); | ||
assertEquals(1, size.intValue()); | ||
assertEquals(2, totalPages.intValue()); | ||
} | ||
|
||
@Test | ||
public void testInvalidPageRanges() { | ||
String url = "/v2/studies?species=mmusculus&assembly=grcm38&pageNumber=1000&pageSize=1"; | ||
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); | ||
assertEquals(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE, response.getStatusCode()); | ||
assertEquals("For the given page size, there are 2 page(s), so the correct page range is from 0 to 1" + | ||
" (both included).", response.getBody()); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
eva-server/src/test/java/uk/ac/ebi/eva/server/ws/StudyWSServerV2Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package uk.ac.ebi.eva.server.ws; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.jayway.jsonpath.Configuration; | ||
import com.jayway.jsonpath.JsonPath; | ||
import com.jayway.jsonpath.Option; | ||
import com.jayway.jsonpath.TypeRef; | ||
import com.jayway.jsonpath.spi.json.JacksonJsonProvider; | ||
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import uk.ac.ebi.eva.commons.mongodb.entities.projections.VariantStudySummary; | ||
import uk.ac.ebi.eva.commons.mongodb.services.VariantStudySummaryService; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Matchers.eq; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
public class StudyWSServerV2Test { | ||
|
||
private static final long PAGE_SIZE = 2; | ||
|
||
private static final long PAGE_NUMBER = 0; | ||
|
||
@Autowired | ||
private TestRestTemplate restTemplate; | ||
|
||
@MockBean | ||
private VariantStudySummaryService service; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@Before | ||
public void setUp() { | ||
List<VariantStudySummary> studies = new ArrayList<>(); | ||
VariantStudySummary study1 = new VariantStudySummary(); | ||
VariantStudySummary study2 = new VariantStudySummary(); | ||
|
||
study1.setFilesCount(1); | ||
study1.setStudyId("studyId1"); | ||
study1.setStudyName("studyName1"); | ||
studies.add(study1); | ||
|
||
study2.setFilesCount(2); | ||
study2.setStudyId("studyId2"); | ||
study2.setStudyName("studyName2"); | ||
studies.add(study2); | ||
|
||
given(service.findAll(eq(PAGE_NUMBER), eq(PAGE_SIZE))).willReturn(studies); | ||
given(service.countAll()).willReturn(2); | ||
} | ||
|
||
@Test | ||
public void testGetStudies() { | ||
String url = "/v2/studies?species=mmusculus&assembly=grcm38&pageNumber=0&pageSize=2"; | ||
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); | ||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
|
||
Configuration configuration = Configuration.defaultConfiguration() | ||
.jsonProvider(new JacksonJsonProvider()) | ||
.mappingProvider(new JacksonMappingProvider(objectMapper)) | ||
.addOptions(Option.SUPPRESS_EXCEPTIONS); | ||
|
||
List<VariantStudySummary> variantList = JsonPath.using(configuration).parse(response.getBody()) | ||
.read("$['_embedded']['variantStudySummaryList']", new TypeRef<List<VariantStudySummary>>() { | ||
}); | ||
assertEquals("studyId1", variantList.get(0).getStudyId()); | ||
assertEquals("studyName1", variantList.get(0).getStudyName()); | ||
assertEquals("studyId2", variantList.get(1).getStudyId()); | ||
assertEquals("studyName2", variantList.get(1).getStudyName()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
totalPages
is not being used, can you add the validation for it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added 👍