diff --git a/eva-lib/src/main/java/uk/ac/ebi/eva/lib/MongoConfiguration.java b/eva-lib/src/main/java/uk/ac/ebi/eva/lib/MongoConfiguration.java index cf5a84eff..f88b4c685 100644 --- a/eva-lib/src/main/java/uk/ac/ebi/eva/lib/MongoConfiguration.java +++ b/eva-lib/src/main/java/uk/ac/ebi/eva/lib/MongoConfiguration.java @@ -62,6 +62,11 @@ public String mongoCollectionsAnnotationMetadata() { return dbCollectionsProperties.getAnnotationMetadata(); } + @Bean + public String mongoCollectionsDefaultLocusRangeMetadata() { + return dbCollectionsProperties.getDefaultLocusRangeMetadata(); + } + @Bean public String mongoCollectionsAnnotations() { return dbCollectionsProperties.getAnnotations(); diff --git a/eva-lib/src/main/java/uk/ac/ebi/eva/lib/configuration/DbCollectionsProperties.java b/eva-lib/src/main/java/uk/ac/ebi/eva/lib/configuration/DbCollectionsProperties.java index 3ef32658e..d95ac7d33 100644 --- a/eva-lib/src/main/java/uk/ac/ebi/eva/lib/configuration/DbCollectionsProperties.java +++ b/eva-lib/src/main/java/uk/ac/ebi/eva/lib/configuration/DbCollectionsProperties.java @@ -39,6 +39,10 @@ public class DbCollectionsProperties { @NotNull private String annotationMetadata; + @Size(min = 1) + @NotNull + private String defaultLocusRangeMetadata; + @Size(min = 1) @NotNull private String annotations; @@ -67,10 +71,18 @@ public String getAnnotationMetadata() { return annotationMetadata; } + public String getDefaultLocusRangeMetadata() { + return defaultLocusRangeMetadata; + } + public void setAnnotationMetadata(String annotationMetadata) { this.annotationMetadata = annotationMetadata; } + public void setDefaultLocusRangeMetadata(String defaultLocusRangeMetadata) { + this.defaultLocusRangeMetadata = defaultLocusRangeMetadata; + } + public String getAnnotations() { return annotations; } diff --git a/eva-server/src/main/java/uk/ac/ebi/eva/server/ws/DefaultLocusRangeMetadataWSServer.java b/eva-server/src/main/java/uk/ac/ebi/eva/server/ws/DefaultLocusRangeMetadataWSServer.java new file mode 100644 index 000000000..ddf10c801 --- /dev/null +++ b/eva-server/src/main/java/uk/ac/ebi/eva/server/ws/DefaultLocusRangeMetadataWSServer.java @@ -0,0 +1,55 @@ +/* + * 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 io.swagger.annotations.Api; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; +import uk.ac.ebi.eva.commons.core.models.DefaultLocusRangeMetadata; +import uk.ac.ebi.eva.commons.mongodb.services.DefaultLocusRangeMetadataService; +import uk.ac.ebi.eva.lib.eva_utils.DBAdaptorConnector; +import uk.ac.ebi.eva.lib.eva_utils.MultiMongoDbFactory; +import uk.ac.ebi.eva.lib.utils.QueryResponse; +import uk.ac.ebi.eva.lib.utils.QueryResult; + +import javax.servlet.http.HttpServletResponse; +import java.util.List; + +@RestController +@RequestMapping(value = "/v1/default-locus-range", produces = "application/json") +@Api(tags = {"default-locus-range"}) +public class DefaultLocusRangeMetadataWSServer extends EvaWSServer { + + @Autowired + private DefaultLocusRangeMetadataService service; + + @RequestMapping(value = "", method = RequestMethod.GET) + @ResponseBody + public QueryResponse getDefaultLocusRangeMetadata(@RequestParam(name = "species") String species, + HttpServletResponse response) { + if (species.isEmpty()) { + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + return setQueryResponse("Please specify a species"); + } + + MultiMongoDbFactory.setDatabaseNameForCurrentThread(DBAdaptorConnector.getDBName(species)); + List defaultLocusRangeMetadataList = + service.findAllByOrderByChromosomeAscStartAscEndAsc(); + QueryResult queryResult = buildQueryResult(defaultLocusRangeMetadataList); + return setQueryResponse(queryResult); + } + +} diff --git a/eva-server/src/test/java/uk/ac/ebi/eva/server/ws/DefaultLocusRangeMetadataWSServerTest.java b/eva-server/src/test/java/uk/ac/ebi/eva/server/ws/DefaultLocusRangeMetadataWSServerTest.java new file mode 100644 index 000000000..7cb5b9f51 --- /dev/null +++ b/eva-server/src/test/java/uk/ac/ebi/eva/server/ws/DefaultLocusRangeMetadataWSServerTest.java @@ -0,0 +1,81 @@ +/* + * 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 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.core.ParameterizedTypeReference; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; +import uk.ac.ebi.eva.commons.core.models.DefaultLocusRangeMetadata; +import uk.ac.ebi.eva.commons.mongodb.services.DefaultLocusRangeMetadataService; +import uk.ac.ebi.eva.lib.utils.QueryResponse; +import uk.ac.ebi.eva.lib.utils.QueryResult; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.BDDMockito.given; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class DefaultLocusRangeMetadataWSServerTest { + + private List defaultLocusRangeMetadataList; + + @Autowired + private TestRestTemplate restTemplate; + + @MockBean + private DefaultLocusRangeMetadataService service; + + @Before + public void setUp() throws Exception { + defaultLocusRangeMetadataList = Arrays.asList(new DefaultLocusRangeMetadata("1", 1, 1000000)); + + given(service.findAllByOrderByChromosomeAscStartAscEndAsc()).willReturn(defaultLocusRangeMetadataList); + } + + @Test + public void testDefaultLocusRangeMetadata() { + String url = "/v1/default-locus-range?species=mmusculus_grcm38"; + ResponseEntity>> response = restTemplate.exchange( + url, HttpMethod.GET, null, + new ParameterizedTypeReference>>() { + }); + assertEquals(HttpStatus.OK, response.getStatusCode()); + + QueryResponse> queryResponse = response.getBody(); + assertEquals(1, queryResponse.getResponse().size()); + + List results = queryResponse.getResponse().get(0).getResult(); + assertEquals(defaultLocusRangeMetadataList.size(), results.size()); + + for (DefaultLocusRangeMetadata result : results) { + assertTrue(defaultLocusRangeMetadataList.contains(result)); + } + } + +} \ No newline at end of file diff --git a/eva-server/src/test/resources/application.properties b/eva-server/src/test/resources/application.properties index dcd8fce01..b80688b11 100644 --- a/eva-server/src/test/resources/application.properties +++ b/eva-server/src/test/resources/application.properties @@ -7,5 +7,6 @@ spring.data.mongodb.read-preference= db.collection-names.files=testFiles db.collection-names.variants=testVariants db.collection-names.annotation-metadata=testMetadata +db.collection-names.default-locus-range-metadata=testDefaultLocusRange db.collection-names.annotations=testAnnotations db.collection-names.features=testFeatures